@@ -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
9497func (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 )
0 commit comments