Skip to content

Commit

Permalink
Cleanup in ec
Browse files Browse the repository at this point in the history
  • Loading branch information
teschlg committed Nov 12, 2024
1 parent 692951d commit 3197417
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion doc/kryptools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@
{
"data": {
"text/plain": [
"270"
"266"
]
},
"execution_count": 29,
Expand Down
5 changes: 1 addition & 4 deletions kryptools/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ def dbl(self, x, y):

def mult(self, j: int, x, y): # Addition-subtraction ladder
"Point multiplication"
if x is None:
return None, None
if j == 0:
if j == 0 or x is None:
return None, None
if j < 0:
y = -y
Expand Down Expand Up @@ -166,7 +164,6 @@ def random(self):

def psi(self, n: int):
"""The x-part of the n'th division polynomial."""

if len(self.psi_list) < 5:
self.psi_list = [ Poly([i], ring=self.gf) for i in range(3)]
self.psi_list += [ Poly([-self.a * self.a, 12 * self.b, 6 * self.a, 0, 3], ring=self.gf) ]
Expand Down
52 changes: 34 additions & 18 deletions tests/test_ec.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import pytest
from random import randint, seed
from math import gcd
from kryptools import EC_Weierstrass
seed(0)


def test_EC():
ec = EC_Weierstrass(239, 3, 1)
assert len(list(ec)) == ec.order()
O = ec.inf() # point at infinity
assert O in ec
P = ec.random()
assert P in ec
Q = ec.random()
assert Q in ec
assert P + O == P
assert O + P == P
assert P - P == O
assert P + P == 2 * P
R = O
for i in range(5):
assert i * P == R
R += P
assert P + Q == Q + P
assert Q.order() * Q == O
k = randint(1, Q.order())
R = k * Q
assert R.dlog(Q) == k
assert len(list(ec)) == ec.order()
assert O + O == O
assert O - O == O
assert 0 * O == O
assert 3 * O == O
assert O.order() == 1
assert O.dlog(O) == 0
for _ in range(100):
P = ec.random()
assert P in ec
assert P + O == P
assert O + P == P
assert P - O == P
assert O - P == -P
assert P - P == O
assert P + P == 2 * P
Q = O
order_P = P.order()
assert order_P * P == O
for i in range(9):
assert i * P == Q
assert Q.order() == order_P // gcd(order_P, i)
Q += P
Q = ec.random()
assert Q in ec
assert P + Q == Q + P
R = ec.random()
assert R in ec
assert (P + Q) + R == P + (Q + R)
for _ in range(10):
k = randint(0, Q.order()-1)
R = k * Q
assert R.dlog(Q) == k

0 comments on commit 3197417

Please sign in to comment.