Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move is_native_field up into noirc_frontend #5119

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion acvm-repo/acir_field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", optional = true, default-features = false, features = [
"curve",
Expand Down
33 changes: 0 additions & 33 deletions acvm-repo/acir_field/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,18 @@
#![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;

cfg_if::cfg_if! {
if #[cfg(feature = "bn254")] {
mod generic_ark;
pub type FieldElement = generic_ark::FieldElement<ark_bn254::Fr>;
pub const CHOSEN_FIELD : FieldOptions = FieldOptions::BN254;

} else if #[cfg(feature = "bls12_381")] {
mod generic_ark;
pub type FieldElement = generic_ark::FieldElement<ark_bls12_381::Fr>;
pub const CHOSEN_FIELD : FieldOptions = FieldOptions::BLS12_381;
} else {
compile_error!("please specify a field to compile with");
}
}

#[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]
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,4 +42,7 @@ tempfile.workspace = true
lalrpop = "0.20.2"

[features]
default=["bn254"]
experimental_parser = []
bn254 = []
bls12_381 = []
29 changes: 27 additions & 2 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{collections::HashMap, path::Path, vec};

use acvm::acir::acir_field::FieldOptions;
use acvm::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,
Expand Down Expand Up @@ -233,7 +235,7 @@
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;
}
}
Expand Down Expand Up @@ -482,7 +484,7 @@
}
}
TraitItem::Type { name } => {
// TODO(nickysn or alexvitkov): implement context.def_interner.push_empty_type_alias and get an id, instead of using TypeAliasId::dummy_id()

Check warning on line 487 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (nickysn)

Check warning on line 487 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (alexvitkov)
if let Err((first_def, second_def)) = self.def_collector.def_map.modules
[trait_id.0.local_id.0]
.declare_type_alias(name.clone(), TypeAliasId::dummy_id())
Expand Down Expand Up @@ -721,6 +723,29 @@
}
}

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::*;
Expand Down
Loading