Skip to main content

light_hasher/
lib.rs

1//! # light-hasher
2//!
3//! Trait for generic hash function usage on Solana.
4//!
5//! | Type | Description |
6//! |------|-------------|
7//! | [`Hasher`] | Trait with `hash`, `hashv`, and `zero_bytes` |
8//! | [`Poseidon`] | Poseidon hash over BN254 |
9//! | [`Keccak`] | Keccak-256 hash |
10//! | [`Sha256`] | SHA-256 hash |
11//! | [`DataHasher`] | Trait to hash structured data |
12//! | [`HasherError`] | Error type for hash operations |
13//! | [`hash_chain`] | Sequential hash chaining |
14//! | [`hash_to_field_size`] | Truncate hash output to BN254 field size |
15//! | [`zero_bytes`] | Precomputed zero-leaf hashes per hasher |
16
17#![allow(unexpected_cfgs)]
18#![cfg_attr(not(feature = "std"), no_std)]
19
20#[cfg(all(feature = "alloc", not(feature = "std")))]
21extern crate alloc;
22
23#[cfg(feature = "alloc")]
24#[cfg(not(feature = "std"))]
25pub use alloc::{string::String, vec, vec::Vec};
26#[cfg(feature = "std")]
27pub use std::{string::String, vec, vec::Vec};
28
29pub mod bigint;
30mod data_hasher;
31pub mod errors;
32pub mod hash_chain;
33pub mod hash_to_field_size;
34pub mod keccak;
35pub mod poseidon;
36pub mod sha256;
37pub mod syscalls;
38pub mod to_byte_array;
39pub mod zero_bytes;
40pub mod zero_indexed_leaf;
41
42pub use data_hasher::DataHasher;
43pub use keccak::Keccak;
44pub use poseidon::Poseidon;
45pub use sha256::Sha256;
46
47pub use crate::errors::HasherError;
48use crate::zero_bytes::ZeroBytes;
49
50pub const HASH_BYTES: usize = 32;
51
52pub type Hash = [u8; HASH_BYTES];
53
54pub trait Hasher {
55    const ID: u8;
56    fn hash(val: &[u8]) -> Result<Hash, HasherError>;
57    fn hashv(vals: &[&[u8]]) -> Result<Hash, HasherError>;
58    fn zero_bytes() -> ZeroBytes;
59    fn zero_indexed_leaf() -> [u8; 32];
60}