-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathabc_test.go
274 lines (215 loc) · 7.61 KB
/
abc_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
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
package zksigma
import (
"crypto/rand"
"math/big"
"testing"
)
// TestABCProof tests if the ABC Proof can generate and verify.
func TestABCProof(t *testing.T) {
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
value, _ := rand.Int(rand.Reader, big.NewInt(10000000000)) // "realistic rarnge"
ua, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
A := TestCurve.Mult(TestCurve.H, ua) // uaH
temp := TestCurve.Mult(TestCurve.G, value) // value(G)
// A = vG + uaH
A = TestCurve.Add(A, temp)
AToken := TestCurve.Mult(PK, ua)
aProof, status := NewABCProof(TestCurve, A, AToken, value, sk, Right)
if status != nil {
proofStatus(status.(*errorProof))
t.Logf("ABCProof RIGHT failed to generate!\n")
t.Fatalf("ABCProof RIGHT failed\n")
}
check, err := aProof.Verify(TestCurve, A, AToken)
if !check || err != nil {
t.Logf("ABCProof RIGHT Failed to verify!\n")
t.Fatalf("ABCVerify RIGHT failed\n")
}
A = TestCurve.Mult(TestCurve.H, ua)
aProof, status = NewABCProof(TestCurve, A, AToken, big.NewInt(0), sk, Left)
if status != nil {
proofStatus(status.(*errorProof))
t.Logf("ABCProof LEFT failed to generate!\n")
t.Fatalf("ABCProof LEFT failed\n")
}
check, err = aProof.Verify(TestCurve, A, AToken)
if !check || err != nil {
t.Logf("ABCProof LEFT Failed to verify!\n")
t.Fatalf("ABCVerify LEFT failed\n")
}
A, ua, err = PedCommit(TestCurve, big.NewInt(1000))
if err != nil {
t.Fatalf("%v\n", err)
}
AToken = TestCurve.Mult(PK, ua)
aProof, status = NewABCProof(TestCurve, A, AToken, big.NewInt(1001), sk, Right)
if status != nil {
t.Logf("False proof generation succeeded! (bad)\n")
t.Fatalf("ABCProve generated for false proof\n")
}
t.Logf("Next ABCVerify should catch false proof\n")
check, err = aProof.Verify(TestCurve, A, AToken)
if check || err == nil {
t.Logf("ABCVerify: should have failed on false proof check!\n")
t.Fatalf("ABCVerify: not working...\n")
}
}
// TestABCProofSerialization tests if the ABC Proof can generate, serialize, deserialize, and then verify.
func TestABCProofSerialization(t *testing.T) {
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
value, _ := rand.Int(rand.Reader, big.NewInt(10000000000)) // "realistic rarnge"
ua, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
A := TestCurve.Mult(TestCurve.H, ua) // uaH
temp := TestCurve.Mult(TestCurve.G, value) // value(G)
// A = vG + uaH
A = TestCurve.Add(A, temp)
AToken := TestCurve.Mult(PK, ua)
aProof, status := NewABCProof(TestCurve, A, AToken, value, sk, Right)
if status != nil {
proofStatus(status.(*errorProof))
t.Logf("ABCProof RIGHT failed to generate!\n")
t.Fatalf("ABCProof RIGHT failed\n")
}
aProof, status = NewABCProofFromBytes(aProof.Bytes())
if status != nil {
proofStatus(status.(*errorProof))
t.Fatalf("ABCProof failed to deserialize!\n")
}
check, err := aProof.Verify(TestCurve, A, AToken)
if !check || err != nil {
t.Fatalf("ABCVerify failed: %s\n", err.Error())
}
}
// TestBreakABCProve tests if the ABC Proof can will catch invalid proofs.
func TestBreakABCProve(t *testing.T) {
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
value, _ := rand.Int(rand.Reader, big.NewInt(10000000000)) // "realistic rarnge"
ua, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
CM := TestCurve.Mult(TestCurve.H, ua) // uaH
temp := TestCurve.Mult(TestCurve.G, value) // value(G)
// A = vG + uaH
CM = TestCurve.Add(CM, temp)
CMTok := TestCurve.Mult(PK, ua)
u1, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
u2, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
u3, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
ub, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
uc, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
B := ECPoint{}
C := ECPoint{}
CToken := TestCurve.Mult(TestCurve.H, uc)
// B = 2/v
x := new(big.Int).ModInverse(value, TestCurve.C.Params().N)
B = PedCommitR(TestCurve, new(big.Int).Mul(big.NewInt(2), x), ub)
// C = 2G + ucH, the 2 here is the big deal
C = PedCommitR(TestCurve, big.NewInt(2), uc)
disjuncAC, _ := NewDisjunctiveProof(TestCurve, CMTok, CM, TestCurve.H, TestCurve.Sub(C, TestCurve.Mult(TestCurve.G, big.NewInt(2))), uc, Right)
// CMTok is Ta for the rest of the proof
// T1 = u1G + u2Ta
// u1G
u1G := TestCurve.Mult(TestCurve.G, u1)
// u2Ta
u2Ta := TestCurve.Mult(CMTok, u2)
// Sum the above two
T1X, T1Y := TestCurve.C.Add(u1G.X, u1G.Y, u2Ta.X, u2Ta.Y)
// T2 = u1B + u3H
// u1B
u1B := TestCurve.Mult(B, u1)
// u3H
u3H := TestCurve.Mult(TestCurve.H, u3)
// Sum of the above two
T2X, T2Y := TestCurve.C.Add(u1B.X, u1B.Y, u3H.X, u3H.Y)
// c = HASH(G,H,CM,CMTok,B,C,T1,T2)
Challenge := GenerateChallenge(TestCurve, TestCurve.G.Bytes(), TestCurve.H.Bytes(),
CM.Bytes(), CMTok.Bytes(),
B.Bytes(), C.Bytes(),
T1X.Bytes(), T1Y.Bytes(),
T2X.Bytes(), T2Y.Bytes())
// j = u1 + v * c , can be though of as s1
j := new(big.Int).Add(u1, new(big.Int).Mul(value, Challenge))
j = new(big.Int).Mod(j, TestCurve.C.Params().N)
// k = u2 + inv(sk) * c
// inv(sk)
isk := new(big.Int).ModInverse(sk, TestCurve.C.Params().N)
k := new(big.Int).Add(u2, new(big.Int).Mul(isk, Challenge))
k = new(big.Int).Mod(k, TestCurve.C.Params().N)
// l = u3 + (uc - v * ub) * c
temp1 := new(big.Int).Sub(uc, new(big.Int).Mul(value, ub))
l := new(big.Int).Add(u3, new(big.Int).Mul(temp1, Challenge))
evilProof := &ABCProof{
B,
C,
ECPoint{T1X, T1Y},
ECPoint{T2X, T2Y},
Challenge,
j, k, l, CToken,
disjuncAC}
t.Logf("Attempting to pass malicious true proof into verification function\n")
t.Logf("This test should throw a couple error messages in debug\n")
check, err := evilProof.Verify(TestCurve, CM, CMTok)
if check || err == nil {
t.Logf("ABCVerify - EVIL: accepted attack input! c = 2, should fail...\n")
t.Fatalf("ABCVerify - EVIL: failed to catch attack!\n")
}
}
func BenchmarkABCProve_0(b *testing.B) {
value := big.NewInt(0)
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
CM, randVal, err := PedCommit(TestCurve, value)
if err != nil {
b.Fatalf("%v\n", err)
}
CMTok := TestCurve.Mult(PK, randVal)
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
NewABCProof(TestCurve, CM, CMTok, value, sk, Left)
}
}
func BenchmarkABCProve_1(b *testing.B) {
value, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
CM, randVal, err := PedCommit(TestCurve, value)
if err != nil {
b.Fatalf("%v\n", err)
}
CMTok := TestCurve.Mult(PK, randVal)
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
NewABCProof(TestCurve, CM, CMTok, value, sk, Right)
}
}
func BenchmarkABCVerify_0(b *testing.B) {
value := big.NewInt(0)
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
CM, randVal, err := PedCommit(TestCurve, value)
if err != nil {
b.Fatalf("%v\n", err)
}
CMTok := TestCurve.Mult(PK, randVal)
proof, _ := NewABCProof(TestCurve, CM, CMTok, value, sk, Left)
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
proof.Verify(TestCurve, CM, CMTok)
}
}
func BenchmarkABCVerify_1(b *testing.B) {
value, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
sk, _ := rand.Int(rand.Reader, TestCurve.C.Params().N)
PK := TestCurve.Mult(TestCurve.H, sk)
CM, randVal, err := PedCommit(TestCurve, value)
if err != nil {
b.Fatalf("%v\n", err)
}
CMTok := TestCurve.Mult(PK, randVal)
proof, _ := NewABCProof(TestCurve, CM, CMTok, value, sk, Right)
b.ResetTimer()
for ii := 0; ii < b.N; ii++ {
proof.Verify(TestCurve, CM, CMTok)
}
}