solana_blake3_hasher/
lib.rs

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