Skip to content

Commit df2dcef

Browse files
committed
Tighten bytes_to_elems error handling; note GF(2^16) bound
1 parent fd70f21 commit df2dcef

1 file changed

Lines changed: 10 additions & 15 deletions

File tree

fastcrypto-tbls/src/threshold_schnorr/reed_solomon.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ impl ErasureCoder {
144144
/// # Errors
145145
/// Returns [`FastCryptoError::InvalidInput`] if `k == 0`, `n <= k` or `n > 65536`.
146146
pub fn new(n: usize, k: usize) -> FastCryptoResult<Self> {
147+
// The code is defined over GF(2^16), which has 2^16 = 65536 elements; n cannot exceed
148+
// that or the evaluation points would collide.
147149
if k == 0 || n <= k || n > 65536 {
148150
return Err(InvalidInput);
149151
}
@@ -173,10 +175,8 @@ impl ErasureCoder {
173175
let mut shards: Vec<Vec<[u8; 2]>> = data
174176
.chunks_exact(bytes_per_shard)
175177
.map(bytes_to_elems)
176-
.collect_vec();
177-
self.0
178-
.encode(&mut shards)
179-
.expect("Inputs are well-formed (non-empty data, equal-sized non-empty shards, exact total_shard_count)");
178+
.collect::<FastCryptoResult<_>>()?;
179+
self.0.encode(&mut shards).map_err(|_| InvalidInput)?;
180180
Ok(shards
181181
.into_iter()
182182
.map(|s| Shard(s.into_iter().flatten().collect()))
@@ -201,15 +201,7 @@ impl ErasureCoder {
201201

202202
let mut shards: Vec<Option<Vec<[u8; 2]>>> = shards
203203
.into_iter()
204-
.map(|s| {
205-
s.map(|s| {
206-
if s.0.len() % 2 != 0 {
207-
return Err(InvalidInput);
208-
}
209-
Ok(bytes_to_elems(&s.0))
210-
})
211-
.transpose()
212-
})
204+
.map(|s| s.map(|s| bytes_to_elems(&s.0)).transpose())
213205
.collect::<FastCryptoResult<_>>()?;
214206
self.0.reconstruct(&mut shards).map_err(|_| InvalidInput)?;
215207
let shards = shards
@@ -236,8 +228,11 @@ impl ErasureCoder {
236228
}
237229
}
238230

239-
fn bytes_to_elems(bytes: &[u8]) -> Vec<[u8; 2]> {
240-
bytes.chunks_exact(2).map(|p| [p[0], p[1]]).collect()
231+
fn bytes_to_elems(bytes: &[u8]) -> FastCryptoResult<Vec<[u8; 2]>> {
232+
if !bytes.len().is_multiple_of(2) {
233+
return Err(InvalidInput);
234+
}
235+
Ok(bytes.chunks_exact(2).map(|p| [p[0], p[1]]).collect())
241236
}
242237

243238
#[cfg(test)]

0 commit comments

Comments
 (0)