diff --git a/fastecdsa/point.py b/fastecdsa/point.py index 4dba935..a67a5cb 100644 --- a/fastecdsa/point.py +++ b/fastecdsa/point.py @@ -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), @@ -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. diff --git a/fastecdsa/tests/test_prime_field_curve_math.py b/fastecdsa/tests/test_prime_field_curve_math.py index e847d0d..a816139 100644 --- a/fastecdsa/tests/test_prime_field_curve_math.py +++ b/fastecdsa/tests/test_prime_field_curve_math.py @@ -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) +