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
module S = Csir.Scalar
let alpha = Z.(shift_left one (Z.numbits S.order) - S.order)
let bitlist : ?le:bool -> bytes -> bool list =
fun ?(le = false) b ->
let l = Bytes.length b in
let start = if le then 0 else l - 1 in
let stop = if le then l else -1 in
let next a = if le then a + 1 else a - 1 in
let rec loop_byte acc n =
if n = stop then acc
else
let byte = Bytes.get_uint8 b n in
let rec loop_bit acc m =
if m = 8 then acc
else
let mask = 1 lsl m in
let bit = byte land mask in
let bit = if bit = 0 then false else true in
loop_bit (bit :: acc) (m + 1)
in
let acc = loop_bit acc 0 in
loop_byte acc (next n)
in
List.rev @@ loop_byte [] start
let bytes_of_hex hs =
let h = `Hex hs in
Hex.to_bytes h
let bool_list_to_scalar : bool list -> S.t =
fun b_list ->
let res, _ =
List.fold_left
(fun (acc_res, acc_p) b ->
let acc_res = if b then S.(acc_res + acc_p) else acc_res in
let acc_p = S.double acc_p in
(acc_res, acc_p))
(S.zero, S.one)
b_list
in
res
let bool_list_to_z : bool list -> Z.t =
fun b_list ->
let res, _ =
List.fold_left
(fun (acc_res, acc_p) b ->
let acc_res = if b then Z.(acc_res + acc_p) else acc_res in
let acc_p = Z.(acc_p + acc_p) in
(acc_res, acc_p))
(Z.zero, Z.one)
b_list
in
res
let bool_list_of_z : ?nb_bits:int -> Z.t -> bool list =
fun ?nb_bits z ->
let two = Z.of_int 2 in
let rec aux bits z = function
| 0 -> List.rev bits
| n ->
let b = Z.(equal (z mod two) one) in
aux (b :: bits) (Z.div z two) (n - 1)
in
aux [] z @@ Option.value ~default:(Z.numbits z) nb_bits
module Z = struct
include Z
let t : t Repr.t =
Repr.(
map
bytes
(fun bs -> Z.of_bits (Bytes.unsafe_to_string bs))
(fun s -> Z.to_bits s |> Bytes.of_string))
end
let rec transpose = function
| [] | [] :: _ -> []
| rows -> List.(map hd rows :: (transpose @@ map tl rows))
let of_bytes repr bs =
Stdlib.Result.get_ok
@@ Repr.(unstage @@ of_bin_string repr) (Bytes.unsafe_to_string bs)
let to_bytes repr e =
Bytes.unsafe_of_string @@ Repr.(unstage @@ to_bin_string repr) e
let tables_cs_encoding_t : (string list * Csir.CS.t) Repr.t =
let open Repr in
pair (list string) Csir.CS.t
let save_cs_to_file path tables cs =
let s = Repr.to_json_string tables_cs_encoding_t (tables, cs) in
let outc = open_out path in
output_string outc s ;
close_out outc
let load_cs_from_file path =
if not (Sys.file_exists path) then
raise
@@ Invalid_argument
(Printf.sprintf "load_cs_from_file: %s does not exist." path) ;
let inc = open_in path in
let content = really_input_string inc (in_channel_length inc) in
let res =
Repr.of_json_string tables_cs_encoding_t content |> Stdlib.Result.get_ok
in
close_in inc ;
res
let get_circuit_id cs =
let serialized_bytes = to_bytes Csir.CS.t cs in
Hacl_star.Hacl.Blake2b_32.hash serialized_bytes 32 |> Hex.of_bytes |> Hex.show
let circuit_dir =
match Sys.getenv_opt "TMPDIR" with
| None -> "/tmp/plompiler"
| Some dir -> dir ^ "/plompiler"
let circuit_path s =
if not @@ Sys.file_exists circuit_dir then Sys.mkdir circuit_dir 0o755 ;
circuit_dir ^ "/" ^ s
let dump_label_traces path (cs : Csir.CS.t) =
let outc = open_out path in
List.iter
Csir.CS.(
Array.iter (fun c ->
Printf.fprintf outc "%s 1\n" @@ String.concat "; " (List.rev c.label)))
cs ;
close_out outc