Skip to content

Commit

Permalink
NDEV-1396 Implement missing precompiled contracts (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-lisanin authored and afalaleev committed Oct 11, 2023
1 parent 882adc0 commit 86fe69e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
54 changes: 24 additions & 30 deletions evm_loader/program/src/evm/precompile/big_mod_exp.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
// use ethnum::U256;
use ethnum::U256;

#[must_use]
pub fn big_mod_exp(_input: &[u8]) -> Vec<u8> {
// Should be implemented via Solana syscall
Vec::new()
pub fn big_mod_exp(input: &[u8]) -> Vec<u8> {
if input.len() < 96 {
return vec![];
}

// if input.len() < 96 {
// return vec![];
// }
let (base_len, rest) = input.split_at(32);
let (exp_len, rest) = rest.split_at(32);
let (mod_len, rest) = rest.split_at(32);

// let (base_len, rest) = input.split_at(32);
// let (exp_len, rest) = rest.split_at(32);
// let (mod_len, rest) = rest.split_at(32);
let Ok(base_len) = U256::from_be_bytes(base_len.try_into().unwrap()).try_into() else {
return vec![];
};
let Ok(exp_len) = U256::from_be_bytes(exp_len.try_into().unwrap()).try_into() else {
return vec![];
};
let Ok(mod_len) = U256::from_be_bytes(mod_len.try_into().unwrap()).try_into() else {
return vec![];
};

// let base_len = match U256::from_be_bytes(base_len.try_into().unwrap()).try_into() {
// Ok(value) => value,
// Err(_) => return vec![]
// };
// let exp_len = match U256::from_be_bytes(exp_len.try_into().unwrap()).try_into() {
// Ok(value) => value,
// Err(_) => return vec![]
// };
// let mod_len = match U256::from_be_bytes(mod_len.try_into().unwrap()).try_into() {
// Ok(value) => value,
// Err(_) => return vec![]
// };
if base_len == 0 && mod_len == 0 {
return vec![0; 32];
}

// if base_len == 0 && mod_len == 0 {
// return vec![0; 32];
// }
let (base_val, rest) = rest.split_at(base_len);
let (exp_val, rest) = rest.split_at(exp_len);
let (mod_val, _) = rest.split_at(mod_len);

// let (base_val, rest) = rest.split_at(base_len);
// let (exp_val, rest) = rest.split_at(exp_len);
// let (mod_val, _) = rest.split_at(mod_len);

// solana_program::big_mod_exp::big_mod_exp(base_val, exp_val, mod_val)
solana_program::big_mod_exp::big_mod_exp(base_val, exp_val, mod_val)
}
21 changes: 7 additions & 14 deletions evm_loader/program/src/evm/precompile/bn256.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
// Should be implemented via Solana syscall
// use solana_program::alt_bn128::prelude::*;
use solana_program::alt_bn128::prelude::*;

/// Call inner `bn256Add`
#[must_use]
pub fn bn256_add(_input: &[u8]) -> Vec<u8> {
// Should be implemented via Solana syscall
Vec::new()
//alt_bn128_addition(input).unwrap_or_else(|_| vec![])
pub fn bn256_add(input: &[u8]) -> Vec<u8> {
alt_bn128_addition(input).unwrap_or_else(|_| vec![])
}

/// Call inner `bn256ScalarMul`
#[must_use]
pub fn bn256_scalar_mul(_input: &[u8]) -> Vec<u8> {
// Should be implemented via Solana syscall
Vec::new()
//alt_bn128_multiplication(input).unwrap_or_else(|_| vec![])
pub fn bn256_scalar_mul(input: &[u8]) -> Vec<u8> {
alt_bn128_multiplication(input).unwrap_or_else(|_| vec![])
}

/// Call inner `bn256Pairing`
#[must_use]
pub fn bn256_pairing(_input: &[u8]) -> Vec<u8> {
// Should be implemented via Solana syscall
Vec::new()
//alt_bn128_pairing(input).unwrap_or_else(|_| vec![])
pub fn bn256_pairing(input: &[u8]) -> Vec<u8> {
alt_bn128_pairing(input).unwrap_or_else(|_| vec![])
}

0 comments on commit 86fe69e

Please sign in to comment.