fullcodec_plonk/constraint_system/
boolean.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4//
5// Copyright (c) DUSK NETWORK. All rights reserved.
6
7use crate::constraint_system::{Constraint, TurboComposer, Witness};
8use dusk_bls12_381::BlsScalar;
9
10impl TurboComposer {
11    /// Adds a boolean constraint (also known as binary constraint) where the
12    /// gate eq. will enforce that the [`Witness`] received is either `0` or `1`
13    /// by adding a constraint in the circuit.
14    ///
15    /// Note that using this constraint with whatever [`Witness`] that
16    /// is not representing a value equalling 0 or 1, will always force the
17    /// equation to fail.
18    pub fn component_boolean(&mut self, a: Witness) {
19        let zero = Self::constant_zero();
20        let constraint = Constraint::new()
21            .mult(1)
22            .output(-BlsScalar::one())
23            .a(a)
24            .b(a)
25            .o(a)
26            .d(zero);
27
28        self.append_gate(constraint);
29    }
30}
31
32#[cfg(feature = "std")]
33#[cfg(test)]
34mod tests {
35    use crate::constraint_system::{helper, TurboComposer};
36    use dusk_bls12_381::BlsScalar;
37
38    #[test]
39    fn test_correct_bool_gate() {
40        let res = helper::gadget_tester(
41            |composer| {
42                let zero = TurboComposer::constant_zero();
43                let one = composer.append_witness(BlsScalar::one());
44
45                composer.component_boolean(zero);
46                composer.component_boolean(one);
47            },
48            32,
49        );
50        assert!(res.is_ok())
51    }
52
53    #[test]
54    fn test_incorrect_bool_gate() {
55        let res = helper::gadget_tester(
56            |composer| {
57                let zero = composer.append_witness(BlsScalar::from(5));
58                let one = composer.append_witness(BlsScalar::one());
59
60                composer.component_boolean(zero);
61                composer.component_boolean(one);
62            },
63            32,
64        );
65        assert!(res.is_err())
66    }
67}