solana_program/
blake3.rs

1//! Hashing with the [blake3] hash function.
2//!
3//! [blake3]: https://github.com/BLAKE3-team/BLAKE3
4
5use {
6    crate::sanitize::Sanitize,
7    borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
8    std::{convert::TryFrom, fmt, mem, str::FromStr},
9    thiserror::Error,
10};
11
12/// Size of a hash in bytes.
13pub const HASH_BYTES: usize = 32;
14/// Maximum string length of a base58 encoded hash.
15const MAX_BASE58_LEN: usize = 44;
16
17/// A blake3 hash.
18#[derive(
19    Serialize,
20    Deserialize,
21    BorshSerialize,
22    BorshDeserialize,
23    BorshSchema,
24    Clone,
25    Copy,
26    Default,
27    Eq,
28    PartialEq,
29    Ord,
30    PartialOrd,
31    Hash,
32    AbiExample,
33)]
34#[repr(transparent)]
35pub struct Hash(pub [u8; HASH_BYTES]);
36
37#[derive(Clone, Default)]
38pub struct Hasher {
39    hasher: blake3::Hasher,
40}
41
42impl Hasher {
43    pub fn hash(&mut self, val: &[u8]) {
44        self.hasher.update(val);
45    }
46    pub fn hashv(&mut self, vals: &[&[u8]]) {
47        for val in vals {
48            self.hash(val);
49        }
50    }
51    pub fn result(self) -> Hash {
52        Hash(*self.hasher.finalize().as_bytes())
53    }
54}
55
56impl Sanitize for Hash {}
57
58impl AsRef<[u8]> for Hash {
59    fn as_ref(&self) -> &[u8] {
60        &self.0[..]
61    }
62}
63
64impl fmt::Debug for Hash {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        write!(f, "{}", bs58::encode(self.0).into_string())
67    }
68}
69
70impl fmt::Display for Hash {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        write!(f, "{}", bs58::encode(self.0).into_string())
73    }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Error)]
77pub enum ParseHashError {
78    #[error("string decoded to wrong size for hash")]
79    WrongSize,
80    #[error("failed to decoded string to hash")]
81    Invalid,
82}
83
84impl FromStr for Hash {
85    type Err = ParseHashError;
86
87    fn from_str(s: &str) -> Result<Self, Self::Err> {
88        if s.len() > MAX_BASE58_LEN {
89            return Err(ParseHashError::WrongSize);
90        }
91        let bytes = bs58::decode(s)
92            .into_vec()
93            .map_err(|_| ParseHashError::Invalid)?;
94        if bytes.len() != mem::size_of::<Hash>() {
95            Err(ParseHashError::WrongSize)
96        } else {
97            Ok(Hash::new(&bytes))
98        }
99    }
100}
101
102impl Hash {
103    pub fn new(hash_slice: &[u8]) -> Self {
104        Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
105    }
106
107    pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
108        Self(hash_array)
109    }
110
111    /// unique Hash for tests and benchmarks.
112    pub fn new_unique() -> Self {
113        use crate::atomic_u64::AtomicU64;
114        static I: AtomicU64 = AtomicU64::new(1);
115
116        let mut b = [0u8; HASH_BYTES];
117        let i = I.fetch_add(1);
118        b[0..8].copy_from_slice(&i.to_le_bytes());
119        Self::new(&b)
120    }
121
122    pub fn to_bytes(self) -> [u8; HASH_BYTES] {
123        self.0
124    }
125}
126
127/// Return a Blake3 hash for the given data.
128pub fn hashv(vals: &[&[u8]]) -> Hash {
129    // Perform the calculation inline, calling this from within a program is
130    // not supported
131    #[cfg(not(target_os = "solana"))]
132    {
133        let mut hasher = Hasher::default();
134        hasher.hashv(vals);
135        hasher.result()
136    }
137    // Call via a system call to perform the calculation
138    #[cfg(target_os = "solana")]
139    {
140        let mut hash_result = [0; HASH_BYTES];
141        unsafe {
142            crate::syscalls::sol_blake3(
143                vals as *const _ as *const u8,
144                vals.len() as u64,
145                &mut hash_result as *mut _ as *mut u8,
146            );
147        }
148        Hash::new_from_array(hash_result)
149    }
150}
151
152/// Return a Blake3 hash for the given data.
153pub fn hash(val: &[u8]) -> Hash {
154    hashv(&[val])
155}
156
157/// Return the hash of the given hash extended with the given value.
158pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
159    let mut hash_data = id.as_ref().to_vec();
160    hash_data.extend_from_slice(val);
161    hash(&hash_data)
162}
163
164#[cfg(test)]
165mod tests {
166    use super::*;
167
168    #[test]
169    fn test_new_unique() {
170        assert!(Hash::new_unique() != Hash::new_unique());
171    }
172
173    #[test]
174    fn test_hash_fromstr() {
175        let hash = hash(&[1u8]);
176
177        let mut hash_base58_str = bs58::encode(hash).into_string();
178
179        assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
180
181        hash_base58_str.push_str(&bs58::encode(hash.0).into_string());
182        assert_eq!(
183            hash_base58_str.parse::<Hash>(),
184            Err(ParseHashError::WrongSize)
185        );
186
187        hash_base58_str.truncate(hash_base58_str.len() / 2);
188        assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
189
190        hash_base58_str.truncate(hash_base58_str.len() / 2);
191        assert_eq!(
192            hash_base58_str.parse::<Hash>(),
193            Err(ParseHashError::WrongSize)
194        );
195
196        let input_too_big = bs58::encode(&[0xffu8; HASH_BYTES + 1]).into_string();
197        assert!(input_too_big.len() > MAX_BASE58_LEN);
198        assert_eq!(
199            input_too_big.parse::<Hash>(),
200            Err(ParseHashError::WrongSize)
201        );
202
203        let mut hash_base58_str = bs58::encode(hash.0).into_string();
204        assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
205
206        // throw some non-base58 stuff in there
207        hash_base58_str.replace_range(..1, "I");
208        assert_eq!(
209            hash_base58_str.parse::<Hash>(),
210            Err(ParseHashError::Invalid)
211        );
212    }
213
214    #[test]
215    fn test_extend_and_hash() {
216        let val = "gHiljKpq";
217        let val_hash = hash(val.as_bytes());
218        let ext = "lM890t";
219        let hash_ext = [&val_hash.0, ext.as_bytes()].concat();
220        let ext_hash = extend_and_hash(&val_hash, ext.as_bytes());
221        assert!(ext_hash == hash(&hash_ext));
222    }
223}