-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sha3FIPS standard precompiles and ECDSA public key recovery (#304)
* Sha3 and ECDSA precompiles * Tests for sha3fips * Updates to spec and adds tests * Spacing and formatting * Fmt * Comment out test * Updates * Updates * Update ECRecoverTests.sol * Remove merge conflict * Remove unnecessary yarn file * Uncomment tests * Fix tests * Reset simple-specs.json change * Remove unused frontier-spec.json file Co-authored-by: Wei Tang <accounts@that.world> Co-authored-by: Wei Tang <wei@that.world>
- Loading branch information
1 parent
4914624
commit 20964b1
Showing
12 changed files
with
2,850 additions
and
1,407 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "pallet-evm-precompile-sha3fips" | ||
version = "3.0.0" | ||
authors = ["Parity Technologies <admin@parity.io>", "Drew Stone <drew@commonwealth.im>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "SHA3 FIPS202 precompile for EVM pallet." | ||
|
||
[dependencies] | ||
sp-core = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } | ||
sp-io = { version = "3.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } | ||
fp-evm = { version = "0.8.0", default-features = false, path = "../../../../primitives/evm" } | ||
evm = { version = "0.24.0", default-features = false, features = ["with-codec"] } | ||
tiny-keccak = { version = "2.0", features = ["fips202"] } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"sp-core/std", | ||
"sp-io/std", | ||
"fp-evm/std", | ||
"evm/std", | ||
] |
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,153 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
extern crate alloc; | ||
|
||
use tiny_keccak::Hasher; | ||
use alloc::vec::Vec; | ||
|
||
use fp_evm::LinearCostPrecompile; | ||
use evm::{ExitSucceed, ExitError}; | ||
|
||
pub struct Sha3FIPS256; | ||
|
||
impl LinearCostPrecompile for Sha3FIPS256 { | ||
const BASE: u64 = 60; | ||
const WORD: u64 = 12; | ||
|
||
fn execute( | ||
input: &[u8], | ||
_: u64, | ||
) -> core::result::Result<(ExitSucceed, Vec<u8>), ExitError> { | ||
let mut output = [0; 32]; | ||
let mut sha3 = tiny_keccak::Sha3::v256(); | ||
sha3.update(input); | ||
sha3.finalize(&mut output); | ||
Ok((ExitSucceed::Returned, output.to_vec())) | ||
} | ||
} | ||
|
||
pub struct Sha3FIPS512; | ||
|
||
impl LinearCostPrecompile for Sha3FIPS512 { | ||
const BASE: u64 = 60; | ||
const WORD: u64 = 12; | ||
|
||
fn execute( | ||
input: &[u8], | ||
_: u64, | ||
) -> core::result::Result<(ExitSucceed, Vec<u8>), ExitError> { | ||
let mut output = [0; 64]; | ||
let mut sha3 = tiny_keccak::Sha3::v512(); | ||
sha3.update(input); | ||
sha3.finalize(&mut output); | ||
Ok((ExitSucceed::Returned, output.to_vec())) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_empty_input() -> std::result::Result<(), ExitError> { | ||
let input: [u8; 0] = []; | ||
let expected = b"\ | ||
\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6\x62\ | ||
\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8\x43\x4a\ | ||
"; | ||
|
||
let cost: u64 = 1; | ||
|
||
match Sha3FIPS256::execute(&input, cost) { | ||
Ok((_, out)) => { | ||
assert_eq!(out, expected); | ||
Ok(()) | ||
}, | ||
Err(e) => { | ||
panic!("Test not expected to fail: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn hello_sha3_256() -> std::result::Result<(), ExitError>{ | ||
let input = b"hello"; | ||
let expected = b"\ | ||
\x33\x38\xbe\x69\x4f\x50\xc5\xf3\x38\x81\x49\x86\xcd\xf0\x68\x64\ | ||
\x53\xa8\x88\xb8\x4f\x42\x4d\x79\x2a\xf4\xb9\x20\x23\x98\xf3\x92\ | ||
"; | ||
|
||
let cost: u64 = 1; | ||
|
||
match Sha3FIPS256::execute(input, cost) { | ||
Ok((_, out)) => { | ||
assert_eq!(out, expected); | ||
Ok(()) | ||
}, | ||
Err(e) => { | ||
panic!("Test not expected to fail: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn long_string_sha3_256() -> std::result::Result<(), ExitError>{ | ||
let input = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | ||
let expected = b"\ | ||
\xbd\xe3\xf2\x69\x17\x5e\x1d\xcd\xa1\x38\x48\x27\x8a\xa6\x04\x6b\ | ||
\xd6\x43\xce\xa8\x5b\x84\xc8\xb8\xbb\x80\x95\x2e\x70\xb6\xea\xe0\ | ||
"; | ||
|
||
let cost: u64 = 1; | ||
|
||
match Sha3FIPS256::execute(input, cost) { | ||
Ok((_, out)) => { | ||
assert_eq!(out, expected); | ||
Ok(()) | ||
}, | ||
Err(e) => { | ||
panic!("Test not expected to fail: {:?}", e); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn long_string_sha3_512() -> std::result::Result<(), ExitError>{ | ||
let input = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; | ||
let expected = b"\ | ||
\xf3\x2a\x94\x23\x55\x13\x51\xdf\x0a\x07\xc0\xb8\xc2\x0e\xb9\x72\ | ||
\x36\x7c\x39\x8d\x61\x06\x60\x38\xe1\x69\x86\x44\x8e\xbf\xbc\x3d\ | ||
\x15\xed\xe0\xed\x36\x93\xe3\x90\x5e\x9a\x8c\x60\x1d\x9d\x00\x2a\ | ||
\x06\x85\x3b\x97\x97\xef\x9a\xb1\x0c\xbd\xe1\x00\x9c\x7d\x0f\x09\ | ||
"; | ||
|
||
let cost: u64 = 1; | ||
|
||
match Sha3FIPS512::execute(input, cost) { | ||
Ok((_, out)) => { | ||
assert_eq!(out, expected); | ||
Ok(()) | ||
}, | ||
Err(e) => { | ||
panic!("Test not expected to fail: {:?}", e); | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pragma solidity 0.8.2; | ||
|
||
contract ECRecoverTests { | ||
function ecrecover(bytes memory input) public returns(bytes memory) { | ||
address ecrecoverAddress = address(0x0000000000000000000000000000000000000001); | ||
(bool success, bytes memory returnData) = ecrecoverAddress.call(input); | ||
return returnData; | ||
} | ||
} |
Oops, something went wrong.