-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmsp_test.go
82 lines (66 loc) · 1.63 KB
/
msp_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
package msp
import (
"bytes"
"crypto/rand"
"errors"
"testing"
)
type Database map[string][][]byte
func (d *Database) ValidUser(name string) bool {
_, ok := (*d)[name]
return ok
}
func (d *Database) CanGetShare(name string) bool {
_, ok := (*d)[name]
return ok
}
func (d *Database) GetShare(name string) ([][]byte, error) {
out, ok := (*d)[name]
if ok {
return out, nil
} else {
return nil, errors.New("Not found!")
}
}
func TestMSP(t *testing.T) {
for _, field := range Fields {
db := &Database{
"Alice": [][]byte{},
"Bob": [][]byte{},
"Carl": [][]byte{},
}
sec := make([]byte, field.Size())
rand.Read(sec)
sec[0] &= 63 // Removes first 2 bits of key.
predicate, _ := StringToMSP("(2, (1, Alice, Bob), Carl)")
shares1, err := predicate.DistributeShares(sec, db)
if err != nil {
t.Error(err)
continue
}
shares2, _ := predicate.DistributeShares(sec, db)
if err != nil {
t.Error(err)
continue
}
alice := bytes.Compare(shares1["Alice"][0], shares2["Alice"][0])
bob := bytes.Compare(shares1["Bob"][0], shares2["Bob"][0])
carl := bytes.Compare(shares1["Carl"][0], shares2["Carl"][0])
if alice == 0 && bob == 0 && carl == 0 {
t.Fatalf("Key splitting isn't random! %v %v", shares1, shares2)
}
db1 := Database(shares1)
db2 := Database(shares2)
sec1, err := predicate.RecoverSecret(&db1)
if err != nil {
t.Fatalf("#1: %v", err)
}
sec2, err := predicate.RecoverSecret(&db2)
if err != nil {
t.Fatalf("#2: %v", err)
}
if !(bytes.Compare(sec, sec1) == 0 && bytes.Compare(sec, sec2) == 0) {
t.Fatalf("Secrets derived differed: %v %v %v", sec, sec1, sec2)
}
}
}