Blake3 syscall (#17358)
This commit is contained in:
2
sdk/program/Cargo.toml
Normal file → Executable file
2
sdk/program/Cargo.toml
Normal file → Executable file
@ -11,6 +11,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.3.1"
|
||||
blake3 = "0.3.7"
|
||||
borsh = "0.9.0"
|
||||
borsh-derive = "0.9.0"
|
||||
bs58 = "0.4.0"
|
||||
@ -33,7 +34,6 @@ solana-sdk-macro = { path = "../macro", version = "=1.8.0" }
|
||||
thiserror = "1.0"
|
||||
|
||||
[target.'cfg(not(target_arch = "bpf"))'.dependencies]
|
||||
blake3 = "0.3.7"
|
||||
curve25519-dalek = "2.1.0"
|
||||
rand = "0.7.0"
|
||||
solana-logger = { path = "../../logger", version = "=1.8.0" }
|
||||
|
219
sdk/program/src/blake3.rs
Normal file
219
sdk/program/src/blake3.rs
Normal file
@ -0,0 +1,219 @@
|
||||
//! The `blake3` module provides functions for creating hashes.
|
||||
use crate::sanitize::Sanitize;
|
||||
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
|
||||
use std::{convert::TryFrom, fmt, mem, str::FromStr};
|
||||
use thiserror::Error;
|
||||
|
||||
/// Size of hash
|
||||
pub const HASH_BYTES: usize = 32;
|
||||
/// Maximum string length of a base58 encoded hash
|
||||
const MAX_BASE58_LEN: usize = 44;
|
||||
#[derive(
|
||||
Serialize,
|
||||
Deserialize,
|
||||
BorshSerialize,
|
||||
BorshDeserialize,
|
||||
BorshSchema,
|
||||
Clone,
|
||||
Copy,
|
||||
Default,
|
||||
Eq,
|
||||
PartialEq,
|
||||
Ord,
|
||||
PartialOrd,
|
||||
Hash,
|
||||
AbiExample,
|
||||
)]
|
||||
#[repr(transparent)]
|
||||
pub struct Hash(pub [u8; HASH_BYTES]);
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Hasher {
|
||||
hasher: blake3::Hasher,
|
||||
}
|
||||
|
||||
impl Hasher {
|
||||
pub fn hash(&mut self, val: &[u8]) {
|
||||
self.hasher.update(val);
|
||||
}
|
||||
pub fn hashv(&mut self, vals: &[&[u8]]) {
|
||||
for val in vals {
|
||||
self.hash(val);
|
||||
}
|
||||
}
|
||||
pub fn result(self) -> Hash {
|
||||
Hash(*self.hasher.finalize().as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl Sanitize for Hash {}
|
||||
|
||||
impl AsRef<[u8]> for Hash {
|
||||
fn as_ref(&self) -> &[u8] {
|
||||
&self.0[..]
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Hash {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", bs58::encode(self.0).into_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Hash {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", bs58::encode(self.0).into_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Error)]
|
||||
pub enum ParseHashError {
|
||||
#[error("string decoded to wrong size for hash")]
|
||||
WrongSize,
|
||||
#[error("failed to decoded string to hash")]
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl FromStr for Hash {
|
||||
type Err = ParseHashError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
if s.len() > MAX_BASE58_LEN {
|
||||
return Err(ParseHashError::WrongSize);
|
||||
}
|
||||
let bytes = bs58::decode(s)
|
||||
.into_vec()
|
||||
.map_err(|_| ParseHashError::Invalid)?;
|
||||
if bytes.len() != mem::size_of::<Hash>() {
|
||||
Err(ParseHashError::WrongSize)
|
||||
} else {
|
||||
Ok(Hash::new(&bytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash {
|
||||
pub fn new(hash_slice: &[u8]) -> Self {
|
||||
Hash(<[u8; HASH_BYTES]>::try_from(hash_slice).unwrap())
|
||||
}
|
||||
|
||||
pub const fn new_from_array(hash_array: [u8; HASH_BYTES]) -> Self {
|
||||
Self(hash_array)
|
||||
}
|
||||
|
||||
/// unique Hash for tests and benchmarks.
|
||||
pub fn new_unique() -> Self {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
static I: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
let mut b = [0u8; HASH_BYTES];
|
||||
let i = I.fetch_add(1, Ordering::Relaxed);
|
||||
b[0..8].copy_from_slice(&i.to_le_bytes());
|
||||
Self::new(&b)
|
||||
}
|
||||
|
||||
pub fn to_bytes(self) -> [u8; HASH_BYTES] {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a Blake3 hash for the given data.
|
||||
pub fn hashv(vals: &[&[u8]]) -> Hash {
|
||||
// Perform the calculation inline, calling this from within a program is
|
||||
// not supported
|
||||
#[cfg(not(target_arch = "bpf"))]
|
||||
{
|
||||
let mut hasher = Hasher::default();
|
||||
hasher.hashv(vals);
|
||||
hasher.result()
|
||||
}
|
||||
// Call via a system call to perform the calculation
|
||||
#[cfg(target_arch = "bpf")]
|
||||
{
|
||||
extern "C" {
|
||||
fn sol_blake3(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;
|
||||
}
|
||||
let mut hash_result = [0; HASH_BYTES];
|
||||
unsafe {
|
||||
sol_blake3(
|
||||
vals as *const _ as *const u8,
|
||||
vals.len() as u64,
|
||||
&mut hash_result as *mut _ as *mut u8,
|
||||
);
|
||||
}
|
||||
Hash::new_from_array(hash_result)
|
||||
}
|
||||
}
|
||||
|
||||
/// Return a Blake3 hash for the given data.
|
||||
pub fn hash(val: &[u8]) -> Hash {
|
||||
hashv(&[val])
|
||||
}
|
||||
|
||||
/// Return the hash of the given hash extended with the given value.
|
||||
pub fn extend_and_hash(id: &Hash, val: &[u8]) -> Hash {
|
||||
let mut hash_data = id.as_ref().to_vec();
|
||||
hash_data.extend_from_slice(val);
|
||||
hash(&hash_data)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_new_unique() {
|
||||
assert!(Hash::new_unique() != Hash::new_unique());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hash_fromstr() {
|
||||
let hash = hash(&[1u8]);
|
||||
|
||||
let mut hash_base58_str = bs58::encode(hash).into_string();
|
||||
|
||||
assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
|
||||
|
||||
hash_base58_str.push_str(&bs58::encode(hash.0).into_string());
|
||||
assert_eq!(
|
||||
hash_base58_str.parse::<Hash>(),
|
||||
Err(ParseHashError::WrongSize)
|
||||
);
|
||||
|
||||
hash_base58_str.truncate(hash_base58_str.len() / 2);
|
||||
assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
|
||||
|
||||
hash_base58_str.truncate(hash_base58_str.len() / 2);
|
||||
assert_eq!(
|
||||
hash_base58_str.parse::<Hash>(),
|
||||
Err(ParseHashError::WrongSize)
|
||||
);
|
||||
|
||||
let input_too_big = bs58::encode(&[0xffu8; HASH_BYTES + 1]).into_string();
|
||||
assert!(input_too_big.len() > MAX_BASE58_LEN);
|
||||
assert_eq!(
|
||||
input_too_big.parse::<Hash>(),
|
||||
Err(ParseHashError::WrongSize)
|
||||
);
|
||||
|
||||
let mut hash_base58_str = bs58::encode(hash.0).into_string();
|
||||
assert_eq!(hash_base58_str.parse::<Hash>(), Ok(hash));
|
||||
|
||||
// throw some non-base58 stuff in there
|
||||
hash_base58_str.replace_range(..1, "I");
|
||||
assert_eq!(
|
||||
hash_base58_str.parse::<Hash>(),
|
||||
Err(ParseHashError::Invalid)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extend_and_hash() {
|
||||
let val = "gHiljKpq";
|
||||
let val_hash = hash(val.as_bytes());
|
||||
let ext = "lM890t";
|
||||
let hash_ext = [&val_hash.0, ext.as_bytes()].concat();
|
||||
let ext_hash = extend_and_hash(&val_hash, ext.as_bytes());
|
||||
assert!(ext_hash == hash(&hash_ext));
|
||||
}
|
||||
}
|
1
sdk/program/src/lib.rs
Normal file → Executable file
1
sdk/program/src/lib.rs
Normal file → Executable file
@ -6,6 +6,7 @@
|
||||
extern crate self as solana_program;
|
||||
|
||||
pub mod account_info;
|
||||
pub mod blake3;
|
||||
pub mod borsh;
|
||||
pub mod bpf_loader;
|
||||
pub mod bpf_loader_deprecated;
|
||||
|
Reference in New Issue
Block a user