Skip to content

Commit

Permalink
use let else
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Jun 13, 2024
1 parent 4bdc633 commit 23147ed
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 60 deletions.
10 changes: 7 additions & 3 deletions sdk/program/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,13 @@ impl TryFrom<&str> for Pubkey {
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())
.map(|compressed_edwards_y| compressed_edwards_y.decompress().is_some())
.unwrap_or(false)
let Ok(compressed_edwards_y) =
curve25519_dalek::edwards::CompressedEdwardsY::from_slice(_bytes.as_ref())
else {
return false;
};

compressed_edwards_y.decompress().is_some()
}
#[cfg(target_os = "solana")]
unimplemented!();
Expand Down
27 changes: 18 additions & 9 deletions zk-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,15 @@ impl TryFrom<&[u8]> for ElGamalPubkey {
return Err(ElGamalError::PubkeyDeserialization);
}

CompressedRistretto::from_slice(bytes)
.map_err(|_| ElGamalError::PubkeyDeserialization)?
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)
.map(ElGamalPubkey)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return Err(ElGamalError::PubkeyDeserialization);
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return Err(ElGamalError::PubkeyDeserialization);
};

Ok(ElGamalPubkey(ristretto_point))
}
}

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

CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(DecryptHandle)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return None;
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return None;
};

Some(DecryptHandle(ristretto_point))
}
}

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

CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(PedersenCommitment)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return None;
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return None;
};

Some(PedersenCommitment(ristretto_point))
}
}

Expand Down
19 changes: 13 additions & 6 deletions zk-sdk/src/sigma_proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ use {
fn ristretto_point_from_optional_slice(
optional_slice: Option<&[u8]>,
) -> Result<CompressedRistretto, SigmaProofVerificationError> {
optional_slice
.and_then(|slice| (slice.len() == RISTRETTO_POINT_LEN).then_some(slice))
.map(CompressedRistretto::from_slice)
.transpose()
.map_err(|_| SigmaProofVerificationError::Deserialization)?
.ok_or(SigmaProofVerificationError::Deserialization)
let Some(slice) = optional_slice else {
return Err(SigmaProofVerificationError::Deserialization);
};

if slice.len() != RISTRETTO_POINT_LEN {
return Err(SigmaProofVerificationError::Deserialization);
}

let Ok(compressed_ristretto) = CompressedRistretto::from_slice(slice) else {
return Err(SigmaProofVerificationError::Deserialization);
};

Ok(compressed_ristretto)
}

/// Deserializes an optional slice of bytes to a scalar.
Expand Down
18 changes: 11 additions & 7 deletions zk-token-sdk/src/curve25519/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,24 @@ mod target_arch {
type Error = Curve25519Error;

fn try_from(pod: &PodEdwardsPoint) -> Result<Self, Self::Error> {
CompressedEdwardsY::from_slice(&pod.0)
.map_err(|_| Curve25519Error::PodConversion)?
.decompress()
.ok_or(Curve25519Error::PodConversion)
let Ok(compressed_edwards_y) = CompressedEdwardsY::from_slice(&pod.0) else {
return Err(Curve25519Error::PodConversion);
};
let Some(edwards_point) = compressed_edwards_y.decompress() else {
return Err(Curve25519Error::PodConversion);
};
Ok(edwards_point)
}
}

impl PointValidation for PodEdwardsPoint {
type Point = Self;

fn validate_point(&self) -> bool {
CompressedEdwardsY::from_slice(&self.0)
.map(|compressed_edwards_y| compressed_edwards_y.decompress().is_some())
.unwrap_or(false)
let Ok(compressed_edwards_y) = CompressedEdwardsY::from_slice(&self.0) else {
return false;
};
compressed_edwards_y.decompress().is_some()
}
}

Expand Down
19 changes: 11 additions & 8 deletions zk-token-sdk/src/curve25519/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,24 @@ mod target_arch {
type Error = Curve25519Error;

fn try_from(pod: &PodRistrettoPoint) -> Result<Self, Self::Error> {
CompressedRistretto::from_slice(&pod.0)
.map_err(|_| Curve25519Error::PodConversion)?
.decompress()
.ok_or(Curve25519Error::PodConversion)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(&pod.0) else {
return Err(Curve25519Error::PodConversion);
};
let Some(ristretto_point) = compressed_ristretto.decompress() else {
return Err(Curve25519Error::PodConversion);
};
Ok(ristretto_point)
}
}

impl PointValidation for PodRistrettoPoint {
type Point = Self;

fn validate_point(&self) -> bool {
CompressedRistretto::from_slice(&self.0)
.ok()
.and_then(|compressed_ristretto| compressed_ristretto.decompress())
.is_some()
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(&self.0) else {
return false;
};
compressed_ristretto.decompress().is_some()
}
}

Expand Down
40 changes: 27 additions & 13 deletions zk-token-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,15 @@ impl ElGamalPubkey {
return None;
}

CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(ElGamalPubkey)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return None;
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return None;
};

Some(ElGamalPubkey(ristretto_point))
}

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

CompressedRistretto::from_slice(bytes)
.map_err(|_| ElGamalError::PubkeyDeserialization)?
.decompress()
.ok_or(ElGamalError::PubkeyDeserialization)
.map(ElGamalPubkey)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return Err(ElGamalError::PubkeyDeserialization);
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return Err(ElGamalError::PubkeyDeserialization);
};

Ok(ElGamalPubkey(ristretto_point))
}
}

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

CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(DecryptHandle)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return None;
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return None;
};

Some(DecryptHandle(ristretto_point))
}
}

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

CompressedRistretto::from_slice(bytes)
.ok()?
.decompress()
.map(PedersenCommitment)
let Ok(compressed_ristretto) = CompressedRistretto::from_slice(bytes) else {
return None;
};

let Some(ristretto_point) = compressed_ristretto.decompress() else {
return None;
};

Some(PedersenCommitment(ristretto_point))
}
}

Expand Down
19 changes: 13 additions & 6 deletions zk-token-sdk/src/sigma_proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ use {
fn ristretto_point_from_optional_slice(
optional_slice: Option<&[u8]>,
) -> Result<CompressedRistretto, SigmaProofVerificationError> {
optional_slice
.and_then(|slice| (slice.len() == RISTRETTO_POINT_LEN).then_some(slice))
.map(CompressedRistretto::from_slice)
.transpose()
.map_err(|_| SigmaProofVerificationError::Deserialization)?
.ok_or(SigmaProofVerificationError::Deserialization)
let Some(slice) = optional_slice else {
return Err(SigmaProofVerificationError::Deserialization);
};

if slice.len() != RISTRETTO_POINT_LEN {
return Err(SigmaProofVerificationError::Deserialization);
}

let Ok(compressed_ristretto) = CompressedRistretto::from_slice(slice) else {
return Err(SigmaProofVerificationError::Deserialization);
};

Ok(compressed_ristretto)
}

/// Deserializes an optional slice of bytes to a scalar.
Expand Down

0 comments on commit 23147ed

Please sign in to comment.