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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) DUSK NETWORK. All rights reserved.

use crate::constraint_system::{Constraint, TurboComposer, Witness};
use dusk_bls12_381::BlsScalar;

impl TurboComposer {
    /// Evaluate and return `o` by appending a new constraint into the circuit.
    ///
    /// The output of the constraint will be overwritten with
    /// `o := q_l · a + q_r · b + q_4 · d + q_c + PI`
    pub fn gate_add(&mut self, s: Constraint) -> Witness {
        let s = Constraint::arithmetic(&s).output(-BlsScalar::one());

        let o = self.append_output_witness(s);
        let s = s.o(o);

        self.append_gate(s);

        o
    }

    /// Evaluate and return `o` by appending a new constraint into the circuit.
    ///
    /// The output of the constraint will be overwritten with
    /// `o := q_m · a · b + q_4 · d + q_c + PI`
    pub fn gate_mul(&mut self, s: Constraint) -> Witness {
        let s = Constraint::arithmetic(&s).output(-BlsScalar::one());

        let o = self.append_output_witness(s);
        let s = s.o(o);

        self.append_gate(s);

        o
    }
}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
    use crate::constraint_system::{helper, Constraint};
    use dusk_bls12_381::BlsScalar;

    #[test]
    fn test_public_inputs() {
        helper::gadget_tester(
            |composer| {
                let one = composer.append_witness(BlsScalar::one());

                composer.append_dummy_gates();

                let constraint =
                    Constraint::new().left(1).right(1).public(1).a(one).b(one);
                let should_be_three = composer.gate_add(constraint);

                composer.assert_equal_constant(
                    should_be_three,
                    BlsScalar::from(3),
                    None,
                );

                let constraint =
                    Constraint::new().left(1).right(1).public(2).a(one).b(one);
                let should_be_four = composer.gate_add(constraint);

                composer.assert_equal_constant(
                    should_be_four,
                    BlsScalar::from(4),
                    None,
                );
            },
            200,
        )
        .expect("Failed to test circuit public inputs");
    }

    #[test]
    fn test_correct_add_mul_gate() {
        let res = helper::gadget_tester(
            |composer| {
                // Verify that (4+5+5) * (6+7+7) = 280
                let four = composer.append_witness(BlsScalar::from(4));
                let five = composer.append_witness(BlsScalar::from(5));
                let six = composer.append_witness(BlsScalar::from(6));
                let seven = composer.append_witness(BlsScalar::from(7));

                let constraint = Constraint::new()
                    .left(1)
                    .right(1)
                    .fourth(1)
                    .a(four)
                    .b(five)
                    .d(five);
                let fourteen = composer.gate_add(constraint);

                let constraint = Constraint::new()
                    .left(1)
                    .right(1)
                    .fourth(1)
                    .a(six)
                    .b(seven)
                    .d(seven);
                let twenty = composer.gate_add(constraint);

                // There are quite a few ways to check the equation is correct,
                // depending on your circumstance If we already
                // have the output wire, we can constrain the output of the
                // mul_gate to be equal to it If we do not, we
                // can compute it using the `mul` If the output
                // is public, we can also constrain the output wire of the mul
                // gate to it. This is what this test does
                let constraint =
                    Constraint::new().mult(1).a(fourteen).b(twenty);
                let output = composer.gate_mul(constraint);

                composer.assert_equal_constant(
                    output,
                    BlsScalar::from(280),
                    None,
                );
            },
            200,
        );
        assert!(res.is_ok());
    }

    #[test]
    fn test_correct_add_gate() {
        helper::gadget_tester(
            |composer| {
                let one = composer.append_witness(BlsScalar::one());

                let constraint = Constraint::new().left(1).constant(2).a(one);
                let c = composer.gate_add(constraint);

                composer.assert_equal_constant(c, BlsScalar::from(3), None);
            },
            32,
        )
        .expect("Circuit consistency failed");
    }

    #[test]
    fn test_correct_big_add_mul_gate() {
        let res = helper::gadget_tester(
            |composer| {
                // Verify that (4+5+5) * (6+7+7) + (8*9) = 352
                let four = composer.append_witness(BlsScalar::from(4));
                let five = composer.append_witness(BlsScalar::from(5));
                let six = composer.append_witness(BlsScalar::from(6));
                let seven = composer.append_witness(BlsScalar::from(7));
                let nine = composer.append_witness(BlsScalar::from(9));

                let constraint = Constraint::new()
                    .left(1)
                    .right(1)
                    .fourth(1)
                    .a(four)
                    .b(five)
                    .d(five);
                let fourteen = composer.gate_add(constraint);

                let constraint = Constraint::new()
                    .left(1)
                    .right(1)
                    .fourth(1)
                    .a(six)
                    .b(seven)
                    .d(seven);
                let twenty = composer.gate_add(constraint);

                let constraint = Constraint::new()
                    .mult(1)
                    .fourth(8)
                    .a(fourteen)
                    .b(twenty)
                    .d(nine);
                let output = composer.gate_mul(constraint);

                composer.assert_equal_constant(
                    output,
                    BlsScalar::from(352),
                    None,
                );
            },
            200,
        );
        assert!(res.is_ok());
    }

    #[test]
    fn test_incorrect_add_mul_gate() {
        let res = helper::gadget_tester(
            |composer| {
                // Verify that (5+5) * (6+7) != 117
                let five = composer.append_witness(BlsScalar::from(5));
                let six = composer.append_witness(BlsScalar::from(6));
                let seven = composer.append_witness(BlsScalar::from(7));

                let constraint =
                    Constraint::new().left(1).right(1).a(five).b(five);
                let five_plus_five = composer.gate_add(constraint);

                let constraint =
                    Constraint::new().left(1).right(1).a(six).b(seven);
                let six_plus_seven = composer.gate_add(constraint);

                let constraint = Constraint::new()
                    .mult(1)
                    .a(five_plus_five)
                    .b(six_plus_seven);
                let output = composer.gate_mul(constraint);

                composer.assert_equal_constant(
                    output,
                    BlsScalar::from(117),
                    None,
                );
            },
            200,
        );
        assert!(res.is_err());
    }
}