Skip to main content

ovr_evm_precompile_sha3fips/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// This file is part of Frontier.
3//
4// Copyright (c) 2020 Parity Technologies (UK) Ltd.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18extern crate alloc;
19
20use alloc::vec::Vec;
21
22use fp_evm::{ExitSucceed, LinearCostPrecompile, PrecompileFailure};
23
24pub struct Sha3FIPS256;
25
26impl LinearCostPrecompile for Sha3FIPS256 {
27	const BASE: u64 = 60;
28	const WORD: u64 = 12;
29
30	fn execute(input: &[u8], _: u64) -> Result<(ExitSucceed, Vec<u8>), PrecompileFailure> {
31		use tiny_keccak::Hasher;
32		let mut output = [0; 32];
33		let mut sha3 = tiny_keccak::Sha3::v256();
34		sha3.update(input);
35		sha3.finalize(&mut output);
36		Ok((ExitSucceed::Returned, output.to_vec()))
37	}
38}
39
40pub struct Sha3FIPS512;
41
42impl LinearCostPrecompile for Sha3FIPS512 {
43	const BASE: u64 = 60;
44	const WORD: u64 = 12;
45
46	fn execute(input: &[u8], _: u64) -> Result<(ExitSucceed, Vec<u8>), PrecompileFailure> {
47		use tiny_keccak::Hasher;
48		let mut output = [0; 64];
49		let mut sha3 = tiny_keccak::Sha3::v512();
50		sha3.update(input);
51		sha3.finalize(&mut output);
52		Ok((ExitSucceed::Returned, output.to_vec()))
53	}
54}
55
56#[cfg(test)]
57mod tests {
58	use super::*;
59
60	#[test]
61	fn test_empty_input() -> Result<(), PrecompileFailure> {
62		let input: [u8; 0] = [];
63		let expected = b"\
64			\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6\x62\
65			\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8\x43\x4a\
66		";
67
68		let cost: u64 = 1;
69
70		match Sha3FIPS256::execute(&input, cost) {
71			Ok((_, out)) => {
72				assert_eq!(out, expected);
73				Ok(())
74			}
75			Err(e) => {
76				panic!("Test not expected to fail: {:?}", e);
77			}
78		}
79	}
80
81	#[test]
82	fn hello_sha3_256() -> Result<(), PrecompileFailure> {
83		let input = b"hello";
84		let expected = b"\
85			\x33\x38\xbe\x69\x4f\x50\xc5\xf3\x38\x81\x49\x86\xcd\xf0\x68\x64\
86			\x53\xa8\x88\xb8\x4f\x42\x4d\x79\x2a\xf4\xb9\x20\x23\x98\xf3\x92\
87		";
88
89		let cost: u64 = 1;
90
91		match Sha3FIPS256::execute(input, cost) {
92			Ok((_, out)) => {
93				assert_eq!(out, expected);
94				Ok(())
95			}
96			Err(e) => {
97				panic!("Test not expected to fail: {:?}", e);
98			}
99		}
100	}
101
102	#[test]
103	fn long_string_sha3_256() -> Result<(), PrecompileFailure> {
104		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.";
105		let expected = b"\
106			\xbd\xe3\xf2\x69\x17\x5e\x1d\xcd\xa1\x38\x48\x27\x8a\xa6\x04\x6b\
107			\xd6\x43\xce\xa8\x5b\x84\xc8\xb8\xbb\x80\x95\x2e\x70\xb6\xea\xe0\
108		";
109
110		let cost: u64 = 1;
111
112		match Sha3FIPS256::execute(input, cost) {
113			Ok((_, out)) => {
114				assert_eq!(out, expected);
115				Ok(())
116			}
117			Err(e) => {
118				panic!("Test not expected to fail: {:?}", e);
119			}
120		}
121	}
122
123	#[test]
124	fn long_string_sha3_512() -> Result<(), PrecompileFailure> {
125		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.";
126		let expected = b"\
127			\xf3\x2a\x94\x23\x55\x13\x51\xdf\x0a\x07\xc0\xb8\xc2\x0e\xb9\x72\
128			\x36\x7c\x39\x8d\x61\x06\x60\x38\xe1\x69\x86\x44\x8e\xbf\xbc\x3d\
129			\x15\xed\xe0\xed\x36\x93\xe3\x90\x5e\x9a\x8c\x60\x1d\x9d\x00\x2a\
130			\x06\x85\x3b\x97\x97\xef\x9a\xb1\x0c\xbd\xe1\x00\x9c\x7d\x0f\x09\
131		";
132
133		let cost: u64 = 1;
134
135		match Sha3FIPS512::execute(input, cost) {
136			Ok((_, out)) => {
137				assert_eq!(out, expected);
138				Ok(())
139			}
140			Err(e) => {
141				panic!("Test not expected to fail: {:?}", e);
142			}
143		}
144	}
145}