Skip to content
Merged
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
14 changes: 9 additions & 5 deletions crates/precompile/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn benchmark_crypto_precompiles(c: &mut Criterion) {
u64::MAX,
)
.unwrap()
.0;
.gas_used;

println!("gas used by regular pairing call: {:?}", res);

Expand All @@ -60,7 +60,9 @@ pub fn benchmark_crypto_precompiles(c: &mut Criterion) {
)
.unwrap();

let res = run_add(&ecadd_input, ISTANBUL_ADD_GAS_COST, 150).unwrap().0;
let res = run_add(&ecadd_input, ISTANBUL_ADD_GAS_COST, 150)
.unwrap()
.gas_used;
println!("gas used by bn128 add precompile: {:?}", res);

// === ECRECOVER ===
Expand All @@ -86,7 +88,9 @@ pub fn benchmark_crypto_precompiles(c: &mut Criterion) {
message_and_signature[64..128].copy_from_slice(&data);

let message_and_signature = Bytes::from(message_and_signature);
let gas = ec_recover_run(&message_and_signature, u64::MAX).unwrap().0;
let gas = ec_recover_run(&message_and_signature, u64::MAX)
.unwrap()
.gas_used;
println!("gas used by ecrecover precompile: {:?}", gas);

// === POINT_EVALUATION ===
Expand All @@ -103,8 +107,8 @@ pub fn benchmark_crypto_precompiles(c: &mut Criterion) {

let gas = 50000;
let env = Env::default();
let (actual_gas, _actual_output) = run(&kzg_input, gas, &env).unwrap();
println!("gas used by kzg precompile: {:?}", actual_gas);
let output = run(&kzg_input, gas, &env).unwrap();
println!("gas used by kzg precompile: {:?}", output.gas_used);

group.bench_function(group_name("ecrecover precompile"), |b| {
b.iter(|| {
Expand Down
10 changes: 5 additions & 5 deletions crates/precompile/src/blake2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{Error, Precompile, PrecompileResult, PrecompileWithAddress};
use revm_primitives::Bytes;
use revm_primitives::{Bytes, PrecompileOutput};

const F_ROUND: u64 = 1;
const INPUT_LENGTH: usize = 213;
Expand All @@ -14,20 +14,20 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
let input = &input[..];

if input.len() != INPUT_LENGTH {
return Err(Error::Blake2WrongLength);
return Err(Error::Blake2WrongLength.into());
}

let f = match input[212] {
1 => true,
0 => false,
_ => return Err(Error::Blake2WrongFinalIndicatorFlag),
_ => return Err(Error::Blake2WrongFinalIndicatorFlag.into()),
};

// rounds 4 bytes
let rounds = u32::from_be_bytes(input[..4].try_into().unwrap()) as usize;
let gas_used = rounds as u64 * F_ROUND;
if gas_used > gas_limit {
return Err(Error::OutOfGas);
return Err(Error::OutOfGas.into());
}

let mut h = [0u64; 8];
Expand All @@ -51,7 +51,7 @@ pub fn run(input: &Bytes, gas_limit: u64) -> PrecompileResult {
out[i..i + 8].copy_from_slice(&h.to_le_bytes());
}

Ok((gas_used, out.into()))
Ok(PrecompileOutput::new(gas_used, out.into()))
}

pub mod algo {
Expand Down
15 changes: 9 additions & 6 deletions crates/precompile/src/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,23 @@ mod test {
let Some(gas) = vector.gas else {
panic!("gas is missing in {test_name}");
};
let (actual_gas, actual_output) =
res.unwrap_or_else(|e| panic!("precompile call failed for {test_name}: {e}"));
let outcome = res.unwrap_or_else(|e: revm_primitives::PrecompileErrors| {
panic!("precompile call failed for {test_name}: {e}")
});
assert_eq!(
gas, actual_gas,
gas, outcome.gas_used,
"expected gas: {}, actual gas: {} in {test_name}",
gas, actual_gas
gas, outcome.gas_used
);
let Some(expected) = vector.expected else {
panic!("expected output is missing in {test_name}");
};
let expected_output = Bytes::from_hex(expected).unwrap();
assert_eq!(
expected_output, actual_output,
"expected output: {expected_output}, actual output: {actual_output} in {test_name}");
expected_output, outcome.bytes,
"expected output: {expected_output}, actual output: {:?} in {test_name}",
outcome.bytes
);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/precompile/src/bls12_381/g1_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{u64_to_address, PrecompileWithAddress};
use blst::{
blst_p1, blst_p1_add_or_double_affine, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine,
};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -23,14 +23,15 @@ const INPUT_LENGTH: usize = 256;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-addition>
pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G1ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
))
.into());
}

// NB: There is no subgroup check for the G1 addition precompile.
Expand All @@ -52,5 +53,5 @@ pub(super) fn g1_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((BASE_GAS_FEE, out))
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}
11 changes: 6 additions & 5 deletions crates/precompile/src/bls12_381/g1_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_to_affine, p1_affines};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MSM precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -30,13 +30,14 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
"G1MSM input length should be multiple of {}, was {}",
g1_mul::INPUT_LENGTH,
input_len
)));
))
.into());
}

let k = input_len / g1_mul::INPUT_LENGTH;
let required_gas = msm_required_gas(k, g1_mul::BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

let mut g1_points: Vec<blst_p1> = Vec::with_capacity(k);
Expand Down Expand Up @@ -72,7 +73,7 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {

// return infinity point if all points are infinity
if g1_points.is_empty() {
return Ok((required_gas, [0; 128].into()));
return Ok(PrecompileOutput::new(required_gas, [0; 128].into()));
}

let points = p1_affines::from(&g1_points);
Expand All @@ -83,5 +84,5 @@ pub(super) fn g1_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut multiexp_aff, &multiexp) };

let out = encode_g1_point(&multiexp_aff);
Ok((required_gas, out))
Ok(PrecompileOutput::new(required_gas, out))
}
9 changes: 5 additions & 4 deletions crates/precompile/src/bls12_381/g1_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p1, blst_p1_affine, blst_p1_from_affine, blst_p1_mult, blst_p1_to_affine};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G1MUL precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -25,13 +25,14 @@ pub(super) const INPUT_LENGTH: usize = 160;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g1-multiplication>
pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G1MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
))
.into());
}

// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
Expand All @@ -55,5 +56,5 @@ pub(super) fn g1_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p1_to_affine(&mut p_aff, &p) };

let out = encode_g1_point(&p_aff);
Ok((BASE_GAS_FEE, out))
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}
9 changes: 5 additions & 4 deletions crates/precompile/src/bls12_381/g2_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{u64_to_address, PrecompileWithAddress};
use blst::{
blst_p2, blst_p2_add_or_double_affine, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine,
};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2ADD precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -24,14 +24,15 @@ const INPUT_LENGTH: usize = 512;
/// See also <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-addition>
pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G2ADD input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
))
.into());
}

// NB: There is no subgroup check for the G2 addition precompile.
Expand All @@ -53,5 +54,5 @@ pub(super) fn g2_add(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut p_aff, &p) };

let out = encode_g2_point(&p_aff);
Ok((BASE_GAS_FEE, out))
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}
11 changes: 6 additions & 5 deletions crates/precompile/src/bls12_381/g2_msm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_to_affine, p2_affines};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MSM precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -30,13 +30,14 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
"G2MSM input length should be multiple of {}, was {}",
g2_mul::INPUT_LENGTH,
input_len
)));
))
.into());
}

let k = input_len / g2_mul::INPUT_LENGTH;
let required_gas = msm_required_gas(k, g2_mul::BASE_GAS_FEE);
if required_gas > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

let mut g2_points: Vec<blst_p2> = Vec::with_capacity(k);
Expand Down Expand Up @@ -72,7 +73,7 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {

// return infinity point if all points are infinity
if g2_points.is_empty() {
return Ok((required_gas, [0; 256].into()));
return Ok(PrecompileOutput::new(required_gas, [0; 256].into()));
}

let points = p2_affines::from(&g2_points);
Expand All @@ -83,5 +84,5 @@ pub(super) fn g2_msm(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut multiexp_aff, &multiexp) };

let out = encode_g2_point(&multiexp_aff);
Ok((required_gas, out))
Ok(PrecompileOutput::new(required_gas, out))
}
9 changes: 5 additions & 4 deletions crates/precompile/src/bls12_381/g2_mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_p2, blst_p2_affine, blst_p2_from_affine, blst_p2_mult, blst_p2_to_affine};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_G2MUL precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -25,13 +25,14 @@ pub(super) const INPUT_LENGTH: usize = 288;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-g2-multiplication>
pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}
if input.len() != INPUT_LENGTH {
return Err(PrecompileError::Other(format!(
"G2MUL input should be {INPUT_LENGTH} bytes, was {}",
input.len()
)));
))
.into());
}

// NB: Scalar multiplications, MSMs and pairings MUST perform a subgroup check.
Expand All @@ -52,5 +53,5 @@ pub(super) fn g2_mul(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut p_aff, &p) };

let out = encode_g2_point(&p_aff);
Ok((BASE_GAS_FEE, out))
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}
9 changes: 5 additions & 4 deletions crates/precompile/src/bls12_381/map_fp2_to_g2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
};
use crate::{u64_to_address, PrecompileWithAddress};
use blst::{blst_map_to_g2, blst_p2, blst_p2_affine, blst_p2_to_affine};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileResult};
use revm_primitives::{Bytes, Precompile, PrecompileError, PrecompileOutput, PrecompileResult};

/// [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537#specification) BLS12_MAP_FP2_TO_G2 precompile.
pub const PRECOMPILE: PrecompileWithAddress =
Expand All @@ -23,14 +23,15 @@ const BASE_GAS_FEE: u64 = 75000;
/// See also: <https://eips.ethereum.org/EIPS/eip-2537#abi-for-mapping-fp2-element-to-g2-point>
pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult {
if BASE_GAS_FEE > gas_limit {
return Err(PrecompileError::OutOfGas);
return Err(PrecompileError::OutOfGas.into());
}

if input.len() != PADDED_FP2_LENGTH {
return Err(PrecompileError::Other(format!(
"MAP_FP2_TO_G2 input should be {PADDED_FP2_LENGTH} bytes, was {}",
input.len()
)));
))
.into());
}

let input_p0_x = remove_padding(&input[..PADDED_FP_LENGTH])?;
Expand All @@ -47,5 +48,5 @@ pub(super) fn map_fp2_to_g2(input: &Bytes, gas_limit: u64) -> PrecompileResult {
unsafe { blst_p2_to_affine(&mut p_aff, &p) };

let out = encode_g2_point(&p_aff);
Ok((BASE_GAS_FEE, out))
Ok(PrecompileOutput::new(BASE_GAS_FEE, out))
}
Loading