-
Notifications
You must be signed in to change notification settings - Fork 72
/
snark0.ml
1445 lines (1100 loc) · 40.7 KB
/
snark0.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
open Core_kernel
module Cvar0 = Cvar
module Bignum_bigint = Bigint
exception Runtime_error of string list * exn * string
module Runner = Checked_runner
let set_eval_constraints b = Runner.eval_constraints := b
module Make_basic
(Backend : Backend_extended.S)
(Checked : Checked_intf.Extended with type field = Backend.Field.t)
(As_prover : As_prover0.Extended with type field := Backend.Field.t)
(Ref : As_prover_ref.S
with module Types := Checked.Types
and type 'f field := Backend.Field.t
and type ('a, 'f) checked := 'a Checked.t)
(Runner : Runner.S
with module Types := Checked.Types
with type field := Backend.Field.t
and type cvar := Backend.Cvar.t
and type constr := Backend.Constraint.t option
and type r1cs := Backend.R1CS_constraint_system.t) =
struct
open Backend
module Checked_S = Checked_intf.Unextend (Checked)
include Runners.Make (Backend) (Checked) (As_prover) (Runner)
module Bigint = Bigint
module Field0 = Field
module Cvar = Cvar
module Constraint = Constraint
module Handler = struct
type t = Request.request -> Request.response
end
module Typ = struct
include Types.Typ.T
module T = Typ.Make (Checked_S)
include T.T
type ('var, 'value) t = ('var, 'value, Field.t) T.t
let unit : (unit, unit) t = unit ()
let field : (Cvar.t, Field.t) t = field ()
end
let constant (Typ typ : _ Typ.t) x =
let fields, aux = typ.value_to_fields x in
let field_vars = Array.map fields ~f:(fun x -> Cvar0.Constant x) in
typ.var_of_fields (field_vars, aux)
module As_prover = struct
include As_prover
type 'a as_prover = 'a t
module Ref = Ref
end
module Handle = struct
include Handle
let value = As_prover.Handle.value
end
module Checked = struct
include (
Checked :
Checked_intf.Extended
with module Types := Checked.Types
with type field := field )
let perform req = request_witness Typ.unit req
module Runner = Runner
type run_state = Runner.run_state
include Utils.Make (Backend) (Checked) (As_prover) (Runner)
module Control = struct end
let two_to_the n =
let rec go acc i =
if i = 0 then acc else go (Field0.add acc acc) (i - 1)
in
go Field0.one n
type _ Request.t += Choose_preimage : Field.t * int -> bool list Request.t
let choose_preimage_unchecked v ~length =
exists
(Typ.list Boolean.typ ~length)
~request:
As_prover.(map (read_var v) ~f:(fun x -> Choose_preimage (x, length)))
~compute:
(let open As_prover.Let_syntax in
let%map x = As_prover.read_var v in
let x = Bigint.of_field x in
List.init length ~f:(fun i -> Bigint.test_bit x i))
let packing_sum (bits : Boolean.var list) =
let ts, _ =
List.fold_left bits ~init:([], Field.one) ~f:(fun (acc, c) v ->
((c, (v :> Cvar.t)) :: acc, Field.add c c) )
in
Cvar.linear_combination ts
let choose_preimage (v : Cvar.t) ~length : Boolean.var list t =
let open Let_syntax in
let%bind bits = choose_preimage_unchecked v ~length in
let lc = packing_sum bits in
let%map () =
assert_r1cs ~label:"Choose_preimage" lc (Cvar.constant Field.one) v
in
bits
let choose_preimage_flagged (v : Cvar.t) ~length =
let open Let_syntax in
let%bind bits = choose_preimage_unchecked v ~length in
let lc = packing_sum bits in
let%map success = equal lc v in
(bits, `Success success)
module List =
Monad_sequence.List
(Checked)
(struct
type t = Boolean.var
include Boolean
end)
module Array =
Monad_sequence.Array
(Checked)
(struct
type t = Boolean.var
let any = Boolean.Array.any
let all = Boolean.Array.all
end)
end
module Cvar1 = struct
include Cvar
let project =
let two = Field.of_int 2 in
fun (vars : Checked.Boolean.var list) ->
let rec go res = function
| [] ->
res
| v :: vs ->
go Cvar0.(Add (v, Scale (two, res))) vs
in
match List.rev (vars :> Cvar.t list) with
| [] ->
Cvar0.Constant Field.zero
| v :: vs ->
go v vs
let pack vars =
assert (List.length vars < Field.size_in_bits) ;
project vars
let unpack v ~length =
assert (length < Field.size_in_bits) ;
Checked.choose_preimage v ~length
let unpack_flagged v ~length =
assert (length < Field.size_in_bits) ;
Checked.choose_preimage_flagged v ~length
end
module Field = struct
include Field0
let gen =
Quickcheck.Generator.map
Bignum_bigint.(gen_incl zero (size - one))
~f:(fun x -> Bigint.(to_field (of_bignum_bigint x)))
let gen_incl lo hi =
let lo_bigint = Bigint.(to_bignum_bigint @@ of_field lo) in
let hi_bigint = Bigint.(to_bignum_bigint @@ of_field hi) in
Quickcheck.Generator.map
Bignum_bigint.(gen_incl lo_bigint hi_bigint)
~f:(fun x -> Bigint.(to_field (of_bignum_bigint x)))
let gen_uniform =
Quickcheck.Generator.map
Bignum_bigint.(gen_uniform_incl zero (size - one))
~f:(fun x -> Bigint.(to_field (of_bignum_bigint x)))
let gen_uniform_incl lo hi =
let lo_bigint = Bigint.(to_bignum_bigint @@ of_field lo) in
let hi_bigint = Bigint.(to_bignum_bigint @@ of_field hi) in
Quickcheck.Generator.map
Bignum_bigint.(gen_uniform_incl lo_bigint hi_bigint)
~f:(fun x -> Bigint.(to_field (of_bignum_bigint x)))
let typ = Typ.field
module Var = Cvar1
let parity x = Bigint.(test_bit (of_field x) 0)
module Checked = struct
include Cvar1
let equal = Checked.equal
let mul x y = Checked.mul ~label:"Field.Checked.mul" x y
let square x = Checked.square ~label:"Field.Checked.square" x
let div x y = Checked.div ~label:"Field.Checked.div" x y
let inv x = Checked.inv ~label:"Field.Checked.inv" x
let sqrt (x : Cvar.t) : Cvar.t Checked.t =
match x with
| Constant x ->
Checked.return (Cvar.constant (Field.sqrt x))
| _ ->
let open Checked in
let open Let_syntax in
let%bind y =
exists ~compute:As_prover.(map (read_var x) ~f:Field.sqrt) typ
in
let%map () = assert_square y x in
y
let quadratic_nonresidue =
lazy
(let rec go i =
let x = Field.of_int i in
if not (Field.is_square x) then x else go Int.(i + 1)
in
go 2 )
(* The trick here is the following.
Let beta be a known non-square.
x is not a square iff beta*x is a square
So we guess the result [is_square] and y a sqrt of one of {x, beta*x} and assert
y * y = is_square * x + (1 - is_square) * (beta * x)
which, letting B = beta*x holds iff
y * y
= is_square * x + B - is_square * B
= is_square * (x - B) + B
*)
let sqrt_check x =
let open Checked in
let open Let_syntax in
let%bind is_square =
exists
~compute:As_prover.(map (read_var x) ~f:Field.is_square)
Boolean.typ
in
let%bind y =
exists typ
~compute:
As_prover.(
Let_syntax.(
let%map is_square = read Boolean.typ is_square
and x = read_var x in
if is_square then Field.sqrt x
else Field.(sqrt (Lazy.force quadratic_nonresidue * x))))
in
let b = scale x (Lazy.force quadratic_nonresidue) in
let%bind t = mul (is_square :> Var.t) (x - b) in
let%map () = assert_square y (t + b) in
(y, is_square)
let is_square x =
let open Checked.Let_syntax in
let%map _, b = sqrt_check x in
b
let%test_unit "is_square" =
let x = Field.random () in
let typf = Typ.field in
let x2 = Field.square x in
assert (Field.(equal (x * x) x2)) ;
let run elt =
let answer =
run_and_check
(Checked.map
~f:(As_prover.read Checked.Boolean.typ)
Checked.(
Let_syntax.(
let%bind x = exists typf ~compute:(As_prover.return elt) in
is_square x)) )
|> Or_error.ok_exn
in
answer
in
assert (run x2) ;
assert (not (run (Field.mul (Lazy.force quadratic_nonresidue) x2)))
let choose_preimage_var = Checked.choose_preimage
type comparison_result =
{ less : Checked.Boolean.var; less_or_equal : Checked.Boolean.var }
let if_ = Checked.if_
let compare ~bit_length a b =
(* Overview of the logic:
let n = bit_length
We have 0 <= a < 2^n, 0 <= b < 2^n, and so
-2^n < b - a < 2^n
If (b - a) >= 0, then
2^n <= 2^n + b - a < 2^{n+1},
and so the n-th bit must be set.
If (b - a) < 0 then
0 < 2^n + b - a < 2^n
and so the n-th bit must not be set.
Thus, we can use the n-th bit of 2^n + b - a to determine whether
(b - a) >= 0 <-> a <= b.
We also need that the maximum value
2^n + (2^n - 1) - 0 = 2^{n+1} - 1
fits inside the field, so for the max field element f,
2^{n+1} - 1 <= f -> n+1 <= log2(f) = size_in_bits - 1
*)
assert (Int.(bit_length <= size_in_bits - 2)) ;
let open Checked in
let open Let_syntax in
[%with_label_ "compare"] (fun () ->
let alpha_packed =
Cvar.(constant (two_to_the bit_length) + b - a)
in
let%bind alpha = unpack alpha_packed ~length:Int.(bit_length + 1) in
let prefix, less_or_equal =
match Core_kernel.List.split_n alpha bit_length with
| p, [ l ] ->
(p, l)
| _ ->
failwith "compare: Invalid alpha"
in
let%bind not_all_zeros = Boolean.any prefix in
let%map less = Boolean.(less_or_equal && not_all_zeros) in
{ less; less_or_equal } )
module Assert = struct
let lt ~bit_length (x : Cvar.t) (y : Cvar.t) =
match (x, y) with
| Constant x, Constant y ->
assert (Field.compare x y < 0) ;
Checked.return ()
| _ ->
let open Checked in
let open Let_syntax in
let%bind { less; _ } = compare ~bit_length x y in
Boolean.Assert.is_true less
let lte ~bit_length (x : Cvar.t) (y : Cvar.t) =
match (x, y) with
| Constant x, Constant y ->
assert (Field.compare x y <= 0) ;
Checked.return ()
| _ ->
let open Checked in
let open Let_syntax in
let%bind { less_or_equal; _ } = compare ~bit_length x y in
Boolean.Assert.is_true less_or_equal
let gt ~bit_length x y = lt ~bit_length y x
let gte ~bit_length x y = lte ~bit_length y x
let non_zero (v : Cvar.t) =
match v with
| Constant v ->
if Field.(equal zero v) then
failwithf "assert_non_zero: failed on constant %s"
(Field.to_string v) () ;
Checked.return ()
| _ ->
Checked.assert_non_zero v
let equal x y = Checked.assert_equal ~label:"Checked.Assert.equal" x y
let not_equal (x : t) (y : t) =
match (x, y) with
| Constant x, Constant y ->
if Field.(equal x y) then
failwithf "not_equal: failed on constants %s and %s"
(Field.to_string x) (Field.to_string y) () ;
Checked.return ()
| _, _ ->
Checked.with_label "Checked.Assert.not_equal" (fun () ->
non_zero (sub x y) )
end
let lt_bitstring_value =
let module Boolean = Checked.Boolean in
let module Expr = struct
module Binary = struct
type 'a t = Lit of 'a | And of 'a * 'a t | Or of 'a * 'a t
end
module Nary = struct
type 'a t = Lit of 'a | And of 'a t list | Or of 'a t list
let rec of_binary : 'a Binary.t -> 'a t = function
| Lit x ->
Lit x
| And (x, And (y, t)) ->
And [ Lit x; Lit y; of_binary t ]
| Or (x, Or (y, t)) ->
Or [ Lit x; Lit y; of_binary t ]
| And (x, t) ->
And [ Lit x; of_binary t ]
| Or (x, t) ->
Or [ Lit x; of_binary t ]
let rec eval =
let open Checked.Let_syntax in
function
| Lit x ->
return x
| And xs ->
Checked.List.map xs ~f:eval >>= Boolean.all
| Or xs ->
Checked.List.map xs ~f:eval >>= Boolean.any
end
end in
let rec lt_binary xs ys : Boolean.var Expr.Binary.t =
match (xs, ys) with
| [], [] ->
Lit Boolean.false_
| [ _x ], [ false ] ->
Lit Boolean.false_
| [ x ], [ true ] ->
Lit (Boolean.not x)
| [ x1; _x2 ], [ true; false ] ->
Lit (Boolean.not x1)
| [ _x1; _x2 ], [ false; false ] ->
Lit Boolean.false_
| x :: xs, false :: ys ->
And (Boolean.not x, lt_binary xs ys)
| x :: xs, true :: ys ->
Or (Boolean.not x, lt_binary xs ys)
| _ :: _, [] | [], _ :: _ ->
failwith "lt_bitstring_value: Got unequal length strings"
in
fun (xs : Boolean.var Bitstring_lib.Bitstring.Msb_first.t)
(ys : bool Bitstring_lib.Bitstring.Msb_first.t) ->
let open Expr.Nary in
eval
(of_binary (lt_binary (xs :> Boolean.var list) (ys :> bool list)))
let field_size_bits =
List.init Field.size_in_bits ~f:(fun i ->
Z.testbit
(Bignum_bigint.to_zarith_bigint Field.size)
Stdlib.(Field.size_in_bits - 1 - i) )
|> Bitstring_lib.Bitstring.Msb_first.of_list
let unpack_full x =
let module Bitstring = Bitstring_lib.Bitstring in
let open Checked.Let_syntax in
let%bind res =
choose_preimage_var x ~length:Field.size_in_bits
>>| Bitstring.Lsb_first.of_list
in
let%map () =
lt_bitstring_value
(Bitstring.Msb_first.of_lsb_first res)
field_size_bits
>>= Checked.Boolean.Assert.is_true
in
res
let parity ?length x =
let open Checked in
let unpack =
let unpack_full x =
unpack_full x >>| Bitstring_lib.Bitstring.Lsb_first.to_list
in
match length with
| None ->
unpack_full
| Some length ->
let length = Int.min length Field.size_in_bits in
if Int.equal length Field.size_in_bits then unpack_full
else choose_preimage_var ~length
in
unpack x >>| Base.List.hd_exn
end
end
module Bitstring_checked = struct
type t = Checked.Boolean.var list
let lt_value = Field.Checked.lt_bitstring_value
let chunk_for_equality (t1 : t) (t2 : t) =
let chunk_size = Field.size_in_bits - 1 in
let rec go acc t1 t2 =
match (t1, t2) with
| [], [] ->
acc
| _, _ ->
let t1_a, t1_b = List.split_n t1 chunk_size in
let t2_a, t2_b = List.split_n t2 chunk_size in
go ((t1_a, t2_a) :: acc) t1_b t2_b
in
go [] t1 t2
let equal t1 t2 =
let open Checked in
all
(Base.List.map (chunk_for_equality t1 t2) ~f:(fun (x1, x2) ->
equal (Cvar1.pack x1) (Cvar1.pack x2) ) )
>>= Boolean.all
let equal_expect_true t1 t2 =
let open Checked in
all
(Core_kernel.List.map (chunk_for_equality t1 t2) ~f:(fun (x1, x2) ->
(* Inlined [Field.equal], but skip creating the field element for
this chunk if possible.
*)
let z = Cvar1.(pack x1 - pack x2) in
let%bind r, inv =
exists
Typ.(field * field)
~compute:
As_prover.(
match
Core_kernel.List.map2 x1 x2 ~f:(fun x1 x2 ->
let%map x1 = read_var (x1 :> Cvar.t)
and x2 = read_var (x2 :> Cvar.t) in
Field.equal x1 x2 )
with
| Ok res ->
let%bind res = all res in
if Core_kernel.List.for_all ~f:Fn.id res then
return (Field.one, Field.zero)
else equal_vars z
| _ ->
equal_vars z)
in
let%map () = equal_constraints z inv r in
Boolean.Unsafe.of_cvar r ) )
>>= Boolean.all
module Assert = struct
let equal t1 t2 =
let open Checked in
Base.List.map (chunk_for_equality t1 t2) ~f:(fun (x1, x2) ->
Constraint.equal (Cvar1.pack x1) (Cvar1.pack x2) )
|> assert_all ~label:"Bitstring.Assert.equal"
end
end
let%test_unit "lt_bitstring_value" =
let gen =
let open Quickcheck.Generator in
let open Let_syntax in
let%bind length = small_positive_int in
let%map x = list_with_length length bool
and y = list_with_length length bool in
(x, y)
in
Quickcheck.test gen ~f:(fun (x, y) ->
let correct_answer = [%compare: bool list] x y < 0 in
let lt =
run_and_check
(Checked.map
~f:(As_prover.read Checked.Boolean.typ)
(Field.Checked.lt_bitstring_value
(Bitstring_lib.Bitstring.Msb_first.of_list
(List.map ~f:Checked.(constant Boolean.typ) x) )
(Bitstring_lib.Bitstring.Msb_first.of_list y) ) )
|> Or_error.ok_exn
in
assert (Bool.equal lt correct_answer) )
include Checked
let%snarkydef_ if_ (b : Boolean.var) ~typ:(Typ typ : ('var, _) Typ.t)
~(then_ : 'var) ~(else_ : 'var) =
let then_, then_aux = typ.var_to_fields then_ in
let else_, else_aux = typ.var_to_fields else_ in
let%bind res =
Array.all
(Core_kernel.Array.map2_exn then_ else_ ~f:(fun then_ else_ ->
if_ b ~then_ ~else_ ) )
in
let%map res_aux =
(* Abstraction leak.. *)
let res_aux = ref None in
let%map () =
as_prover
As_prover.(
if%map read Boolean.typ b then res_aux := Some then_aux
else res_aux := Some else_aux)
in
match !res_aux with
| Some res_aux ->
res_aux
| None ->
typ.constraint_system_auxiliary ()
in
typ.var_of_fields (res, res_aux)
module Test = struct
let checked_to_unchecked typ1 typ2 checked input =
let checked_result =
run_and_check
(let open Let_syntax in
let%bind input = exists typ1 ~compute:(As_prover.return input) in
let%map result = checked input in
As_prover.read typ2 result)
|> Or_error.ok_exn
in
checked_result
let test_equal (type a) ?(sexp_of_t = sexp_of_opaque) ?(equal = Caml.( = ))
typ1 typ2 checked unchecked input =
let checked_result = checked_to_unchecked typ1 typ2 checked input in
let sexp_of_a = sexp_of_t in
let compare_a x y = if equal x y then 0 else 1 in
[%test_eq: a] checked_result (unchecked input)
end
module R1CS_constraint_system = struct
include R1CS_constraint_system
end
end
(** The main functor for the monadic interface.
See [Run.Make] for the same thing but for the imperative interface. *)
module Make (Backend : Backend_intf.S) = struct
module Backend_extended = Backend_extended.Make (Backend)
module Runner0 = Runner.Make (Backend_extended)
module Checked_runner = Runner0.Checked_runner
module Checked1 = Checked.Make (Backend.Field) (Checked_runner) (As_prover0)
module Field_T = struct
type field = Backend_extended.Field.t
end
module As_prover_ext = As_prover0.Make_extended (Field_T) (As_prover0)
module Ref :
As_prover_ref.S
with module Types = Checked1.Types
and type ('a, 'f) checked := ('a, 'f) Checked1.t
and type 'f field := Backend_extended.Field.t =
As_prover_ref.Make (Checked1) (As_prover0)
module Checked_for_basic = struct
include (
Checked1 :
Checked_intf.S
with module Types = Checked1.Types
with type ('a, 'f) t := ('a, 'f) Checked1.t
and type 'f field := Backend_extended.Field.t )
type field = Backend_extended.Field.t
type 'a t = ('a, field) Types.Checked.t
let run = Runner0.run
end
module Basic =
Make_basic (Backend_extended) (Checked_for_basic) (As_prover_ext) (Ref)
(Runner0)
include Basic
module Number = Number.Make (Basic)
module Enumerable = Enumerable.Make (Basic)
end
module Typ0 = Typ
module Run = struct
let functor_counter = ref 0
let active_counters = ref []
let is_active_functor_id num =
match !active_counters with
| [] ->
(* Show the usual error, the functor isn't wrong as far as we can tell.
*)
true
| active :: _ ->
Int.equal active num
let active_functor_id () = List.hd_exn !active_counters
module Make_basic (Backend : Backend_intf.S) = struct
module Snark = Make (Backend)
open Run_state
open Snark
let set_constraint_logger = set_constraint_logger
let clear_constraint_logger = clear_constraint_logger
let this_functor_id = incr functor_counter ; !functor_counter
let state =
ref
(Run_state.make ~input:(field_vec ()) ~aux:(field_vec ())
~eval_constraints:false ~num_inputs:0 ~next_auxiliary:(ref 0)
~with_witness:false ~stack:[] ~is_running:false () )
let dump () = Run_state.dump !state
let in_prover () : bool = Run_state.has_witness !state
let in_checked_computation () : bool =
is_active_functor_id this_functor_id && Run_state.is_running !state
let run (checked : _ Checked.t) =
match checked with
| Pure a ->
a
| _ ->
if not (is_active_functor_id this_functor_id) then
failwithf
"Could not run this function.\n\n\
Hint: The module used to create this function had internal ID \
%i, but the module used to run it had internal ID %i. The same \
instance of Snarky.Snark.Run.Make must be used for both."
this_functor_id (active_functor_id ()) ()
else if not (Run_state.is_running !state) then
failwith
"This function can't be run outside of a checked computation." ;
let state', x = Runner.run checked !state in
state := state' ;
x
let as_stateful x state' =
state := state' ;
let a = x () in
(!state, a)
let make_checked (type a) (f : unit -> a) : _ Checked.t =
let g : run_state -> run_state * a = as_stateful f in
Function g
module R1CS_constraint_system = Snark.R1CS_constraint_system
type field = Snark.field
module Bigint = Snark.Bigint
module Constraint = Snark.Constraint
module Typ = struct
open Snark.Typ
type nonrec ('var, 'value) t = ('var, 'value) t
let unit = unit
let field = field
let tuple2 = tuple2
let ( * ) = ( * )
let tuple3 = tuple3
let list = list
let array = array
let hlist = hlist
let transport = transport
let transport_var = transport_var
let of_hlistable = of_hlistable
module Internal = Internal
end
let constant (Typ typ : _ Typ.t) x =
let fields, aux = typ.value_to_fields x in
let field_vars = Core_kernel.Array.map ~f:Cvar.constant fields in
typ.var_of_fields (field_vars, aux)
module Boolean = struct
open Snark.Boolean
type nonrec var = var
type value = bool
let true_ = true_
let false_ = false_
let if_ b ~then_ ~else_ = run (if_ b ~then_ ~else_)
let not = not
let ( && ) x y = run (x && y)
let ( &&& ) = ( && )
let ( || ) x y = run (x || y)
let ( ||| ) = ( || )
let ( lxor ) x y = run (x lxor y)
let any l = run (any l)
let all l = run (all l)
let of_field x = run (of_field x)
let var_of_value = var_of_value
let typ = typ
let typ_unchecked = typ_unchecked
let equal x y = run (equal x y)
module Expr = struct
open Snark.Boolean.Expr
type nonrec t = t
let ( ! ) = ( ! )
let ( && ) = ( && )
let ( &&& ) = ( && )
let ( || ) = ( || )
let ( ||| ) = ( ||| )
let any = any
let all = all
let not = not
let eval x = run (eval x)
let assert_ x = run (assert_ x)
end
module Unsafe = Unsafe
module Assert = struct
open Snark.Boolean.Assert
let ( = ) x y = run (x = y)
let is_true x = run (is_true x)
let any l = run (any l)
let all l = run (all l)
let exactly_one l = run (exactly_one l)
end
module Array = struct
open Snark.Boolean.Array
let any x = run (any x)
let all x = run (all x)
module Assert = struct
let any x = run (Assert.any x)
let all x = run (Assert.all x)
end
end
end
module Field = struct
open Snark.Field
let size_in_bits = size_in_bits
let size = size
module Constant = struct
type t = Snark.Field.t [@@deriving bin_io, sexp, hash, compare, eq]
let gen = gen
let gen_uniform = gen_uniform
module T = struct
let bin_shape_t = bin_shape_t
let bin_writer_t = bin_writer_t
let bin_write_t = bin_write_t
let bin_size_t = bin_size_t
let bin_reader_t = bin_reader_t
let __bin_read_t__ = __bin_read_t__
let bin_read_t = bin_read_t
let bin_t = bin_t
let sexp_of_t = sexp_of_t
let t_of_sexp = t_of_sexp
let of_int = of_int
let one = one
let zero = zero
let add = add
let sub = sub
let mul = mul
let inv = inv
let square = square
let sqrt = sqrt
let is_square = is_square
let equal = equal
let size_in_bits = size_in_bits
let print = print
let to_string = to_string
let random = random
module Mutable = Mutable
let ( += ) = ( += )
let ( -= ) = ( -= )
let ( *= ) = ( *= )
module Vector = Vector
let negate = negate
let ( + ) = ( + )
let ( - ) = ( - )
let ( * ) = ( * )
let ( / ) = ( / )
let of_string = of_string
let to_string = to_string
let unpack = unpack
let project = project
let parity = parity
end
include T
end
open Snark.Field.Var
type nonrec t = t