Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ALT_BN128_MULTIPLICATION_INPUT_LEN constant #3686

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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