Skip to content

Commit 2456cc7

Browse files
committed
fix: make hashers panicing less
1 parent 80159c5 commit 2456cc7

File tree

6 files changed

+24
-24
lines changed

6 files changed

+24
-24
lines changed

storage-proofs/benches/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn encode_single_node<H: Hasher>(
5454

5555
let node_data = H::Domain::try_from_bytes(&data[start..end]).unwrap();
5656
let key_data = H::Domain::try_from_bytes(&key).unwrap();
57-
let encoded = H::sloth_encode(&key_data, &node_data);
57+
let encoded = H::sloth_encode(&key_data, &node_data).unwrap();
5858
encoded.write_bytes(&mut data[start..end]).unwrap();
5959
}
6060

storage-proofs/src/drgporep.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ where
459459
let end = start + NODE_SIZE;
460460

461461
let node_data = H::Domain::try_from_bytes(&data[start..end])?;
462-
let encoded = H::sloth_encode(key.as_ref(), &node_data);
462+
let encoded = H::sloth_encode(key.as_ref(), &node_data)?;
463463

464464
encoded.write_bytes(&mut data[start..end])?;
465465
}

storage-proofs/src/hasher/blake2s.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ impl Hasher for Blake2sHasher {
3838
Ok(<Self::Function as HashFunction<Self::Domain>>::hash(data))
3939
}
4040

41-
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
41+
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
4242
// TODO: validate this is how sloth should work in this case
4343
let k = (*key).into();
4444
let c = (*ciphertext).into();
4545

46-
sloth::encode::<Bls12>(&k, &c).into()
46+
Ok(sloth::encode::<Bls12>(&k, &c).into())
4747
}
4848

49-
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
49+
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
5050
// TODO: validate this is how sloth should work in this case
51-
sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into()
51+
Ok(sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into())
5252
}
5353
}
5454

@@ -247,8 +247,8 @@ impl HashFunction<Blake2sDomain> for Blake2sFunction {
247247
Some(_) => {
248248
let bits = alloc_bits
249249
.iter()
250-
.map(|v| v.get_value().unwrap())
251-
.collect::<Vec<bool>>();
250+
.map(|v| v.get_value().ok_or(SynthesisError::AssignmentMissing))
251+
.collect::<std::result::Result<Vec<bool>, SynthesisError>>()?;
252252
// TODO: figure out if we can avoid this
253253
let frs = multipack::compute_multipacking::<E>(&bits);
254254
Ok(frs[0])

storage-proofs/src/hasher/pedersen.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ impl Hasher for PedersenHasher {
3232
}
3333

3434
#[inline]
35-
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
35+
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
3636
// Unrapping here is safe; `Fr` elements and hash domain elements are the same byte length.
37-
let key = Fr::from_repr(key.0).unwrap();
38-
let ciphertext = Fr::from_repr(ciphertext.0).unwrap();
39-
sloth::encode::<Bls12>(&key, &ciphertext).into()
37+
let key = Fr::from_repr(key.0)?;
38+
let ciphertext = Fr::from_repr(ciphertext.0)?;
39+
Ok(sloth::encode::<Bls12>(&key, &ciphertext).into())
4040
}
4141

4242
#[inline]
43-
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
43+
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
4444
// Unrapping here is safe; `Fr` elements and hash domain elements are the same byte length.
45-
let key = Fr::from_repr(key.0).unwrap();
46-
let ciphertext = Fr::from_repr(ciphertext.0).unwrap();
45+
let key = Fr::from_repr(key.0)?;
46+
let ciphertext = Fr::from_repr(ciphertext.0)?;
4747

48-
sloth::decode::<Bls12>(&key, &ciphertext).into()
48+
Ok(sloth::decode::<Bls12>(&key, &ciphertext).into())
4949
}
5050
}
5151

storage-proofs/src/hasher/sha256.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ impl Hasher for Sha256Hasher {
3030
Ok(<Self::Function as HashFunction<Self::Domain>>::hash(data))
3131
}
3232

33-
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
33+
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
3434
// TODO: validate this is how sloth should work in this case
3535
let k = (*key).into();
3636
let c = (*ciphertext).into();
3737

38-
sloth::encode::<Bls12>(&k, &c).into()
38+
Ok(sloth::encode::<Bls12>(&k, &c).into())
3939
}
4040

41-
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain {
41+
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain> {
4242
// TODO: validate this is how sloth should work in this case
43-
sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into()
43+
Ok(sloth::decode::<Bls12>(&(*key).into(), &(*ciphertext).into()).into())
4444
}
4545
}
4646

@@ -223,8 +223,8 @@ impl HashFunction<Sha256Domain> for Sha256Function {
223223
let fr = if alloc_bits[0].get_value().is_some() {
224224
let be_bits = alloc_bits
225225
.iter()
226-
.map(|v| v.get_value().unwrap())
227-
.collect::<Vec<bool>>();
226+
.map(|v| v.get_value().ok_or(SynthesisError::AssignmentMissing))
227+
.collect::<std::result::Result<Vec<bool>, SynthesisError>>()?;
228228

229229
let le_bits = be_bits
230230
.chunks(8)

storage-proofs/src/hasher/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ pub trait Hasher: Clone + ::std::fmt::Debug + Eq + Default + Send + Sync {
7474
type Function: HashFunction<Self::Domain>;
7575

7676
fn create_label(data: &[u8], m: usize) -> Result<Self::Domain>;
77-
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain;
78-
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Self::Domain;
77+
fn sloth_encode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain>;
78+
fn sloth_decode(key: &Self::Domain, ciphertext: &Self::Domain) -> Result<Self::Domain>;
7979

8080
fn name() -> String;
8181
}

0 commit comments

Comments
 (0)