Source file custom_gate.ml
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
module Custom_gate_impl (PP : Polynomial_protocol.Polynomial_protocol_sig) =
struct
module PP = PP
module MP = PP.MP
module Scalar = PP.PC.Scalar
module Domain = PP.PC.Polynomial.Domain
module Poly = PP.PC.Polynomial.Polynomial
module Evaluations = PP.Evaluations
module MPoly = PP.MP.Polynomial
module Fr_generation = PP.PC.Fr_generation
let monomial_of_list = SMap.monomial_of_list
let one = Scalar.one
let minus_one = Scalar.negate one
let x_poly = Poly.of_coefficients [ (one, 1) ]
let left = "a"
let right = "b"
let output = "c"
let next_left = "ag"
let next_right = "bg"
let next_output = "cg"
let prefix_list prefix x = List.map (fun x -> prefix ^ x) x
let arith = "Arith"
module type Gate_base_sig = sig
val q_label : string
val blinds : int array SMap.t
val identity : string * int
val equations :
q:PP.PC.Scalar.t ->
a:PP.PC.Scalar.t ->
b:PP.PC.Scalar.t ->
c:PP.PC.Scalar.t ->
ag:PP.PC.Scalar.t ->
bg:PP.PC.Scalar.t ->
cg:PP.PC.Scalar.t ->
?table:PP.PC.Scalar.t array array ->
unit ->
PP.PC.Scalar.t list
val prover_query :
prefix:string ->
public_inputs:PP.PC.Scalar.t array ->
domain:PP.PC.Polynomial.Domain.t ->
evaluations:Evaluations.t SMap.t ->
PP.prover_query
val verifier_query :
prefix:string ->
generator:PP.PC.Scalar.t ->
size_domain:int ->
PP.verifier_query
val polynomials_degree : int SMap.t
end
module type Params = sig
val wire : string
val selector : string
val is_next : bool
end
module AddWire_gate (Params : Params) : Gate_base_sig = struct
let next_name = Params.wire ^ "g"
let q_label = Params.selector
let identity = (arith, 1)
let equations ~q ~a ~b ~c ~ag ~bg ~cg ?table:_ () =
let var =
match Params.wire with
| s when s = left -> if Params.is_next then ag else a
| s when s = right -> if Params.is_next then bg else b
| s when s = output -> if Params.is_next then cg else c
| _ -> assert false
in
Scalar.[ q * var ]
let blinds =
let array = if Params.is_next then [| 0; 1 |] else [| 1; 0 |] in
SMap.singleton Params.wire array
let identities prefix =
let wire_name = if Params.is_next then next_name else Params.wire in
SMap.singleton
(prefix ^ arith ^ ".0")
(MP.Polynomial.of_list
[
(monomial_of_list (prefix_list prefix [ q_label; wire_name ]), one);
])
let v_map ~prefix generator =
if Params.is_next then
let g_poly = Poly.of_coefficients [ (generator, 1) ] in
SMap.singleton (prefix ^ next_name) (prefix ^ Params.wire, g_poly)
else SMap.empty
let prover_query ~prefix ~public_inputs:_ ~domain ~evaluations =
let qf =
let poly_names = [ q_label; Params.wire ] in
let composition_gx =
if Params.is_next then ([ 0; 1 ], Domain.length domain)
else ([ 0; 0 ], 1)
in
let res = Evaluations.find_evaluation evaluations "tmp_eval" in
Evaluations.mul ~res ~evaluations ~poly_names ~composition_gx ()
in
let precomputed_polys = SMap.singleton (arith ^ ".0") qf in
PP.{ v_map = v_map ~prefix (Domain.get domain 1); precomputed_polys }
let verifier_query ~prefix ~generator ~size_domain:_ =
let v_map = v_map ~prefix generator in
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree = SMap.of_list [ (Params.wire, 2); (q_label, 2) ]
end
module AddOutput_gate = AddWire_gate (struct
let wire = output
let selector = "qo"
let is_next = false
end)
module AddLeft_gate = AddWire_gate (struct
let wire = left
let selector = "ql"
let is_next = false
end)
module AddRight_gate = AddWire_gate (struct
let wire = right
let selector = "qr"
let is_next = false
end)
module AddNextOutput_gate = AddWire_gate (struct
let wire = output
let selector = "qog"
let is_next = true
end)
module AddNextLeft_gate = AddWire_gate (struct
let wire = left
let selector = "qlg"
let is_next = true
end)
module AddNextRight_gate = AddWire_gate (struct
let wire = right
let selector = "qrg"
let is_next = true
end)
module Constant_gate : Gate_base_sig = struct
let q_label = "qc"
let blinds = SMap.empty
let identity = (arith, 1)
let equations ~q ~a:_ ~b:_ ~c:_ ~ag:_ ~bg:_ ~cg:_ ?table:_ () = [ q ]
let identities prefix =
SMap.singleton
(prefix ^ arith ^ ".0")
(MP.Polynomial.of_list [ (monomial_of_list [ prefix ^ q_label ], one) ])
let v_map = SMap.empty
let prover_query ~prefix:_ ~public_inputs:_ ~domain:_ ~evaluations =
let res = Evaluations.find_evaluation evaluations "tmp_eval" in
PP.
{
v_map;
precomputed_polys =
SMap.singleton (arith ^ ".0")
(Evaluations.copy ~res (SMap.find q_label evaluations));
}
let verifier_query ~prefix ~generator:_ ~size_domain:_ =
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree = SMap.empty
end
module Multiplication_gate : Gate_base_sig = struct
let q_label = "qm"
let identity = (arith, 1)
let equations ~q ~a ~b ~c:_ ~ag:_ ~bg:_ ~cg:_ ?table:_ () =
Scalar.[ q * a * b ]
let blinds = SMap.of_list [ (right, [| 1; 0 |]); (left, [| 1; 0 |]) ]
let identities prefix =
SMap.singleton
(prefix ^ arith ^ ".0")
(MP.Polynomial.of_list
[
( monomial_of_list (prefix_list prefix [ q_label; left; right ]),
one );
])
let v_map = SMap.empty
let prover_query ~prefix:_ ~public_inputs:_ ~domain:_ ~evaluations =
let res = Evaluations.find_evaluation evaluations "tmp_eval" in
let qmflfr =
Evaluations.mul ~res ~evaluations ~poly_names:[ q_label; left; right ]
()
in
let precomputed_polys = SMap.singleton (arith ^ ".0") qmflfr in
PP.{ v_map; precomputed_polys }
let verifier_query ~prefix ~generator:_ ~size_domain:_ =
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree =
SMap.of_list [ (left, 3); (right, 3); (q_label, 3) ]
end
module AddWeierstrass_gate : Gate_base_sig = struct
let q_label = "qecc_ws_add"
let blinds =
SMap.of_list
[ (right, [| 1; 1 |]); (left, [| 1; 1 |]); (output, [| 1; 1 |]) ]
let identity = (q_label, 2)
let equations ~q ~a ~b ~c ~ag ~bg ~cg ?table:_ () =
if Scalar.is_zero q then Scalar.[ zero; zero ]
else
let lambda = Scalar.(div_exn (bg + negate ag) (b + negate a)) in
let x = Scalar.((lambda * lambda) + negate a + negate b) in
let y = Scalar.((lambda * (a + negate x)) + negate ag) in
Scalar.[ x + negate c; y + negate cg ]
let identities prefix =
let q_label = prefix ^ q_label in
let left = prefix ^ left in
let right = prefix ^ right in
let output = prefix ^ output in
let next_left = prefix ^ next_left in
let next_right = prefix ^ next_right in
let next_output = prefix ^ next_output in
let q_poly =
MP.Polynomial.of_list [ (monomial_of_list [ q_label ], one) ]
in
let num_lambda =
MP.Polynomial.of_list
[
(monomial_of_list [ next_right ], one);
(monomial_of_list [ next_left ], minus_one);
]
in
let denom_lambda =
MP.Polynomial.of_list
[
(monomial_of_list [ right ], one);
(monomial_of_list [ left ], minus_one);
]
in
let first_identity =
let left_part =
let sum_fi =
MP.Polynomial.of_list
[
(monomial_of_list [ output ], one);
(monomial_of_list [ right ], one);
(monomial_of_list [ left ], one);
]
in
let denom_lambda2 = MPoly.mul denom_lambda denom_lambda in
MPoly.mul sum_fi denom_lambda2
in
let right_part = MPoly.mul num_lambda num_lambda in
let first = MPoly.sub left_part right_part in
MPoly.mul q_poly first
in
let second_identity =
let fog_p_flg =
MP.Polynomial.of_list
[
(monomial_of_list [ next_output ], one);
(monomial_of_list [ next_left ], one);
]
in
let left_part = MPoly.mul fog_p_flg denom_lambda in
let fl_m_fo =
MP.Polynomial.of_list
[
(monomial_of_list [ left ], one);
(monomial_of_list [ output ], minus_one);
]
in
let right_part = MPoly.mul fl_m_fo num_lambda in
let second = MPoly.sub left_part right_part in
MPoly.mul q_poly second
in
SMap.of_list
[ (q_label ^ ".0", first_identity); (q_label ^ ".1", second_identity) ]
let v_map ~prefix generator =
let g_poly = Poly.of_coefficients [ (generator, 1) ] in
SMap.of_list
[
(prefix ^ next_left, (prefix ^ left, g_poly));
(prefix ^ next_right, (prefix ^ right, g_poly));
(prefix ^ next_output, (prefix ^ output, g_poly));
]
let prover_query ~prefix ~public_inputs:_ ~domain ~evaluations =
let g = Domain.get domain 1 in
let domain_size = Domain.length domain in
let eval_q_label = Evaluations.find_evaluation evaluations q_label in
let eval_length = Evaluations.length eval_q_label in
let id1_evaluation = Evaluations.create eval_length in
let id2_evaluation = Evaluations.create eval_length in
let tmp1_evaluation = Evaluations.create eval_length in
let tmp2_evaluation = Evaluations.create eval_length in
let tmp3_evaluation = Evaluations.create eval_length in
let eval_b_minus_a =
Evaluations.linear ~res:tmp3_evaluation ~evaluations
~poly_names:[ right; left ] ~linear_coeffs:[ one; minus_one ] ()
in
let eval_b_minus_a_sqr =
Evaluations.mul_c ~res:tmp2_evaluation ~evaluations:[ eval_b_minus_a ]
~powers:[ 2 ] ()
in
let eval_a_plus_b_plus_c =
Evaluations.linear ~res:id2_evaluation ~evaluations
~poly_names:[ left; right; output ] ()
in
let eval_left_term =
Evaluations.mul_c ~res:tmp1_evaluation
~evaluations:[ eval_a_plus_b_plus_c; eval_b_minus_a_sqr ]
()
in
let eval_first_identity =
Evaluations.linear_c ~res:id2_evaluation
~evaluations:[ eval_left_term; eval_b_minus_a_sqr ]
~composition_gx:([ 0; 1 ], domain_size)
~linear_coeffs:[ one; minus_one ] ()
in
let first_identity =
Evaluations.mul_c ~res:id1_evaluation
~evaluations:[ eval_q_label; eval_first_identity ]
()
in
let eval_cg_plus_ag =
Evaluations.linear ~res:id2_evaluation ~evaluations
~poly_names:[ output; left ]
~composition_gx:([ 1; 1 ], domain_size)
()
in
let eval_left_term_2 =
Evaluations.mul_c ~res:tmp1_evaluation
~evaluations:[ eval_cg_plus_ag; eval_b_minus_a ]
()
in
let eval_a_minus_c =
Evaluations.linear ~res:id2_evaluation ~evaluations
~poly_names:[ left; output ] ~linear_coeffs:[ one; minus_one ] ()
in
let eval_right_term_2 =
Evaluations.mul_c ~res:tmp2_evaluation
~evaluations:[ eval_b_minus_a; eval_a_minus_c ]
~composition_gx:([ 1; 0 ], domain_size)
()
in
let eval_second_identity =
Evaluations.linear_c ~res:tmp3_evaluation
~evaluations:[ eval_left_term_2; eval_right_term_2 ]
~linear_coeffs:[ one; minus_one ] ()
in
let second_identity =
Evaluations.mul_c ~res:id2_evaluation
~evaluations:[ eval_q_label; eval_second_identity ]
()
in
let precomputed_polys =
SMap.of_list
[
(q_label ^ ".0", first_identity); (q_label ^ ".1", second_identity);
]
in
PP.{ v_map = v_map ~prefix g; precomputed_polys }
let verifier_query ~prefix ~generator ~size_domain:_ =
let v_map = v_map ~prefix generator in
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree =
SMap.of_list [ (left, 4); (right, 4); (output, 4); (q_label, 4) ]
end
module AddEdwards_gate : Gate_base_sig = struct
let q_label = "qecc_ed_add"
let blinds =
SMap.of_list
[ (right, [| 1; 1 |]); (left, [| 1; 1 |]); (output, [| 1; 1 |]) ]
let identity = (q_label, 2)
let a = Scalar.(negate one)
let d =
Scalar.of_string
"19257038036680949359750312669786877991949435402254120286184196891950884077233"
let equations ~q ~a ~b ~c ~ag ~bg ~cg ?table:_ () =
if Scalar.is_zero q then Scalar.[ zero; zero ]
else
let a_curve = Scalar.(negate one) in
let xpyq = Scalar.(a * bg) in
let xqyp = Scalar.(b * ag) in
let ypyq = Scalar.(bg * ag) in
let xpxq = Scalar.(b * a) in
let xr = Scalar.((xpyq + xqyp) / (one + (d * xpyq * xqyp))) in
let yr =
Scalar.(
(ypyq + (negate a_curve * xpxq)) / (one + (negate d * xpyq * xqyp)))
in
Scalar.[ xr + negate c; yr + negate cg ]
let identities prefix =
let q_label = prefix ^ q_label in
let left = prefix ^ left in
let right = prefix ^ right in
let output = prefix ^ output in
let next_left = prefix ^ next_left in
let next_right = prefix ^ next_right in
let next_output = prefix ^ next_output in
let q_poly =
MP.Polynomial.of_list [ (monomial_of_list [ q_label ], one) ]
in
let one_poly = MP.Polynomial.of_list [ (MP.Monomial.one, one) ] in
let x1 = MP.Polynomial.singleton left in
let y1 = MP.Polynomial.singleton next_left in
let x2 = MP.Polynomial.singleton right in
let y2 = MP.Polynomial.singleton next_right in
let x3 = MP.Polynomial.singleton output in
let y3 = MP.Polynomial.singleton next_output in
let x1x2 = MPoly.mul x1 x2 in
let y1y2 = MPoly.mul y1 y2 in
let x1y2 = MPoly.mul x1 y2 in
let x2y1 = MPoly.mul x2 y1 in
let xys = MPoly.mul x1x2 y1y2 in
let denom = MPoly.mul_scalar d xys in
let first_identity =
let num = MPoly.add x1y2 x2y1 in
let denom = MPoly.add one_poly denom in
let x3_times_denom = MPoly.mul x3 denom in
let first = MPoly.sub x3_times_denom num in
MPoly.mul q_poly first
in
let second_identity =
let tmp = MPoly.mul_scalar a x1x2 in
let num = MPoly.sub y1y2 tmp in
let denom = MPoly.sub one_poly denom in
let y3_times_denom = MPoly.mul y3 denom in
let second = MPoly.sub y3_times_denom num in
MPoly.mul q_poly second
in
SMap.of_list
[ (q_label ^ ".0", first_identity); (q_label ^ ".1", second_identity) ]
let v_map ~prefix generator =
let g_poly = Poly.of_coefficients [ (generator, 1) ] in
SMap.of_list
[
(prefix ^ next_left, (prefix ^ left, g_poly));
(prefix ^ next_right, (prefix ^ right, g_poly));
(prefix ^ next_output, (prefix ^ output, g_poly));
]
let prover_query ~prefix ~public_inputs:_ ~domain ~evaluations =
let g = Domain.get domain 1 in
let domain_size = Domain.length domain in
let eval_c = Evaluations.find_evaluation evaluations output in
let eval_q_label = Evaluations.find_evaluation evaluations q_label in
let eval_length = Evaluations.length eval_q_label in
let id1_evaluation = Evaluations.create eval_length in
let id2_evaluation = Evaluations.create eval_length in
let tmp1_evaluation = Evaluations.create eval_length in
let tmp2_evaluation =
Evaluations.find_evaluation evaluations "tmp_eval"
in
let eval_a_mul_bg =
Evaluations.mul ~res:tmp1_evaluation ~evaluations
~poly_names:[ left; right ]
~composition_gx:([ 0; 1 ], domain_size)
()
in
let eval_b_mul_ag =
Evaluations.mul ~res:tmp2_evaluation ~evaluations
~poly_names:[ left; right ]
~composition_gx:([ 1; 0 ], domain_size)
()
in
let eval_a_mul_b_mul_ag_mul_bg =
Evaluations.mul_c ~res:id1_evaluation
~evaluations:[ eval_a_mul_bg; eval_b_mul_ag ]
()
in
let eval_1_plus_d_mul_a_mul_b_mul_ag_mul_bg =
Evaluations.linear_c ~res:id2_evaluation
~evaluations:[ eval_a_mul_b_mul_ag_mul_bg ]
~linear_coeffs:[ d ] ~add_constant:one ()
in
let eval_a_mul_bg_plus_b_mul_ag =
Evaluations.linear_c ~res:id1_evaluation
~evaluations:[ eval_a_mul_bg; eval_b_mul_ag ]
()
in
let eval_c_mul_p_1_plus_d_mul_a_mul_b_mul_ag_mul_bg_p =
Evaluations.mul_c ~res:tmp1_evaluation
~evaluations:[ eval_1_plus_d_mul_a_mul_b_mul_ag_mul_bg; eval_c ]
()
in
let eval_first_identity =
Evaluations.linear_c ~res:tmp2_evaluation
~evaluations:
[
eval_c_mul_p_1_plus_d_mul_a_mul_b_mul_ag_mul_bg_p;
eval_a_mul_bg_plus_b_mul_ag;
]
~linear_coeffs:[ one; minus_one ] ()
in
let first_identity =
Evaluations.mul_c ~res:id1_evaluation
~evaluations:[ eval_q_label; eval_first_identity ]
()
in
let eval_1_minus_d_mul_a_mul_b_mul_ag_mul_bg =
Evaluations.linear_c ~res:tmp2_evaluation
~evaluations:[ eval_1_plus_d_mul_a_mul_b_mul_ag_mul_bg ]
~linear_coeffs:[ minus_one ] ~add_constant:(Scalar.add one one) ()
in
let eval_cg_mul_p_1_minus_d_mul_a_mul_b_mul_ag_mul_bg_p =
Evaluations.mul_c ~res:tmp1_evaluation
~evaluations:[ eval_1_minus_d_mul_a_mul_b_mul_ag_mul_bg; eval_c ]
~composition_gx:([ 0; 1 ], domain_size)
()
in
let eval_a_mul_b =
Evaluations.mul ~res:id2_evaluation ~evaluations
~poly_names:[ left; right ] ()
in
let eval_second_identity =
Evaluations.linear_c ~res:tmp2_evaluation
~evaluations:
[
eval_cg_mul_p_1_minus_d_mul_a_mul_b_mul_ag_mul_bg_p;
eval_a_mul_b;
eval_a_mul_b;
]
~composition_gx:([ 0; 1; 0 ], domain_size)
~linear_coeffs:[ one; minus_one; a ] ()
in
let second_identity =
Evaluations.mul_c ~res:id2_evaluation
~evaluations:[ eval_q_label; eval_second_identity ]
()
in
let precomputed_polys =
SMap.of_list
[
(q_label ^ ".0", first_identity); (q_label ^ ".1", second_identity);
]
in
PP.{ v_map = v_map ~prefix g; precomputed_polys }
let verifier_query ~prefix ~generator ~size_domain:_ =
let v_map = v_map ~prefix generator in
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree =
SMap.of_list [ (left, 6); (right, 6); (output, 6); (q_label, 6) ]
end
module Public_gate : Gate_base_sig = struct
let q_label = "qpub"
let identity = (arith, 1)
let equations ~q:_ ~a:_ ~b:_ ~c:_ ~ag:_ ~bg:_ ~cg:_ ?table:_ () =
Scalar.[ zero ]
let blinds = SMap.empty
let identities prefix =
SMap.singleton
(prefix ^ arith ^ ".0")
(MP.Polynomial.of_list [ (monomial_of_list [ prefix ^ "PI" ], one) ])
let v_map = SMap.empty
let compute_PI public_inputs domain evaluations =
let size_domain = Domain.length domain in
if size_domain = 0 then Evaluations.zero
else
let l = Array.length public_inputs in
let scalars =
Array.(
append public_inputs (init (size_domain - l) (fun _ -> Scalar.zero)))
in
let pi =
Poly.(opposite (Evaluations.interpolation_fft2 domain scalars))
in
let domain = Evaluations.get_domain evaluations in
Evaluations.evaluation_fft domain pi
let prover_query ~prefix:_ ~public_inputs ~domain ~evaluations =
let pi_poly = compute_PI public_inputs domain evaluations in
PP.{ v_map; precomputed_polys = SMap.singleton (arith ^ ".0") pi_poly }
let verifier_query ~prefix ~generator:_ ~size_domain:_ =
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree = SMap.empty
end
module X5_gate : Gate_base_sig = struct
let q_label = "qx5"
let identity = (arith, 1)
let equations ~q ~a ~b:_ ~c:_ ~ag:_ ~bg:_ ~cg:_ ?table:_ () =
Scalar.[ q * pow a (Z.of_int 5) ]
let blinds = SMap.singleton left [| 1; 0 |]
let identities prefix =
let q_poly =
MP.Polynomial.of_list [ (monomial_of_list [ prefix ^ q_label ], one) ]
in
let fl_poly =
MP.Polynomial.of_list [ (monomial_of_list [ prefix ^ left ], one) ]
in
let fl2_poly = MPoly.mul fl_poly fl_poly in
let fl4_poly = MPoly.mul fl2_poly fl2_poly in
let fl5_poly = MPoly.mul fl4_poly fl_poly in
let poly = MPoly.mul q_poly fl5_poly in
SMap.singleton (prefix ^ arith ^ ".0") poly
let v_map = SMap.empty
let prover_query ~prefix:_ ~public_inputs:_ ~domain:_ ~evaluations =
let res = Evaluations.find_evaluation evaluations "tmp_eval" in
let poly =
Evaluations.mul ~res ~evaluations ~poly_names:[ q_label; left ]
~powers:[ 1; 5 ] ()
in
let precomputed_polys = SMap.singleton (arith ^ ".0") poly in
PP.{ v_map; precomputed_polys }
let verifier_query ~prefix ~generator:_ ~size_domain:_ =
PP.{ v_map; identities = identities prefix; not_committed = SMap.empty }
let polynomials_degree = SMap.of_list [ (left, 6); (q_label, 6) ]
end
module Gate_aggregator : sig
val aggregate_blinds : module_list:(module Gate_base_sig) list -> int SMap.t
val aggregate_prover_queries :
?prefix:string ->
module_list:(module Gate_base_sig) list ->
public_inputs:Scalar.t array ->
domain:PP.PC.Polynomial.Domain.t ->
evaluations:Evaluations.t SMap.t ->
unit ->
PP.prover_query
val aggregate_verifier_queries :
?prefix:string ->
module_list:(module Gate_base_sig) list ->
generator:PP.PC.Scalar.t ->
size_domain:int ->
unit ->
PP.verifier_query
val aggregate_polynomials_degree :
module_list:(module Gate_base_sig) list -> int SMap.t
val add_public_inputs :
prefix:string ->
public_inputs:Scalar.t array ->
generator:PP.PC.Scalar.t ->
size_domain:int ->
PP.verifier_query ->
PP.verifier_query
end = struct
let get_blinds m =
let module M = (val m : Gate_base_sig) in
M.blinds
let get_identity m =
let module M = (val m : Gate_base_sig) in
M.identity
let get_prover_query m =
let module M = (val m : Gate_base_sig) in
M.prover_query
let get_verifier_query m =
let module M = (val m : Gate_base_sig) in
M.verifier_query
let get_polynomials_degree m =
let module M = (val m : Gate_base_sig) in
M.polynomials_degree
let empty_verifier_query =
PP.
{
v_map = SMap.empty;
identities = SMap.empty;
not_committed = SMap.empty;
}
let aggregate_blinds ~module_list =
let f_union _key a1 a2 =
if Array.length a1 <> Array.length a2 then
raise (Invalid_argument "All blinds arrays must have the same size.")
else Some Array.(init (length a1) (fun i -> max a1.(i) a2.(i)))
in
let blinds_array =
SMap.(
List.fold_left
(fun acc_blinds gate -> union f_union acc_blinds (get_blinds gate))
empty module_list)
in
let sum_array a = Array.fold_left ( + ) 0 a in
SMap.map sum_array blinds_array
let aggregate_prover_queries ?(prefix = "") ~module_list ~public_inputs
~domain ~evaluations () =
let size_eval = Evaluations.size_evaluations evaluations in
let tmp_evaluation = Evaluations.create size_eval in
let evaluations = SMap.add "tmp_eval" tmp_evaluation evaluations in
let precomputed_polys =
let is_arith =
List.exists
(fun gate ->
let name, size = get_identity gate in
name = arith && size = 1)
module_list
in
if is_arith then
let eval_length = Evaluations.size_evaluations evaluations in
let arith_acc_evaluation = Evaluations.create eval_length in
SMap.singleton (arith ^ ".0") arith_acc_evaluation
else SMap.empty
in
let empty_prover_query = PP.{ v_map = SMap.empty; precomputed_polys } in
List.fold_left
(fun accumulated_query gate ->
PP.sum_prover_queries accumulated_query
(get_prover_query gate ~prefix ~public_inputs ~domain ~evaluations))
empty_prover_query module_list
let aggregate_verifier_queries ?(prefix = "") ~module_list ~generator
~size_domain () =
List.fold_left
(fun accumulated_query gate ->
PP.sum_verifier_queries accumulated_query
((get_verifier_query gate) ~prefix ~generator ~size_domain))
empty_verifier_query module_list
let aggregate_polynomials_degree ~module_list =
List.fold_left
(fun accumulated_map gate ->
let map = get_polynomials_degree gate in
SMap.union
(fun _key value_1 value_2 -> Some (max value_1 value_2))
accumulated_map map)
SMap.empty module_list
let compute_PIx public_inputs generator n x_ni =
if n = 0 then Scalar.zero
else
let g = Scalar.inverse_exn generator in
let f (acc, gi) wi =
let deno = Scalar.((gi * x_ni.(0)) + negate one) in
(Scalar.(acc + (wi / deno)), Scalar.mul g gi)
in
let res, _ = Array.fold_left f Scalar.(zero, one) public_inputs in
let xn_min_1_div_n =
Scalar.((x_ni.(1) + negate one) / Fr_generation.fr_of_int_safe n)
in
Scalar.(negate (mul xn_min_1_div_n res))
type PP.not_committed +=
| ComputePublic of Evaluations.scalar array * Evaluations.scalar * int
let () =
let inner =
let open Encodings in
Data_encoding.(tup3 (array fr_encoding) fr_encoding int31)
in
let from = function
| ComputePublic (a, b, c) -> Some (a, b, c)
| _ -> None
in
let to' (a, b, c) = ComputePublic (a, b, c) in
PP.register_nc_eval_and_encoding
(function
| ComputePublic (public_inputs, generator, n) ->
Some (compute_PIx public_inputs generator n)
| _ -> None)
~title:"public" ~tag:0
Data_encoding.(obj1 (req "public" inner))
from to'
let add_public_inputs ~prefix ~public_inputs ~generator ~size_domain query =
PP.
{
query with
not_committed =
SMap.add_unique (prefix ^ "PI")
(ComputePublic (public_inputs, generator, size_domain))
query.not_committed;
}
end
end
module Custom_gate (PP : Polynomial_protocol.Polynomial_protocol_sig) =
Custom_gate_impl (PP)