Skip to content

Commit 94c0a9a

Browse files
Avaneesh-axiomjonathanpwang
authored andcommitted
Parse struct name in complex_init input as string
1 parent e67deab commit 94c0a9a

24 files changed

+77
-28
lines changed

book/src/custom-extensions/algebra.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ moduli_init! {
8080
"21888242871839275222246405745257275088696311157297823662689037894645226208583"
8181
}
8282
complex_init! {
83-
Bn254Fp2 { mod_idx = 0 },
83+
"Bn254Fp2" { mod_idx = 0 },
8484
}
8585
*/
8686
```

examples/algebra/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ moduli_init! {
2828
// The order of these structs does not matter,
2929
// given that we specify the `mod_idx` parameters properly.
3030
openvm_algebra_complex_macros::complex_init! {
31-
Complex1 { mod_idx = 0 }, Complex2 { mod_idx = 1 },
31+
"Complex1" { mod_idx = 0 }, "Complex2" { mod_idx = 1 },
3232
}
3333
*/
3434

extensions/algebra/circuit/src/fp2_extension.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Fp2Extension {
6262
.iter()
6363
.map(|(name, modulus)| {
6464
format!(
65-
"{} {{ mod_idx = {} }}",
65+
"\"{}\" {{ mod_idx = {} }}",
6666
name,
6767
get_index_of_modulus(modulus, modular_config)
6868
)

extensions/algebra/complex-macros/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ openvm_algebra_moduli_macros::moduli_init!(
2222
);
2323
2424
openvm_algebra_complex_macros::complex_init! {
25-
Complex { mod_idx = 0 },
25+
"Complex" { mod_idx = 0 },
2626
}
2727
*/
2828

@@ -39,7 +39,7 @@ The crate provides two macros: `complex_declare!` and `complex_init!`. The signa
3939

4040
- `complex_declare!` receives comma-separated list of moduli classes descriptions. Each description looks like `ComplexStruct { mod_type = ModulusName }`. Here `ModulusName` is the name of any struct that implements `trait IntMod` -- in particular, the ones created by `moduli_declare!` do, and `ComplexStruct` is the name for the complex arithmetic struct to create.
4141

42-
- `complex_init!` receives comma-separated list of struct descriptions. Each description looks like `ComplexStruct { mod_idx = idx }`. Here `ComplexStruct` is the name of the complex struct used in `complex_declare!`, and `idx` is the index of the modulus **in the `moduli_init!` macro**.
42+
- `complex_init!` receives comma-separated list of struct descriptions. Each description looks like `"ComplexStruct" { mod_idx = idx }`. Here `ComplexStruct` is the name of the complex struct used in `complex_declare!`, and `idx` is the index of the modulus **in the `moduli_init!` macro**.
4343

4444
What happens under the hood:
4545

@@ -96,7 +96,7 @@ complex_declare! {
9696
pub type Fp2 = Bn254Fp2;
9797

9898
complex_init! {
99-
Fp2 { mod_idx = 0 },
99+
"Fp2" { mod_idx = 0 },
100100
}
101101
```
102102

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

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
extern crate proc_macro;
22

3-
use openvm_macros_common::MacroArgs;
3+
use openvm_macros_common::{MacroArgs, Param};
44
use proc_macro::TokenStream;
55
use syn::{
66
parse::{Parse, ParseStream},
7-
parse_macro_input, Expr, ExprPath, Path, Token,
7+
parse_macro_input,
8+
punctuated::Punctuated,
9+
Expr, ExprPath, LitStr, Path, Token,
810
};
911

1012
/// This macro is used to declare the complex extension fields.
@@ -529,6 +531,38 @@ pub fn complex_declare(input: TokenStream) -> TokenStream {
529531
TokenStream::from_iter(output)
530532
}
531533

534+
// Override the MacroArgs struct to use LitStr for item names instead of Ident.
535+
// This removes the need to import the complex struct when using the complex_init macro.
536+
struct ComplexInitArgs {
537+
pub items: Vec<ComplexInitItem>,
538+
}
539+
540+
struct ComplexInitItem {
541+
pub name: LitStr,
542+
pub params: Punctuated<Param, Token![,]>,
543+
}
544+
545+
impl Parse for ComplexInitArgs {
546+
fn parse(input: ParseStream) -> syn::Result<Self> {
547+
Ok(ComplexInitArgs {
548+
items: input
549+
.parse_terminated(ComplexInitItem::parse, Token![,])?
550+
.into_iter()
551+
.collect(),
552+
})
553+
}
554+
}
555+
556+
impl Parse for ComplexInitItem {
557+
fn parse(input: ParseStream) -> syn::Result<Self> {
558+
let name = input.parse()?;
559+
let content;
560+
syn::braced!(content in input);
561+
let params = content.parse_terminated(Param::parse, Token![,])?;
562+
Ok(ComplexInitItem { name, params })
563+
}
564+
}
565+
532566
/// This macro is used to initialize the complex extension fields.
533567
/// It must be called after `moduli_init!` is called.
534568
///
@@ -543,14 +577,14 @@ pub fn complex_declare(input: TokenStream) -> TokenStream {
543577
/// the `moduli_init!` macro (not `moduli_declare!`).
544578
#[proc_macro]
545579
pub fn complex_init(input: TokenStream) -> TokenStream {
546-
let MacroArgs { items } = parse_macro_input!(input as MacroArgs);
580+
let ComplexInitArgs { items } = parse_macro_input!(input as ComplexInitArgs);
547581

548582
let mut externs = Vec::new();
549583

550584
let span = proc_macro::Span::call_site();
551585

552586
for (complex_idx, item) in items.into_iter().enumerate() {
553-
let struct_name = item.name.to_string();
587+
let struct_name = item.name.value();
554588
let struct_name = syn::Ident::new(&struct_name, span.into());
555589
let mut intmod_idx: Option<usize> = None;
556590
for param in item.params {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "998244353", "1000000007", "1000000009", "987898789" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Complex2 { mod_idx = 2 } }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Complex2" { mod_idx = 2 } }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "115792089237316195423570985008687907853269984665640564039457584007908834671663" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Complex { mod_idx = 0 } }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Complex" { mod_idx = 0 } }
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "998244353", "1000000007" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Complex1 { mod_idx = 0 }, Complex2 { mod_idx = 1 } }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Complex1" { mod_idx = 0 }, "Complex2" { mod_idx = 1 } }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787", "52435875175126190479447740508185965837690552500527637822603658699938581184513" }
33
openvm_ecc_guest::sw_macros::sw_init! { Bls12_381G1Affine }
4+
openvm_ecc_guest::te_macros::te_init! { }
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Bls12_381Fp2 { mod_idx = 0 } }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Bls12_381Fp2" { mod_idx = 0 } }
44
openvm_ecc_guest::sw_macros::sw_init! { }
5+
openvm_ecc_guest::te_macros::te_init! { }

0 commit comments

Comments
 (0)