Skip to content

Commit

Permalink
Merge branch 'negative-multiplication'
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKueltz committed Jul 14, 2021
2 parents f2c8337 + 184beb6 commit bc203c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fastecdsa/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def __mul__(self, scalar: int):
x, y = curvemath.mul(
str(self.x),
str(self.y),
str(scalar),
str(abs(scalar)),
str(self.curve.p),
str(self.curve.a),
str(self.curve.b),
Expand All @@ -152,7 +152,7 @@ def __mul__(self, scalar: int):
y = int(y)
if x == 0 and y == 0:
return self.IDENTITY_ELEMENT
return Point(x, y, self.curve)
return Point(x, y, self.curve) if scalar > 0 else -Point(x, y, self.curve)

def __rmul__(self, scalar: int):
"""Multiply a :class:`Point` on an elliptic curve by an integer.
Expand Down
14 changes: 14 additions & 0 deletions fastecdsa/tests/test_prime_field_curve_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,17 @@ def test_point_at_infinity_arithmetic(self):

self.assertEqual(P + Q, Point.IDENTITY_ELEMENT)
self.assertEqual((P + Q) + P, P)

def test_mul_by_negative(self):
for curve in CURVES:
P = -1 * curve.G
self.assertEqual(P.x, (-curve.G).x)
self.assertEqual(P.y, (-curve.G).y)

a = randint(0, curve.q)
P = -a * curve.G
Q = a * -curve.G

self.assertEqual(P.x, Q.x)
self.assertEqual(P.y, Q.y)

0 comments on commit bc203c5

Please sign in to comment.