-
Notifications
You must be signed in to change notification settings - Fork 40
/
verlin_proof.rs
308 lines (277 loc) · 9.12 KB
/
verlin_proof.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
use std::iter;
use serde::{Deserialize, Serialize};
use curv::arithmetic::traits::*;
use curv::BigInt;
use paillier::traits::{Add, Mul};
use paillier::EncryptWithChosenRandomness;
use paillier::Paillier;
use paillier::{EncryptionKey, Randomness, RawCiphertext, RawPlaintext};
use super::errors::IncorrectProof;
/// A sigma protocol to allow a prover to demonstrate that a ciphertext c_x has been computed using
/// two other ciphertexts c_cprime, as well as a known value.
///
/// The proof is taken from https://eprint.iacr.org/2011/494.pdf 3.3.1
///
/// Witness: {x,x_prime, x_double_prime, r_x}
///
/// Statement: {c_x, c, c_prime}.
///
/// The relation is such that:
/// phi_x = c^x * c_prime^x_prime * Enc(x_double_prime, r_x)
///
/// The protocol:
///
/// 1. Prover picks random: a,a_prime,a_double_prime and r_a and computes: phi_a
/// 2. prover computes a challenge e using Fiat-Shamir
/// 3. Prover computes z = xe + a, z' = x'e + a', z_double_prime = x_double_prime*e + a_double_prime
/// and r_z = r_x^e*r_a
///
/// Verifier accepts if phi_z = phi_x^e * phi_a
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct VerlinProof {
pub phi_a: BigInt,
pub z: BigInt,
pub z_prime: BigInt,
pub z_double_prime: BigInt,
pub r_z: BigInt,
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct VerlinWitness {
pub x: BigInt,
pub x_prime: BigInt,
pub x_double_prime: BigInt,
pub r_x: BigInt,
}
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct VerlinStatement {
pub ek: EncryptionKey,
pub c: BigInt,
pub c_prime: BigInt,
pub phi_x: BigInt,
}
impl VerlinProof {
pub fn prove(witness: &VerlinWitness, statement: &VerlinStatement) -> Self {
let a = BigInt::sample_below(&statement.ek.n);
let a_prime = BigInt::sample_below(&statement.ek.n);
let a_double_prime = BigInt::sample_below(&statement.ek.n);
let mut r_a = BigInt::sample_below(&statement.ek.n);
while BigInt::gcd(&r_a, &statement.ek.n) != BigInt::one() {
r_a = BigInt::sample_below(&statement.ek.n);
}
let phi_a = gen_phi(
&statement.ek,
&statement.c,
&statement.c_prime,
&a,
&a_prime,
&a_double_prime,
&r_a,
);
let e = super::compute_digest(
iter::once(&statement.ek.n)
.chain(iter::once(&statement.c))
.chain(iter::once(&statement.c_prime))
.chain(iter::once(&statement.phi_x))
.chain(iter::once(&phi_a)),
);
let z = &witness.x * &e + &a;
let z_prime = &witness.x_prime * &e + &a_prime;
let z_double_prime = &witness.x_double_prime * &e + &a_double_prime;
let r_x_e = BigInt::mod_pow(&witness.r_x, &e, &statement.ek.nn);
let r_z = BigInt::mod_mul(&r_x_e, &r_a, &statement.ek.nn);
VerlinProof {
phi_a,
z,
z_prime,
z_double_prime,
r_z,
}
}
pub fn verify(&self, statement: &VerlinStatement) -> Result<(), IncorrectProof> {
let e = super::compute_digest(
iter::once(&statement.ek.n)
.chain(iter::once(&statement.c))
.chain(iter::once(&statement.c_prime))
.chain(iter::once(&statement.phi_x))
.chain(iter::once(&self.phi_a)),
);
let phi_x_e = Paillier::mul(
&statement.ek,
RawCiphertext::from(statement.phi_x.clone()),
RawPlaintext::from(e),
);
let phi_x_e_phi_a = Paillier::add(
&statement.ek,
phi_x_e,
RawCiphertext::from(self.phi_a.clone()),
);
let phi_z = gen_phi(
&statement.ek,
&statement.c,
&statement.c_prime,
&self.z,
&self.z_prime,
&self.z_double_prime,
&self.r_z,
);
match phi_z == phi_x_e_phi_a.0.into_owned() {
true => Ok(()),
false => Err(IncorrectProof),
}
}
}
// helper
fn gen_phi(
ek: &EncryptionKey,
c: &BigInt,
c_prime: &BigInt,
y: &BigInt,
y_prime: &BigInt,
y_double_prime: &BigInt,
r_y: &BigInt,
) -> BigInt {
let c_y = Paillier::mul(
ek,
RawCiphertext::from(c.clone()),
RawPlaintext::from(y.clone()),
);
let c_prime_y_prime = Paillier::mul(
ek,
RawCiphertext::from(c_prime.clone()),
RawPlaintext::from(y_prime.clone()),
);
let c_y_double_prime_r_y = Paillier::encrypt_with_chosen_randomness(
ek,
RawPlaintext::from(y_double_prime.clone()),
&Randomness(r_y.clone()),
);
let c_y_c_prime_y_prime = Paillier::add(ek, c_y, c_prime_y_prime);
let phi_y = Paillier::add(ek, c_y_c_prime_y_prime, c_y_double_prime_r_y);
phi_y.0.into_owned()
}
#[cfg(test)]
mod tests {
use curv::arithmetic::traits::*;
use curv::BigInt;
use paillier::traits::Encrypt;
use paillier::traits::KeyGeneration;
use paillier::Paillier;
use paillier::RawPlaintext;
use crate::zkproofs::verlin_proof::gen_phi;
use crate::zkproofs::verlin_proof::VerlinProof;
use crate::zkproofs::verlin_proof::VerlinStatement;
use crate::zkproofs::verlin_proof::VerlinWitness;
#[test]
fn test_verlin_proof() {
let (ek, _) = Paillier::keypair().keys();
let x = BigInt::sample_below(&ek.n);
let x_prime = BigInt::sample_below(&ek.n);
let x_double_prime = BigInt::sample_below(&ek.n);
let mut r_x = BigInt::sample_below(&ek.n);
while BigInt::gcd(&r_x, &ek.n) != BigInt::one() {
r_x = BigInt::sample_below(&ek.n);
}
let c = Paillier::encrypt(&ek, RawPlaintext::from(x.clone()));
let c_bn = c.0.clone().into_owned();
let c_prime = Paillier::encrypt(&ek, RawPlaintext::from(x_prime.clone()));
let c_prime_bn = c_prime.0.clone().into_owned();
let phi_x = gen_phi(&ek, &c_bn, &c_prime_bn, &x, &x_prime, &x_double_prime, &r_x);
let witness = VerlinWitness {
x,
x_prime,
x_double_prime,
r_x,
};
let statement = VerlinStatement {
ek,
c: c_bn,
c_prime: c_prime_bn,
phi_x,
};
let proof = VerlinProof::prove(&witness, &statement);
let verify = proof.verify(&statement);
assert!(verify.is_ok());
}
#[test]
#[should_panic]
fn test_bad_verlin_proof() {
let (ek, _) = Paillier::keypair().keys();
let x = BigInt::sample_below(&ek.n);
let x_prime = BigInt::sample_below(&ek.n);
let x_double_prime = BigInt::sample_below(&ek.n);
let mut r_x = BigInt::sample_below(&ek.n);
while BigInt::gcd(&r_x, &ek.n) != BigInt::one() {
r_x = BigInt::sample_below(&ek.n);
}
let c = Paillier::encrypt(&ek, RawPlaintext::from(x.clone()));
let c_bn = c.0.clone().into_owned();
let c_prime = Paillier::encrypt(&ek, RawPlaintext::from(x_prime.clone()));
let c_prime_bn = c_prime.0.clone().into_owned();
// we inject x_bad = 2x
let phi_x = gen_phi(
&ek,
&c_bn,
&c_prime_bn,
&(&x * BigInt::from(2)),
&x_prime,
&x_double_prime,
&r_x,
);
let witness = VerlinWitness {
x,
x_prime,
x_double_prime,
r_x,
};
let statement = VerlinStatement {
ek,
c: c_bn,
c_prime: c_prime_bn,
phi_x,
};
let proof = VerlinProof::prove(&witness, &statement);
let verify = proof.verify(&statement);
assert!(verify.is_ok());
}
#[test]
#[should_panic]
fn test_bad_verlin_proof_2() {
let (ek, _) = Paillier::keypair().keys();
let x = BigInt::sample_below(&ek.n);
let x_prime = BigInt::sample_below(&ek.n);
let x_double_prime = BigInt::sample_below(&ek.n);
let mut r_x = BigInt::sample_below(&ek.n);
while BigInt::gcd(&r_x, &ek.n) != BigInt::one() {
r_x = BigInt::sample_below(&ek.n);
}
let c = Paillier::encrypt(&ek, RawPlaintext::from(x.clone()));
let c_bn = c.0.clone().into_owned();
let c_prime = Paillier::encrypt(&ek, RawPlaintext::from(x_prime.clone()));
let c_prime_bn = c_prime.0.clone().into_owned();
// we inject r_x_bad = r_x + 1
let phi_x = gen_phi(
&ek,
&c_bn,
&c_prime_bn,
&(&x * BigInt::from(2)),
&x_prime,
&x_double_prime,
&(&r_x + BigInt::one()),
);
let witness = VerlinWitness {
x,
x_prime,
x_double_prime,
r_x,
};
let statement = VerlinStatement {
ek,
c: c_bn,
c_prime: c_prime_bn,
phi_x,
};
let proof = VerlinProof::prove(&witness, &statement);
let verify = proof.verify(&statement);
assert!(verify.is_ok());
}
}