Skip to content

Commit

Permalink
use into_option
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Jun 24, 2024
1 parent 64c9541 commit 3a72ac5
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 27 deletions.
8 changes: 6 additions & 2 deletions curves/curve25519/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ mod target_arch {
type Error = Curve25519Error;

fn try_from(pod: &PodScalar) -> Result<Self, Self::Error> {
Option::from(Scalar::from_canonical_bytes(pod.0)).ok_or(Curve25519Error::PodConversion)
Scalar::from_canonical_bytes(pod.0)
.into_option()
.ok_or(Curve25519Error::PodConversion)
}
}

Expand All @@ -32,7 +34,9 @@ mod target_arch {
type Error = Curve25519Error;

fn try_from(pod: PodScalar) -> Result<Self, Self::Error> {
Option::from(Scalar::from_canonical_bytes(pod.0)).ok_or(Curve25519Error::PodConversion)
Scalar::from_canonical_bytes(pod.0)
.into_option()
.ok_or(Curve25519Error::PodConversion)
}
}
}
2 changes: 1 addition & 1 deletion perf/src/sigverify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ mod tests {
for _ in 0..1_000_000 {
thread_rng().fill(&mut input);
let ans = get_checked_scalar(&input);
let ref_ans = Option::<Scalar>::from(Scalar::from_canonical_bytes(input));
let ref_ans = Scalar::from_canonical_bytes(input).into_option();
if let Some(ref_ans) = ref_ans {
passed += 1;
assert_eq!(ans.unwrap(), ref_ans.to_bytes());
Expand Down
3 changes: 2 additions & 1 deletion zk-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ impl TryFrom<&[u8]> for ElGamalSecretKey {
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
match bytes.try_into() {
Ok(bytes) => Ok(ElGamalSecretKey::from(
Option::<Scalar>::from(Scalar::from_canonical_bytes(bytes))
Scalar::from_canonical_bytes(bytes)
.into_option()
.ok_or(ElGamalError::SecretKeyDeserialization)?,
)),
_ => Err(ElGamalError::SecretKeyDeserialization),
Expand Down
4 changes: 3 additions & 1 deletion zk-sdk/src/encryption/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ impl PedersenOpening {

pub fn from_bytes(bytes: &[u8]) -> Option<PedersenOpening> {
match bytes.try_into() {
Ok(bytes) => Option::from(Scalar::from_canonical_bytes(bytes)).map(PedersenOpening),
Ok(bytes) => Scalar::from_canonical_bytes(bytes)
.into_option()
.map(PedersenOpening),
_ => None,
}
}
Expand Down
10 changes: 5 additions & 5 deletions zk-sdk/src/range_proof/inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ impl InnerProductProof {
}

let pos = 2 * lg_n * 32;
let a = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[pos..])))
let a = Scalar::from_canonical_bytes(util::read32(&slice[pos..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let b = Scalar::from_canonical_bytes(util::read32(&slice[pos + 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let b = Option::from(Scalar::from_canonical_bytes(util::read32(
&slice[pos + 32..],
)))
.ok_or(RangeProofVerificationError::Deserialization)?;

Ok(InnerProductProof { L_vec, R_vec, a, b })
}
Expand Down
12 changes: 7 additions & 5 deletions zk-sdk/src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,14 @@ impl RangeProof {
let T_1 = CompressedRistretto(util::read32(&slice[2 * 32..]));
let T_2 = CompressedRistretto(util::read32(&slice[3 * 32..]));

let t_x = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[4 * 32..])))
let t_x = Scalar::from_canonical_bytes(util::read32(&slice[4 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let t_x_blinding =
Option::from(Scalar::from_canonical_bytes(util::read32(&slice[5 * 32..])))
.ok_or(RangeProofVerificationError::Deserialization)?;
let e_blinding = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[6 * 32..])))
let t_x_blinding = Scalar::from_canonical_bytes(util::read32(&slice[5 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let e_blinding = Scalar::from_canonical_bytes(util::read32(&slice[6 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;

let ipp_proof = InnerProductProof::from_bytes(&slice[7 * 32..])?;
Expand Down
2 changes: 1 addition & 1 deletion zk-sdk/src/sigma_proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ fn canonical_scalar_from_optional_slice(
optional_slice
.and_then(|slice| (slice.len() == SCALAR_LEN).then_some(slice)) // if chunk is the wrong length, convert to None
.and_then(|slice| slice.try_into().ok()) // convert to array
.and_then(|slice| Option::from(Scalar::from_canonical_bytes(slice)))
.and_then(|slice| Scalar::from_canonical_bytes(slice).into_option())
.ok_or(SigmaProofVerificationError::Deserialization)
}
3 changes: 2 additions & 1 deletion zk-token-sdk/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ impl TryFrom<&[u8]> for ElGamalSecretKey {
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
match bytes.try_into() {
Ok(bytes) => Ok(ElGamalSecretKey::from(
Option::<Scalar>::from(Scalar::from_canonical_bytes(bytes))
Scalar::from_canonical_bytes(bytes)
.into_option()
.ok_or(ElGamalError::SecretKeyDeserialization)?,
)),
_ => Err(ElGamalError::SecretKeyDeserialization),
Expand Down
10 changes: 5 additions & 5 deletions zk-token-sdk/src/range_proof/inner_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,12 @@ impl InnerProductProof {
}

let pos = 2 * lg_n * 32;
let a = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[pos..])))
let a = Scalar::from_canonical_bytes(util::read32(&slice[pos..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let b = Scalar::from_canonical_bytes(util::read32(&slice[pos + 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let b = Option::from(Scalar::from_canonical_bytes(util::read32(
&slice[pos + 32..],
)))
.ok_or(RangeProofVerificationError::Deserialization)?;

Ok(InnerProductProof { L_vec, R_vec, a, b })
}
Expand Down
12 changes: 7 additions & 5 deletions zk-token-sdk/src/range_proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,14 @@ impl RangeProof {
let T_1 = CompressedRistretto(util::read32(&slice[2 * 32..]));
let T_2 = CompressedRistretto(util::read32(&slice[3 * 32..]));

let t_x = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[4 * 32..])))
let t_x = Scalar::from_canonical_bytes(util::read32(&slice[4 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let t_x_blinding =
Option::from(Scalar::from_canonical_bytes(util::read32(&slice[5 * 32..])))
.ok_or(RangeProofVerificationError::Deserialization)?;
let e_blinding = Option::from(Scalar::from_canonical_bytes(util::read32(&slice[6 * 32..])))
let t_x_blinding = Scalar::from_canonical_bytes(util::read32(&slice[5 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;
let e_blinding = Scalar::from_canonical_bytes(util::read32(&slice[6 * 32..]))
.into_option()
.ok_or(RangeProofVerificationError::Deserialization)?;

let ipp_proof = InnerProductProof::from_bytes(&slice[7 * 32..])?;
Expand Down

0 comments on commit 3a72ac5

Please sign in to comment.