Source file boolean_gates.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
open Bls
open Identities
module L = Plompiler.LibCircuit
open Gates_common
module BoolCheck : Base_sig = struct
let q_label = "qbool"
let identity = (q_label, 1)
let index_com = None
let nb_advs = 0
let nb_buffers = 1
let gx_composition = false
let equations ~q ~wires ~wires_g:_ ?precomputed_advice:_ () =
let a = wires.(0) in
Scalar.[q * sub one a * a]
let prover_identities ~prefix_common ~prefix ~public:_ ~domain:_ evaluations =
let tmps, ids = get_buffers ~nb_buffers ~nb_ids:(snd identity) in
let ({q; wires} : witness) =
get_evaluations ~q_label ~prefix ~prefix_common evaluations
in
let a = wires.(0) in
let one_minus_a =
Evaluations.linear_c
~res:tmps.(0)
~evaluations:[a]
~add_constant:one
~linear_coeffs:[mone]
()
in
let id =
Evaluations.mul_c ~res:ids.(0) ~evaluations:[q; a; one_minus_a] ()
in
SMap.singleton (prefix @@ q_label ^ ".0") id
let verifier_identities ~prefix_common ~prefix ~public:_ ~generator:_
~size_domain:_ _ answers =
let q = get_answer answers X @@ prefix_common q_label in
let a = get_answer answers X @@ prefix (wire_name 0) in
let res = Scalar.(q * a * sub one a) in
SMap.singleton (prefix @@ q_label ^ ".0") res
let polynomials_degree = SMap.of_list [(wire_name 0, 3); (q_label, 3)]
let cs ~q ~wires ~wires_g:_ ?precomputed_advice:_ () =
let a = wires.(0) in
let open L in
map_singleton
(let* tmp = Num.mul q a in
let* tmp' = Num.add_constant Scalar.(one) a ~ql:mone in
Num.mul tmp tmp')
end
module CondSwap : Base_sig = struct
let q_label = "qcond_swap"
let identity = (q_label, 2)
let index_com = None
let nb_advs = 0
let nb_buffers = 4
let gx_composition = false
let equations ~q ~wires ~wires_g:_ ?precomputed_advice:_ () =
let bit = wires.(0) in
let b = wires.(1) in
let c = wires.(2) in
let d = wires.(3) in
let e = wires.(4) in
let bbit = Scalar.(sub one bit) in
Scalar.
[q * ((bbit * b) + sub (bit * c) d); q * ((bit * b) + sub (bbit * c) e)]
let prover_identities ~prefix_common ~prefix ~public:_ ~domain:_ evaluations =
let tmps, ids = get_buffers ~nb_buffers ~nb_ids:(snd identity) in
let ({q; wires} : witness) =
get_evaluations ~q_label ~prefix ~prefix_common evaluations
in
let b = wires.(0) in
let x, y = (wires.(1), wires.(2)) in
let u, v = (wires.(3), wires.(4)) in
let bb =
Evaluations.linear_c
~res:tmps.(0)
~evaluations:[b]
~add_constant:one
~linear_coeffs:[mone]
()
in
let t0 = Evaluations.mul_c ~res:tmps.(1) ~evaluations:[q; bb; x] () in
let t1 = Evaluations.mul_c ~res:tmps.(2) ~evaluations:[q; b; y] () in
let t2 = Evaluations.mul_c ~res:tmps.(3) ~evaluations:[q; u] () in
let id1 =
Evaluations.linear_c
~res:ids.(0)
~evaluations:[t0; t1; t2]
~linear_coeffs:[one; one; mone]
()
in
let t0 = Evaluations.mul_c ~res:tmps.(1) ~evaluations:[q; bb; y] () in
let t1 = Evaluations.mul_c ~res:tmps.(2) ~evaluations:[q; b; x] () in
let t2 = Evaluations.mul_c ~res:tmps.(3) ~evaluations:[q; v] () in
let id2 =
Evaluations.linear_c
~res:ids.(1)
~evaluations:[t0; t1; t2]
~linear_coeffs:[one; one; mone]
()
in
SMap.of_list
[(prefix @@ q_label ^ ".0", id1); (prefix @@ q_label ^ ".1", id2)]
let verifier_identities ~prefix_common ~prefix ~public:_ ~generator:_
~size_domain:_ : verifier_identities =
fun _ answers ->
let ({q; wires; _} : answers) =
get_answers ~q_label ~prefix ~prefix_common answers
in
let bit = wires.(0) in
let x, y = (wires.(1), wires.(2)) in
let u, v = (wires.(3), wires.(4)) in
let bbit = Scalar.(sub one bit) in
let id1 = Scalar.(q * ((bbit * x) + sub (bit * y) u)) in
let id2 = Scalar.(q * ((bit * x) + sub (bbit * y) v)) in
SMap.of_list
[(prefix @@ q_label ^ ".0", id1); (prefix @@ q_label ^ ".1", id2)]
let polynomials_degree =
SMap.of_list
[
(q_label, 3);
(wire_name 0, 3);
(wire_name 1, 3);
(wire_name 2, 3);
(wire_name 3, 3);
(wire_name 4, 3);
]
let cs ~q:qbool ~wires ~wires_g:_ ?precomputed_advice:_ () =
let bit = wires.(0) in
let x = wires.(1) in
let y = wires.(2) in
let u = wires.(3) in
let v = wires.(4) in
let open L in
let* bit_times_x = Num.mul bit x in
let* bit_times_y = Num.mul bit y in
let* id1 =
let* all =
Num.add_list
~coeffs:[one; mone; one; mone]
(to_list [x; bit_times_x; bit_times_y; u])
in
Num.mul qbool all
in
let* id2 =
let* all =
Num.add_list
~coeffs:[one; one; mone; mone]
(to_list [bit_times_x; y; bit_times_y; v])
in
Num.mul qbool all
in
ret [id1; id2]
end