This repository has been archived by the owner on Jun 10, 2020. It is now read-only.
forked from kilic/bls12-381
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyber_test.go
313 lines (282 loc) · 9.19 KB
/
kyber_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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package bls
import (
"bytes"
"crypto/cipher"
"testing"
"github.com/drand/kyber"
"github.com/drand/kyber/sign/bls"
"github.com/drand/kyber/sign/tbls"
"github.com/drand/kyber/sign/test"
"github.com/drand/kyber/util/random"
"github.com/stretchr/testify/require"
)
// Code extracted from kyber/utils/test
// TODO: expose API in forked drand/kyber
// Apply a generic set of validation tests to a cryptographic Group,
// using a given source of [pseudo-]randomness.
//
// Returns a log of the pseudorandom Points produced in the test,
// for comparison across alternative implementations
// that are supposed to be equivalent.
func testGroup(t *testing.T, g kyber.Group, rand cipher.Stream) []kyber.Point {
t.Logf("\nTesting group '%s': %d-byte Point, %d-byte Scalar\n",
g.String(), g.PointLen(), g.ScalarLen())
points := make([]kyber.Point, 0)
ptmp := g.Point()
stmp := g.Scalar()
pzero := g.Point().Null()
szero := g.Scalar().Zero()
sone := g.Scalar().One()
// Do a simple Diffie-Hellman test
s1 := g.Scalar().Pick(rand)
s2 := g.Scalar().Pick(rand)
if s1.Equal(szero) {
t.Fatalf("first secret is scalar zero %v", s1)
}
if s2.Equal(szero) {
t.Fatalf("second secret is scalar zero %v", s2)
}
if s1.Equal(s2) {
t.Fatalf("not getting unique secrets: picked %s twice", s1)
}
gen := g.Point().Base()
points = append(points, gen)
// Sanity-check relationship between addition and multiplication
p1 := g.Point().Add(gen, gen)
p2 := g.Point().Mul(stmp.SetInt64(2), nil)
if !p1.Equal(p2) {
t.Fatalf("multiply by two doesn't work: %v == %v (+) %[2]v != %[2]v (x) 2 == %v", p1, gen, p2)
}
p1.Add(p1, p1)
p2.Mul(stmp.SetInt64(4), nil)
if !p1.Equal(p2) {
t.Fatalf("multiply by four doesn't work: %v (+) %[1]v != %v (x) 4 == %v",
g.Point().Add(gen, gen), gen, p2)
}
points = append(points, p1)
// Find out if this curve has a prime order:
// if the curve does not offer a method IsPrimeOrder,
// then assume that it is.
type canCheckPrimeOrder interface {
IsPrimeOrder() bool
}
primeOrder := true
if gpo, ok := g.(canCheckPrimeOrder); ok {
primeOrder = gpo.IsPrimeOrder()
}
// Verify additive and multiplicative identities of the generator.
// TODO: Check GT exp
/*fmt.Println("Inverse of base")*/
//f := ptmp.Base().(*KyberGT).f
//newFp12(nil).inverse(f, f)
//fmt.Printf("\n-Inverse: %v\n", f)
//fmt.Println("Multiply by -1")
ptmp.Mul(stmp.SetInt64(-1), nil).Add(ptmp, gen)
/*fmt.Printf(" \n\nChecking equality additive identity\nptmp: %v \n\n zero %v\n", ptmp, pzero)*/
if !ptmp.Equal(pzero) {
t.Fatalf("generator additive identity doesn't work: (scalar -1 %v) %v (x) -1 (+) %v = %v != %v the group point identity",
stmp.SetInt64(-1), ptmp.Mul(stmp.SetInt64(-1), nil), gen, ptmp.Mul(stmp.SetInt64(-1), nil).Add(ptmp, gen), pzero)
}
// secret.Inv works only in prime-order groups
if primeOrder {
ptmp.Mul(stmp.SetInt64(2), nil).Mul(stmp.Inv(stmp), ptmp)
if !ptmp.Equal(gen) {
t.Fatalf("generator multiplicative identity doesn't work:\n%v (x) %v = %v\n%[3]v (x) %v = %v",
ptmp.Base().String(), stmp.SetInt64(2).String(),
ptmp.Mul(stmp.SetInt64(2), nil).String(),
stmp.Inv(stmp).String(),
ptmp.Mul(stmp.SetInt64(2), nil).Mul(stmp.Inv(stmp), ptmp).String())
}
}
p1.Mul(s1, gen)
p2.Mul(s2, gen)
if p1.Equal(p2) {
t.Fatalf("encryption isn't producing unique points: %v (x) %v == %v (x) %[2]v == %[4]v", s1, gen, s2, p1)
}
points = append(points, p1)
dh1 := g.Point().Mul(s2, p1)
dh2 := g.Point().Mul(s1, p2)
if !dh1.Equal(dh2) {
t.Fatalf("Diffie-Hellman didn't work: %v == %v (x) %v != %v (x) %v == %v", dh1, s2, p1, s1, p2, dh2)
}
points = append(points, dh1)
//t.Logf("shared secret = %v", dh1)
// Test secret inverse to get from dh1 back to p1
if primeOrder {
ptmp.Mul(g.Scalar().Inv(s2), dh1)
if !ptmp.Equal(p1) {
t.Fatalf("Scalar inverse didn't work: %v != (-)%v (x) %v == %v", p1, s2, dh1, ptmp)
}
}
// Zero and One identity secrets
//println("dh1^0 = ",ptmp.Mul(dh1, szero).String())
if !ptmp.Mul(szero, dh1).Equal(pzero) {
t.Fatalf("Encryption with secret=0 didn't work: %v (x) %v == %v != %v", szero, dh1, ptmp, pzero)
}
if !ptmp.Mul(sone, dh1).Equal(dh1) {
t.Fatalf("Encryption with secret=1 didn't work: %v (x) %v == %v != %[2]v", sone, dh1, ptmp)
}
// Additive homomorphic identities
ptmp.Add(p1, p2)
stmp.Add(s1, s2)
pt2 := g.Point().Mul(stmp, gen)
if !pt2.Equal(ptmp) {
t.Fatalf("Additive homomorphism doesn't work: %v + %v == %v, %[3]v (x) %v == %v != %v == %v (+) %v",
s1, s2, stmp, gen, pt2, ptmp, p1, p2)
}
ptmp.Sub(p1, p2)
stmp.Sub(s1, s2)
pt2.Mul(stmp, gen)
if !pt2.Equal(ptmp) {
t.Fatalf("Additive homomorphism doesn't work: %v - %v == %v, %[3]v (x) %v == %v != %v == %v (-) %v",
s1, s2, stmp, gen, pt2, ptmp, p1, p2)
}
st2 := g.Scalar().Neg(s2)
st2.Add(s1, st2)
if !stmp.Equal(st2) {
t.Fatalf("Scalar.Neg doesn't work: -%v == %v, %[2]v + %v == %v != %v",
s2, g.Scalar().Neg(s2), s1, st2, stmp)
}
pt2.Neg(p2).Add(pt2, p1)
if !pt2.Equal(ptmp) {
t.Fatalf("Point.Neg doesn't work: (-)%v == %v, %[2]v (+) %v == %v != %v",
p2, g.Point().Neg(p2), p1, pt2, ptmp)
}
// Multiplicative homomorphic identities
stmp.Mul(s1, s2)
if !ptmp.Mul(stmp, gen).Equal(dh1) {
t.Fatalf("Multiplicative homomorphism doesn't work: %v * %v == %v, %[2]v (x) %v == %v != %v",
s1, s2, stmp, gen, ptmp, dh1)
}
if primeOrder {
st2.Inv(s2)
st2.Mul(st2, stmp)
if !st2.Equal(s1) {
t.Fatalf("Scalar division doesn't work: %v^-1 * %v == %v * %[2]v == %[4]v != %v",
s2, stmp, g.Scalar().Inv(s2), st2, s1)
}
st2.Div(stmp, s2)
if !st2.Equal(s1) {
t.Fatalf("Scalar division doesn't work: %v / %v == %v != %v",
stmp, s2, st2, s1)
}
}
pick := func(rand cipher.Stream) (p kyber.Point) {
defer func() {
/*if err := recover(); err != nil {*/
//// TODO implement Pick for GT
//p = g.Point().Mul(g.Scalar().Pick(rand), nil)
//return
/*}*/
}()
p = g.Point().Pick(rand)
return
}
// Test randomly picked points
last := gen
for i := 0; i < 5; i++ {
// TODO fork kyber and make that an interface
rgen := pick(rand)
if rgen.Equal(last) {
t.Fatalf("Pick() not producing unique points: got %v twice", rgen)
}
last = rgen
ptmp.Mul(stmp.SetInt64(-1), rgen).Add(ptmp, rgen)
if !ptmp.Equal(pzero) {
t.Fatalf("random generator fails additive identity: %v (x) %v == %v, %v (+) %[3]v == %[5]v != %v",
g.Scalar().SetInt64(-1), rgen, g.Point().Mul(g.Scalar().SetInt64(-1), rgen),
rgen, g.Point().Mul(g.Scalar().SetInt64(-1), rgen), pzero)
}
if primeOrder {
ptmp.Mul(stmp.SetInt64(2), rgen).Mul(stmp.Inv(stmp), ptmp)
if !ptmp.Equal(rgen) {
t.Fatalf("random generator fails multiplicative identity: %v (x) (2 (x) %v) == %v != %[2]v",
stmp, rgen, ptmp)
}
}
points = append(points, rgen)
}
// Test encoding and decoding
buf := new(bytes.Buffer)
for i := 0; i < 5; i++ {
buf.Reset()
s := g.Scalar().Pick(rand)
if _, err := s.MarshalTo(buf); err != nil {
t.Fatalf("encoding of secret fails: " + err.Error())
}
if _, err := stmp.UnmarshalFrom(buf); err != nil {
t.Fatalf("decoding of secret fails: " + err.Error())
}
if !stmp.Equal(s) {
t.Fatalf("decoding produces different secret than encoded")
}
buf.Reset()
p := pick(rand)
if _, err := p.MarshalTo(buf); err != nil {
t.Fatalf("encoding of point fails: " + err.Error())
}
if _, err := ptmp.UnmarshalFrom(buf); err != nil {
t.Fatalf("decoding of point fails: " + err.Error())
}
if !ptmp.Equal(p) {
t.Fatalf("decoding produces different point than encoded")
}
}
// Test that we can marshal/ unmarshal null point
pzero = g.Point().Null()
b, _ := pzero.MarshalBinary()
repzero := g.Point()
err := repzero.UnmarshalBinary(b)
if err != nil {
t.Fatalf("Could not unmarshall binary %v: %v", b, err)
}
return points
}
// GroupTest applies a generic set of validation tests to a cryptographic Group.
func GroupTest(t *testing.T, g kyber.Group) {
testGroup(t, g, random.New())
}
func TestKyberG1(t *testing.T) {
GroupTest(t, NewGroupG1())
}
func TestKyberG2(t *testing.T) {
GroupTest(t, NewGroupG2())
}
func TestKyberPairingG2(t *testing.T) {
s := NewBLS12381Suite()
a := s.G1().Scalar().Pick(s.RandomStream())
b := s.G2().Scalar().Pick(s.RandomStream())
aG := s.G1().Point().Mul(a, nil)
bH := s.G2().Point().Mul(b, nil)
ab := s.G1().Scalar().Mul(a, b)
abG := s.G1().Point().Mul(ab, nil)
// e(aG, bG) = e(G,H)^(ab)
p1 := s.Pair(aG, bH)
// e((ab)G,H) = e(G,H)^(ab)
p2 := s.Pair(abG, s.G2().Point().Base())
require.True(t, p1.Equal(p2))
pRandom := s.Pair(aG, s.G2().Point().Pick(s.RandomStream()))
require.False(t, p1.Equal(pRandom))
pRandom = s.Pair(s.G1().Point().Pick(s.RandomStream()), bH)
require.False(t, p1.Equal(pRandom))
}
func TestKyberBLSG2(t *testing.T) {
suite := NewBLS12381Suite()
scheme := bls.NewSchemeOnG2(suite)
test.SchemeTesting(t, scheme)
}
func TestKyberBLSG1(t *testing.T) {
suite := NewBLS12381Suite()
scheme := bls.NewSchemeOnG2(suite)
test.SchemeTesting(t, scheme)
}
func TestKyberThresholdG2(t *testing.T) {
suite := NewBLS12381Suite()
tscheme := tbls.NewThresholdSchemeOnG2(suite)
test.ThresholdTest(t, suite.G1(), tscheme)
}
func TestKyberThresholdG1(t *testing.T) {
suite := NewBLS12381Suite()
tscheme := tbls.NewThresholdSchemeOnG2(suite)
test.ThresholdTest(t, suite.G1(), tscheme)
}