From 3a72ac5a477bef83bf990304c13de99d1ca242fe Mon Sep 17 00:00:00 2001 From: yihau Date: Mon, 24 Jun 2024 19:14:06 +0800 Subject: [PATCH] use into_option --- curves/curve25519/src/scalar.rs | 8 ++++++-- perf/src/sigverify.rs | 2 +- zk-sdk/src/encryption/elgamal.rs | 3 ++- zk-sdk/src/encryption/pedersen.rs | 4 +++- zk-sdk/src/range_proof/inner_product.rs | 10 +++++----- zk-sdk/src/range_proof/mod.rs | 12 +++++++----- zk-sdk/src/sigma_proofs/mod.rs | 2 +- zk-token-sdk/src/encryption/elgamal.rs | 3 ++- zk-token-sdk/src/range_proof/inner_product.rs | 10 +++++----- zk-token-sdk/src/range_proof/mod.rs | 12 +++++++----- 10 files changed, 39 insertions(+), 27 deletions(-) diff --git a/curves/curve25519/src/scalar.rs b/curves/curve25519/src/scalar.rs index 0b78221fbe7be8..5df77de1aa1e01 100644 --- a/curves/curve25519/src/scalar.rs +++ b/curves/curve25519/src/scalar.rs @@ -18,7 +18,9 @@ mod target_arch { type Error = Curve25519Error; fn try_from(pod: &PodScalar) -> Result { - Option::from(Scalar::from_canonical_bytes(pod.0)).ok_or(Curve25519Error::PodConversion) + Scalar::from_canonical_bytes(pod.0) + .into_option() + .ok_or(Curve25519Error::PodConversion) } } @@ -32,7 +34,9 @@ mod target_arch { type Error = Curve25519Error; fn try_from(pod: PodScalar) -> Result { - Option::from(Scalar::from_canonical_bytes(pod.0)).ok_or(Curve25519Error::PodConversion) + Scalar::from_canonical_bytes(pod.0) + .into_option() + .ok_or(Curve25519Error::PodConversion) } } } diff --git a/perf/src/sigverify.rs b/perf/src/sigverify.rs index 6dbc7995bf37a8..12a8d92f54137b 100644 --- a/perf/src/sigverify.rs +++ b/perf/src/sigverify.rs @@ -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::::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()); diff --git a/zk-sdk/src/encryption/elgamal.rs b/zk-sdk/src/encryption/elgamal.rs index 2fd6c0ff797990..0496a042fb6950 100644 --- a/zk-sdk/src/encryption/elgamal.rs +++ b/zk-sdk/src/encryption/elgamal.rs @@ -552,7 +552,8 @@ impl TryFrom<&[u8]> for ElGamalSecretKey { fn try_from(bytes: &[u8]) -> Result { match bytes.try_into() { Ok(bytes) => Ok(ElGamalSecretKey::from( - Option::::from(Scalar::from_canonical_bytes(bytes)) + Scalar::from_canonical_bytes(bytes) + .into_option() .ok_or(ElGamalError::SecretKeyDeserialization)?, )), _ => Err(ElGamalError::SecretKeyDeserialization), diff --git a/zk-sdk/src/encryption/pedersen.rs b/zk-sdk/src/encryption/pedersen.rs index 7beac1312f52cd..2dc20cb0b520bf 100644 --- a/zk-sdk/src/encryption/pedersen.rs +++ b/zk-sdk/src/encryption/pedersen.rs @@ -89,7 +89,9 @@ impl PedersenOpening { pub fn from_bytes(bytes: &[u8]) -> Option { 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, } } diff --git a/zk-sdk/src/range_proof/inner_product.rs b/zk-sdk/src/range_proof/inner_product.rs index 4305a96c6ab9f2..fef991817e016c 100644 --- a/zk-sdk/src/range_proof/inner_product.rs +++ b/zk-sdk/src/range_proof/inner_product.rs @@ -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 }) } diff --git a/zk-sdk/src/range_proof/mod.rs b/zk-sdk/src/range_proof/mod.rs index 0f50c6e0fe47af..eacbb272e852a9 100644 --- a/zk-sdk/src/range_proof/mod.rs +++ b/zk-sdk/src/range_proof/mod.rs @@ -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..])?; diff --git a/zk-sdk/src/sigma_proofs/mod.rs b/zk-sdk/src/sigma_proofs/mod.rs index 7bd5f7e9b8d546..c8b847a61399b3 100644 --- a/zk-sdk/src/sigma_proofs/mod.rs +++ b/zk-sdk/src/sigma_proofs/mod.rs @@ -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) } diff --git a/zk-token-sdk/src/encryption/elgamal.rs b/zk-token-sdk/src/encryption/elgamal.rs index ed2e2d6cc90adc..c6911abce60bd8 100644 --- a/zk-token-sdk/src/encryption/elgamal.rs +++ b/zk-token-sdk/src/encryption/elgamal.rs @@ -617,7 +617,8 @@ impl TryFrom<&[u8]> for ElGamalSecretKey { fn try_from(bytes: &[u8]) -> Result { match bytes.try_into() { Ok(bytes) => Ok(ElGamalSecretKey::from( - Option::::from(Scalar::from_canonical_bytes(bytes)) + Scalar::from_canonical_bytes(bytes) + .into_option() .ok_or(ElGamalError::SecretKeyDeserialization)?, )), _ => Err(ElGamalError::SecretKeyDeserialization), diff --git a/zk-token-sdk/src/range_proof/inner_product.rs b/zk-token-sdk/src/range_proof/inner_product.rs index 7f7ae980de6e77..3360dcf8809fba 100644 --- a/zk-token-sdk/src/range_proof/inner_product.rs +++ b/zk-token-sdk/src/range_proof/inner_product.rs @@ -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 }) } diff --git a/zk-token-sdk/src/range_proof/mod.rs b/zk-token-sdk/src/range_proof/mod.rs index f047c8b1cb0ae3..61783a3991adaf 100644 --- a/zk-token-sdk/src/range_proof/mod.rs +++ b/zk-token-sdk/src/range_proof/mod.rs @@ -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..])?;