Source file cryptobox.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
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
open Error_monad
include Cryptobox_intf
module Srs_g1 = Kzg.Bls.Srs_g1
module Srs_g2 = Kzg.Bls.Srs_g2
module Scalar = Kzg.Bls.Scalar
module Poly = Kzg.Bls.Poly
module Domain = Kzg.Bls.Domain
module Evals = Kzg.Bls.Evals
module FFT = Kzg.Utils.FFT
module Degree_check = Kzg.Degree_check
module Kate_amortized = Kzg.Kate_amortized
module Base58 = Tezos_crypto.Base58
type error += Failed_to_load_trusted_setup of string
let () =
register_error_kind
`Permanent
~id:"dal.node.trusted_setup_loading_failed"
~title:"Trusted setup loading failed"
~description:"Trusted setup failed to load"
~pp:(fun ppf msg ->
Format.fprintf ppf "Trusted setup failed to load: %s" msg)
Data_encoding.(obj1 (req "msg" string))
(function
| Failed_to_load_trusted_setup parameter -> Some parameter | _ -> None)
(fun parameter -> Failed_to_load_trusted_setup parameter)
[@@coverage off]
type initialisation_parameters =
| Verifier of {is_fake : bool}
| Prover of {is_fake : bool; srs_g1 : Srs_g1.t; srs_g2 : Srs_g2.t}
let initialisation_parameters = ref @@ Verifier {is_fake = false}
let load_parameters parameters =
let open Result_syntax in
initialisation_parameters := parameters ;
return_unit
let initialisation_parameters_from_files ~srs_g1_path ~srs_g2_path ~srs_size =
let open Lwt_syntax in
let* srs = Srs.read_srs ~len:srs_size ~srs_g1_path ~srs_g2_path () in
let open Result_syntax in
Lwt.return
@@
match srs with
| Error (`End_of_file s) ->
tzfail (Failed_to_load_trusted_setup ("EOF: " ^ s))
| Error (`Invalid_point p) ->
tzfail
(Failed_to_load_trusted_setup (Printf.sprintf "Invalid point %i" p))
| Ok srs -> return srs
module Inner = struct
module Commitment = struct
include Kzg.Commitment.Single_G1
type Tezos_crypto.Base58.data += Data of t
let b58check_encoding =
Tezos_crypto.Base58.register_encoding
~prefix:Tezos_crypto.Base58.Prefix.slot_header
~length:size
~to_raw:to_string
~of_raw:of_string_opt
~wrap:(fun x -> Data x)
[@@coverage off]
let raw_encoding = encoding [@@coverage off]
include Tezos_crypto.Helpers.Make (struct
type t = Kzg.Commitment.Single_G1.t
let name = "DAL_commitment"
let title = "Commitment representation for the DAL"
let b58check_encoding = b58check_encoding
let raw_encoding = raw_encoding
let compare = compare
let equal = equal
let hash _ =
assert false
[@@coverage off]
let seeded_hash _ _ =
assert false
[@@coverage off]
end)
let of_b58check = of_b58check
end
module Proof = Commitment
module Commitment_proof = Degree_check.Proof
type slot = bytes
type scalar = Scalar.t
type polynomial = Poly.t
type commitment = Commitment.t
type shard_proof = Proof.t
type commitment_proof = Commitment_proof.t
type page_proof = Proof.t
type page = bytes
type share = Scalar.t array
type shard = {index : int; share : share}
type shards_proofs_precomputation = Kate_amortized.preprocess
type ('a, 'b) error_container = {given : 'a; expected : 'b}
module Encoding = struct
open Data_encoding
let page_proof_encoding = Proof.encoding
let share_encoding = array Scalar.encoding
let shard_proof_encoding = Proof.encoding
let shard_encoding =
conv
(fun {index; share} -> (index, share))
(fun (index, share) -> {index; share})
(tup2 int31 share_encoding)
[@@coverage off]
let shards_proofs_precomputation_encoding =
Kate_amortized.preprocess_encoding
let error_container_encoding (given_encoding : 'a encoding)
(expected_encoding : 'b encoding) : ('a, 'b) error_container encoding =
conv
(fun {given; expected} -> (given, expected))
(fun (given, expected) -> {given; expected})
(obj2 (req "given" given_encoding) (req "expected" expected_encoding))
end
include Encoding
type error += Invalid_precomputation_hash of (string, string) error_container
let () =
register_error_kind
`Permanent
~id:"dal.node.invalid_precomputation_hash"
~title:"Invalid_precomputation_hash"
~description:"Unexpected precomputation hash"
~pp:(fun ppf {given; expected} ->
Format.fprintf
ppf
"Invalid precomputation hash: expected %s. Got %s"
expected
given)
(Encoding.error_container_encoding
Data_encoding.string
Data_encoding.string)
(function Invalid_precomputation_hash err -> Some err | _ -> None)
(function err -> Invalid_precomputation_hash err)
[@@coverage off]
let make_domain n = Domain.build n
type t = {
redundancy_factor : int;
slot_size : int;
page_size : int;
number_of_shards : int;
max_polynomial_length : int;
erasure_encoded_polynomial_length : int;
domain_polynomial_length : Domain.t;
domain_2_times_polynomial_length : Domain.t;
domain_erasure_encoded_polynomial_length : Domain.t;
shard_length : int;
pages_per_slot : int;
page_length : int;
page_length_domain : int;
remaining_bytes : int;
srs_verifier : Srs.srs_verifier;
mode : [`Verifier | `Prover];
kate_amortized : Kate_amortized.public_parameters;
degree_check : Srs_g2.t option;
}
let ensure_validity ~is_fake ~mode ~slot_size ~page_size ~redundancy_factor
~number_of_shards =
let open Result_syntax in
let* () =
Parameters_check.ensure_validity_without_srs
~slot_size
~page_size
~redundancy_factor
~number_of_shards
in
Srs.ensure_srs_validity
~is_fake
~mode
~slot_size
~page_size
~redundancy_factor
~number_of_shards
type parameters = Dal_config.parameters = {
redundancy_factor : int;
page_size : int;
slot_size : int;
number_of_shards : int;
}
let parameters_encoding = Dal_config.parameters_encoding
let pages_per_slot {slot_size; page_size; _} = slot_size / page_size
module Cache = Hashtbl.Make (struct
type t = parameters * initialisation_parameters
let equal (param, init_param) (param', init_param') =
param = param'
&&
match (init_param, init_param') with
| Verifier {is_fake; _}, Verifier {is_fake = is_fake'; _}
when is_fake = is_fake' ->
true
| Prover {is_fake; _}, Prover {is_fake = is_fake'; _}
when is_fake = is_fake' ->
true
| _ -> false
let hash = Hashtbl.hash
end)
let make =
let open Result_syntax in
let table = Cache.create 5 in
let with_cache (parameters, initialisation_parameters) f =
match Cache.find_opt table (parameters, initialisation_parameters) with
| Some x -> return x
| None ->
let* x = f () in
Cache.replace table (parameters, initialisation_parameters) x ;
return x
in
fun ({redundancy_factor; slot_size; page_size; number_of_shards} as
parameters) ->
with_cache (parameters, !initialisation_parameters) @@ fun () ->
let max_polynomial_length, erasure_encoded_polynomial_length, shard_length
=
Parameters_check.compute_lengths
~redundancy_factor
~slot_size
~page_size
~number_of_shards
in
let page_length = Parameters_check.page_length ~page_size in
let page_length_domain, _, _ = FFT.select_fft_domain page_length in
let mode, is_fake, srs_g1, degree_check =
match !initialisation_parameters with
| Verifier {is_fake} ->
let srs =
if is_fake then Srs.Internal_for_tests.get_verifier_srs1 ()
else Srs.get_verifier_srs1 ()
in
(`Verifier, is_fake, srs, None)
| Prover {is_fake; srs_g1; srs_g2} ->
(`Prover, is_fake, srs_g1, Some srs_g2)
in
let* () =
ensure_validity
~is_fake
~mode
~slot_size
~page_size
~redundancy_factor
~number_of_shards
in
let srs_verifier =
(if is_fake then Srs.Internal_for_tests.get_verifier_srs2
else Srs.get_verifier_srs2)
~max_polynomial_length
~page_length_domain
~shard_length
in
let kate_amortized =
Kate_amortized.
{max_polynomial_length; shard_length; srs_g1; number_of_shards}
in
return
{
redundancy_factor;
slot_size;
page_size;
number_of_shards;
max_polynomial_length;
erasure_encoded_polynomial_length;
domain_polynomial_length = make_domain max_polynomial_length;
domain_2_times_polynomial_length =
make_domain (2 * max_polynomial_length);
domain_erasure_encoded_polynomial_length =
make_domain erasure_encoded_polynomial_length;
shard_length;
pages_per_slot = pages_per_slot parameters;
page_length;
page_length_domain;
remaining_bytes = page_size mod Parameters_check.scalar_bytes_amount;
srs_verifier;
mode;
kate_amortized;
degree_check;
}
let parameters
({redundancy_factor; slot_size; page_size; number_of_shards; _} : t) =
{redundancy_factor; slot_size; page_size; number_of_shards}
[@@coverage off]
let polynomial_degree = Poly.degree
let polynomial_evaluate = Poly.evaluate
let polynomials_product d ps =
let evaluations = List.map (FFT.fft d) ps in
FFT.ifft_inplace d (Evals.mul_c ~evaluations ())
let polynomial_from_slot (t : t) slot =
if Bytes.length slot <> t.slot_size then
Error
(`Slot_wrong_size
(Printf.sprintf "message must be %d bytes long" t.slot_size))
else
let offset = ref 0 in
let coefficients =
Array.init t.max_polynomial_length (fun _ -> Scalar.(copy zero))
in
for page = 0 to t.pages_per_slot - 1 do
for elt = 0 to t.page_length - 2 do
if !offset >= t.slot_size then ()
else
let dst = Bytes.create Parameters_check.scalar_bytes_amount in
Bytes.blit slot !offset dst 0 Parameters_check.scalar_bytes_amount ;
offset := !offset + Parameters_check.scalar_bytes_amount ;
coefficients.((elt * t.pages_per_slot) + page) <-
Scalar.of_bytes_exn dst
done ;
let dst = Bytes.create t.remaining_bytes in
Bytes.blit slot !offset dst 0 t.remaining_bytes ;
offset := !offset + t.remaining_bytes ;
coefficients.(((t.page_length - 1) * t.pages_per_slot) + page) <-
Scalar.of_bytes_exn dst
done ;
Ok
(FFT.ifft_inplace
(Domain.build t.max_polynomial_length)
(Evals.of_array (t.max_polynomial_length - 1, coefficients)))
let polynomial_to_slot t p =
let evaluations = FFT.fft (Domain.build t.max_polynomial_length) p in
let slot = Bytes.make t.slot_size '\x00' in
let offset = ref 0 in
for page = 0 to t.pages_per_slot - 1 do
for elt = 0 to t.page_length - 2 do
let idx = (elt * t.pages_per_slot) + page in
let coeff = Scalar.to_bytes (Evals.get evaluations idx) in
Bytes.blit coeff 0 slot !offset Parameters_check.scalar_bytes_amount ;
offset := !offset + Parameters_check.scalar_bytes_amount
done ;
let idx = ((t.page_length - 1) * t.pages_per_slot) + page in
let coeff = Scalar.to_bytes (Evals.get evaluations idx) in
Bytes.blit coeff 0 slot !offset t.remaining_bytes ;
offset := !offset + t.remaining_bytes
done ;
slot
let encode t p =
Evals.to_array
(FFT.fft (Domain.build t.erasure_encoded_polynomial_length) p)
let shards_from_polynomial t p =
let codeword = encode t p in
let rec loop index seq =
if index < 0 then seq
else
let share = Array.init t.shard_length (fun _ -> Scalar.(copy zero)) in
for j = 0 to t.shard_length - 1 do
share.(j) <- codeword.((t.number_of_shards * j) + index)
done ;
loop (index - 1) (Seq.cons {index; share} seq)
in
loop (t.number_of_shards - 1) Seq.empty
module ShardSet = Set.Make (struct
type t = shard
let compare a b = Int.compare a.index b.index
end)
let encoded_share_size t =
let share_scalar_len =
t.erasure_encoded_polynomial_length / t.number_of_shards
in
(share_scalar_len * Scalar.size_in_bytes) + 4
let polynomial_from_shards t shards =
let shards =
Seq.take (t.max_polynomial_length / t.shard_length) shards
|> ShardSet.of_seq
in
if t.max_polynomial_length / t.shard_length > ShardSet.cardinal shards then
Error
(`Not_enough_shards
(Printf.sprintf
"there must be at least %d shards to decode"
(t.max_polynomial_length / t.shard_length)))
else if
ShardSet.exists
(fun {share; _} -> Array.length share <> t.shard_length)
shards
then
Error
(`Invalid_shard_length
(Printf.sprintf
"At least one shard of invalid length: expected length %d."
t.shard_length))
else if
ShardSet.exists
(fun {index; _} -> index >= t.number_of_shards || index < 0)
shards
then
Error
(`Shard_index_out_of_range
(Printf.sprintf
"At least one shard index out of range: expected indices within \
the range [%d, %d]."
0
(t.number_of_shards - 1)))
else
let mul acc i =
Poly.mul_xn
acc
t.shard_length
(Scalar.negate
(Domain.get
t.domain_erasure_encoded_polynomial_length
(i * t.shard_length)))
in
let partition_products seq =
ShardSet.fold
(fun {index; _} (l, r) -> (mul r index, l))
seq
(Poly.one, Poly.one)
in
let p1, p2 = partition_products shards in
assert (Poly.degree p1 + Poly.degree p2 = t.max_polynomial_length) ;
let mul_domain = Domain.build (2 * t.max_polynomial_length) in
let eep_domain = Domain.build t.erasure_encoded_polynomial_length in
let a_poly = polynomials_product mul_domain [p1; p2] in
assert (Poly.degree a_poly = t.max_polynomial_length) ;
let a' = Poly.derivative a_poly in
let eval_a' = FFT.fft eep_domain a' in
let compute_n t eval_a' shards =
let n_poly =
Array.init t.erasure_encoded_polynomial_length (fun _ ->
Scalar.(copy zero))
in
ShardSet.iter
(fun {index; share} ->
for j = 0 to Array.length share - 1 do
let c_i = share.(j) in
let i = (t.number_of_shards * j) + index in
let x_i =
Domain.get t.domain_erasure_encoded_polynomial_length i
in
let tmp = Evals.get eval_a' i in
Scalar.mul_inplace tmp tmp x_i ;
Scalar.inverse_exn_inplace tmp tmp ;
Scalar.mul_inplace tmp tmp c_i ;
n_poly.(i) <- tmp
done)
shards ;
Evals.of_array (t.erasure_encoded_polynomial_length - 1, n_poly)
in
let n_poly = compute_n t eval_a' shards in
let b =
Poly.truncate
~len:t.max_polynomial_length
(FFT.ifft_inplace eep_domain n_poly)
in
Poly.mul_by_scalar_inplace
b
(Scalar.negate (Scalar.of_int t.erasure_encoded_polynomial_length))
b ;
let p = polynomials_product mul_domain [a_poly; b] in
Ok (Poly.truncate ~len:t.max_polynomial_length p)
let commit t p =
match t.mode with
| `Verifier -> Error `Prover_SRS_not_loaded
| `Prover -> (
try Ok (Commitment.commit t.kate_amortized.srs_g1 p)
with Kzg.Commitment.SRS_too_short _ ->
Error
(`Invalid_degree_strictly_less_than_expected
{
given = Poly.degree p;
expected = Srs_g1.size t.kate_amortized.srs_g1;
}))
let pp_commit_error fmt = function
| `Invalid_degree_strictly_less_than_expected {given; expected} ->
Format.fprintf
fmt
"Invalid degree: expecting input polynomial to commit function to \
have a degree strictly less than %d. Got %d."
expected
given
| `Prover_SRS_not_loaded ->
Format.fprintf
fmt
"The prover's SRS was not loaded: cannot commit a polynomial without \
the prover's SRS."
let string_of_commit_error err = Format.asprintf "%a" pp_commit_error err
let prove_commitment ({max_polynomial_length; degree_check; _} : t) p =
match degree_check with
| None -> Error `Prover_SRS_not_loaded
| Some srs_g2 ->
if Srs_g2.size srs_g2 >= max_polynomial_length then
Ok
(Degree_check.prove
~max_commit:(Srs_g2.size srs_g2 - 1)
~max_degree:(max_polynomial_length - 1)
srs_g2
p)
else
Error
(`Invalid_degree_strictly_less_than_expected
{given = max_polynomial_length; expected = Srs_g2.size srs_g2})
let verify_commitment (t : t) cm proof =
Degree_check.verify t.srs_verifier.commitment cm proof
let save_precompute_shards_proofs precomputation ~filename =
protect (fun () ->
Lwt_io.with_file ~mode:Output filename (fun chan ->
let open Lwt_result_syntax in
let str =
Data_encoding.Binary.to_string_exn
Encoding.shards_proofs_precomputation_encoding
precomputation
in
let*! () = Lwt_io.write chan str in
return_unit))
let hash_precomputation precomputation =
let encoding =
Data_encoding.Binary.to_bytes_exn
Encoding.shards_proofs_precomputation_encoding
precomputation
in
Tezos_crypto.Blake2B.hash_bytes [encoding]
let load_precompute_shards_proofs ~hash ~filename () =
protect (fun () ->
Lwt_io.with_file ~mode:Input filename (fun chan ->
let open Lwt_result_syntax in
let*! str = Lwt_io.read chan in
let precomputation =
Data_encoding.Binary.of_string_exn
Encoding.shards_proofs_precomputation_encoding
str
in
let* () =
match hash with
| Some given ->
let expected = hash_precomputation precomputation in
if Tezos_crypto.Blake2B.equal given expected then return_unit
else
tzfail
(Invalid_precomputation_hash
{
given = Tezos_crypto.Blake2B.to_string given;
expected = Tezos_crypto.Blake2B.to_string expected;
})
| None -> return_unit
in
return precomputation))
let precompute_shards_proofs {kate_amortized; _} =
try Ok (Kate_amortized.preprocess_multiple_multi_reveals kate_amortized)
with Kzg.Commitment.SRS_too_short _ ->
Error
(`Invalid_degree_strictly_less_than_expected
{
given = Srs_g1.size kate_amortized.srs_g1;
expected = kate_amortized.max_polynomial_length;
})
let prove_shards t ~precomputation ~polynomial =
let coefficients =
Array.init (t.max_polynomial_length + 1) (fun _ -> Scalar.(copy zero))
in
let p_length = Poly.degree polynomial + 1 in
let p = Poly.to_dense_coefficients polynomial in
Array.blit p 0 coefficients 0 p_length ;
Kate_amortized.multiple_multi_reveals
t.kate_amortized
~preprocess:precomputation
~coefficients
let verify_shard (t : t) commitment {index = shard_index; share = evaluations}
proof =
if shard_index < 0 || shard_index >= t.number_of_shards then
Error
(`Shard_index_out_of_range
(Printf.sprintf
"Shard index out of range: got index %d, expected index within \
range [%d, %d]."
shard_index
0
(t.number_of_shards - 1)))
else
let expected_shard_length = t.shard_length in
let got_shard_length = Array.length evaluations in
if expected_shard_length <> got_shard_length then
Error `Shard_length_mismatch
else
let root =
Domain.get t.domain_erasure_encoded_polynomial_length shard_index
in
let domain = Domain.build t.shard_length in
let srs_point = t.srs_verifier.shards in
if
Kate_amortized.verify
t.kate_amortized
~commitment
~srs_point
~domain
~root
~evaluations
~proof
then Ok ()
else Error `Invalid_shard
let verify_shard_multi (t : t) commitment shard_list proof_list =
let out_of_range_list =
List.filter
(fun shard -> shard.index < 0 || shard.index >= t.number_of_shards)
shard_list
in
let error_message =
String.concat
"\n"
(List.map
(fun shard ->
Printf.sprintf
"Shard index out of range: got index %d, expected index within \
range [%d, %d]."
shard.index
0
(t.number_of_shards - 1))
out_of_range_list)
in
if out_of_range_list != [] then
Error (`Shard_index_out_of_range error_message)
else
let length_mismatch_list =
let expected_shard_length = t.shard_length in
List.filter
(fun shard ->
let got_shard_length = Array.length shard.share in
expected_shard_length <> got_shard_length)
shard_list
in
if length_mismatch_list != [] then Error `Shard_length_mismatch
else
let root_list =
List.map
(fun shard ->
Domain.get t.domain_erasure_encoded_polynomial_length shard.index)
shard_list
in
let evaluations_list = List.map (fun shard -> shard.share) shard_list in
let domain = Domain.build t.shard_length in
let srs_point = t.srs_verifier.shards in
if
Kate_amortized.verify_multi
t.kate_amortized
~commitment
~srs_point
~domain
~root_list
~evaluations_list
~proof_list
then Ok ()
else Error `Invalid_shard
let prove_page t p page_index =
if page_index < 0 || page_index >= t.pages_per_slot then
Error `Page_index_out_of_range
else
let wi = Domain.get t.domain_polynomial_length page_index in
let quotient, _ =
Poly.division_xn
p
t.page_length_domain
(Scalar.negate (Scalar.pow wi (Z.of_int t.page_length_domain)))
in
commit t quotient
let verify_page t commitment ~page_index page proof =
if page_index < 0 || page_index >= t.pages_per_slot then
Error `Page_index_out_of_range
else
let expected_page_length = t.page_size in
let got_page_length = Bytes.length page in
if expected_page_length <> got_page_length then
Error `Page_length_mismatch
else
let domain = Domain.build t.page_length_domain in
let evaluations =
Array.init t.page_length_domain (function
| i when i < t.page_length - 1 ->
let dst = Bytes.create Parameters_check.scalar_bytes_amount in
Bytes.blit
page
(i * Parameters_check.scalar_bytes_amount)
dst
0
Parameters_check.scalar_bytes_amount ;
Scalar.of_bytes_exn dst
| i when i = t.page_length - 1 ->
let dst = Bytes.create t.remaining_bytes in
Bytes.blit
page
(i * Parameters_check.scalar_bytes_amount)
dst
0
t.remaining_bytes ;
Scalar.of_bytes_exn dst
| _ -> Scalar.(copy zero))
in
let root = Domain.get t.domain_polynomial_length page_index in
let srs_point = t.srs_verifier.pages in
if
Kate_amortized.verify
t.kate_amortized
~commitment
~srs_point
~domain
~root
~evaluations
~proof
then Ok ()
else Error `Invalid_page
end
include Inner
module Verifier = Inner
module Internal_for_tests = struct
let parameters_initialisation () =
Prover
{
is_fake = true;
srs_g1 = Lazy.force Srs.Internal_for_tests.fake_srs1;
srs_g2 = Lazy.force Srs.Internal_for_tests.fake_srs2;
}
let init_prover_dal () =
initialisation_parameters := parameters_initialisation ()
let init_verifier_dal () =
initialisation_parameters := Verifier {is_fake = true}
let init_verifier_dal_default () =
initialisation_parameters := Verifier {is_fake = false}
let make_dummy_shards (t : t) ~state =
Random.set_state state ;
let rec loop index seq =
if index = t.number_of_shards then seq
else
let share =
Array.init
(t.shard_length + 1 + Random.int 100)
(fun _ -> Scalar.(random ~state ()))
in
loop (index + 1) (Seq.cons {index; share} seq)
in
loop 0 Seq.empty
let polynomials_equal = Poly.equal
let page_proof_equal = Proof.equal
let alter_page_proof (proof : page_proof) = Proof.alter_proof proof
let alter_shard_proof (proof : shard_proof) = Proof.alter_proof proof
let alter_commitment_proof (proof : commitment_proof) =
Commitment_proof.alter_proof proof
let minimum_number_of_shards_to_reconstruct_slot (t : t) =
t.number_of_shards / t.redundancy_factor
let select_fft_domain = FFT.select_fft_domain
let precomputation_equal = Kate_amortized.preprocess_equal
let dummy_commitment ~state () = Commitment.random ~state ()
let dummy_page_proof ~state () = Proof.random ~state ()
let dummy_shard_proof ~state () = Proof.random ~state ()
let make_dummy_shard ~state ~index ~length =
{index; share = Array.init length (fun _ -> Scalar.(random ~state ()))}
let number_of_pages t = t.pages_per_slot
let shard_length t = t.shard_length
let dummy_polynomial ~state ~degree =
let rec nonzero () =
let res = Scalar.random ~state () in
if Scalar.is_zero res then nonzero () else res
in
Poly.init (degree + 1) (fun i ->
if i = degree then nonzero () else Scalar.random ~state ())
let srs_size_g1 t = Srs_g1.size t.kate_amortized.srs_g1
let encoded_share_size = encoded_share_size
let ensure_validity_without_srs
{redundancy_factor; slot_size; page_size; number_of_shards; _} =
Parameters_check.ensure_validity_without_srs
~redundancy_factor
~slot_size
~page_size
~number_of_shards
let ensure_validity
{redundancy_factor; slot_size; page_size; number_of_shards} =
let mode, is_fake =
match !initialisation_parameters with
| Verifier {is_fake} -> (`Verifier, is_fake)
| Prover {is_fake; _} -> (`Prover, is_fake)
in
match
ensure_validity
~is_fake
~mode
~slot_size
~page_size
~redundancy_factor
~number_of_shards
with
| Ok _ -> true
| _ -> false
let slot_as_polynomial_length = Parameters_check.slot_as_polynomial_length
end
module Config = struct
type t = Dal_config.t = {
activated : bool;
use_mock_srs_for_testing : bool;
bootstrap_peers : string list;
}
let encoding : t Data_encoding.t = Dal_config.encoding
let default = Dal_config.default
let init_verifier_dal dal_config =
let open Result_syntax in
if dal_config.activated then
let initialisation_parameters =
Verifier {is_fake = dal_config.use_mock_srs_for_testing}
in
load_parameters initialisation_parameters
else return_unit
let init_prover_dal ~find_srs_files ?(srs_size_log2 = 21) dal_config =
let open Lwt_result_syntax in
if dal_config.activated then
let* initialisation_parameters =
if dal_config.use_mock_srs_for_testing then
return (Internal_for_tests.parameters_initialisation ())
else
let*? srs_g1_path, srs_g2_path = find_srs_files () in
let* srs_g1, srs_g2 =
initialisation_parameters_from_files
~srs_g1_path
~srs_g2_path
~srs_size:(1 lsl srs_size_log2)
in
return (Prover {is_fake = false; srs_g1; srs_g2})
in
Lwt.return (load_parameters initialisation_parameters)
else return_unit
end