Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

chore!: allow backends to specify support for all opcode variants #273

Merged
merged 13 commits into from
May 16, 2023
Merged
6 changes: 3 additions & 3 deletions acvm/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use acir::{
use indexmap::IndexMap;
use optimizers::GeneralOptimizer;
use thiserror::Error;
use transformers::{CSatTransformer, FallbackTransformer, IsOpcodeSupported, R1CSTransformer};
use transformers::{CSatTransformer, FallbackTransformer, R1CSTransformer};

use self::optimizers::RangeOptimizer;
use self::optimizers::Simplifier;
Expand All @@ -22,10 +22,10 @@ pub enum CompileError {
UnsupportedBlackBox(BlackBoxFunc),
}

pub fn compile(
pub fn compile<F: Fn(&Opcode) -> bool>(
acir: Circuit,
np_language: Language,
is_opcode_supported: IsOpcodeSupported,
is_opcode_supported: F,
simplifier: &Simplifier,
) -> Result<Circuit, CompileError> {
// Instantiate the optimizer.
Expand Down
7 changes: 2 additions & 5 deletions acvm/src/compiler/optimizers/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,7 @@ mod test {
FieldElement,
};

use crate::compiler::{
optimizers::Simplifier,
transformers::{FallbackTransformer, IsOpcodeSupported},
};
use crate::compiler::{optimizers::Simplifier, transformers::FallbackTransformer};

#[test]
fn simplify_test() {
Expand Down Expand Up @@ -479,7 +476,7 @@ mod test {
simplifier.simplify(&mut circuit);
assert_eq!(circuit.len(), 3);
assert_eq!(simplifier.solved_gates.len(), 1);
let support_all: IsOpcodeSupported = |_| true;
let support_all = |_opcode: &Opcode| true;
let mut acir = Circuit::default();
acir.opcodes = circuit;
let acir = FallbackTransformer::transform(acir, support_all, &simplifier).unwrap();
Expand Down
7 changes: 2 additions & 5 deletions acvm/src/compiler/transformers/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ use acir::{
BlackBoxFunc,
};

// A predicate that returns true if the black box function is supported
pub type IsOpcodeSupported = fn(&Opcode) -> bool;

pub struct FallbackTransformer;

impl FallbackTransformer {
//ACIR pass which replace unsupported opcodes using arithmetic fallback
pub fn transform(
pub fn transform<F: Fn(&Opcode) -> bool>(
acir: Circuit,
is_supported: IsOpcodeSupported,
is_supported: F,
simplifier: &Simplifier,
) -> Result<Circuit, CompileError> {
let mut acir_supported_opcodes = Vec::with_capacity(acir.opcodes.len());
Expand Down
1 change: 0 additions & 1 deletion acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ mod r1cs;

pub use csat::CSatTransformer;
pub use fallback::FallbackTransformer;
pub use fallback::IsOpcodeSupported;
pub use r1cs::R1CSTransformer;
8 changes: 3 additions & 5 deletions acvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub trait ProofSystemCompiler {
/// if the language and proof system does not line up.
fn np_language(&self) -> Language;

// Returns true if the backend supports the selected black box function
fn black_box_function_supported(&self, opcode: &BlackBoxFunc) -> bool;
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
// Returns true if the backend supports the selected opcode
fn opcode_supported(&self, opcode: &Opcode) -> bool;
phated marked this conversation as resolved.
Show resolved Hide resolved

/// Returns the number of gates in a circuit
fn get_exact_circuit_size(&self, circuit: &Circuit) -> Result<u32, Self::Error>;
Expand Down Expand Up @@ -227,9 +227,7 @@ pub fn checksum_constraint_system(cs: &Circuit) -> u32 {
// This is set to match the previous functionality that we had
// Where we could deduce what opcodes were supported
// by knowing the np complete language
pub fn default_is_opcode_supported(
language: Language,
) -> compiler::transformers::IsOpcodeSupported {
pub fn default_is_opcode_supported(language: Language) -> impl Fn(&Opcode) -> bool {
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
// R1CS does not support any of the opcode except Arithmetic by default.
// The compiler will replace those that it can -- ie range, xor, and
fn r1cs_is_supported(opcode: &Opcode) -> bool {
Expand Down