Skip to content

Commit e8b926c

Browse files
committed
fixed
1 parent 19638da commit e8b926c

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

elliptic/e521/params.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ func (curve *E521Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
7777
return x1, y1
7878
}
7979

80+
panicIfNotOnCurve(curve, x1, y1)
81+
panicIfNotOnCurve(curve, x2, y2)
82+
8083
// x3 = (x1*y2 + y1*x2) / (1 + d*x1*x2*y1*y2)
8184
// y3 = (y1*y2 - x1*x2) / (1 - d*x1*x2*y1*y2)
8285

@@ -110,7 +113,12 @@ func (curve *E521Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
110113

111114
// Double returns 2*(x,y)
112115
func (curve *E521Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
113-
return curve.Add(x1, y1, x1, y1)
116+
panicIfNotOnCurve(curve, x1, y1)
117+
118+
x2 := new(big.Int).Set(x1)
119+
y2 := new(big.Int).Set(y1)
120+
121+
return curve.Add(x2, y2, x2, y2)
114122
}
115123

116124
func (curve *E521Curve) ScalarMult(x, y *big.Int, k []byte) (*big.Int, *big.Int) {

elliptic/kg/params.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func (curve *KGCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
7272
return nil, nil
7373
}
7474

75+
panicIfNotOnCurve(curve, x1, y1)
76+
panicIfNotOnCurve(curve, x2, y2)
77+
7578
y2MinusY1 := new(big.Int).Sub(y2, y1)
7679
x2MinusX1 := new(big.Int).Sub(x2, x1)
7780
x2MinusX1Inv := new(big.Int).ModInverse(x2MinusX1, curve.P)
@@ -92,23 +95,27 @@ func (curve *KGCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
9295
}
9396

9497
func (curve *KGCurve) Double(x, y *big.Int) (*big.Int, *big.Int) {
95-
// 3 * x³ + a
98+
panicIfNotOnCurve(curve, x, y)
99+
100+
// numerator = 3 * x³ + a
96101
x2 := new(big.Int).Mul(x, x)
97102
threeX2 := new(big.Int).Mul(big.NewInt(3), x2)
98103
numerator := new(big.Int).Add(threeX2, curve.A)
99104

100-
// 2 * y
105+
// denomInv = 1 / (2 * y)
101106
twoY := new(big.Int).Mul(big.NewInt(2), y)
102107
denomInv := new(big.Int).ModInverse(twoY, curve.P)
103108

104-
// (3 * x³ + a) * (2 * y)
109+
// lambda = (3 * x³ + a) / (2 * y)
105110
lambda := new(big.Int).Mul(numerator, denomInv)
106111
lambda.Mod(lambda, curve.P)
107112

113+
// x3 = lambda^2 - 2 * x
108114
x3 := new(big.Int).Mul(lambda, lambda)
109115
x3.Sub(x3, new(big.Int).Mul(big.NewInt(2), x))
110116
x3.Mod(x3, curve.P)
111117

118+
// y3 = (x - x^3) * lambda - y
112119
y3 := new(big.Int).Sub(x, x3)
113120
y3.Mul(lambda, y3)
114121
y3.Sub(y3, y)

elliptic/secp256k1/params.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (crv *secp256k1) IsOnCurve(x, y *big.Int) bool {
2424
if _, err := p.NewPoint(x, y); err != nil {
2525
return false
2626
}
27+
2728
return curve256k1.IsOnCurve(&p)
2829
}
2930

0 commit comments

Comments
 (0)