solana_program/
keccak.rs

1//! Hashing with the [keccak] (SHA-3) hash function.
2//!
3//! [keccak]: https://keccak.team/keccak.html
4
5#[cfg(feature = "borsh")]
6use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
7use {
8    sha3::{Digest, Keccak256},
9    solana_sanitize::Sanitize,
10    std::{convert::TryFrom, fmt, mem, str::FromStr},
11    thiserror::Error,
12};
13
14pub const HASH_BYTES: usize = 32;
15/// Maximum string length of a base58 encoded hash
16const MAX_BASE58_LEN: usize = 44;
17#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
18#[cfg_attr(
19    feature = "borsh",
20    derive(BorshSerialize, BorshDeserialize, BorshSchema),
21    borsh(crate = "borsh")
22)]
23#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
24#[repr(transparent)]
25pub struct Hash(pub [u8; HASH_BYTES]);
26
27#[derive(Clone, Default)]
28pub struct Hasher {
29    hasher: Keccak256,
30}
31
32impl Hasher {
33    pub fn hash(&mut self, val: &[u8]) {
34        self.hasher.update(val);
35    }
36    pub fn hashv(&mut self, vals: &[&[u8]]) {
37        for val in vals {
38            self.hash(val);
39        }
40    }
41    pub fn result(self) -> Hash {
42        Hash(self.hasher.finalize().into())
43    }
44}
45
46impl Sanitize for Hash {}
47
48impl AsRef<[u8]> for Hash {
49    fn as_ref(&self) -> &[u8] {
50        &self.0[..]
51    }
52}
53
54impl fmt::Debug for Hash {
55    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56        write!(f, "{}", bs58::encode(self.0).into_string())
57    }
58}
59
60impl fmt::Display for Hash {
61    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
62        write!(f, "{}", bs58::encode(self.0).into_string())
63    }
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, Error)]
67pub enum ParseHashError {
68    #[error("string decoded to wrong size for hash")]
69    WrongSize,
70    #[error("failed to decoded string to hash")]
71    Invalid,
72}
73
74impl FromStr for Hash {
75    type Err = ParseHashError;
76
77    fn from_str(s: &str) -> Result<Self, Self::Err> {
78        if s.len() > MAX_BASE58_LEN {
79            return Err(ParseHashError::WrongSize);
80        }
81        let bytes = bs58::decode(s)
82            .into_vec()
83            .map_err(|_| ParseHashError::Invalid)?;
84        if bytes.len() != mem::size_of::<Hash>() {
85            Err(ParseHashError::WrongSize)
86        } else {
87            Ok(Hash::new(&bytes))
88        }
89    }
90}
91
92impl Hash {
93    pub fn new(hash_slice: &[u8]) -> Self {
94        Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
95    }
96
97    pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
98        Self(hash_array)
99    }
100
101    /// unique Hash for tests and benchmarks.
102    pub fn new_unique() -> Self {
103        use solana_atomic_u64::AtomicU64;
104        static I: AtomicU64 = AtomicU64::new(1);
105
106        let mut b = [0u8; HASH_BYTES];
107        let i = I.fetch_add(1);
108        b[0..8].copy_from_slice(&i.to_le_bytes());
109        Self::new(&b)
110    }
111
112    pub fn to_bytes(self) -> [u8; HASH_BYTES] {
113        self.0
114    }
115}
116
117/// Return a Keccak256 hash for the given data.
118pub fn hashv(vals: &[&[u8]]) -> Hash {
119    // Perform the calculation inline, calling this from within a program is
120    // not supported
121    #[cfg(not(target_os = "solana"))]
122    {
123        let mut hasher = Hasher::default();
124        hasher.hashv(vals);
125        hasher.result()
126    }
127    // Call via a system call to perform the calculation
128    #[cfg(target_os = "solana")]
129    {
130        let mut hash_result = [0; HASH_BYTES];
131        unsafe {
132            crate::syscalls::sol_keccak256(
133                vals as *const _ as *const u8,
134                vals.len() as u64,
135                &mut hash_result as *mut _ as *mut u8,
136            );
137        }
138        Hash::new_from_array(hash_result)
139    }
140}
141
142/// Return a Keccak256 hash for the given data.
143pub fn hash(val: &[u8]) -> Hash {
144    hashv(&[val])
145}
146
147/// Return the hash of the given hash extended with the given value.
148pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
149    let mut hash_data = id.as_ref().to_vec();
150    hash_data.extend_from_slice(val);
151    hash(&hash_data)
152}