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
use std::fmt;
use std::iter;
use std::error::Error;
use std::borrow::Borrow;
use ring::digest::{Context, SHA256};
use rayon::prelude::*;
use ::arithimpl::traits::*;
use ::{Paillier, BigInt, EncryptionKey, DecryptionKey};
use core::extract_nroot;
const STATISTICAL_ERROR_FACTOR: usize = 40;
#[derive(Debug)]
pub struct CorrectKeyProofError;
impl fmt::Display for CorrectKeyProofError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ProofError")
}
}
impl Error for CorrectKeyProofError {
fn description(&self) -> &str {
"Error while verifying"
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Challenge {
#[serde(with = "::serialize::vecbigint")]
pub sn: Vec<BigInt>,
#[serde(with = "::serialize::bigint")]
pub e: BigInt,
#[serde(with = "::serialize::vecbigint")]
pub z: Vec<BigInt>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct VerificationAid {
#[serde(with = "::serialize::bigint")]
s_digest: BigInt
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CorrectKeyProof {
#[serde(with = "::serialize::bigint")]
s_digest: BigInt
}
pub trait CorrectKey<EK, DK> {
fn challenge(ek: &EK) -> (Challenge, VerificationAid);
fn prove(dk: &DK, challenge: &Challenge) -> Result<CorrectKeyProof, CorrectKeyProofError>;
fn verify(proof: &CorrectKeyProof, aid: &VerificationAid) -> Result<(), CorrectKeyProofError>;
}
impl CorrectKey<EncryptionKey, DecryptionKey> for Paillier
{
fn challenge(ek: &EncryptionKey) -> (Challenge, VerificationAid) {
let s: Vec<_> = (0..STATISTICAL_ERROR_FACTOR).into_par_iter()
.map(|_| BigInt::sample_below(&ek.n))
.collect();
let sn: Vec<_> = s.par_iter()
.map(|si| BigInt::modpow(si, &ek.n, &ek.nn))
.collect();
let r: Vec<_> = (0..STATISTICAL_ERROR_FACTOR).into_par_iter()
.map(|_| BigInt::sample_below(&ek.n))
.collect();
let rn: Vec<_> = r.par_iter()
.map(|ri| BigInt::modpow(ri, &ek.n, &ek.nn))
.collect();
let e = compute_digest(
iter::once(&ek.n)
.chain(&sn)
.chain(&rn)
);
let z: Vec<_> = r.par_iter()
.zip(s.par_iter())
.map(|(ri, si)| (ri * BigInt::modpow(si, &e, &ek.nn)) % &ek.nn)
.collect();
let s_digest: BigInt = compute_digest(s.iter());
(Challenge { sn, e, z }, VerificationAid { s_digest })
}
fn prove(dk: &DecryptionKey, challenge: &Challenge) -> Result<CorrectKeyProof, CorrectKeyProofError>
{
let mut fail = false;
fail = challenge.sn.par_iter()
.any(|sni| BigInt::egcd(&dk.n, sni).0 != BigInt::one()) || fail;
fail = challenge.z.par_iter()
.any(|zi| BigInt::egcd(&dk.n, zi).0 != BigInt::one()) || fail;
let phimine = &dk.phi - (&challenge.e % &dk.phi);
let rn: Vec<_> = challenge.z.par_iter().zip(challenge.sn.par_iter())
.map(|(zi, sni)| {
let zn = BigInt::modpow(zi, &dk.n, &dk.nn);
let snphi = BigInt::modpow(sni, &phimine, &dk.nn);
(zn * snphi) % &dk.nn
})
.collect();
fail = rn.par_iter()
.any(|rni| BigInt::egcd(&dk.n, rni).0 != BigInt::one()) || fail;
let e = compute_digest(
iter::once(&dk.n)
.chain(&challenge.sn)
.chain(&rn)
);
fail = (challenge.e != e) || fail;
if fail { return Err(CorrectKeyProofError); }
let s_digest = compute_digest(
challenge.sn.iter()
.map(|sni| {
let si = extract_nroot(dk, sni);
si
}));
Ok(CorrectKeyProof { s_digest })
}
fn verify(proof: &CorrectKeyProof, va: &VerificationAid) -> Result<(), CorrectKeyProofError> {
if proof.s_digest == va.s_digest {
Ok(())
} else {
Err(CorrectKeyProofError)
}
}
}
fn compute_digest<IT>(values: IT) -> BigInt
where IT: Iterator, IT::Item: Borrow<BigInt>
{
let mut digest = Context::new(&SHA256);
for value in values {
let bytes: Vec<u8> = value.borrow().into();
digest.update(&bytes);
}
BigInt::from(digest.finish().as_ref())
}
#[cfg(test)]
mod tests {
use super::*;
use ::Keypair;
use traits::*;
fn test_keypair() -> Keypair {
let p = str::parse("148677972634832330983979593310074301486537017973460461278300587514468301043894574906886127642530475786889672304776052879927627556769456140664043088700743909632312483413393134504352834240399191134336344285483935856491230340093391784574980688823380828143810804684752914935441384845195613674104960646037368551517").unwrap();
let q = str::parse("158741574437007245654463598139927898730476924736461654463975966787719309357536545869203069369466212089132653564188443272208127277664424448947476335413293018778018615899291704693105620242763173357203898195318179150836424196645745308205164116144020613415407736216097185962171301808761138424668335445923774195463").unwrap();
Keypair {
p: p,
q: q,
}
}
#[test]
fn test_correct_zk_proof() {
let (ek, dk) = test_keypair().keys();
let (challenge, verification_aid) = Paillier::challenge(&ek);
let proof_results = Paillier::prove(&dk, &challenge);
assert!(proof_results.is_ok());
let result = Paillier::verify(&proof_results.unwrap(), &verification_aid);
assert!(result.is_ok());
}
#[test]
fn test_incorrect_zk_proof() {
let (ek, dk) = test_keypair().keys();
let (mut challenge, _verification_aid) = Paillier::challenge(&ek);
challenge.e += 1;
let proof_results = Paillier::prove(&dk, &challenge);
assert!(proof_results.is_err());
}
#[test]
fn test_incorrect_zk_proof_2() {
let (ek, dk) = test_keypair().keys();
let (challenge, mut verification_aid) = Paillier::challenge(&ek);
let proof_results = Paillier::prove(&dk, &challenge);
assert!(proof_results.is_ok());
verification_aid.s_digest += 1;
let result = Paillier::verify(&proof_results.unwrap(), &verification_aid);
assert!(result.is_err());
}
}