forked from neonevm/neon-evm
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bn256 precompile arguments (#205)
- Loading branch information
1 parent
af4d71e
commit ed1f97b
Showing
1 changed file
with
26 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |