Skip to content

Commit

Permalink
refactor: use thiserror to derive errors and update error messages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter authored Nov 21, 2024
1 parent 50dd6bd commit a27f9ad
Show file tree
Hide file tree
Showing 22 changed files with 392 additions and 347 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## 0.11.0 (2024-10-30)
## 0.13.0 (TBD)

- [BREAKING] Updated Winterfell dependency to v0.10 (#338).
- Fixed a bug in the implementation of `draw_integers` for `RpoRandomCoin` (#343).
- [BREAKING] Refactor error messages and use `thiserror` to derive errors (#344).

## 0.12.0 (2024-10-30)

- [BREAKING] Updated Winterfell dependency to v0.10 (#338).

## 0.11.0 (2024-10-17)

Expand Down
36 changes: 32 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ rand_core = { version = "0.6", default-features = false }
rand-utils = { version = "0.10", package = "winter-rand-utils", optional = true }
serde = { version = "1.0", default-features = false, optional = true, features = ["derive"] }
sha3 = { version = "0.10", default-features = false }
thiserror = { version = "2.0", default-features = false }
winter-crypto = { version = "0.10", default-features = false }
winter-math = { version = "0.10", default-features = false }
winter-utils = { version = "0.10", default-features = false }

[dev-dependencies]
assert_matches = { version = "1.5.0", default-features = false }
criterion = { version = "0.5", features = ["html_reports"] }
getrandom = { version = "0.2", features = ["js"] }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
Expand Down
85 changes: 63 additions & 22 deletions src/hash/rescue/rpo/digest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use alloc::string::String;
use core::{cmp::Ordering, fmt::Display, ops::Deref, slice};

use thiserror::Error;

use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::{
rand::Randomizable,
Expand Down Expand Up @@ -127,9 +129,12 @@ impl Randomizable for RpoDigest {
// CONVERSIONS: FROM RPO DIGEST
// ================================================================================================

#[derive(Copy, Clone, Debug)]
#[derive(Debug, Error)]
pub enum RpoDigestError {
InvalidInteger,
#[error("failed to convert digest field element to {0}")]
TypeConversion(&'static str),
#[error("failed to convert to field element: {0}")]
InvalidFieldElement(String),
}

impl TryFrom<&RpoDigest> for [bool; DIGEST_SIZE] {
Expand All @@ -153,10 +158,10 @@ impl TryFrom<RpoDigest> for [bool; DIGEST_SIZE] {
}

Ok([
to_bool(value.0[0].as_int()).ok_or(RpoDigestError::InvalidInteger)?,
to_bool(value.0[1].as_int()).ok_or(RpoDigestError::InvalidInteger)?,
to_bool(value.0[2].as_int()).ok_or(RpoDigestError::InvalidInteger)?,
to_bool(value.0[3].as_int()).ok_or(RpoDigestError::InvalidInteger)?,
to_bool(value.0[0].as_int()).ok_or(RpoDigestError::TypeConversion("bool"))?,
to_bool(value.0[1].as_int()).ok_or(RpoDigestError::TypeConversion("bool"))?,
to_bool(value.0[2].as_int()).ok_or(RpoDigestError::TypeConversion("bool"))?,
to_bool(value.0[3].as_int()).ok_or(RpoDigestError::TypeConversion("bool"))?,
])
}
}
Expand All @@ -174,10 +179,22 @@ impl TryFrom<RpoDigest> for [u8; DIGEST_SIZE] {

fn try_from(value: RpoDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u8"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u8"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u8"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u8"))?,
])
}
}
Expand All @@ -195,10 +212,22 @@ impl TryFrom<RpoDigest> for [u16; DIGEST_SIZE] {

fn try_from(value: RpoDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u16"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u16"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u16"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u16"))?,
])
}
}
Expand All @@ -216,10 +245,22 @@ impl TryFrom<RpoDigest> for [u32; DIGEST_SIZE] {

fn try_from(value: RpoDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u32"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u32"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u32"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpoDigestError::TypeConversion("u32"))?,
])
}
}
Expand Down Expand Up @@ -343,10 +384,10 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpoDigest {

fn try_from(value: [u64; DIGEST_SIZE]) -> Result<Self, RpoDigestError> {
Ok(Self([
value[0].try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value[1].try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value[2].try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value[3].try_into().map_err(|_| RpoDigestError::InvalidInteger)?,
value[0].try_into().map_err(RpoDigestError::InvalidFieldElement)?,
value[1].try_into().map_err(RpoDigestError::InvalidFieldElement)?,
value[2].try_into().map_err(RpoDigestError::InvalidFieldElement)?,
value[3].try_into().map_err(RpoDigestError::InvalidFieldElement)?,
]))
}
}
Expand Down
85 changes: 63 additions & 22 deletions src/hash/rescue/rpx/digest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use alloc::string::String;
use core::{cmp::Ordering, fmt::Display, ops::Deref, slice};

use thiserror::Error;

use super::{Digest, Felt, StarkField, DIGEST_BYTES, DIGEST_SIZE, ZERO};
use crate::{
rand::Randomizable,
Expand Down Expand Up @@ -127,9 +129,12 @@ impl Randomizable for RpxDigest {
// CONVERSIONS: FROM RPX DIGEST
// ================================================================================================

#[derive(Copy, Clone, Debug)]
#[derive(Debug, Error)]
pub enum RpxDigestError {
InvalidInteger,
#[error("failed to convert digest field element to {0}")]
TypeConversion(&'static str),
#[error("failed to convert to field element: {0}")]
InvalidFieldElement(String),
}

impl TryFrom<&RpxDigest> for [bool; DIGEST_SIZE] {
Expand All @@ -153,10 +158,10 @@ impl TryFrom<RpxDigest> for [bool; DIGEST_SIZE] {
}

Ok([
to_bool(value.0[0].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
to_bool(value.0[1].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
to_bool(value.0[2].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
to_bool(value.0[3].as_int()).ok_or(RpxDigestError::InvalidInteger)?,
to_bool(value.0[0].as_int()).ok_or(RpxDigestError::TypeConversion("bool"))?,
to_bool(value.0[1].as_int()).ok_or(RpxDigestError::TypeConversion("bool"))?,
to_bool(value.0[2].as_int()).ok_or(RpxDigestError::TypeConversion("bool"))?,
to_bool(value.0[3].as_int()).ok_or(RpxDigestError::TypeConversion("bool"))?,
])
}
}
Expand All @@ -174,10 +179,22 @@ impl TryFrom<RpxDigest> for [u8; DIGEST_SIZE] {

fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u8"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u8"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u8"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u8"))?,
])
}
}
Expand All @@ -195,10 +212,22 @@ impl TryFrom<RpxDigest> for [u16; DIGEST_SIZE] {

fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u16"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u16"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u16"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u16"))?,
])
}
}
Expand All @@ -216,10 +245,22 @@ impl TryFrom<RpxDigest> for [u32; DIGEST_SIZE] {

fn try_from(value: RpxDigest) -> Result<Self, Self::Error> {
Ok([
value.0[0].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[1].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[2].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[3].as_int().try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value.0[0]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u32"))?,
value.0[1]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u32"))?,
value.0[2]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u32"))?,
value.0[3]
.as_int()
.try_into()
.map_err(|_| RpxDigestError::TypeConversion("u32"))?,
])
}
}
Expand Down Expand Up @@ -343,10 +384,10 @@ impl TryFrom<[u64; DIGEST_SIZE]> for RpxDigest {

fn try_from(value: [u64; DIGEST_SIZE]) -> Result<Self, RpxDigestError> {
Ok(Self([
value[0].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value[1].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value[2].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value[3].try_into().map_err(|_| RpxDigestError::InvalidInteger)?,
value[0].try_into().map_err(RpxDigestError::InvalidFieldElement)?,
value[1].try_into().map_err(RpxDigestError::InvalidFieldElement)?,
value[2].try_into().map_err(RpxDigestError::InvalidFieldElement)?,
value[3].try_into().map_err(RpxDigestError::InvalidFieldElement)?,
]))
}
}
Expand Down
Loading

0 comments on commit a27f9ad

Please sign in to comment.