stack_test_progpow_cpu/keccak.rs
1// Copyright 2015-2019 Parity Technologies (UK) Ltd.
2// This file is part of Parity Ethereum.
3
4// Parity Ethereum is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Parity Ethereum is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
16
17extern crate keccak_hash as hash;
18
19pub type H256 = [u8; 32];
20
21pub mod keccak_512 {
22 use super::hash;
23
24 pub use self::hash::keccak_512_unchecked as unchecked;
25
26 pub fn write(input: &[u8], output: &mut [u8]) {
27 hash::keccak_512(input, output);
28 }
29
30 pub fn inplace(input: &mut [u8]) {
31 // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This
32 // means that we can reuse the input buffer for both input and output.
33 unsafe {
34 hash::keccak_512_unchecked(
35 input.as_mut_ptr(),
36 input.len(),
37 input.as_ptr(),
38 input.len(),
39 );
40 }
41 }
42}
43
44pub mod keccak_256 {
45 use super::hash;
46
47 pub use self::hash::keccak_256_unchecked as unchecked;
48
49 #[allow(dead_code)]
50 pub fn write(input: &[u8], output: &mut [u8]) {
51 hash::keccak_256(input, output);
52 }
53
54 pub fn inplace(input: &mut [u8]) {
55 // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This
56 // means that we can reuse the input buffer for both input and output.
57 unsafe {
58 hash::keccak_256_unchecked(
59 input.as_mut_ptr(),
60 input.len(),
61 input.as_ptr(),
62 input.len(),
63 );
64 }
65 }
66}