Skip to content

Commit 487ebd7

Browse files
committed
Simplify decode: drop redundant guard, merge length/zero check, untangle shadowed binding
1 parent af49629 commit 487ebd7

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

fastcrypto-tbls/src/threshold_schnorr/reed_solomon.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,7 @@ impl ErasureCoder {
183183
.map(bytes_to_elements)
184184
.collect::<FastCryptoResult<_>>()?;
185185
self.0.encode(&mut shards).map_err(|_| InvalidInput)?;
186-
Ok(shards
187-
.into_iter()
188-
.map(|s| Shard(s.into_iter().flatten().collect()))
189-
.collect_vec())
186+
Ok(shards.into_iter().map(|s| Shard(s.concat())).collect_vec())
190187
}
191188

192189
/// Reconstruct the original data from `n` (possibly missing) shards, returning the first
@@ -201,13 +198,12 @@ impl ErasureCoder {
201198
return Err(InputLengthWrong(self.0.total_shard_count()));
202199
}
203200

204-
if shards.iter().filter(|s| s.is_none()).count() > self.0.parity_shard_count() {
205-
return Err(InvalidInput);
206-
}
207-
208201
let mut shards: Vec<Option<Vec<Element>>> = shards
209202
.into_iter()
210-
.map(|s| s.map(|s| bytes_to_elements(&s.0)).transpose())
203+
.map(|opt| {
204+
opt.map(|Shard(bytes)| bytes_to_elements(&bytes))
205+
.transpose()
206+
})
211207
.collect::<FastCryptoResult<_>>()?;
212208
self.0.reconstruct(&mut shards).map_err(|_| InvalidInput)?;
213209
let shards = shards
@@ -226,12 +222,9 @@ impl ErasureCoder {
226222
.flatten()
227223
.flatten()
228224
.collect();
229-
if data.len() < expected_len {
230-
return Err(InvalidInput);
231-
}
232225
// The bytes past `expected_len` are zero-padding inserted by `encode`; reject anything
233226
// that doesn't match.
234-
if data[expected_len..].iter().any(|&b| b != 0) {
227+
if data.len() < expected_len || data[expected_len..].iter().any(|&b| b != 0) {
235228
return Err(InvalidInput);
236229
}
237230
data.truncate(expected_len);

0 commit comments

Comments
 (0)