-
Notifications
You must be signed in to change notification settings - Fork 0
/
vss_ibe_test.go
183 lines (147 loc) · 4.49 KB
/
vss_ibe_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
package tibe
import (
"github.com/stretchr/testify/require"
"strconv"
"testing"
)
func TestVSSIBEEncryptDecrypt(t *testing.T) {
p := NewRandomPoly(5)
vb := newShareableIbeScheme(p)
sk := vb.Decrypter([]byte("key1"))
c, err := vb.PK.Encrypt([]byte("key1"), []byte("check"))
require.NoError(t, err)
o, err := sk.Decrypt(c)
require.NoError(t, err)
require.Equal(t, o, []byte("check"))
}
// Example on how to verify the shares of a VSSIbeNode. (you must gain access to the exponent polynomial of p - the node's random polynomial)
// in a distributed setting, you'd need to use some reliable broadcast mechanism (can be all2all broadcasts, concensus mechanism etc.).
func TestVerifyShare(t *testing.T) {
p := NewRandomPoly(5)
vssNode := NewNode(p)
shrs, err := vssNode.VssShares(10)
require.NoError(t, err)
expoPoly := p.GetExponentPoly()
for _, shr := range shrs {
require.True(t, shr.ExponentPoly.Equal(expoPoly))
expoPoly.VerifyShare(uint64(shr.Index), shr.Value)
vssNode.ReceiveShare("myself", shr)
}
}
func TestVSSIBEReconstructKeyForSpecificIDUsingNodes(t *testing.T) {
nodes := []VssIbeNode{}
for i := 0; i < 10; i++ {
p := NewRandomPoly(5)
nodes = append(nodes, NewNode(p))
}
// send shares:
for i, node := range nodes {
shrs, err := node.VssShares(len(nodes))
require.NoError(t, err)
for j, ibeNode := range nodes {
ibeNode.ReceiveShare(strconv.Itoa(i), shrs[j])
}
}
keyID := []byte("key1")
votes := make([]Vote, len(nodes))
for i, node := range nodes {
v, err := node.Vote("0", keyID)
require.NoError(t, err)
votes[i] = v
}
// reconstruct the key:
dec, err := nodes[3].ReconstructDecrypter("0", votes)
require.NoError(t, err)
// comparing keys:
require.Equal(t, dec.(idBasedPrivateKey).Bytes(), nodes[0].Decrypter(keyID).(idBasedPrivateKey).Bytes())
// ensuring both can decrypt the same things:
mk, err := nodes[0].GetMasterPublicKey()
require.NoError(t, err)
c, err := mk.Encrypt(keyID, []byte("check"))
require.NoError(t, err)
require.NotEqual(t, c.Encrypted, []byte("check")) // ensuring the ciphertext is not equal to the plaintext.
msg, err := dec.Decrypt(c.Copy()) // using copy to ensure we do not change the cipher in between...
require.NoError(t, err)
msg2, err := nodes[0].Decrypter(keyID).Decrypt(c.Copy())
require.NoError(t, err)
require.Equal(t, msg, msg2)
}
func TestVSSIBEReconstructKeyForSpecificID(t *testing.T) {
p := NewRandomPoly(5)
vb := newShareableIbeScheme(p)
shrs, err := vb.VssShares(10)
require.NoError(t, err)
// create Votes using the shares to get a specific key:
keyname := []byte("key1")
votes := make([]Vote, len(shrs))
for _, shr := range shrs {
tmp := g2Hash(keyname)
tmp.ScalarMult(shr.Value, tmp)
votes[shr.Index-1] = Vote{shr.Index, keyname, tmp}
}
c, err := vb.PK.Encrypt(keyname, []byte("check"))
require.NoError(t, err)
nd := NewNode(p)
nd.ReceiveShare("0", VssShare{
ExponentPoly: shrs[0].ExponentPoly,
PolyShare: PolyShare{},
})
sk, err := nd.ReconstructDecrypter("0", votes)
require.NoError(t, err)
msg, err := sk.Decrypt(c)
require.NoError(t, err)
require.Equal(t, msg, []byte("check"))
}
func BenchmarkReconstructIbeDecryptor(b *testing.B) {
p := NewRandomPoly(50)
vb := newShareableIbeScheme(p)
shrs, err := vb.VssShares(100)
require.NoError(b, err)
// create Votes using the shares to get a specific key:
keyname := []byte("key1")
votes := make([]Vote, len(shrs))
for _, shr := range shrs {
tmp := g2Hash(keyname)
tmp.ScalarMult(shr.Value, tmp)
votes[shr.Index-1] = Vote{shr.Index, keyname, tmp}
}
for _, vote := range votes {
shrs[0].ExponentPoly.GetPublicShare(uint64(vote.Index))
}
nd := NewNode(p)
nd.ReceiveShare("0", VssShare{
ExponentPoly: shrs[0].ExponentPoly,
PolyShare: PolyShare{},
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := nd.ReconstructDecrypter("0", votes)
require.NoError(b, err)
}
}
func BenchmarkEncryptionDecryption(b *testing.B) {
ns := NewNode(NewRandomPoly(50))
ID := []byte("key1")
msg := []byte("check")
b.Run("Encryption", func(b *testing.B) {
pk, err := ns.GetMasterPublicKey()
require.NoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := pk.Encrypt(ID, msg)
require.NoError(b, err)
}
})
b.Run("Decryption", func(b *testing.B) {
pk, err := ns.GetMasterPublicKey()
require.NoError(b, err)
c, err := pk.Encrypt(ID, msg)
require.NoError(b, err)
sk := ns.Decrypter(ID)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := sk.Decrypt(c.Copy())
require.NoError(b, err)
}
})
}