This repository was archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtpke_test.go
156 lines (133 loc) · 3.32 KB
/
tpke_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
package tpke
import (
"testing"
bls "github.com/kilic/bls12-381"
)
func TestTPKE(t *testing.T) {
size := 7
threshold := 5
dkg := NewDKG(size, threshold)
dkg.Prepare()
if err := dkg.VerifyPrepare(); err != nil {
t.Fatalf(err.Error())
}
pubkey := dkg.PublishGlobalPublicKey()
prvkeys := dkg.GetPrivateKeysFromPrepare()
// Encrypt
msg := make([]*bls.PointG1, 1)
msg[0] = RandPG1()
cipherTexts := Encrypt(msg, pubkey)
// Verify ciphertext
if err := cipherTexts[0].Verify(); err != nil {
t.Fatalf("invalid ciphertext.")
}
// Generate shares
shares := decryptShare(cipherTexts, prvkeys)
// Put a wrong share
shares[2][0].pg1 = RandPG1()
// Decrypt
results, err := Decrypt(cipherTexts, shares, pubkey, threshold, dkg.GetScaler())
if err != nil {
t.Fatalf(err.Error())
}
if !bls.NewG1().Equal(msg[0], results[0]) {
t.Fatalf("decryption failed.")
}
}
func TestReshareTPKEOld(t *testing.T) {
size := 7
threshold := 5
dkg := NewDKG(size, threshold)
dkg.Prepare()
if err := dkg.VerifyPrepare(); err != nil {
t.Fatalf(err.Error())
}
pubkey := dkg.PublishGlobalPublicKey()
dkg.Reshare()
if err := dkg.VerifyReshare(); err != nil {
t.Fatalf(err.Error())
}
prvkeys := dkg.GetPrivateKeysFromPrepare()
// Encrypt with old key
msg := make([]*bls.PointG1, 1)
msg[0] = RandPG1()
cipherTexts := Encrypt(msg, pubkey)
cipherTexts[0].fromLastRound = true
// Verify ciphertext
if err := cipherTexts[0].Verify(); err != nil {
t.Fatalf("invalid ciphertext.")
}
// Generate shares
shares := decryptShare(cipherTexts, prvkeys)
// Put a wrong share
shares[2][0].pg1 = RandPG1()
// Decrypt
results, err := Decrypt(cipherTexts, shares, pubkey, threshold, dkg.GetScaler())
if err != nil {
t.Fatalf(err.Error())
}
if !bls.NewG1().Equal(msg[0], results[0]) {
t.Fatalf("decryption failed.")
}
}
func TestReshareTPKENew(t *testing.T) {
size := 7
threshold := 5
dkg := NewDKG(size, threshold)
dkg.Prepare()
if err := dkg.VerifyPrepare(); err != nil {
t.Fatalf(err.Error())
}
dkg.Reshare()
if err := dkg.VerifyReshare(); err != nil {
t.Fatalf(err.Error())
}
dkg.Prepare()
if err := dkg.VerifyPrepare(); err != nil {
t.Fatalf(err.Error())
}
pubkey := dkg.PublishGlobalPublicKey()
prvkeys := dkg.GetPrivateKeysFromPrepare()
// Encrypt with new key
msg := make([]*bls.PointG1, 1)
msg[0] = RandPG1()
cipherTexts := Encrypt(msg, pubkey)
cipherTexts[0].fromLastRound = false
// Verify ciphertext
if err := cipherTexts[0].Verify(); err != nil {
t.Fatalf("invalid ciphertext.")
}
// Generate shares
shares := decryptShare(cipherTexts, prvkeys)
// Put a wrong share
shares[2][0].pg1 = RandPG1()
// Decrypt
results, err := Decrypt(cipherTexts, shares, pubkey, threshold, dkg.GetScaler())
if err != nil {
t.Fatalf(err.Error())
}
if !bls.NewG1().Equal(msg[0], results[0]) {
t.Fatalf("decryption failed.")
}
}
func TestBytesEncoding(t *testing.T) {
ct := &CipherText{
cMsg: &bls.G1One,
bigR: &bls.G1One,
commitment: &bls.G2One,
}
b := ct.ToBytes()
result, err := BytesToCipherText(b)
if err != nil {
t.Fatalf(err.Error())
}
if !bls.NewG1().Equal(ct.cMsg, result.cMsg) {
t.Fatalf("cMsg mismatch.")
}
if !bls.NewG1().Equal(ct.bigR, result.bigR) {
t.Fatalf("bigR mismatch.")
}
if !bls.NewG2().Equal(ct.commitment, result.commitment) {
t.Fatalf("commitment mismatch.")
}
}