corevm-engine 0.1.28

CoreVM engine that drives program execution either on the builder or CoreVM service side
Documentation
use codec::{Encode, Output};
use jam_types::Hash;

pub fn hash_encoded<E: Encode>(x: E) -> Hash {
	let mut hasher = Blake2bHasher::new();
	x.encode_to(&mut hasher);
	hasher.into_hash()
}

pub fn hash_raw(data: &[u8]) -> Hash {
	let mut hasher = Blake2bHasher::new();
	hasher.update(data);
	hasher.into_hash()
}

struct Blake2bHasher {
	state: blake2b_simd::State,
}

impl Blake2bHasher {
	fn new() -> Self {
		let state = blake2b_simd::Params::new().hash_length(32).to_state();
		Self { state }
	}

	fn update(&mut self, data: &[u8]) {
		self.state.update(data);
	}

	fn into_hash(self) -> Hash {
		self.state.finalize().as_bytes().try_into().expect("Hash length set to 32")
	}
}

impl Output for Blake2bHasher {
	fn write(&mut self, bytes: &[u8]) {
		self.update(bytes);
	}
}