Skip to content

Commit 321491a

Browse files
committed
fixed
1 parent a4f5149 commit 321491a

File tree

7 files changed

+122
-90
lines changed

7 files changed

+122
-90
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package e521
1+
package ed521
22

33
import (
44
"sync"
@@ -10,13 +10,13 @@ import (
1010
// https://eprint.iacr.org/2013/647
1111

1212
var (
13-
// E-521 EdDSA curve oid
14-
OIDE521 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44588, 2, 1}
13+
// Ed-521 curve oid
14+
OIDED521 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44588, 2, 1}
1515
)
1616

1717
var once sync.Once
1818

19-
func E521() *E521Curve {
19+
func ED521() *Ed521Curve {
2020
once.Do(initAll)
21-
return e521
21+
return ed521
2222
}

elliptic/e521/e521_curves.go renamed to elliptic/ed521/ed521_curves.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package e521
1+
package ed521
22

33
import (
44
"math/big"
55
)
66

7-
var e521 *E521Curve
7+
var ed521 *Ed521Curve
88

99
func initAll() {
1010
initE521()
1111
}
1212

1313
func initE521() {
14-
e521 = &E521Curve{
15-
Name: "E-521",
14+
ed521 = &Ed521Curve{
15+
Name: "Ed521",
1616
P: bigFromHex("1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
1717
N: bigFromHex("7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd15b6c64746fc85f736b8af5e7ec53f04fbd8c4569a8f1f4540ea2435f5180d6b"),
1818
D: bigFromHex("1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa4331"),

elliptic/e521/e521_test.go renamed to elliptic/ed521/ed521_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package e521
1+
package ed521
22

33
import (
44
"fmt"
@@ -16,7 +16,7 @@ func bigintFromHex(s string) *big.Int {
1616
}
1717

1818
func Test_Interface(t *testing.T) {
19-
var _ elliptic.Curve = (*E521Curve)(nil)
19+
var _ elliptic.Curve = (*Ed521Curve)(nil)
2020
}
2121

2222
func Test_Curve_Add(t *testing.T) {
@@ -26,7 +26,7 @@ func Test_Curve_Add(t *testing.T) {
2626
a2 := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
2727
b2 := bigintFromHex("5d33e51bfe1cbb3c263ad569b213be723a45920ac38070147d8d85c1779b4fe4eaa0912a17765f2d87bb2ac27106fb8d019152c373e9ea060f591c1d85141cc830")
2828

29-
xx, yy := E521().Add(a1, b1, a2, b2)
29+
xx, yy := ED521().Add(a1, b1, a2, b2)
3030

3131
xx2 := fmt.Sprintf("%x", xx.Bytes())
3232
yy2 := fmt.Sprintf("%x", yy.Bytes())
@@ -46,7 +46,7 @@ func Test_Curve_Add(t *testing.T) {
4646
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
4747
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
4848

49-
xx, yy := E521().Add(a1, b1, a1, b1)
49+
xx, yy := ED521().Add(a1, b1, a1, b1)
5050

5151
xx2 := fmt.Sprintf("%x", xx.Bytes())
5252
yy2 := fmt.Sprintf("%x", yy.Bytes())
@@ -66,7 +66,7 @@ func Test_Curve_Add(t *testing.T) {
6666
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
6767
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
6868

69-
xx, yy := E521().Double(a1, b1)
69+
xx, yy := ED521().Double(a1, b1)
7070

7171
xx2 := fmt.Sprintf("%x", xx.Bytes())
7272
yy2 := fmt.Sprintf("%x", yy.Bytes())
@@ -90,7 +90,7 @@ func Test_Curve_ScalarMult(t *testing.T) {
9090
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
9191
k := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
9292

93-
xx, yy := E521().ScalarMult(a1, b1, k.Bytes())
93+
xx, yy := ED521().ScalarMult(a1, b1, k.Bytes())
9494

9595
xx2 := fmt.Sprintf("%x", xx.Bytes())
9696
yy2 := fmt.Sprintf("%x", yy.Bytes())
@@ -109,7 +109,7 @@ func Test_Curve_ScalarMult(t *testing.T) {
109109
{
110110
k := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
111111

112-
xx, yy := E521().ScalarBaseMult(k.Bytes())
112+
xx, yy := ED521().ScalarBaseMult(k.Bytes())
113113

114114
xx2 := fmt.Sprintf("%x", xx.Bytes())
115115
yy2 := fmt.Sprintf("%x", yy.Bytes())
@@ -131,7 +131,7 @@ func Test_MarshalCompressed(t *testing.T) {
131131
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
132132
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
133133

134-
m := MarshalCompressed(E521(), a1, b1)
134+
m := MarshalCompressed(ED521(), a1, b1)
135135

136136
m2 := fmt.Sprintf("%x", m)
137137
mcheck := "0300d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde"
@@ -140,7 +140,7 @@ func Test_MarshalCompressed(t *testing.T) {
140140

141141
mcheck2 := bigintFromHex(mcheck).Bytes()
142142

143-
x, y := UnmarshalCompressed(E521(), mcheck2)
143+
x, y := UnmarshalCompressed(ED521(), mcheck2)
144144
cryptobin_test.Equal(t, a1, x)
145145
cryptobin_test.Equal(t, b1, y)
146146
}

elliptic/e521/params.go renamed to elliptic/ed521/params.go

Lines changed: 80 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
package e521
1+
package ed521
22

33
import (
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

250282
func 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

258290
func 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

Comments
 (0)