From f15eec95c1c5c3fa49f990fd18813f43afc1e206 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Fri, 28 Oct 2022 21:21:59 -0700 Subject: [PATCH] NFC: Polyfill `const_option` feature. --- src/polyfill.rs | 4 +++- src/polyfill/unwrap_const.rs | 15 +++++++++++++++ src/rsa/public_exponent.rs | 22 ++++------------------ 3 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 src/polyfill/unwrap_const.rs diff --git a/src/polyfill.rs b/src/polyfill.rs index a3cc6d5cf..fbdfdbef3 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -35,7 +35,9 @@ mod leading_zeros_skipped; #[cfg(test)] mod test; -pub use self::{array_flat_map::ArrayFlatMap, chunks_fixed::*}; +mod unwrap_const; + +pub use self::{array_flat_map::ArrayFlatMap, chunks_fixed::*, unwrap_const::unwrap_const}; #[cfg(feature = "alloc")] pub use leading_zeros_skipped::LeadingZerosStripped; diff --git a/src/polyfill/unwrap_const.rs b/src/polyfill/unwrap_const.rs new file mode 100644 index 000000000..5d85ee0aa --- /dev/null +++ b/src/polyfill/unwrap_const.rs @@ -0,0 +1,15 @@ +/// Polyfill for `Option::unwrap()` as a const fn; feature `const_option`. +/// https://github.com/rust-lang/rust/issues/67441. +/// TODO(MSRV): Replace this with `x.unwrap()`. +/// +/// `T: Copy` avoids "constant functions cannot evaluate destructors." +pub const fn unwrap_const(x: Option) -> T +where + T: Copy, +{ + if let Some(x) = x { + x + } else { + panic!("unwrap_const on `None`"); + } +} diff --git a/src/rsa/public_exponent.rs b/src/rsa/public_exponent.rs index 0c1d633c5..b87b30b21 100644 --- a/src/rsa/public_exponent.rs +++ b/src/rsa/public_exponent.rs @@ -1,5 +1,5 @@ use crate::error; -use crate::polyfill::{ArrayFlatMap, LeadingZerosStripped}; +use crate::polyfill::{unwrap_const, ArrayFlatMap, LeadingZerosStripped}; use core::num::NonZeroU64; /// The exponent `e` of an RSA public key. @@ -10,16 +10,8 @@ impl PublicExponent { #[cfg(test)] const ALL_CONSTANTS: [Self; 3] = [Self::_3, Self::_65537, Self::MAX]; - // TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is - // stable. - pub(super) const _3: Self = Self(match NonZeroU64::new(3) { - Some(nz) => nz, - None => unreachable!(), - }); - pub(super) const _65537: Self = Self(match NonZeroU64::new(65537) { - Some(nz) => nz, - None => unreachable!(), - }); + pub(super) const _3: Self = Self(unwrap_const(NonZeroU64::new(3))); + pub(super) const _65537: Self = Self(unwrap_const(NonZeroU64::new(65537))); // This limit was chosen to bound the performance of the simple // exponentiation-by-squaring implementation in `elem_exp_vartime`. In @@ -32,13 +24,7 @@ impl PublicExponent { // [1] https://www.imperialviolet.org/2012/03/16/rsae.html // [2] https://www.imperialviolet.org/2012/03/17/rsados.html // [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx - // - // TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is - // stable. - const MAX: Self = Self(match NonZeroU64::new((1u64 << 33) - 1) { - Some(nz) => nz, - None => unreachable!(), - }); + const MAX: Self = Self(unwrap_const(NonZeroU64::new((1u64 << 33) - 1))); pub(super) fn from_be_bytes( input: untrusted::Input,