1- package e521
1+ package ed521
22
33import (
44 "math/big"
55 "crypto/elliptic"
66)
77
8- type E521Curve struct {
8+ // E521
9+ type Ed521Curve struct {
910 Name string
1011 P * big.Int
1112 N * big.Int
@@ -14,7 +15,7 @@ type E521Curve struct {
1415 BitSize int
1516}
1617
17- func (curve * E521Curve ) Params () * elliptic.CurveParams {
18+ func (curve * Ed521Curve ) Params () * elliptic.CurveParams {
1819 cp := new (elliptic.CurveParams )
1920 cp .Name = curve .Name
2021 cp .P = curve .P
@@ -26,7 +27,7 @@ func (curve *E521Curve) Params() *elliptic.CurveParams {
2627}
2728
2829// polynomial returns (y² - 1) / (dy² - 1).
29- func (curve * E521Curve ) polynomial (y * big.Int ) * big.Int {
30+ func (curve * Ed521Curve ) polynomial (y * big.Int ) * big.Int {
3031 // x² + y² = 1 + dx²y²
3132 // dx²y² - x² = x²(dy² - 1) = y² - 1
3233 // x² = (y² - 1) / (dy² - 1)
@@ -58,7 +59,7 @@ func (curve *E521Curve) polynomial(y *big.Int) *big.Int {
5859// IsOnCurve reports whether the given (x,y) lies on the curve.
5960// check equation: x² + y² ≡ 1 + d*x²*y² (mod p),
6061// so we can check equation: x² = (1 - y²) / (1 - d*y²).
61- func (curve * E521Curve ) IsOnCurve (x , y * big.Int ) bool {
62+ func (curve * Ed521Curve ) IsOnCurve (x , y * big.Int ) bool {
6263 if x .Sign () == 0 && y .Sign () == 0 {
6364 return true
6465 }
@@ -70,7 +71,7 @@ func (curve *E521Curve) IsOnCurve(x, y *big.Int) bool {
7071}
7172
7273// Add returns the sum of (x1,y1) and (x2,y2)
73- func (curve * E521Curve ) Add (x1 , y1 , x2 , y2 * big.Int ) (x , y * big.Int ) {
74+ func (curve * Ed521Curve ) Add (x1 , y1 , x2 , y2 * big.Int ) (x , y * big.Int ) {
7475 if x1 .Sign () == 0 && y1 .Sign () == 0 {
7576 return x2 , y2
7677 }
@@ -81,52 +82,83 @@ func (curve *E521Curve) Add(x1, y1, x2, y2 *big.Int) (x, y *big.Int) {
8182 panicIfNotOnCurve (curve , x1 , y1 )
8283 panicIfNotOnCurve (curve , x2 , y2 )
8384
84- x1y2 := new (big.Int ).Mul (x1 , y2 )
85- x2y1 := new (big.Int ).Mul (x2 , y1 )
86-
87- y1y2 := new (big.Int ).Mul (y1 , y2 )
88- x1x2 := new (big.Int ).Mul (x1 , x2 )
89-
90- // c = d*x1*x2*y1*y2
91- c := new (big.Int ).Mul (x1x2 , y1y2 )
92- c .Mul (c , curve .D )
93- c .Mod (c , curve .P )
94-
95- // x = (x1*y2 + x2*y1) / (d*x1*x2*y1*y2 + 1)
96- rx1 := new (big.Int ).Add (x1y2 , x2y1 )
97- rx2 := new (big.Int ).Add (c , big .NewInt (1 ))
98- invRx2 := new (big.Int ).ModInverse (rx2 , curve .P )
99- if invRx2 == nil {
100- return
101- }
102-
103- x = new (big.Int ).Mul (rx1 , invRx2 )
85+ // C = X1*X2
86+ c := new (big.Int ).Mul (x1 , x2 )
87+ // D = Y1*Y2
88+ d := new (big.Int ).Mul (y1 , y2 )
89+
90+ // E = d*C*D
91+ e := new (big.Int ).Mul (c , curve .D )
92+ e .Mul (e , d )
93+ e .Mod (e , curve .P )
94+
95+ // F = B-E
96+ f := new (big.Int ).Sub (big .NewInt (1 ), e )
97+ // G = B+E
98+ g := new (big.Int ).Add (big .NewInt (1 ), e )
99+
100+ // H = (X1+Y1)*(X2+Y2)
101+ tmp1 := new (big.Int ).Add (x1 , y1 )
102+ tmp2 := new (big.Int ).Add (x2 , y2 )
103+ h := new (big.Int ).Mul (tmp1 , tmp2 )
104+
105+ // Z3 = F*G
106+ z := new (big.Int ).Mul (f , g )
107+ zInv := new (big.Int ).ModInverse (z , curve .P )
108+
109+ // X3 = (z^-1) * A*F*(H-C-D)
110+ x = new (big.Int ).Sub (h , c )
111+ x .Sub (x , d )
112+ x .Mul (x , f )
113+ x .Mul (x , zInv )
104114 x .Mod (x , curve .P )
105115
106- // y = (x1*x2 - y1*y2) / (d*x1*x2*y1*y2 - 1)
107- ry1 := new (big.Int ).Sub (x1x2 , y1y2 )
108- ry2 := new (big.Int ).Sub (c , big .NewInt (1 ))
109- invRy2 := new (big.Int ).ModInverse (ry2 , curve .P )
110- if invRx2 == nil {
111- return
112- }
113-
114- y = new (big.Int ).Mul (ry1 , invRy2 )
116+ // Y3 = (z^-1) * A*G*(D-C)
117+ y = new (big.Int ).Sub (d , c )
118+ y .Mul (y , g )
119+ y .Mul (y , zInv )
115120 y .Mod (y , curve .P )
116121
117- // return result (x, y)
118122 return
119123}
120124
121125// Double returns 2*(x,y)
122- func (curve * E521Curve ) Double (x1 , y1 * big.Int ) (* big.Int , * big.Int ) {
123- x2 := new (big.Int ).Set (x1 )
124- y2 := new (big.Int ).Set (y1 )
126+ func (curve * Ed521Curve ) Double (x1 , y1 * big.Int ) (* big.Int , * big.Int ) {
127+ // B = (X1+Y1)^2
128+ b := new (big.Int ).Add (x1 , y1 )
129+ b .Mul (b , b )
130+
131+ // C = X1^2
132+ c := new (big.Int ).Mul (x1 , x1 )
133+ // D = Y1^2
134+ d := new (big.Int ).Mul (y1 , y1 )
135+
136+ // E = C+D
137+ e := new (big.Int ).Add (c , d )
138+
139+ // J = E-2*H
140+ j := new (big.Int ).Sub (e , big .NewInt (2 ))
141+
142+ // Z3 = E*J
143+ z := new (big.Int ).Mul (e , j )
144+ zInv := new (big.Int ).ModInverse (z , curve .P )
145+
146+ // X3 = (z^-1) * (B-E)*J
147+ x := new (big.Int ).Sub (b , e )
148+ x .Mul (x , j )
149+ x .Mul (x , zInv )
150+ x .Mod (x , curve .P )
125151
126- return curve .Add (x2 , y2 , x2 , y2 )
152+ // Y3 = (z^-1) * E*(C-D)
153+ y := new (big.Int ).Sub (c , d )
154+ y .Mul (y , e )
155+ y .Mul (y , zInv )
156+ y .Mod (y , curve .P )
157+
158+ return x , y
127159}
128160
129- func (curve * E521Curve ) ScalarMult (Bx , By * big.Int , k []byte ) (* big.Int , * big.Int ) {
161+ func (curve * Ed521Curve ) ScalarMult (Bx , By * big.Int , k []byte ) (* big.Int , * big.Int ) {
130162 x , y := big .NewInt (0 ), big .NewInt (1 )
131163
132164 Bx2 := new (big.Int ).Set (Bx )
@@ -147,20 +179,20 @@ func (curve *E521Curve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.In
147179 return x , y
148180}
149181
150- func (curve * E521Curve ) ScalarBaseMult (k []byte ) (* big.Int , * big.Int ) {
182+ func (curve * Ed521Curve ) ScalarBaseMult (k []byte ) (* big.Int , * big.Int ) {
151183 return curve .ScalarMult (curve .Gx , curve .Gy , k )
152184}
153185
154- func (curve * E521Curve ) Marshal (x , y * big.Int ) []byte {
186+ func (curve * Ed521Curve ) Marshal (x , y * big.Int ) []byte {
155187 return Marshal (curve , x , y )
156188}
157189
158190// MarshalCompressed compresses Edwards point according to RFC 8032: store sign bit of x
159- func (curve * E521Curve ) MarshalCompressed (x , y * big.Int ) []byte {
191+ func (curve * Ed521Curve ) MarshalCompressed (x , y * big.Int ) []byte {
160192 return MarshalCompressed (curve , x , y )
161193}
162194
163- func (curve * E521Curve ) Unmarshal (data []byte ) (* big.Int , * big.Int ) {
195+ func (curve * Ed521Curve ) Unmarshal (data []byte ) (* big.Int , * big.Int ) {
164196 if len (data ) == 0 {
165197 return nil , nil
166198 }
@@ -191,7 +223,7 @@ func (curve *E521Curve) Unmarshal(data []byte) (*big.Int, *big.Int) {
191223}
192224
193225// UnmarshalCompressed decompresses a compressed point according to RFC 8032
194- func (curve * E521Curve ) UnmarshalCompressed (data []byte ) (x , y * big.Int ) {
226+ func (curve * Ed521Curve ) UnmarshalCompressed (data []byte ) (x , y * big.Int ) {
195227 byteLen := (curve .BitSize + 7 ) / 8
196228 if len (data ) != 1 + byteLen {
197229 return
@@ -248,15 +280,15 @@ func MarshalCompressed(curve elliptic.Curve, x, y *big.Int) []byte {
248280}
249281
250282func Unmarshal (curve elliptic.Curve , data []byte ) (* big.Int , * big.Int ) {
251- if c , ok := curve .(* E521Curve ); ok {
283+ if c , ok := curve .(* Ed521Curve ); ok {
252284 return c .Unmarshal (data )
253285 }
254286
255287 return nil , nil
256288}
257289
258290func UnmarshalCompressed (curve elliptic.Curve , data []byte ) (* big.Int , * big.Int ) {
259- if c , ok := curve .(* E521Curve ); ok {
291+ if c , ok := curve .(* Ed521Curve ); ok {
260292 return c .UnmarshalCompressed (data )
261293 }
262294
0 commit comments