-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
1856 lines (1448 loc) · 48.8 KB
/
main.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <stdbool.h>
#include <relic.h>
#include <openssl/sha.h>
#include "accumulator.h"
#define maxUsers 1020000
#define batchMax 10000
#define maxEpochs 1000
#define MEMBERSHIP true
#define NONMEMBERSHIP false
//Accumulator initialization
void init(t_state * accumulator) {
bn_t n2;
g1_null(accumulator->P);
g1_null(accumulator->X);
g1_null(accumulator->Z);
g1_null(accumulator->J);
g1_null(accumulator->K);
g1_null(accumulator->V);
g2_null(accumulator->Pt);
g2_null(accumulator->Qt);
gt_null(accumulator->ePPt);
gt_null(accumulator->eZPt);
gt_null(accumulator->eZQt);
gt_null(accumulator->eKPt);
gt_null(accumulator->eVPt);
bn_null(accumulator->n);
bn_null(n2);
bn_null(accumulator->a);
TRY {
g1_new(accumulator->P);
g1_new(accumulator->X);
g1_new(accumulator->Z);
g1_new(accumulator->J);
g1_new(accumulator->K);
g1_new(accumulator->V);
g2_new(accumulator->Pt);
g2_new(accumulator->Qt);
gt_new(accumulator->ePPt);
gt_new(accumulator->eZPt);
gt_new(accumulator->eZQt);
gt_new(accumulator->eKPt);
gt_new(accumulator->eVPt);
bn_new(accumulator->n);
bn_new(n2);
bn_new(accumulator->a);
//|G1| = |G2| ?
g1_get_ord(accumulator->n);
g2_get_ord(n2);
assert(bn_cmp(accumulator->n, n2) == RLC_EQ);
//Secret alpha
bn_rand_mod(accumulator->a, accumulator->n);
//Generators initialization
g1_get_gen(accumulator->P);
g2_get_gen(accumulator->Pt);
g2_mul(accumulator->Qt, accumulator->Pt, accumulator->a);
//Generators for the ZK protocol
g1_rand(accumulator->X);
g1_rand(accumulator->Z);
g1_rand(accumulator->J);
g1_rand(accumulator->K);
//accumulator initialization V0=P
g1_get_gen(accumulator->V);
//Cached pairings, for fast zkproofs creation and verification
pc_map(accumulator->ePPt, accumulator->P, accumulator->Pt);
pc_map(accumulator->eZPt, accumulator->Z, accumulator->Pt);
pc_map(accumulator->eZQt, accumulator->Z, accumulator->Qt);
pc_map(accumulator->eKPt, accumulator->K, accumulator->Pt);
pc_map(accumulator->eVPt, accumulator->V, accumulator->Pt);
//No accumulated elements
accumulator->totAccEl = 0;
//fV(a) = 1
bn_null(accumulator->fVa);
bn_new(accumulator->fVa);
bn_set_dig(accumulator->fVa, 1);
//The set of all accumulated elements
accumulator->Y = (bn_t *)malloc(sizeof(bn_t)*(maxUsers));
for (int i = 0; i<maxUsers; i++) {
bn_null(accumulator->Y[i]);
bn_new(accumulator->Y[i]);
bn_set_dig(accumulator->Y[i], 0);
}
//Initializing epoch array
accumulator->epochs = (t_epoch *)malloc(sizeof(t_epoch)*maxEpochs);
accumulator->currentEpoch = 0;
g1_copy((accumulator->epochs[0]).V, accumulator->V);
(accumulator->epochs[0]).Y_add = NULL;
(accumulator->epochs[0]).Y_del = NULL;
(accumulator->epochs[0]).addSize = 0;
(accumulator->epochs[0]).delSize = 0;
(accumulator->epochs[0]).OmegaAdd = NULL;
(accumulator->epochs[0]).OmegaDel = NULL;
}
CATCH_ANY {
util_print("FATAL ERROR!\n");
}
bn_free(n2);
return;
}
//Issue (non-)membership witnesses
t_witness * issueWitness(t_state * accumulator, bn_t y, bool isMembership) {
t_witness * w_y = (t_witness *)malloc(sizeof(t_witness));
bn_t tmp, one, yplusainv;
bn_null(w_y->y);
g1_null(w_y->C);
bn_null(w_y->d);
gt_null(w_y->eCPt);
bn_null(tmp);
bn_null(one);
bn_null(yplusainv);
TRY {
bn_new(w_y->y);
g1_new(w_y->C);
bn_new(w_y->d);
gt_new(w_y->eCPt);
bn_new(one);
bn_new(tmp);
bn_new(yplusainv);
//Compute 1/y+a = y+a^{-1} (n)
bn_add(tmp, y, accumulator->a);
bn_mod(tmp, tmp, accumulator->n);
//Computing inverse
bn_gcd_ext_lehme(one, yplusainv, tmp, tmp, accumulator->n);
assert(bn_cmp_dig(one,1) == RLC_EQ);
bn_mod(yplusainv, yplusainv, accumulator->n);
if (isMembership == true) {
g1_mul(w_y->C, accumulator->V, yplusainv);
bn_set_dig(w_y->d, 0);
}
else {
//Compute fV(-y)
bn_set_dig(w_y->d, 1);
//Computing fV(-y) everyime is better than keeping fV updated
for (int i=0; i< accumulator->totAccEl; i++) {
bn_sub(tmp, accumulator->Y[i], y);
bn_mul(w_y->d, w_y->d, tmp);
bn_mod(w_y->d, w_y->d, accumulator->n);
}
//Check y is not already accumulated
assert(bn_cmp_dig(w_y->d, 0) != RLC_EQ);
//Compute fV(a)-d
bn_sub(tmp, accumulator->fVa, w_y->d);
bn_mul(tmp, tmp, yplusainv);
bn_mod(tmp, tmp, accumulator->n);
//Compute fV(a)-d C = (fV(a)-d)/(y+a)P
g1_mul(w_y->C, accumulator->P, tmp);
}
//Store associated accumulator epoch and identity
w_y->epoch = accumulator->currentEpoch;
bn_copy(w_y->y, y);
//Set e(C,Pt) value for efficient ZK PoK
pc_map(w_y->eCPt, w_y->C, accumulator->Pt);
}
CATCH_ANY {
util_print("FATAL ERROR!\n");
}
bn_free(one);
bn_free(yplusainv);
bn_free(tmp);
return w_y;
}
//Witness Verification
void verifyWitness(t_state * accumulator, t_witness * wit) {
gt_t e, ed;
g2_t yPt, yPtpQt;
g2_null(yPt); g2_null(yPtpQt);
gt_null(e); gt_null(ed);
g2_new(yPt); g2_new(yPtpQt);
gt_new(e); gt_new(ed);
//yPtpQt = Qt + yPt
g2_copy(yPtpQt, accumulator->Qt);
g2_mul(yPt, accumulator->Pt, wit->y);
g2_add(yPtpQt, yPtpQt, yPt);
//e = e(C, yPt + Qt)
pc_map(e, wit->C, yPtpQt);
//e *= e(P, Pt)^d
gt_exp(ed, accumulator->ePPt, wit->d);
gt_mul(e, e, ed);
if (bn_cmp_dig(wit->d,0) == RLC_EQ)
printf("\tMembership ");
else
printf("\tNon-membership ");
if (gt_cmp(e, accumulator->eVPt) == RLC_EQ)
printf("Witness Verification SUCCEEDED!\n");
else
printf("Witness Verification FAILED!\n");
g2_free(yPtpQt);
gt_free(e);
gt_free(ed);
return;
}
//Update witness to current epoch
void updateWitness(t_state * accumulator, t_witness * w_y) {
bn_t x; bn_null(x); bn_new(x);
bn_t y; bn_null(y); bn_new(y);
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_t res; bn_null(res); bn_new(res);
g1_t g1tmp; g1_null(g1tmp); g1_new(g1tmp);
g1_t g1res; g1_null(g1res); g1_new(g1res);
g1_t * OmegaAdd;
bn_t * Y_add;
int addSize;
g1_t * OmegaDel;
bn_t * Y_del;
int delSize;
//All polynomials are computed in -x rather than x. I will simply evaluate in -y.
bn_neg(y, w_y->y);
while (w_y->epoch != accumulator->currentEpoch) {
OmegaAdd = (accumulator->epochs[w_y->epoch+1]).OmegaAdd;
addSize = (accumulator->epochs[w_y->epoch+1]).addSize;
Y_add = (accumulator->epochs[w_y->epoch+1]).Y_add;
OmegaDel = (accumulator->epochs[w_y->epoch+1]).OmegaDel;
delSize = (accumulator->epochs[w_y->epoch+1]).delSize;
Y_del = (accumulator->epochs[w_y->epoch+1]).Y_del;
g1_set_infty(g1res);
//TODO: combine Add and Del in one Omega
//Addition
if(addSize > 0) {
bn_set_dig(res,1);
bn_set_dig(x,1);
for(int i=0; i<addSize; i++){
//<Yy,Omega> computation
g1_mul(g1tmp, OmegaAdd[i], x);
g1_add(g1res, g1res, g1tmp);
//dA(y) computation
bn_add(tmp, Y_add[i], y);
bn_mul(res, res, tmp);
bn_mod(res, res, accumulator->n);
bn_mul(x,x,y);
bn_mod(x,x,accumulator->n);
}
g1_mul(w_y->C, w_y->C, res);
g1_add(w_y->C, w_y->C, g1res);
if (bn_cmp_dig(w_y->d,0) != RLC_EQ) {
bn_mul(w_y->d, w_y->d, res);
bn_mod(w_y->d, w_y->d, accumulator->n);
}
}
//Deletion
if(delSize > 0) {
bn_set_dig(x,1);
bn_set_dig(res,1);
for(int i=0; i<delSize; i++){
//<Yy,Omega> computation
g1_mul(g1tmp, OmegaDel[i], x);
g1_add(g1res, g1res, g1tmp);
//dD(y) computation
bn_add(tmp, Y_del[i], y);
bn_mul(res, res, tmp);
bn_mod(res, res, accumulator->n);
//x *= y (=y^i)
bn_mul(x,x,y);
bn_mod(x,x,accumulator->n);
}
//Compute 1/dD(y)
bn_t one; bn_null(one); bn_new(one); bn_set_dig(one, 1);
bn_t tmp2; bn_null(tmp2); bn_new(tmp2);
bn_gcd_ext_lehme(one, tmp, tmp2, res, accumulator->n);
//The inverse cannot be computed if current user was revoked
if (bn_cmp_dig(one,1) != RLC_EQ) {
printf("\tUser revoked. No update possible.\n");
return;
}
bn_copy(res, tmp);
bn_mod(res, res, accumulator->n);
//C' = 1/dD(y)*( C - vD(y)V )
g1_sub(w_y->C, w_y->C, g1res);
g1_mul(w_y->C, w_y->C, res);
if (bn_cmp_dig(w_y->d,0) != RLC_EQ) {
bn_mul(w_y->d, w_y->d, res);
bn_mod(w_y->d, w_y->d, accumulator->n);
}
}
//Update witness epoch
w_y->epoch += 1;
}
//Update e(C, Pt) value for efficient ZK PoK
pc_map(w_y->eCPt, w_y->C, accumulator->Pt);
return;
}
//Update witness to current epoch
void updateWitnessNonBatch(t_state * accumulator, t_witness * w_y) {
bn_t y; bn_null(y); bn_new(y);
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_t tmp2; bn_null(tmp2); bn_new(tmp2);
bn_t res; bn_null(res); bn_new(res);
bn_t one; bn_null(one); bn_new(one);
g1_t V; g1_null(V); g1_new(V);
bn_t * Y_add;
int addSize;
bn_t * Y_del;
int delSize;
//I will simply evaluate in -y.
bn_neg(y, w_y->y);
while (w_y->epoch != accumulator->currentEpoch) {
g1_copy(V, (accumulator->epochs[w_y->epoch]).V);
addSize = (accumulator->epochs[w_y->epoch+1]).addSize;
Y_add = (accumulator->epochs[w_y->epoch+1]).Y_add;
delSize = (accumulator->epochs[w_y->epoch+1]).delSize;
Y_del = (accumulator->epochs[w_y->epoch+1]).Y_del;
//Addition
if(addSize > 0) {
for(int i=0; i<addSize; i++){
//C' = (y'-y)C + V
bn_add(res, Y_add[i], y);
g1_mul(w_y->C, w_y->C, res);
g1_add(w_y->C, w_y->C, V);
//d' = (y'-y)d
if (bn_cmp_dig(w_y->d,0) != RLC_EQ) {
bn_mul(w_y->d, w_y->d, res);
bn_mod(w_y->d, w_y->d, accumulator->n);
}
//update acc V' = (y'+a)V
bn_add(res, Y_add[i], accumulator->a);
g1_mul(V, V, res);
}
}
//Deletion
if(delSize > 0) {
bn_set_dig(res,1);
for(int i=0; i<delSize; i++){
//update acc V' = 1/(y'+a)V
bn_add(res, Y_del[i], accumulator->a);
bn_gcd_ext_lehme(one, tmp, tmp2, res, accumulator->n);
if (bn_cmp_dig(one,1) != RLC_EQ) {
printf("\tUser revoked equals alpha.\n");
return;
}
bn_copy(res, tmp);
bn_mod(res, res, accumulator->n);
g1_mul(V, V, res);
//1/(y'-y) computation
bn_add(res, Y_del[i], y);
bn_mod(res, res, accumulator->n);
//Compute 1/(y'-y)
bn_gcd_ext_lehme(one, tmp, tmp2, res, accumulator->n);
if (bn_cmp_dig(one,1) != RLC_EQ) {
printf("\tUser revoked. No update possible.\n");
return;
}
bn_copy(res, tmp);
bn_mod(res, res, accumulator->n);
//C' = 1/(y'-y)*( C - V' )
g1_sub(w_y->C, w_y->C, V);
g1_mul(w_y->C, w_y->C, res);
if (bn_cmp_dig(w_y->d,0) != RLC_EQ) {
bn_mul(w_y->d, w_y->d, res);
bn_mod(w_y->d, w_y->d, accumulator->n);
}
}
}
//Update witness epoch
w_y->epoch += 1;
}
//Update e(C, Pt) value for efficient ZK PoK
pc_map(w_y->eCPt, w_y->C, accumulator->Pt);
return;
}
//Basic Batch Addition (no batch update data)
void basicBatchAdd(t_state * accumulator, int batchSize) {
//Check if maxUsers is reached
assert(accumulator->totAccEl + batchSize <= maxUsers);
bn_t currfVa; bn_null(currfVa); bn_new(currfVa); bn_set_dig(currfVa, 1);
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_t * Y_add = (bn_t *)malloc(sizeof(bn_t)*batchSize);
clock_t start,end; double cpu_time_used = 0;
for (int i = 0; i<batchSize; i++) {
//Initializing arrays
bn_null(Y_add[i]); bn_new(Y_add[i]);
//Random y element. We measure the time needed to generate elements
start = clock();
bn_rand_mod(Y_add[i], accumulator->n);
end = clock();
cpu_time_used += ((double) (end - start)) / CLOCKS_PER_SEC;
//Computing Pr(y+a)
bn_add(tmp, Y_add[i], accumulator->a);
bn_mul(currfVa, currfVa, tmp);
bn_mod(currfVa, currfVa, accumulator->n);
}
printf("~~Elements generation and storage took %fs\n", cpu_time_used);
//Copy the batch added elements to all accumulated ones
memcpy(accumulator->Y+accumulator->totAccEl, Y_add, sizeof(bn_t)*batchSize);
//Update fVa (for non-membership witnesses)
bn_mul(accumulator->fVa, accumulator->fVa, currfVa);
bn_mod(accumulator->fVa, accumulator->fVa, accumulator->n);
//Accumulator Update
g1_mul(accumulator->V, accumulator->V, currfVa);
//Update cached pairing value
gt_exp(accumulator->eVPt, accumulator->eVPt, currfVa);
//Update total number of accumulated elements
accumulator->totAccEl = accumulator->totAccEl + batchSize;
bn_free(tmp);
bn_free(currfVa);
free(Y_add);
return;
}
//Basic Batch Deletion (no batch update data)
void basicBatchDel(t_state * accumulator, int batchSize) {
//Check if there are enough elements to delete
assert(accumulator->totAccEl - batchSize >= 0);
bn_t currfVa; bn_null(currfVa); bn_new(currfVa); bn_set_dig(currfVa, 1);
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_t tmp2; bn_null(tmp2); bn_new(tmp2);
bn_t one; bn_null(one); bn_new(one);
bn_t * Y_del = (bn_t *)malloc(sizeof(bn_t)*batchSize);
//Pick random added elements to delete. Last index set to totAccEl to properly memmove
int * delIdx = randomIdx(accumulator->totAccEl, batchSize);
//delIdx[batchSize] = accumulator->totAccEl;
//Sort indexes
qsort(delIdx, batchSize, sizeof(int), compare);
for (int i = 0; i<batchSize; i++) {
//Store deleted elements
bn_null(Y_del[i]); bn_new(Y_del[i]); bn_copy(Y_del[i], accumulator->Y[delIdx[i]]);
//Removing deleted elements from accumulated values set
memmove(accumulator->Y + delIdx[i] - i, accumulator->Y + delIdx[i] + 1, sizeof(bn_t)*(delIdx[i+1]-delIdx[i]-1));
//Computing Prod(y+a)
bn_add(tmp, Y_del[i], accumulator->a);
bn_mul(currfVa, currfVa, tmp);
bn_mod(currfVa, currfVa, accumulator->n);
}
//tmp = 1/currfVa
bn_gcd_ext_lehme(one, tmp, tmp2, currfVa, accumulator->n);
assert(bn_cmp_dig(one,1) == RLC_EQ);
bn_mod(tmp, tmp, accumulator->n);
//Update fVa (for non-membership witnesses)
bn_mul(accumulator->fVa, accumulator->fVa, tmp);
bn_mod(accumulator->fVa, accumulator->fVa, accumulator->n);
//Update Accumulator value
g1_mul(accumulator->V, accumulator->V, tmp);
//Update cached pairing value
gt_exp(accumulator->eVPt, accumulator->eVPt, tmp);
//Update total number of accumulated elements
accumulator->totAccEl = accumulator->totAccEl - batchSize;
bn_free(tmp);
bn_free(currfVa);
bn_free(y_add);
return;
}
//Batch Addition operation
void batchAdd(t_state * accumulator, int batchSize) {
//Check if maxUsers is reached
assert(accumulator->totAccEl + batchSize <= maxUsers);
//Generate batchMax random elements
bn_t currfVa; bn_null(currfVa); bn_new(currfVa); bn_set_dig(currfVa, 1);
bn_t tmp; bn_null(tmp); bn_new(tmp);
bn_t * tmpPoly = (bn_t *)malloc(sizeof(bn_t)*(batchSize));
bn_t * vA = (bn_t *)malloc(sizeof(bn_t)*(batchSize));
bn_t * Y_add = (bn_t *)malloc(sizeof(bn_t)*batchSize);
bn_t * lpr = (bn_t *)malloc(sizeof(bn_t)*batchSize);
bn_null(lpr[0]); bn_new(lpr[0]); bn_set_dig(lpr[0],1);
for (int i = 0; i<batchSize; i++) {
//Initializing arrays
bn_null(Y_add[i]); bn_new(Y_add[i]);
//Random y element
bn_rand_mod(Y_add[i], accumulator->n);
//Computing Pr(y+a)
bn_add(tmp, Y_add[i], accumulator->a);
bn_mul(currfVa, currfVa, tmp);
bn_mod(currfVa, currfVa, accumulator->n);
//I store consecutive products up to batchSize-1
if (i<batchSize-1) {
bn_null(lpr[i+1]); bn_new(lpr[i+1]);
bn_copy(lpr[i+1], currfVa);
}
//tmpPoly initilization
bn_null(tmpPoly[i]); bn_new(tmpPoly[i]);
if (i==0)
bn_set_dig(tmpPoly[i], 1);
else
bn_set_dig(tmpPoly[i], 0);
//vA initialization
bn_null(vA[i]); bn_new(vA[i]); bn_set_dig(vA[i], 0);
}
//Store partial product polynomial for witness update
for (int i=0; i<batchSize; i++) {
if (i>0) {
memmove(tmpPoly+1, tmpPoly, sizeof(bn_t)*i);
bn_set_dig(tmpPoly[0],0);
}
for (int j=0; j<=accumulator->totAccEl+i; j++) {
if ((i>0) && (j<=i-1)) {
bn_mul(tmp,tmpPoly[j+1],Y_add[i]);
bn_add(tmp,tmpPoly[j],tmp);
bn_mod(tmpPoly[j],tmp,accumulator->n);
}
}
}
//Omega EC points vector
//External Sum
for(int s=0; s<batchSize; s++) {
//Add to vA lpr*tmpPoly
for(int i=0; i<batchSize; i++) {
bn_mul(tmp, lpr[s], tmpPoly[i]);
bn_add(vA[i], vA[i], tmp);
bn_mod(vA[i], vA[i], accumulator->n);
}
if (s<batchSize-1) {
//Dividing tmpPoly by y+x
for (int j=batchSize-s-1; j>0; j--) {
bn_mul(tmp,tmpPoly[j],Y_add[s+1]);
bn_sub(tmpPoly[j-1],tmpPoly[j-1],tmp);
bn_mod(tmpPoly[j-1],tmpPoly[j-1],accumulator->n);
}
memmove(tmpPoly, tmpPoly+1, sizeof(bn_t)*(batchSize-s-1));
bn_set_dig(tmpPoly[batchSize-s-1],0);
}
}
free(tmpPoly);
free(lpr);
//Copy the batch added elements to all accumulated ones
memcpy(accumulator->Y+accumulator->totAccEl, Y_add, sizeof(bn_t)*batchSize);
//Update total number of accumulated elements
accumulator->totAccEl = accumulator->totAccEl + batchSize;
//Update fVa (for non-membership witnesses)
bn_mul(accumulator->fVa, accumulator->fVa, currfVa);
bn_mod(accumulator->fVa, accumulator->fVa, accumulator->n);
//Computing the ECP vector from vA. Note we need V before update.
g1_t * OmegaAdd = (g1_t *)malloc(sizeof(g1_t)*(batchSize));
for (int i=0; i<batchSize; i++){
g1_null(OmegaAdd[i]); g1_new(OmegaAdd[i]);
g1_mul(OmegaAdd[i], accumulator->V, vA[i]);
}
free(vA);
//Update accumulator value
g1_mul(accumulator->V, accumulator->V, currfVa);
//Update cached pairing value
gt_exp(accumulator->eVPt, accumulator->eVPt, currfVa);
//Update epoch
accumulator->currentEpoch += 1;
g1_copy((accumulator->epochs[accumulator->currentEpoch]).V, accumulator->V);
(accumulator->epochs[accumulator->currentEpoch]).Y_add = Y_add;
(accumulator->epochs[accumulator->currentEpoch]).Y_del = NULL;
(accumulator->epochs[accumulator->currentEpoch]).addSize = batchSize;
(accumulator->epochs[accumulator->currentEpoch]).delSize = 0;
(accumulator->epochs[accumulator->currentEpoch]).OmegaAdd = OmegaAdd;
(accumulator->epochs[accumulator->currentEpoch]).OmegaDel = NULL;
bn_free(tmp);
bn_free(currfVa);
return;
}
//Batch Deletion operation
void batchDel(t_state * accumulator, int batchSize) {
//Check if there are enough elements to delete
assert(accumulator->totAccEl - batchSize >= 0);
bn_t tmp, tmp2, currfVa, one;
bn_t * Y_del = (bn_t *)malloc(sizeof(bn_t)*batchSize);
bn_t * lpr = (bn_t *)malloc(sizeof(bn_t)*batchSize);
bn_t * tmpPoly = (bn_t *)malloc(sizeof(bn_t)*(batchSize));
bn_t * vD = (bn_t *)malloc(sizeof(bn_t)*(batchSize));
bn_null(tmp); bn_new(tmp);
bn_null(tmp2); bn_new(tmp2);
bn_null(one); bn_new(one);
bn_null(currfVa); bn_new(currfVa); bn_set_dig(currfVa, 1);
//Pick random added elements to delete. Last index set to totAccEl in randomIdx to properly memmove
int * delIdx = randomIdx(accumulator->totAccEl, batchSize);
//Sort indexes
qsort(delIdx, batchSize, sizeof(int), compare);
for(int i=0; i<batchSize; i++) {
//Store deleted elements
bn_null(Y_del[i]); bn_new(Y_del[i]); bn_copy(Y_del[i], accumulator->Y[delIdx[i]]);
//Removing deleted elements from accumulated values set
memmove(accumulator->Y + delIdx[i] - i, accumulator->Y + delIdx[i] + 1, sizeof(bn_t)*(delIdx[i+1]-delIdx[i]-1));
//Computing Prod(y+a)
bn_add(tmp, Y_del[i], accumulator->a);
bn_mul(currfVa, currfVa, tmp);
bn_mod(currfVa, currfVa, accumulator->n);
//I store consecutive products up to batchSize-1
bn_null(lpr[i]); bn_new(lpr[i]);
bn_gcd_ext_lehme(one, tmp, tmp2, currfVa, accumulator->n);
assert(bn_cmp_dig(one,1) == RLC_EQ);
bn_mod(tmp, tmp, accumulator->n);
bn_copy(lpr[i], tmp);
//tmpPoly initilization
bn_null(tmpPoly[i]); bn_new(tmpPoly[i]);
if (i==0)
bn_set_dig(tmpPoly[i], 1);
else
bn_set_dig(tmpPoly[i], 0);
//vA initialization
bn_null(vD[i]); bn_new(vD[i]); bn_set_dig(vD[i], 0);
}
for (int s=0; s<batchSize; s++) {
//sum tmpPoly to vD
//Add to vA lpr*tmpPoly
for(int i=0; i<s+1; i++) {
bn_mul(tmp,tmpPoly[i], lpr[s]);
bn_mod(tmp,tmp,accumulator->n);
bn_add(vD[i], vD[i], tmp);
bn_mod(vD[i], vD[i], accumulator->n);
}
if (s<batchSize-1) {
memmove(tmpPoly+1, tmpPoly, sizeof(bn_t)*(s+1));
bn_set_dig(tmpPoly[0],0);
for (int j=0; j<=s; j++) {
bn_mul(tmp,tmpPoly[j+1],Y_del[s]);
bn_add(tmp,tmpPoly[j],tmp);
bn_mod(tmpPoly[j],tmp,accumulator->n);
}
}
}
free(tmpPoly);
//Computing the ECP vector from vA. Note we need -V before update.
g1_t * OmegaDel = (g1_t *)malloc(sizeof(g1_t)*(batchSize));
for (int i=0; i<batchSize; i++){
g1_null(OmegaDel[i]); g1_new(OmegaDel[i]);
g1_mul(OmegaDel[i], accumulator->V, vD[i]);
}
free(vD);
//TODO: we can skip this step. totAccEl is the relevant parameter
//Zeroing the trail moved memory
for(int i=0; i<batchSize; i++)
bn_set_dig(accumulator->Y[accumulator->totAccEl-batchSize+i], 0);
//Updating fVa and V
bn_mul(accumulator->fVa, accumulator->fVa, lpr[batchSize-1]);
bn_mod(accumulator->fVa, accumulator->fVa, accumulator->n);
g1_mul(accumulator->V, accumulator->V, lpr[batchSize-1]);
//Update cached pairing value
gt_exp(accumulator->eVPt, accumulator->eVPt, lpr[batchSize-1]);
//Updating the total number of accumulated values
accumulator->totAccEl = accumulator->totAccEl-batchSize;
//Update epoch
accumulator->currentEpoch += 1;
g1_copy((accumulator->epochs[accumulator->currentEpoch]).V, accumulator->V);
(accumulator->epochs[accumulator->currentEpoch]).Y_add = NULL;
(accumulator->epochs[accumulator->currentEpoch]).Y_del = Y_del;
(accumulator->epochs[accumulator->currentEpoch]).addSize = 0;
(accumulator->epochs[accumulator->currentEpoch]).delSize = batchSize;
(accumulator->epochs[accumulator->currentEpoch]).OmegaAdd = NULL;
(accumulator->epochs[accumulator->currentEpoch]).OmegaDel = OmegaDel;
free(lpr);
free(delIdx);
bn_free(tmp);
bn_free(currfVa);
bn_free(tmp2);
return;
}
//Witness Verification
void zkVerifier(t_state * accumulator, t_proof * proof) {
unsigned char hash[SHA512_DIGEST_LENGTH];
int data_size = 0, pos = 0, l;
gt_t Re;
g1_t Rsigma;
g1_t Rrho;
g1_t Rdeltasigma;
g1_t Rdeltarho;
g1_t tmp;
g1_t Ra;
g1_t Rb;
g2_t tmp2;
gt_t tmpt;
bn_t tmpb;
bn_t c;
gt_null(Re); gt_new(Re); gt_set_unity(Re);
g1_null(Rsigma); g1_new(Rsigma);
g1_null(Rrho); g1_new(Rrho);
g1_null(Rdeltasigma); g1_new(Rdeltasigma);
g1_null(Rdeltarho); g1_new(Rdeltarho);
g1_null(tmp); g1_new(tmp);
g2_null(tmp2); g2_new(tmp2);
gt_null(tmpt); gt_new(tmpt);
bn_null(tmpb); bn_new(tmpb);
bn_null(c); bn_new(c);
//Rsigma = ssigmaX - cTsigma
bn_neg(tmpb, proof->c);
bn_mod(tmpb, tmpb, accumulator->n);
g1_mul_sim(Rsigma, accumulator->X, proof->ssigma, proof->Tsigma, tmpb);
//Rrho = srhoJ - cTrho (-c = tmpb)
g1_mul_sim(Rrho, accumulator->J, proof->srho, proof->Trho, tmpb);
//Rdeltasigma = syTsigma - sdeltasigmaX
bn_neg(tmpb, proof->sdeltasigma);
bn_mod(tmpb, tmpb, accumulator->n);
g1_mul_sim(Rdeltasigma, proof->Tsigma, proof->sy, accumulator->X, tmpb);
//Rdeltarho = syTrho - sdeltarhoJ
bn_neg(tmpb, proof->sdeltarho);
bn_mod(tmpb, tmpb, accumulator->n);
g1_mul_sim(Rdeltarho, proof->Trho, proof->sy, accumulator->J, tmpb);
//Re
//Re *= e(Ec,syPt + cQt)
g2_mul_sim(tmp2, accumulator->Pt, proof->sy, accumulator->Qt, proof->c);
pc_map(tmpt, proof->Ec, tmp2);
gt_mul(Re, Re, tmpt);
//Re *= e(Z,Pt)^(-sdeltasigma -sdeltarho)
bn_add(tmpb, proof->sdeltasigma, proof->sdeltarho);
bn_neg(tmpb, tmpb);
bn_mod(tmpb, tmpb, accumulator->n);
gt_exp(tmpt, accumulator->eZPt, tmpb);
gt_mul(Re, Re, tmpt);
//Re *= e(Z,Qt)^(-ssigma -srho)
bn_add(tmpb, proof->ssigma, proof->srho);
bn_neg(tmpb, tmpb);
bn_mod(tmpb, tmpb, accumulator->n);
gt_exp(tmpt, accumulator->eZQt, tmpb);
gt_mul(Re, Re, tmpt);
//Re *= e(V,Pt)^(-c)
bn_neg(tmpb, proof->c);
bn_mod(tmpb, tmpb, accumulator->n);
gt_exp(tmpt, accumulator->eVPt, tmpb);
gt_mul(Re, Re, tmpt);
if (!proof->isMembership) {
g1_null(Ra); g1_new(Ra); g1_set_infty(Ra);
g1_null(Rb); g1_new(Rb); g1_set_infty(Rb);
//Ra = suP + svK - cEd
g1_mul(tmp, accumulator->P, proof->su);
g1_add(Ra, Ra, tmp);
g1_mul(tmp, accumulator->K, proof->sv);
g1_add(Ra, Ra, tmp);
g1_mul(tmp, proof->Ed, proof->c);
g1_sub(Ra, Ra, tmp);
//Rb = swK + suEd_1 - cP
g1_mul(tmp, accumulator->K, proof->sw);
g1_add(Rb, Rb, tmp);
g1_mul(tmp, proof->Ed_1, proof->su);
g1_add(Rb, Rb, tmp);
g1_mul(tmp, accumulator->P, proof->c);
g1_sub(Rb, Rb, tmp);
//Re *= e(K,Pt)^(-sv)
bn_neg(tmpb, proof->sv);
bn_mod(tmpb, tmpb, accumulator->n);
gt_exp(tmpt, accumulator->eKPt, tmpb);
gt_mul(Re, Re, tmpt);
//Re *= e(Ed,P)^c
pc_map(tmpt, proof->Ed, accumulator->Pt);
gt_exp(tmpt, tmpt, proof->c);
gt_mul(Re, Re, tmpt);
//Update data size
data_size += g1_size_bin(proof->Ed , 1) +
g1_size_bin(proof->Ed_1, 1) +
g1_size_bin(Ra , 1) +
g1_size_bin(Rb , 1);
}
//Update data size (not before because Re can be updated by non-membership)
data_size += g1_size_bin(accumulator->V, 1) +
g1_size_bin(proof->Ec , 1) +
g1_size_bin(proof->Tsigma , 1) +
g1_size_bin(proof->Trho , 1) +
gt_size_bin(Re , 1) +
g1_size_bin(Rsigma , 1) +
g1_size_bin(Rrho , 1) +
g1_size_bin(Rdeltasigma , 1) +
g1_size_bin(Rdeltarho , 1);
//Point normalization
g1_norm(Rsigma, Rsigma);
g1_norm(Rrho, Rrho);
g1_norm(Rdeltasigma, Rdeltasigma);
g1_norm(Rdeltarho, Rdeltarho);
//Collecting challenge element in one buffer for hash
//c = H(V,Ec,Tsigma,Trho,Re,Rsigma,Rrho,Rdeltasigma,Rdeltarho, - Ed,Ed_1,Ra,Rb);
unsigned char * data = malloc(sizeof(char)*data_size);
l = g1_size_bin(accumulator->V, 1); g1_write_bin(data + pos, l, accumulator->V, 1); pos += l;
l = g1_size_bin(proof->Ec , 1); g1_write_bin(data + pos, l, proof->Ec , 1); pos += l;
l = g1_size_bin(proof->Tsigma , 1); g1_write_bin(data + pos, l, proof->Tsigma , 1); pos += l;
l = g1_size_bin(proof->Trho , 1); g1_write_bin(data + pos, l, proof->Trho , 1); pos += l;
l = gt_size_bin(Re , 1); gt_write_bin(data + pos, l, Re , 1); pos += l;
l = g1_size_bin(Rsigma , 1); g1_write_bin(data + pos, l, Rsigma , 1); pos += l;
l = g1_size_bin(Rrho , 1); g1_write_bin(data + pos, l, Rrho , 1); pos += l;