Skip to content

Commit

Permalink
feat: pass in opcode support function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed May 16, 2023
1 parent c3bdec2 commit f24b605
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
7 changes: 6 additions & 1 deletion crates/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ fn check_from_path<B: Backend, P: AsRef<Path>>(
p: P,
compile_options: &CompileOptions,
) -> Result<(), CliError<B>> {
let mut driver = Resolver::resolve_root_manifest(p.as_ref(), backend.np_language())?;
let mut driver = Resolver::resolve_root_manifest(
p.as_ref(),
backend.np_language(),
#[allow(deprecated)]
Box::new(acvm::default_is_opcode_supported(backend.np_language())),
)?;

driver.check_crate(compile_options).map_err(|_| CliError::CompilationError)?;

Expand Down
7 changes: 6 additions & 1 deletion crates/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ fn setup_driver<B: Backend>(
backend: &B,
program_dir: &Path,
) -> Result<Driver, DependencyResolutionError> {
Resolver::resolve_root_manifest(program_dir, backend.np_language())
Resolver::resolve_root_manifest(
program_dir,
backend.np_language(),
#[allow(deprecated)]
Box::new(acvm::default_is_opcode_supported(backend.np_language())),
)
}

pub(crate) fn compile_circuit<B: Backend>(
Expand Down
7 changes: 6 additions & 1 deletion crates/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub fn prove_and_verify(proof_name: &str, program_dir: &Path, show_ssa: bool) ->
// FIXME: I not sure that this is the right place for this tests.
#[cfg(test)]
mod tests {
use acvm::{acir::circuit::Opcode, default_is_opcode_supported};
use noirc_driver::Driver;
use noirc_frontend::graph::CrateType;

Expand All @@ -128,7 +129,11 @@ mod tests {
///
/// This is used for tests.
fn file_compiles<P: AsRef<Path>>(root_file: P) -> bool {
let mut driver = Driver::new(&acvm::Language::R1CS);
let mut driver = Driver::new(
&acvm::Language::R1CS,
#[allow(deprecated)]
Box::new(default_is_opcode_supported(acvm::Language::R1CS)),
);
driver.create_local_crate(&root_file, CrateType::Binary);
crate::resolver::add_std_lib(&mut driver);
driver.file_compiles()
Expand Down
7 changes: 6 additions & 1 deletion crates/nargo_cli/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ fn run_tests<B: Backend>(
test_name: &str,
compile_options: &CompileOptions,
) -> Result<(), CliError<B>> {
let mut driver = Resolver::resolve_root_manifest(program_dir, backend.np_language())?;
let mut driver = Resolver::resolve_root_manifest(
program_dir,
backend.np_language(),
#[allow(deprecated)]
Box::new(acvm::default_is_opcode_supported(backend.np_language())),
)?;

driver.check_crate(compile_options).map_err(|_| CliError::CompilationError)?;

Expand Down
5 changes: 3 additions & 2 deletions crates/nargo_cli/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use acvm::Language;
use acvm::{acir::circuit::Opcode, Language};
use nargo::manifest::{Dependency, PackageManifest};
use noirc_driver::Driver;
use noirc_frontend::graph::{CrateId, CrateName, CrateType};
Expand Down Expand Up @@ -71,8 +71,9 @@ impl<'a> Resolver<'a> {
pub(crate) fn resolve_root_manifest(
dir_path: &std::path::Path,
np_language: Language,
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,
) -> Result<Driver, DependencyResolutionError> {
let mut driver = Driver::new(&np_language);
let mut driver = Driver::new(&np_language, is_opcode_supported);
let (entry_path, crate_type) = super::lib_or_bin(dir_path)?;

let manifest_path = super::find_package_manifest(dir_path)?;
Expand Down
15 changes: 9 additions & 6 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]

use acvm::acir::circuit::Opcode;
use acvm::Language;
use clap::Args;
use contract::ContractFunction;
Expand Down Expand Up @@ -56,19 +57,21 @@ impl Default for CompileOptions {
}

impl Driver {
pub fn new(np_language: &Language) -> Self {
let mut driver = Driver { context: Context::default(), language: np_language.clone() };
driver.context.def_interner.set_language(np_language);
pub fn new(language: &Language, is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>) -> Self {
let mut driver = Driver { context: Context::default(), language: language.clone() };
driver.context.def_interner.set_language(language);
driver.context.def_interner.set_opcode_support(is_opcode_supported);
driver
}

// This is here for backwards compatibility
// with the restricted version which only uses one file
pub fn compile_file(
root_file: PathBuf,
np_language: acvm::Language,
language: &Language,
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,
) -> Result<CompiledProgram, ReportedError> {
let mut driver = Driver::new(&np_language);
let mut driver = Driver::new(language, is_opcode_supported);
driver.create_local_crate(root_file, CrateType::Binary);
driver.compile_main(&CompileOptions::default())
}
Expand Down Expand Up @@ -318,6 +321,6 @@ impl Driver {

impl Default for Driver {
fn default() -> Self {
Self::new(&Language::R1CS)
Self::new(&Language::R1CS, Box::new(|opcode| matches!(opcode, Opcode::Arithmetic(_))))
}
}
6 changes: 5 additions & 1 deletion crates/noirc_driver/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use acvm::acir::circuit::Opcode;
use noirc_driver::{CompileOptions, Driver};
use noirc_frontend::graph::{CrateType, LOCAL_CRATE};
fn main() {
const EXTERNAL_DIR: &str = "dep_b/lib.nr";
const EXTERNAL_DIR2: &str = "dep_a/lib.nr";
const ROOT_DIR_MAIN: &str = "example_real_project/main.nr";

let mut driver = Driver::new(&acvm::Language::R1CS);
let mut driver = Driver::new(
&acvm::Language::R1CS,
Box::new(|opcode| matches!(opcode, Opcode::Arithmetic(_))),
);

// Add local crate to dep graph
driver.create_local_crate(ROOT_DIR_MAIN, CrateType::Binary);
Expand Down
10 changes: 8 additions & 2 deletions crates/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct NodeInterner {

//used for fallback mechanism
language: Language,
is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>,

delayed_type_checks: Vec<TypeCheckFn>,

Expand Down Expand Up @@ -259,6 +260,8 @@ impl Default for NodeInterner {
next_type_variable_id: 0,
globals: HashMap::new(),
language: Language::R1CS,
#[allow(deprecated)]
is_opcode_supported: Box::new(acvm::default_is_opcode_supported(Language::R1CS)),
delayed_type_checks: vec![],
struct_methods: HashMap::new(),
primitive_methods: HashMap::new(),
Expand Down Expand Up @@ -583,14 +586,17 @@ impl NodeInterner {
self.language = language.clone();
}

pub fn set_opcode_support(&mut self, is_opcode_supported: Box<dyn Fn(&Opcode) -> bool>) {
self.is_opcode_supported = is_opcode_supported;
}

#[allow(deprecated)]
pub fn foreign(&self, opcode: &str) -> bool {
let is_supported = acvm::default_is_opcode_supported(self.language.clone());
let black_box_func = match acvm::acir::BlackBoxFunc::lookup(opcode) {
Some(black_box_func) => black_box_func,
None => return false,
};
is_supported(&Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
(self.is_opcode_supported)(&Opcode::BlackBoxFuncCall(BlackBoxFuncCall {
name: black_box_func,
inputs: Vec::new(),
outputs: Vec::new(),
Expand Down
6 changes: 5 additions & 1 deletion crates/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ pub fn compile(args: JsValue) -> JsValue {

// For now we default to plonk width = 3, though we can add it as a parameter
let language = acvm::Language::PLONKCSat { width: 3 };
let mut driver = noirc_driver::Driver::new(&language);
let mut driver = noirc_driver::Driver::new(
&language,
#[allow(deprecated)]
Box::new(acvm::default_is_opcode_supported(language.clone())),
);

let path = PathBuf::from(&options.entry_point);
driver.create_local_crate(path, CrateType::Binary);
Expand Down

0 comments on commit f24b605

Please sign in to comment.