Skip to content

Commit

Permalink
Merge 34ae7a0 into a776084
Browse files Browse the repository at this point in the history
  • Loading branch information
adhintz committed Jul 22, 2020
2 parents a776084 + 34ae7a0 commit f04a0f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
39 changes: 27 additions & 12 deletions src/ecdsa/ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,16 @@ def __init__(self, p, a, b, h=None):
self.__h = h

def __eq__(self, other):
if isinstance(other, CurveFp):
"""Return True if the curves are identical, False otherwise."""
return (
self.__p == other.__p
and self.__a == other.__a
and self.__b == other.__b
)
return NotImplemented
"""Return True if the curves are identical, False otherwise."""
return (
isinstance(other, CurveFp)
and self.__p == other.__p
and self.__a == other.__a
and self.__b == other.__b
)

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash((self.__p, self.__a, self.__b))
Expand Down Expand Up @@ -185,6 +187,21 @@ def __init__(self, curve, x, y, z, order=None, generator=False):
doubler = doubler.double().scale()
self.__precompute.append((doubler.x(), doubler.y()))

def __getstate__(self):
state = None
try:
self._scale_lock.reader_acquire()
state = self.__dict__.copy()
finally:
self._scale_lock.reader_release()
if state:
del state["_scale_lock"]
return state

def __setstate__(self, state):
self.__dict__.update(state)
self._scale_lock = RWLock()

def __eq__(self, other):
"""Compare two points with each-other."""
try:
Expand Down Expand Up @@ -712,8 +729,7 @@ def __add__(self, other):
p = self.__curve.p()

l = (
(other.__y - self.__y)
* numbertheory.inverse_mod(other.__x - self.__x, p)
(other.__y - self.__y) * numbertheory.inverse_mod(other.__x - self.__x, p)
) % p

x3 = (l * l - self.__x - other.__x) % p
Expand Down Expand Up @@ -779,8 +795,7 @@ def double(self):
a = self.__curve.a()

l = (
(3 * self.__x * self.__x + a)
* numbertheory.inverse_mod(2 * self.__y, p)
(3 * self.__x * self.__x + a) * numbertheory.inverse_mod(2 * self.__y, p)
) % p

x3 = (l * l - 2 * self.__x) % p
Expand Down
22 changes: 6 additions & 16 deletions src/ecdsa/test_ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,28 +121,18 @@ def test_p192(self):
# in X9.62:
d = 651056770906015076056810763456358567190100156695615665659
Q = d * self.p192
self.assertEqual(
Q.x(), 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5
)
self.assertEqual(Q.x(), 0x62B12D60690CDCF330BABAB6E69763B471F994DD702D16A5)

k = 6140507067065001063065065565667405560006161556565665656654
R = k * self.p192
self.assertEqual(
R.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD
)
self.assertEqual(
R.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835
)
self.assertEqual(R.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD)
self.assertEqual(R.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835)

u1 = 2563697409189434185194736134579731015366492496392189760599
u2 = 6266643813348617967186477710235785849136406323338782220568
temp = u1 * self.p192 + u2 * Q
self.assertEqual(
temp.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD
)
self.assertEqual(
temp.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835
)
self.assertEqual(temp.x(), 0x885052380FF147B734C330C43D39B2C4A89F29B0F749FEAD)
self.assertEqual(temp.y(), 0x9CF9FA1CBEFEFB917747A3BB29C072B9289C2547884FD835)

def test_double_infinity(self):
p1 = INFINITY
Expand Down Expand Up @@ -195,6 +185,6 @@ def test_inequality_points(self):
p = Point(c, 100, 100, 100)
self.assertNotEqual(self.g_23, p)

def test_inaquality_points_diff_types(self):
def test_inequality_points_diff_types(self):
c = CurveFp(100, -3, 100)
self.assertNotEqual(self.g_23, c)
39 changes: 18 additions & 21 deletions src/ecdsa/test_jacobi.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pickle

try:
import unittest2 as unittest
except ImportError:
Expand All @@ -6,7 +8,7 @@
import hypothesis.strategies as st
from hypothesis import given, assume, settings, example

from .ellipticcurve import Point, PointJacobi, INFINITY
from .ellipticcurve import CurveFp, Point, PointJacobi, INFINITY
from .ecdsa import generator_256, curve_256, generator_224
from .numbertheory import inverse_mod

Expand Down Expand Up @@ -303,16 +305,10 @@ def test_add_different_scale_points(self, a_mul, b_mul, new_z):
new_zz1 = new_z[1] * new_z[1] % p

a = PointJacobi(
curve_256,
a.x() * new_zz0 % p,
a.y() * new_zz0 * new_z[0] % p,
new_z[0],
curve_256, a.x() * new_zz0 % p, a.y() * new_zz0 * new_z[0] % p, new_z[0],
)
b = PointJacobi(
curve_256,
b.x() * new_zz1 % p,
b.y() * new_zz1 * new_z[1] % p,
new_z[1],
curve_256, b.x() * new_zz1 % p, b.y() * new_zz1 * new_z[1] % p, new_z[1],
)

c = a + b
Expand Down Expand Up @@ -347,12 +343,8 @@ def test_mul_add_precompute_large(self):
b = PointJacobi.from_affine(j_g * 255, True)

self.assertEqual(j_g * 256, j_g + b)
self.assertEqual(
j_g * (0xFF00 + 255 * 0xF0F0), j_g * 0xFF00 + b * 0xF0F0
)
self.assertEqual(
j_g * (0xFF00 + 255 * 0xF0F0), j_g.mul_add(0xFF00, b, 0xF0F0)
)
self.assertEqual(j_g * (0xFF00 + 255 * 0xF0F0), j_g * 0xFF00 + b * 0xF0F0)
self.assertEqual(j_g * (0xFF00 + 255 * 0xF0F0), j_g.mul_add(0xFF00, b, 0xF0F0))

def test_mul_add_to_mul(self):
j_g = PointJacobi.from_affine(generator_256)
Expand All @@ -378,9 +370,14 @@ def test_mul_add_large(self):
b = PointJacobi.from_affine(j_g * 255)

self.assertEqual(j_g * 256, j_g + b)
self.assertEqual(
j_g * (0xFF00 + 255 * 0xF0F0), j_g * 0xFF00 + b * 0xF0F0
)
self.assertEqual(
j_g * (0xFF00 + 255 * 0xF0F0), j_g.mul_add(0xFF00, b, 0xF0F0)
)
self.assertEqual(j_g * (0xFF00 + 255 * 0xF0F0), j_g * 0xFF00 + b * 0xF0F0)
self.assertEqual(j_g * (0xFF00 + 255 * 0xF0F0), j_g.mul_add(0xFF00, b, 0xF0F0))

def test_equality(self):
pj1 = PointJacobi(curve=CurveFp(23, 1, 1, 1), x=2, y=3, z=1, order=1)
pj2 = PointJacobi(curve=CurveFp(23, 1, 1, 1), x=2, y=3, z=1, order=1)
self.assertEqual(pj1, pj2)

def test_pickle(self):
pj = PointJacobi(curve=CurveFp(23, 1, 1, 1), x=2, y=3, z=1, order=1)
self.assertEqual(pickle.loads(pickle.dumps(pj)), pj)

0 comments on commit f04a0f5

Please sign in to comment.