acvm/compiler/optimizers/
merge_expressions.rs

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
use std::collections::{BTreeMap, BTreeSet, HashMap};

use acir::{
    AcirField,
    circuit::{
        Circuit, Opcode,
        brillig::{BrilligInputs, BrilligOutputs},
        opcodes::BlockId,
    },
    native_types::{Expression, Witness},
};

use crate::compiler::{CircuitSimulator, optimizers::GeneralOptimizer};

pub(crate) struct MergeExpressionsOptimizer<F: AcirField> {
    resolved_blocks: HashMap<BlockId, BTreeSet<Witness>>,
    modified_gates: HashMap<usize, Opcode<F>>,
    deleted_gates: BTreeSet<usize>,
}

impl<F: AcirField> MergeExpressionsOptimizer<F> {
    pub(crate) fn new() -> Self {
        MergeExpressionsOptimizer {
            resolved_blocks: HashMap::new(),
            modified_gates: HashMap::new(),
            deleted_gates: BTreeSet::new(),
        }
    }
    /// This pass analyzes the circuit and identifies intermediate variables that are
    /// only used in two AssertZero opcodes. It then merges the opcode which produces the
    /// intermediate variable into the second one that uses it
    ///
    /// The first pass maps witnesses to the index of the opcodes using them.
    /// Public inputs are not considered because they cannot be simplified.
    /// Witnesses used by MemoryInit opcodes are put in a separate map and marked as used by a Brillig call
    /// if the memory block is an input to the call.
    ///
    /// The second pass looks for AssertZero opcodes having a witness which is only used by another arithmetic opcode.
    /// In that case, the opcode with the smallest index is merged into the other one via Gaussian elimination.
    /// For instance, if we have 'w1' used only by these two opcodes,
    /// `5*w2*w3` and `w1`:
    /// w2*w3 + 2*w2 + w1 + w3 = 0   // This opcode 'defines' the variable w1
    /// 2*w3*w4 + w1 + w4 = 0        // which is only used here
    ///
    /// For w1 we can say:
    /// w1 = -1/2*w2*w3 - w2 - 1/2*w3
    ///
    /// Then we will remove the first one and modify the second one like this:
    /// 2*w3*w4 + w4 - w2 - 1/2*w3 - 1/2*w2*w3 = 0
    ///
    /// This transformation is relevant for Plonk-ish backends although they have a limited width because
    /// they can potentially handle expressions with large linear combinations using 'big-add' gates.
    ///
    /// Pre-condition:
    /// - This pass is only relevant for backends that can handle unlimited width
    /// - CSAT pass should have been run prior to this one.
    pub(crate) fn eliminate_intermediate_variable(
        &mut self,
        circuit: &Circuit<F>,
        acir_opcode_positions: Vec<usize>,
    ) -> (Vec<Opcode<F>>, Vec<usize>) {
        // Initialization
        self.modified_gates.clear();
        self.deleted_gates.clear();
        self.resolved_blocks.clear();

        // Keep track, for each witness, of the gates that use it
        let circuit_io: BTreeSet<Witness> =
            circuit.circuit_arguments().union(&circuit.public_inputs().0).cloned().collect();

        let mut used_witness: BTreeMap<Witness, BTreeSet<usize>> = BTreeMap::new();
        for (i, opcode) in circuit.opcodes.iter().enumerate() {
            let witnesses = self.witness_inputs(opcode);
            if let Opcode::MemoryInit { block_id, .. } = opcode {
                self.resolved_blocks.insert(*block_id, witnesses.clone());
            }
            for w in witnesses {
                // We do not simplify circuit inputs and outputs
                if !circuit_io.contains(&w) {
                    used_witness.entry(w).or_default().insert(i);
                }
            }
        }

        // For each opcode, try to get a target opcode to merge with
        for (i, opcode) in circuit.opcodes.iter().enumerate() {
            if !matches!(opcode, Opcode::AssertZero(_)) {
                continue;
            }
            if let Some(opcode) = self.get_opcode(i, circuit) {
                let input_witnesses = self.witness_inputs(&opcode);
                for w in input_witnesses {
                    let Some(gates_using_w) = used_witness.get(&w) else {
                        continue;
                    };
                    // We only consider witness which are used in exactly two arithmetic gates
                    if gates_using_w.len() == 2 {
                        let first = *gates_using_w.first().expect("gates_using_w.len == 2");
                        let second = *gates_using_w.last().expect("gates_using_w.len == 2");
                        let b = if second == i {
                            first
                        } else {
                            // sanity check
                            assert!(i == first);
                            second
                        };
                        // Merge the opcode with smaller index into the other one
                        // by updating modified_gates/deleted_gates/used_witness
                        // returns false if it could not merge them
                        let mut merge_opcodes = |op1, op2| -> bool {
                            if op1 == op2 {
                                return false;
                            }
                            let (source, target) = if op1 < op2 { (op1, op2) } else { (op2, op1) };
                            let source_opcode = self.get_opcode(source, circuit);
                            let target_opcode = self.get_opcode(target, circuit);

                            if let (
                                Some(Opcode::AssertZero(expr_use)),
                                Some(Opcode::AssertZero(expr_define)),
                            ) = (target_opcode, source_opcode)
                            {
                                if let Some(expr) =
                                    Self::merge_expression(&expr_use, &expr_define, w)
                                {
                                    self.modified_gates.insert(target, Opcode::AssertZero(expr));
                                    self.deleted_gates.insert(source);
                                    // Update the 'used_witness' map to account for the merge.
                                    let witness_list = CircuitSimulator::expr_wit(&expr_use);
                                    let witness_list = witness_list
                                        .chain(CircuitSimulator::expr_wit(&expr_define));

                                    for w2 in witness_list {
                                        if !circuit_io.contains(&w2) {
                                            used_witness.entry(w2).and_modify(|v| {
                                                v.insert(target);
                                                v.remove(&source);
                                            });
                                        }
                                    }
                                    return true;
                                }
                            }
                            false
                        };

                        if merge_opcodes(b, i) {
                            // We need to stop here and continue with the next opcode
                            // because the merge invalidates the current opcode.
                            break;
                        }
                    }
                }
            }
        }

        // Construct the new circuit from modified/deleted gates
        let mut new_circuit = Vec::new();
        let mut new_acir_opcode_positions = Vec::new();

        for (i, opcode_position) in acir_opcode_positions.iter().enumerate() {
            if let Some(op) = self.get_opcode(i, circuit) {
                new_circuit.push(op);
                new_acir_opcode_positions.push(*opcode_position);
            }
        }
        (new_circuit, new_acir_opcode_positions)
    }

    fn for_each_brillig_input_wit(&self, input: &BrilligInputs<F>, mut f: impl FnMut(Witness)) {
        match input {
            BrilligInputs::Single(expr) => {
                for witness in CircuitSimulator::expr_wit(expr) {
                    f(witness);
                }
            }
            BrilligInputs::Array(exprs) => {
                for expr in exprs {
                    for witness in CircuitSimulator::expr_wit(expr) {
                        f(witness);
                    }
                }
            }
            BrilligInputs::MemoryArray(block_id) => {
                for witness in self.resolved_blocks.get(block_id).expect("Unknown block id") {
                    f(*witness);
                }
            }
        }
    }

    fn for_each_brillig_output_wit(&self, output: &BrilligOutputs, mut f: impl FnMut(Witness)) {
        match output {
            BrilligOutputs::Simple(witness) => f(*witness),
            BrilligOutputs::Array(witnesses) => {
                for witness in witnesses {
                    f(*witness);
                }
            }
        }
    }

    // Returns the input witnesses used by the opcode
    fn witness_inputs(&self, opcode: &Opcode<F>) -> BTreeSet<Witness> {
        match opcode {
            Opcode::AssertZero(expr) => CircuitSimulator::expr_wit(expr).collect(),
            Opcode::BlackBoxFuncCall(bb_func) => {
                let mut witnesses = bb_func.get_input_witnesses();
                witnesses.extend(bb_func.get_outputs_vec());

                witnesses
            }
            Opcode::MemoryOp { block_id: _, op } => {
                //index and value
                let witnesses = CircuitSimulator::expr_wit(&op.index);
                witnesses.chain(CircuitSimulator::expr_wit(&op.value)).collect()
            }

            Opcode::MemoryInit { block_id: _, init, block_type: _ } => {
                init.iter().cloned().collect()
            }
            Opcode::BrilligCall { inputs, outputs, .. } => {
                let mut witnesses = BTreeSet::new();
                for i in inputs {
                    self.for_each_brillig_input_wit(i, |witness| {
                        witnesses.insert(witness);
                    });
                }
                for i in outputs {
                    self.for_each_brillig_output_wit(i, |witness| {
                        witnesses.insert(witness);
                    });
                }
                witnesses
            }
            Opcode::Call { id: _, inputs, outputs, predicate } => {
                let mut witnesses: BTreeSet<Witness> = inputs.iter().copied().collect();
                witnesses.extend(outputs);

                if let Some(p) = predicate {
                    witnesses.extend(CircuitSimulator::expr_wit(p));
                }
                witnesses
            }
        }
    }

    // Merge 'expr' into 'target' via Gaussian elimination on 'w'
    // Returns None if the expressions cannot be merged
    fn merge_expression(
        target: &Expression<F>,
        expr: &Expression<F>,
        w: Witness,
    ) -> Option<Expression<F>> {
        // Check that the witness is not part of multiplication terms
        for m in &target.mul_terms {
            if m.1 == w || m.2 == w {
                return None;
            }
        }
        for m in &expr.mul_terms {
            if m.1 == w || m.2 == w {
                return None;
            }
        }

        for k in &target.linear_combinations {
            if k.1 == w {
                for i in &expr.linear_combinations {
                    if i.1 == w {
                        let expr = target.add_mul(-(k.0 / i.0), expr);
                        let expr = GeneralOptimizer::optimize(expr);
                        return Some(expr);
                    }
                }
            }
        }
        None
    }

    /// Returns the 'updated' opcode at index 'g' in the circuit
    /// The modifications to the circuits are stored with 'deleted_gates' and 'modified_gates'
    /// These structures are used to give the 'updated' opcode.
    /// For instance, if the opcode has been deleted inside 'deleted_gates', then it returns None.
    fn get_opcode(&self, g: usize, circuit: &Circuit<F>) -> Option<Opcode<F>> {
        if self.deleted_gates.contains(&g) {
            return None;
        }
        self.modified_gates.get(&g).or(circuit.opcodes.get(g)).cloned()
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        assert_circuit_snapshot,
        compiler::{CircuitSimulator, optimizers::MergeExpressionsOptimizer},
    };
    use acir::{FieldElement, circuit::Circuit};

    fn merge_expressions(circuit: Circuit<FieldElement>) -> Circuit<FieldElement> {
        assert!(CircuitSimulator::default().check_circuit(&circuit).is_none());
        let mut merge_optimizer = MergeExpressionsOptimizer::new();
        let acir_opcode_positions = vec![0; 20];
        let (opcodes, _) =
            merge_optimizer.eliminate_intermediate_variable(&circuit, acir_opcode_positions);
        let mut optimized_circuit = circuit;
        optimized_circuit.opcodes = opcodes;

        // check that the circuit is still valid after optimization
        assert!(CircuitSimulator::default().check_circuit(&optimized_circuit).is_none());
        optimized_circuit
    }

    #[test]
    fn merges_expressions() {
        let src = "
        private parameters: [w0]
        public parameters: []
        return values: [w2]
        ASSERT 2*w1 = w0 + 5
        ASSERT w2 = 4*w1 + 4
        ";
        let circuit = Circuit::from_str(src).unwrap();
        let optimized_circuit = merge_expressions(circuit.clone());
        assert_circuit_snapshot!(optimized_circuit, @r"
        private parameters: [w0]
        public parameters: []
        return values: [w2]
        ASSERT w2 = 2*w0 + 14
        ");
    }

    #[test]
    fn does_not_eliminate_witnesses_returned_from_brillig() {
        let src = "
        private parameters: [w0]
        public parameters: []
        return values: []
        BRILLIG CALL func: 0, inputs: [], outputs: [w1]
        ASSERT 2*w0 + 3*w1 + w2 + 1 = 0
        ASSERT 2*w0 + 2*w1 + w5 + 1 = 0
        ";
        let circuit = Circuit::from_str(src).unwrap();
        let optimized_circuit = merge_expressions(circuit.clone());
        assert_eq!(circuit, optimized_circuit);
    }

    #[test]
    fn does_not_eliminate_witnesses_returned_from_circuit() {
        let src = "
        private parameters: [w0]
        public parameters: []
        return values: [w1, w2]
        ASSERT -w0*w0 + w1 = 0
        ASSERT -w1 + w2 = 0
        ";
        let circuit = Circuit::from_str(src).unwrap();
        let optimized_circuit = merge_expressions(circuit.clone());
        assert_eq!(circuit, optimized_circuit);
    }

    #[test]
    fn does_not_attempt_to_merge_into_previous_opcodes() {
        let src = "
        private parameters: [w0, w1]
        public parameters: []
        return values: []
        ASSERT w0*w0 - w4 = 0
        ASSERT w0*w1 + w5 = 0
        ASSERT -w2 + w4 + w5 = 0
        ASSERT w2 - w3 + w4 + w5 = 0
        BLACKBOX::RANGE input: w3, bits: 32
        ";
        let circuit = Circuit::from_str(src).unwrap();

        let optimized_circuit = merge_expressions(circuit);
        assert_circuit_snapshot!(optimized_circuit, @r"
        private parameters: [w0, w1]
        public parameters: []
        return values: []
        ASSERT w5 = -w0*w1
        ASSERT w3 = 2*w0*w0 + 2*w5
        BLACKBOX::RANGE input: w3, bits: 32
        ");
    }

    #[test]
    fn takes_blackbox_opcode_outputs_into_account() {
        // Regression test for https://github.com/noir-lang/noir/issues/6527
        // Previously we would not track the usage of witness 4 in the output of the blackbox function.
        // We would then merge the final two opcodes losing the check that the brillig call must match
        // with `w0 ^ w1`.
        let src = "
        private parameters: [w0, w1]
        public parameters: []
        return values: [w2]
        BRILLIG CALL func: 0, inputs: [], outputs: [w3]
        BLACKBOX::AND inputs: [w0, w1], bits: 8, output: w4
        ASSERT w3 - w4 = 0
        ASSERT -w2 + w4 = 0
        ";
        let circuit = Circuit::from_str(src).unwrap();
        let optimized_circuit = merge_expressions(circuit.clone());
        assert_eq!(circuit, optimized_circuit);
    }
}