-
Notifications
You must be signed in to change notification settings - Fork 550
/
Copy pathpickles.ml
2595 lines (2380 loc) · 104 KB
/
pickles.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
(** Pickles implementation *)
(** See documentation of the {!Mina_wire_types} library *)
module Wire_types = Mina_wire_types.Pickles
module Make_sig (A : Wire_types.Types.S) = struct
module type S =
Pickles_intf.S
with type Side_loaded.Verification_key.Stable.V2.t =
A.Side_loaded.Verification_key.V2.t
and type ('a, 'b) Proof.t = ('a, 'b) A.Proof.t
end
module Make_str (_ : Wire_types.Concrete) = struct
module Endo = Endo
module P = Proof
module type Statement_intf = Intf.Statement
module type Statement_var_intf = Intf.Statement_var
module type Statement_value_intf = Intf.Statement_value
module Common = Common
module Scalar_challenge = Scalar_challenge
module SC = Scalar_challenge
open Core_kernel
open Async_kernel
open Import
open Pickles_types
open Hlist
open Common
open Backend
module Backend = Backend
module Sponge_inputs = Sponge_inputs
module Util = Util
module Tick_field_sponge = Tick_field_sponge
module Impls = Impls
module Inductive_rule = Inductive_rule
module Tag = Tag
module Types_map = Types_map
module Dirty = Dirty
module Cache_handle = Cache_handle
module Step_main_inputs = Step_main_inputs
module Step_verifier = Step_verifier
module Proof_cache = Proof_cache
module Cache = Cache
module Storables = Compile.Storables
module Ro = Ro
type chunking_data = Verify.Instance.chunking_data =
{ num_chunks : int; domain_size : int; zk_rows : int }
let verify_promise = Verify.verify
let verify max_proofs_verified statement key proofs =
verify_promise max_proofs_verified statement key proofs
|> Promise.to_deferred
(* This file (as you can see from the mli) defines a compiler which turns an inductive
definition of a set into an inductive SNARK system for proving using those rules.
The two ingredients we use are two SNARKs.
- A step based SNARK for a field Fp, using the group G1/Fq (whose scalar field is Fp)
- A DLOG based SNARK for a field Fq, using the group G/Fp (whose scalar field is Fq)
For convenience in this discussion, let's define
(F_0, G_0) := (Fp, G1)
(F_1, G_1) := (Fq, G)
So ScalarField(G_i) = F_i and G_i / F_{1-i}.
An inductive set A is defined by a sequence of inductive rules.
An inductive rule is intuitively described by something of the form
a1 ∈ A1, ..., an ∈ An
f [ a0, ... a1 ] a
----------------------
a ∈ A
where f is a snarky function defined over an Impl with Field.t = Fp
and each Ai is itself an inductive rule (possibly equal to A itself).
a1, ..., an can be seen as previous statements, i.e.
prev_statement_1 ∈ A1, ..., prev_statement_n ∈ An
f [ prev_statement_1; ...; prev_statement_n ] new_statement = true
-------------------------------------------------------------------
new_statement ∈ A
In the case of a blockchain, the description of the sets A1, ..., An, A can
be blockchain state, and f would be a function updating the state:
prev_blockchain_state ∈ A
update_blockchain_state [prev_blockchain_state] new_blockchain_state = true
---------------------------------------------------------------------------
new_blockchain_state ∈ A
We pursue the "step" then "wrap" approach for proof composition.
The main source of complexity is that we must "wrap" proofs whose verifiers are
slightly different.
The main sources of complexity are twofold:
1. Each SNARK verifier includes group operations and scalar field operations.
This is problematic because the group operations use the base field, which is
not equal to the scalar field.
Schematically, from the circuit point-of-view, we can say a proof is
- a sequence of F_0 elements xs_0
- a sequence of F_1 elements xs_1
and a verifier is a pair of "snarky functions"
- check_0 : F_0 list -> F_1 list -> unit which uses the Impl with Field.t = F_0
- check_1 : F_0 list -> F_1 list -> unit which uses the Impl with Field.t = F_1
- subset_00 : 'a list -> 'a list
- subset_01 : 'a list -> 'a list
- subset_10 : 'a list -> 'a list
- subset_11 : 'a list -> 'a list
and a proof verifies if
( check_0 (subset_00 xs_0) (subset_01 xs_1) ;
check_1 (subset_10 xs_0) (subset_11 xs_1) )
When verifying a proof, we perform the parts of the verifier involving group operations
and expose as public input the scalar-field elements we need to perform the final checks.
In the F_0 circuit, we witness xs_0 and xs_1,
execute `check_0 (subset_00 xs_0) (subset_01 xs_1)` and
expose `subset_10 xs_0` and `subset_11 xs_1` as public inputs.
So the "public inputs" contain within them an "unfinalized proof".
Then, the next time we verify that proof within an F_1 circuit we "finalize" those
unfinalized proofs by running `check_1 xs_0_subset xs_1_subset`.
I didn't implement it exactly this way (although in retrospect probably I should have) but
that's the basic idea.
**The complexity this causes:**
When you prove a rule that includes k recursive verifications, you expose k unfinalized
proofs. So, the shape of a statement depends on how many "predecessor statements" it has
or in other words, how many verifications were performed within it.
Say we have an inductive set given by inductive rules R_1, ... R_n such that
each rule R_i has k_i predecessor statements.
In the "wrap" circuit, we must be able to verify a proof coming from any of the R_i.
So, we must pad the statement for the proof we're wrapping to have `max_i k_i`
unfinalized proof components.
2. The verifier for each R_i looks a little different depending on the complexity of the "step"
circuit corresponding to R_i has. Namely, it is dependent on the "domains" H and K for this
circuit.
So, when the "wrap" circuit proves the statement,
"there exists some index i in 1,...,n and a proof P such that verifies(P)"
"verifies(P)" must also take the index "i", compute the correct domain sizes correspond to rule "i"
and use *that* in the "verifies" computation.
*)
open Kimchi_backend
module Proof = P
module Statement_with_proof = struct
type ('s, 'max_width, _) t =
(* TODO: use Max local max proofs verified instead of max_width *)
('max_width, 'max_width) Proof.t
end
module Verification_key = struct
include Verification_key
module Id = struct
include Cache.Wrap.Key.Verification
let dummy_id = Type_equal.Id.(uid (create ~name:"dummy" sexp_of_opaque))
let dummy : unit -> t =
let header =
{ Snark_keys_header.header_version = Snark_keys_header.header_version
; kind = { type_ = "verification key"; identifier = "dummy" }
; constraint_constants =
{ sub_windows_per_window = 0
; ledger_depth = 0
; work_delay = 0
; block_window_duration_ms = 0
; transaction_capacity = Log_2 0
; pending_coinbase_depth = 0
; coinbase_amount = Unsigned.UInt64.of_int 0
; supercharged_coinbase_factor = 0
; account_creation_fee = Unsigned.UInt64.of_int 0
; fork = None
}
; length = 0
; constraint_system_hash = ""
; identifying_hash = ""
}
in
let t = lazy (dummy_id, header, Md5.digest_string "") in
fun () -> Lazy.force t
end
(* TODO: Make async *)
let load ~cache id =
Key_cache.Sync.read cache
(Key_cache.Sync.Disk_storable.of_binable Id.to_string
(module Verification_key.Stable.Latest) )
id
|> Deferred.return
end
module type Proof_intf = Compile.Proof_intf
module Prover = Compile.Prover
module Side_loaded = struct
module V = Verification_key
module Verification_key = struct
include Side_loaded_verification_key
let to_input (t : t) =
to_input ~field_of_int:Impls.Step.Field.Constant.of_int t
let of_compiled_promise tag : t Promise.t =
let d = Types_map.lookup_compiled tag.Tag.id in
let%bind.Promise wrap_key = Lazy.force d.wrap_key in
let%map.Promise wrap_vk = Lazy.force d.wrap_vk in
let actual_wrap_domain_size =
Common.actual_wrap_domain_size
~log_2_domain_size:wrap_vk.domain.log_size_of_group
in
( { wrap_vk = Some wrap_vk
; wrap_index =
Plonk_verification_key_evals.map wrap_key ~f:(fun x -> x.(0))
; max_proofs_verified =
Pickles_base.Proofs_verified.of_nat_exn
(Nat.Add.n d.max_proofs_verified)
; actual_wrap_domain_size
}
: t )
let of_compiled tag = of_compiled_promise tag |> Promise.to_deferred
module Max_width = Width.Max
end
let in_circuit tag vk =
Types_map.set_ephemeral tag { index = `In_circuit vk }
let in_prover tag vk = Types_map.set_ephemeral tag { index = `In_prover vk }
let create ~name ~max_proofs_verified ~feature_flags ~typ =
Types_map.add_side_loaded ~name
{ max_proofs_verified
; public_input = typ
; branches = Verification_key.Max_branches.n
; feature_flags =
Plonk_types.(Features.to_full ~or_:Opt.Flag.( ||| ) feature_flags)
; num_chunks = Plonk_checks.num_chunks_by_default
; zk_rows = Plonk_checks.zk_rows_by_default
}
module Proof = struct
include Proof.Proofs_verified_max
let of_proof : _ Proof.t -> t = Wrap_hack.pad_proof
end
let verify_promise (type t) ~(typ : (_, t) Impls.Step.Typ.t)
(ts : (Verification_key.t * t * Proof.t) list) =
let m =
( module struct
type nonrec t = t
let to_field_elements =
let (Typ typ) = typ in
fun x -> fst (typ.value_to_fields x)
end : Intf.Statement_value
with type t = t )
in
(* TODO: This should be the actual max width on a per proof basis *)
let max_proofs_verified =
( module Verification_key.Max_width : Nat.Intf
with type n = Verification_key.Max_width.n )
in
with_return (fun { return } ->
List.map ts ~f:(fun (vk, x, p) ->
let vk : V.t =
{ commitments = vk.wrap_index
; index =
( match vk.wrap_vk with
| None ->
return
(Promise.return
(Or_error.errorf
"Pickles.verify: wrap_vk not found" ) )
| Some x ->
x )
; data =
(* This isn't used in verify_heterogeneous, so we can leave this dummy *)
{ constraints = 0 }
}
in
Verify.Instance.T (max_proofs_verified, m, None, vk, x, p) )
|> Verify.verify_heterogenous )
let verify ~typ ts = verify_promise ~typ ts |> Promise.to_deferred
let srs_precomputation () : unit =
let srs = Tock.Keypair.load_urs () in
List.iter [ 0; 1; 2 ] ~f:(fun i ->
Kimchi_bindings.Protocol.SRS.Fq.add_lagrange_basis srs
(Domain.log2_size (Common.wrap_domains ~proofs_verified:i).h) )
end
let compile_with_wrap_main_override_promise =
Compile.compile_with_wrap_main_override_promise
let compile_promise ?self ?cache ?storables ?proof_cache ?disk_keys
?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ ~branches
~max_proofs_verified ~name ?constraint_constants ~choices () =
compile_with_wrap_main_override_promise ?self ?cache ?storables ?proof_cache
?disk_keys ?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ
~branches ~max_proofs_verified ~name ?constraint_constants ~choices ()
let compile ?self ?cache ?storables ?proof_cache ?disk_keys
?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ ~branches
~max_proofs_verified ~name ?constraint_constants ~choices () =
let choices ~self =
let choices = choices ~self in
let rec go :
type a b c d e f g h i j.
(a, b, c, d, e, f, g, h, i, j) H4_6.T(Inductive_rule).t
-> (a, b, c, d, e, f, g, h, i, j) H4_6.T(Inductive_rule.Promise).t =
function
| [] ->
[]
| { identifier; prevs; main; feature_flags } :: rest ->
{ identifier
; prevs
; main = (fun x -> Promise.return (main x))
; feature_flags
}
:: go rest
in
go choices
in
let self, cache_handle, proof_module, provers =
compile_promise ?self ?cache ?storables ?proof_cache ?disk_keys
?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ ~branches
~max_proofs_verified ~name ?constraint_constants ~choices ()
in
let rec adjust_provers :
type a1 a2 a3 s1 s2_inner.
(a1, a2, a3, s1, s2_inner Promise.t) H3_2.T(Prover).t
-> (a1, a2, a3, s1, s2_inner Deferred.t) H3_2.T(Prover).t = function
| [] ->
[]
| prover :: tl ->
(fun ?handler public_input ->
Promise.to_deferred (prover ?handler public_input) )
:: adjust_provers tl
in
(self, cache_handle, proof_module, adjust_provers provers)
let compile_async ?self ?cache ?storables ?proof_cache ?disk_keys
?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ ~branches
~max_proofs_verified ~name ?constraint_constants ~choices () =
let choices ~self =
let choices = choices ~self in
let rec go :
type a b c d e f g h i j.
(a, b, c, d, e, f, g, h, i, j) H4_6.T(Inductive_rule.Deferred).t
-> (a, b, c, d, e, f, g, h, i, j) H4_6.T(Inductive_rule.Promise).t =
function
| [] ->
[]
| { identifier; prevs; main; feature_flags } :: rest ->
{ identifier
; prevs
; main =
(fun x ->
Promise.create (fun callback ->
Deferred.don't_wait_for
(let%map res = main x in
callback res ) ) )
; feature_flags
}
:: go rest
in
go choices
in
let self, cache_handle, proof_module, provers =
compile_promise ?self ?cache ?storables ?proof_cache ?disk_keys
?override_wrap_domain ?num_chunks ~public_input ~auxiliary_typ ~branches
~max_proofs_verified ~name ?constraint_constants ~choices ()
in
let rec adjust_provers :
type a1 a2 a3 s1 s2_inner.
(a1, a2, a3, s1, s2_inner Promise.t) H3_2.T(Prover).t
-> (a1, a2, a3, s1, s2_inner Deferred.t) H3_2.T(Prover).t = function
| [] ->
[]
| prover :: tl ->
(fun ?handler public_input ->
Promise.to_deferred (prover ?handler public_input) )
:: adjust_provers tl
in
(self, cache_handle, proof_module, adjust_provers provers)
module Provers = H3_2.T (Prover)
module Proof0 = Proof
let%test_module "test no side-loaded" =
( module struct
let () = Tock.Keypair.set_urs_info []
let () = Tick.Keypair.set_urs_info []
let () = Backtrace.elide := false
open Impls.Step
let () = Snarky_backendless.Snark0.set_eval_constraints true
(* Currently, a circuit must have at least 1 of every type of constraint. *)
let dummy_constraints () =
Impl.(
let x =
exists Field.typ ~compute:(fun () -> Field.Constant.of_int 3)
in
let g =
exists Step_main_inputs.Inner_curve.typ ~compute:(fun _ ->
Tick.Inner_curve.(to_affine_exn one) )
in
ignore
( SC.to_field_checked'
(module Impl)
~num_bits:16
(Kimchi_backend_common.Scalar_challenge.create x)
: Field.t * Field.t * Field.t ) ;
ignore
( Step_main_inputs.Ops.scale_fast g ~num_bits:5 (Shifted_value x)
: Step_main_inputs.Inner_curve.t ) ;
ignore
( Step_main_inputs.Ops.scale_fast g ~num_bits:5 (Shifted_value x)
: Step_main_inputs.Inner_curve.t ) ;
ignore
( Step_verifier.Scalar_challenge.endo g ~num_bits:4
(Kimchi_backend_common.Scalar_challenge.create x)
: Field.t * Field.t ) )
module No_recursion = struct
let[@warning "-45"] tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise () ~public_input:(Input Field.typ)
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N0)
~name:"blockchain-snark"
~choices:(fun ~self:_ ->
[ { identifier = "main"
; prevs = []
; feature_flags = Plonk_types.Features.none_bool
; main =
(fun { public_input = self } ->
dummy_constraints () ;
Field.Assert.equal self Field.zero ;
Promise.return
{ Inductive_rule.previous_proof_statements = []
; public_output = ()
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example =
let (), (), b0 =
Common.time "b0" (fun () ->
Promise.block_on_async_exn (fun () -> step Field.Constant.zero) )
in
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (Field.Constant.zero, b0) ] ) ) ;
(Field.Constant.zero, b0)
let _example_input, _example_proof = example
end
module No_recursion_return = struct
let[@warning "-45"] tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise () ~public_input:(Output Field.typ)
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N0)
~name:"blockchain-snark"
~choices:(fun ~self:_ ->
[ { identifier = "main"
; prevs = []
; feature_flags = Plonk_types.Features.none_bool
; main =
(fun _ ->
dummy_constraints () ;
Promise.return
{ Inductive_rule.previous_proof_statements = []
; public_output = Field.zero
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example =
let res, (), b0 =
Common.time "b0" (fun () ->
Promise.block_on_async_exn (fun () -> step ()) )
in
assert (Field.Constant.(equal zero) res) ;
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (res, b0) ] ) ) ;
(res, b0)
let _example_input, _example_proof = example
end
[@@@warning "-60"]
module Simple_chain = struct
type _ Snarky_backendless.Request.t +=
| Prev_input : Field.Constant.t Snarky_backendless.Request.t
| Proof : (Nat.N1.n, Nat.N1.n) Proof.t Snarky_backendless.Request.t
let handler (prev_input : Field.Constant.t) (proof : _ Proof.t)
(Snarky_backendless.Request.With { request; respond }) =
match request with
| Prev_input ->
respond (Provide prev_input)
| Proof ->
respond (Provide proof)
| _ ->
respond Unhandled
let[@warning "-45"] _tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise () ~public_input:(Input Field.typ)
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N1)
~name:"blockchain-snark"
~choices:(fun ~self ->
[ { identifier = "main"
; prevs = [ self ]
; feature_flags = Plonk_types.Features.none_bool
; main =
(fun { public_input = self } ->
let prev =
exists Field.typ ~request:(fun () -> Prev_input)
in
let proof =
exists (Typ.Internal.ref ()) ~request:(fun () ->
Proof )
in
let is_base_case = Field.equal Field.zero self in
let proof_must_verify = Boolean.not is_base_case in
let self_correct = Field.(equal (one + prev) self) in
Boolean.Assert.any [ self_correct; is_base_case ] ;
Promise.return
{ Inductive_rule.previous_proof_statements =
[ { public_input = prev
; proof
; proof_must_verify
}
]
; public_output = ()
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example =
let s_neg_one = Field.Constant.(negate one) in
let b_neg_one : (Nat.N1.n, Nat.N1.n) Proof0.t =
Proof0.dummy Nat.N1.n Nat.N1.n Nat.N1.n ~domain_log2:14
in
let (), (), b0 =
Common.time "b0" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:(handler s_neg_one b_neg_one)
Field.Constant.zero ) )
in
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (Field.Constant.zero, b0) ] ) ) ;
let (), (), b1 =
Common.time "b1" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:(handler Field.Constant.zero b0)
Field.Constant.one ) )
in
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (Field.Constant.one, b1) ] ) ) ;
(Field.Constant.one, b1)
let _example_input, _example_proof = example
end
module Tree_proof = struct
type _ Snarky_backendless.Request.t +=
| No_recursion_input : Field.Constant.t Snarky_backendless.Request.t
| No_recursion_proof :
(Nat.N0.n, Nat.N0.n) Proof.t Snarky_backendless.Request.t
| Recursive_input : Field.Constant.t Snarky_backendless.Request.t
| Recursive_proof :
(Nat.N2.n, Nat.N2.n) Proof.t Snarky_backendless.Request.t
let handler
((no_recursion_input, no_recursion_proof) :
Field.Constant.t * _ Proof.t )
((recursion_input, recursion_proof) : Field.Constant.t * _ Proof.t)
(Snarky_backendless.Request.With { request; respond }) =
match request with
| No_recursion_input ->
respond (Provide no_recursion_input)
| No_recursion_proof ->
respond (Provide no_recursion_proof)
| Recursive_input ->
respond (Provide recursion_input)
| Recursive_proof ->
respond (Provide recursion_proof)
| _ ->
respond Unhandled
let[@warning "-45"] _tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise () ~public_input:(Input Field.typ)
~override_wrap_domain:Pickles_base.Proofs_verified.N1
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N2)
~name:"blockchain-snark"
~choices:(fun ~self ->
[ { identifier = "main"
; feature_flags = Plonk_types.Features.none_bool
; prevs = [ No_recursion.tag; self ]
; main =
(fun { public_input = self } ->
let no_recursive_input =
exists Field.typ ~request:(fun () ->
No_recursion_input )
in
let no_recursive_proof =
exists (Typ.Internal.ref ()) ~request:(fun () ->
No_recursion_proof )
in
let prev =
exists Field.typ ~request:(fun () ->
Recursive_input )
in
let prev_proof =
exists (Typ.Internal.ref ()) ~request:(fun () ->
Recursive_proof )
in
let is_base_case = Field.equal Field.zero self in
let proof_must_verify = Boolean.not is_base_case in
let self_correct = Field.(equal (one + prev) self) in
Boolean.Assert.any [ self_correct; is_base_case ] ;
Promise.return
{ Inductive_rule.previous_proof_statements =
[ { public_input = no_recursive_input
; proof = no_recursive_proof
; proof_must_verify = Boolean.true_
}
; { public_input = prev
; proof = prev_proof
; proof_must_verify
}
]
; public_output = ()
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example1, example2 =
let s_neg_one = Field.Constant.(negate one) in
let b_neg_one : (Nat.N2.n, Nat.N2.n) Proof0.t =
Proof0.dummy Nat.N2.n Nat.N2.n Nat.N2.n ~domain_log2:15
in
let (), (), b0 =
Common.time "tree b0" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:
(handler No_recursion.example (s_neg_one, b_neg_one))
Field.Constant.zero ) )
in
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (Field.Constant.zero, b0) ] ) ) ;
let (), (), b1 =
Common.time "tree b1" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:
(handler No_recursion.example (Field.Constant.zero, b0))
Field.Constant.one ) )
in
((Field.Constant.zero, b0), (Field.Constant.one, b1))
let examples = [ example1; example2 ]
let _example1_input, _example_proof = example1
let _example2_input, _example2_proof = example2
end
let%test_unit "verify" =
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Tree_proof.Proof.verify_promise Tree_proof.examples ) )
module Tree_proof_return = struct
type _ Snarky_backendless.Request.t +=
| Is_base_case : bool Snarky_backendless.Request.t
| No_recursion_input : Field.Constant.t Snarky_backendless.Request.t
| No_recursion_proof :
(Nat.N0.n, Nat.N0.n) Proof.t Snarky_backendless.Request.t
| Recursive_input : Field.Constant.t Snarky_backendless.Request.t
| Recursive_proof :
(Nat.N2.n, Nat.N2.n) Proof.t Snarky_backendless.Request.t
let handler (is_base_case : bool)
((no_recursion_input, no_recursion_proof) :
Field.Constant.t * _ Proof.t )
((recursion_input, recursion_proof) : Field.Constant.t * _ Proof.t)
(Snarky_backendless.Request.With { request; respond }) =
match request with
| Is_base_case ->
respond (Provide is_base_case)
| No_recursion_input ->
respond (Provide no_recursion_input)
| No_recursion_proof ->
respond (Provide no_recursion_proof)
| Recursive_input ->
respond (Provide recursion_input)
| Recursive_proof ->
respond (Provide recursion_proof)
| _ ->
respond Unhandled
let[@warning "-45"] _tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise () ~public_input:(Output Field.typ)
~override_wrap_domain:Pickles_base.Proofs_verified.N1
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N2)
~name:"blockchain-snark"
~choices:(fun ~self ->
[ { identifier = "main"
; feature_flags = Plonk_types.Features.none_bool
; prevs = [ No_recursion_return.tag; self ]
; main =
(fun { public_input = () } ->
let no_recursive_input =
exists Field.typ ~request:(fun () ->
No_recursion_input )
in
let no_recursive_proof =
exists (Typ.Internal.ref ()) ~request:(fun () ->
No_recursion_proof )
in
let prev =
exists Field.typ ~request:(fun () ->
Recursive_input )
in
let prev_proof =
exists (Typ.Internal.ref ()) ~request:(fun () ->
Recursive_proof )
in
let is_base_case =
exists Boolean.typ ~request:(fun () -> Is_base_case)
in
let proof_must_verify = Boolean.not is_base_case in
let self =
Field.(
if_ is_base_case ~then_:zero ~else_:(one + prev) )
in
Promise.return
{ Inductive_rule.previous_proof_statements =
[ { public_input = no_recursive_input
; proof = no_recursive_proof
; proof_must_verify = Boolean.true_
}
; { public_input = prev
; proof = prev_proof
; proof_must_verify
}
]
; public_output = self
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example1, example2 =
let s_neg_one = Field.Constant.(negate one) in
let b_neg_one : (Nat.N2.n, Nat.N2.n) Proof0.t =
Proof0.dummy Nat.N2.n Nat.N2.n Nat.N2.n ~domain_log2:15
in
let s0, (), b0 =
Common.time "tree b0" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:
(handler true No_recursion_return.example
(s_neg_one, b_neg_one) )
() ) )
in
assert (Field.Constant.(equal zero) s0) ;
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ (s0, b0) ] ) ) ;
let s1, (), b1 =
Common.time "tree b1" (fun () ->
Promise.block_on_async_exn (fun () ->
step
~handler:
(handler false No_recursion_return.example (s0, b0))
() ) )
in
assert (Field.Constant.(equal one) s1) ;
((s0, b0), (s1, b1))
let examples = [ example1; example2 ]
let _example1_input, _example1_proof = example1
let _example2_input, _example2_proof = example2
end
let%test_unit "verify" =
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Tree_proof_return.Proof.verify_promise Tree_proof_return.examples )
)
module Add_one_return = struct
let[@warning "-45"] _tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise ()
~public_input:(Input_and_output (Field.typ, Field.typ))
~auxiliary_typ:Typ.unit
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N0)
~name:"blockchain-snark"
~choices:(fun ~self:_ ->
[ { identifier = "main"
; feature_flags = Plonk_types.Features.none_bool
; prevs = []
; main =
(fun { public_input = x } ->
dummy_constraints () ;
Promise.return
{ Inductive_rule.previous_proof_statements = []
; public_output = Field.(add one) x
; auxiliary_output = ()
} )
}
] ) )
module Proof = (val p)
let example =
let input = Field.Constant.of_int 42 in
let res, (), b0 =
Common.time "b0" (fun () ->
Promise.block_on_async_exn (fun () -> step input) )
in
assert (Field.Constant.(equal (of_int 43)) res) ;
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ ((input, res), b0) ] ) ) ;
((input, res), b0)
let _example_input, _example_proof = example
end
module Auxiliary_return = struct
let[@warning "-45"] _tag, _, p, Provers.[ step ] =
Common.time "compile" (fun () ->
compile_promise ()
~public_input:(Input_and_output (Field.typ, Field.typ))
~auxiliary_typ:Field.typ
~branches:(module Nat.N1)
~max_proofs_verified:(module Nat.N0)
~name:"blockchain-snark"
~choices:(fun ~self:_ ->
[ { identifier = "main"
; feature_flags = Plonk_types.Features.none_bool
; prevs = []
; main =
(fun { public_input = input } ->
dummy_constraints () ;
let sponge =
Step_main_inputs.Sponge.create
Step_main_inputs.sponge_params
in
let blinding_value =
exists Field.typ ~compute:Field.Constant.random
in
Step_main_inputs.Sponge.absorb sponge (`Field input) ;
Step_main_inputs.Sponge.absorb sponge
(`Field blinding_value) ;
let result = Step_main_inputs.Sponge.squeeze sponge in
Promise.return
{ Inductive_rule.previous_proof_statements = []
; public_output = result
; auxiliary_output = blinding_value
} )
}
] ) )
module Proof = (val p)
let example =
let input = Field.Constant.of_int 42 in
let result, blinding_value, b0 =
Common.time "b0" (fun () ->
Promise.block_on_async_exn (fun () -> step input) )
in
let sponge =
Tick_field_sponge.Field.create Tick_field_sponge.params
in
Tick_field_sponge.Field.absorb sponge input ;
Tick_field_sponge.Field.absorb sponge blinding_value ;
let result' = Tick_field_sponge.Field.squeeze sponge in
assert (Field.Constant.equal result result') ;
Or_error.ok_exn
(Promise.block_on_async_exn (fun () ->
Proof.verify_promise [ ((input, result), b0) ] ) ) ;
((input, result), b0)
let _example_input, _example_proof = example
end
end )
let%test_module "test uncorrelated bulletproof_challenges" =
( module struct
let () = Backtrace.elide := false
let () = Snarky_backendless.Snark0.set_eval_constraints true
module Statement = struct
type t = unit
let to_field_elements () = [||]
end
module A = Statement
module A_value = Statement
let typ = Impls.Step.Typ.unit
module Branches = Nat.N1
module Max_proofs_verified = Nat.N2
let constraint_constants : Snark_keys_header.Constraint_constants.t =
{ sub_windows_per_window = 0
; ledger_depth = 0
; work_delay = 0
; block_window_duration_ms = 0
; transaction_capacity = Log_2 0
; pending_coinbase_depth = 0
; coinbase_amount = Unsigned.UInt64.of_int 0
; supercharged_coinbase_factor = 0
; account_creation_fee = Unsigned.UInt64.of_int 0
; fork = None
}
let tag =
let tagname = "" in
Tag.create ~kind:Compiled tagname
let rule : _ Inductive_rule.Promise.t =
let open Impls.Step in
{ identifier = "main"
; prevs = [ tag; tag ]