1#![cfg_attr(not(feature = "std"), no_std)]
34#![deny(unsafe_code)]
35#![deny(unused_qualifications)]
36
37#[cfg(feature = "alloc")]
38extern crate alloc;
39
40#[cfg(feature = "alloc")]
41use alloc::string::{
42 String,
43 ToString,
44};
45
46mod constants;
47#[cfg(feature = "alloc")]
48mod params;
49#[cfg(feature = "alloc")]
50mod permutation;
51#[cfg(feature = "alloc")]
52mod sponge;
53
54pub use constants::sbox;
56#[cfg(feature = "alloc")]
57pub use constants::{
58 mds_matrix_5x5,
59 mds_matrix_7x7,
60};
61#[cfg(feature = "alloc")]
62pub use constants::{
63 round_constants_128,
64 round_constants_256,
65};
66#[cfg(feature = "alloc")]
67pub use params::{
68 Poseidon128,
69 Poseidon256,
70 PoseidonField,
71 PoseidonParams,
72};
73#[cfg(feature = "alloc")]
74pub use permutation::{
75 PoseidonPermutation,
76 PoseidonState,
77};
78#[cfg(feature = "alloc")]
79pub use sponge::{
80 Poseidon,
81 PoseidonSponge,
82 PoseidonSpongeSqueeze,
83};
84
85#[cfg(feature = "wasm")]
86mod wasm;
87
88#[derive(Debug, Clone, PartialEq, Eq)]
90pub enum PoseidonError {
91 InputTooLarge { max: usize, actual: usize },
93 #[cfg(feature = "alloc")]
95 InvalidParams { reason: String },
96 #[cfg(feature = "alloc")]
98 InternalError { reason: String },
99}
100
101impl core::fmt::Display for PoseidonError {
102 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
103 match self {
104 PoseidonError::InputTooLarge { max, actual } => {
105 write!(f, "Input size {} exceeds maximum {}", actual, max)
106 }
107 #[cfg(feature = "alloc")]
108 PoseidonError::InvalidParams { reason } => {
109 write!(f, "Invalid Poseidon parameters: {}", reason)
110 }
111 #[cfg(feature = "alloc")]
112 PoseidonError::InternalError { reason } => {
113 write!(f, "Internal Poseidon error: {}", reason)
114 }
115 }
116 }
117}
118
119#[cfg(feature = "alloc")]
120impl From<PoseidonError> for lib_q_core::Error {
121 fn from(err: PoseidonError) -> Self {
122 lib_q_core::Error::InternalError {
123 operation: "Poseidon hash".into(),
124 details: err.to_string(),
125 }
126 }
127}
128
129#[cfg(not(feature = "alloc"))]
130impl From<PoseidonError> for lib_q_core::Error {
131 fn from(err: PoseidonError) -> Self {
132 match err {
133 PoseidonError::InputTooLarge { .. } => lib_q_core::Error::InternalError {
134 operation: "Poseidon hash",
135 details: "input too large",
136 },
137 }
138 }
139}