midnight-circuits 7.0.0

Circuit and gadget implementations for Midnight zero-knowledge proofs
Documentation
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
// 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.

//! Range-check and comparison instructions interface.
//!
//! It provides functions to compare assigned values with other assigned
//! values or fixed elements.
//!
//! Comparisons are defined by comparing the *interger representation* of field
//! elements and assumes we only compare "small" integers, i.e. all elements are
//! bounded. The maximum allowed bound is implementation specific and should be
//! at most 2^{F::NUM_BITS/2 - 1} to avoid breaking "natural" properties of
//! comparison

use std::{fmt::Debug, ops::Add};

use midnight_proofs::{circuit::Layouter, plonk::Error};

use crate::{
    field::AssignedBounded,
    instructions::BinaryInstructions,
    types::{AssignedBit, InnerValue},
    CircuitField,
};

/// The set of circuit instructions for comparison operations.
pub trait ComparisonInstructions<F, Assigned>: Clone + Debug + BinaryInstructions<F>
where
    F: CircuitField,
    Assigned: InnerValue,
    Assigned::Element: From<u64> + Add<Output = Assigned::Element>,
{
    /// All numbers involved in comparisons should be in the range [0,
    /// 2^{MAX_BOUND_IN_BITS}) and no comparison should be allowed for some
    /// bound > MAX_BOUND_IN_BITS.
    const MAX_BOUND_IN_BITS: u32;

    /// Converts an assigned element into an assigned bounded element.
    /// The circuit becomes unsatisfiable if the element value is not in [0,
    /// 2^{bound_in_bits}).
    fn bounded_of_element(
        &self,
        layouter: &mut impl Layouter<F>,
        n: usize,
        x: &Assigned,
    ) -> Result<AssignedBounded<F>, Error>;

    /// Converts an assigned bounded element into an assigned element with the
    /// same value.
    fn element_of_bounded(
        &self,
        layouter: &mut impl Layouter<F>,
        bounded: &AssignedBounded<F>,
    ) -> Result<Assigned, Error>;

    /// Returns `true` iff the given assigned element is strictly lower than the
    /// given bound.
    fn lower_than_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        bound: Assigned::Element,
    ) -> Result<AssignedBit<F>, Error>;

    /// Returns `true` iff the given assigned element is strictly greater than
    /// the given bound.
    fn greater_than_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        bound: Assigned::Element,
    ) -> Result<AssignedBit<F>, Error> {
        let b = self.leq_fixed(layouter, x, bound)?;
        self.not(layouter, &b)
    }

    /// Returns `true1` iff the given assigned element is lower than or equal to
    /// the given bound.
    fn leq_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        bound: Assigned::Element,
    ) -> Result<AssignedBit<F>, Error> {
        self.lower_than_fixed(layouter, x, bound + Assigned::Element::from(1))
    }

    /// Returns `true` iff the given assigned element is greater than or equal
    /// to the given bound.
    fn geq_fixed(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        bound: Assigned::Element,
    ) -> Result<AssignedBit<F>, Error> {
        let b = self.lower_than_fixed(layouter, x, bound)?;
        self.not(layouter, &b)
    }

    /// Returns `true` iff `x < y`.
    ///
    /// The following example will make the circuit unsatisfiable
    /// ```
    /// # midnight_circuits::run_test_native_gadget!(chip, layouter, {
    /// let x: AssignedNative<F> = chip.assign(&mut layouter, Value::known(F::from(276u64)))?;
    /// let y: AssignedNative<F> = chip.assign(&mut layouter, Value::known(F::from(313u64)))?;
    ///
    /// let x: AssignedBounded<F> = chip.bounded_of_element(&mut layouter, 16, &x)?;
    /// let y: AssignedBounded<F> = chip.bounded_of_element(&mut layouter, 16, &y)?;
    ///
    /// let check = chip.lower_than(&mut layouter, &x, &y)?;
    /// chip.assert_equal_to_fixed(&mut layouter, &check, true)?;
    /// # });
    /// ```
    fn lower_than(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        y: &AssignedBounded<F>,
    ) -> Result<AssignedBit<F>, Error>;

    /// Returns `true` iff `x > y`.
    fn greater_than(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        y: &AssignedBounded<F>,
    ) -> Result<AssignedBit<F>, Error> {
        let b = self.leq(layouter, x, y)?;
        self.not(layouter, &b)
    }

    /// Returns `true` iff `x <= y`.
    fn leq(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        y: &AssignedBounded<F>,
    ) -> Result<AssignedBit<F>, Error>;

    /// Returns `true` iff `x >= y`.
    fn geq(
        &self,
        layouter: &mut impl Layouter<F>,
        x: &AssignedBounded<F>,
        y: &AssignedBounded<F>,
    ) -> Result<AssignedBit<F>, Error>;
}

#[cfg(test)]
pub(crate) mod tests {
    use std::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::{
        instructions::{AssertionInstructions, AssignmentInstructions, DecompositionInstructions},
        testing_utils::FromScratch,
        types::{InnerConstants, Instantiable},
        utils::circuit_modeling::{circuit_to_json, cost_measure_end, cost_measure_start},
    };

    #[derive(Clone, Debug)]
    enum Op {
        BoundedOfElement,
        LeqFixed,
        GeqFixed,
        LowerFixed,
        GreaterFixed,
        Leq,
        Geq,
        Lower,
        Greater,
    }

    #[derive(Clone, Debug)]
    struct TestCircuit<F, Assigned, Chip>
    where
        Assigned: InnerValue,
    {
        n: usize,
        x: Assigned::Element,
        y: Assigned::Element,
        expected: bool,
        operation: Op,
        _marker: PhantomData<(F, Assigned, Chip)>,
    }

    impl<F, Assigned, Chip> Circuit<F> for TestCircuit<F, Assigned, Chip>
    where
        F: CircuitField + FromUniformBytes<64> + Ord,
        Assigned::Element: CircuitField,
        Assigned: Instantiable<F> + InnerConstants + Clone + Debug,
        Chip: AssignmentInstructions<F, Assigned>
            + AssignmentInstructions<F, AssignedBit<F>>
            + AssertionInstructions<F, AssignedBit<F>>
            + ComparisonInstructions<F, Assigned>
            + DecompositionInstructions<F, Assigned>
            + FromScratch<F>,
    {
        type Config = <Chip 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();
            Chip::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 = Chip::new_from_scratch(&config);

            let x = chip.assign(&mut layouter, Value::known(self.x))?;

            cost_measure_start(&mut layouter);
            match self.operation {
                Op::BoundedOfElement => {
                    chip.bounded_of_element(&mut layouter, self.n, &x)?;
                    Ok(())
                }
                _ => {
                    let assigned_x = {
                        let x = chip.assign(&mut layouter, Value::known(self.x))?;
                        chip.bounded_of_element(&mut layouter, self.n, &x)?
                    };
                    let assigned_y = {
                        let y = chip.assign(&mut layouter, Value::known(self.y))?;
                        chip.bounded_of_element(&mut layouter, self.n, &y)?
                    };
                    let b = match self.operation {
                        Op::Leq => chip.leq(&mut layouter, &assigned_x, &assigned_y),
                        Op::Geq => chip.geq(&mut layouter, &assigned_x, &assigned_y),
                        Op::Lower => chip.lower_than(&mut layouter, &assigned_x, &assigned_y),
                        Op::Greater => chip.greater_than(&mut layouter, &assigned_x, &assigned_y),
                        Op::LeqFixed => chip.leq_fixed(&mut layouter, &assigned_x, self.y),
                        Op::GeqFixed => chip.geq_fixed(&mut layouter, &assigned_x, self.y),
                        Op::LowerFixed => chip.lower_than_fixed(&mut layouter, &assigned_x, self.y),
                        Op::GreaterFixed => {
                            chip.greater_than_fixed(&mut layouter, &assigned_x, self.y)
                        }
                        _ => unreachable!(),
                    }?;

                    let expected = chip.assign_fixed(&mut layouter, self.expected)?;
                    chip.assert_equal(&mut layouter, &b, &expected)
                }
            }?;
            cost_measure_end(&mut layouter);

            chip.load_from_scratch(&mut layouter)
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn run<F, Assigned, Chip>(
        x: u64,
        y: u64,
        expected: bool,
        n: usize,
        operation: Op,
        must_pass: bool,
        cost_model: bool,
        chip_name: &str,
        op_name: &str,
    ) where
        F: CircuitField + FromUniformBytes<64> + Ord,
        Assigned::Element: CircuitField,
        Assigned: Instantiable<F> + InnerConstants + Clone + Debug,
        Chip: AssignmentInstructions<F, Assigned>
            + AssignmentInstructions<F, AssignedBit<F>>
            + AssertionInstructions<F, AssignedBit<F>>
            + ComparisonInstructions<F, Assigned>
            + DecompositionInstructions<F, Assigned>
            + FromScratch<F>,
    {
        let circuit = TestCircuit::<F, Assigned, Chip> {
            x: Assigned::Element::from(x),
            y: Assigned::Element::from(y),
            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(chip_name, op_name, circuit);
        }
    }

    pub fn test_lower_and_greater<F, Assigned, Chip>(name: &str)
    where
        F: CircuitField + FromUniformBytes<64> + Ord,
        Assigned::Element: CircuitField,
        Assigned: Instantiable<F> + InnerConstants + Clone + Debug,
        Chip: AssignmentInstructions<F, Assigned>
            + AssignmentInstructions<F, AssignedBit<F>>
            + AssertionInstructions<F, AssignedBit<F>>
            + ComparisonInstructions<F, Assigned>
            + DecompositionInstructions<F, Assigned>
            + FromScratch<F>,
    {
        let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
        let x = rng.next_u64();
        let y = rng.next_u64();
        let m = u64::MAX;
        let mut cost_model = true;
        [
            (x, x),
            (x, y),
            (y, x),
            (x, m),
            (m, x),
            (m - 1, m),
            (m, m - 1),
            (m, m),
            (0, 0),
            (0, 1),
            (1, 0),
            (0, 2),
            (1, 2),
            (2, 2),
            (2, 1),
        ]
        .into_iter()
        .for_each(|(x, y)| {
            // Positive
            run::<F, Assigned, Chip>(x, y, x >= y, 64, Op::Geq, true, cost_model, name, "geq");
            run::<F, Assigned, Chip>(x, y, x <= y, 64, Op::Leq, true, cost_model, name, "leq");
            run::<F, Assigned, Chip>(x, y, x < y, 64, Op::Lower, true, cost_model, name, "lower");
            run::<F, Assigned, Chip>(
                x,
                y,
                x > y,
                64,
                Op::Greater,
                true,
                cost_model,
                name,
                "greater",
            );
            run::<F, Assigned, Chip>(
                x,
                y,
                x <= y,
                64,
                Op::LeqFixed,
                true,
                cost_model,
                name,
                "leq_fixed",
            );
            run::<F, Assigned, Chip>(
                x,
                y,
                x >= y,
                64,
                Op::GeqFixed,
                true,
                cost_model,
                name,
                "geq_fixed",
            );
            run::<F, Assigned, Chip>(
                x,
                y,
                x < y,
                64,
                Op::LowerFixed,
                true,
                cost_model,
                name,
                "lower_fixed",
            );
            run::<F, Assigned, Chip>(
                x,
                y,
                x > y,
                64,
                Op::GreaterFixed,
                true,
                cost_model,
                name,
                "greater_fixed",
            );
            cost_model = false;
            // Negative
            run::<F, Assigned, Chip>(x, y, x > y, 64, Op::Leq, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x < y, 64, Op::Geq, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x >= y, 64, Op::Lower, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x <= y, 64, Op::Greater, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x > y, 64, Op::LeqFixed, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x < y, 64, Op::GeqFixed, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x >= y, 64, Op::LowerFixed, false, false, "", "");
            run::<F, Assigned, Chip>(x, y, x <= y, 64, Op::GreaterFixed, false, false, "", "");
        })
    }

    pub fn test_assert_bounded_element<F, Assigned, Chip>(name: &str)
    where
        F: CircuitField + FromUniformBytes<64> + Ord,
        Assigned::Element: CircuitField,
        Assigned: Instantiable<F> + InnerConstants + Clone + Debug,
        Chip: AssignmentInstructions<F, Assigned>
            + AssignmentInstructions<F, AssignedBit<F>>
            + AssertionInstructions<F, AssignedBit<F>>
            + ComparisonInstructions<F, Assigned>
            + DecompositionInstructions<F, Assigned>
            + FromScratch<F>,
    {
        let mut rng = ChaCha8Rng::seed_from_u64(0xc0ffee);
        let x = rng.next_u64();
        let m = u64::MAX;
        let mut cost_model = true;
        [
            (x, 64, true),
            (m, 64, true),
            (m, 63, false),
            (0, 1, true),
            (0, 2, true),
            (2, 1, false),
            (2, 2, true),
            (7, 3, true),
            (8, 3, false),
            (15, 4, true),
            (16, 4, false),
            ((1 << 20) - 1, 20, true),
            (1 << 20, 20, false),
        ]
        .into_iter()
        .for_each(|(x, bound, must_pass)| {
            run::<F, Assigned, Chip>(
                x,
                u64::default(),
                bool::default(),
                bound,
                Op::BoundedOfElement,
                must_pass,
                cost_model,
                name,
                "bounded_of_element",
            );
            cost_model = false;
        })
    }
}