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