#![cfg_attr(not(feature = "std"), no_std)]
#![deny(unsafe_code)]
#![deny(unused_qualifications)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(all(feature = "alloc", feature = "std"))]
use alloc::string::ToString;
mod constants;
#[cfg(feature = "alloc")]
mod params;
#[cfg(feature = "alloc")]
mod permutation;
#[cfg(feature = "alloc")]
mod sponge;
pub use constants::sbox;
#[cfg(feature = "alloc")]
pub use constants::{
mds_matrix_5x5,
mds_matrix_7x7,
};
#[cfg(feature = "alloc")]
pub use constants::{
round_constants_128,
round_constants_256,
};
#[cfg(feature = "alloc")]
pub use params::{
Poseidon128,
Poseidon256,
PoseidonField,
PoseidonParams,
};
#[cfg(feature = "alloc")]
pub use permutation::{
PoseidonPermutation,
PoseidonState,
};
#[cfg(feature = "alloc")]
pub use sponge::{
Poseidon,
PoseidonSponge,
PoseidonSpongeSqueeze,
};
#[cfg(feature = "wasm")]
pub mod wasm;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PoseidonError {
InputTooLarge { max: usize, actual: usize },
#[cfg(feature = "alloc")]
InvalidParams { reason: String },
#[cfg(feature = "alloc")]
InternalError { reason: String },
}
impl core::fmt::Display for PoseidonError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
PoseidonError::InputTooLarge { max, actual } => {
write!(f, "Input size {} exceeds maximum {}", actual, max)
}
#[cfg(feature = "alloc")]
PoseidonError::InvalidParams { reason } => {
write!(f, "Invalid Poseidon parameters: {}", reason)
}
#[cfg(feature = "alloc")]
PoseidonError::InternalError { reason } => {
write!(f, "Internal Poseidon error: {}", reason)
}
}
}
}
#[cfg(all(feature = "alloc", feature = "std"))]
impl From<PoseidonError> for lib_q_core::Error {
fn from(err: PoseidonError) -> Self {
lib_q_core::Error::InternalError {
operation: "Poseidon hash".into(),
details: err.to_string(),
}
}
}
#[cfg(all(not(feature = "alloc"), feature = "std"))]
impl From<PoseidonError> for lib_q_core::Error {
fn from(err: PoseidonError) -> Self {
match err {
PoseidonError::InputTooLarge { .. } => lib_q_core::Error::InternalError {
operation: "Poseidon hash",
details: "input too large",
},
}
}
}