diff --git a/perf/src/sigverify.rs b/perf/src/sigverify.rs index 3c434b898a65f4..d6755d846634bb 100644 --- a/perf/src/sigverify.rs +++ b/perf/src/sigverify.rs @@ -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 { diff --git a/sdk/program/src/pubkey.rs b/sdk/program/src/pubkey.rs index e6763ddbde2640..52b8f9bff23f14 100644 --- a/sdk/program/src/pubkey.rs +++ b/sdk/program/src/pubkey.rs @@ -170,9 +170,8 @@ pub fn bytes_are_curve_point>(_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!(); @@ -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); } diff --git a/zk-sdk/src/encryption/elgamal.rs b/zk-sdk/src/encryption/elgamal.rs index 6a84e3d6fae030..96569d41d7f90b 100644 --- a/zk-sdk/src/encryption/elgamal.rs +++ b/zk-sdk/src/encryption/elgamal.rs @@ -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) } } @@ -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) } } diff --git a/zk-sdk/src/encryption/pedersen.rs b/zk-sdk/src/encryption/pedersen.rs index 92fd5b0bad7a33..318cb0915d17e3 100644 --- a/zk-sdk/src/encryption/pedersen.rs +++ b/zk-sdk/src/encryption/pedersen.rs @@ -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) } } diff --git a/zk-sdk/src/sigma_proofs/mod.rs b/zk-sdk/src/sigma_proofs/mod.rs index 3947fa57a43d3e..9ee91ddb7dd0b0 100644 --- a/zk-sdk/src/sigma_proofs/mod.rs +++ b/zk-sdk/src/sigma_proofs/mod.rs @@ -68,9 +68,9 @@ fn ristretto_point_from_optional_slice( ) -> Result { 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) } diff --git a/zk-token-sdk/src/curve25519/edwards.rs b/zk-token-sdk/src/curve25519/edwards.rs index dff37d5529d7af..90ed4249e32451 100644 --- a/zk-token-sdk/src/curve25519/edwards.rs +++ b/zk-token-sdk/src/curve25519/edwards.rs @@ -64,7 +64,7 @@ mod target_arch { fn try_from(pod: &PodEdwardsPoint) -> Result { CompressedEdwardsY::from_slice(&pod.0) - .expect("Input slice should have a length of 32") + .map_err(|_| Curve25519Error::PodConversion)? .decompress() .ok_or(Curve25519Error::PodConversion) } @@ -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) } } diff --git a/zk-token-sdk/src/curve25519/ristretto.rs b/zk-token-sdk/src/curve25519/ristretto.rs index 837c118438cab3..0ce247ae56168b 100644 --- a/zk-token-sdk/src/curve25519/ristretto.rs +++ b/zk-token-sdk/src/curve25519/ristretto.rs @@ -64,7 +64,7 @@ mod target_arch { fn try_from(pod: &PodRistrettoPoint) -> Result { CompressedRistretto::from_slice(&pod.0) - .expect("Input slice should have a length of 32") + .map_err(|_| Curve25519Error::PodConversion)? .decompress() .ok_or(Curve25519Error::PodConversion) } @@ -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() } } diff --git a/zk-token-sdk/src/encryption/elgamal.rs b/zk-token-sdk/src/encryption/elgamal.rs index ac7f765bedafa6..3763505ceab915 100644 --- a/zk-token-sdk/src/encryption/elgamal.rs +++ b/zk-token-sdk/src/encryption/elgamal.rs @@ -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. @@ -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) } } @@ -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) } } diff --git a/zk-token-sdk/src/encryption/pedersen.rs b/zk-token-sdk/src/encryption/pedersen.rs index 41f3898a5833ca..16d9e05353e36c 100644 --- a/zk-token-sdk/src/encryption/pedersen.rs +++ b/zk-token-sdk/src/encryption/pedersen.rs @@ -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) } } diff --git a/zk-token-sdk/src/sigma_proofs/mod.rs b/zk-token-sdk/src/sigma_proofs/mod.rs index ee23352557f4ed..db2b6535843096 100644 --- a/zk-token-sdk/src/sigma_proofs/mod.rs +++ b/zk-token-sdk/src/sigma_proofs/mod.rs @@ -38,9 +38,9 @@ fn ristretto_point_from_optional_slice( ) -> Result { 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) }