light-hasher 6.0.0

Trait for generic usage of hash functions on Solana
Documentation
//! # light-hasher
//!
//! Trait for generic hash function usage on Solana.
//!
//! | Type | Description |
//! |------|-------------|
//! | [`Hasher`] | Trait with `hash`, `hashv`, and `zero_bytes` |
//! | [`Poseidon`] | Poseidon hash over BN254 |
//! | [`Keccak`] | Keccak-256 hash |
//! | [`Sha256`] | SHA-256 hash |
//! | [`DataHasher`] | Trait to hash structured data |
//! | [`HasherError`] | Error type for hash operations |
//! | [`hash_chain`] | Sequential hash chaining |
//! | [`hash_to_field_size`] | Truncate hash output to BN254 field size |
//! | [`zero_bytes`] | Precomputed zero-leaf hashes per hasher |

#![allow(unexpected_cfgs)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;

#[cfg(feature = "alloc")]
#[cfg(not(feature = "std"))]
pub use alloc::{string::String, vec, vec::Vec};
#[cfg(feature = "std")]
pub use std::{string::String, vec, vec::Vec};

pub mod bigint;
mod data_hasher;
pub mod errors;
pub mod hash_chain;
pub mod hash_to_field_size;
pub mod keccak;
pub mod poseidon;
pub mod sha256;
pub mod syscalls;
pub mod to_byte_array;
pub mod zero_bytes;
pub mod zero_indexed_leaf;

pub use data_hasher::DataHasher;
pub use keccak::Keccak;
pub use poseidon::Poseidon;
pub use sha256::Sha256;

pub use crate::errors::HasherError;
use crate::zero_bytes::ZeroBytes;

pub const HASH_BYTES: usize = 32;

pub type Hash = [u8; HASH_BYTES];

pub trait Hasher {
    const ID: u8;
    fn hash(val: &[u8]) -> Result<Hash, HasherError>;
    fn hashv(vals: &[&[u8]]) -> Result<Hash, HasherError>;
    fn zero_bytes() -> ZeroBytes;
    fn zero_indexed_leaf() -> [u8; 32];
}