-
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
refactor(precompile): make use of padding utilities, simplify secp256k1 #1073
Merged
+64
−81
Merged
Changes from 1 commit
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,8 +181,8 @@ impl Precompiles { | |
|
||
/// Returns the precompile for the given address. | ||
#[inline] | ||
pub fn get(&self, address: &Address) -> Option<Precompile> { | ||
self.inner.get(address).cloned() | ||
pub fn get(&self, address: &Address) -> Option<&Precompile> { | ||
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. Precompile is just a function, as we know concrete type, it is okay to clone it |
||
self.inner.get(address) | ||
} | ||
|
||
/// Is the precompiles list empty. | ||
|
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,21 +1,19 @@ | ||
use crate::{ | ||
primitives::U256, | ||
utilities::{get_right_padded, get_right_padded_vec, left_padding, left_padding_vec}, | ||
Error, Precompile, PrecompileResult, PrecompileWithAddress, StandardPrecompileFn, | ||
Error, Precompile, PrecompileResult, PrecompileWithAddress, | ||
}; | ||
use alloc::vec::Vec; | ||
use aurora_engine_modexp::modexp; | ||
use core::cmp::{max, min}; | ||
|
||
pub const BYZANTIUM: PrecompileWithAddress = PrecompileWithAddress( | ||
crate::u64_to_address(5), | ||
Precompile::Standard(byzantium_run as StandardPrecompileFn), | ||
Precompile::Standard(byzantium_run), | ||
); | ||
|
||
pub const BERLIN: PrecompileWithAddress = PrecompileWithAddress( | ||
crate::u64_to_address(5), | ||
Precompile::Standard(berlin_run as StandardPrecompileFn), | ||
); | ||
pub const BERLIN: PrecompileWithAddress = | ||
PrecompileWithAddress(crate::u64_to_address(5), Precompile::Standard(berlin_run)); | ||
|
||
/// See: <https://eips.ethereum.org/EIPS/eip-198> | ||
/// See: <https://etherscan.io/address/0000000000000000000000000000000000000005> | ||
|
@@ -139,12 +137,7 @@ fn byzantium_gas_calc(base_len: u64, exp_len: u64, mod_len: u64, exp_highp: &U25 | |
let iter_count = U256::from(calculate_iteration_count(exp_len, exp_highp)); | ||
// mul * iter_count bounded by 2^195 < 2^256 (no overflow) | ||
let gas = (mul * iter_count) / U256::from(20); | ||
|
||
if gas.as_limbs()[1] != 0 || gas.as_limbs()[2] != 0 || gas.as_limbs()[3] != 0 { | ||
u64::MAX | ||
} else { | ||
gas.as_limbs()[0] | ||
} | ||
gas.saturating_to() | ||
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. Liking this a lot! |
||
} | ||
|
||
// Calculate gas cost according to EIP 2565: | ||
|
@@ -163,12 +156,7 @@ fn berlin_gas_calc(base_length: u64, exp_length: u64, mod_length: u64, exp_highp | |
let multiplication_complexity = calculate_multiplication_complexity(base_length, mod_length); | ||
let iteration_count = calculate_iteration_count(exp_length, exp_highp); | ||
let gas = (multiplication_complexity * U256::from(iteration_count)) / U256::from(3); | ||
|
||
if gas.as_limbs()[1] != 0 || gas.as_limbs()[2] != 0 || gas.as_limbs()[3] != 0 { | ||
u64::MAX | ||
} else { | ||
max(200, gas.as_limbs()[0]) | ||
} | ||
max(200, gas.saturating_to()) | ||
} | ||
|
||
#[cfg(test)] | ||
|
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
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.
The previous version seems better
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.
::digest
returns a new array so this avoids thecopy_from_slice
by directly using the output buffer