Source file cases.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
open Plonk
module Scalar = Bls.Scalar
let ( ! ) = List.map Scalar.of_int
let ( !! ) l = List.map Scalar.of_int l |> Array.of_list
type outcome = Valid | Proof_error | Lookup_error
type case = {
name : string;
circuit : Circuit.t;
witness : Scalar.t array;
outcome : outcome;
}
let aggregate_cases ?(prefix = "") cases =
let outcome = (List.hd cases).outcome in
let name, circuits_map, inputs_map, outcome =
List.fold_left
(fun (name, circuits_map, inputs_map, outcome) case ->
assert (outcome = case.outcome) ;
let circuit = case.circuit in
let other_inputs =
Option.value ~default:[] @@ SMap.find_opt case.name inputs_map
in
let inputs = case.witness :: other_inputs in
SMap.
( (if name = "" then case.name else name ^ "+" ^ case.name),
add case.name (circuit, List.length inputs) circuits_map,
add case.name inputs inputs_map,
outcome ))
SMap.(prefix, empty, empty, outcome)
cases
in
let inputs_map = SMap.map List.rev inputs_map in
(name, circuits_map, inputs_map, outcome)
module Unit_tests_for_each_selector = struct
let qc =
let name = "qc" in
let witness = !![0; 1] in
let circuit =
let wires =
let a = [0; 0] in
let b = [0; 0] in
let c = [0; 1] in
[|a; b; c|]
in
let qo = ![0; -1] in
let gates = Circuit.make_gates ~linear:[(2, qo)] ~qc:![0; 1] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let linear_selector_test i =
assert (i <> 2) ;
let name = Plompiler.Csir.linear_selector_name i in
let witness = !![0; 1] in
let circuit =
let wires =
Array.init Plompiler.Csir.nb_wires_arch (fun j ->
if j = i || j = 2 then [0; 1] else [0; 0])
in
let q_linear = ![1; 1] in
let qo = ![0; -1] in
let linear = [(i, q_linear); (2, qo)] in
let gates = Circuit.make_gates ~linear () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let next_linear_selector_test i =
let name =
Plompiler.Csir.(linear_selector_name i |> add_next_wire_suffix)
in
let witness = !![0; 1] in
let circuit =
let wires =
Array.init Plompiler.Csir.nb_wires_arch (fun j ->
if j = 2 then if i = 2 then [1; 1] else [1; 0]
else if j = i then [0; 1]
else [0; 0])
in
let gates =
Circuit.make_gates ~linear:[(2, ![-1; 0])] ~linear_g:[(i, ![1; 0])] ()
in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qm =
let name = "qm" in
let witness = !![0; 1] in
let circuit =
let wires =
let a = [0; 1] in
let b = [1; 1] in
let c = [0; 1] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qm:![1; 1] ~linear:[(2, ![0; -1])] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qx2b =
let name = "qx2b" in
let witness = !![0; -3; 3; 9] in
let circuit =
let wires =
let a = [0; 0; 0] in
let b = [0; 1; 2] in
let c = [0; 3; 3] in
[|a; b; c|]
in
let gates =
Circuit.make_gates ~qx2b:![1; 1; 1] ~linear:[(2, ![0; -1; -1])] ()
in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qx5a =
let name = "qx5a" in
let witness = !![0; 2; 32] in
let circuit =
let wires =
let a = [0; 1] in
let b = [0; 0] in
let c = [0; 2] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qx5a:![1; 1] ~linear:[(2, ![0; -1])] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qx5c =
let name = "qx5c" in
let witness = !![0; 243; 3] in
let circuit =
let wires =
let a = [0; 1] in
let b = [0; 0] in
let c = [0; 2] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qx5c:![1; 1] ~linear:[(0, ![0; -1])] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qecc_ws_add =
let name = "qecc_ws_add" in
let witness = !![1; 2; 3; 4] in
let circuit =
let wires =
let a = [2; 0] in
let b = [3; 3] in
let c = [1; 1] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qecc_ws_add:![1; 0] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qecc_ed_add =
let name = "qecc_ed_add" in
let witness = !![0; 1] in
let circuit =
let wires =
let a = [0; 1] in
let b = [0; 1] in
let c = [0; 1] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qecc_ed_add:![1; 0] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qecc_ed_cond_add =
let name = "qecc_cond_ed_add" in
let witness = !![0; 1; 2; 3] in
let circuit =
let wires =
let a = [0; 0; 1; 0] in
let b = [2; 0; 0; 0] in
let c = [3; 0; 1; 0] in
let d = [1; 1; 0; 0] in
let e = [2; 2; 1; 1] in
[|a; b; c; d; e|]
in
let gates = Circuit.make_gates ~qecc_ed_cond_add:![1; 0; 1; 0] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qbool =
let name = "qbool" in
let witness = !![0; 1] in
let circuit =
let wires =
let a = [0; 1] in
let b = [0; 0] in
let c = [0; 0] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~qbool:![1; 1] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qcond_swap =
let name = "qcond_swap" in
let witness = !![0; 1; 2] in
let circuit =
let wires =
let a = [0; 1] in
let b = [1; 1] in
let c = [2; 2] in
let d = [1; 2] in
let e = [2; 1] in
[|a; b; c; d; e|]
in
let gates = Circuit.make_gates ~qcond_swap:![1; 1] () in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let q_anemoi =
let name = "q_anemoi" in
let x0 = "1" in
let y0 = "1" in
let x1 =
"39027417478195954763090966714903261667484379123570980405377627458504913299628"
in
let y1 =
"12807118572207854608879870979725669075358379219019309928321803168748017551407"
in
let x2 =
"16347289160862248212893857406118677211322091184422502665988411845410114556281"
in
let y2 =
"7279269625797671375403766084307099687590046281141194129086784189878517048332"
in
let kx1 = Scalar.of_string "39" in
let ky1 =
Scalar.of_string
"14981678621464625851270783002338847382197300714436467949315331057125308909900"
in
let kx2 =
Scalar.of_string
"41362478282768062297187132445775312675360473883834860695283235286481594490621"
in
let ky2 =
Scalar.of_string
"28253420209785428420233456008091632509255652343634529984400816700490470131093"
in
let witness = Array.map Scalar.of_string [|"0"; x0; y0; x1; y1; x2; y2|] in
let circuit =
let wires =
let a = [0; 0; 0; 0] in
let b = [0; 0; 3; 0] in
let c = [0; 0; 4; 0] in
let d = [0; 0; 1; 5] in
let e = [0; 0; 2; 6] in
[|a; b; c; d; e|]
in
let precomputed_advice =
SMap.of_list
[
("qadv0", Scalar.[zero; zero; kx1; zero]);
("qadv1", Scalar.[zero; zero; ky1; zero]);
("qadv2", Scalar.[zero; zero; kx2; zero]);
("qadv3", Scalar.[zero; zero; ky2; zero]);
]
in
let gates =
Circuit.make_gates
~q_anemoi:Scalar.[zero; zero; one; zero]
~linear:[(0, ![1; 1; 0; 0]); (1, ![1; 0; 0; 0])]
~precomputed_advice
()
in
Circuit.make ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let list =
let wires = List.init Plompiler.Csir.nb_wires_arch Fun.id in
(qc :: List.map linear_selector_test (List.filter (fun i -> i <> 2) wires))
@ List.map next_linear_selector_test wires
@ [
qm;
qx2b;
qx5a;
qx5c;
qecc_ws_add;
qecc_ed_add;
qecc_ed_cond_add;
qbool;
qcond_swap;
q_anemoi;
]
end
module General_circuits = struct
let bnot =
let name = "bnot" in
let witness = !![0; 1] in
let circuit =
let wires =
let a = [0; 0] in
let b = [0; 0] in
let c = [1; 0] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~linear:[(2, ![-1; -1])] ~qc:![1; 0] () in
Circuit.make ~wires ~gates ~public_input_size:1 ()
in
{name; circuit; witness; outcome = Valid}
let list = [bnot]
end
module General = struct
let wires =
let a = [0; 1; 2; 3; 4; 2; 0] in
let b = [9; 8; 7; 6; 5; 3; 3] in
let c = [10; 9; 8; 7; 6; 1; 1] in
[|a; b; c|]
let gates =
Circuit.make_gates
~linear:
[
(0, ![1; 0; 1; 0; 1; 0; 0]);
(1, ![1; 0; 1; 0; 1; 0; 0]);
(2, ![-1; -1; -1; -1; -1; 0; 0]);
]
~qm:![0; 1; 0; 1; 0; 0; 0]
~qecc_ws_add:![0; 0; 0; 0; 0; 1; 0]
()
let circuit = Circuit.make ~wires ~gates ~public_input_size:1 ()
let witness = !![1; 2; 3; 4; 5; 6; 11; 44; 47; 94; 95]
let zero_values =
let name = "zero_values" in
let witness = Array.make 11 (Scalar.of_int 0) in
{name; circuit; witness; outcome = Valid}
let non_zero_values =
let name = "non_zero_values" in
{name; circuit; witness; outcome = Valid}
let no_public_inputs =
let name = "no_public_inputs" in
let circuit = Circuit.make ~wires ~gates ~public_input_size:0 () in
{name; circuit; witness; outcome = Valid}
let wrong_values =
let name = "wrong_values" in
let witness =
!![1; 2; 3; 4; 5; 6; 11; 44; 47; 94; 94]
in
{name; circuit; witness; outcome = Proof_error}
let input_com =
let name = "input_commitment" in
let circuit =
Circuit.make ~wires ~gates ~public_input_size:0 ~input_com_sizes:[3; 1] ()
in
{name; circuit; witness; outcome = Valid}
let list =
[zero_values; non_zero_values; no_public_inputs; wrong_values; input_com]
let list_one_public_input = [zero_values; non_zero_values; wrong_values]
end
module Big_circuit = struct
let make ~nb_proofs ~public_input_size ~k =
let name = Format.sprintf "big_circuit.%i.%i" public_input_size k in
let len_fst_layer = 1 lsl (k - 1) in
let e_d =
Array.init len_fst_layer (fun _ -> Scalar.[|random (); random ()|])
in
let witness nb_proofs k qc =
let rec build_w (acc_w_left, acc_w_right) =
let l = List.hd acc_w_left in
let r = List.hd acc_w_right in
let len_l = Array.length l in
assert (Array.(len_l = length r)) ;
let op =
if List.length acc_w_left mod 2 = 0 then Scalar.mul else Scalar.add
in
let add_qc i =
let i_constraint =
let lens =
List.(fold_left (fun i l -> i + Array.length l))
0
(acc_w_left @ acc_w_right)
in
lens - (1 lsl k) + i
in
Scalar.(add qc.(i_constraint))
in
if len_l = 1 then
let last = add_qc 0 (op l.(0) r.(0)) in
Array.(
concat
List.(rev acc_w_left @ rev acc_w_right @ [[|last|]] @ to_list e_d))
else if len_l = len_fst_layer then
let wl =
Array.init (len_l / 2) (fun i ->
let j = 2 * i in
add_qc j Scalar.(l.(j) + r.(j) + e_d.(j).(0) + e_d.(j).(1)))
in
let wr =
Array.init (len_l / 2) (fun i ->
let j = (2 * i) + 1 in
add_qc j Scalar.(l.(j) + r.(j) + e_d.(j).(0) + e_d.(j).(1)))
in
build_w (wl :: acc_w_left, wr :: acc_w_right)
else
let wl =
Array.init (len_l / 2) (fun i ->
add_qc (2 * i) (op l.(2 * i) r.(2 * i)))
in
let wr =
Array.init (len_l / 2) (fun i ->
add_qc ((2 * i) + 1) (op l.((2 * i) + 1) r.((2 * i) + 1)))
in
build_w (wl :: acc_w_left, wr :: acc_w_right)
in
let open Stdlib in
let w0 = ref (Scalar.random ()) in
List.init nb_proofs (fun _ ->
let w =
let w_left =
Array.init len_fst_layer (fun i ->
if i = 0 then !w0 else Scalar.random ())
in
let w_right =
Array.init len_fst_layer (fun _ -> Scalar.random ())
in
build_w ([w_left], [w_right])
in
w0 := w.(1) ;
w)
in
let n = (1 lsl k) - 1 in
let qc = List.init n (fun _ -> Scalar.random ()) in
let circuit =
let wires =
let last_c = 2 * n in
let a = List.init n Fun.id in
let b = List.init n (fun i -> n + i) in
let c =
List.init (n / 2) (fun i ->
let i = (1 lsl (k - 1)) + i in
[i; n + i])
@ [[last_c]]
|> List.concat
in
let d =
List.init n (fun i ->
if i < len_fst_layer then last_c + ((2 * i) + 1) else 0)
in
let e =
List.mapi (fun i x -> if i < len_fst_layer then x + 1 else 0) d
in
[|a; b; c; d; e|]
in
let gates =
let is_add k n i =
let dist = k - Z.(log2 (of_int (Int.sub n i))) in
if dist mod 2 = 0 then false else true
in
let qm = List.init n (fun i -> if is_add k n i then 0 else 1) in
let ql = List.init n (fun i -> if is_add k n i then 1 else 0) in
let qr = ql in
let qo = List.init n (fun _ -> -1) in
let qd = List.init n (fun i -> if i < 1 lsl (k - 1) then 1 else 0) in
let qe = qd in
Circuit.Circuit.make_gates
~qm:!qm
~linear:[(0, !ql); (1, !qr); (2, !qo); (3, !qd); (4, !qe)]
~qc
()
in
Circuit.make ~wires ~gates ~public_input_size ()
in
let witnesses = witness nb_proofs k (Array.of_list qc) in
List.map
(fun witness -> {name; circuit; witness; outcome = Valid})
witnesses
let list = make ~nb_proofs:2 ~public_input_size:2 ~k:5
let list_slow = make ~nb_proofs:2 ~public_input_size:2 ~k:16
end
let list =
Unit_tests_for_each_selector.list @ General_circuits.list @ General.list
@ Big_circuit.list
let list_slow = Big_circuit.list_slow
module Lookup = struct
let table_add_mod_m m =
let m2 = m * m in
let t_1 = List.init m2 (fun i -> i / m) in
let t_2 = List.init m2 (fun i -> i mod m) in
let t_3 = List.init m2 (fun i -> ((i / m) + i) mod m) in
[!!t_1; !!t_2; !!t_3]
let table_add_mod_5 = table_add_mod_m 5
let table_add_mod_10 = table_add_mod_m 10
let qplookup =
let name = "qplookup" in
let witness = !![0; 1; 2] in
let circuit =
let a = [1] in
let b = [1] in
let c = [2] in
let wires = [|a; b; c|] in
let gates = Circuit.make_gates ~q_plookup:![1] ~q_table:![0] () in
let tables = [table_add_mod_5] in
Circuit.make ~tables ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let qplookup_two_tables =
let name = "qplookup_two_tables" in
let witness = !![0; 1; 3; 9] in
let circuit =
let wires =
let a = [2; 3] in
let b = [2; 1] in
let c = [1; 0] in
[|a; b; c|]
in
let gates = Circuit.make_gates ~q_plookup:![1; 1] ~q_table:![0; 1] () in
let tables = [table_add_mod_5; table_add_mod_10] in
Circuit.make ~tables ~wires ~gates ~public_input_size:0 ()
in
{name; circuit; witness; outcome = Valid}
let wires =
let a = [1; 1; 4; 2; 4; 3; 1] in
let b = [2; 1; 2; 2; 3; 4; 4] in
let c = [3; 1; 1; 4; 2; 2; 2] in
[|a; b; c|]
let gates =
Circuit.make_gates
~linear:[(2, ![0; -1; 0; -1; 0; 0; 0])]
~qm:![0; 1; 0; 1; 0; 0; 0]
~qecc_ws_add:![0; 0; 0; 0; 0; 1; 0]
~q_plookup:![1; 0; 1; 0; 1; 0; 0]
~q_table:![0; 0; 0; 0; 0; 0; 0]
()
let tables = [table_add_mod_5]
let circuit =
Circuit.make ~tables ~wires ~gates ~public_input_size:1 ()
let witness = !![0; 1; 2; 3; 4]
let lookup_zero_values =
let name = "lookup_zero_values" in
let witness = !!(List.init 5 (fun _i -> 0)) in
{name; circuit; witness; outcome = Valid}
let lookup_non_zero_values =
let name = "lookup_non_zero_values" in
{name; circuit; witness; outcome = Valid}
let lookup_no_public_inputs =
let name = "lookup_no_public_inputs" in
let circuit = Circuit.make ~tables ~wires ~gates ~public_input_size:0 () in
{name; circuit; witness; outcome = Valid}
let lookup_wrong_arith_values =
let name = "lookup_wrong_arith_values" in
let wires =
let a = [1; 1; 4; 2; 4; 3; 1] in
let b = [2; 1; 2; 2; 3; 4; 4] in
let c = [3; 1; 1; 3; 2; 2; 2] in
[|a; b; c|]
in
let circuit = Circuit.make ~tables ~wires ~gates ~public_input_size:1 () in
{name; circuit; witness; outcome = Proof_error}
let wrong_plookup_values =
let name = "wrong_plookup_values" in
let wires =
let a = [0; 1; 4; 2; 4; 3; 1] in
let b = [2; 1; 2; 2; 3; 4; 4] in
let c = [3; 1; 1; 4; 2; 2; 2] in
[|a; b; c|]
in
let circuit = Circuit.make ~tables ~wires ~gates ~public_input_size:1 () in
{name; circuit; witness; outcome = Lookup_error}
let list =
[
qplookup;
qplookup_two_tables;
lookup_zero_values;
lookup_non_zero_values;
lookup_no_public_inputs;
lookup_wrong_arith_values;
wrong_plookup_values;
]
end