Skip to content

Commit

Permalink
fix: libraries should use complex_declare! with unique names (#1043)
Browse files Browse the repository at this point in the history
* fix: libraries should use `complex_declare!` with unique names

Because the extern function name will depend on the struct name used.

* chore: renames

* chore: more rename
  • Loading branch information
jonathanpwang authored Dec 15, 2024
1 parent 0a3905c commit f8a9af6
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 51 deletions.
10 changes: 5 additions & 5 deletions book/src/using-extensions/customizable-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ To declare a modular arithmetic struct, one needs to use the `moduli_declare!` m

```rust
moduli_declare! {
Bls12381_Fp { modulus = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab" },
Bn254_Fp { modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583" },
Bls12_381Fp { modulus = "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab" },
Bn254Fp { modulus = "21888242871839275222246405745257275088696311157297823662689037894645226208583" },
}
```

Expand Down Expand Up @@ -47,8 +47,8 @@ To declare an elliptic curve struct, one needs to use the `sw_declare!` macro. A

```rust
sw_declare! {
Bls12381 { mod_type = Bls12381_Fp, b = BLS12381_B },
Bn254 { mod_type = Bn254_Fp, b = BN254_B },
Bls12_381G1Affine { mod_type = Bls12_381Fp, b = BLS12_381_B },
Bn254G1Affine { mod_type = Bn254Fp, b = BN254_B },
}
```

Expand All @@ -58,7 +58,7 @@ The arithmetic operations for these classes, when compiling for the `zkvm` targe

```rust
sw_init! {
Bls12381, Bn254,
Bls12_381Fp, Bn254Fp,
}
```

Expand Down
4 changes: 0 additions & 4 deletions crates/toolchain/tests/programs/examples/final_exp_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ openvm_algebra_moduli_setup::moduli_init! {
"0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
}

openvm_ecc_sw_setup::sw_init! {
Fp,
}

pub fn main() {
let (p, q, expected): (Vec<AffinePoint<Fp>>, Vec<AffinePoint<Fp2>>, (Fp12, Fp12)) = read();
let actual = Bls12_381::pairing_check_hint(&p, &q);
Expand Down
8 changes: 0 additions & 8 deletions crates/toolchain/tests/programs/examples/fp12_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ mod bn254 {
"0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
}

openvm_ecc_sw_setup::sw_init! {
Fp,
}

pub fn test_fp12_mul(io: &[u8]) {
setup_0();
assert_eq!(io.len(), 32 * 36);
Expand Down Expand Up @@ -56,10 +52,6 @@ mod bls12_381 {
"0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
}

openvm_ecc_sw_setup::sw_init! {
Fp,
}

pub fn test_fp12_mul(io: &[u8]) {
setup_0();
assert_eq!(io.len(), 48 * 36);
Expand Down
4 changes: 2 additions & 2 deletions crates/toolchain/tests/programs/examples/pairing_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod bn254 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
Bn254Fp2 { mod_idx = 0 },
}

pub fn test_pairing_check(io: &[u8]) {
Expand Down Expand Up @@ -65,7 +65,7 @@ mod bls12_381 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
Bls12_381Fp2 { mod_idx = 0 },
}

pub fn test_pairing_check(io: &[u8]) {
Expand Down
8 changes: 0 additions & 8 deletions crates/toolchain/tests/programs/examples/pairing_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ mod bn254 {
"0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001"
}

openvm_ecc_sw_setup::sw_init! {
Fp,
}

pub fn test_mul_013_by_013(io: &[u8]) {
assert_eq!(io.len(), 32 * 18);
let l0 = &io[..32 * 4];
Expand Down Expand Up @@ -76,10 +72,6 @@ mod bls12_381 {
"0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001"
}

openvm_ecc_sw_setup::sw_init! {
Fp,
}

pub fn test_mul_023_by_023(io: &[u8]) {
assert_eq!(io.len(), 48 * 18);
let l0 = &io[..48 * 4];
Expand Down
12 changes: 2 additions & 10 deletions crates/toolchain/tests/programs/examples/pairing_miller_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ mod bn254 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
}

openvm_ecc_sw_setup::sw_init! {
Fp,
Bn254Fp2 { mod_idx = 0 },
}

pub fn test_miller_loop(io: &[u8]) {
Expand Down Expand Up @@ -72,11 +68,7 @@ mod bls12_381 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
}

openvm_ecc_sw_setup::sw_init! {
Fp,
Bls12_381Fp2 { mod_idx = 0 },
}

pub fn test_miller_loop(io: &[u8]) {
Expand Down
12 changes: 2 additions & 10 deletions crates/toolchain/tests/programs/examples/pairing_miller_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ mod bn254 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
}

openvm_ecc_sw_setup::sw_init! {
Fp,
Bn254Fp2 { mod_idx = 0 },
}

pub fn test_miller_step(io: &[u8]) {
Expand Down Expand Up @@ -104,11 +100,7 @@ mod bls12_381 {
}

openvm_algebra_complex_macros::complex_init! {
Fp2 { mod_idx = 0 },
}

openvm_ecc_sw_setup::sw_init! {
Fp,
Bls12_381Fp2 { mod_idx = 0 },
}

pub fn test_miller_step(io: &[u8]) {
Expand Down
8 changes: 6 additions & 2 deletions extensions/pairing/guest/src/bls12_381/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ use openvm_algebra_guest::{field::FieldExtension, DivUnsafe, Field, IntMod};

use super::Fp;

// The struct name needs to be globally unique for linking purposes.
// The mod_type is a path used only in the struct definition.
complex_declare! {
Fp2 { mod_type = Fp }
Bls12_381Fp2 { mod_type = Fp }
}

complex_impl_field! {
Fp2,
Bls12_381Fp2,
}

pub type Fp2 = Bls12_381Fp2;

impl FieldExtension<Fp> for Fp2 {
const D: usize = 2;
type Coeffs = [Fp; 2];
Expand Down
8 changes: 6 additions & 2 deletions extensions/pairing/guest/src/bn254/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ use openvm_algebra_guest::{field::FieldExtension, DivUnsafe, Field, IntMod};

use super::Fp;

// The struct name needs to be globally unique for linking purposes.
// The mod_type is a path used only in the struct definition.
complex_declare! {
Fp2 { mod_type = Fp }
Bn254Fp2 { mod_type = Fp }
}

complex_impl_field! {
Fp2,
Bn254Fp2,
}

pub type Fp2 = Bn254Fp2;

impl FieldExtension<Fp> for Fp2 {
const D: usize = 2;
type Coeffs = [Fp; 2];
Expand Down

0 comments on commit f8a9af6

Please sign in to comment.