Skip to content

Commit 2147465

Browse files
committed
Migrate to thiserror v2. Error handling improvements.
1 parent d9830c3 commit 2147465

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ categories = ["cryptography"] # https://cra
1515
bc-rand = "^0.4.0"
1616
bc-crypto = "^0.9.0"
1717

18-
thiserror = "^1.0.48"
18+
thiserror = "^2.0"
1919

2020
[dev-dependencies]
2121
hex-literal = "^0.4.1"

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ pub enum Error {
2626
#[error("shares have unequal length")]
2727
SharesUnequalLength,
2828
}
29+
30+
pub type Result<T> = std::result::Result<T, Error>;

src/interpolate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
hazmat::{bitslice, bitslice_setall, gf256_add, gf256_mul, gf256_inv, unbitslice},
3-
Error, MAX_SECRET_LEN
3+
Result, MAX_SECRET_LEN
44
};
55
use bc_crypto::{memzero, memzero_vec_vec_u8};
66

@@ -103,7 +103,7 @@ pub fn interpolate<T>(
103103
yl: usize,
104104
yij: &[T],
105105
x: u8
106-
) -> Result<Vec<u8>, Error>
106+
) -> Result<Vec<u8>>
107107
where
108108
T: AsRef<[u8]>
109109
{

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ mod hazmat;
6767
mod interpolate;
6868

6969
mod error;
70-
pub use error::Error;
70+
pub use error::{Error, Result};
7171

7272
mod shamir;
7373
pub use shamir::{ recover_secret, split_secret };
@@ -95,7 +95,7 @@ mod tests {
9595
unimplemented!()
9696
}
9797

98-
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), rand::Error> {
98+
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> std::result::Result<(), rand::Error> {
9999
unimplemented!()
100100
}
101101
}

src/shamir.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
MAX_SECRET_LEN,
77
MIN_SECRET_LEN,
88
Error,
9+
Result,
910
interpolate::interpolate,
1011
};
1112

@@ -16,7 +17,7 @@ fn create_digest(random_data: &[u8], shared_secret: &[u8]) -> [u8; 32] {
1617
hmac_sha256(random_data, shared_secret)
1718
}
1819

19-
fn validate_parameters(threshold: usize, share_count: usize, secret_length: usize) -> Result<(), Error> {
20+
fn validate_parameters(threshold: usize, share_count: usize, secret_length: usize) -> Result<()> {
2021
if share_count > MAX_SHARE_COUNT {
2122
return Err(Error::TooManyShares);
2223
} else if threshold < 1 || threshold > share_count {
@@ -73,7 +74,7 @@ pub fn split_secret(
7374
share_count: usize,
7475
secret: &[u8],
7576
random_generator: &mut impl RandomNumberGenerator
76-
) -> Result<Vec<Vec<u8>>, Error> {
77+
) -> Result<Vec<Vec<u8>>> {
7778
validate_parameters(threshold, share_count, secret.len())?;
7879

7980
if threshold == 1 {
@@ -158,7 +159,7 @@ pub fn split_secret(
158159
///
159160
/// assert_eq!(secret, b"my secret belongs to me.");
160161
/// ```
161-
pub fn recover_secret<T>(indexes: &[usize], shares: &[T]) -> Result<Vec<u8>, Error>
162+
pub fn recover_secret<T>(indexes: &[usize], shares: &[T]) -> Result<Vec<u8>>
162163
where T: AsRef<[u8]>
163164
{
164165
let threshold = shares.len();

0 commit comments

Comments
 (0)