ink_ir/ir/sha3.rs
1// Copyright (C) Use Ink (UK) Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/// Computes the KECCAK 256-bit hash for the given input and stores it in output.
16pub fn keccak_256(input: &[u8], output: &mut [u8; 32]) {
17 use sha3::{
18 digest::generic_array::GenericArray,
19 Digest as _,
20 };
21 let mut hasher = sha3::Keccak256::new();
22 hasher.update(input);
23 hasher.finalize_into(<&mut GenericArray<u8, _>>::from(&mut output[..]));
24}
25
26// /// Computes the BLAKE2b-256 bit hash of a string or byte string literal.
27// ///
28// /// # Note
29// ///
30// /// This is mainly used for analysis and codegen of the `blake2x256!` macro.
31// #[derive(Debug)]
32// pub struct Blake2x256Macro {
33// hash: [u8; 32],
34// input: syn::Lit,
35// }
36
37// impl Blake2x256Macro {
38// /// Returns the underlying selector.
39// pub fn hash(&self) -> [u8; 32] {
40// self.hash
41// }
42//
43// /// Returns the literal input of the BLAKE-2b hash.
44// pub fn input(&self) -> &syn::Lit {
45// &self.input
46// }
47// }
48//
49// impl TryFrom<TokenStream2> for Blake2x256Macro {
50// type Error = syn::Error;
51//
52// fn try_from(input: TokenStream2) -> Result<Self, Self::Error> {
53// let input_span = input.span();
54// let lit = syn::parse2::<syn::Lit>(input).map_err(|error| {
55// format_err!(
56// input_span,
57// "expected string or byte string literal as input: {}",
58// error
59// )
60// })?;
61// let input_bytes = match lit {
62// syn::Lit::Str(ref lit_str) => lit_str.value().into_bytes(),
63// syn::Lit::ByteStr(ref byte_str) => byte_str.value(),
64// invalid => {
65// return Err(format_err!(
66// invalid.span(),
67// "expected string or byte string literal as input. found {:?}",
68// invalid,
69// ))
70// }
71// };
72// let mut output = [0u8; 32];
73// blake2b_256(&input_bytes, &mut output);
74// Ok(Self {
75// hash: output,
76// input: lit,
77// })
78// }
79// }