/
range_proof.go
423 lines (347 loc) · 8.46 KB
/
range_proof.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
// Package kms
package kms
import (
"crypto/rand"
"io"
"math/big"
"github.com/chain5j/chain5j-multi-sign/paillier"
)
// / Zero-knowledge range proof that a value x<q/3 lies in interval [0,q].
// /
// / The verifier is given only c = ENC(ek,x).
// / The prover has input x, dk, r (randomness used for calculating c)
// / It is assumed that q is known to both.
// /
// / References:
// / - Appendix A in [Lindell'17](https://eprint.iacr.org/2017/552)
// / - Section 1.2.2 in [Boudot '00](https://www.iacr.org/archive/eurocrypt2000/1807/18070437-new.pdf)
// /
// / This is an interactive version of the proof, assuming only DCRA which is alreasy assumed for Paillier cryptosystem security
func NewRangeProofVerifier(q3 *big.Int, accuracy int) (*RangeProofVerifier, error) {
challenge, err := randBitSlice(accuracy)
if err != nil {
return nil, err
}
comm, nonce, err := NewCommit(challenge)
if err != nil {
return nil, err
}
return &RangeProofVerifier{
Q3: q3,
Challenge: challenge,
Comm: comm,
Nonce: nonce,
Accuracy: accuracy,
}, nil
}
func (p *RangeProofVerifier) ReceiveCtxt(c *big.Int, ppk *paillier.PublicKey, ctxtPairs []CiphertextPair) {
p.C = c
p.PPK = ppk
// TODO(conner): verify nil-ness of ctxts
p.CtxtPairs = ctxtPairs
}
type RangeProofVerifier struct {
C *big.Int
PPK *paillier.PublicKey
Q3 *big.Int
Challenge BitSlice
Comm Commitments
Nonce Nonce
Accuracy int
CtxtPairs []CiphertextPair
}
type RangeProofProver struct {
X *big.Int
R *big.Int
PSK *paillier.PrivateKey
Q *big.Int
Q3 *big.Int
ChallengeComm Commitments
Accuracy int
SecPairs []SecretPair
CtxtPairs []CiphertextPair
}
type SecretPair struct {
W1 *big.Int
R1 *big.Int
W2 *big.Int
R2 *big.Int
}
func NewSecretPairs(size int) []SecretPair {
return make([]SecretPair, size)
}
type CiphertextPair struct {
C1 *big.Int
C2 *big.Int
}
func NewCiphertextPairs(size int) []CiphertextPair {
return make([]CiphertextPair, size)
}
type BitSlice []byte
func (b BitSlice) Bit(i int) byte {
byt := i / 8
bit := i % 8
return (b[byt] >> uint(8-bit)) & 0x01
}
func NewRangeProofProver(x *big.Int, r *big.Int, q *big.Int, q3 *big.Int, psk *paillier.PrivateKey, comm Commitments, accuracy int) (*RangeProofProver, error) {
secPairs := NewSecretPairs(accuracy)
ctxtPairs := NewCiphertextPairs(accuracy)
flipBits, err := randBitSlice(accuracy)
if err != nil {
return nil, err
}
prover := &RangeProofProver{
X: x,
R: r,
Q: q,
Q3: q3,
PSK: psk,
ChallengeComm: comm,
Accuracy: accuracy,
SecPairs: secPairs,
CtxtPairs: ctxtPairs,
}
for i := 0; i < prover.Accuracy; i++ {
flipi := flipBits.Bit(i)
err := prover.initInstance(i, flipi)
if err != nil {
return nil, err
}
}
return prover, nil
}
func (p *RangeProofProver) initInstance(i int, flipi byte) error {
// Sample w1 in {q3, ... , 2*q3}.
w1, err := rand.Int(rand.Reader, p.Q3)
if err != nil {
return err
}
w1.Add(w1, p.Q3)
// Compute w2 = w1 - q3.
w2 := new(big.Int)
w2.Sub(w1, p.Q3)
// Sample k1 and r2 in N.
r1, err := rand.Int(rand.Reader, p.PSK.PublicKey.N)
if err != nil {
return err
}
r2, err := rand.Int(rand.Reader, p.PSK.PublicKey.N)
if err != nil {
return err
}
// Swap the position of 1 and 2 with probability 1/2.
switch flipi {
case 0:
p.SecPairs[i] = SecretPair{
W1: w1,
R1: r1,
W2: w2,
R2: r2,
}
case 1:
p.SecPairs[i] = SecretPair{
W1: w2,
R1: r2,
W2: w1,
R2: r1,
}
}
// Compute c1 = Enc(w1, k1) and c2 = Enc(w2, r2).
c1, err := paillier.EncryptWithNonce(
&p.PSK.PublicKey, p.SecPairs[i].R1, p.SecPairs[i].W1.Bytes(),
)
if err != nil {
return err
}
c2, err := paillier.EncryptWithNonce(
&p.PSK.PublicKey, p.SecPairs[i].R2, p.SecPairs[i].W2.Bytes(),
)
if err != nil {
return err
}
p.CtxtPairs[i] = CiphertextPair{
C1: c1,
C2: c2,
}
return nil
}
type ProofPair struct {
J byte `json:"j"`
W1 *big.Int `json:"w1,omitempty"`
R1 *big.Int `json:"k1,omitempty"`
W2 *big.Int `json:"w2,omitempty"`
R2 *big.Int `json:"r2,omitempty"`
}
func NewProofPairs(size int) []ProofPair {
return make([]ProofPair, size)
}
type RangeProof struct {
CtxtPairs []CiphertextPair
ProofPairs []ProofPair
}
func (p *RangeProofProver) Prove(challenge BitSlice, nonce *Nonce) ([]ProofPair, error) {
err := p.ChallengeComm.Verify(challenge, nonce)
if err != nil {
return nil, err
}
proofPairs := NewProofPairs(p.Accuracy)
for i := 0; i < p.Accuracy; i++ {
ei := challenge.Bit(i)
err := p.proveInstance(i, ei, proofPairs)
if err != nil {
return nil, err
}
}
return proofPairs, nil
}
func (p *RangeProofProver) proveInstance(
i int, ei byte, proofPairs []ProofPair) error {
lower := p.Q3
upper := new(big.Int).Add(p.Q3, p.Q3)
switch ei {
case 0:
proofPairs[i] = ProofPair{
J: 0,
W1: p.SecPairs[i].W1,
R1: p.SecPairs[i].R1,
W2: p.SecPairs[i].W2,
R2: p.SecPairs[i].R2,
}
case 1:
// Compute w1 + x.
w1x := new(big.Int)
w1x.Add(p.SecPairs[i].W1, p.X)
// Compute w2 + x.
w2x := new(big.Int)
w2x.Add(p.SecPairs[i].W2, p.X)
// Check if l <= w1 + x <= 2*l.
use1 := lower.Cmp(w1x) <= 0 && w1x.Cmp(upper) < 0
// Check if l <= w2 + x <= 2*l.
use2 := lower.Cmp(w2x) <= 0 && w2x.Cmp(upper) < 0
switch {
case use1 && use2:
return invalidProofPair
case use1:
r := new(big.Int).Mul(p.R, p.SecPairs[i].R1)
r.Mod(r, p.PSK.PublicKey.N)
proofPairs[i] = ProofPair{
J: 1,
W1: w1x,
R1: r,
}
case use2:
r := new(big.Int).Mul(p.R, p.SecPairs[i].R2)
r.Mod(r, p.PSK.PublicKey.N)
proofPairs[i] = ProofPair{
J: 2,
W2: w2x,
R2: r,
}
default:
return invalidProofPair
}
}
return nil
}
func (p *RangeProofVerifier) Verify(proofPairs []ProofPair) error {
for i := 0; i < p.Accuracy; i++ {
proofPair := &proofPairs[i]
err := p.verifyInstance(i, proofPair)
if err != nil {
return err
}
}
return nil
}
func (p *RangeProofVerifier) verifyInstance(
i int, proofPair *ProofPair) error {
lower := p.Q3
upper := new(big.Int).Add(p.Q3, p.Q3)
ctxtPair := p.CtxtPairs[i]
ei := p.Challenge.Bit(i)
switch {
case ei == 0 && proofPair.J == 0:
if proofPair.W1 == nil || proofPair.R1 == nil ||
proofPair.W2 == nil || proofPair.R2 == nil {
return invalidRangeProof
}
// Check if l <= w1 <= 2*l.
validW1Low := proofPair.W1.Sign() >= 0 &&
proofPair.W1.Cmp(lower) < 0
validW1High := lower.Cmp(proofPair.W1) <= 0 &&
proofPair.W1.Cmp(upper) < 0
// Check if l <= w2 <= 2*l.
validW2Low := proofPair.W1.Sign() >= 0 &&
proofPair.W2.Cmp(lower) < 0
validW2High := lower.Cmp(proofPair.W2) <= 0 &&
proofPair.W2.Cmp(upper) < 0
validW1 := validW1Low == !validW1High
validW2 := validW2Low == !validW2High
c1, err := paillier.EncryptWithNonce(
p.PPK, proofPair.R1, proofPair.W1.Bytes(),
)
if err != nil {
return err
}
c2, err := paillier.EncryptWithNonce(
p.PPK, proofPair.R2, proofPair.W2.Bytes(),
)
if err != nil {
return err
}
validC1 := c1.Cmp(ctxtPair.C1) == 0
validC2 := c2.Cmp(ctxtPair.C2) == 0
if !validW1 || !validW2 || !validC1 || !validC2 {
return invalidRangeProof
}
case ei == 1 && proofPair.J == 1:
if proofPair.W1 == nil || proofPair.R1 == nil ||
proofPair.W2 != nil || proofPair.R2 != nil {
return invalidRangeProof
}
// Check if l <= w1 <= 2*l.
validW1 := lower.Cmp(proofPair.W1) <= 0 &&
proofPair.W1.Cmp(upper) < 0
cc1 := new(big.Int).Mul(p.C, ctxtPair.C1)
cc1.Mod(cc1, p.PPK.NSquared)
cj, err := paillier.EncryptWithNonce(
p.PPK, proofPair.R1, proofPair.W1.Bytes(),
)
if err != nil {
return err
}
validCj := cc1.Cmp(cj) == 0
if !validW1 || !validCj {
return invalidRangeProof
}
case ei == 1 && proofPair.J == 2:
if proofPair.W1 != nil || proofPair.R1 != nil ||
proofPair.W2 == nil || proofPair.R2 == nil {
return invalidRangeProof
}
// Check if l <= w2 <= 2*l.
validW2 := lower.Cmp(proofPair.W2) <= 0 &&
proofPair.W2.Cmp(upper) < 0
cc2 := new(big.Int).Mul(p.C, ctxtPair.C2)
cc2.Mod(cc2, p.PPK.NSquared)
cj, err := paillier.EncryptWithNonce(
p.PPK, proofPair.R2, proofPair.W2.Bytes(),
)
if err != nil {
return err
}
validCj := cc2.Cmp(cj) == 0
if !validW2 || !validCj {
return invalidRangeProof
}
default:
return invalidRangeProof
}
return nil
}
func randBitSlice(n int) (BitSlice, error) {
nbytes := (n + 7) / 8
b := make([]byte, nbytes)
_, err := io.ReadFull(rand.Reader, b)
return BitSlice(b), err
}