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
// This file is part of MIDNIGHT-ZK.
// Copyright (C) Midnight Foundation
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Bitwise instructions interface.
//!
//! It provides functions for performing bit-wise Boolean operations between
//! assigned values in the circuit.
//!
//! This trait is parametrized by a generic `Assigned` (required to implement
//! [InnerValue](crate::types::InnerValue)) and whose inner `Element` type is
//! required to implement [CircuitField]). `Assigned` defined the type over
//! which the bitwise operations take place.
use ff::Field;
use midnight_proofs::{circuit::Layouter, plonk::Error};
use num_bigint::BigUint;
use num_traits::One;
use super::{BinaryInstructions, DecompositionInstructions, RangeCheckInstructions};
use crate::{
types::{InnerConstants, Instantiable},
CircuitField,
};
/// The set of circuit instructions for binary bit-wise operations.
pub trait BitwiseInstructions<F, Assigned>:
BinaryInstructions<F> + DecompositionInstructions<F, Assigned> + RangeCheckInstructions<F, Assigned>
where
F: CircuitField,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField,
{
/// Bitwise conjunction of the given assigned elements, interpreted as
/// binary bit-strings of length `n`.
///
/// # Unsatisfiable Circuit
///
/// If any of the given assigned elements cannot be decomposed in `n` bits.
///
/// ```
/// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
/// let x = chip.assign(&mut layouter, Value::known(F::from(13)))?;
/// let y = chip.assign(&mut layouter, Value::known(F::from(7)))?;
///
/// // 0b1101 & 0b0111 = 0b0101
/// let res = chip.band(&mut layouter, &x, &y, 4)?;
/// chip.assert_equal_to_fixed(&mut layouter, &res, F::from(5))?;
/// # });
/// ```
///
/// ```should_panic
/// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
/// let x = chip.assign(&mut layouter, Value::known(F::from(16)))?;
/// let y = chip.assign(&mut layouter, Value::known(F::from(7)))?;
///
/// // x is not in the range [0, 2^4), the following should be unsatisfiable
/// let res = chip.band(&mut layouter, &x, &y, 4)?;
/// # });
/// ```
fn band(
&self,
layouter: &mut impl Layouter<F>,
x: &Assigned,
y: &Assigned,
n: usize,
) -> Result<Assigned, Error> {
let x_bits = self.assigned_to_le_bits(layouter, x, Some(n), true)?;
let y_bits = self.assigned_to_le_bits(layouter, y, Some(n), true)?;
let res_bits = x_bits
.into_iter()
.zip(y_bits.into_iter())
.map(|(bx, by)| self.and(layouter, &[bx, by]))
.collect::<Result<Vec<_>, Error>>()?;
self.assigned_from_le_bits(layouter, &res_bits)
}
/// Bitwise disjunction of the given assigned elements, interpreted as
/// binary bit-strings of length `n`.
///
/// # Unsatisfiable Circuit
///
/// If any of the given assigned elements cannot be decomposed in `n` bits.
///
/// ```
/// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
/// let x = chip.assign(&mut layouter, Value::known(F::from(13)))?;
/// let y = chip.assign(&mut layouter, Value::known(F::from(7)))?;
///
/// // 0b1101 | 0b0111 = 0b1111
/// let res = chip.bor(&mut layouter, &x, &y, 4)?;
/// chip.assert_equal_to_fixed(&mut layouter, &res, F::from(15))?;
/// # });
/// ```
fn bor(
&self,
layouter: &mut impl Layouter<F>,
x: &Assigned,
y: &Assigned,
n: usize,
) -> Result<Assigned, Error> {
let x_bits = self.assigned_to_le_bits(layouter, x, Some(n), true)?;
let y_bits = self.assigned_to_le_bits(layouter, y, Some(n), true)?;
let res_bits = x_bits
.into_iter()
.zip(y_bits.into_iter())
.map(|(bx, by)| self.or(layouter, &[bx, by]))
.collect::<Result<Vec<_>, Error>>()?;
self.assigned_from_le_bits(layouter, &res_bits)
}
/// Bitwise exclusive-or of the given assigned elements, interpreted as
/// binary bit-strings of length `n`.
///
/// # Unsatisfiable Circuit
///
/// If any of the given assigned elements cannot be decomposed in `n` bits.
///
/// ```
/// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
/// let x = chip.assign(&mut layouter, Value::known(F::from(13)))?;
/// let y = chip.assign(&mut layouter, Value::known(F::from(7)))?;
///
/// // 0b1101 ^ 0b0111 = 0b1010
/// let res = chip.bxor(&mut layouter, &x, &y, 4)?;
/// chip.assert_equal_to_fixed(&mut layouter, &res, F::from(10))?;
/// # });
/// ```
fn bxor(
&self,
layouter: &mut impl Layouter<F>,
x: &Assigned,
y: &Assigned,
n: usize,
) -> Result<Assigned, Error> {
let x_bits = self.assigned_to_le_bits(layouter, x, Some(n), true)?;
let y_bits = self.assigned_to_le_bits(layouter, y, Some(n), true)?;
let res_bits = x_bits
.into_iter()
.zip(y_bits.into_iter())
.map(|(bx, by)| self.xor(layouter, &[bx, by]))
.collect::<Result<Vec<_>, Error>>()?;
self.assigned_from_le_bits(layouter, &res_bits)
}
/// Bitwise negation of the given assigned element, interpreted as a
/// binary bit-string of length `n`.
///
/// # Unsatisfiable Circuit
///
/// If the given assigned element cannot be decomposed in `n` bits.
///
/// ```
/// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
/// let x = chip.assign(&mut layouter, Value::known(F::from(6)))?;
///
/// // !0b0110 = 0b1001
/// let res = chip.bnot(&mut layouter, &x, 4)?;
/// chip.assert_equal_to_fixed(&mut layouter, &res, F::from(9))?;
/// # });
/// ```
fn bnot(
&self,
layouter: &mut impl Layouter<F>,
x: &Assigned,
n: usize,
) -> Result<Assigned, Error> {
// Simply return `2^n - 1 - x` after having verified that `x in [0, 2^n)`.
self.assert_lower_than_fixed(layouter, x, &(BigUint::one() << n))?;
self.linear_combination(
layouter,
&[(-Assigned::inner_one(), x.clone())],
Assigned::Element::from(2).pow([n as u64]) - Assigned::inner_one(),
)
}
}
#[cfg(test)]
pub(crate) mod tests {
use std::{cmp::min, marker::PhantomData};
use ff::FromUniformBytes;
use midnight_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
dev::MockProver,
plonk::{Circuit, ConstraintSystem},
};
use rand::{RngCore, SeedableRng};
use rand_chacha::ChaCha8Rng;
use super::*;
use crate::{
testing_utils::FromScratch,
utils::circuit_modeling::{circuit_to_json, cost_measure_end, cost_measure_start},
};
#[derive(Clone, Copy, Debug)]
enum Operation {
Band,
Bor,
Bxor,
Bnot,
}
#[derive(Clone, Debug)]
struct TestCircuit<F, Assigned, BitwiseChip>
where
Assigned: InnerConstants,
{
inputs: Vec<Assigned::Element>,
expected: Assigned::Element,
n: usize,
operation: Operation,
_marker: PhantomData<(F, Assigned, BitwiseChip)>,
}
impl<F, Assigned, BitwiseChip> Circuit<F> for TestCircuit<F, Assigned, BitwiseChip>
where
F: CircuitField,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
type Config = <BitwiseChip as FromScratch<F>>::Config;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();
fn without_witnesses(&self) -> Self {
unreachable!()
}
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
let committed_instance_column = meta.instance_column();
let instance_column = meta.instance_column();
BitwiseChip::configure_from_scratch(
meta,
&mut vec![],
&mut vec![],
&[committed_instance_column, instance_column],
)
}
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let chip = BitwiseChip::new_from_scratch(&config);
// y does not apply in tests of arity-1 functions.
let y_idx = min(self.inputs.len() - 1, 1);
let x = chip.assign(&mut layouter, Value::known(self.inputs[0]))?;
let y = chip.assign(&mut layouter, Value::known(self.inputs[y_idx]))?;
cost_measure_start(&mut layouter);
let res = match self.operation {
Operation::Band => chip.band(&mut layouter, &x, &y, self.n),
Operation::Bor => chip.bor(&mut layouter, &x, &y, self.n),
Operation::Bxor => chip.bxor(&mut layouter, &x, &y, self.n),
Operation::Bnot => chip.bnot(&mut layouter, &x, self.n),
}?;
cost_measure_end(&mut layouter);
chip.assert_equal_to_fixed(&mut layouter, &res, self.expected)?;
chip.load_from_scratch(&mut layouter)
}
}
#[allow(clippy::too_many_arguments)]
fn run<F, Assigned, BitwiseChip>(
inputs: &[Assigned::Element],
expected: &Assigned::Element,
n: usize,
operation: Operation,
must_pass: bool,
cost_model: bool,
circuit_name: &str,
op_name: &str,
) where
F: CircuitField + FromUniformBytes<64> + Ord,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
let circuit = TestCircuit::<F, Assigned, BitwiseChip> {
inputs: inputs.to_vec(),
expected: *expected,
n,
operation,
_marker: PhantomData,
};
let public_inputs = vec![vec![], vec![]];
match MockProver::run(&circuit, public_inputs) {
Ok(prover) => match prover.verify() {
Ok(()) => assert!(must_pass),
Err(e) => assert!(!must_pass, "Failed verifier with error {e:?}"),
},
Err(e) => assert!(!must_pass, "Failed prover with error {e:?}"),
}
if cost_model {
circuit_to_json(circuit_name, op_name, circuit);
}
}
pub fn test_band<F, Assigned, BitwiseChip>(name: &str)
where
F: CircuitField + FromUniformBytes<64> + Ord,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField + From<u64>,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
let r = rng.next_u64();
let s = rng.next_u64();
let mut cost_model = true;
[
(r, r, r, true),
(r, r, 0, false),
(r, s, r & s, true),
(0, 0, 0, true),
(1, 1, 1, true),
(5, 7, 5, true),
(5, 2, 0, true),
(0, 0, 1, false),
]
.iter()
.for_each(|(x, y, z, must_pass)| {
let inputs = [Assigned::Element::from(*x), Assigned::Element::from(*y)];
let expected = Assigned::Element::from(*z);
run::<F, Assigned, BitwiseChip>(
&inputs,
&expected,
64,
Operation::Band,
*must_pass,
cost_model,
name,
"band",
);
cost_model = false;
});
let ten = Assigned::Element::from(10);
run::<F, Assigned, BitwiseChip>(
&[ten, ten],
&ten,
3,
Operation::Band,
false,
cost_model,
"",
"",
);
}
pub fn test_bor<F, Assigned, BitwiseChip>(name: &str)
where
F: CircuitField + FromUniformBytes<64> + Ord,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField + From<u64>,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
let r = rng.next_u64();
let s = rng.next_u64();
let mut cost_model = true;
[
(r, r, r, true),
(r, r, 0, false),
(r, s, r | s, true),
(0, 0, 0, true),
(1, 1, 1, true),
(5, 7, 7, true),
(5, 2, 7, true),
(0, 0, 1, false),
]
.iter()
.for_each(|(x, y, z, must_pass)| {
let inputs = [Assigned::Element::from(*x), Assigned::Element::from(*y)];
let expected = Assigned::Element::from(*z);
run::<F, Assigned, BitwiseChip>(
&inputs,
&expected,
64,
Operation::Bor,
*must_pass,
cost_model,
name,
"bor",
);
cost_model = false;
});
let ten = Assigned::Element::from(10);
run::<F, Assigned, BitwiseChip>(&[ten, ten], &ten, 3, Operation::Bor, false, false, "", "");
}
pub fn test_bxor<F, Assigned, BitwiseChip>(name: &str)
where
F: CircuitField + FromUniformBytes<64> + Ord,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField + From<u64>,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
let r = rng.next_u64();
let s = rng.next_u64();
let mut cost_model = true;
[
(r, r, r, false),
(r, r, 0, true),
(r, s, r ^ s, true),
(0, 0, 0, true),
(1, 1, 0, true),
(5, 7, 2, true),
(5, 2, 7, true),
(0, 0, 1, false),
]
.iter()
.for_each(|(x, y, z, must_pass)| {
let inputs = [Assigned::Element::from(*x), Assigned::Element::from(*y)];
let expected = Assigned::Element::from(*z);
run::<F, Assigned, BitwiseChip>(
&inputs,
&expected,
64,
Operation::Bxor,
*must_pass,
cost_model,
name,
"bxor",
);
cost_model = false;
});
let zero = Assigned::Element::from(0);
let ten = Assigned::Element::from(10);
run::<F, Assigned, BitwiseChip>(
&[ten, ten],
&zero,
3,
Operation::Bxor,
false,
false,
"",
"",
);
}
pub fn test_bnot<F, Assigned, BitwiseChip>(name: &str)
where
F: CircuitField + FromUniformBytes<64> + Ord,
Assigned: Instantiable<F> + InnerConstants + Clone,
Assigned::Element: CircuitField + From<u64>,
BitwiseChip: BitwiseInstructions<F, Assigned> + FromScratch<F>,
{
let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
let r = rng.next_u64();
let mut cost_model = true;
[
(r, !r, true, 64),
(r, !r + 1, false, 64),
(0, 7, true, 3),
(1, 6, true, 3),
(2, 5, true, 3),
(5, 2, true, 3),
(0, 1, false, 3),
]
.iter()
.for_each(|(x, z, must_pass, n)| {
let inputs = [Assigned::Element::from(*x)];
let expected = Assigned::Element::from(*z);
run::<F, Assigned, BitwiseChip>(
&inputs,
&expected,
*n,
Operation::Bnot,
*must_pass,
cost_model,
name,
"bnot",
);
cost_model = false;
});
let mone = -Assigned::Element::from(1);
let eight = Assigned::Element::from(8);
run::<F, Assigned, BitwiseChip>(&[eight], &mone, 3, Operation::Bnot, false, false, "", "");
}
}