This repository has been archived by the owner on Apr 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
curve_test.go
183 lines (147 loc) · 4.88 KB
/
curve_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package edwards
import (
"crypto/rand"
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)
func testCurves() []Curve {
return []Curve{E222(), E382(), E521(), Ed448Goldilocks(), Curve1174(), Curve41417()}
}
func TestScalarMultZero(t *testing.T) {
for _, crv := range testCurves() {
_, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
rx, ry := crv.ScalarMult(x0, y0, []byte{0, 0})
assert.Equal(t, zero, rx, "(0*P).X != 0")
assert.Equal(t, one, ry, "(0*P).Y != 1")
}
}
func TestScalarMultOne(t *testing.T) {
for _, crv := range testCurves() {
_, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
rx, ry := crv.ScalarMult(x0, y0, []byte{0, 1})
assert.Equal(t, rx, x0, "(1*P).X != P.X")
assert.Equal(t, ry, y0, "(1*P).Y != P.Y")
}
}
func TestScalarMultTwo(t *testing.T) {
for _, crv := range testCurves() {
_, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
rx, ry := crv.ScalarMult(x0, y0, []byte{2})
assert.True(t, crv.IsOnCurve(rx, ry), "2*P must be on curve")
sx, sy := crv.Add(x0, y0, x0, y0)
assert.True(t, crv.IsOnCurve(sx, sy), "P+P must be on curve")
assert.Equal(t, rx, sx, "(2*P).X != (P+P).X")
assert.Equal(t, ry, sy, "(2*P).Y != (P+P).Y")
}
}
func TestScalarMultCommutative(t *testing.T) {
for _, crv := range testCurves() {
priv0, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
priv1, x1, y1, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x1, y1), "Generated P must be on curve")
rx, ry := crv.ScalarMult(x0, y0, priv1)
assert.True(t, crv.IsOnCurve(rx, ry), "priv1*P0 must be on curve")
sx, sy := crv.ScalarMult(x1, y1, priv0)
assert.True(t, crv.IsOnCurve(sx, sy), "priv0*P1 must be on curve")
assert.Equal(t, rx, sx, "(priv0*P1).X != (priv1*P0).X")
assert.Equal(t, ry, sy, "(priv0*P1).Y != (priv1*P0).Y")
}
}
func TestAdditionCommutative(t *testing.T) {
for _, crv := range testCurves() {
_, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
_, x1, y1, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x1, y1), "Generated P must be on curve")
rx, ry := crv.Add(x0, y0, x1, y1)
assert.True(t, crv.IsOnCurve(rx, ry), "P0+P1 must be on curve")
sx, sy := crv.Add(x1, y1, x0, y0)
assert.True(t, crv.IsOnCurve(sx, sy), "P1+P0 must be on curve")
assert.Equal(t, rx, sx, "(P0+P1).X != (P1+P0).X")
assert.Equal(t, ry, sy, "(P0+P1).Y != (P1+P0).Y")
}
}
func TestBasePointOnCurve(t *testing.T) {
for _, crv := range testCurves() {
assert.True(t, crv.IsOnCurve(crv.Gx, crv.Gy), "Base point must be on curve")
}
}
func TestBasePointOrderCorrect(t *testing.T) {
for _, crv := range testCurves() {
rx, ry := crv.ScalarBaseMult(crv.N.Bytes())
assert.Equal(t, zero, rx, "(N*G).X != 0")
assert.Equal(t, one, ry, "(N*G).Y != 1")
}
}
func TestAdditiveInverse(t *testing.T) {
for _, crv := range testCurves() {
_, x0, y0, err := crv.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
assert.True(t, crv.IsOnCurve(x0, y0), "Generated P must be on curve")
x1 := new(big.Int).Neg(x0)
rx, ry := crv.Add(x0, y0, x1, y0)
assert.Equal(t, zero, rx, "(P+(-P)).X != 0")
assert.Equal(t, one, ry, "(P+(-P)).Y != 1")
}
}
func TestNeutralAddition(t *testing.T) {
for _, crv := range testCurves() {
x, y := crv.Add(zero, one, zero, one)
assert.Equal(t, zero, x, "(E+E).X != 0")
assert.Equal(t, one, y, "(E+E).Y != 1")
}
}
func TestNeutralScalarMult(t *testing.T) {
for _, crv := range testCurves() {
x, y := crv.ScalarMult(zero, one, []byte{0})
assert.Equal(t, zero, x, "(0*E).X != 0")
assert.Equal(t, one, y, "(0*E).Y != 1")
x, y = crv.ScalarMult(zero, one, []byte{1})
assert.Equal(t, zero, x, "(1*E).X != 0")
assert.Equal(t, one, y, "(1*E).Y != 1")
x, y = crv.ScalarMult(zero, one, []byte{2})
assert.Equal(t, zero, x, "(2*E).X != 0")
assert.Equal(t, one, y, "(2*E).Y != 1")
}
}
func TestKnownPoints(t *testing.T) {
for _, crv := range testCurves() {
x, y := crv.ScalarMult(zero, big.NewInt(-1), []byte{2})
assert.Equal(t, zero, x, "(2*(0, -1)).X != 0")
assert.Equal(t, one, y, "(2*(0, -1)).Y != 1")
x, y = crv.ScalarMult(one, zero, []byte{4})
assert.Equal(t, zero, x, "(2*(1, 0)).X != 0")
assert.Equal(t, one, y, "(2*(1, 0)).Y != 1")
x, y = crv.ScalarMult(big.NewInt(-1), zero, []byte{4})
assert.Equal(t, zero, x, "(4*(-1, 0)).X != 0")
assert.Equal(t, one, y, "(4*(-1, 0)).Y != 1")
}
}