From 5410eb53cb3a0ef36ded8f320f80bd304ef82649 Mon Sep 17 00:00:00 2001 From: Stanislav Ladyzhenskiy Date: Sun, 17 Nov 2024 19:05:30 +0700 Subject: [PATCH 1/4] Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant --- curves/bn254/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curves/bn254/src/lib.rs b/curves/bn254/src/lib.rs index ff582abab32818..5138e44ee11966 100644 --- a/curves/bn254/src/lib.rs +++ b/curves/bn254/src/lib.rs @@ -14,7 +14,7 @@ mod consts { pub const ALT_BN128_ADDITION_INPUT_LEN: usize = 128; /// Input length for the multiplication operation. - pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 128; + pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 96; /// Pair element length. pub const ALT_BN128_PAIRING_ELEMENT_LEN: usize = 192; From 3ae233d132cac2763e51a5d89e06430abd6233e5 Mon Sep 17 00:00:00 2001 From: Stanislav Ladyzhenskiy Date: Thu, 28 Nov 2024 17:06:21 +0700 Subject: [PATCH 2/4] Add feature --- curves/bn254/src/lib.rs | 18 +++++++++++++++--- programs/bpf_loader/src/syscalls/mod.rs | 18 ++++++++++++++---- sdk/feature-set/src/lib.rs | 5 +++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/curves/bn254/src/lib.rs b/curves/bn254/src/lib.rs index 5138e44ee11966..98aed77776d06d 100644 --- a/curves/bn254/src/lib.rs +++ b/curves/bn254/src/lib.rs @@ -14,7 +14,8 @@ mod consts { pub const ALT_BN128_ADDITION_INPUT_LEN: usize = 128; /// Input length for the multiplication operation. - pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 96; + pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 128; + pub const ALT_BN128_MULTIPLICATION_INPUT_LEN_FIX: usize = 96; /// Pair element length. pub const ALT_BN128_PAIRING_ELEMENT_LEN: usize = 192; @@ -198,12 +199,23 @@ mod target_arch { } pub fn alt_bn128_multiplication(input: &[u8]) -> Result, AltBn128Error> { - if input.len() > ALT_BN128_MULTIPLICATION_INPUT_LEN { + alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN_FIX) + } + + pub fn alt_bn128_multiplication_128(input: &[u8]) -> Result, AltBn128Error> { + alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN) + } + + fn alt_bn128_apply_multiplication( + input: &[u8], + expected_length: usize, + ) -> Result, AltBn128Error> { + if input.len() > expected_length { return Err(AltBn128Error::InvalidInputData); } let mut input = input.to_vec(); - input.resize(ALT_BN128_MULTIPLICATION_INPUT_LEN, 0); + input.resize(expected_length, 0); let p: G1 = PodG1( convert_endianness_64(&input[..64]) diff --git a/programs/bpf_loader/src/syscalls/mod.rs b/programs/bpf_loader/src/syscalls/mod.rs index 3438c819c74bbe..55fc73b4af6ad7 100644 --- a/programs/bpf_loader/src/syscalls/mod.rs +++ b/programs/bpf_loader/src/syscalls/mod.rs @@ -13,9 +13,10 @@ pub use self::{ #[allow(deprecated)] use { solana_bn254::prelude::{ - alt_bn128_addition, alt_bn128_multiplication, alt_bn128_pairing, AltBn128Error, - ALT_BN128_ADDITION_OUTPUT_LEN, ALT_BN128_MULTIPLICATION_OUTPUT_LEN, - ALT_BN128_PAIRING_ELEMENT_LEN, ALT_BN128_PAIRING_OUTPUT_LEN, + alt_bn128_addition, alt_bn128_multiplication, alt_bn128_multiplication_128, + alt_bn128_pairing, AltBn128Error, ALT_BN128_ADDITION_OUTPUT_LEN, + ALT_BN128_MULTIPLICATION_OUTPUT_LEN, ALT_BN128_PAIRING_ELEMENT_LEN, + ALT_BN128_PAIRING_OUTPUT_LEN, }, solana_compute_budget::compute_budget::ComputeBudget, solana_feature_set::{ @@ -1646,7 +1647,16 @@ declare_builtin_function!( let calculation = match group_op { ALT_BN128_ADD => alt_bn128_addition, - ALT_BN128_MUL => alt_bn128_multiplication, + ALT_BN128_MUL => { + let fix_alt_bn128_multiplication_input_length = invoke_context + .get_feature_set() + .is_active(&feature_set::fix_alt_bn128_multiplication_input_length::id()); + if fix_alt_bn128_multiplication_input_length { + alt_bn128_multiplication + } else { + alt_bn128_multiplication_128 + } + } ALT_BN128_PAIRING => alt_bn128_pairing, _ => { return Err(SyscallError::InvalidAttribute.into()); diff --git a/sdk/feature-set/src/lib.rs b/sdk/feature-set/src/lib.rs index 6725f02212b425..62d97dd7c9b136 100644 --- a/sdk/feature-set/src/lib.rs +++ b/sdk/feature-set/src/lib.rs @@ -561,6 +561,10 @@ pub mod enable_alt_bn128_compression_syscall { solana_pubkey::declare_id!("EJJewYSddEEtSZHiqugnvhQHiWyZKjkFDQASd7oKSagn"); } +pub mod fix_alt_bn128_multiplication_input_length { + solana_pubkey::declare_id!("bn2puAyxUx6JUabAxYdKdJ5QHbNNmKw8dCGuGCyRrFN"); +} + pub mod enable_program_redeployment_cooldown { solana_pubkey::declare_id!("J4HFT8usBxpcF63y46t1upYobJgChmKyZPm5uTBRg25Z"); } @@ -1110,6 +1114,7 @@ lazy_static! { (accounts_lt_hash::id(), "enables lattice-based accounts hash #3333"), (enable_secp256r1_precompile::id(), "Enable secp256r1 precompile SIMD-0075"), (migrate_stake_program_to_core_bpf::id(), "Migrate Stake program to Core BPF SIMD-0196 #3655"), + (fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length #3686"), /*************** ADD NEW FEATURES HERE ***************/ ] .iter() From d2df66d3b201d1d8452ba435ea9cc7766df367e8 Mon Sep 17 00:00:00 2001 From: Stanislav Ladyzhenskiy Date: Wed, 4 Dec 2024 03:51:05 +0700 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: samkim-crypto --- curves/bn254/src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/curves/bn254/src/lib.rs b/curves/bn254/src/lib.rs index 98aed77776d06d..880917c727ca13 100644 --- a/curves/bn254/src/lib.rs +++ b/curves/bn254/src/lib.rs @@ -14,8 +14,7 @@ mod consts { pub const ALT_BN128_ADDITION_INPUT_LEN: usize = 128; /// Input length for the multiplication operation. - pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 128; - pub const ALT_BN128_MULTIPLICATION_INPUT_LEN_FIX: usize = 96; + pub const ALT_BN128_MULTIPLICATION_INPUT_LEN: usize = 96; /// Pair element length. pub const ALT_BN128_PAIRING_ELEMENT_LEN: usize = 192; @@ -199,11 +198,11 @@ mod target_arch { } pub fn alt_bn128_multiplication(input: &[u8]) -> Result, AltBn128Error> { - alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN_FIX) + alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN) } pub fn alt_bn128_multiplication_128(input: &[u8]) -> Result, AltBn128Error> { - alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN) + alt_bn128_apply_multiplication(input, 128) // hard-code length; we will remove this function in the future } fn alt_bn128_apply_multiplication( From f65ec086e74c02527fa7479de25c9c4a2724274f Mon Sep 17 00:00:00 2001 From: Stanislav Ladyzhenskiy Date: Fri, 17 Jan 2025 17:10:20 +0700 Subject: [PATCH 4/4] Add SIMD --- sdk/feature-set/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/feature-set/src/lib.rs b/sdk/feature-set/src/lib.rs index 62d97dd7c9b136..614d8811ed19d1 100644 --- a/sdk/feature-set/src/lib.rs +++ b/sdk/feature-set/src/lib.rs @@ -1114,7 +1114,7 @@ lazy_static! { (accounts_lt_hash::id(), "enables lattice-based accounts hash #3333"), (enable_secp256r1_precompile::id(), "Enable secp256r1 precompile SIMD-0075"), (migrate_stake_program_to_core_bpf::id(), "Migrate Stake program to Core BPF SIMD-0196 #3655"), - (fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length #3686"), + (fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length SIMD-0222 #3686"), /*************** ADD NEW FEATURES HERE ***************/ ] .iter()