-
Notifications
You must be signed in to change notification settings - Fork 6
/
zkp_paillier_blum_modulus.c
294 lines (223 loc) · 10.4 KB
/
zkp_paillier_blum_modulus.c
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
#include "zkp_paillier_blum_modulus.h"
zkp_paillier_blum_modulus_proof_t *zkp_paillier_blum_new ()
{
zkp_paillier_blum_modulus_proof_t *proof = malloc(sizeof(zkp_paillier_blum_modulus_proof_t));
proof->w = scalar_new();
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
proof->x[i] = scalar_new();
proof->z[i] = scalar_new();
}
memset(proof->a, 0x00, STATISTICAL_SECURITY);
memset(proof->b, 0x00, STATISTICAL_SECURITY);
return proof;
}
void zkp_paillier_blum_free (zkp_paillier_blum_modulus_proof_t *proof)
{
scalar_free(proof->w);
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
scalar_free(proof->x[i]);
scalar_free(proof->z[i]);
}
memset(proof->a, 0x00, STATISTICAL_SECURITY);
memset(proof->b, 0x00, STATISTICAL_SECURITY);
free(proof);
}
void zkp_paillier_blum_challenge (scalar_t y[STATISTICAL_SECURITY], zkp_paillier_blum_modulus_proof_t *proof, const scalar_t N_modulus, const zkp_aux_info_t *aux)
{
// Fiat-Shamir on (paillier_pub_N, w).
uint64_t fs_data_len = aux->info_len + 2*PAILLIER_MODULUS_BYTES;
uint8_t *fs_data = malloc(fs_data_len);
uint8_t *data_pos = fs_data;
memcpy(data_pos, aux->info, aux->info_len);
data_pos += aux->info_len;
scalar_to_bytes(&data_pos, PAILLIER_MODULUS_BYTES, N_modulus, 1);
scalar_to_bytes(&data_pos, PAILLIER_MODULUS_BYTES, proof->w, 1);
assert(fs_data + fs_data_len == data_pos);
fiat_shamir_scalars_in_range(y, STATISTICAL_SECURITY, N_modulus, fs_data, fs_data_len);
free(fs_data);
}
void zkp_paillier_blum_prove (zkp_paillier_blum_modulus_proof_t *proof, const paillier_private_key_t *private, const zkp_aux_info_t *aux)
{
assert(BN_num_bytes(private->N) == PAILLIER_MODULUS_BYTES);
BN_CTX *bn_ctx = BN_CTX_secure_new();
// Generate random w with (-1, 1) Jacobi signs wrt (p,q)
// Use CRT to set w as -a^2 mod q and b^2 mod q for uniform a,b
scalar_t crt_mod_q_factor = scalar_new();
scalar_t crt_mod_p_factor = scalar_new();
scalar_t w_p_part = scalar_new();
scalar_t w_q_part = scalar_new();
BN_mod_inverse(crt_mod_q_factor, private->p, private->q, bn_ctx);
BN_mod_inverse(crt_mod_p_factor, private->q, private->p, bn_ctx);
BN_mod_mul(crt_mod_q_factor, crt_mod_q_factor, private->p, private->N, bn_ctx);
BN_mod_mul(crt_mod_p_factor, crt_mod_p_factor, private->q, private->N, bn_ctx);
BN_rand_range(w_p_part, private->p);
BN_rand_range(w_q_part, private->q);
BN_mod_sqr(w_p_part, w_p_part, private->p, bn_ctx);
BN_mod_sqr(w_q_part, w_q_part, private->q, bn_ctx);
BN_mod_mul(w_p_part, w_p_part, crt_mod_p_factor, private->N, bn_ctx);
BN_mod_mul(w_q_part, w_q_part, crt_mod_q_factor, private->N, bn_ctx);
BN_mod_sub(proof->w, w_q_part, w_p_part, private->N, bn_ctx);
scalar_t y[STATISTICAL_SECURITY];
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i) y[i] = scalar_new();
zkp_paillier_blum_challenge(y, proof, private->N, aux);
// The following is needed to compute z[i]
scalar_t N_inverse_mod_phiN = scalar_new();
BN_mod_inverse(N_inverse_mod_phiN, private->N, private->phi_N, bn_ctx);
// We first compute each y[i] Legendre symbol pair (leg_q,leg_p) wrt (p,q), using Euler's Criterion (exp to (p-1)/2)
// Then change y to be QR mod N by y_qr = (-1)^a * w^b * y where a = (leg_q == 1) and b = (leg_p != leg_q) (by choice of w with symbols (-1,1))
// Then we can take y_qr's 4th root (wrt mod p and mod q seperately), by exponentation with ((prime+1)/4 mod (prime -1))
// Lastly with randomize the 4th root by randomly changing the signs mod p and mod q (before CRT to compute the root mod N).
scalar_t p_minus_1 = BN_dup(private->p);
scalar_t q_minus_1 = BN_dup(private->q);
BN_sub_word(p_minus_1, 1);
BN_sub_word(q_minus_1, 1);
scalar_t p_euler_exp = BN_dup(p_minus_1);
scalar_t q_euler_exp = BN_dup(q_minus_1);
BN_div_word(p_euler_exp, 2);
BN_div_word(q_euler_exp, 2);
scalar_t p_exp_4th_root = BN_dup(private->p);
scalar_t q_exp_4th_root = BN_dup(private->q);
BN_add_word(p_exp_4th_root, 1);
BN_div_word(p_exp_4th_root, 4);
BN_mod_sqr(p_exp_4th_root, p_exp_4th_root, p_minus_1, bn_ctx);
BN_add_word(q_exp_4th_root, 1);
BN_div_word(q_exp_4th_root, 4);
BN_mod_sqr(q_exp_4th_root, q_exp_4th_root, q_minus_1, bn_ctx);
scalar_t temp = scalar_new();
scalar_t y_qr = scalar_new();
scalar_t legendre_p = scalar_new();
scalar_t legendre_q = scalar_new();
scalar_t y_4th_root_mod_p = scalar_new();
scalar_t y_4th_root_mod_q = scalar_new();
// Sanity Check start...
BN_mod_exp(temp, proof->w, p_euler_exp, private->p, bn_ctx);
BN_mod_sub(temp, private->p, temp, private->p, bn_ctx);
assert(BN_is_one(temp));
BN_mod_exp(temp, proof->w, q_euler_exp, private->q, bn_ctx);
assert(BN_is_one(temp));
// ...end
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
BN_mod_exp(proof->z[i], y[i], N_inverse_mod_phiN, private->N, bn_ctx);
// Compute Ledengre symbols of y, using Euler's criterion
BN_mod_exp(legendre_p, y[i], p_euler_exp, private->p, bn_ctx);
BN_mod_exp(legendre_q, y[i], q_euler_exp, private->q, bn_ctx);
// Sanity checks start...
assert(BN_is_one(legendre_p) != (BN_cmp(legendre_p, p_minus_1) == 0));
assert(BN_is_one(legendre_q) != (BN_cmp(legendre_q, q_minus_1) == 0));
// ...end
// Derive a,b and compute fixed y_qr = (-1)^a * w^b * y
proof->a[i] = BN_cmp(legendre_q, q_minus_1) == 0;
proof->b[i] = (BN_is_one(legendre_p) && (BN_cmp(legendre_q, q_minus_1) == 0)) ||
(BN_is_one(legendre_q) && (BN_cmp(legendre_p, p_minus_1) == 0));
BN_copy(y_qr, y[i]);
if (proof->a[i]) BN_mod_sub(y_qr, private->N, y_qr, private->N, bn_ctx);
if (proof->b[i]) BN_mod_mul(y_qr, proof->w, y_qr, private->N, bn_ctx);
// Sanity Check start...
BN_mod_exp(temp, y_qr, p_euler_exp, private->p, bn_ctx);
assert(BN_is_one(temp));
BN_mod_exp(temp, y_qr, q_euler_exp, private->q, bn_ctx);
assert(BN_is_one(temp));
// ...end
// Compute x as random 4th root of fixed y_qr with CRT
BN_mod_exp(y_4th_root_mod_p, y_qr, p_exp_4th_root, private->p, bn_ctx);
BN_mod_exp(y_4th_root_mod_q, y_qr, q_exp_4th_root, private->q, bn_ctx);
// Randomly change mod p and mod q components signs, to get random 4th root
BN_rand(temp, 2, -1, 0); // two random bits
if (BN_is_bit_set(temp, 0)) BN_mod_sub(y_4th_root_mod_p, private->p, y_4th_root_mod_p, private->p, bn_ctx);
if (BN_is_bit_set(temp, 1)) BN_mod_sub(y_4th_root_mod_q, private->q, y_4th_root_mod_q, private->q, bn_ctx);
BN_mod_mul(y_4th_root_mod_p, y_4th_root_mod_p, crt_mod_p_factor, private->N, bn_ctx);
BN_mod_mul(y_4th_root_mod_q, y_4th_root_mod_q, crt_mod_q_factor, private->N, bn_ctx);
BN_mod_add(proof->x[i], y_4th_root_mod_p, y_4th_root_mod_q, private->N, bn_ctx);
// Sanity Checks start...
BN_mod_sqr(temp, proof->x[i], private->N, bn_ctx);
BN_mod_sqr(temp, temp, private->N, bn_ctx);
assert(BN_cmp(temp, y_qr) == 0);
// ...end
}
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i) scalar_free(y[i]);
scalar_free(N_inverse_mod_phiN);
scalar_free(y_4th_root_mod_p);
scalar_free(y_4th_root_mod_q);
scalar_free(q_exp_4th_root);
scalar_free(p_exp_4th_root);
scalar_free(p_minus_1);
scalar_free(q_minus_1);
scalar_free(crt_mod_q_factor);
scalar_free(crt_mod_p_factor);
scalar_free(temp);
scalar_free(w_p_part);
scalar_free(w_q_part);
BN_CTX_free(bn_ctx);
}
int zkp_paillier_blum_verify (zkp_paillier_blum_modulus_proof_t *proof, const paillier_public_key_t *public, const zkp_aux_info_t *aux)
{
BN_CTX *bn_ctx = BN_CTX_secure_new();
// Check composite odd number of required byte-length
int is_verified = BN_is_odd(public->N);
is_verified &= (uint64_t) BN_num_bytes(public->N) == PAILLIER_MODULUS_BYTES;
is_verified &= BN_is_prime_ex(public->N, 128, bn_ctx, NULL) == 0;
scalar_t y[STATISTICAL_SECURITY];
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i) y[i] = scalar_new();
zkp_paillier_blum_challenge(y, proof, public->N, aux);
scalar_t lhs_value = scalar_new();
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
BN_mod_exp(lhs_value, proof->z[i], public->N, public->N, bn_ctx);
is_verified &= scalar_equal(lhs_value, y[i]);
BN_mod_sqr(lhs_value, proof->x[i], public->N, bn_ctx);
BN_mod_sqr(lhs_value, lhs_value, public->N, bn_ctx);
if (proof->b[i]) BN_mod_mul(y[i], proof->w, y[i], public->N, bn_ctx);
if (proof->a[i]) BN_mod_sub(y[i], public->N, y[i], public->N, bn_ctx);
is_verified &= scalar_equal(lhs_value, y[i]);
}
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i) scalar_free(y[i]);
scalar_free(lhs_value);
BN_CTX_free(bn_ctx);
return is_verified;
}
void zkp_paillier_blum_proof_to_bytes (uint8_t **bytes, uint64_t *byte_len, const zkp_paillier_blum_modulus_proof_t *proof, int move_to_end)
{
uint64_t needed_byte_len = PAILLIER_MODULUS_BYTES*(1 + 2*STATISTICAL_SECURITY) + 2*STATISTICAL_SECURITY;
if ((!bytes) || (!*bytes) || (!proof) || (needed_byte_len > *byte_len))
{
*byte_len = needed_byte_len;
return ;
}
uint8_t *set_bytes = *bytes;
scalar_to_bytes(&set_bytes, PAILLIER_MODULUS_BYTES, proof->w, 1);
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
scalar_to_bytes(&set_bytes, PAILLIER_MODULUS_BYTES, proof->x[i], 1);
scalar_to_bytes(&set_bytes, PAILLIER_MODULUS_BYTES, proof->z[i], 1);
memcpy(set_bytes, &proof->a[i], 1); set_bytes += 1;
memcpy(set_bytes, &proof->b[i], 1); set_bytes += 1;
}
assert(set_bytes == *bytes + needed_byte_len);
*byte_len = needed_byte_len;
if (move_to_end) *bytes = set_bytes;
}
void zkp_paillier_blum_proof_from_bytes (zkp_paillier_blum_modulus_proof_t *proof, uint8_t **bytes, uint64_t *byte_len, int move_to_end)
{
uint64_t needed_byte_len;
zkp_paillier_blum_proof_to_bytes(NULL, &needed_byte_len, NULL, 0);
if ((!bytes) || (!*bytes) || (!proof) || (needed_byte_len > *byte_len))
{
*byte_len = needed_byte_len;
return ;
}
uint8_t *read_bytes = *bytes;
scalar_from_bytes(proof->w, &read_bytes, PAILLIER_MODULUS_BYTES, 1);
for (uint64_t i = 0; i < STATISTICAL_SECURITY; ++i)
{
scalar_from_bytes(proof->x[i], &read_bytes, PAILLIER_MODULUS_BYTES, 1);
scalar_from_bytes(proof->z[i], &read_bytes, PAILLIER_MODULUS_BYTES, 1);
memcpy(&proof->a[i], read_bytes, 1); read_bytes += 1;
memcpy(&proof->b[i], read_bytes, 1); read_bytes += 1;
}
assert(read_bytes == *bytes + needed_byte_len);
*byte_len = needed_byte_len;
if (move_to_end) *bytes = read_bytes;
}