Skip to content

Commit 7e5c6e9

Browse files
Removed impl_field argument of moduli_declare
Primality check is now performed in the macro
1 parent a607d2f commit 7e5c6e9

File tree

11 files changed

+60
-45
lines changed

11 files changed

+60
-45
lines changed

Cargo.lock

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

book/src/custom-extensions/algebra.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ To [leverage](./overview.md) compile-time known moduli for performance, you decl
2929
```rust
3030
moduli_declare! {
3131
Bls12_381Fp { modulus = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab" },
32-
Bn254Fp {
33-
modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583", impl_field = true
34-
},
32+
Bn254Fp { modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583" },
3533
}
3634
```
3735

3836
This creates `Bls12_381Fp` and `Bn254Fp` structs, each implementing the `IntMod` trait.
39-
Since `impl_field = true` is specified for `Bn254Fp`, it also implements the `Field` and `Sqrt` traits.
37+
Since both moduli are prime, both structs also implement the `Field` and `Sqrt` traits.
4038
The modulus parameter must be a string literal in decimal or hexadecimal format.
4139

4240
2. **Init**: Use the `init!` macro exactly once in the final binary:

extensions/algebra/moduli-macros/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ edition = "2021"
77
syn = { version = "2.0", features = ["full"] }
88
quote = "1.0"
99
openvm-macros-common = { workspace = true, default-features = false }
10+
num-prime = { version = "0.4.4", default-features = false, features = ["big-int"] }
11+
num-bigint = { workspace = true }
1012

1113
[lib]
1214
proc-macro = true

extensions/algebra/moduli-macros/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ Procedural macros for use in guest program to generate modular arithmetic struct
66

77
```rust
88
openvm_algebra_moduli_macros::moduli_declare! {
9-
Bls12381 {
10-
modulus = "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787",
11-
impl_field = true
12-
},
9+
Bls12381 { modulus = "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" },
1310
Mod1e18 { modulus = "1000000000000000003" },
1411
}
1512

@@ -31,10 +28,7 @@ openvm_algebra_moduli_macros::moduli_init! {
3128

3229
The crate provides two macros: `moduli_declare!` and `moduli_init!`. The signatures are:
3330

34-
- `moduli_declare!` receives comma-separated list of moduli classes descriptions. Each description looks like `ModulusName { modulus = "modulus_value", impl_field = <boolean_value> }`. Here `ModulusName` is the name of the struct, and `modulus_value` is the modulus value in decimal or hex format.
35-
- The `impl_field` argument indicates whether or not the `Field` and `Sqrt` traits should be automatically implemented on the resulting struct.
36-
It should only be set to `true` if the modulus is prime.
37-
If unspecified, it defaults to `false`.
31+
- `moduli_declare!` receives comma-separated list of moduli classes descriptions. Each description looks like `ModulusName { modulus = "modulus_value" }`. Here `ModulusName` is the name of the struct, and `modulus_value` is the modulus value in decimal or hex format.
3832

3933
- `moduli_init!` receives comma-separated list of modulus values in decimal or hex format.
4034

@@ -120,6 +114,14 @@ mod algebra_impl_0 {
120114
}
121115
// ...
122116
}
117+
118+
impl Field for Mod1e18 {
119+
// ...
120+
}
121+
122+
impl Sqrt for Mod1e18 {
123+
// ...
124+
}
123125
}
124126
```
125127

extensions/algebra/moduli-macros/src/lib.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::sync::atomic::AtomicUsize;
44

55
use openvm_macros_common::{string_to_bytes, MacroArgs};
66
use proc_macro::TokenStream;
7+
use num_bigint::BigUint;
8+
use num_prime::nt_funcs::is_prime;
79
use quote::format_ident;
810
use syn::{
911
parse::{Parse, ParseStream},
@@ -34,7 +36,6 @@ pub fn moduli_declare(input: TokenStream) -> TokenStream {
3436
let struct_name = item.name.to_string();
3537
let struct_name = syn::Ident::new(&struct_name, span.into());
3638
let mut modulus: Option<String> = None;
37-
let mut impl_field: Option<bool> = None;
3839
for param in item.params {
3940
match param.name.to_string().as_str() {
4041
"modulus" => {
@@ -53,22 +54,6 @@ pub fn moduli_declare(input: TokenStream) -> TokenStream {
5354
.into();
5455
}
5556
}
56-
"impl_field" => {
57-
if let syn::Expr::Lit(syn::ExprLit {
58-
lit: syn::Lit::Bool(value),
59-
..
60-
}) = param.value
61-
{
62-
impl_field = Some(value.value());
63-
} else {
64-
return syn::Error::new_spanned(
65-
param.value,
66-
"Expected a boolean literal for macro argument `impl_field`",
67-
)
68-
.to_compile_error()
69-
.into();
70-
}
71-
}
7257
_ => {
7358
panic!("Unknown parameter {}", param.name);
7459
}
@@ -84,8 +69,6 @@ pub fn moduli_declare(input: TokenStream) -> TokenStream {
8469
let mut limbs = modulus_bytes.len();
8570
let mut block_size = 32;
8671

87-
let impl_field = impl_field.unwrap_or(false);
88-
8972
if limbs <= 32 {
9073
limbs = 32;
9174
} else if limbs <= 48 {
@@ -757,7 +740,10 @@ pub fn moduli_declare(input: TokenStream) -> TokenStream {
757740

758741
output.push(result);
759742

760-
if impl_field {
743+
let modulus_biguint = BigUint::from_bytes_le(&modulus_bytes);
744+
let modulus_is_prime = is_prime(&modulus_biguint, None);
745+
746+
if modulus_is_prime.probably() {
761747
// implement Field and Sqrt traits for prime moduli
762748
let field_and_sqrt_impl = TokenStream::from(quote::quote_spanned! { span.into() =>
763749
impl ::openvm_algebra_guest::Field for #struct_name {
@@ -1063,8 +1049,8 @@ pub fn moduli_init(input: TokenStream) -> TokenStream {
10631049

10641050
});
10651051

1066-
// This function will be defined regardless of whether impl_field is true or false,
1067-
// but it will be called only if the impl_field is true.
1052+
// This function will be defined regardless of whether the modulus is prime or not,
1053+
// but it will be called only if the modulus is prime.
10681054
let hint_sqrt_extern_func = syn::Ident::new(
10691055
&format!("hint_sqrt_extern_func_{}", modulus_hex),
10701056
span.into(),

extensions/algebra/tests/programs/examples/sqrt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ openvm::entry!(main);
1010
openvm_algebra_moduli_macros::moduli_declare! {
1111
Secp256k1Coord {
1212
modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F",
13-
impl_field = true
1413
}
1514
}
1615

extensions/ecc/guest/src/k256.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ const fn seven_le() -> [u8; 32] {
3636
}
3737

3838
moduli_declare! {
39-
Secp256k1Coord { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F", impl_field = true },
40-
Secp256k1Scalar { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141", impl_field = true },
39+
Secp256k1Coord { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F" },
40+
Secp256k1Scalar { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141" },
4141
}
4242

4343
sw_declare! {

extensions/ecc/guest/src/p256.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ lazy_static! {
2222
}
2323

2424
openvm_algebra_moduli_macros::moduli_declare! {
25-
P256Coord { modulus = "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff", impl_field = true },
26-
P256Scalar { modulus = "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", impl_field = true },
25+
P256Coord { modulus = "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff" },
26+
P256Scalar { modulus = "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551" },
2727
}
2828

2929
pub const P256_NUM_LIMBS: usize = 32;

guest-libs/ff_derive/guest/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,7 @@ fn openvm_struct_impl(ast: &syn::ItemStruct, modulus: &BigUint) -> proc_macro2::
277277
quote! {
278278
::openvm_algebra_moduli_macros::moduli_declare! {
279279
#struct_ident {
280-
modulus = #modulus_str,
281-
impl_field = true
280+
modulus = #modulus_str
282281
}
283282
}
284283
}

guest-libs/k256/guest/src/internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub const fn seven_le() -> [u8; 32] {
2121
}
2222

2323
moduli_declare! {
24-
Secp256k1Coord { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F", impl_field = true },
25-
Secp256k1Scalar { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141", impl_field = true },
24+
Secp256k1Coord { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F" },
25+
Secp256k1Scalar { modulus = "0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141" },
2626
}
2727

2828
sw_declare! {

guest-libs/p256/guest/src/internal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use crate::P256;
1414
// --- Define the OpenVM modular arithmetic and ecc types ---
1515

1616
moduli_declare! {
17-
P256Coord { modulus = "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff", impl_field = true },
18-
P256Scalar { modulus = "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", impl_field = true },
17+
P256Coord { modulus = "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff" },
18+
P256Scalar { modulus = "0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551" },
1919
}
2020

2121
// from_const_bytes is little endian

0 commit comments

Comments
 (0)