mod crypto;
#[cfg(test)]
mod tests;
use core::{cmp, convert, fmt};
use internals::slice::SliceExt;
use crate::{incomplete_block_len, sha256d, HashEngine as _};
#[cfg(doc)]
use crate::{sha256t, sha256t_tag};
crate::internal_macros::general_hash_type! {
256,
false,
"Output of the SHA256 hash function."
}
const BLOCK_SIZE: usize = 64;
#[derive(Debug, Clone)]
pub struct HashEngine {
buffer: [u8; BLOCK_SIZE],
h: [u32; 8],
bytes_hashed: u64,
}
impl HashEngine {
pub const fn new() -> Self {
Self {
h: [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab,
0x5be0cd19,
],
bytes_hashed: 0,
buffer: [0; BLOCK_SIZE],
}
}
pub fn from_midstate(midstate: Midstate) -> Self {
let mut ret = [0; 8];
for (ret_val, midstate_bytes) in ret.iter_mut().zip(midstate.as_ref().bitcoin_as_chunks().0)
{
*ret_val = u32::from_be_bytes(*midstate_bytes);
}
Self { buffer: [0; BLOCK_SIZE], h: ret, bytes_hashed: midstate.bytes_hashed }
}
pub const fn can_extract_midstate(&self) -> bool { self.bytes_hashed % 64 == 0 }
pub fn midstate(&self) -> Result<Midstate, MidstateError> {
if !self.can_extract_midstate() {
let unprocessed_len = (self.bytes_hashed % BLOCK_SIZE as u64) as usize;
let aligned_bytes = self.bytes_hashed - unprocessed_len as u64;
let mut midstate_bytes = [0; 32];
for (val, ret_bytes) in self.h.iter().zip(midstate_bytes.bitcoin_as_chunks_mut::<4>().0)
{
*ret_bytes = val.to_be_bytes();
}
return Err(MidstateError {
invalid_n_bytes_hashed: self.bytes_hashed,
block_aligned_midstate: Midstate {
bytes: midstate_bytes,
bytes_hashed: aligned_bytes,
},
unprocessed_bytes: self.buffer,
unprocessed_bytes_len: unprocessed_len,
});
}
Ok(self.midstate_unchecked())
}
#[cfg(not(hashes_fuzz))]
fn midstate_unchecked(&self) -> Midstate {
let mut ret = [0; 32];
for (val, ret_bytes) in self.h.iter().zip(ret.bitcoin_as_chunks_mut::<4>().0) {
*ret_bytes = val.to_be_bytes();
}
Midstate { bytes: ret, bytes_hashed: self.bytes_hashed }
}
#[cfg(hashes_fuzz)]
fn midstate_unchecked(&self) -> Midstate {
let mut ret = [0; 32];
ret.copy_from_slice(&self.buffer[..32]);
Midstate { bytes: ret, bytes_hashed: self.bytes_hashed }
}
}
impl Default for HashEngine {
fn default() -> Self { Self::new() }
}
impl crate::HashEngine for HashEngine {
type Hash = Hash;
type Bytes = [u8; 32];
const BLOCK_SIZE: usize = 64;
fn n_bytes_hashed(&self) -> u64 { self.bytes_hashed }
crate::internal_macros::engine_input_impl!();
fn finalize(self) -> Self::Hash { Hash::from_engine(self) }
}
impl Hash {
#[cfg(not(hashes_fuzz))]
pub fn from_engine(mut e: HashEngine) -> Self {
let n_bytes_hashed = e.bytes_hashed;
let zeroes = [0; BLOCK_SIZE - 8];
e.input(&[0x80]);
if incomplete_block_len(&e) > zeroes.len() {
e.input(&zeroes);
}
let pad_length = zeroes.len() - incomplete_block_len(&e);
e.input(&zeroes[..pad_length]);
debug_assert_eq!(incomplete_block_len(&e), zeroes.len());
e.input(&(8 * n_bytes_hashed).to_be_bytes());
debug_assert_eq!(incomplete_block_len(&e), 0);
Self(e.midstate_unchecked().bytes)
}
#[cfg(hashes_fuzz)]
pub fn from_engine(e: HashEngine) -> Self {
let mut hash = e.midstate_unchecked().bytes;
if hash == [0; 32] {
hash[0] = 1;
}
Hash(hash)
}
#[must_use]
pub fn hash_again(&self) -> sha256d::Hash { sha256d::Hash::from_byte_array(hash(&self.0).0) }
pub const fn hash_unoptimized(bytes: &[u8]) -> Self {
Self(Midstate::compute_midstate_unoptimized(bytes, true).bytes)
}
}
#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Midstate {
bytes: [u8; 32],
bytes_hashed: u64,
}
impl Midstate {
#[track_caller]
pub const fn new(state: [u8; 32], bytes_hashed: u64) -> Self {
if bytes_hashed % 64 != 0 {
panic!("bytes hashed is not a multiple of 64");
}
Self { bytes: state, bytes_hashed }
}
pub const fn as_parts(&self) -> (&[u8; 32], u64) { (&self.bytes, self.bytes_hashed) }
pub const fn to_parts(self) -> ([u8; 32], u64) { (self.bytes, self.bytes_hashed) }
#[must_use]
pub const fn hash_tag(tag: &[u8]) -> Self {
let hash = Hash::hash_unoptimized(tag);
let mut buf = [0u8; 64];
let mut i = 0usize;
while i < buf.len() {
buf[i] = hash.0[i % hash.0.len()];
i += 1;
}
Self::compute_midstate_unoptimized(&buf, false)
}
}
impl fmt::Debug for Midstate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
struct Encoder<'a> {
bytes: &'a [u8; 32],
}
impl fmt::Debug for Encoder<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { crate::debug_hex(self.bytes, f) }
}
f.debug_struct("Midstate")
.field("bytes", &Encoder { bytes: &self.bytes })
.field("length", &self.bytes_hashed)
.finish()
}
}
impl convert::AsRef<[u8]> for Midstate {
fn as_ref(&self) -> &[u8] { &self.bytes }
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MidstateError {
invalid_n_bytes_hashed: u64,
block_aligned_midstate: Midstate,
unprocessed_bytes: [u8; BLOCK_SIZE],
unprocessed_bytes_len: usize,
}
impl MidstateError {
pub const fn midstate(&self) -> &Midstate { &self.block_aligned_midstate }
pub fn unprocessed_bytes(&self) -> &[u8] {
&self.unprocessed_bytes[..self.unprocessed_bytes_len]
}
}
impl fmt::Display for MidstateError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"invalid number of bytes hashed {} (should have been a multiple of 64)",
self.invalid_n_bytes_hashed
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for MidstateError {}