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
module SMap = Plonk.SMap
open Plonk.Bls
let nb_wires = Plompiler.Csir.nb_wires_arch
module V (Main : Aggregation.Main_protocol.S) = struct
module Gates = Main.Gates
module Perm = Main.Perm
module RC = Main.RangeCheck
module S = Plompiler.S
open Plompiler.LibCircuit
type scalar_input = scalar Input.input
type circuit_inputs = {
switches : bool Input.input list;
compressed_switches : scalar_input;
alpha : scalar_input;
beta : scalar_input;
gamma : scalar_input;
delta : scalar_input;
x : scalar_input;
r : scalar_input;
ss_list : scalar_input list;
selectors : (string * scalar_input) list;
ids_batch : scalar_input;
wires_g : scalar_input list list;
wires : scalar_input list list;
zg : scalar_input;
z : scalar_input;
batch : scalar_input list;
inner_pi : scalar_input list list;
outer_pi : scalar_input list;
rc_selectors : scalar_input list;
zg_rc_perm : scalar_input list;
z_rc_perm : scalar_input list;
}
let nb_batches_default = 4
let nb_batches circuit =
let gates =
Plonk.Circuit.get_selectors circuit
|> List.map (fun g -> (g, ()))
|> SMap.of_list
in
let nb_rc = SMap.cardinal circuit.range_checks in
if Gates.exists_gx_composition ~gates || nb_rc <> 0 then
nb_batches_default + 1
else nb_batches_default
let dummy_input range_checks gates nb_batches nb_proofs nb_inner_pi
nb_outer_pi =
let nb_rc = SMap.cardinal range_checks in
let switches = List.init nb_proofs (fun _ -> Input.bool true) in
let dummy_input = Input.scalar S.one in
let wires =
List.init nb_proofs (fun _ ->
List.init (nb_wires + nb_rc) (fun _ -> dummy_input))
in
let inner_pi =
List.init nb_proofs (fun _ ->
List.init nb_inner_pi (fun _ -> dummy_input))
in
let outer_pi = List.init nb_outer_pi (fun _ -> dummy_input) in
let selectors = List.map (fun q -> (q, dummy_input)) gates in
let ss_list =
List.init Plompiler.Csir.nb_wires_arch @@ Fun.const dummy_input
in
let batch = List.init nb_batches (fun _ -> dummy_input) in
let wires_g = if nb_batches = nb_batches_default then [] else wires in
let z_rc_perm, rc_selectors =
if SMap.is_empty range_checks then ([], [])
else
( List.init nb_rc (Fun.const dummy_input),
List.init (4 * nb_rc) (Fun.const dummy_input) )
in
{
switches;
compressed_switches = dummy_input;
alpha = dummy_input;
beta = dummy_input;
gamma = dummy_input;
delta = dummy_input;
x = dummy_input;
r = dummy_input;
ss_list;
selectors;
ids_batch = dummy_input;
wires_g;
wires;
zg = dummy_input;
z = dummy_input;
batch;
inner_pi;
outer_pi;
rc_selectors;
zg_rc_perm = z_rc_perm;
z_rc_perm;
}
module Constraints = struct
let switch_list = map2M (fun s l -> mapM (Num.mul s) l)
let switch switches ~wires ~wires_g ~inner_pi =
let switches = List.map scalar_of_bool switches in
let* wires = switch_list switches wires in
let* wires_g =
match wires_g with
| [] -> ret []
| wires_g -> switch_list switches wires_g
in
let* inner_pi = switch_list switches inner_pi in
ret (wires, wires_g, inner_pi)
let format_arith_cs : scalar repr list t -> scalar repr t =
fun scalar_repr_list_t ->
let* scalar_repr_list = scalar_repr_list_t in
match scalar_repr_list with
| [cs] -> ret cs
| _ -> raise (Invalid_argument "Invalid format for Arith constraint.")
let cs_of_arith_sel name q wires wires_g =
let wires = Array.of_list wires in
let wires_g = Array.of_list wires_g in
format_arith_cs (Gates.get_cs name ~q ~wires ~wires_g ())
let cs_of_custom_sel ?precomputed_advice name q wires wires_g =
let wires = Array.of_list wires in
let wires_g = Array.of_list wires_g in
Gates.get_cs name ~q ~wires ~wires_g ?precomputed_advice ()
let compute_zs x n =
with_label ~label:"zs"
@@
let nb_bits = S.size_in_bytes in
let* n_repr = Num.constant n in
let* n_bytes = bits_of_scalar ~nb_bits n_repr in
let* xn = Num.pow x (of_list n_bytes) in
Num.add_constant S.(negate one) xn
let compute_l1 x xn_minus_one n generator =
let* den = Num.add_constant S.(negate generator) x in
Num.div ~den_coeff:(S.div_exn n generator) xn_minus_one den
let sum_alpha_i list_circuit alpha =
let list_circuit = List.rev list_circuit in
match list_circuit with
| [] -> Num.zero
| init :: list_circuit ->
foldM
(fun acc circuit ->
let* tmp = Num.mul acc alpha in
Num.add tmp circuit)
init
list_circuit
let sum_alpha_i_switched switches list_circuit alpha =
let* zero = Num.zero in
let* alpha_min_one = Num.add ~qc:S.(negate one) alpha zero in
let nb_proofs = List.length switches in
let list_circuit = List.rev list_circuit in
let switches = List.rev switches in
match list_circuit with
| [] -> Num.zero
| list_circuit ->
let* res, _ =
fold2M
(fun (acc, i) coeff switch ->
let switch = scalar_of_bool switch in
let* coeff = Num.mul switch coeff in
let* alpha =
Num.custom ~qm:S.one ~qc:S.one switch alpha_min_one
in
let* tmp = Num.add acc coeff in
let* res =
if i = nb_proofs then ret tmp else Num.mul tmp alpha
in
ret (res, i + 1))
(zero, 1)
list_circuit
switches
in
ret res
let compute_t xn t_list = sum_alpha_i t_list xn
let add_circuits circuit_list =
let* circuit_list = mapM Fun.id circuit_list in
Num.add_list (to_list circuit_list)
let check_switches compressed_switches switches =
let switches = List.map scalar_of_bool switches in
let* sum = Num.add_list (to_list switches) in
let switches_pairs, _ =
List.fold_left
(fun (pairs, s_prev) s -> ((s_prev, s) :: pairs, s))
([], List.hd switches)
(List.tl switches)
in
iterM
(fun (s_prev, s) ->
Num.assert_custom ~qm:S.one ~qr:S.(negate one) s_prev s s)
switches_pairs
>* equal compressed_switches sum
let check_batch r (g_list, wires, wiresg, z_list, zg_list) batch =
let wires = List.flatten wires in
let wiresg = List.flatten wiresg in
let* batch_g = sum_alpha_i g_list r in
let* batch_wires = sum_alpha_i wires r in
let* batch_z = sum_alpha_i z_list r in
let* batch_zg = sum_alpha_i zg_list r in
let g_exp = List.nth batch 0 in
let zg_exp = List.nth batch 1 in
let z_exp = List.nth batch 2 in
let* g = equal batch_g g_exp in
let* zg = equal batch_zg zg_exp in
let* z = equal batch_z z_exp in
match wiresg with
| [] ->
let wires_exp = List.nth batch 3 in
let* wires = equal batch_wires wires_exp in
Bool.band_list [g; wires; zg; z]
| wg ->
let* batch_wires_g = sum_alpha_i wg r in
let wiresg_exp = List.nth batch 3 in
let wires_exp = List.nth batch 4 in
let* wiresg = equal batch_wires_g wiresg_exp in
let* wires = equal batch_wires wires_exp in
Bool.band_list [g; wiresg; wires; zg; z]
let format_custom_ids custom_ids =
let suffix_ids l =
List.map
(fun (s, l) ->
List.mapi (fun i x -> (s ^ "." ^ string_of_int i, x)) l)
l
|> List.flatten
in
let index_proofs l =
let n = List.length l in
List.mapi
(fun i inner_l ->
List.map
(fun (s, x) -> (SMap.Aggregation.add_prefix ~n ~i "" s, x))
inner_l)
l
in
index_proofs (List.map suffix_ids custom_ids) |> List.flatten
let format_arith_ids arith_ids =
let arith = Plonk.Custom_gates.arith_label in
let n = List.length arith_ids in
List.mapi
(fun i x -> (SMap.Aggregation.add_prefix ~n ~i "" arith, x))
arith_ids
let split_rc_wires nb_rc wires =
if nb_rc = 0 then ([], wires)
else
let rec aux acc_rc acc_w = function
| [] -> List.(rev acc_rc, rev acc_w)
| rc_w :: tl ->
let rc, w = Plonk.List.split_n nb_rc rc_w in
aux (rc :: acc_rc) (w :: acc_w) tl
in
aux [] [] wires
let check_identities ~switches (n, generator) x rc_wires ids_batch
rc_selectors (q_names, selectors) (alpha, beta, gamma, delta)
(wires_g, wires, zg, z, zg_rc_perm, z_rc_perm) ss_list pi_list_list =
let nb_rc = List.length rc_wires in
let wires_g = match wires_g with [] -> wires | w -> w in
let zg_rc, wires_g = split_rc_wires nb_rc wires_g in
let z_rc, wires = split_rc_wires nb_rc wires in
let* t = Bool.constant true in
let* zs = compute_zs x n in
let* l1 = compute_l1 x zs n generator in
let is_advice_sel s = String.starts_with ~prefix:Gates.qadv_label s in
let arith_selectors, custom_selectors =
List.combine q_names selectors
|> List.partition (fun (q_name, _) ->
if is_advice_sel q_name then false
else
let id_name, nb_id = Gates.get_ids q_name in
if id_name = Gates.arith_label then
if nb_id <> 1 then
failwith "partition_selector : invalid Arith identity."
else true
else false)
in
let advice_selectors, custom_selectors =
List.partition (fun (s, _) -> is_advice_sel s) custom_selectors
in
let precomputed_advice = SMap.of_list advice_selectors in
let* custom_ids =
map2M
(fun wires wires_g ->
mapM
(fun (name, q) ->
let* id_values =
cs_of_custom_sel ~precomputed_advice name q wires wires_g
in
ret (name, id_values))
custom_selectors)
wires
wires_g
in
let* arith_list =
let monomials =
List.map
(fun (name, q) -> List.map2 (cs_of_arith_sel name q) wires wires_g)
arith_selectors
in
let pi_list =
List.map (Gates.cs_pi ~generator ~n ~x ~zs) pi_list_list
in
Plonk.List.mapn add_circuits (pi_list :: monomials) |> mapM Fun.id
in
let* aggregated_wires =
Plonk.List.mapn (fun i -> sum_alpha_i i delta) wires |> mapM Fun.id
in
let* perm_ids =
(Perm.cs ~l1 ~ss_list ~beta ~gamma ~x ~z ~zg) ~aggregated_wires ()
in
let* rc_ids =
match rc_wires with
| [] -> ret []
| _ ->
let nb_proofs = List.length wires in
let lnin1 = Plonk.List.sub rc_selectors 0 nb_rc in
let pnin1 = Plonk.List.sub rc_selectors nb_rc nb_rc in
let ss_list =
List.init nb_rc (fun i ->
Plonk.List.sub rc_selectors (nb_rc * (2 + i)) 2)
in
RC.cs
~rc_index:rc_wires
~nb_proofs
~lnin1
~pnin1
~z_rc
~zg_rc
~z_perm:z_rc_perm
~zg_perm:zg_rc_perm
~aggregated_wires
~sum_alpha_i
~l1
~ss_list
~beta
~gamma
~delta
~x
in
let identities =
format_arith_ids arith_list
@ format_custom_ids custom_ids
@ perm_ids @ rc_ids
|> List.sort (fun (s, _) (s', _) -> String.compare s s')
|> List.map snd
in
let id_switches =
let for_each_proof =
let nb_ids_per_proof =
let nb_custom_ids =
List.(
hd custom_ids |> fold_left (fun acc l -> acc + length (snd l)) 0)
in
match rc_ids with
| [] -> nb_custom_ids + 1
| _ -> nb_custom_ids + 1 + (2 * nb_rc)
in
List.concat_map
(fun b -> List.init nb_ids_per_proof (Fun.const b))
switches
in
let for_whole_circuit =
match rc_ids with
| [] -> [t; t]
| _ -> [t; t] @ List.init (2 * nb_rc) (Fun.const t)
in
for_each_proof @ for_whole_circuit
in
let* sum_id = sum_alpha_i_switched id_switches identities alpha in
equal sum_id ids_batch
end
let verify_batch r batch batches t_answers =
let init_sum =
List.map (SMap.map @@ Fun.const Scalar.zero) (List.tl batch)
in
let init_sizes = List.map (SMap.map @@ Fun.const 0) (List.tl batch) in
let sum_batches, _ =
SMap.fold
(fun _circuit_name this_batch (acc_sum, acc_sizes) ->
let values = List.map (SMap.map fst) this_batch in
let sizes_list = List.map (SMap.map snd) this_batch in
let acc_sum =
List.map2
(fun map_sum (map_this_batch, map_sizes) ->
SMap.mapi
(fun key acc_value ->
let size = SMap.find key map_sizes |> Z.of_int in
let this_value = SMap.find key map_this_batch in
Scalar.(acc_value + (this_value * pow r size)))
map_sum)
acc_sum
(List.combine values acc_sizes)
in
let acc_sizes =
List.map2
(fun map_acc_sizes map_sizes ->
SMap.mapi
(fun key size -> size + SMap.find key map_sizes)
map_acc_sizes)
acc_sizes
sizes_list
in
(acc_sum, acc_sizes))
batches
(init_sum, init_sizes)
in
let t_batch =
List.fold_left
(fun (acc, rk) x -> Scalar.(acc + (rk * x), r * rk))
(Scalar.zero, Scalar.one)
t_answers
|> fst
in
let sum_batches =
let hd = List.hd batch in
assert (SMap.cardinal hd = 1) ;
let t_key, _t_value = SMap.choose hd in
SMap.singleton t_key t_batch :: sum_batches
in
let given = List.concat_map SMap.values batch in
let computed = List.concat_map SMap.values sum_batches in
List.for_all2 Scalar.( = ) given computed
let aggreg_public_inputs pi_size (alpha, beta, gamma, delta, x, r) batch
ids_batch compressed_switches outer_pi =
let batch = List.concat_map SMap.values batch |> List.map fst in
let public_input =
Array.of_list
([alpha; beta; gamma; delta; x; r]
@ batch
@ [ids_batch; compressed_switches]
@ outer_pi)
in
let l = Array.length public_input in
if l <> pi_size then
failwith
(Printf.sprintf
"Public input has not expected size (expected: %d; actual: %d)."
pi_size
l) ;
public_input
let compute_switches max_nb_proofs nb_proofs =
let switches =
Array.init max_nb_proofs S.(fun i -> if i < nb_proofs then one else zero)
in
(switches, S.of_int nb_proofs)
let pad_inputs nb_max_proofs nb_rc_wires inner_pi answers =
let nb_proofs = List.length inner_pi in
let padded_inner_pi =
let to_pad = nb_max_proofs - nb_proofs in
let nb_inner_pi = List.(length (hd inner_pi)) in
List.flatten inner_pi
@ List.(init (to_pad * nb_inner_pi) (Fun.const S.zero))
in
let padded_answers =
Plonk.Utils.pad_answers nb_max_proofs nb_rc_wires nb_proofs answers
in
(padded_inner_pi, padded_answers)
let get_witness max_nb_proofs nb_rc_wires (p : Main.prover_aux) circuit_name
pi_size solver (inner_pi, outer_pi) switches compressed_switches batch =
let ids_batch = SMap.find circuit_name p.ids_batch |> fst in
let public =
aggreg_public_inputs
pi_size
(p.alpha, p.beta, p.gamma, p.delta, p.x, p.r)
batch
ids_batch
compressed_switches
outer_pi
in
let circuit_answers =
List.map
(SMap.Aggregation.select_answers_by_circuit circuit_name)
(List.tl p.answers)
in
let inputs =
let inner_pi, answers =
pad_inputs max_nb_proofs nb_rc_wires inner_pi circuit_answers
in
Array.(concat [of_list (inner_pi @ answers); public; switches])
in
try Plompiler.Solver.solve solver inputs
with e ->
print_string "\nSolver failure\n" ;
raise e
let get_batches inputs answers r =
let batch_map r map = Plonk.Utils.Fr_generation.batch r (SMap.values map) in
SMap.mapi
(fun circuit_name _ ->
let answers =
List.map
(SMap.Aggregation.select_answers_by_circuit circuit_name)
(List.tl answers)
in
List.map SMap.(map (fun m -> (batch_map r m, cardinal m))) answers)
inputs
let verification_circuit (generator, n) rc_wires check_pi
{
switches;
compressed_switches;
alpha;
beta;
gamma;
delta;
x;
r;
ss_list;
selectors;
ids_batch;
wires_g;
wires;
zg;
z;
batch;
outer_pi;
inner_pi;
rc_selectors;
zg_rc_perm;
z_rc_perm;
} =
let n = S.of_int n in
let q_names, selectors = List.split selectors in
let* inner_pi =
begin_input_com (fun inner_pi -> List.map of_list (of_list inner_pi))
|: Input.list (List.map Input.list inner_pi)
|> end_input_com
in
let* ( rc_selectors,
ss_list,
selectors,
zg_rc_perm,
zg,
z_rc_perm,
z,
wires_g,
wires ) =
begin_input_com
(fun
rc_selectors
ss_list
selectors
zg_rc_perm
zg
z_rc_perm
z
wires_g
wires
->
( of_list rc_selectors,
ss_list,
of_list selectors,
of_list zg_rc_perm,
zg,
of_list z_rc_perm,
z,
List.map of_list (of_list wires_g),
List.map of_list (of_list wires) ))
|: Input.list rc_selectors |: Input.list ss_list |: Input.list selectors
|: Input.list zg_rc_perm |: zg |: Input.list z_rc_perm |: z
|: Input.list (List.map Input.list wires_g)
|: Input.list (List.map Input.list wires)
|> end_input_com
in
let* alpha = input ~kind:`Public alpha in
let* beta = input ~kind:`Public beta in
let* gamma = input ~kind:`Public gamma in
let* delta = input ~kind:`Public delta in
let* x = input ~kind:`Public x in
let* r = input ~kind:`Public r in
let* batch = mapM (input ~kind:`Public) batch in
let* ids_batch = input ~kind:`Public ids_batch in
let* compressed_switches = input ~kind:`Public compressed_switches in
let* outer_pi = mapM (input ~kind:`Public) outer_pi in
let* switches = mapM input switches in
let* switched_wires, switched_wires_g, switched_inner_pi =
Constraints.switch switches ~wires ~wires_g ~inner_pi
in
let ss_list = of_list ss_list in
let* check_switches =
with_label ~label:"check_switches"
@@ Constraints.check_switches compressed_switches switches
in
let* check_pi =
with_label ~label:"check_pi"
@@ check_pi ~switches ~outer:outer_pi ~inner:inner_pi
in
let* check_identities =
with_label ~label:"check_identities"
@@ Constraints.check_identities
~switches
(n, generator)
x
rc_wires
ids_batch
rc_selectors
(q_names, selectors)
(alpha, beta, gamma, delta)
(switched_wires_g, switched_wires, zg, z, zg_rc_perm, z_rc_perm)
ss_list
switched_inner_pi
in
let* check_batch =
with_label ~label:"check_batch"
@@
let g_list = rc_selectors @ ss_list @ selectors in
let z_list = z_rc_perm @ [z] in
let zg_list = zg_rc_perm @ [zg] in
Constraints.check_batch
r
(g_list, switched_wires, switched_wires_g, z_list, zg_list)
batch
in
let* res =
with_label ~label:"check_aplonk_res"
@@ Bool.band_list
[check_switches; check_batch; check_identities; check_pi]
in
Bool.assert_true res
let get_cs_verification pp circuit nb_batches nb_proofs
(nb_outer_pi, nb_inner_pi) check_pi =
let gen, n = Main.get_gen_n_prover pp in
let gates = Plonk.Circuit.get_selectors circuit in
let dummy_input =
dummy_input
circuit.range_checks
gates
nb_batches
nb_proofs
nb_inner_pi
nb_outer_pi
in
let rc_wires =
SMap.(keys circuit.range_checks)
|> List.map Plompiler.Csir.int_of_wire_name
in
Plompiler.LibCircuit.get_cs
(verification_circuit (gen, n) rc_wires check_pi dummy_input)
end