cita_hashable/
lib.rs

1// Copyright Rivtower Technologies LLC.
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
15use cita_types::H256;
16
17/// The hash of the empty bytes string.
18#[cfg(feature = "sha3hash")]
19pub const HASH_EMPTY: H256 = H256([
20    0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0,
21    0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70,
22]);
23#[cfg(feature = "blake2bhash")]
24pub const HASH_EMPTY: H256 = H256([
25    0xd6, 0x7f, 0x72, 0x9f, 0x8d, 0x19, 0xed, 0x2e, 0x92, 0xf8, 0x17, 0xcf, 0x5c, 0x31, 0xc7, 0x81,
26    0x2d, 0xd3, 0x9e, 0xd3, 0x5b, 0x0b, 0x1a, 0xae, 0x41, 0xc7, 0x66, 0x5f, 0x46, 0xc3, 0x6b, 0x9f,
27]);
28#[cfg(feature = "sm3hash")]
29pub const HASH_EMPTY: H256 = H256([
30    0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f, 0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f,
31    0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74, 0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b,
32]);
33
34/// The hash of the RLP encoding of empty data.
35#[cfg(feature = "sha3hash")]
36pub const HASH_NULL_RLP: H256 = H256([
37    0x56, 0xe8, 0x1f, 0x17, 0x1b, 0xcc, 0x55, 0xa6, 0xff, 0x83, 0x45, 0xe6, 0x92, 0xc0, 0xf8, 0x6e,
38    0x5b, 0x48, 0xe0, 0x1b, 0x99, 0x6c, 0xad, 0xc0, 0x01, 0x62, 0x2f, 0xb5, 0xe3, 0x63, 0xb4, 0x21,
39]);
40#[cfg(feature = "blake2bhash")]
41pub const HASH_NULL_RLP: H256 = H256([
42    0xc1, 0x4a, 0xf5, 0x91, 0x07, 0xef, 0x14, 0x00, 0x3e, 0x46, 0x97, 0xa4, 0x0e, 0xa9, 0x12, 0xd8,
43    0x65, 0xeb, 0x14, 0x63, 0x08, 0x6a, 0x46, 0x49, 0x97, 0x7c, 0x13, 0xea, 0x69, 0xb0, 0xd9, 0xaf,
44]);
45#[cfg(feature = "sm3hash")]
46pub const HASH_NULL_RLP: H256 = H256([
47    0x99, 0x5b, 0x94, 0x98, 0x69, 0xf8, 0x0f, 0xa1, 0x46, 0x5a, 0x9d, 0x8b, 0x6f, 0xa7, 0x59, 0xec,
48    0x65, 0xc3, 0x02, 0x0d, 0x59, 0xc2, 0x62, 0x46, 0x62, 0xbd, 0xff, 0x05, 0x9b, 0xdf, 0x19, 0xb3,
49]);
50
51/// The hash of the RLP encoding of empty list.
52#[cfg(feature = "sha3hash")]
53pub const HASH_EMPTY_LIST_RLP: H256 = H256([
54    0x1d, 0xcc, 0x4d, 0xe8, 0xde, 0xc7, 0x5d, 0x7a, 0xab, 0x85, 0xb5, 0x67, 0xb6, 0xcc, 0xd4, 0x1a,
55    0xd3, 0x12, 0x45, 0x1b, 0x94, 0x8a, 0x74, 0x13, 0xf0, 0xa1, 0x42, 0xfd, 0x40, 0xd4, 0x93, 0x47,
56]);
57#[cfg(feature = "blake2bhash")]
58pub const HASH_EMPTY_LIST_RLP: H256 = H256([
59    0x7b, 0x7b, 0x72, 0xfb, 0x1e, 0x60, 0xea, 0x75, 0x69, 0x9e, 0x30, 0x3f, 0xbc, 0x97, 0xc5, 0xce,
60    0xb5, 0x78, 0xba, 0x92, 0x43, 0x2a, 0x64, 0xe2, 0x18, 0xc9, 0xab, 0xbc, 0xd9, 0x72, 0xa5, 0x83,
61]);
62#[cfg(feature = "sm3hash")]
63pub const HASH_EMPTY_LIST_RLP: H256 = H256([
64    0x47, 0x44, 0x68, 0x32, 0xc4, 0x4e, 0x75, 0x55, 0x27, 0x02, 0x2e, 0x3e, 0x57, 0x21, 0x33, 0x92,
65    0x2b, 0x49, 0x76, 0x8d, 0x46, 0x0f, 0xb1, 0x74, 0x12, 0xc1, 0xb6, 0xc8, 0xfa, 0x64, 0xed, 0x48,
66]);
67
68#[cfg(feature = "blake2bhash")]
69pub const BLAKE2BKEY: &str = "CryptapeCryptape";
70
71pub trait Hashable {
72    /// Calculate crypt HASH of this object.
73    fn crypt_hash(&self) -> H256 {
74        let mut result = [0u8; 32];
75        self.crypt_hash_into(&mut result);
76        H256(result)
77    }
78
79    /// Calculate crypt HASH of this object and place result into dest.
80    fn crypt_hash_into(&self, dest: &mut [u8]);
81}
82
83#[cfg(feature = "sha3hash")]
84impl<T> Hashable for T
85where
86    T: AsRef<[u8]>,
87{
88    fn crypt_hash_into(&self, dest: &mut [u8]) {
89        use tiny_keccak::Hasher;
90
91        let input: &[u8] = self.as_ref();
92        let mut hasher = tiny_keccak::Keccak::v256();
93        hasher.update(input);
94        hasher.finalize(dest);
95    }
96}
97
98#[cfg(feature = "blake2bhash")]
99impl<T> Hashable for T
100where
101    T: AsRef<[u8]>,
102{
103    fn crypt_hash_into(&self, dest: &mut [u8]) {
104        let input: &[u8] = self.as_ref();
105
106        let mut params = blake2b_simd::Params::new();
107        params.key(BLAKE2BKEY.as_bytes());
108        params.hash_length(32);
109        let hash = params.hash(input);
110        dest.copy_from_slice(hash.as_bytes());
111    }
112}
113
114#[cfg(feature = "sm3hash")]
115impl<T> Hashable for T
116where
117    T: AsRef<[u8]>,
118{
119    fn crypt_hash_into(&self, dest: &mut [u8]) {
120        let input: &[u8] = self.as_ref();
121        dest.copy_from_slice(libsm::sm3::hash::Sm3Hash::new(input).get_hash().as_ref());
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    #[cfg(feature = "sha3hash")]
131    fn sha3_empty() {
132        assert_eq!([0u8; 0].crypt_hash(), HASH_EMPTY);
133    }
134    #[test]
135    #[cfg(feature = "sha3hash")]
136    fn sha3_as() {
137        use std::str::FromStr;
138
139        assert_eq!(
140            [0x41u8; 32].crypt_hash(),
141            H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8")
142                .unwrap()
143        );
144    }
145
146    #[test]
147    #[cfg(feature = "blake2bhash")]
148    fn blake2b_empty() {
149        assert_eq!([0u8; 0].crypt_hash(), HASH_EMPTY);
150    }
151    #[test]
152    #[cfg(feature = "blake2bhash")]
153    fn blake2b_as() {
154        use std::str::FromStr;
155
156        assert_eq!(
157            [0x41u8; 32].crypt_hash(),
158            H256::from_str("8a786e4840b7b5ad9b0cfa44539b886086c2e1050bb802c8e40ecf09b3a64a11")
159                .unwrap()
160        );
161    }
162
163    #[test]
164    #[cfg(feature = "sm3hash")]
165    fn test_sm3() {
166        let hash = [0u8; 0].crypt_hash();
167        assert_eq!(hash, HASH_EMPTY);
168
169        let hash = [0x80; 1].crypt_hash();
170        assert_eq!(hash, HASH_NULL_RLP);
171
172        let hash = [0xC0; 1].crypt_hash();
173        assert_eq!(hash, HASH_EMPTY_LIST_RLP);
174    }
175}