1use std::str::FromStr;
2
3use thiserror::Error;
4
5pub use crate::math::bfield_codec::BFieldCodecError;
6pub use crate::math::bfield_codec::PolynomialBFieldCodecError;
7use crate::prelude::BFieldElement;
8use crate::prelude::Digest;
9use crate::prelude::x_field_element::EXTENSION_DEGREE;
10pub use crate::util_types::merkle_tree::MerkleTreeError;
11
12pub(crate) const USIZE_TO_U64_ERR: &str =
13 "internal error: type `usize` should have at most 64 bits";
14pub(crate) const U32_TO_USIZE_ERR: &str =
15 "internal error: type `usize` should have at least 32 bits";
16
17#[derive(Debug, Clone, Eq, PartialEq, Error)]
18#[non_exhaustive]
19pub enum ParseBFieldElementError {
20 #[error("invalid `i128`")]
21 ParseIntError(#[source] <i128 as FromStr>::Err),
22
23 #[error("{0} must be in canonical (open) interval (-{p}, {p})", p = BFieldElement::P - 1)]
24 NotCanonical(i128),
25
26 #[error(
27 "incorrect number of bytes: {0} != {bytes} == `BFieldElement::BYTES`",
28 bytes = BFieldElement::BYTES
29 )]
30 InvalidNumBytes(usize),
31}
32
33#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Error)]
34#[non_exhaustive]
35pub enum TryFromU32sError {
36 #[error("U32s<N>: `N` not big enough to hold the value")]
37 InsufficientSize,
38}
39
40#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Error)]
41#[non_exhaustive]
42pub enum TryFromXFieldElementError {
43 #[error("expected {EXTENSION_DEGREE} elements for extension field element, but got {0}")]
44 InvalidLength(usize),
45
46 #[error("Digest is not an XFieldElement")]
47 InvalidDigest,
48}
49
50#[derive(Debug, Clone, Eq, PartialEq, Error)]
51#[non_exhaustive]
52pub enum TryFromDigestError {
53 #[error("expected {len} elements for digest, but got {0}", len = Digest::LEN)]
54 InvalidLength(usize),
55
56 #[error("invalid `BFieldElement`")]
57 InvalidBFieldElement(#[from] ParseBFieldElementError),
58
59 #[error("overflow converting to Digest")]
60 Overflow,
61}
62
63#[derive(Debug, Clone, PartialEq, Error)]
64#[non_exhaustive]
65pub enum TryFromHexDigestError {
66 #[error("hex decoding error")]
67 HexDecode(#[from] hex::FromHexError),
68
69 #[error("digest error")]
70 Digest(#[from] TryFromDigestError),
71}