Source file polynomial_protocol.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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
module Make (PC : Kzg.Polynomial_commitment_sig) = struct
module Fr_generation = PC.Fr_generation
module PC = PC
module MP =
Polynomial.Multivariate.MultiPoly
(PC.Scalar)
(Polynomial.Univariate.Make (PC.Scalar))
module MPoly = MP.Polynomial
module Domain = PC.Polynomial.Domain
module Poly = PC.Polynomial.Polynomial
module Evaluations = Evaluations_map.Make (PC.Polynomial.Evaluations)
type prover_public_parameters = {
pc_public_parameters : PC.Public_parameters.prover;
subgroup_size : int;
g_map : Poly.t SMap.t;
g_prover_aux : PC.Commitment.prover_aux;
}
type verifier_public_parameters = {
pc_public_parameters : PC.Public_parameters.verifier;
subgroup_size : int;
cm_g_map : PC.Commitment.t;
}
let verifier_public_parameters_encoding :
verifier_public_parameters Data_encoding.t =
Data_encoding.(
conv
(fun { pc_public_parameters; subgroup_size; cm_g_map } ->
(pc_public_parameters, subgroup_size, cm_g_map))
(fun (pc_public_parameters, subgroup_size, cm_g_map) ->
{ pc_public_parameters; subgroup_size; cm_g_map })
(obj3
(req "pc_public_parameter" PC.Public_parameters.verifier_encoding)
(req "subgroup_size" int31)
(req "cm_g_map" PC.Commitment.encoding)))
type prover_query = {
v_map : (string * Poly.t) SMap.t;
precomputed_polys : Evaluations.t SMap.t;
}
let empty_prover_query =
{ v_map = SMap.empty; precomputed_polys = SMap.empty }
type not_committed = ..
let eval_not_committed :
(not_committed -> PC.Scalar.t array -> PC.Scalar.t) ref =
ref @@ function _ -> failwith "eval case not matched"
let cases_not_committed : not_committed Data_encoding.case SMap.t ref =
ref SMap.empty
let not_committed_encoding () =
Data_encoding.union (List.map snd @@ SMap.bindings @@ !cases_not_committed)
let not_committed_encoding : not_committed Data_encoding.t =
Data_encoding.delayed not_committed_encoding
let register_nc_eval_and_encoding :
(not_committed -> (PC.Scalar.t array -> PC.Scalar.t) option) ->
title:string ->
tag:int ->
'b Data_encoding.t ->
(not_committed -> 'b option) ->
('b -> not_committed) ->
unit =
fun f ~title ~tag inner from to' ->
let old = !eval_not_committed in
let new_eval e = match f e with Some g -> g | None -> old e in
eval_not_committed := new_eval;
cases_not_committed :=
SMap.add title
Data_encoding.(case ~title (Tag tag) inner from to')
!cases_not_committed
type verifier_query = {
v_map : (string * Poly.t) SMap.t;
identities : MPoly.t SMap.t;
not_committed : not_committed SMap.t;
}
let empty_verifier_query =
{ v_map = SMap.empty; identities = SMap.empty; not_committed = SMap.empty }
let verifier_query_encoding : verifier_query Data_encoding.t =
let open Encodings in
Data_encoding.(
delayed @@ fun () ->
conv
(fun { v_map; identities; not_committed } ->
(v_map, identities, not_committed))
(fun (v_map, identities, not_committed) ->
{ v_map; identities; not_committed })
(obj3
(req "v_map" (SMap.encoding (tup2 string Poly.encoding)))
(req "identities"
(SMap.encoding (MP.MonomialMap.encoding fr_encoding)))
(req "not_committed" (SMap.encoding not_committed_encoding))))
type aggregation_infos = {
nb_proofs : int SMap.t;
common_keys : string list;
}
type proof_type = Single | Aggregated of aggregation_infos
type secret = Poly.t SMap.t
type transcript = Bytes.t
let transcript_encoding = Data_encoding.bytes
type proof = {
cm_t_map : PC.Commitment.t;
pc_proof : PC.proof;
pc_answers : PC.answer list;
}
let proof_encoding : proof Data_encoding.t =
Data_encoding.(
conv
(fun { cm_t_map; pc_proof; pc_answers } ->
(cm_t_map, pc_proof, pc_answers))
(fun (cm_t_map, pc_proof, pc_answers) ->
{ cm_t_map; pc_proof; pc_answers })
(obj3
(req "cm_t_map" PC.Commitment.encoding)
(req "pc_proof" PC.proof_encoding)
(req "pc_answers" (list PC.answer_encoding))))
exception Rest_not_null of string
module Prover = struct
let compute_T n a precomputed =
let polys = List.map snd (SMap.bindings precomputed) in
let linear_coeffs =
Fr_generation.powers (List.length polys) a |> Array.to_list
in
let t_evaluation =
Evaluations.linear_c ~evaluations:polys ~linear_coeffs ()
in
let log = Z.log2up (Z.of_int @@ (Evaluations.degree t_evaluation + 1)) in
let domain = Domain.build ~log in
let sum = Evaluations.interpolation_fft domain t_evaluation in
let minus_one = PC.Scalar.(negate one) in
let proof, rem = Poly.division_xn sum n minus_one in
if Poly.is_zero rem then proof
else raise @@ Poly.Rest_not_null "T not divisible by (X^n - 1)"
let update_scalar_map query x name_list =
PC.Scalar_map.update x
(function
| None -> Some name_list
| Some l -> Some (List.rev_append name_list l))
query
let sort_query query =
PC.Scalar_map.map (List.sort_uniq String.compare) query
let format_query x h_map v_map =
let ffold _ (name, v) map =
let name, vx = (name, Poly.evaluate v x) in
update_scalar_map map vx [ name ]
in
let query_from_v_map = SMap.fold ffold v_map PC.Scalar_map.empty in
let query =
let list_h, _ = List.split (SMap.bindings h_map) in
update_scalar_map query_from_v_map x list_h
in
sort_query query
end
exception Wrong_transcript of string
module Verifier = struct
let search string list =
try List.assoc string list
with Not_found ->
failwith
(Format.sprintf "PP.Verifier.search : \"%s\" not found in answers."
string)
let format_query answers = PC.Scalar_map.map (List.map fst) answers
let add_first_common x h_map_base keys evals s_map v_map =
let rec aux h_map keys evals =
match (keys, evals) with
| [], _ -> h_map
| key :: key_tl, (name, eval) :: eval_tl -> (
if key = name then aux (SMap.add name eval h_map) key_tl eval_tl
else
match SMap.find_opt key v_map with
| None ->
failwith
(Printf.sprintf
"PP.Verifier.build_common_h_map : '%s' not found in \
v_map"
key)
| Some (base_name, poly) -> (
if not (base_name = name) then
failwith
(Printf.sprintf
"PP.Verifier.build_common_h_map : '%s' base name \
found in v_map does not match with s_map's '%s'"
base_name name)
else
match
PC.Scalar_map.find_opt (Poly.evaluate poly x) s_map
with
| None ->
failwith
(Printf.sprintf
"PP.Verifier.build_common_h_map : no binding for \
'%s' found in s_map"
key)
| Some eval_list ->
let comp_eval = search base_name (List.rev eval_list) in
aux
(SMap.add key comp_eval h_map)
key_tl ((name, eval) :: eval_tl)))
| _ ->
failwith
"PP.Verifier.take_first_common : keys must be shorter than evals"
in
aux h_map_base keys evals
let build_common_h_map x h_not_committed common_keys s_map v_map =
let common_keys =
common_keys
|> List.filter (fun name -> not (SMap.mem name h_not_committed))
|> List.sort (fun s t -> -String.compare s t)
in
let main_list_evals =
List.rev
(match PC.Scalar_map.find_opt x s_map with
| None ->
raise
@@ Wrong_transcript
(Printf.sprintf
"PP.Verifier.build_h_map : %s not found in answers ; \
make sure that transcript is the same for prover and \
verifier."
(PC.Scalar.to_string x))
| Some res -> res)
in
add_first_common x h_not_committed common_keys main_list_evals s_map v_map
let add_in_hmap name name_identity vx h_map s_map_list =
match List.assoc_opt vx s_map_list with
| None ->
raise
@@ Wrong_transcript
(Printf.sprintf
"PP.Verifier.build_h_map : %s(%s) not found in answers ; \
make sure that transcript is the same for prover and \
verifier."
name (PC.Scalar.to_string vx))
| Some eval_map -> (
match SMap.find_opt name eval_map with
| None ->
failwith
(Format.sprintf
"PP.Verifier.build_h_map : \"%s\" not found in answers." name)
| Some hx -> SMap.add_unique name_identity hx h_map)
let build_h_map x names_identity h_not_committed v_map s_map =
let ffold h_map name =
match SMap.find_opt name h_not_committed with
| Some _ -> h_map
| None -> (
match SMap.find_opt name v_map with
| Some (old_name, v) ->
let vx = Poly.evaluate v x in
add_in_hmap old_name name vx h_map s_map
| None -> add_in_hmap name name x h_map s_map)
in
List.fold_left ffold h_not_committed names_identity
let build_h_map_i ~prefix x names_identity h_not_committed v_map answer =
let ffold h_map name =
let name_prefixed = prefix ^ name in
match SMap.find_opt name_prefixed h_not_committed with
| Some value -> SMap.add name value h_map
| None -> (
match SMap.find_opt name v_map with
| Some (old_name, v) ->
let vx = Poly.evaluate v x in
let old_name_prefixed = prefix ^ old_name in
add_in_hmap old_name_prefixed name vx h_map answer
| None -> add_in_hmap name_prefixed name x h_map answer)
in
List.fold_left ffold SMap.empty names_identity
let is_t_name name = Char.(equal name.[0] 'T' && equal name.[1] '_')
let build_tzs_h_map x_ni s_map eval_point =
let t_evals =
List.filter
(fun x -> is_t_name (fst x))
(PC.Scalar_map.find eval_point s_map)
in
SMap.(add "Zs" PC.Scalar.(sub x_ni.(1) one) (of_list t_evals))
let remove_t map =
let ret = SMap.filter (fun name _ -> not (is_t_name name)) map in
(ret, SMap.cardinal map - SMap.cardinal ret)
let starts_with prefix name =
let len_prefix = String.length prefix in
if len_prefix >= String.length name then false
else
let start = String.sub name 0 len_prefix in
prefix = start
let apply ~proof_type a x identities h_not_committed v_map answer =
let add_ids sum h_map identities =
let identities = List.rev_map snd (SMap.bindings identities) in
List.fold_left
(fun sum_id id ->
PC.Scalar.((sum_id * a) + MPoly.fast_apply id h_map))
sum identities
in
match proof_type with
| Single ->
let id_support = MPoly.get_support_map identities in
let h_map = build_h_map x id_support h_not_committed v_map answer in
add_ids PC.Scalar.zero h_map identities
| Aggregated aggregation_infos ->
let circuit_list =
List.rev (SMap.bindings aggregation_infos.nb_proofs)
in
let rec apply_all identities id_support len_prefix res i =
if i < 0 then res
else
let prefix = SMap.Aggregation.int_to_string ~len_prefix i in
let h_map =
build_h_map_i ~prefix x id_support h_not_committed v_map answer
in
apply_all identities id_support len_prefix
(add_ids res h_map identities)
(i - 1)
in
List.fold_left
(fun res (name, nb_proofs) ->
let identities =
let c_prefix =
if name = "" then "" else name ^ SMap.Aggregation.sep
in
SMap.filter
(fun id_name _ -> starts_with c_prefix id_name)
identities
in
let perm_ids, rest_ids =
SMap.partition
(fun id_name _ ->
let last_piece =
List.hd
(List.rev
(String.split_on_char
(String.get SMap.Aggregation.sep 0)
id_name))
in
String.sub last_piece 0 4 = "Perm")
identities
in
let id_support = MPoly.get_support_map rest_ids in
let len_prefix = SMap.Aggregation.compute_len_prefix ~nb_proofs in
let res = apply_all perm_ids id_support len_prefix res 0 in
let res =
apply_all rest_ids id_support len_prefix res (nb_proofs - 1)
in
res)
PC.Scalar.zero circuit_list
end
let setup ~setup_params g_map ~subgroup_size srsfiles =
let pp_prover, pp_verifier =
PC.Public_parameters.import setup_params srsfiles
in
let cm_g_map, g_prover_aux = PC.Commitment.commit pp_prover g_map in
( { pc_public_parameters = pp_prover; subgroup_size; g_map; g_prover_aux },
{ pc_public_parameters = pp_verifier; subgroup_size; cm_g_map } )
let merge_prover_queries list_queries =
let list_v_map =
List.map (fun (query : prover_query) -> query.v_map) list_queries
in
let v_map = SMap.union_disjoint_list list_v_map in
let list_precomputed_polys =
List.map (fun query -> query.precomputed_polys) list_queries
in
let precomputed_polys = SMap.union_disjoint_list list_precomputed_polys in
{ v_map; precomputed_polys }
let merge_verifier_queries ?(common_keys = []) list_queries =
let common_keys_equal_elt = (common_keys, fun _ _ -> true) in
let list_v_map = List.map (fun query -> query.v_map) list_queries in
let v_map = SMap.union_disjoint_list list_v_map in
let list_identities =
List.map (fun query -> query.identities) list_queries
in
let identities = SMap.union_disjoint_list list_identities in
let list_not_committed =
List.map (fun query -> query.not_committed) list_queries
in
let not_committed =
SMap.union_disjoint_list ~common_keys_equal_elt list_not_committed
in
{ v_map; identities; not_committed }
let merge_v_map ~len_prefix common_keys list_map =
let equal_elt (name_1, poly_1) (name_2, poly_2) =
name_1 = name_2 && Poly.equal poly_1 poly_2
in
assert (List.for_all (SMap.equal equal_elt (List.hd list_map)) list_map);
let new_list_map =
let update_value prefix (name, poly) = (prefix ^ name, poly) in
List.mapi
(SMap.Aggregation.rename ~len_prefix ~update_value ~common_keys)
list_map
in
SMap.union_disjoint_list ~common_keys_equal_elt:(common_keys, equal_elt)
new_list_map
let update_common_keys_with_v_map ?( = "") common_keys ~v_map =
SMap.fold
(fun composed_name (base_name, _poly_to_compose) common_keys ->
if List.mem (extra_prefix ^ base_name) common_keys then
(extra_prefix ^ composed_name) :: common_keys
else common_keys)
v_map common_keys
let merge_equal_set_of_keys_prover_queries ?( = "") ~len_prefix
~common_keys (list_queries : prover_query list) =
let list_v_map =
List.map (fun (query : prover_query) -> query.v_map) list_queries
in
let v_map = merge_v_map ~len_prefix common_keys list_v_map in
let list_precomputed_polys =
List.map (fun query -> query.precomputed_polys) list_queries
in
let precomputed_polys =
SMap.Aggregation.merge_equal_set_of_keys
~common_keys_equal_elt:(common_keys, Evaluations.equal)
~len_prefix list_precomputed_polys
in
let precomputed_polys =
SMap.Aggregation.prefix_map ~prefix:extra_prefix precomputed_polys
in
{ v_map; precomputed_polys }
let merge_equal_set_of_keys_verifier_queries ?( = "") ~len_prefix
~common_keys list_queries =
let list_v_map = List.map (fun query -> query.v_map) list_queries in
let v_map =
SMap.Aggregation.prefix_map ~prefix:extra_prefix (List.hd list_v_map)
in
let list_identities =
List.map (fun query -> query.identities) list_queries
in
let identities =
SMap.Aggregation.prefix_map ~prefix:extra_prefix (List.hd list_identities)
in
let list_not_committed =
List.map (fun query -> query.not_committed) list_queries
in
let not_committed =
let equal_elt _elt_1 _elt_2 = true in
SMap.Aggregation.merge_equal_set_of_keys ~extra_prefix
~common_keys_equal_elt:(common_keys, equal_elt) ~len_prefix
list_not_committed
in
{ v_map; identities; not_committed }
let merge_vmaps map1 map2 function_name =
SMap.union
(fun key v1 v2 ->
let str1, poly1 = v1 in
let str2, poly2 = v2 in
if String.equal str1 str2 && Poly.equal poly1 poly2 then Some v1
else
raise
(Invalid_argument
(Printf.sprintf "PP/%s: Distinct values in vmap for label '%s'."
function_name key)))
map1 map2
let sum_prover_queries q1 q2 =
let precomputed_polys =
SMap.union
(fun _key v1 v2 ->
let res_eval =
if Evaluations.length v1 <= Evaluations.length v2 then v1 else v2
in
Some (Evaluations.add ~res:res_eval v1 v2))
q1.precomputed_polys q2.precomputed_polys
in
let v_map = merge_vmaps q1.v_map q2.v_map "sum_prover_queries" in
{ v_map; precomputed_polys }
let sum_verifier_queries q1 q2 =
let v_map = merge_vmaps q1.v_map q2.v_map "sum_verifier_queries" in
let identities =
SMap.union
(fun _key v1 v2 -> Some (MPoly.add v1 v2))
q1.identities q2.identities
in
let not_committed = SMap.union_disjoint q1.not_committed q2.not_committed in
{ v_map; identities; not_committed }
let intersects_v_map v_map map =
SMap.exists
(fun name _ ->
SMap.exists (fun _key (name', _) -> String.equal name name') v_map)
map
let prove { pc_public_parameters; subgroup_size; g_map; g_prover_aux }
transcript (f_map_list, f_prover_aux) { v_map; precomputed_polys } =
let a, transcript = Fr_generation.random_fr transcript in
let t_map =
let t = Prover.compute_T subgroup_size a precomputed_polys in
let t_list =
let d = PC.Public_parameters.get_d pc_public_parameters in
Poly.split d subgroup_size t
in
SMap.of_list (List.mapi (fun i t -> ("T_" ^ string_of_int i, t)) t_list)
in
let cm_t_map, t_prover_aux =
PC.Commitment.commit pc_public_parameters t_map
in
let transcript = PC.Commitment.expand_transcript transcript cm_t_map in
let x, transcript = Fr_generation.random_fr transcript in
let h_map_list = g_map :: t_map :: f_map_list in
let prover_aux_list = g_prover_aux :: t_prover_aux :: f_prover_aux in
let eval_points =
match SMap.choose_opt v_map with
| None -> SMap.singleton "x" x
| Some (_, (_, gX)) ->
let gx = Poly.evaluate gX x in
SMap.of_list [ ("x", x); ("gx", gx) ]
in
let query_list =
let g_query =
if intersects_v_map v_map (List.hd h_map_list) then eval_points
else SMap.singleton "x" x
in
let t_query = SMap.singleton "x" x in
g_query :: t_query :: List.map (fun _ -> eval_points) f_map_list
in
let answer_list = List.map2 PC.evaluate h_map_list query_list in
let pc_proof, transcript =
PC.prove pc_public_parameters transcript h_map_list prover_aux_list
query_list answer_list
in
({ cm_t_map; pc_proof; pc_answers = answer_list }, transcript)
let verify ~proof_type { pc_public_parameters; subgroup_size; cm_g_map }
transcript cm_f_map_list ?(not_committed_online = SMap.empty)
{ cm_t_map; pc_proof; pc_answers } { v_map; identities; not_committed } =
let a, transcript = Fr_generation.random_fr transcript in
let transcript = PC.Commitment.expand_transcript transcript cm_t_map in
let cm_map_list = cm_g_map :: cm_t_map :: cm_f_map_list in
let x, transcript = Fr_generation.random_fr transcript in
let eval_points =
match SMap.choose_opt v_map with
| None -> SMap.singleton "x" x
| Some (_, (_, gX)) ->
let gx = Poly.evaluate gX x in
SMap.of_list [ ("x", x); ("gx", gx) ]
in
let query_list =
List.map (SMap.mapi (fun key _ -> SMap.find key eval_points)) pc_answers
in
let kzg_verif, transcript =
PC.verify pc_public_parameters transcript cm_map_list query_list
pc_answers pc_proof
in
let nb_t = PC.Commitment.cardinal cm_t_map in
let x_ni =
let pows =
let nb_x = max 2 nb_t in
Fr_generation.powers nb_x (PC.Scalar.pow x (Z.of_int subgroup_size))
in
pows.(0) <- x;
pows
in
let h_map_not_committed =
SMap.map (fun e -> !eval_not_committed e x_ni) not_committed
in
let query =
let union =
SMap.union (fun _ z z' ->
if PC.Scalar.eq z z' then Some z
else
failwith "group_query: equal query names must map to equal values")
in
List.fold_left union (List.hd query_list) (List.tl query_list)
in
let answer_list =
List.fold_left
(SMap.union (fun _ m1 m2 -> Some (SMap.union_disjoint m1 m2)))
SMap.empty pc_answers
|> SMap.bindings
|> List.map (fun (x, map) -> (SMap.find x query, map))
in
let answer =
PC.Scalar_map.of_seq
(List.to_seq
@@ List.map
(fun (z, m) -> (z, SMap.to_seq m |> List.of_seq))
answer_list)
in
let x_answer = PC.Scalar_map.find x answer |> SMap.of_list in
let additional_answers =
SMap.map (fun p -> MPoly.fast_apply p x_answer) not_committed_online
in
let answer =
PC.Scalar_map.add x
(SMap.union_disjoint x_answer additional_answers |> SMap.bindings)
answer
in
let sum_id =
match proof_type with
| Single ->
let identities =
SMap.map
(fun id -> MPoly.partial_apply id h_map_not_committed)
identities
in
Verifier.apply ~proof_type a x identities h_map_not_committed v_map
answer_list
| Aggregated aggregation_infos ->
let common_not_committed, h_map_not_committed =
SMap.partition
(fun k _ -> List.mem k aggregation_infos.common_keys)
h_map_not_committed
in
let s_map = answer in
let common_h_map =
let common_keys =
aggregation_infos.common_keys
@ List.init nb_t (fun i -> "T_" ^ string_of_int i)
in
Verifier.build_common_h_map x common_not_committed common_keys s_map
v_map
in
let identities =
SMap.map (fun id -> MPoly.partial_apply id common_h_map) identities
in
Verifier.apply ~proof_type a x identities h_map_not_committed v_map
answer_list
in
let tzs =
let identity =
MPoly.of_list
(List.init nb_t (function
| 0 -> (SMap.monomial_of_list [ "T_0"; "Zs" ], PC.Scalar.one)
| i ->
( SMap.monomial_of_list [ "T_" ^ string_of_int i; "Zs" ],
x_ni.(i) )))
in
let h_map = Verifier.build_tzs_h_map x_ni answer x in
MPoly.fast_apply identity h_map
in
let id_verif = PC.Scalar.eq sum_id tzs in
(kzg_verif && id_verif, transcript)
end
module type Polynomial_protocol_sig = sig
module PC : Kzg.Polynomial_commitment_sig
module MP :
Polynomial.Multivariate.MultiPoly_sig with type scalar = PC.Scalar.t
module Evaluations :
Evaluations_map.Evaluations_sig
with type scalar = PC.Scalar.t
and type domain = PC.Polynomial.Domain.t
and type polynomial = PC.Polynomial.Polynomial.t
and type t = PC.Polynomial.Evaluations.t
exception Wrong_transcript of string
type prover_public_parameters = {
pc_public_parameters : PC.Public_parameters.prover;
subgroup_size : int;
g_map : PC.Polynomial.Polynomial.t SMap.t;
g_prover_aux : PC.Commitment.prover_aux;
}
type verifier_public_parameters = {
pc_public_parameters : PC.Public_parameters.verifier;
subgroup_size : int;
cm_g_map : PC.Commitment.t;
}
val verifier_public_parameters_encoding :
verifier_public_parameters Data_encoding.t
type prover_query = {
v_map : (string * PC.Polynomial.Polynomial.t) SMap.t;
precomputed_polys : Evaluations.t SMap.t;
}
type not_committed = ..
type verifier_query = {
v_map : (string * PC.Polynomial.Polynomial.t) SMap.t;
identities : PC.Scalar.t MP.MonomialMap.t SMap.t;
not_committed : not_committed SMap.t;
}
val verifier_query_encoding : verifier_query Data_encoding.t
val register_nc_eval_and_encoding :
(not_committed -> (PC.Scalar.t array -> PC.Scalar.t) option) ->
title:string ->
tag:int ->
'b Data_encoding.t ->
(not_committed -> 'b option) ->
('b -> not_committed) ->
unit
type aggregation_infos = {
nb_proofs : int SMap.t;
common_keys : string list;
}
type proof_type = Single | Aggregated of aggregation_infos
val empty_prover_query : prover_query
val empty_verifier_query : verifier_query
val merge_prover_queries : prover_query list -> prover_query
val merge_verifier_queries :
?common_keys:string list -> verifier_query list -> verifier_query
val sum_prover_queries : prover_query -> prover_query -> prover_query
val sum_verifier_queries : verifier_query -> verifier_query -> verifier_query
val update_common_keys_with_v_map :
?extra_prefix:string ->
string list ->
v_map:(string * PC.Polynomial.Polynomial.t) SMap.t ->
string list
val merge_equal_set_of_keys_prover_queries :
?extra_prefix:string ->
len_prefix:int ->
common_keys:string list ->
prover_query list ->
prover_query
val merge_equal_set_of_keys_verifier_queries :
?extra_prefix:string ->
len_prefix:int ->
common_keys:string list ->
verifier_query list ->
verifier_query
type transcript = PC.transcript
val transcript_encoding : transcript Data_encoding.t
type proof = {
cm_t_map : PC.Commitment.t;
pc_proof : PC.proof;
pc_answers : PC.answer list;
}
val proof_encoding : proof Data_encoding.t
val setup :
setup_params:PC.Public_parameters.setup_params ->
PC.secret ->
subgroup_size:int ->
(string * string) * (string * string) ->
prover_public_parameters * verifier_public_parameters
val prove :
prover_public_parameters ->
transcript ->
PC.secret list * PC.Commitment.prover_aux list ->
prover_query ->
proof * transcript
val verify :
proof_type:proof_type ->
verifier_public_parameters ->
transcript ->
PC.Commitment.t list ->
?not_committed_online:MP.Polynomial.t SMap.t ->
proof ->
verifier_query ->
bool * transcript
end
include (Make (Kzg) : Polynomial_protocol_sig)