-
Notifications
You must be signed in to change notification settings - Fork 0
/
nizkp.c
404 lines (335 loc) · 8.98 KB
/
nizkp.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
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
/*
Note that this code uses a modified Paillierlib that includes the random value 'r' with each ciphertext.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <openssl/sha.h>
#include <gmp.h>
#include "paillier.c"
void get_rand_file( void* buf, int len, char* file )
{
FILE* fp;
void* p;
fp = fopen(file, "r");
p = buf;
while( len )
{
size_t s;
s = fread(p, 1, len, fp);
p += s;
len -= s;
}
fclose(fp);
}
void get_rand_devurandom( void* buf, int len )
{
get_rand_file(buf, len, "/dev/urandom");
}
void print_hash(char * d)
{
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
printf("%02x", d[i]);
printf("\n");
}
int main(int argc, char *argv[])
{
// Security parameter (number of bits of the modulus)
mpz_t n;
mpz_init(n);
mpz_set_ui(n, 256);
// Generate keys
paillier_pubkey_t* pubKey;
paillier_prvkey_t* secKey;
paillier_keygen(256, &pubKey, &secKey, paillier_get_rand_devurandom);
// Plaintexts initialization
paillier_plaintext_t* m;
m = paillier_plaintext_from_ui(100);
gmp_printf("Plaintext created: %Zd\n", m);
// Encrypt the messages
paillier_ciphertext_t* c;
c = paillier_enc(NULL, pubKey, m, paillier_get_rand_devurandom);
gmp_printf("Ciphertext created: %Zd\n", c);
printf("\n");
// Now verify that ctxt1 is a valid message
// Following https://paillier.daylightingsociety.org/Paillier_Zero_Knowledge_Proof.pdf
// Declare variables
mpz_t u_1;
mpz_t u_2;
mpz_t u_3;
mpz_t m1;
mpz_t m2;
mpz_t m3;
mpz_t g_m1;
mpz_t g_m2;
mpz_t g_m3;
// Init variables
mpz_init(u_1);
mpz_init(u_2);
mpz_init(u_3);
mpz_init(g_m1);
mpz_init(g_m2);
mpz_init(g_m3);
mpz_init(m1);
mpz_init(m2);
mpz_init(m3);
// Assign values of valid votes to m_k's
mpz_set_ui(m1, 1);
mpz_set_ui(m2, 100);
mpz_set_ui(m3, 10000);
// Assign values for g_k
mpz_powm(g_m1, pubKey->n_plusone, m1, pubKey->n_squared);
mpz_powm(g_m2, pubKey->n_plusone, m2, pubKey->n_squared);
mpz_powm(g_m3, pubKey->n_plusone, m3, pubKey->n_squared);
// Calculate u_k's
mpz_invert(u_1, g_m1, pubKey->n_squared);
mpz_mul(u_1, c->c, u_1);
mpz_mod(u_1, u_1, pubKey->n_squared);
mpz_invert(u_2, g_m2, pubKey->n_squared);
mpz_mul(u_2, c->c, u_2);
mpz_mod(u_2, u_2, pubKey->n_squared);
mpz_invert(u_3, g_m3, pubKey->n_squared);
mpz_mul(u_3, c->c, u_3);
mpz_mod(u_3, u_3, pubKey->n_squared);
/*
PASSED
// Test that the following holds: u_2 = r^n mod n^2
// divide c by u_2 and I should get g^m:
//mpz_div(u_3, c->c, u_2);
//mpz_mod(u_3, u_3, pubKey->n_squared);
mpz_invert(u_3, u_2, pubKey->n_squared);
mpz_mul(u_3, c->c, u_3);
mpz_mod(u_3, u_3, pubKey->n_squared);
gmp_printf("c divided by u2 should be g^m: %Zd\n", u_3);
gmp_printf("g^m: %Zd\n", g_m2);
// Try g^m * u_2 and see if we get c
mpz_mul(u_3, g_m2, u_2);
mpz_mod(u_3, u_3, pubKey->n_squared);
gmp_printf("c: %Zd\n", c->c);
gmp_printf("gm * u2: %Zd\n", u_3);
*/
// Select 2 random e_k, z_k, and a random w_k
// TODO: use better random number generation
mpz_t e_1;
mpz_t e_3;
mpz_t z_1;
mpz_t z_3;
mpz_t w;
mpz_t res;
mpz_init(e_1);
mpz_init(e_3);
mpz_init(z_1);
mpz_init(z_3);
mpz_init(w);
mpz_init(res);
// Initialize random state for rng
void* buf;
mpz_t s;
buf = malloc(16);
get_rand_devurandom(buf, 16);
mpz_init(s);
mpz_import(s, 16, 1, 1, 0, 0, buf);
gmp_randstate_t rand_state;
gmp_randinit_mt(rand_state);
srand(time(0));
gmp_randseed(rand_state, s);
mpz_clear(s);
free(buf);
// Generate e_1 and e_3
// p and q are 256/2 bits each, so our b is 1 less bit than that to ensure 2^b < p,q
mpz_urandomb(e_1, rand_state, 256 / 2 - 1);
mpz_urandomb(e_3, rand_state, 256 / 2 - 1);
// Generate z_1 and z_3
// since n = pq, every number smaller than n that is not p or q is coprime to n
mpz_urandomb(z_1, rand_state, 255);
mpz_gcd(res, z_1, pubKey->n);
while (mpz_cmp_ui(res, 1) != 0)
{
mpz_urandomb(z_1, rand_state, 255);
mpz_gcd(res, z_1, pubKey->n);
}
mpz_set_ui(res, 2);
mpz_urandomb(z_3, rand_state, 255);
mpz_gcd(res, z_3, pubKey->n);
while (mpz_cmp_ui(res, 1) != 0)
{
mpz_urandomb(z_3, rand_state, 255);
mpz_gcd(res, z_3, pubKey->n);
}
// Generate omega
mpz_set_ui(res, 2);
mpz_urandomb(w, rand_state, 255);
mpz_gcd(res, w, pubKey->n);
while (mpz_cmp_ui(res, 1) != 0)
{
mpz_urandomb(w, rand_state, 255);
mpz_gcd(res, w, pubKey->n);
}
// Calculate a_k's
mpz_t a_1;
mpz_t a_2;
mpz_t a_3;
mpz_t z_n;
mpz_t u_e;
mpz_init(a_1);
mpz_init(a_2);
mpz_init(a_3);
mpz_init(z_n);
mpz_init(u_e);
// Calculate a_1
mpz_powm(z_n, z_1, pubKey->n, pubKey->n_squared);
mpz_powm(u_e, u_1, e_1, pubKey->n_squared);
mpz_invert(a_1, u_e, pubKey->n_squared);
mpz_mul(a_1, z_n, a_1);
mpz_mod(a_1, a_1, pubKey->n_squared);
// Calculate a_3
mpz_powm(z_n, z_3, pubKey->n, pubKey->n_squared);
mpz_powm(u_e, u_3, e_3, pubKey->n_squared);
mpz_invert(a_3, u_e, pubKey->n_squared);
mpz_mul(a_3, z_n, a_3);
mpz_mod(a_3, a_3, pubKey->n_squared);
// Calculate a_2 (for case m_i = m)
mpz_powm(a_2, w, pubKey->n, pubKey->n_squared);
// Concatenate a_k's and hash, making challenge string e_c
mpz_t e_c;
mpz_init(e_c);
char * a_1s = mpz_get_str(NULL, 10, a_1);
char * a_2s = mpz_get_str(NULL, 10, a_2);
char * a_3s = mpz_get_str(NULL, 10, a_3);
char * str = malloc(sizeof(a_1s)/sizeof(a_1s[0]) + sizeof(a_2s)/sizeof(a_2s[0]) + sizeof(a_3s)/sizeof(a_3s[0]) + 1);
snprintf(str, sizeof(a_1s)/sizeof(a_1s[0]) + sizeof(a_2s)/sizeof(a_2s[0]) + sizeof(a_3s)/sizeof(a_3s[0]) + 1, "%s%s%s", a_1s, a_2s, a_3s);
char * d = SHA256(str, strlen(str), 0);
free(str);
free(a_1s);
free(a_2s);
free(a_3s);
char * encrypted;
encrypted = malloc(65);
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(encrypted + (i*2), "%02x", d[i]);
}
encrypted[64] = 0;
mpz_set_str(e_c, encrypted, 16);
free(encrypted);
// Calculate e_2 (for m_i == m)
mpz_t e_2;
mpz_t two_raised_b;
mpz_init(e_2);
mpz_init(two_raised_b);
mpz_set_ui(two_raised_b, 2);
mpz_pow_ui(two_raised_b, two_raised_b, 256 / 2 - 1);
mpz_mod(e_c, e_c, two_raised_b);
mpz_sub(e_2, e_c, e_1);
mpz_sub(e_2, e_2, e_3);
mpz_mod(e_2, e_2, two_raised_b);
// Calculate z_2 (for m_i == m)
mpz_t z_2;
mpz_init(z_2);
mpz_powm(z_2, c->r, e_2, pubKey->n);
mpz_mul(z_2, w, z_2);
mpz_mod(z_2, z_2, pubKey->n);
// Voter's commit
printf("Voter (prover) commits the following values to the verifier\n");
gmp_printf("z_1: %Zd\n", z_1);
gmp_printf("z_2: %Zd\n", z_2);
gmp_printf("z_3: %Zd\n", z_3);
gmp_printf("a_1: %Zd\n", a_1);
gmp_printf("a_2: %Zd\n", a_2);
gmp_printf("a_3: %Zd\n", a_3);
gmp_printf("e_1: %Zd\n", e_1);
gmp_printf("e_2: %Zd\n", e_2);
gmp_printf("e_3: %Zd\n", e_3);
gmp_printf("e_c: %Zd\n", e_c);
printf("\n");
// Now we verify
// Sum up all e_k and confirm it equals e_c
mpz_t sum;
mpz_init(sum);
mpz_add(sum, e_1, e_2);
mpz_add(sum, sum, e_3);
mpz_mod(sum, sum, two_raised_b);
mpz_mod(e_c, e_c, two_raised_b);
if (mpz_cmp(e_c, sum) == 0)
{
printf("Verified sum of e_k equals challenge string!\n");
gmp_printf("sum: %Zd\n", sum);
gmp_printf("e_c: %Zd\n", e_c);
}
else
{
printf("Prover failed the challenge. CHEATER!\n");
gmp_printf("sum: %Zd\n", sum);
gmp_printf("e_c: %Zd\n", e_c);
}
printf("\n");
// Verify z_k^n = a_k * u_k^e_k mod n^2
// use res for right side, z_n for left side
mpz_powm(z_n, z_1, pubKey->n, pubKey->n_squared);
mpz_powm(res, u_1, e_1, pubKey->n_squared);
mpz_mul(res, a_1, res);
mpz_mod(res, res, pubKey->n_squared);
int verified = 1;
if (mpz_cmp(z_n, res) == 0)
{
printf("Verified z_1 = a_1 * u_1!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
}
else
{
printf("Prover failed the challenge. CHEATER!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
verified = 0;
}
printf("\n");
mpz_powm(z_n, z_2, pubKey->n, pubKey->n_squared);
mpz_powm(res, u_2, e_2, pubKey->n_squared);
mpz_mul(res, a_2, res);
mpz_mod(res, res, pubKey->n_squared);
if (mpz_cmp(z_n, res) == 0)
{
printf("Verified z_2 = a_2 * u_2!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
}
else
{
printf("Prover failed the challenge. CHEATER!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
verified = 0;
}
printf("\n");
mpz_powm(z_n, z_3, pubKey->n, pubKey->n_squared);
mpz_powm(res, u_3, e_3, pubKey->n_squared);
mpz_mul(res, a_3, res);
mpz_mod(res, res, pubKey->n_squared);
if (mpz_cmp(z_n, res) == 0)
{
printf("Verified z_3 = a_3 * u_3!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
}
else
{
printf("Prover failed the challenge. CHEATER!\n");
gmp_printf("z_n: %Zd\n", z_n);
gmp_printf("a_k * u_k: %Zd\n", res);
verified = 0;
}
printf("\n");
if (verified == 1)
printf("This voter voted responsibly. Accept block\n");
else
printf("This voter cheated! Reject block\n");
// Create an invalid ctxt2 and prove it's invalid
// Cleaning up
paillier_freepubkey(pubKey);
paillier_freeprvkey(secKey);
paillier_freeplaintext(m);
paillier_freeciphertext(c);
return 0;
}