Skip to content

Commit

Permalink
Fix bn256 precompile arguments (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-lisanin authored and afalaleev committed Oct 18, 2023
1 parent af4d71e commit ed1f97b
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions evm_loader/program/src/evm/precompile/bn256.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
use ethnum::U256;
use solana_program::alt_bn128::prelude::*;

/// Call inner `bn256Add`
#[must_use]
pub fn bn256_add(input: &[u8]) -> Vec<u8> {
alt_bn128_addition(input).unwrap_or_else(|_| vec![])
if input.len() >= ALT_BN128_ADDITION_INPUT_LEN {
alt_bn128_addition(&input[..ALT_BN128_ADDITION_INPUT_LEN])
} else {
let mut buffer = vec![0_u8; ALT_BN128_ADDITION_INPUT_LEN];
buffer[..input.len()].copy_from_slice(input);
alt_bn128_addition(&buffer)
}
.unwrap()
}

/// Call inner `bn256ScalarMul`
#[must_use]
pub fn bn256_scalar_mul(input: &[u8]) -> Vec<u8> {
alt_bn128_multiplication(input).unwrap_or_else(|_| vec![])
if input.len() >= ALT_BN128_MULTIPLICATION_INPUT_LEN {
alt_bn128_multiplication(&input[..ALT_BN128_MULTIPLICATION_INPUT_LEN])
} else {
let mut buffer = vec![0_u8; ALT_BN128_MULTIPLICATION_INPUT_LEN];
buffer[..input.len()].copy_from_slice(input);
alt_bn128_multiplication(&buffer)
}
.unwrap()
}

/// Call inner `bn256Pairing`
#[must_use]
pub fn bn256_pairing(input: &[u8]) -> Vec<u8> {
alt_bn128_pairing(input).unwrap_or_else(|_| vec![])
if input.is_empty() {
return U256::ONE.to_be_bytes().to_vec();
}

if (input.len() % ALT_BN128_PAIRING_ELEMENT_LEN) != 0 {
return U256::ZERO.to_be_bytes().to_vec();
}

alt_bn128_pairing(input).unwrap()
}

0 comments on commit ed1f97b

Please sign in to comment.