-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1408 from oasisprotocol/ptrus/feature/sha512-256
evm: add sha512-256 precompile
- Loading branch information
Showing
8 changed files
with
408 additions
and
32 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//! Implements sha512_256 precompile. | ||
use evm::{ | ||
executor::stack::{PrecompileHandle, PrecompileOutput}, | ||
ExitSucceed, | ||
}; | ||
use ripemd160::Digest as _; | ||
use sha2::Sha512Trunc256; | ||
|
||
use super::{record_linear_cost, PrecompileResult}; | ||
|
||
pub(super) fn call_sha512_256(handle: &mut impl PrecompileHandle) -> PrecompileResult { | ||
// Costs were computed by benchmarking and comparing to SHA256 and using the SHA256 costs (defined by EVM spec). | ||
// See benches/criterion_benchmark.rs for the benchmarks. | ||
record_linear_cost(handle, handle.input().len() as u64, 115, 13)?; | ||
|
||
let mut hasher = Sha512Trunc256::new(); | ||
hasher.update(handle.input()); | ||
let digest = hasher.finalize(); | ||
|
||
Ok(PrecompileOutput { | ||
exit_status: ExitSucceed::Returned, | ||
output: digest.to_vec(), | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::super::testing::*; | ||
|
||
#[test] | ||
fn test_sha512_256() { | ||
let input = "38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e000000000000000000000000000000000000000000000000000000000000001b38d18acb67d25c8bb9942764b62f18e17054f66a817bd4295423adf9ed98873e789d1dd423d25f0772d2748d60f7e4b81bb14d086eba8e8e8efb6dcff8a4ae02"; | ||
let ret = call_contract( | ||
H160([ | ||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0x01, | ||
]), | ||
&hex::decode(input).unwrap(), | ||
3000, | ||
) | ||
.unwrap(); | ||
assert_eq!( | ||
hex::encode(ret.unwrap().output), | ||
"41f7883fc8df1d31b1b1f7c0379f7b5a990d457347d997fdd76a2f4bb5812342" | ||
); | ||
} | ||
} |
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