/
ccs08.go
431 lines (390 loc) · 12.3 KB
/
ccs08.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
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
// Copyright 2018 ING Bank N.V.
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
/*
This file contains the implementation of the ZKRP scheme proposed in the paper:
Efficient Protocols for Set Membership and Range Proofs
Jan Camenisch, Rafik Chaabouni, abhi shelat
Asiacrypt 2008
*/
package zkproofs
import (
"bytes"
"crypto/rand"
"errors"
"math"
"math/big"
"strconv"
bn256 "github.com/ethereum/go-ethereum/crypto/bn256/google"
// "../crypto/bn256"
)
/*
paramsSet contains elements generated by the verifier, which are necessary for the prover.
This must be computed in a trusted setup.
*/
type paramsSet struct {
signatures map[int64]*bn256.G2
H *bn256.G2
// TODO:must protect the private key
kp keypair
// u determines the amount of signatures we need in the public params.
// Each signature can be compressed to just 1 field element of 256 bits.
// Then the parameters have minimum size equal to 256*u bits.
// l determines how many pairings we need to compute, then in order to improve
// verifier`s performance we want to minize it.
// Namely, we have 2*l pairings for the prover and 3*l for the verifier.
}
/*
paramsUL contains elements generated by the verifier, which are necessary for the prover.
This must be computed in a trusted setup.
*/
type paramsUL struct {
signatures map[string]*bn256.G2
H *bn256.G2
// TODO:must protect the private key
kp keypair
// u determines the amount of signatures we need in the public params.
// Each signature can be compressed to just 1 field element of 256 bits.
// Then the parameters have minimum size equal to 256*u bits.
// l determines how many pairings we need to compute, then in order to improve
// verifier`s performance we want to minize it.
// Namely, we have 2*l pairings for the prover and 3*l for the verifier.
u, l int64
}
/*
proofSet contains the necessary elements for the ZK Set Membership proof.
*/
type proofSet struct {
V *bn256.G2
D, C *bn256.G2
a *bn256.GT
s, t, zsig, zv *big.Int
c, m, zr *big.Int
}
/*
proofUL contains the necessary elements for the ZK proof.
*/
type proofUL struct {
V []*bn256.G2
D, C *bn256.G2
a []*bn256.GT
s, t, zsig, zv []*big.Int
c, m, zr *big.Int
}
/*
SetupSet generates the signature for the elements in the set.
*/
func SetupSet(s []int64) (paramsSet, error) {
var (
i int
p paramsSet
)
p.kp, _ = keygen()
p.signatures = make(map[int64]*bn256.G2)
for i = 0; i < len(s); i++ {
sig_i, _ := sign(new(big.Int).SetInt64(int64(s[i])), p.kp.privk)
p.signatures[s[i]] = sig_i
}
//TODO: protect the 'master' key
h := GetBigInt("18560948149108576432482904553159745978835170526553990798435819795989606410925")
p.H = new(bn256.G2).ScalarBaseMult(h)
return p, nil
}
/*
SetupUL generates the signature for the interval [0,u^l).
The value of u should be roughly b/log(b), but we can choose smaller values in
order to get smaller parameters, at the cost of having worse performance.
*/
func SetupUL(u, l int64) (paramsUL, error) {
var (
i int64
p paramsUL
)
p.kp, _ = keygen()
p.signatures = make(map[string]*bn256.G2)
for i = 0; i < u; i++ {
sig_i, _ := sign(new(big.Int).SetInt64(i), p.kp.privk)
p.signatures[strconv.FormatInt(i, 10)] = sig_i
}
//TODO: protect the 'master' key
h := GetBigInt("18560948149108576432482904553159745978835170526553990798435819795989606410925")
p.H = new(bn256.G2).ScalarBaseMult(h)
p.u = u
p.l = l
return p, nil
}
/*
ProveSet method is used to produce the ZK Set Membership proof.
*/
func ProveSet(x int64, r *big.Int, p paramsSet) (proofSet, error) {
var (
v *big.Int
proof_out proofSet
)
// Initialize variables
proof_out.D = new(bn256.G2)
// proof_out.D.SetInfinity()
_, _, epz, _ := proof_out.D.CurvePoints()
epz.SetZero()
proof_out.m, _ = rand.Int(rand.Reader, bn256.Order)
D := new(bn256.G2)
v, _ = rand.Int(rand.Reader, bn256.Order)
A, ok := p.signatures[x]
if ok {
// D = g^s.H^m
D = new(bn256.G2).ScalarMult(p.H, proof_out.m)
proof_out.s, _ = rand.Int(rand.Reader, bn256.Order)
aux := new(bn256.G2).ScalarBaseMult(proof_out.s)
D.Add(D, aux)
proof_out.V = new(bn256.G2).ScalarMult(A, v)
proof_out.t, _ = rand.Int(rand.Reader, bn256.Order)
proof_out.a = bn256.Pair(G1, proof_out.V)
proof_out.a.ScalarMult(proof_out.a, proof_out.s)
proof_out.a.Neg(proof_out.a)
proof_out.a.Add(proof_out.a, new(bn256.GT).ScalarMult(E, proof_out.t))
} else {
return proof_out, errors.New("Could not generate proof. Element does not belong to the interval.")
}
proof_out.D.Add(proof_out.D, D)
// Consider passing C as input,
// so that it is possible to delegate the commitment computation to an external party.
proof_out.C, _ = Commit(new(big.Int).SetInt64(x), r, p.H)
// Fiat-Shamir heuristic
proof_out.c, _ = HashSet(proof_out.a, proof_out.D)
proof_out.c = Mod(proof_out.c, bn256.Order)
proof_out.zr = Sub(proof_out.m, Multiply(r, proof_out.c))
proof_out.zr = Mod(proof_out.zr, bn256.Order)
proof_out.zsig = Sub(proof_out.s, Multiply(new(big.Int).SetInt64(x), proof_out.c))
proof_out.zsig = Mod(proof_out.zsig, bn256.Order)
proof_out.zv = Sub(proof_out.t, Multiply(v, proof_out.c))
proof_out.zv = Mod(proof_out.zv, bn256.Order)
return proof_out, nil
}
/*
ProveUL method is used to produce the ZKRP proof that secret x belongs to the interval [0,U^L].
*/
func ProveUL(x, r *big.Int, p paramsUL) (proofUL, error) {
var (
i int64
v []*big.Int
proof_out proofUL
)
decx, _ := Decompose(x, p.u, p.l)
// Initialize variables
v = make([]*big.Int, p.l, p.l)
proof_out.V = make([]*bn256.G2, p.l, p.l)
proof_out.a = make([]*bn256.GT, p.l, p.l)
proof_out.s = make([]*big.Int, p.l, p.l)
proof_out.t = make([]*big.Int, p.l, p.l)
proof_out.zsig = make([]*big.Int, p.l, p.l)
proof_out.zv = make([]*big.Int, p.l, p.l)
proof_out.D = new(bn256.G2)
// proof_out.D.SetInfinity()
_, _, epz, _ := proof_out.D.CurvePoints()
epz.SetZero()
proof_out.m, _ = rand.Int(rand.Reader, bn256.Order)
// D = H^m
D := new(bn256.G2).ScalarMult(p.H, proof_out.m)
for i = 0; i < p.l; i++ {
v[i], _ = rand.Int(rand.Reader, bn256.Order)
A, ok := p.signatures[strconv.FormatInt(decx[i], 10)]
if ok {
proof_out.V[i] = new(bn256.G2).ScalarMult(A, v[i])
proof_out.s[i], _ = rand.Int(rand.Reader, bn256.Order)
proof_out.t[i], _ = rand.Int(rand.Reader, bn256.Order)
proof_out.a[i] = bn256.Pair(G1, proof_out.V[i])
proof_out.a[i].ScalarMult(proof_out.a[i], proof_out.s[i])
proof_out.a[i].Neg(proof_out.a[i])
proof_out.a[i].Add(proof_out.a[i], new(bn256.GT).ScalarMult(E, proof_out.t[i]))
ui := new(big.Int).Exp(new(big.Int).SetInt64(p.u), new(big.Int).SetInt64(i), nil)
muisi := new(big.Int).Mul(proof_out.s[i], ui)
muisi = Mod(muisi, bn256.Order)
aux := new(bn256.G2).ScalarBaseMult(muisi)
D.Add(D, aux)
} else {
return proof_out, errors.New("Could not generate proof. Element does not belong to the interval.")
}
}
proof_out.D.Add(proof_out.D, D)
// Consider passing C as input,
// so that it is possible to delegate the commitment computation to an external party.
proof_out.C, _ = Commit(x, r, p.H)
// Fiat-Shamir heuristic
proof_out.c, _ = Hash(proof_out.a, proof_out.D)
proof_out.c = Mod(proof_out.c, bn256.Order)
proof_out.zr = Sub(proof_out.m, Multiply(r, proof_out.c))
proof_out.zr = Mod(proof_out.zr, bn256.Order)
for i = 0; i < p.l; i++ {
proof_out.zsig[i] = Sub(proof_out.s[i], Multiply(new(big.Int).SetInt64(decx[i]), proof_out.c))
proof_out.zsig[i] = Mod(proof_out.zsig[i], bn256.Order)
proof_out.zv[i] = Sub(proof_out.t[i], Multiply(v[i], proof_out.c))
proof_out.zv[i] = Mod(proof_out.zv[i], bn256.Order)
}
return proof_out, nil
}
/*
VerifySet is used to validate the ZK Set Membership proof. It returns true iff the proof is valid.
*/
func VerifySet(proof_out *proofSet, p *paramsSet) (bool, error) {
var (
D *bn256.G2
r1, r2 bool
p1, p2 *bn256.GT
)
// D == C^c.h^ zr.g^zsig ?
D = new(bn256.G2).ScalarMult(proof_out.C, proof_out.c)
D.Add(D, new(bn256.G2).ScalarMult(p.H, proof_out.zr))
aux := new(bn256.G2).ScalarBaseMult(proof_out.zsig)
D.Add(D, aux)
DBytes := D.Marshal()
pDBytes := proof_out.D.Marshal()
r1 = bytes.Equal(DBytes, pDBytes)
r2 = true
// a == [e(V,y)^c].[e(V,g)^-zsig].[e(g,g)^zv]
p1 = bn256.Pair(p.kp.pubk, proof_out.V)
p1.ScalarMult(p1, proof_out.c)
p2 = bn256.Pair(G1, proof_out.V)
p2.ScalarMult(p2, proof_out.zsig)
p2.Neg(p2)
p1.Add(p1, p2)
p1.Add(p1, new(bn256.GT).ScalarMult(E, proof_out.zv))
pBytes := p1.Marshal()
aBytes := proof_out.a.Marshal()
r2 = r2 && bytes.Equal(pBytes, aBytes)
return r1 && r2, nil
}
/*
VerifyUL is used to validate the ZKRP proof. It returns true iff the proof is valid.
*/
func VerifyUL(proof_out *proofUL, p *paramsUL) (bool, error) {
var (
i int64
D *bn256.G2
r1, r2 bool
p1, p2 *bn256.GT
)
// D == C^c.h^ zr.g^zsig ?
D = new(bn256.G2).ScalarMult(proof_out.C, proof_out.c)
D.Add(D, new(bn256.G2).ScalarMult(p.H, proof_out.zr))
for i = 0; i < p.l; i++ {
ui := new(big.Int).Exp(new(big.Int).SetInt64(p.u), new(big.Int).SetInt64(i), nil)
muizsigi := new(big.Int).Mul(proof_out.zsig[i], ui)
muizsigi = Mod(muizsigi, bn256.Order)
aux := new(bn256.G2).ScalarBaseMult(muizsigi)
D.Add(D, aux)
}
DBytes := D.Marshal()
pDBytes := proof_out.D.Marshal()
r1 = bytes.Equal(DBytes, pDBytes)
r2 = true
for i = 0; i < p.l; i++ {
// a == [e(V,y)^c].[e(V,g)^-zsig].[e(g,g)^zv]
p1 = bn256.Pair(p.kp.pubk, proof_out.V[i])
p1.ScalarMult(p1, proof_out.c)
p2 = bn256.Pair(G1, proof_out.V[i])
p2.ScalarMult(p2, proof_out.zsig[i])
p2.Neg(p2)
p1.Add(p1, p2)
p1.Add(p1, new(bn256.GT).ScalarMult(E, proof_out.zv[i]))
pBytes := p1.Marshal()
aBytes := proof_out.a[i].Marshal()
r2 = r2 && bytes.Equal(pBytes, aBytes)
}
return r1 && r2, nil
}
/*
proof contains the necessary elements for the ZK proof.
*/
type proof struct {
p1, p2 proofUL
}
/*
params contains elements generated by the verifier, which are necessary for the prover.
This must be computed in a trusted setup.
*/
type params struct {
p *paramsUL
a, b int64
}
type ccs08 struct {
p *params
x, r *big.Int
proof_out proof
pubk *bn256.G1
}
/*
Setup receives integers a and b, and configures the parameters for the rangeproof scheme.
*/
func (zkrp *ccs08) Setup(a, b int64) error {
// Compute optimal values for u and l
var (
u, l int64
logb float64
p *params
)
if a > b {
zkrp.p = nil
return errors.New("a must be less than or equal to b")
}
p = new(params)
logb = math.Log(float64(b))
if logb != 0 {
// TODO: understand how to find optimal parameters
//u = b / int64(logb)
u = 57
if u != 0 {
l = 0
for i := b; i > 0; i = i / u {
l = l + 1
}
params_out, e := SetupUL(u, l)
p.p = ¶ms_out
p.a = a
p.b = b
zkrp.p = p
return e
} else {
zkrp.p = nil
return errors.New("u is zero")
}
} else {
zkrp.p = nil
return errors.New("log(b) is zero")
}
}
/*
Prove method is responsible for generating the zero knowledge proof.
*/
func (zkrp *ccs08) Prove() error {
ul := new(big.Int).Exp(new(big.Int).SetInt64(zkrp.p.p.u), new(big.Int).SetInt64(zkrp.p.p.l), nil)
// x - b + ul
xb := new(big.Int).Sub(zkrp.x, new(big.Int).SetInt64(zkrp.p.b))
xb.Add(xb, ul)
first, _ := ProveUL(xb, zkrp.r, *zkrp.p.p)
// x - a
xa := new(big.Int).Sub(zkrp.x, new(big.Int).SetInt64(zkrp.p.a))
second, _ := ProveUL(xa, zkrp.r, *zkrp.p.p)
zkrp.proof_out.p1 = first
zkrp.proof_out.p2 = second
return nil
}
/*
Verify is responsible for validating the proof.
*/
func (zkrp *ccs08) Verify() (bool, error) {
first, _ := VerifyUL(&zkrp.proof_out.p1, zkrp.p.p)
second, _ := VerifyUL(&zkrp.proof_out.p2, zkrp.p.p)
return first && second, nil
}