From f73be000e286abc57a5d72fc0b789d997590e287 Mon Sep 17 00:00:00 2001 From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:41:32 -0500 Subject: [PATCH 1/3] fix: libraries should use `complex_declare!` with unique names Because the extern function name will depend on the struct name used. --- crates/toolchain/tests/programs/examples/pairing_check.rs | 4 ++-- .../tests/programs/examples/pairing_miller_loop.rs | 8 ++------ .../tests/programs/examples/pairing_miller_step.rs | 4 ++-- extensions/pairing/guest/src/bls12_381/fp2.rs | 8 ++++++-- extensions/pairing/guest/src/bn254/fp2.rs | 8 ++++++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/crates/toolchain/tests/programs/examples/pairing_check.rs b/crates/toolchain/tests/programs/examples/pairing_check.rs index e77cac55d0..cae6b3ab32 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 }, + Bn254_Fp2 { 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_381_Fp2 { mod_idx = 0 }, } pub fn test_pairing_check(io: &[u8]) { diff --git a/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs b/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs index fa6eea1817..62353e0a42 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, + Bn254_Fp2 { mod_idx = 0 }, } pub fn test_miller_loop(io: &[u8]) { @@ -72,7 +68,7 @@ mod bls12_381 { } openvm_algebra_complex_macros::complex_init! { - Fp2 { mod_idx = 0 }, + Bls12_381_Fp2 { mod_idx = 0 }, } openvm_ecc_sw_setup::sw_init! { diff --git a/crates/toolchain/tests/programs/examples/pairing_miller_step.rs b/crates/toolchain/tests/programs/examples/pairing_miller_step.rs index 1ea0460f15..de7e864494 100644 --- a/crates/toolchain/tests/programs/examples/pairing_miller_step.rs +++ b/crates/toolchain/tests/programs/examples/pairing_miller_step.rs @@ -22,7 +22,7 @@ mod bn254 { } openvm_algebra_complex_macros::complex_init! { - Fp2 { mod_idx = 0 }, + Bn254_Fp2 { mod_idx = 0 }, } openvm_ecc_sw_setup::sw_init! { @@ -104,7 +104,7 @@ mod bls12_381 { } openvm_algebra_complex_macros::complex_init! { - Fp2 { mod_idx = 0 }, + Bls12_381_Fp2 { mod_idx = 0 }, } openvm_ecc_sw_setup::sw_init! { diff --git a/extensions/pairing/guest/src/bls12_381/fp2.rs b/extensions/pairing/guest/src/bls12_381/fp2.rs index 7af9f2f62e..34feb14e0b 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_381_Fp2 { mod_type = Fp } } complex_impl_field! { - Fp2, + Bls12_381_Fp2, } +pub type Fp2 = Bls12_381_Fp2; + 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..e6cbac32ff 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 } + Bn254_Fp2 { mod_type = Fp } } complex_impl_field! { - Fp2, + Bn254_Fp2, } +pub type Fp2 = Bn254_Fp2; + impl FieldExtension for Fp2 { const D: usize = 2; type Coeffs = [Fp; 2]; From bdb2472bf8578254f09de1f3da29ec42fcb9ea83 Mon Sep 17 00:00:00 2001 From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com> Date: Sat, 14 Dec 2024 22:55:30 -0500 Subject: [PATCH 2/3] chore: renames --- book/src/using-extensions/customizable-extensions.md | 6 +++--- .../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 | 8 ++------ .../tests/programs/examples/pairing_miller_step.rs | 12 ++---------- extensions/pairing/guest/src/bls12_381/fp2.rs | 6 +++--- extensions/pairing/guest/src/bn254/fp2.rs | 6 +++--- 9 files changed, 15 insertions(+), 47 deletions(-) diff --git a/book/src/using-extensions/customizable-extensions.md b/book/src/using-extensions/customizable-extensions.md index 9bf789f23e..dca5c25704 100644 --- a/book/src/using-extensions/customizable-extensions.md +++ b/book/src/using-extensions/customizable-extensions.md @@ -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 }, + Bn254 { 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 cae6b3ab32..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! { - Bn254_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! { - Bls12_381_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 62353e0a42..cb7e670e1f 100644 --- a/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs +++ b/crates/toolchain/tests/programs/examples/pairing_miller_loop.rs @@ -24,7 +24,7 @@ mod bn254 { } openvm_algebra_complex_macros::complex_init! { - Bn254_Fp2 { mod_idx = 0 }, + Bn254Fp2 { mod_idx = 0 }, } pub fn test_miller_loop(io: &[u8]) { @@ -68,11 +68,7 @@ mod bls12_381 { } openvm_algebra_complex_macros::complex_init! { - Bls12_381_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 de7e864494..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! { - Bn254_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! { - Bls12_381_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 34feb14e0b..746efbbbd2 100644 --- a/extensions/pairing/guest/src/bls12_381/fp2.rs +++ b/extensions/pairing/guest/src/bls12_381/fp2.rs @@ -9,14 +9,14 @@ 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! { - Bls12_381_Fp2 { mod_type = Fp } + Bls12_381Fp2 { mod_type = Fp } } complex_impl_field! { - Bls12_381_Fp2, + Bls12_381Fp2, } -pub type Fp2 = Bls12_381_Fp2; +pub type Fp2 = Bls12_381Fp2; impl FieldExtension for Fp2 { const D: usize = 2; diff --git a/extensions/pairing/guest/src/bn254/fp2.rs b/extensions/pairing/guest/src/bn254/fp2.rs index e6cbac32ff..6f3ba86430 100644 --- a/extensions/pairing/guest/src/bn254/fp2.rs +++ b/extensions/pairing/guest/src/bn254/fp2.rs @@ -9,14 +9,14 @@ 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! { - Bn254_Fp2 { mod_type = Fp } + Bn254Fp2 { mod_type = Fp } } complex_impl_field! { - Bn254_Fp2, + Bn254Fp2, } -pub type Fp2 = Bn254_Fp2; +pub type Fp2 = Bn254Fp2; impl FieldExtension for Fp2 { const D: usize = 2; From 41210e37dbfd4f5f0cf7b426ecf307d6701a921f Mon Sep 17 00:00:00 2001 From: Jonathan Wang <31040440+jonathanpwang@users.noreply.github.com> Date: Sat, 14 Dec 2024 23:01:30 -0500 Subject: [PATCH 3/3] chore: more rename --- book/src/using-extensions/customizable-extensions.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/book/src/using-extensions/customizable-extensions.md b/book/src/using-extensions/customizable-extensions.md index dca5c25704..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" }, } ``` @@ -48,7 +48,7 @@ To declare an elliptic curve struct, one needs to use the `sw_declare!` macro. A ```rust sw_declare! { Bls12_381G1Affine { mod_type = Bls12_381Fp, b = BLS12_381_B }, - Bn254 { mod_type = Bn254Fp, b = BN254_B }, + Bn254G1Affine { mod_type = Bn254Fp, b = BN254_B }, } ```