Skip to content

Commit

Permalink
Merge 9d42c52 into a776084
Browse files Browse the repository at this point in the history
  • Loading branch information
adhintz committed Jul 22, 2020
2 parents a776084 + 9d42c52 commit 4244a42
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
33 changes: 25 additions & 8 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
2 changes: 1 addition & 1 deletion src/ecdsa/test_ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,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)
13 changes: 12 additions & 1 deletion 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 @@ -384,3 +386,12 @@ def test_mul_add_large(self):
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 4244a42

Please sign in to comment.