Skip to content

Commit

Permalink
Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant (#3686)
Browse files Browse the repository at this point in the history
* Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant

* Add feature

* Apply suggestions from code review

Co-authored-by: samkim-crypto <[email protected]>

* Add SIMD

---------

Co-authored-by: samkim-crypto <[email protected]>
  • Loading branch information
LStan and samkim-crypto authored Jan 24, 2025
1 parent d63bd1c commit cb9cc49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
17 changes: 14 additions & 3 deletions curves/bn254/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -275,12 +275,23 @@ mod target_arch {
}

pub fn alt_bn128_multiplication(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> {
if input.len() > ALT_BN128_MULTIPLICATION_INPUT_LEN {
alt_bn128_apply_multiplication(input, ALT_BN128_MULTIPLICATION_INPUT_LEN)
}

pub fn alt_bn128_multiplication_128(input: &[u8]) -> Result<Vec<u8>, AltBn128Error> {
alt_bn128_apply_multiplication(input, 128) // hard-code length; we will remove this function in the future
}

fn alt_bn128_apply_multiplication(
input: &[u8],
expected_length: usize,
) -> Result<Vec<u8>, 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::from_be_bytes(&input[..64])?.try_into()?;
let mut fr_bytes = [0u8; 32];
Expand Down
18 changes: 14 additions & 4 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ pub use self::{
use {
solana_account_info::AccountInfo,
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_cpi::MAX_RETURN_DATA,
Expand Down Expand Up @@ -1728,7 +1729,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());
Expand Down
5 changes: 5 additions & 0 deletions sdk/feature-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -1145,6 +1149,7 @@ lazy_static! {
(deplete_cu_meter_on_vm_failure::id(), "Deplete compute meter for vm errors SIMD-0182 #3993"),
(reserve_minimal_cus_for_builtin_instructions::id(), "Reserve minimal CUs for builtin instructions SIMD-170 #2562"),
(raise_block_limits_to_50m::id(), "Raise block limit to 50M SIMD-0207"),
(fix_alt_bn128_multiplication_input_length::id(), "fix alt_bn128 multiplication input length SIMD-0222 #3686"),
/*************** ADD NEW FEATURES HERE ***************/
]
.iter()
Expand Down

0 comments on commit cb9cc49

Please sign in to comment.