ckb_hash/
lib.rs

1//! CKB default hash function.
2//!
3//! CKB uses [blake2b] with following configurations as the default hash function.
4//!
5//! * output digest size: 32
6//! * personalization: ckb-default-hash
7//!
8//! [blake2b]: https://blake2.net/blake2.pdf
9
10#![no_std]
11
12#[cfg(feature = "ckb-contract")]
13pub use blake2b_ref::{Blake2b, Blake2bBuilder};
14
15#[cfg(all(not(feature = "ckb-contract"), target_family = "wasm"))]
16pub use blake2b_ref::{Blake2b, Blake2bBuilder};
17
18#[cfg(all(not(feature = "ckb-contract"), not(target_family = "wasm")))]
19pub use blake2b_rs::{Blake2b, Blake2bBuilder};
20
21#[doc(hidden)]
22pub const BLAKE2B_KEY: &[u8] = &[];
23/// Output digest size.
24pub const BLAKE2B_LEN: usize = 32;
25/// Blake2b personalization.
26pub const CKB_HASH_PERSONALIZATION: &[u8] = b"ckb-default-hash";
27/// The hash output on empty input.
28///
29/// ## Examples
30///
31/// ```
32/// use ckb_hash::{BLANK_HASH, blake2b_256};
33///
34/// assert_eq!(BLANK_HASH, blake2b_256(&b""));
35/// ```
36pub const BLANK_HASH: [u8; 32] = [
37    68, 244, 198, 151, 68, 213, 248, 197, 93, 100, 32, 98, 148, 157, 202, 228, 155, 196, 231, 239,
38    67, 211, 136, 197, 161, 47, 66, 181, 99, 61, 22, 62,
39];
40
41/// Creates a new hasher.
42///
43/// This can be used to hash inputs incrementally.
44///
45/// ## Examples
46///
47/// ```
48/// use ckb_hash::new_blake2b;
49///
50/// let mut hasher = new_blake2b();
51/// hasher.update(&b"left|"[..]);
52/// hasher.update(&b"right"[..]);
53/// let mut result = [0u8; 32];
54/// hasher.finalize(&mut result); // Saves hash result
55/// ```
56pub fn new_blake2b() -> Blake2b {
57    Blake2bBuilder::new(32)
58        .personal(CKB_HASH_PERSONALIZATION)
59        .build()
60}
61
62/// Hashes the slice of binary and returns the digest.
63///
64/// ## Examples
65///
66/// ```
67/// use ckb_hash::blake2b_256;
68///
69/// let input = b"ckb";
70/// let digest = blake2b_256(&input);
71/// println!("ckbhash({:?}) = {:?}", input, digest);
72/// ```
73pub fn blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
74    if s.as_ref().is_empty() {
75        return BLANK_HASH;
76    }
77    inner_blake2b_256(s)
78}
79
80fn inner_blake2b_256<T: AsRef<[u8]>>(s: T) -> [u8; 32] {
81    let mut result = [0u8; 32];
82    let mut blake2b = new_blake2b();
83    blake2b.update(s.as_ref());
84    blake2b.finalize(&mut result);
85    result
86}
87
88#[test]
89fn empty_blake2b() {
90    let actual = inner_blake2b_256([]);
91    assert_eq!(actual, BLANK_HASH);
92}