-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_functions.go
More file actions
601 lines (491 loc) · 14.8 KB
/
test_functions.go
File metadata and controls
601 lines (491 loc) · 14.8 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package distIBE
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"math/big"
"reflect"
"sync"
enc "github.com/FairBlock/DistributedIBE/encryption"
//"github.com/aws/aws-sdk-go/service/panorama"
"github.com/drand/kyber"
bls "github.com/drand/kyber-bls12381"
"github.com/drand/kyber/group/mod"
"github.com/drand/kyber/pairing"
)
func H3Tag() []byte {
return []byte("IBE-H3")
}
func h3(s pairing.Suite, sigma, msg []byte) (kyber.Scalar, error) {
h := s.Hash()
if _, err := h.Write(H3Tag()); err != nil {
return nil, fmt.Errorf("err hashing h3 tag: %v", err)
}
if _, err := h.Write(sigma); err != nil {
return nil, fmt.Errorf("err hashing sigma: %v", err)
}
if _, err := h.Write(msg); err != nil {
return nil, fmt.Errorf("err hashing msg: %v", err)
}
// we hash it a first time: buffer = hash("IBE-H3" || sigma || msg)
buffer := h.Sum(nil)
hashable, ok := s.G1().Scalar().(*mod.Int)
if !ok {
return nil, fmt.Errorf("unable to instantiate scalar as a mod.Int")
}
canonicalBitLen := hashable.MarshalSize() * 8
actualBitLen := hashable.M.BitLen()
toMask := canonicalBitLen - actualBitLen
for i := uint16(1); i < 65535; i++ {
h.Reset()
// We will hash iteratively: H(i || H("IBE-H3" || sigma || msg)) until we get a
// value that is suitable as a scalar.
iter := make([]byte, 2)
binary.LittleEndian.PutUint16(iter, i)
_, _ = h.Write(iter)
_, _ = h.Write(buffer)
hashed := h.Sum(nil)
// We then apply masking to our resulting bytes at the bit level
// but we assume that toMask is a few bits, at most 8.
// For instance when using BLS12-381 toMask == 1.
if hashable.BO == mod.BigEndian {
hashed[0] = hashed[0] >> toMask
} else {
hashed[len(hashed)-1] = hashed[len(hashed)-1] >> toMask
}
// NOTE: Here we unmarshal as a test if the buffer is within the modulo
// because we know unmarshal does this test. This implementation
// is almost generic if not for this line. TO make it truly generic
// we would need to add methods to create a scalar from bytes without
// reduction and a method to check if it is within the modulo on the
// Scalar interface.
if err := hashable.UnmarshalBinary(hashed); err == nil {
// fmt.Println("value of i is ", i)
return hashable, nil
}
}
// if we didn't return in the for loop then something is wrong
return nil, fmt.Errorf("rejection sampling failure")
}
func bigFromHex(hex string) *big.Int {
if len(hex) > 1 && hex[:2] == "0x" {
hex = hex[2:]
}
n, _ := new(big.Int).SetString(hex, 16)
return n
}
// n keepers in total, threshold = t, (t+1) of them participated in decryption
func DistributedIBE(n int, t int, ID string, src bytes.Buffer, message string) (bool, error) {
// Setup
s := bls.NewBLS12381Suite()
// buf := make([]byte, 128)
// _, err := rand.Read(buf)
// if err != nil {
// return false, err
// }
// var secretVal []byte = buf
// var qBig = bigFromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
// secret, _ := h3(s, secretVal, []byte("msg"))
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t+1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
//fmt.Println(shares[j].Value)
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Encryption
var cipherData bytes.Buffer
_ = enc.Encrypt(PK, []byte(ID), &cipherData, &src)
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// Aggregating keys to get the secret key for decryption
SK, _ := AggregateSK(s,
sk,
c, []byte(ID))
var plainData bytes.Buffer
// Decryption
_ = enc.Decrypt(PK, SK, &plainData, &cipherData)
// Verify that the decrypted message matches the original message
if !reflect.DeepEqual(string(plainData.Bytes()[:]), message) {
return false, fmt.Errorf("wrong decrypted message: %s VS %s", string(plainData.Bytes()[:]), message)
}
return true, nil
}
// n keepers in total, threshold = t, (t-1) of them participated in decryption
func DistributedIBEFail(n int, t int, ID string, src bytes.Buffer, message string) (bool, error) {
// Setup
s := bls.NewBLS12381Suite()
// buf := make([]byte, 128)
// _, err := rand.Read(buf)
// if err != nil {
// return false, err
// }
// var secretVal []byte = buf
// var qBig = bigFromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
// secret, _ := h3(s, secretVal, []byte("msg"))
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t-1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Encryption
var cipherData bytes.Buffer
_ = enc.Encrypt(PK, []byte(ID), &cipherData, &src)
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// Aggregating keys to get the secret key for decryption
SK, _ := AggregateSK(s,
sk,
c, []byte(ID))
var plainData bytes.Buffer
// Decryption
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
// Verify that the decrypted message matches the original message
if !reflect.DeepEqual(string(plainData.Bytes()[:]), message) {
return false, fmt.Errorf("wrong decrypted message: %s VS %s", string(plainData.Bytes()[:]), message)
}
return true, nil
}
// n keepers in total, threshold = t, (t+1) of them participated in decryption but one commitment is wrong
func DistributedIBEFInvalidCommitment(n int, t int, ID string, src bytes.Buffer, message string) (bool, error) {
// Setup
s := bls.NewBLS12381Suite()
// buf := make([]byte, 128)
// _, err := rand.Read(buf)
// if err != nil {
// return false, err
// }
// var secretVal []byte = buf
// var qBig = bigFromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
// secret, _ := h3(s, secretVal, []byte("msg"))
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t+1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Encryption
var cipherData bytes.Buffer
_ = enc.Encrypt(PK, []byte(ID), &cipherData, &src)
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// chaning the first commitment to something else
c[0] = c[1]
// Aggregating keys to get the secret key for decryption
SK, invalids := AggregateSK(s,
sk,
c, []byte(ID))
if len(invalids) != 0 {
return false, fmt.Errorf("invalids: %d", invalids)
}
var plainData bytes.Buffer
// Decryption
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
// Verify that the decrypted message matches the original message
if !reflect.DeepEqual(string(plainData.Bytes()[:]), message) {
return false, fmt.Errorf("wrong decrypted message: %s VS %s", string(plainData.Bytes()[:]), message)
}
return true, nil
}
// n keepers in total, threshold = t, (t+1) of them participated in decryption but one share is wrong
func DistributedIBEFInvalidShare(n int, t int, ID string, src bytes.Buffer, message string) (bool, error) {
// Setup
s := bls.NewBLS12381Suite()
// buf := make([]byte, 128)
// _, err := rand.Read(buf)
// if err != nil {
// return false, err
// }
// var secretVal []byte = buf
// var qBig = bigFromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
// secret, _ := h3(s, secretVal, []byte("msg"))
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t+1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Encryption
var cipherData bytes.Buffer
_ = enc.Encrypt(PK, []byte(ID), &cipherData, &src)
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// chaning the first extracted key to something else (previous value * 2 in this case)
sk[0].SK = sk[0].SK.Add(sk[0].SK, sk[0].SK)
// Aggregating keys to get the secret key for decryption
SK, invalids := AggregateSK(s,
sk,
c, []byte(ID))
if len(invalids) != 0 {
return false, fmt.Errorf("invalids: %d", invalids)
}
var plainData bytes.Buffer
// Decryption
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
// Verify that the decrypted message matches the original message
if !reflect.DeepEqual(string(plainData.Bytes()[:]), message) {
return false, fmt.Errorf("wrong decrypted message: %s VS %s", string(plainData.Bytes()[:]), message)
}
return true, nil
}
// n keepers in total, threshold = t, (t+1) of them participated in decryption. The ciphertext is changed to become invalid.
func DistributedIBEWrongCiphertext(n int, t int, ID string, src bytes.Buffer, message string) (bool, error) {
// Setup
s := bls.NewBLS12381Suite()
// buf := make([]byte, 128)
// _, err := rand.Read(buf)
// if err != nil {
// return false, err
// }
// var secretVal []byte = buf
// var qBig = bigFromHex("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001")
// secret, _ := h3(s, secretVal, []byte("msg"))
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t+1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Encryption
var cipherData bytes.Buffer
_ = enc.Encrypt(PK, []byte(ID), &cipherData, &src)
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// Aggregating keys to get the secret key for decryption
SK, _ := AggregateSK(s,
sk,
c, []byte(ID))
var plainData bytes.Buffer
// Adding random string to ciphertext
cipherData.WriteString("hihihihihi")
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
// Verify that the decrypted message matches the original message
if !reflect.DeepEqual(string(plainData.Bytes()[:]), message) {
return false, fmt.Errorf("wrong decrypted message: %s VS %s", string(plainData.Bytes()[:]), message)
}
return true, nil
}
func Config(n int, t int, ID string) (kyber.Point, kyber.Point, error) {
// Setup
s := bls.NewBLS12381Suite()
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t+1 {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
// Extracting the keys using shares
var sk []ExtractedKey
for k := 0; k < n; k++ {
if signers[k] == 1 {
sk = append(sk, Extract(s, shares[k].Value, uint32(k+1), []byte(ID)))
}
}
// Aggregating keys to get the secret key for decryption
SK, _ := AggregateSK(s,
sk,
c, []byte(ID))
return PK, SK, nil
}
func Encrypt(PK kyber.Point, ID string, src bytes.Buffer, message string) (bytes.Buffer, error) {
// Encryption
var cipherData bytes.Buffer
err := enc.Encrypt(PK, []byte(ID), &cipherData, &src)
if err != nil {
return bytes.Buffer{}, err
}
return cipherData, nil
}
func Decrypt(PK kyber.Point, SK kyber.Point, cipherData bytes.Buffer) (bool, error) {
var plainData bytes.Buffer
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
return true, nil
}
func DecryptParallel(PK kyber.Point, SK kyber.Point, cipherData bytes.Buffer, wg *sync.WaitGroup) (bool, error) {
defer wg.Done()
var plainData bytes.Buffer
err := enc.Decrypt(PK, SK, &plainData, &cipherData)
if err != nil {
return false, err
}
return true, nil
}
func Shares(n int, t int, ID string) ([]Commitment, []Share, []int, error) {
// Setup
s := bls.NewBLS12381Suite()
signers := []int{}
for i := 0; i < n; i++ {
signers = append(signers, 0)
}
j := 0
for j < t {
randomVal, _ := rand.Int(rand.Reader, big.NewInt(int64(n)))
if signers[randomVal.Int64()] == 0 {
signers[randomVal.Int64()] = 1
j++
}
}
// generating secret shares
shares, PK, _, _ := GenerateShares(uint32(n), uint32(t))
// Public Key
_ = PK
// Generating commitments
var c []Commitment
for j := 0; j < n; j++ {
if signers[j] == 1 {
c = append(c, Commitment{s.G1().Point().Mul(shares[j].Value, s.G1().Point().Base()), uint32(j + 1)})
}
}
return c, shares, signers, nil
}
func VSSTest(n uint32, t uint32) error {
shares, _, commitments, err := GenerateShares(n, t)
if err != nil {
return err
}
for i := 0; uint32(i) < n; i++ {
res := VerifyVSSShare(shares[i], commitments)
if !res {
return fmt.Errorf("wrong share")
}
}
return nil
}