forked from SarangNoether/qesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_zk.rs
363 lines (288 loc) · 12.3 KB
/
no_zk.rs
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
#![allow(non_snake_case)]
use crate::math_utils::inner_product;
use crate::transcript::TranscriptProtocol;
use curve25519_dalek::{
ristretto::{CompressedRistretto, RistrettoPoint},
scalar::Scalar,
traits::{IsIdentity, VartimeMultiscalarMul},
};
use merlin::Transcript;
use std::iter;
/// NoZK is an optimisation over the bulletproofs IPA.
#[derive(Clone)]
pub struct NoZK {
// From the literature this would be u_{-1}
pub(crate) L_vec: Vec<CompressedRistretto>,
// From the literature this would be u_{+1}
pub(crate) R_vec: Vec<CompressedRistretto>,
// From the literature, this would be w'
pub(crate) a: Scalar,
// From the literature, this would be w''
pub(crate) b: Scalar,
}
// a and b are the witness prime and witness prime prime respectively
// G_Vec, H_Vec and Q is g prime, g prime prime and Q of the crs respectively
// This implementation will intentionally try to mirror the dalek-cryptography implementation in design choices and variable naming
// making it easier to draw comparisons between the two and provide benchmarks
pub fn create(
transcript: &mut Transcript,
mut G_Vec: Vec<RistrettoPoint>,
mut H_Vec: Vec<RistrettoPoint>,
Q: &RistrettoPoint,
mut a_vec: Vec<Scalar>,
mut b_vec: Vec<Scalar>,
) -> NoZK {
let mut a = &mut a_vec[..];
let mut b = &mut b_vec[..];
let mut G = &mut G_Vec[..];
let mut H = &mut H_Vec[..];
let mut n = G.len();
// All of the input vectors must have the same length.
assert_eq!(G.len(), n);
assert_eq!(H.len(), n);
assert_eq!(a.len(), n);
assert_eq!(b.len(), n);
// All of the input vectors must have a length that is a power of two.
assert!(n.is_power_of_two());
transcript.append_u64(b"n", n as u64);
let lg_n = n.next_power_of_two().trailing_zeros() as usize;
let mut L_vec: Vec<CompressedRistretto> = Vec::with_capacity(lg_n);
let mut R_vec: Vec<CompressedRistretto> = Vec::with_capacity(lg_n);
let alpha = transcript.challenge_scalar(b"alpha");
let Q = alpha.invert() * Q;
while n != 1 {
n = n / 2;
let (a_L, a_R) = a.split_at_mut(n);
let (b_L, b_R) = b.split_at_mut(n);
let (G_L, G_R) = G.split_at_mut(n);
let (H_L, H_R) = H.split_at_mut(n);
let c_R = inner_product(a_R, b_L);
let c_L = inner_product(a_L, b_R);
let L = RistrettoPoint::vartime_multiscalar_mul(
a_L.iter().chain(b_R.iter()).chain(iter::once(&c_L)),
G_R.iter().chain(H_L.iter()).chain(iter::once(&Q)),
)
.compress();
let R = RistrettoPoint::vartime_multiscalar_mul(
a_R.iter().chain(b_L.iter()).chain(iter::once(&c_R)),
G_L.iter().chain(H_R.iter().chain(iter::once(&Q))),
)
.compress();
L_vec.push(L);
R_vec.push(R);
transcript.append_message(b"L", L.as_bytes());
transcript.append_message(b"R", R.as_bytes());
let x_i: Scalar = transcript.challenge_scalar(b"x_i");
for i in 0..n {
a_L[i] = a_L[i] * x_i + a_R[i];
b_L[i] = b_L[i] + x_i * b_R[i];
G_L[i] = &G_L[i] + x_i * &G_R[i];
H_L[i] = x_i * &H_L[i] + &H_R[i];
}
a = a_L;
b = b_L;
G = G_L;
H = H_L;
}
NoZK {
L_vec: L_vec,
R_vec: R_vec,
a: a[0],
b: b[0],
}
}
impl NoZK {
pub fn verify(
&self,
transcript: &mut Transcript,
G_Vec: &[RistrettoPoint],
H_Vec: &[RistrettoPoint],
Q: &RistrettoPoint,
mut n: usize,
mut P: RistrettoPoint,
t: Scalar,
) -> bool {
let mut G = G_Vec.to_owned();
let mut H = H_Vec.to_owned();
let Ls: Vec<RistrettoPoint> = self.L_vec.iter().map(|L| L.decompress().unwrap()).collect();
let Rs: Vec<RistrettoPoint> = self.R_vec.iter().map(|R| R.decompress().unwrap()).collect();
assert_eq!(n, 1 << Ls.len());
transcript.append_u64(b"n", n as u64);
let alpha = transcript.challenge_scalar(b"alpha");
let Q = alpha.invert() * Q;
P = P - (alpha - Scalar::one()) * t * Q;
let challenges = generate_challenges(self, transcript);
for ((L, R), challenge) in Ls.iter().zip(Rs.iter()).zip(challenges.iter()) {
let challenge_sq = challenge * challenge;
P = challenge_sq * L + challenge * P + R;
}
for challenge in challenges.iter() {
n = n / 2;
let (G_L, G_R) = G.split_at_mut(n);
let (H_L, H_R) = H.split_at_mut(n);
G = vector_vector_add(G_L, &mut vector_scalar_mul(G_R, challenge));
H = vector_vector_add(&mut vector_scalar_mul(H_L, challenge), H_R);
}
let exp_P = G[0] * self.a + H[0] * self.b + (self.a * self.b) * Q;
exp_P == P
}
pub fn verify_multiexp(
&self,
transcript: &mut Transcript,
G_Vec: &[RistrettoPoint],
H_Vec: &[RistrettoPoint],
Q: &RistrettoPoint,
n: usize,
P: RistrettoPoint,
t: Scalar,
) -> bool {
// decode L,R
let Ls: Vec<RistrettoPoint> = self.L_vec.iter().map(|L| L.decompress().unwrap()).collect();
let Rs: Vec<RistrettoPoint> = self.R_vec.iter().map(|R| R.decompress().unwrap()).collect();
assert_eq!(n, 1 << Ls.len());
let mut n = 1 << Ls.len();
// challenge data
transcript.append_u64(b"n", n as u64);
let alpha = transcript.challenge_scalar(b"alpha");
let challenges = generate_challenges(self, transcript);
let logn = challenges.len();
// {g_i},{h_i}
let mut g_i: Vec<Scalar> = Vec::with_capacity(n);
let mut h_i: Vec<Scalar> = Vec::with_capacity(n);
for x in 0..n {
let mut i: usize = 1;
let mut j: usize = 0;
let mut g = self.a;
let mut h = self.b;
while i < n {
if i & x != 0 {
g *= challenges[logn-j-1];
}
else {
h *= challenges[logn-j-1];
}
i <<= 1;
j += 1;
}
g_i.push(g);
h_i.push(h);
}
// {l_j},{r_j}
let mut l_j: Vec<Scalar> = Vec::with_capacity(n);
let mut r_j: Vec<Scalar> = Vec::with_capacity(n);
let mut p = Scalar::one();
for i in 0..logn {
let mut l = -challenges[i]*challenges[i];
let mut r = -Scalar::one();
for j in (i+1)..logn {
l *= challenges[j];
r *= challenges[j];
}
l_j.push(l);
r_j.push(r);
p *= challenges[i];
}
// return value goes here
let q = alpha.invert()*((alpha - Scalar::one())*t*p + self.a*self.b);
let R = RistrettoPoint::vartime_multiscalar_mul(
g_i.iter().chain(h_i.iter()).chain(l_j.iter()).chain(r_j.iter()).chain(iter::once(&q)).chain(iter::once(&-p)),
G_Vec.iter().chain( H_Vec.iter()).chain( Ls.iter()).chain( Rs.iter()).chain(iter::once( Q)).chain(iter::once(&P))
);
R.is_identity()
}
}
fn generate_challenges(proof: &NoZK, transcript: &mut Transcript) -> Vec<Scalar> {
let mut challenges: Vec<Scalar> = Vec::new();
for (L, R) in proof.L_vec.iter().zip(proof.R_vec.iter()) {
transcript.append_message(b"L", L.as_bytes());
transcript.append_message(b"R", R.as_bytes());
let x_i = transcript.challenge_scalar(b"x_i");
challenges.push(x_i);
}
challenges
}
fn vector_scalar_mul(vec_p: &mut [RistrettoPoint], challenge: &Scalar) -> Vec<RistrettoPoint> {
vec_p.iter().map(|p| p * challenge).collect()
}
fn vector_vector_add(a: &mut [RistrettoPoint], b: &mut [RistrettoPoint]) -> Vec<RistrettoPoint> {
a.iter().zip(b.iter()).map(|(a, b)| a + b).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::math_utils::inner_product;
use sha3::Sha3_512;
use std::iter;
#[test]
fn test_create_nozk_proof() {
let n = 4;
let mut rng = rand::thread_rng();
let a: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let b: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let t = inner_product(&a, &b);
let G: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let H: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let Q = RistrettoPoint::hash_from_bytes::<Sha3_512>(b"test point");
let mut prover_transcript = Transcript::new(b"ip_no_zk");
let P = RistrettoPoint::vartime_multiscalar_mul(
a.iter().chain(b.iter()).chain(iter::once(&t)),
G.iter().chain(H.iter()).chain(iter::once(&Q)),
);
// We add the compressed point to the transcript, because we need some non-trivial input to generate alpha
// If this is not done, then the prover always will be able to predict what the first challenge will be
prover_transcript.append_message(b"P", P.compress().as_bytes());
let proof = create(&mut prover_transcript, G.clone(), H.clone(), &Q, a, b);
let mut verifier_transcript = Transcript::new(b"ip_no_zk");
verifier_transcript.append_message(b"P", P.compress().as_bytes());
assert!(proof.verify(&mut verifier_transcript, &G, &H, &Q, n, P, t));
}
extern crate test;
use super::*;
use test::Bencher;
#[bench]
fn bench_verify_multiexp(bnch: &mut Bencher) {
let n = 256;
let mut rng = rand::thread_rng();
let a: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let b: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let t = inner_product(&a, &b);
let G: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let H: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let Q = RistrettoPoint::hash_from_bytes::<Sha3_512>(b"test point");
let mut prover_transcript = Transcript::new(b"ip_no_zk");
let P = RistrettoPoint::vartime_multiscalar_mul(
a.iter().chain(b.iter()).chain(iter::once(&t)),
G.iter().chain(H.iter()).chain(iter::once(&Q)),
);
// We add the compressed point to the transcript, because we need some non-trivial input to generate alpha
// If this is not done, then the prover always will be able to predict what the first challenge will be
prover_transcript.append_message(b"P", P.compress().as_bytes());
let proof = create(&mut prover_transcript, G.clone(), H.clone(), &Q, a, b);
let mut verifier_transcript = Transcript::new(b"ip_no_zk");
verifier_transcript.append_message(b"P", P.compress().as_bytes());
bnch.iter(|| proof.verify_multiexp(&mut verifier_transcript, &G, &H, &Q, n, P, t));
}
#[bench]
fn bench_verify(bnch: &mut Bencher) {
let n = 256;
let mut rng = rand::thread_rng();
let a: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let b: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let t = inner_product(&a, &b);
let G: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let H: Vec<RistrettoPoint> = (0..n).map(|_| RistrettoPoint::random(&mut rng)).collect();
let Q = RistrettoPoint::hash_from_bytes::<Sha3_512>(b"test point");
let mut prover_transcript = Transcript::new(b"ip_no_zk");
let P = RistrettoPoint::vartime_multiscalar_mul(
a.iter().chain(b.iter()).chain(iter::once(&t)),
G.iter().chain(H.iter()).chain(iter::once(&Q)),
);
// We add the compressed point to the transcript, because we need some non-trivial input to generate alpha
// If this is not done, then the prover always will be able to predict what the first challenge will be
prover_transcript.append_message(b"P", P.compress().as_bytes());
let proof = create(&mut prover_transcript, G.clone(), H.clone(), &Q, a, b);
let mut verifier_transcript = Transcript::new(b"ip_no_zk");
verifier_transcript.append_message(b"P", P.compress().as_bytes());
bnch.iter(|| proof.verify(&mut verifier_transcript, &G, &H, &Q, n, P, t));
}
}