Skip to content

Commit 56fe578

Browse files
authored
fix add_assign for ProjectivePoint (#543)
1 parent acdbeae commit 56fe578

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

starknet-curve/src/ec_point.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,22 @@ impl ops::AddAssign<&AffinePoint> for AffinePoint {
8383
return;
8484
}
8585
if self.infinity {
86-
self.x = rhs.x;
87-
self.y = rhs.y;
88-
self.infinity = rhs.infinity;
86+
*self = *rhs;
8987
return;
9088
}
9189
if self.x == rhs.x {
92-
if self.y == -rhs.y {
93-
self.x = FieldElement::ZERO;
94-
self.y = FieldElement::ZERO;
95-
self.infinity = true;
96-
return;
90+
if self.y == rhs.y {
91+
self.double_assign();
92+
} else {
93+
*self = AffinePoint::identity();
9794
}
98-
self.double_assign();
9995
return;
10096
}
10197

102-
// l = (y2-y1)/(x2-x1)
103-
let lambda = {
104-
let dividend = rhs.y - self.y;
105-
let divisor_inv = (rhs.x - self.x).invert().unwrap();
106-
dividend * divisor_inv
107-
};
98+
let lambda = (rhs.y - self.y) * (rhs.x - self.x).invert().unwrap();
99+
100+
let result_x = lambda * lambda - self.x - rhs.x;
108101

109-
let result_x = (lambda * lambda) - self.x - rhs.x;
110102
self.y = lambda * (self.x - result_x) - self.y;
111103
self.x = result_x;
112104
}
@@ -247,16 +239,17 @@ impl ops::AddAssign<&ProjectivePoint> for ProjectivePoint {
247239
return;
248240
}
249241
if self.infinity {
250-
self.x = rhs.x;
251-
self.y = rhs.y;
252-
self.z = rhs.z;
253-
self.infinity = rhs.infinity;
242+
*self = *rhs;
254243
return;
255244
}
256245
let u0 = self.x * rhs.z;
257246
let u1 = rhs.x * self.z;
258247
if u0 == u1 {
259-
self.double_assign();
248+
if self.y == rhs.y {
249+
self.double_assign();
250+
} else {
251+
*self = ProjectivePoint::identity();
252+
}
260253
return;
261254
}
262255

0 commit comments

Comments
 (0)