Skip to content

Commit

Permalink
remove expect to keep old behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Jun 12, 2024
1 parent d368614 commit e8283bc
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 63 deletions.
7 changes: 4 additions & 3 deletions perf/src/sigverify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,9 +1315,10 @@ mod tests {
for _ in 0..1_000_000 {
thread_rng().fill(&mut input);
let ans = check_packed_ge_small_order(&input);
let ref_ge = CompressedEdwardsY::from_slice(&input)
.expect("Input slice should have a length of 32");
if let Some(ref_element) = ref_ge.decompress() {
let compressed_edwards_y = CompressedEdwardsY::from_slice(&input);
assert!(compressed_edwards_y.is_ok());
let compressed_edwards_y = compressed_edwards_y.unwrap();
if let Some(ref_element) = compressed_edwards_y.decompress() {
if ref_element.is_small_order() {
assert!(!ans);
} else {
Expand Down
13 changes: 3 additions & 10 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,8 @@ pub fn bytes_are_curve_point<T: AsRef<[u8]>>(_bytes: T) -> bool {
#[cfg(not(target_os = "solana"))]
{
curve25519_dalek::edwards::CompressedEdwardsY::from_slice(_bytes.as_ref())
.expect("Input slice should have a length of 32")
.decompress()
.is_some()
.map(|compressed_edwards_y| compressed_edwards_y.decompress().is_some())
.unwrap_or(false)
}
#[cfg(target_os = "solana")]
unimplemented!();
Expand Down Expand Up @@ -946,13 +945,7 @@ mod tests {
if let Ok(program_address) =
Pubkey::create_program_address(&[&bytes1, &bytes2], &program_id)
{
let is_on_curve = curve25519_dalek::edwards::CompressedEdwardsY::from_slice(
&program_address.to_bytes(),
)
.expect("Input slice should have a length of 32")
.decompress()
.is_some();
assert!(!is_on_curve);
assert!(!program_address.is_on_curve());
assert!(!addresses.contains(&program_address));
addresses.push(program_address);
}
Expand Down
20 changes: 9 additions & 11 deletions zk-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,11 @@ impl TryFrom<&[u8]> for ElGamalPubkey {
return Err(ElGamalError::PubkeyDeserialization);
}

Ok(ElGamalPubkey(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)?,
))
CompressedRistretto::from_slice(bytes)
.map_err(|_| ElGamalError::PubkeyDeserialization)?
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)
.map(ElGamalPubkey)
}
}

Expand Down Expand Up @@ -720,11 +719,10 @@ impl DecryptHandle {
return None;
}

Some(DecryptHandle(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()?,
))
CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(DecryptHandle)
}
}

Expand Down
9 changes: 4 additions & 5 deletions zk-sdk/src/encryption/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,10 @@ impl PedersenCommitment {
return None;
}

Some(PedersenCommitment(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()?,
))
CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(PedersenCommitment)
}
}

Expand Down
6 changes: 3 additions & 3 deletions zk-sdk/src/sigma_proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ fn ristretto_point_from_optional_slice(
) -> Result<CompressedRistretto, SigmaProofVerificationError> {
optional_slice
.and_then(|slice| (slice.len() == RISTRETTO_POINT_LEN).then_some(slice))
.map(|slice| {
CompressedRistretto::from_slice(slice).expect("Input slice should have a length of 32")
})
.map(CompressedRistretto::from_slice)
.transpose()
.map_err(|_| SigmaProofVerificationError::Deserialization)?
.ok_or(SigmaProofVerificationError::Deserialization)
}

Expand Down
7 changes: 3 additions & 4 deletions zk-token-sdk/src/curve25519/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod target_arch {

fn try_from(pod: &PodEdwardsPoint) -> Result<Self, Self::Error> {
CompressedEdwardsY::from_slice(&pod.0)
.expect("Input slice should have a length of 32")
.map_err(|_| Curve25519Error::PodConversion)?
.decompress()
.ok_or(Curve25519Error::PodConversion)
}
Expand All @@ -75,9 +75,8 @@ mod target_arch {

fn validate_point(&self) -> bool {
CompressedEdwardsY::from_slice(&self.0)
.expect("Input slice should have a length of 32")
.decompress()
.is_some()
.map(|compressed_edwards_y| compressed_edwards_y.decompress().is_some())
.unwrap_or(false)
}
}

Expand Down
6 changes: 3 additions & 3 deletions zk-token-sdk/src/curve25519/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod target_arch {

fn try_from(pod: &PodRistrettoPoint) -> Result<Self, Self::Error> {
CompressedRistretto::from_slice(&pod.0)
.expect("Input slice should have a length of 32")
.map_err(|_| Curve25519Error::PodConversion)?
.decompress()
.ok_or(Curve25519Error::PodConversion)
}
Expand All @@ -75,8 +75,8 @@ mod target_arch {

fn validate_point(&self) -> bool {
CompressedRistretto::from_slice(&self.0)
.expect("Input slice should have a length of 32")
.decompress()
.ok()
.and_then(|compressed_ristretto| compressed_ristretto.decompress())
.is_some()
}
}
Expand Down
29 changes: 13 additions & 16 deletions zk-token-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,10 @@ impl ElGamalPubkey {
return None;
}

Some(ElGamalPubkey(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()?,
))
CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(ElGamalPubkey)
}

/// Encrypts an amount under the public key.
Expand Down Expand Up @@ -442,12 +441,11 @@ impl TryFrom<&[u8]> for ElGamalPubkey {
return Err(ElGamalError::PubkeyDeserialization);
}

Ok(ElGamalPubkey(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)?,
))
CompressedRistretto::from_slice(bytes)
.map_err(|_| ElGamalError::PubkeyDeserialization)?
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)
.map(ElGamalPubkey)
}
}

Expand Down Expand Up @@ -804,11 +802,10 @@ impl DecryptHandle {
return None;
}

Some(DecryptHandle(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()?,
))
CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(DecryptHandle)
}
}

Expand Down
9 changes: 4 additions & 5 deletions zk-token-sdk/src/encryption/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ impl PedersenCommitment {
return None;
}

Some(PedersenCommitment(
CompressedRistretto::from_slice(bytes)
.expect("Input slice should have a length of 32")
.decompress()?,
))
CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(PedersenCommitment)
}
}

Expand Down
6 changes: 3 additions & 3 deletions zk-token-sdk/src/sigma_proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ fn ristretto_point_from_optional_slice(
) -> Result<CompressedRistretto, SigmaProofVerificationError> {
optional_slice
.and_then(|slice| (slice.len() == RISTRETTO_POINT_LEN).then_some(slice))
.map(|slice| {
CompressedRistretto::from_slice(slice).expect("Input slice should have a length of 32")
})
.map(CompressedRistretto::from_slice)
.transpose()
.map_err(|_| SigmaProofVerificationError::Deserialization)?
.ok_or(SigmaProofVerificationError::Deserialization)
}

Expand Down

0 comments on commit e8283bc

Please sign in to comment.