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