solana_program/
blake3.rs

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