diff --git a/Cargo.lock b/Cargo.lock index f1ea5929d32..4b140a513f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,7 +34,6 @@ dependencies = [ "cfg-if 1.0.0", "hex", "num-bigint", - "num-traits", "serde", ] @@ -2821,6 +2820,7 @@ version = "0.30.0" dependencies = [ "acvm", "base64 0.21.2", + "cfg-if 1.0.0", "chumsky", "fm", "im", @@ -2830,6 +2830,8 @@ dependencies = [ "noirc_arena", "noirc_errors", "noirc_printable_type", + "num-bigint", + "num-traits", "petgraph", "regex", "rustc-hash", diff --git a/acvm-repo/acir_field/Cargo.toml b/acvm-repo/acir_field/Cargo.toml index 8d8a1e9e2bd..f182c6c4aba 100644 --- a/acvm-repo/acir_field/Cargo.toml +++ b/acvm-repo/acir_field/Cargo.toml @@ -16,7 +16,6 @@ repository.workspace = true hex.workspace = true num-bigint.workspace = true serde.workspace = true -num-traits.workspace = true ark-bn254 = { version = "^0.4.0", default-features = false, features = ["curve"] } ark-bls12-381 = { version = "^0.4.0", optional = true, default-features = false, features = ["curve"] } diff --git a/acvm-repo/acir_field/src/lib.rs b/acvm-repo/acir_field/src/lib.rs index 7f06330ed8d..9b8919e4bcb 100644 --- a/acvm-repo/acir_field/src/lib.rs +++ b/acvm-repo/acir_field/src/lib.rs @@ -2,9 +2,6 @@ #![warn(unreachable_pub)] #![warn(clippy::semicolon_if_nothing_returned)] #![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] - -use num_bigint::BigUint; -use num_traits::Num; mod generic_ark; pub use generic_ark::AcirField; @@ -15,40 +12,11 @@ pub use generic_ark::FieldElement as GenericFieldElement; cfg_if::cfg_if! { if #[cfg(feature = "bls12_381")] { pub type FieldElement = generic_ark::FieldElement; - pub const CHOSEN_FIELD : FieldOptions = FieldOptions::BLS12_381; } else { pub type FieldElement = generic_ark::FieldElement; - pub const CHOSEN_FIELD : FieldOptions = FieldOptions::BN254; } } -#[derive(Debug, PartialEq, Eq)] -pub enum FieldOptions { - BN254, - BLS12_381, -} - -impl FieldOptions { - pub fn to_string(&self) -> &str { - match self { - FieldOptions::BN254 => "bn254", - FieldOptions::BLS12_381 => "bls12_381", - } - } - - pub fn is_native_field(str: &str) -> bool { - let big_num = if let Some(hex) = str.strip_prefix("0x") { - BigUint::from_str_radix(hex, 16) - } else { - BigUint::from_str_radix(str, 10) - }; - if let Ok(big_num) = big_num { - big_num == FieldElement::modulus() - } else { - CHOSEN_FIELD.to_string() == str - } - } -} // This is needed because features are additive through the dependency graph; if a dependency turns on the bn254, then it // will be turned on in all crates that depend on it #[macro_export] diff --git a/compiler/noirc_frontend/Cargo.toml b/compiler/noirc_frontend/Cargo.toml index 0430d214d53..381eb4a9bb9 100644 --- a/compiler/noirc_frontend/Cargo.toml +++ b/compiler/noirc_frontend/Cargo.toml @@ -21,13 +21,17 @@ smol_str.workspace = true im.workspace = true serde_json.workspace = true serde.workspace = true +num-bigint.workspace = true +num-traits.workspace = true rustc-hash = "1.1.0" small-ord-set = "0.1.3" regex = "1.9.1" +cfg-if = "1.0.0" tracing.workspace = true petgraph = "0.6" lalrpop-util = { version = "0.20.2", features = ["lexer"] } + [dev-dependencies] base64.workspace = true strum = "0.24" @@ -39,3 +43,5 @@ lalrpop = "0.20.2" [features] experimental_parser = [] +bn254 = [] +bls12_381 = [] diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index 3d0ffdb0155..56a0387cf8d 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -1,8 +1,10 @@ use std::{collections::HashMap, path::Path, vec}; -use acvm::acir::acir_field::FieldOptions; +use acvm::{AcirField, FieldElement}; use fm::{FileId, FileManager, FILE_EXTENSION}; use noirc_errors::Location; +use num_bigint::BigUint; +use num_traits::Num; use crate::ast::{ FunctionDefinition, Ident, ItemVisibility, LetStatement, ModuleDeclaration, NoirFunction, @@ -233,7 +235,7 @@ impl<'a> ModCollector<'a> { for function in functions { // check if optional field attribute is compatible with native field if let Some(field) = function.attributes().get_field_attribute() { - if !FieldOptions::is_native_field(&field) { + if !is_native_field(&field) { continue; } } @@ -721,6 +723,29 @@ fn should_check_siblings_for_module(module_path: &Path, parent_path: &Path) -> b } } +cfg_if::cfg_if! { + if #[cfg(feature = "bn254")] { + pub const CHOSEN_FIELD: &str = "bn254"; + } else if #[cfg(feature = "bls12_381")] { + pub const CHOSEN_FIELD: &str = "bls12_381"; + } else { + compile_error!("please specify a field to compile with"); + } +} + +fn is_native_field(str: &str) -> bool { + let big_num = if let Some(hex) = str.strip_prefix("0x") { + BigUint::from_str_radix(hex, 16) + } else { + BigUint::from_str_radix(str, 10) + }; + if let Ok(big_num) = big_num { + big_num == FieldElement::modulus() + } else { + CHOSEN_FIELD == str + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/compiler/wasm/Cargo.toml b/compiler/wasm/Cargo.toml index 23686cc4ea1..03e59b3e269 100644 --- a/compiler/wasm/Cargo.toml +++ b/compiler/wasm/Cargo.toml @@ -17,7 +17,7 @@ acvm = { workspace = true, features = ["bn254"] } fm.workspace = true nargo.workspace = true noirc_driver.workspace = true -noirc_frontend.workspace = true +noirc_frontend = { workspace = true, features = ["bn254"] } noirc_errors.workspace = true noirc_evaluator.workspace = true wasm-bindgen.workspace = true diff --git a/tooling/nargo_cli/Cargo.toml b/tooling/nargo_cli/Cargo.toml index 9e886bc7002..a9946c8700c 100644 --- a/tooling/nargo_cli/Cargo.toml +++ b/tooling/nargo_cli/Cargo.toml @@ -29,7 +29,7 @@ nargo_toml.workspace = true noir_lsp.workspace = true noir_debugger.workspace = true noirc_driver.workspace = true -noirc_frontend.workspace = true +noirc_frontend = { workspace = true, features = ["bn254"] } noirc_abi.workspace = true noirc_errors.workspace = true acvm = { workspace = true, features = ["bn254"] } diff --git a/tooling/noirc_abi/Cargo.toml b/tooling/noirc_abi/Cargo.toml index 040f8a0dc79..baae2dfa35e 100644 --- a/tooling/noirc_abi/Cargo.toml +++ b/tooling/noirc_abi/Cargo.toml @@ -23,3 +23,13 @@ num-traits = "0.2" [dev-dependencies] strum = "0.24" strum_macros = "0.24" + +[features] +bn254 = [ + "acvm/bn254", + "noirc_frontend/bn254", +] +bls12_381 = [ + "acvm/bls12_381", + "noirc_frontend/bls12_381", +] \ No newline at end of file diff --git a/tooling/noirc_abi_wasm/Cargo.toml b/tooling/noirc_abi_wasm/Cargo.toml index 5692c757d3e..daa619ca01d 100644 --- a/tooling/noirc_abi_wasm/Cargo.toml +++ b/tooling/noirc_abi_wasm/Cargo.toml @@ -13,7 +13,7 @@ crate-type = ["cdylib"] [dependencies] acvm = { workspace = true, features = ["bn254"] } -noirc_abi.workspace = true +noirc_abi = { workspace = true, features = ["bn254"] } iter-extended.workspace = true wasm-bindgen.workspace = true serde.workspace = true