Skip to content

Commit

Permalink
elliptic-curve: leverage CtOption::into_option
Browse files Browse the repository at this point in the history
This was added in `subtle` v2.6.0.
  • Loading branch information
tarcieri committed Jun 24, 2024
1 parent 5e6ddf6 commit e0fb821
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ base16ct = "0.2"
crypto-bigint = { version = "=0.6.0-pre.12", default-features = false, features = ["rand_core", "hybrid-array", "zeroize"] }
hybrid-array = { version = "0.2.0-rc.8", default-features = false, features = ["zeroize"] }
rand_core = { version = "0.6.4", default-features = false }
subtle = { version = "2", default-features = false }
subtle = { version = "2.6", default-features = false }
zeroize = { version = "1.7", default-features = false }

# optional dependencies
Expand Down
6 changes: 6 additions & 0 deletions elliptic-curve/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ impl From<base16ct::Error> for Error {
}
}

impl From<core::array::TryFromSliceError> for Error {
fn from(_: core::array::TryFromSliceError) -> Error {
Error
}
}

#[cfg(feature = "pkcs8")]
impl From<pkcs8::Error> for Error {
fn from(_: pkcs8::Error) -> Error {
Expand Down
3 changes: 2 additions & 1 deletion elliptic-curve/src/point/non_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ where
where
D: de::Deserializer<'de>,
{
Option::from(Self::new(P::deserialize(deserializer)?))
Self::new(P::deserialize(deserializer)?)
.into_option()
.ok_or_else(|| de::Error::custom("expected non-identity point"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ where
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>,
{
let point = EncodedPoint::<C>::from_bytes(bytes).map_err(|_| Error)?;
Option::from(Self::from_encoded_point(&point)).ok_or(Error)
Self::from_encoded_point(&point).into_option().ok_or(Error)
}

/// Convert this [`PublicKey`] into the
Expand Down
14 changes: 6 additions & 8 deletions elliptic-curve/src/scalar/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use core::{
};
use crypto_bigint::{ArrayEncoding, Integer};
use ff::{Field, PrimeField};
use hybrid_array::{typenum::Unsigned, Array};
use rand_core::CryptoRngCore;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
use zeroize::Zeroize;
Expand Down Expand Up @@ -285,11 +284,9 @@ where
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() == C::FieldBytesSize::USIZE {
Option::from(NonZeroScalar::from_repr(Array::clone_from_slice(bytes))).ok_or(Error)
} else {
Err(Error)
}
NonZeroScalar::from_repr(bytes.try_into()?)
.into_option()
.ok_or(Error)
}
}

Expand Down Expand Up @@ -344,7 +341,7 @@ where
let mut bytes = FieldBytes::<C>::default();

if base16ct::mixed::decode(hex, &mut bytes)?.len() == bytes.len() {
Option::from(Self::from_repr(bytes)).ok_or(Error)
Self::from_repr(bytes).into_option().ok_or(Error)
} else {
Err(Error)
}
Expand Down Expand Up @@ -374,7 +371,8 @@ where
D: de::Deserializer<'de>,
{
let scalar = ScalarPrimitive::deserialize(deserializer)?;
Option::from(Self::new(scalar.into()))
Self::new(scalar.into())
.into_option()
.ok_or_else(|| de::Error::custom("expected non-zero scalar"))
}
}
Expand Down
2 changes: 1 addition & 1 deletion elliptic-curve/src/scalar/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
/// Decode [`ScalarPrimitive`] from a big endian byte slice.
pub fn from_slice(slice: &[u8]) -> Result<Self> {
let bytes = Array::try_from(slice).map_err(|_| Error)?;
Option::from(Self::from_bytes(&bytes)).ok_or(Error)
Self::from_bytes(&bytes).into_option().ok_or(Error)
}

/// Borrow the inner `C::Uint`.
Expand Down
5 changes: 3 additions & 2 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ where

/// Deserialize secret key from an encoded secret scalar.
pub fn from_bytes(bytes: &FieldBytes<C>) -> Result<Self> {
let inner: ScalarPrimitive<C> =
Option::from(ScalarPrimitive::from_bytes(bytes)).ok_or(Error)?;
let inner = ScalarPrimitive::<C>::from_bytes(bytes)
.into_option()
.ok_or(Error)?;

if inner.is_zero().into() {
return Err(Error);
Expand Down

0 comments on commit e0fb821

Please sign in to comment.