From f8a9af6862ce58f8f55d907d3d2c1f13d8c5b4f3 Mon Sep 17 00:00:00 2001 From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com> Date: Sat, 14 Dec 2024 23:35:07 -0500 Subject: [PATCH] fix: libraries should use `complex_declare!` with unique names (#1043) * 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 --- book/src/using-extensions/customizable-extensions.md | 10 +++++----- .../tests/programs/examples/final_exp_hint.rs | 4 ---- crates/toolchain/tests/programs/examples/fp12_mul.rs | 8 -------- .../tests/programs/examples/pairing_check.rs | 4 ++-- .../tests/programs/examples/pairing_line.rs | 8 -------- .../tests/programs/examples/pairing_miller_loop.rs | 12 ++---------- .../tests/programs/examples/pairing_miller_step.rs | 12 ++---------- extensions/pairing/guest/src/bls12_381/fp2.rs | 8 ++++++-- extensions/pairing/guest/src/bn254/fp2.rs | 8 ++++++-- 9 files changed, 23 insertions(+), 51 deletions(-) diff --git a/book/src/using-extensions/customizable-extensions.md b/book/src/using-extensions/customizable-extensions.md index 9bf789f23e..1d69997c2a 100644 --- a/book/src/using-extensions/customizable-extensions.md +++ b/book/src/using-extensions/customizable-extensions.md @@ -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" }, } ``` @@ -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 }, } ``` @@ -58,7 +58,7 @@ The arithmetic operations for these classes, when compiling for the `zkvm` targe ```rust sw_init! { - Bls12381, Bn254, + Bls12_381Fp, Bn254Fp, } ``` diff --git a/crates/toolchain/tests/programs/examples/final_exp_hint.rs b/crates/toolchain/tests/programs/examples/final_exp_hint.rs index 3dd8c2ce22..c5e0e2100a 100644 --- a/crates/toolchain/tests/programs/examples/final_exp_hint.rs +++ b/crates/toolchain/tests/programs/examples/final_exp_hint.rs @@ -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>, Vec>, (Fp12, Fp12)) = read(); let actual = Bls12_381::pairing_check_hint(&p, &q); diff --git a/crates/toolchain/tests/programs/examples/fp12_mul.rs b/crates/toolchain/tests/programs/examples/fp12_mul.rs index 84ac3c4007..f7ef344801 100644 --- a/crates/toolchain/tests/programs/examples/fp12_mul.rs +++ b/crates/toolchain/tests/programs/examples/fp12_mul.rs @@ -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); @@ -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); diff --git a/crates/toolchain/tests/programs/examples/pairing_check.rs b/crates/toolchain/tests/programs/examples/pairing_check.rs index e77cac55d0..b1fce22614 100644 --- a/crates/toolchain/tests/programs/examples/pairing_check.rs +++ b/crates/toolchain/tests/programs/examples/pairing_check.rs @@ -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]) { @@ -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]) { diff --git a/crates/toolchain/tests/programs/examples/pairing_line.rs b/crates/toolchain/tests/programs/examples/pairing_line.rs index f04b364227..922f59992b 100644 --- a/crates/toolchain/tests/programs/examples/pairing_line.rs +++ b/crates/toolchain/tests/programs/examples/pairing_line.rs @@ -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]; @@ -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]; diff --git a/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs b/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs index fa6eea1817..cb7e670e1f 100644 --- a/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs +++ b/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs @@ -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]) { @@ -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]) { diff --git a/crates/toolchain/tests/programs/examples/pairing_miller_step.rs b/crates/toolchain/tests/programs/examples/pairing_miller_step.rs index 1ea0460f15..02e9d60baf 100644 --- a/crates/toolchain/tests/programs/examples/pairing_miller_step.rs +++ b/crates/toolchain/tests/programs/examples/pairing_miller_step.rs @@ -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]) { @@ -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]) { diff --git a/extensions/pairing/guest/src/bls12_381/fp2.rs b/extensions/pairing/guest/src/bls12_381/fp2.rs index 7af9f2f62e..746efbbbd2 100644 --- a/extensions/pairing/guest/src/bls12_381/fp2.rs +++ b/extensions/pairing/guest/src/bls12_381/fp2.rs @@ -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 for Fp2 { const D: usize = 2; type Coeffs = [Fp; 2]; diff --git a/extensions/pairing/guest/src/bn254/fp2.rs b/extensions/pairing/guest/src/bn254/fp2.rs index 5f10b713c2..6f3ba86430 100644 --- a/extensions/pairing/guest/src/bn254/fp2.rs +++ b/extensions/pairing/guest/src/bn254/fp2.rs @@ -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 for Fp2 { const D: usize = 2; type Coeffs = [Fp; 2];