light_hasher/
errors.rs

1#[cfg(feature = "poseidon")]
2use light_poseidon::PoseidonError;
3use thiserror::Error;
4
5use crate::poseidon::PoseidonSyscallError;
6
7#[derive(Debug, Error, PartialEq)]
8pub enum HasherError {
9    #[error("Integer overflow, value too large")]
10    IntegerOverflow,
11    #[cfg(feature = "poseidon")]
12    #[error("Poseidon hasher error: {0}")]
13    Poseidon(#[from] PoseidonError),
14    #[error("Poseidon syscall error: {0}")]
15    PoseidonSyscall(#[from] PoseidonSyscallError),
16    #[error("Unknown Solana syscall error: {0}")]
17    UnknownSolanaSyscall(u64),
18    #[error("Allowed input length {0} provided {1}")]
19    InvalidInputLength(usize, usize),
20    #[error("Invalid number of fields")]
21    InvalidNumFields,
22    #[error("Empty input")]
23    EmptyInput,
24    #[error("Borsh serialization failed.")]
25    BorshError,
26    #[error(
27        "Option hash to field size returned [0u8;32], a collision with None for an Option type."
28    )]
29    OptionHashToFieldSizeZero,
30    #[error("Poseidon feature is not enabled. Without feature poseidon only syscalls are accessible in target os solana")]
31    PoseidonFeatureNotEnabled,
32    #[error("SHA256 feature is not enabled. Enable the sha256 feature to use SHA256 hashing in non-Solana targets")]
33    Sha256FeatureNotEnabled,
34    #[error("Keccak feature is not enabled. Enable the keccak feature to use Keccak hashing in non-Solana targets")]
35    KeccakFeatureNotEnabled,
36}
37
38// NOTE(vadorovsky): Unfortunately, we need to do it by hand. `num_derive::ToPrimitive`
39// doesn't support data-carrying enums.
40impl From<HasherError> for u32 {
41    fn from(e: HasherError) -> u32 {
42        match e {
43            HasherError::IntegerOverflow => 7001,
44            #[cfg(feature = "poseidon")]
45            HasherError::Poseidon(_) => 7002,
46            HasherError::PoseidonSyscall(e) => (u64::from(e)).try_into().unwrap_or(7003),
47            HasherError::UnknownSolanaSyscall(e) => e.try_into().unwrap_or(7004),
48            HasherError::InvalidInputLength(_, _) => 7005,
49            HasherError::InvalidNumFields => 7006,
50            HasherError::EmptyInput => 7007,
51            HasherError::BorshError => 7008,
52            HasherError::OptionHashToFieldSizeZero => 7009,
53            HasherError::PoseidonFeatureNotEnabled => 7010,
54            HasherError::Sha256FeatureNotEnabled => 7011,
55            HasherError::KeccakFeatureNotEnabled => 7012,
56        }
57    }
58}
59
60#[cfg(feature = "solana")]
61impl From<HasherError> for solana_program_error::ProgramError {
62    fn from(e: HasherError) -> Self {
63        solana_program_error::ProgramError::Custom(e.into())
64    }
65}
66
67#[cfg(feature = "pinocchio")]
68impl From<HasherError> for pinocchio::program_error::ProgramError {
69    fn from(e: HasherError) -> Self {
70        pinocchio::program_error::ProgramError::Custom(e.into())
71    }
72}