Skip to content

Commit dbc5005

Browse files
feat: Parse struct name in complex field + ECC init macros as string (#1945)
Cherry-picked from #1891 without the twisted Edwards macros. The `complex_init!` and `sw_init!` macros are updated so that instead of parsing a `Path`, it parses a `LitStr`. This makes it so that the `_init!` macros do not require any path imports in the file where it is called, which greatly improves the developer experience. The main files changed aside from test files and auto-generates `openvm_init.rs` files are: - extensions/algebra/complex-macros/src/lib.rs - extensions/ecc/sw-macros/src/lib.rs In both macros, the change essentially boils down to changing: (before) ```rust let items = input.parse_terminated(<Expr as Parse>::parse, Token![,])?; ``` to (after) ```rust let items = input.parse_terminated(<LitStr as Parse>::parse, Token![,])?; ``` and then associated type changes (mostly simplifications) in converting tokens to strings. There is one additional change needed only for the `sw_init!` macro case. Previously the linked function ```rust extern "C" fn #setup_extern_func() ``` defined by the `sw_init!` macro did use the path of the struct within the function implementation. After this PR, we avoid this import by moving that part of the logic into the `fn set_up_once()` function defined for the struct itself. This changed the function signature of the extern function to: ```rust extern "C" fn #setup_extern_func(uninit: *mut core::ffi::c_void, p1: *const u8, p2: *const u8) ``` [Here](https://www.diffchecker.com/0ncTMwe2/) is the diff of the code that was moved from the `#setup_extern_func()` into the `set_up_once()` function. --------- Co-authored-by: Avaneesh Kulkarni <[email protected]>
1 parent 160de07 commit dbc5005

File tree

60 files changed

+157
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+157
-146
lines changed
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", "115792089237316195423570985008687907852837564279074904382605163141518161494337" }
3-
openvm_ecc_guest::sw_macros::sw_init! { Secp256k1Point }
3+
openvm_ecc_guest::sw_macros::sw_init! { "Secp256k1Point" }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "1000000000000000003", "115792089237316195423570985008687907853269984665640564039457584007908834671663", "115792089237316195423570985008687907852837564279074904382605163141518161494337", "115792089210356248762697446949407573530086143415290314195533631308867097853951", "115792089210356248762697446949407573529996955224135760342422259061068512044369", "21888242871839275222246405745257275088696311157297823662689037894645226208583", "21888242871839275222246405745257275088548364400416034343698204186575808495617", "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787", "52435875175126190479447740508185965837690552500527637822603658699938581184513", "2305843009213693951", "7" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Bn254Fp2 { mod_idx = 5 }, Bls12_381Fp2 { mod_idx = 7 } }
4-
openvm_ecc_guest::sw_macros::sw_init! { Secp256k1Point, P256Point, Bn254G1Affine, Bls12_381G1Affine }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Bn254Fp2" { mod_idx = 5 }, "Bls12_381Fp2" { mod_idx = 7 } }
4+
openvm_ecc_guest::sw_macros::sw_init! { "Secp256k1Point", "P256Point", "Bn254G1Affine", "Bls12_381G1Affine" }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// This file is automatically generated by cargo openvm. Do not rename or edit.
22
openvm_algebra_guest::moduli_macros::moduli_init! { "21888242871839275222246405745257275088696311157297823662689037894645226208583", "21888242871839275222246405745257275088548364400416034343698204186575808495617" }
3-
openvm_algebra_guest::complex_macros::complex_init! { Bn254Fp2 { mod_idx = 0 } }
4-
openvm_ecc_guest::sw_macros::sw_init! { Bn254G1Affine }
3+
openvm_algebra_guest::complex_macros::complex_init! { "Bn254Fp2" { mod_idx = 0 } }
4+
openvm_ecc_guest::sw_macros::sw_init! { "Bn254G1Affine" }

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
```

book/src/custom-extensions/ecc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ This creates `Bls12_381G1Affine` and `P256Affine` structs which implement the `G
4949
openvm::init!();
5050
/* This expands to
5151
sw_init! {
52-
Bls12_381G1Affine, P256Affine,
52+
"Bls12_381G1Affine", "P256Affine",
5353
}
5454
*/
5555
```

docs/specs/ISA.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ r32_ec_point(a) -> EcPoint {
711711
| EC_ADD_NE\<C\> | `a,b,c,1,2` | Set `r32_ec_point(a) = r32_ec_point(b) + r32_ec_point(c)` (curve addition). Assumes that `r32_ec_point(b), r32_ec_point(c)` both lie on the curve and are not the identity point. Further assumes that `r32_ec_point(b).x, r32_ec_point(c).x` are not equal in the coordinate field. |
712712
| SETUP_EC_ADD_NE\<C\> | `a,b,c,1,2` | `assert(r32_ec_point(b).x == C::MODULUS)` in the chip for EC ADD. For the sake of implementation convenience it also writes something (can be anything) into `[r32{0}(a): 2*C::COORD_SIZE]_2`. It is required for proper functionality that `assert(r32_ec_point(b).x != r32_ec_point(c).x)` |
713713
| EC_DOUBLE\<C\> | `a,b,_,1,2` | Set `r32_ec_point(a) = 2 * r32_ec_point(b)`. This doubles the input point. Assumes that `r32_ec_point(b)` lies on the curve and is not the identity point. |
714-
| SETUP_EC_DOUBLE\<C\> | `a,b,_,1,2` | `assert(r32_ec_point(b).x == C::MODULUS)` in the chip for EC DOUBLE. For the sake of implementation convenience it also writes something (can be anything) into `[r32{0}(a): 2*C::COORD_SIZE]_2`. It is required for proper functionality that `assert(r32_ec_point(b).y != 0 mod C::MODULUS)` |
714+
| SETUP_EC_DOUBLE\<C\> | `a,b,_,1,2` | `assert(r32_ec_point(b).x == C::MODULUS && r32_ec_point(b).y == C::A)` in the chip for EC DOUBLE. For the sake of implementation convenience it also writes something (can be anything) into `[r32{0}(a): 2*C::COORD_SIZE]_2`. It is required for proper functionality that `assert(r32_ec_point(b).y != 0 mod C::MODULUS)` |
715715

716716
### Pairing Extension
717717

docs/specs/RISCV.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ the guest must take care to validate all data and account for behavior in cases
7676
|--------------|-----|-------------|---------|--------|------------------------------------------------------------------------------------------------------------------------------|
7777
| nativestorew | R | 0001011 | 111 | 0x2 | Stores the 4-byte word `rs1` at address `rd` in native address space. The address `rd` must be aligned to a 4-byte boundary. |
7878

79-
`nativestorew` connects RV32 address space and native address space. We put it in RV32 extension because its
79+
`nativestorew` connects RV32 address space and native address space. We put it in RV32 extension because its
8080
implementation is here. But we use `funct3 = 111` because the native extension has an available slot.
8181

8282
## Keccak Extension
@@ -182,7 +182,7 @@ The elliptic curve extension supports arithmetic over short Weierstrass curves,
182182
| --------------- | --- | ----------- | ------ | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
183183
| sw_add_ne\<C\> | R | 0101011 | 001 | `idx*8` | `EcPoint([rd:2*C::COORD_SIZE]_2) = EcPoint([rs1:2*C::COORD_SIZE]_2) + EcPoint([rs2:2*C::COORD_SIZE]_2)`. Assumes that input affine points are not identity and do not have same x-coordinate. |
184184
| sw_double\<C\> | R | 0101011 | 001 | `idx*8+1` | `EcPoint([rd:2*C::COORD_SIZE]_2) = 2 * EcPoint([rs1:2*C::COORD_SIZE]_2)`. Assumes that input affine point is not identity. `rs2` is unused and must be set to `x0`. |
185-
| setup\<C\> | R | 0101011 | 001 | `idx*8+2` | `assert([rs1: C::COORD_SIZE]_2 == C::MODULUS)` in the chip defined by the register index of `rs2`. For the sake of implementation convenience it also writes an unconstrained value into `[rd: 2*C::COORD_SIZE]_2`. If `ind(rs2) != 0`, then this instruction is setup for `sw_add_ne`. Otherwise it is setup for `sw_double`. When `ind(rs2) != 0` (add_ne), it is required for proper functionality that `[rs2: C::COORD_SIZE]_2 != [rs1: C::COORD_SIZE]_2`; otherwise (double), it is required that `[rs1 + C::COORD_SIZE: C::COORD_SIZE]_2 != C::Fp::ZERO` |
185+
| setup\<C\> | R | 0101011 | 001 | `idx*8+2` | If `ind(rs2) != 0`, then this instruction is setup for `sw_add_ne`. Otherwise it is setup for `sw_double`. If setup for `sw_add_ne`, it checks `assert([rs1: C::COORD_SIZE]_2 == C::MODULUS)`, and if setup for `sw_double`, checks `assert([rs1: 2*C::COORD_SIZE]_2 == [C::MODULUS, CURVE_A])`. For the sake of implementation convenience it also writes an unconstrained value into `[rd: 2*C::COORD_SIZE]_2`. When `ind(rs2) != 0` (add_ne), it is required for proper functionality that `[rs2: C::COORD_SIZE]_2 != [rs1: C::COORD_SIZE]_2`; otherwise (double), it is required that `[rs1 + C::COORD_SIZE: C::COORD_SIZE]_2 != C::Fp::ZERO` |
186186

187187
Since `funct7` is 7-bits, up to 16 curves can be supported simultaneously. We use `idx*8` to leave some room for future expansion.
188188

examples/algebra/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ num-bigint = { version = "0.4.6", features = ["serde"] }
1717

1818
[features]
1919
default = []
20+
21+
# remove this if copying example outside of monorepo
22+
[patch."https://github.com/openvm-org/openvm.git"]
23+
openvm = { path = "../../crates/toolchain/openvm" }
24+
openvm-algebra-guest = { path = "../../extensions/algebra/guest" }

examples/algebra/openvm_init.rs

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 } }

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

0 commit comments

Comments
 (0)