Skip to content

Commit

Permalink
feat: add conditional compilation of methods based on the underlying …
Browse files Browse the repository at this point in the history
…field being used (#3045)

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
guipublic and kevaundray authored Oct 9, 2023
1 parent 74f4a88 commit 2e008e2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions acvm-repo/acir_field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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
26 changes: 25 additions & 1 deletion acvm-repo/acir_field/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#![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;
Expand All @@ -18,12 +21,33 @@ cfg_if::cfg_if! {
}
}

#[derive(Debug)]
#[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
8 changes: 8 additions & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::vec;

use acvm::acir::acir_field::FieldOptions;
use fm::FileId;
use noirc_errors::Location;

Expand Down Expand Up @@ -202,6 +203,13 @@ impl<'a> ModCollector<'a> {
let module = ModuleId { krate, local_id: self.module_id };

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) {
continue;
}
}

let name = function.name_ident().clone();
let func_id = context.def_interner.push_empty_fn();

Expand Down
17 changes: 16 additions & 1 deletion compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ impl Attributes {
_ => None,
})
}

pub fn get_field_attribute(&self) -> Option<String> {
for secondary in &self.secondary {
if let SecondaryAttribute::Field(field) = secondary {
return Some(field.to_lowercase());
}
}
None
}
}

/// An Attribute can be either a Primary Attribute or a Secondary Attribute
Expand Down Expand Up @@ -466,6 +475,10 @@ impl Attribute {
None => return Err(malformed_scope),
}
}
["field", name] => {
validate(name)?;
Attribute::Secondary(SecondaryAttribute::Field(name.to_string()))
}
// Secondary attributes
["deprecated"] => Attribute::Secondary(SecondaryAttribute::Deprecated(None)),
["contract_library_method"] => {
Expand Down Expand Up @@ -550,6 +563,7 @@ pub enum SecondaryAttribute {
// the entry point.
ContractLibraryMethod,
Event,
Field(String),
Custom(String),
}

Expand All @@ -563,6 +577,7 @@ impl fmt::Display for SecondaryAttribute {
SecondaryAttribute::Custom(ref k) => write!(f, "#[{k}]"),
SecondaryAttribute::ContractLibraryMethod => write!(f, "#[contract_library_method]"),
SecondaryAttribute::Event => write!(f, "#[event]"),
SecondaryAttribute::Field(ref k) => write!(f, "#[field({k})]"),
}
}
}
Expand All @@ -583,7 +598,7 @@ impl AsRef<str> for SecondaryAttribute {
match self {
SecondaryAttribute::Deprecated(Some(string)) => string,
SecondaryAttribute::Deprecated(None) => "",
SecondaryAttribute::Custom(string) => string,
SecondaryAttribute::Custom(string) | SecondaryAttribute::Field(string) => string,
SecondaryAttribute::ContractLibraryMethod => "",
SecondaryAttribute::Event => "",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "field_attribute"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Test integer addition: 3 + 4 = 7
fn main(mut x: u32) {
assert(x > foo());
}

#[field(bn254)]
fn foo() -> u32 {
1
}

#[field(23)]
fn foo() -> u32 {
2
}

#[field(bls12_381)]
fn foo() -> u32 {
3
}

0 comments on commit 2e008e2

Please sign in to comment.