-
Notifications
You must be signed in to change notification settings - Fork 642
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
perf(precompile): use Bytes
in precompile functions
#1085
Merged
+74
−80
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
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,11 +1,11 @@ | ||
use crate::{ | ||
primitives::U256, | ||
utilities::{left_pad, left_pad_vec, right_pad_with_offset, right_pad_with_offset_vec}, | ||
utilities::{left_pad, left_pad_vec, right_pad_vec, right_pad_with_offset}, | ||
Error, Precompile, PrecompileResult, PrecompileWithAddress, | ||
}; | ||
use alloc::vec::Vec; | ||
use aurora_engine_modexp::modexp; | ||
use core::cmp::{max, min}; | ||
use revm_primitives::Bytes; | ||
|
||
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( | ||
crate::u64_to_address(5), | ||
|
@@ -17,13 +17,13 @@ pub const BERLIN: PrecompileWithAddress = | |
|
||
/// See: <https://eips.ethereum.org/EIPS/eip-198> | ||
/// See: <https://etherscan.io/address/0000000000000000000000000000000000000005> | ||
fn byzantium_run(input: &[u8], gas_limit: u64) -> PrecompileResult { | ||
fn byzantium_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { | ||
run_inner(input, gas_limit, 0, |a, b, c, d| { | ||
byzantium_gas_calc(a, b, c, d) | ||
}) | ||
} | ||
|
||
pub fn berlin_run(input: &[u8], gas_limit: u64) -> PrecompileResult { | ||
pub fn berlin_run(input: &Bytes, gas_limit: u64) -> PrecompileResult { | ||
run_inner(input, gas_limit, 200, |a, b, c, d| { | ||
berlin_gas_calc(a, b, c, d) | ||
}) | ||
|
@@ -52,6 +52,7 @@ where | |
if min_gas > gas_limit { | ||
return Err(Error::OutOfGas); | ||
} | ||
|
||
// The format of input is: | ||
// <length_of_BASE> <length_of_EXPONENT> <length_of_MODULUS> <BASE> <EXPONENT> <MODULUS> | ||
// Where every length is a 32-byte left-padded integer representing the number of bytes | ||
|
@@ -71,26 +72,21 @@ where | |
return Err(Error::ModexpModOverflow); | ||
}; | ||
|
||
// Handle a special case when both the base and mod length is zero | ||
// Handle a special case when both the base and mod length are zero. | ||
if base_len == 0 && mod_len == 0 { | ||
return Ok((min_gas, Vec::new())); | ||
return Ok((min_gas, Bytes::new())); | ||
} | ||
|
||
// cast exponent length to usize, it does not make sense to handle larger values. | ||
// Cast exponent length to usize, since it does not make sense to handle larger values. | ||
let Ok(exp_len) = usize::try_from(exp_len) else { | ||
return Err(Error::ModexpModOverflow); | ||
}; | ||
|
||
// Used to extract ADJUSTED_EXPONENT_LENGTH. | ||
let exp_highp_len = min(exp_len, 32); | ||
|
||
// throw away the header data as we already extracted lengths. | ||
let input = if input.len() >= 96 { | ||
&input[HEADER_LENGTH..] | ||
} else { | ||
// or set input to zero if there is no more data | ||
&[] | ||
}; | ||
// Throw away the header data as we already extracted lengths. | ||
let input = input.get(HEADER_LENGTH..).unwrap_or_default(); | ||
|
||
let exp_highp = { | ||
// get right padded bytes so if data.len is less then exp_len we will get right padded zeroes. | ||
|
@@ -100,23 +96,23 @@ where | |
U256::from_be_bytes(out.into_owned()) | ||
}; | ||
|
||
// calculate gas spent. | ||
// Check if we have enough gas. | ||
let gas_cost = calc_gas(base_len as u64, exp_len as u64, mod_len as u64, &exp_highp); | ||
// check if we have enough gas. | ||
if gas_cost > gas_limit { | ||
return Err(Error::OutOfGas); | ||
} | ||
|
||
// Padding is needed if the input does not contain all 3 values. | ||
let base = right_pad_with_offset_vec(input, 0, base_len); | ||
let exponent = right_pad_with_offset_vec(input, base_len, exp_len); | ||
let modulus = right_pad_with_offset_vec(input, base_len.saturating_add(exp_len), mod_len); | ||
let input = right_pad_vec(input, base_len + exp_len + mod_len); | ||
let (base, input) = input.split_at(base_len); | ||
let (exponent, modulus) = input.split_at(exp_len); | ||
debug_assert_eq!(modulus.len(), mod_len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made this allocate only once in case input is too short.
|
||
|
||
// Call the modexp. | ||
let output = modexp(&base, &exponent, &modulus); | ||
|
||
// left pad the result to modulus length. bytes will always by less or equal to modulus length. | ||
Ok((gas_cost, left_pad_vec(&output, mod_len).into_owned())) | ||
Ok((gas_cost, left_pad_vec(&output, mod_len).into_owned().into())) | ||
} | ||
|
||
fn byzantium_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U256) -> u64 { | ||
|
@@ -163,6 +159,7 @@ fn berlin_gas_calc(base_length: u64, exp_length: u64, mod_length: u64, exp_highp | |
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use alloc::vec::Vec; | ||
use revm_primitives::hex; | ||
|
||
struct Test { | ||
|
@@ -343,8 +340,7 @@ mod tests { | |
#[test] | ||
fn test_byzantium_modexp_gas() { | ||
for (test, &test_gas) in TESTS.iter().zip(BYZANTIUM_GAS.iter()) { | ||
let input = hex::decode(test.input).unwrap(); | ||
|
||
let input = hex::decode(test.input).unwrap().into(); | ||
let res = byzantium_run(&input, 100_000_000).unwrap(); | ||
let expected = hex::decode(test.expected).unwrap(); | ||
assert_eq!( | ||
|
@@ -359,7 +355,7 @@ mod tests { | |
#[test] | ||
fn test_berlin_modexp_gas() { | ||
for (test, &test_gas) in TESTS.iter().zip(BERLIN_GAS.iter()) { | ||
let input = hex::decode(test.input).unwrap(); | ||
let input = hex::decode(test.input).unwrap().into(); | ||
let res = berlin_run(&input, 100_000_000).unwrap(); | ||
let expected = hex::decode(test.expected).unwrap(); | ||
assert_eq!( | ||
|
@@ -373,7 +369,7 @@ mod tests { | |
|
||
#[test] | ||
fn test_berlin_modexp_empty_input() { | ||
let res = berlin_run(&[], 100_000).unwrap(); | ||
let res = berlin_run(&Bytes::new(), 100_000).unwrap(); | ||
let expected: Vec<u8> = Vec::new(); | ||
assert_eq!(res.1, expected) | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the biggest win IMO, avoids malloc+memcpy basically as if we're using Arc.
identity
precompile is common in code generated by Vyper.