Skip to content

Commit

Permalink
Add MontFp macros replacement to generate field literals
Browse files Browse the repository at this point in the history
  • Loading branch information
aleasims committed Oct 27, 2023
1 parent 993a4e7 commit b2185d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
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 @@ pub fn to_sign_and_limbs(input: TokenStream) -> TokenStream {
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

0 comments on commit b2185d1

Please sign in to comment.