ACVM
optimise R1CS using the general optimiser
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use acir::{
circuit::{Circuit, Gate},
native_types::{Arithmetic, Witness},
- optimiser::CSatOptimiser,
+ optimiser::{CSatOptimiser, GeneralOptimiser},
};
use crate::BackendPointer;
@@ -14,7 +14,7 @@ pub fn compile(acir: Circuit, backend: BackendPointer) -> Circuit {
// Instantiate the optimiser.
// Currently the optimiser and reducer are one in the same
let optimiser = match backend.np_language() {
- crate::Language::R1CS => todo!(),
+ crate::Language::R1CS => return optimise_r1cs(acir),
crate::Language::PLONKCSat { width } => CSatOptimiser::new(width),
};
@@ -46,3 +46,27 @@ pub fn compile(acir: Circuit, backend: BackendPointer) -> Circuit {
public_inputs: acir.public_inputs, // The optimiser does not add public inputs
}
}
+
+// R1CS optimisations uses the general optimiser.
+// Once R1CS specific optimisations are found, then we can
+// refactor this function
+fn optimise_r1cs(acir: Circuit) -> Circuit {
+ let optimised_arith_gates: Vec<_> = acir
+ .gates
+ .into_iter()
+ .map(|gate| match gate {
+ Gate::Arithmetic(arith) => Gate::Arithmetic(GeneralOptimiser::optimise(arith)),
+ other_gates => other_gates,
+ })
+ .collect();
+
+ Circuit {
+ // The general optimiser may remove enough gates that a witness is no longer used
+ // however, we cannot decrement the number of witnesses, as that
+ // would require a linear scan over all gates in order to decrement all witness indices
+ // above the witness which was removed
+ current_witness_index: acir.current_witness_index,
+ gates: optimised_arith_gates,
+ public_inputs: acir.public_inputs,
+ }
+}
