-
Notifications
You must be signed in to change notification settings - Fork 146
/
g1_test.go
284 lines (260 loc) · 6.53 KB
/
g1_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package bls12381
import (
"crypto/rand"
"fmt"
"testing"
"github.com/cloudflare/circl/ecc/bls12381/ff"
"github.com/cloudflare/circl/internal/test"
)
func randomScalar(t testing.TB) *Scalar {
s := &Scalar{}
err := s.Random(rand.Reader)
test.CheckNoErr(t, err, "random scalar")
return s
}
func randomG1(t testing.TB) *G1 {
P := &G1{}
u := &ff.Fp{}
r := &isogG1Point{}
err := u.Random(rand.Reader)
test.CheckNoErr(t, err, "random fp")
r.sswu(u)
P.evalIsogG1(r)
P.clearCofactor()
got := P.IsOnG1()
want := true
if got != want {
test.ReportError(t, got, want, "point not in G1", u)
}
return P
}
func TestG1Add(t *testing.T) {
const testTimes = 1 << 6
var Q, R G1
for i := 0; i < testTimes; i++ {
P := randomG1(t)
Q = *P
R = *P
R.Add(&R, &R)
R.Neg()
Q.Double()
Q.Neg()
got := R
want := Q
if !got.IsEqual(&want) {
test.ReportError(t, got, want, P)
}
}
}
func TestG1ScalarMult(t *testing.T) {
const testTimes = 1 << 6
var Q G1
for i := 0; i < testTimes; i++ {
P := randomG1(t)
k := randomScalar(t)
Q.ScalarMult(k, P)
Q.toAffine()
got := Q.IsOnG1()
want := true
if got != want {
test.ReportError(t, got, want, P, k)
}
}
}
func TestG1Hash(t *testing.T) {
const testTimes = 1 << 8
for _, e := range [...]struct {
Name string
Enc func(p *G1, input, dst []byte)
}{
{"Encode", func(p *G1, input, dst []byte) { p.Encode(input, dst) }},
{"Hash", func(p *G1, input, dst []byte) { p.Hash(input, dst) }},
} {
var msg, dst [4]byte
var p G1
t.Run(e.Name, func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(msg[:])
_, _ = rand.Read(dst[:])
e.Enc(&p, msg[:], dst[:])
got := p.isRTorsion()
want := true
if got != want {
test.ReportError(t, got, want, e.Name, msg, dst)
}
}
})
}
}
func BenchmarkG1(b *testing.B) {
P := randomG1(b)
Q := randomG1(b)
k := randomScalar(b)
var msg, dst [4]byte
_, _ = rand.Read(msg[:])
_, _ = rand.Read(dst[:])
b.Run("Add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.Add(P, Q)
}
})
b.Run("Mul", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.ScalarMult(k, P)
}
})
b.Run("Hash", func(b *testing.B) {
for i := 0; i < b.N; i++ {
P.Hash(msg[:], dst[:])
}
})
}
func TestG1Serial(t *testing.T) {
mustOk := "must be ok"
mustErr := "must be an error"
t.Run("valid", func(t *testing.T) {
testTimes := 1 << 6
var got, want G1
want.SetIdentity()
for i := 0; i < testTimes; i++ {
for _, b := range [][]byte{want.Bytes(), want.BytesCompressed()} {
err := got.SetBytes(b)
test.CheckNoErr(t, err, fmt.Sprintf("failure to deserialize: (P:%v b:%x)", want, b))
if !got.IsEqual(&want) {
test.ReportError(t, got, want, b)
}
}
want = *randomG1(t)
}
})
t.Run("badPrefix", func(t *testing.T) {
q := new(G1)
b := make([]byte, G1Size)
for _, b[0] = range []byte{0x20, 0x60, 0xE0} {
test.CheckIsErr(t, q.SetBytes(b), mustErr)
}
})
t.Run("badLength", func(t *testing.T) {
q := new(G1)
p := randomG1(t)
b := p.Bytes()
test.CheckIsErr(t, q.SetBytes(b[:0]), mustErr)
test.CheckIsErr(t, q.SetBytes(b[:1]), mustErr)
test.CheckIsErr(t, q.SetBytes(b[:G1Size-1]), mustErr)
test.CheckIsErr(t, q.SetBytes(b[:G1SizeCompressed]), mustErr)
test.CheckNoErr(t, q.SetBytes(b), mustOk)
test.CheckNoErr(t, q.SetBytes(append(b, 0)), mustOk)
b = p.BytesCompressed()
test.CheckIsErr(t, q.SetBytes(b[:0]), mustErr)
test.CheckIsErr(t, q.SetBytes(b[:1]), mustErr)
test.CheckIsErr(t, q.SetBytes(b[:G1SizeCompressed-1]), mustErr)
test.CheckNoErr(t, q.SetBytes(b), mustOk)
test.CheckNoErr(t, q.SetBytes(append(b, 0)), mustOk)
})
t.Run("badInfinity", func(t *testing.T) {
var badInf, p G1
badInf.SetIdentity()
b := badInf.Bytes()
b[0] |= 0x1F
err := p.SetBytes(b)
test.CheckIsErr(t, err, mustErr)
b[0] &= 0xE0
b[1] = 0xFF
err = p.SetBytes(b)
test.CheckIsErr(t, err, mustErr)
})
t.Run("badCoords", func(t *testing.T) {
bad := (&[ff.FpSize]byte{})[:]
for i := range bad {
bad[i] = 0xFF
}
var e ff.Fp
_ = e.Random(rand.Reader)
good, err := e.MarshalBinary()
test.CheckNoErr(t, err, mustOk)
// bad x, good y
b := append(bad, good...)
b[0] = b[0]&0x1F | headerEncoding(0, 0, 0)
test.CheckIsErr(t, new(G1).SetBytes(b), mustErr)
// good x, bad y
b = append(good, bad...)
b[0] = b[0]&0x1F | headerEncoding(0, 0, 0)
test.CheckIsErr(t, new(G1).SetBytes(b), mustErr)
})
t.Run("noQR", func(t *testing.T) {
var x ff.Fp
x.SetUint64(1) // Let x=1, so x^3+4 = 5, which is not QR.
b, err := x.MarshalBinary()
test.CheckNoErr(t, err, mustOk)
b[0] = b[0]&0x1F | headerEncoding(1, 0, 0)
test.CheckIsErr(t, new(G1).SetBytes(b), mustErr)
})
t.Run("notInG1", func(t *testing.T) {
// p=(0,1) is not on curve.
var x, y ff.Fp
y.SetUint64(1)
bx, err := x.MarshalBinary()
test.CheckNoErr(t, err, mustOk)
by, err := y.MarshalBinary()
test.CheckNoErr(t, err, mustOk)
b := append(bx, by...)
b[0] = b[0]&0x1F | headerEncoding(0, 0, 0)
test.CheckIsErr(t, new(G1).SetBytes(b), mustErr)
})
}
func TestG1Affinize(t *testing.T) {
N := 20
testTimes := 1 << 6
g1 := make([]*G1, N)
for i := 0; i < testTimes; i++ {
for j := 0; j < N; j++ {
g1[j] = randomG1(t)
}
g2 := affinize(g1)
for j := 0; j < N; j++ {
g1[j].toAffine()
if !g1[j].IsEqual(&g2[j]) {
t.Fatal("failure to preserve points")
}
if g2[j].z.IsEqual(&g1[j].z) != 1 {
t.Fatal("failure to make affine")
}
}
}
}
func TestG1Torsion(t *testing.T) {
if !G1Generator().isRTorsion() {
t.Fatalf("G1 generator is not r-torsion")
}
}
func TestG1Bytes(t *testing.T) {
got := new(G1)
id := new(G1)
id.SetIdentity()
g := G1Generator()
minusG := G1Generator()
minusG.Neg()
type testCase struct {
header byte
length int
point *G1
toBytes func(G1) []byte
}
for i, v := range []testCase{
{headerEncoding(0, 0, 0), G1Size, randomG1(t), (G1).Bytes},
{headerEncoding(0, 0, 0), G1Size, g, (G1).Bytes},
{headerEncoding(1, 0, 0), G1SizeCompressed, g, (G1).BytesCompressed},
{headerEncoding(1, 0, 1), G1SizeCompressed, minusG, (G1).BytesCompressed},
{headerEncoding(0, 1, 0), G1Size, id, (G1).Bytes},
{headerEncoding(1, 1, 0), G1SizeCompressed, id, (G1).BytesCompressed},
} {
b := v.toBytes(*v.point)
test.CheckOk(len(b) == v.length, fmt.Sprintf("bad encoding size (case:%v point:%v b:%x)", i, v.point, b), t)
test.CheckOk(b[0]&0xE0 == v.header, fmt.Sprintf("bad encoding header (case:%v point:%v b:%x)", i, v.point, b), t)
err := got.SetBytes(b)
want := v.point
if err != nil || !got.IsEqual(want) {
test.ReportError(t, got, want, i, b)
}
}
}