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

Add MontFp macros replacement to generate field literals #695

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions ff-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ num-traits.workspace = true

[lib]
proc-macro = true

[features]
zkllvm = []
9 changes: 8 additions & 1 deletion ff-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![forbid(unsafe_code)]

use num_bigint::BigUint;
use proc_macro::TokenStream;
use proc_macro::{Literal, TokenStream, TokenTree};

Check warning on line 11 in ff-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / Check no_std

unused imports: `Literal`, `TokenTree`
use syn::{Expr, Item, ItemFn, Lit};

mod montgomery;
Expand All @@ -27,6 +27,13 @@
quote::quote!(#tuple).into()
}

#[cfg(feature = "zkllvm")]
#[proc_macro]
pub fn to_field_literal(input: TokenStream) -> TokenStream {
let num = utils::parse_string(input).expect("expected decimal string");
TokenTree::Literal(Literal::field(&num)).into()

Check failure on line 34 in ff-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (stable)

no function or associated item named `field` found for struct `proc_macro::Literal` in the current scope

Check failure on line 34 in ff-macros/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test assembly

no function or associated item named `field` found for struct `proc_macro::Literal` in the current scope
}

/// Derive the `MontConfig` trait.
///
/// The attributes available to this macro are
Expand Down
1 change: 1 addition & 0 deletions ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ default = []
std = [ "ark-std/std", "ark-serialize/std", "itertools/use_std" ]
parallel = [ "std", "rayon", "ark-std/parallel" ]
asm = []
zkllvm = [ "ark-ff-macros/zkllvm" ]


[package.metadata.docs.rs]
Expand Down
9 changes: 9 additions & 0 deletions ff/src/fields/models/fp/montgomery_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,22 @@ pub const fn sqrt_precomputation<const N: usize, T: MontConfig<N>>(
/// }
/// ```
#[macro_export]
#[cfg(not(feature = "zkllvm"))]
macro_rules! MontFp {
($c0:expr) => {{
let (is_positive, limbs) = $crate::ark_ff_macros::to_sign_and_limbs!($c0);
$crate::Fp::from_sign_and_limbs(is_positive, &limbs)
}};
}

#[macro_export]
#[cfg(feature = "zkllvm")]
macro_rules! MontFp {
($c0:expr) => {{
$crate::ark_ff_macros::to_field_literal!($c0)
}};
}

pub use ark_ff_macros::MontConfig;

pub use MontFp;
Expand Down
Loading