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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package bls
import "testing"
import "strconv"
var curve = CurveFp382_1
//var curve = CurveFp254BNb
var unitN = 0
func testPre(t *testing.T) {
t.Log("init")
{
var id ID
id.Set([]uint64{6, 5, 4, 3, 2, 1}[0:unitN])
t.Log("id :", id)
var id2 ID
err := id2.SetStr(id.String())
if err != nil {
t.Fatal(err)
}
t.Log("id2:", id2)
}
{
var sec SecretKey
sec.SetArray([]uint64{1, 2, 3, 4, 5, 6}[0:unitN])
t.Log("sec=", sec)
}
t.Log("create secret key")
m := "this is a bls sample for go"
var sec SecretKey
sec.Init()
t.Log("sec:", sec)
t.Log("create public key")
pub := sec.GetPublicKey()
t.Log("pub:", pub)
sign := sec.Sign(m)
t.Log("sign:", sign)
if !sign.Verify(pub, m) {
t.Error("Signature does not verify")
}
// How to make array of SecretKey
{
sec := make([]SecretKey, 3)
for i := 0; i < len(sec); i++ {
sec[i].Init()
t.Log("sec=", sec[i].String())
}
}
}
func testStringConversion(t *testing.T) {
t.Log("testRecoverSecretKey")
var sec SecretKey
var s string
if unitN == 6 {
s = "16798108731015832284940804142231733909759579603404752749028378864165570215949"
} else {
s = "40804142231733909759579603404752749028378864165570215949"
}
err := sec.SetStr(s)
if err != nil {
t.Fatal(err)
}
t.Log("String: ", s, sec.String())
s = "401035055535747319451436327113007154621327258807739504261475863403006987855"
err = sec.SetStr(s)
if err != nil {
t.Fatal(err)
}
t.Log("String: ", s, sec.String())
}
func testRecoverSecretKey(t *testing.T) {
t.Log("testRecoverSecretKey")
k := 3000
var sec SecretKey
sec.Init()
// make master secret key
msk := sec.GetMasterSecretKey(k)
n := k
secVec := make([]SecretKey, n)
idVec := make([]ID, n)
for i := 0; i < n; i++ {
idVec[i].Set([]uint64{uint64(i), 1, 2, 3, 4, 5}[0:unitN])
secVec[i].Set(msk, &idVec[i])
}
// recover sec2 from secVec and idVec
var sec2 SecretKey
sec2.Recover(secVec, idVec)
if sec.String() != sec2.String() {
t.Errorf("Mismatch in recovered secret key:\n %s\n %s.", sec.String(), sec2.String())
}
}
func testSign(t *testing.T) {
m := "testSign"
t.Log(m)
var sec0 SecretKey
sec0.Init()
pub0 := sec0.GetPublicKey()
s0 := sec0.Sign(m)
if !s0.Verify(pub0, m) {
t.Error("Signature does not verify")
}
k := 3
msk := sec0.GetMasterSecretKey(k)
mpk := GetMasterPublicKey(msk)
idTbl := []uint64{3, 5, 193, 22, 15}
n := len(idTbl)
secVec := make([]SecretKey, n)
pubVec := make([]PublicKey, n)
signVec := make([]Sign, n)
idVec := make([]ID, n)
for i := 0; i < n; i++ {
idVec[i].Set([]uint64{idTbl[i], 0, 0, 0, 0, 0}[0:unitN])
t.Logf("idVec[%d]=%s\n", i, idVec[i].String())
secVec[i].Set(msk, &idVec[i])
pubVec[i].Set(mpk, &idVec[i])
t.Logf("pubVec[%d]=%s\n", i, pubVec[i].String())
if pubVec[i].String() != secVec[i].GetPublicKey().String() {
t.Error("Pubkey derivation does not match")
}
signVec[i] = *secVec[i].Sign(m)
if !signVec[i].Verify(&pubVec[i], m) {
t.Error("Pubkey derivation does not match")
}
}
var sec1 SecretKey
sec1.Recover(secVec, idVec)
if sec0.String() != sec1.String() {
t.Error("Mismatch in recovered seckey.")
}
var pub1 PublicKey
pub1.Recover(pubVec, idVec)
if pub0.String() != pub1.String() {
t.Error("Mismatch in recovered pubkey.")
}
var s1 Sign
s1.Recover(signVec, idVec)
if s0.String() != s1.String() {
t.Error("Mismatch in recovered signature.")
}
}
func testAdd(t *testing.T) {
t.Log("testAdd")
var sec1 SecretKey
var sec2 SecretKey
sec1.Init()
sec2.Init()
pub1 := sec1.GetPublicKey()
pub2 := sec2.GetPublicKey()
m := "test test"
sign1 := sec1.Sign(m)
sign2 := sec2.Sign(m)
t.Log("sign1 :", sign1)
sign1.Add(sign2)
t.Log("sign1 add:", sign1)
pub1.Add(pub2)
if !sign1.Verify(pub1, m) {
t.Fail()
}
}
func testPop(t *testing.T) {
t.Log("testPop")
var sec SecretKey
sec.Init()
pop := sec.GetPop()
if !pop.VerifyPop(sec.GetPublicKey()) {
t.Errorf("Valid Pop does not verify")
}
sec.Init()
if pop.VerifyPop(sec.GetPublicKey()) {
t.Errorf("Invalid Pop verifies")
}
}
func testData(t *testing.T) {
t.Log("testData")
var sec1, sec2 SecretKey
sec1.Init()
s := sec1.GetData()
err := sec2.SetData(s)
if err != nil {
t.Fatal(err)
}
if !sec1.IsSame(&sec2) {
t.Error("SecretKey not same")
}
pub1 := sec1.GetPublicKey()
s = pub1.GetData()
var pub2 PublicKey
err = pub2.SetData(s)
if err != nil {
t.Fatal(err)
}
if !pub1.IsSame(&pub2) {
t.Error("PublicKey not same")
}
m := "doremi"
sign1 := sec1.Sign(m)
s = sign1.GetData()
var sign2 Sign
err = sign2.SetData(s)
if err != nil {
t.Fatal(err)
}
if !sign1.IsSame(&sign2) {
t.Error("Sign not same")
}
}
func BenchmarkPubkeyFromSeckey(b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
for n := 0; n < b.N; n++ {
sec.Init()
b.StartTimer()
sec.GetPublicKey()
b.StopTimer()
}
}
func BenchmarkSigning(b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
for n := 0; n < b.N; n++ {
sec.Init()
b.StartTimer()
sec.Sign(strconv.Itoa(n))
b.StopTimer()
}
}
func BenchmarkValidation(b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
for n := 0; n < b.N; n++ {
sec.Init()
pub := sec.GetPublicKey()
m := strconv.Itoa(n)
sig := sec.Sign(m)
b.StartTimer()
sig.Verify(pub, m)
b.StopTimer()
}
}
func benchmarkDeriveSeckeyShare(k int, b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
sec.Init()
msk := sec.GetMasterSecretKey(k)
var id ID
for n := 0; n < b.N; n++ {
id.Set([]uint64{1, 2, 3, 4, 5, uint64(n)})
b.StartTimer()
sec.Set(msk, &id)
b.StopTimer()
}
}
//func BenchmarkDeriveSeckeyShare100(b *testing.B) { benchmarkDeriveSeckeyShare(100, b) }
//func BenchmarkDeriveSeckeyShare200(b *testing.B) { benchmarkDeriveSeckeyShare(200, b) }
func BenchmarkDeriveSeckeyShare500(b *testing.B) { benchmarkDeriveSeckeyShare(500, b) }
//func BenchmarkDeriveSeckeyShare1000(b *testing.B) { benchmarkDeriveSeckeyShare(1000, b) }
func benchmarkRecoverSeckey(k int, b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
sec.Init()
msk := sec.GetMasterSecretKey(k)
// derive n shares
n := k
secVec := make([]SecretKey, n)
idVec := make([]ID, n)
for i := 0; i < n; i++ {
idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)})
secVec[i].Set(msk, &idVec[i])
}
// recover from secVec and idVec
var sec2 SecretKey
b.StartTimer()
for n := 0; n < b.N; n++ {
sec2.Recover(secVec, idVec)
}
}
func BenchmarkRecoverSeckey100(b *testing.B) { benchmarkRecoverSeckey(100, b) }
func BenchmarkRecoverSeckey200(b *testing.B) { benchmarkRecoverSeckey(200, b) }
func BenchmarkRecoverSeckey500(b *testing.B) { benchmarkRecoverSeckey(500, b) }
func BenchmarkRecoverSeckey1000(b *testing.B) { benchmarkRecoverSeckey(1000, b) }
func benchmarkRecoverSignature(k int, b *testing.B) {
b.StopTimer()
Init(curve)
var sec SecretKey
sec.Init()
msk := sec.GetMasterSecretKey(k)
// derive n shares
n := k
idVec := make([]ID, n)
secVec := make([]SecretKey, n)
signVec := make([]Sign, n)
for i := 0; i < n; i++ {
idVec[i].Set([]uint64{1, 2, 3, 4, 5, uint64(i)})
secVec[i].Set(msk, &idVec[i])
signVec[i] = *secVec[i].Sign("test message")
}
// recover signature
var sig Sign
b.StartTimer()
for n := 0; n < b.N; n++ {
sig.Recover(signVec, idVec)
}
}
func BenchmarkRecoverSignature100(b *testing.B) { benchmarkRecoverSignature(100, b) }
func BenchmarkRecoverSignature200(b *testing.B) { benchmarkRecoverSignature(200, b) }
func BenchmarkRecoverSignature500(b *testing.B) { benchmarkRecoverSignature(500, b) }
func BenchmarkRecoverSignature1000(b *testing.B) { benchmarkRecoverSignature(1000, b) }
func test(t *testing.T, c int) {
Init(c)
unitN = GetOpUnitSize()
t.Logf("unitN=%d\n", unitN)
testPre(t)
testRecoverSecretKey(t)
testAdd(t)
testSign(t)
testPop(t)
testData(t)
testStringConversion(t)
}
func TestMain(t *testing.T) {
t.Logf("GetMaxOpUnitSize() = %d\n", GetMaxOpUnitSize())
t.Log("CurveFp254BNb")
test(t, CurveFp254BNb)
if GetMaxOpUnitSize() == 6 {
t.Log("CurveFp382_1")
test(t, CurveFp382_1)
t.Log("CurveFp382_2")
test(t, CurveFp382_2)
}
}
|