pub mod optimizers;
pub mod transformers;
use crate::Language;
use acir::{
circuit::{Circuit, Opcode},
native_types::{Expression, Witness},
BlackBoxFunc, FieldElement,
};
use indexmap::IndexMap;
use optimizers::GeneralOptimizer;
use thiserror::Error;
use transformers::{CSatTransformer, FallbackTransformer, R1CSTransformer};
use self::optimizers::RangeOptimizer;
use self::optimizers::Simplifier;
#[derive(PartialEq, Eq, Debug, Error)]
pub enum CompileError {
#[error("The blackbox function {0} is not supported by the backend and acvm does not have a fallback implementation")]
UnsupportedBlackBox(BlackBoxFunc),
}
pub fn compile(
acir: Circuit,
np_language: Language,
is_opcode_supported: impl Fn(&Opcode) -> bool,
simplifier: &Simplifier,
) -> Result<Circuit, CompileError> {
let acir = FallbackTransformer::transform(acir, is_opcode_supported, simplifier)?;
let mut opcodes: Vec<Opcode> = Vec::new();
for opcode in acir.opcodes {
match opcode {
Opcode::Arithmetic(arith_expr) => {
opcodes.push(Opcode::Arithmetic(GeneralOptimizer::optimize(arith_expr)))
}
other_gate => opcodes.push(other_gate),
};
}
let acir = Circuit { opcodes, ..acir };
let range_optimizer = RangeOptimizer::new(acir);
let acir = range_optimizer.replace_redundant_ranges();
let transformer = match &np_language {
crate::Language::R1CS => {
let transformer = R1CSTransformer::new(acir);
return Ok(transformer.transform());
}
crate::Language::PLONKCSat { width } => CSatTransformer::new(*width),
};
let mut transformed_gates = Vec::new();
let mut next_witness_index = acir.current_witness_index + 1;
let mut intermediate_variables: IndexMap<Expression, (FieldElement, Witness)> = IndexMap::new();
for opcode in acir.opcodes {
match opcode {
Opcode::Arithmetic(arith_expr) => {
let len = intermediate_variables.len();
let arith_expr = transformer.transform(
arith_expr,
&mut intermediate_variables,
&mut next_witness_index,
);
next_witness_index += (intermediate_variables.len() - len) as u32;
let mut new_gates = Vec::new();
for (g, (norm, w)) in intermediate_variables.iter().skip(len) {
let mut intermediate_gate = g * *norm;
intermediate_gate.linear_combinations.push((-FieldElement::one(), *w));
intermediate_gate.sort();
new_gates.push(intermediate_gate);
}
new_gates.push(arith_expr);
new_gates.sort();
for gate in new_gates {
transformed_gates.push(Opcode::Arithmetic(gate));
}
}
other_gate => transformed_gates.push(other_gate),
}
}
let current_witness_index = next_witness_index - 1;
Ok(Circuit {
current_witness_index,
opcodes: transformed_gates,
public_parameters: acir.public_parameters,
return_values: acir.return_values,
})
}