ink_env/
hash.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//! Provides type definitions and traits for the built-in cryptographic hashes.
16
17/// The output type of built-in cryptographic hash functions.
18pub trait HashOutput: private::Sealed {
19    /// The output type of the crypto hash.
20    ///
21    /// This should be a byte array with some constant size such as `[u8; 32]`.
22    type Type: Default;
23}
24
25/// Types that are usable as built-in cryptographic hashes.
26pub trait CryptoHash: HashOutput + private::Sealed {
27    /// Hashes the given raw byte input and copies the result into `output`.
28    fn hash(input: &[u8], output: &mut <Self as HashOutput>::Type);
29}
30
31/// The SHA-2 crypto hash with 256-bit output.
32#[derive(Debug, Copy, Clone, PartialEq, Eq)]
33pub enum Sha2x256 {}
34
35/// The KECCAK crypto hash with 256-bit output.
36#[derive(Debug, Copy, Clone, PartialEq, Eq)]
37pub enum Keccak256 {}
38
39/// The BLAKE-2 crypto hash with 256-bit output.
40#[derive(Debug, Copy, Clone, PartialEq, Eq)]
41pub enum Blake2x256 {}
42
43/// The BLAKE-2 crypto hash with 128-bit output.
44#[derive(Debug, Copy, Clone, PartialEq, Eq)]
45pub enum Blake2x128 {}
46
47mod private {
48    /// Seals the implementation of `CryptoHash` and `HashOutput`.
49    pub trait Sealed {}
50}
51
52impl private::Sealed for Sha2x256 {}
53impl private::Sealed for Keccak256 {}
54impl private::Sealed for Blake2x256 {}
55impl private::Sealed for Blake2x128 {}
56
57impl HashOutput for Sha2x256 {
58    type Type = [u8; 32];
59}
60
61impl HashOutput for Keccak256 {
62    type Type = [u8; 32];
63}
64
65impl HashOutput for Blake2x256 {
66    type Type = [u8; 32];
67}
68
69impl HashOutput for Blake2x128 {
70    type Type = [u8; 16];
71}