From e88750b87460e38b1b8ed6963a0431d3bd8ded75 Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Thu, 23 May 2024 11:25:54 -0700 Subject: [PATCH] Define native word size as `constant_time::Word`. --- src/constant_time.rs | 6 ++++++ src/limb.rs | 26 ++++++-------------------- src/polyfill.rs | 5 +++++ 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/constant_time.rs b/src/constant_time.rs index a7a8a99681..89f89ac3d5 100644 --- a/src/constant_time.rs +++ b/src/constant_time.rs @@ -16,6 +16,12 @@ use crate::{c, error}; +#[cfg(target_pointer_width = "64")] +pub(crate) type Word = u64; + +#[cfg(target_pointer_width = "32")] +pub(crate) type Word = u32; + /// Returns `Ok(())` if `a == b` and `Err(error::Unspecified)` otherwise. /// The comparison of `a` and `b` is done in constant time with respect to the /// contents of each, but NOT in constant time with respect to the lengths of diff --git a/src/limb.rs b/src/limb.rs index 1fd6c27b5a..b18f929bab 100644 --- a/src/limb.rs +++ b/src/limb.rs @@ -21,34 +21,20 @@ use crate::{c, error, polyfill::ArrayFlatMap}; #[cfg(any(test, feature = "alloc"))] -use crate::bits; +use crate::{bits, constant_time, polyfill::usize_from_u32}; #[cfg(feature = "alloc")] use core::num::Wrapping; // XXX: Not correct for x32 ABIs. -#[cfg(target_pointer_width = "64")] -pub type Limb = u64; -#[cfg(target_pointer_width = "32")] -pub type Limb = u32; -#[cfg(target_pointer_width = "64")] -pub const LIMB_BITS: usize = 64; -#[cfg(target_pointer_width = "32")] -pub const LIMB_BITS: usize = 32; - -#[cfg(target_pointer_width = "64")] -#[derive(Debug, PartialEq)] -#[repr(u64)] -pub enum LimbMask { - True = 0xffff_ffff_ffff_ffff, - False = 0, -} +pub type Limb = constant_time::Word; +pub const LIMB_BITS: usize = usize_from_u32(Limb::BITS); -#[cfg(target_pointer_width = "32")] +#[cfg_attr(target_pointer_width = "64", repr(u64))] +#[cfg_attr(target_pointer_width = "32", repr(u32))] #[derive(Debug, PartialEq)] -#[repr(u32)] pub enum LimbMask { - True = 0xffff_ffff, + True = Limb::MAX, False = 0, } diff --git a/src/polyfill.rs b/src/polyfill.rs index 3408a02a1d..42f455eb6b 100644 --- a/src/polyfill.rs +++ b/src/polyfill.rs @@ -21,6 +21,11 @@ pub const fn u64_from_usize(x: usize) -> u64 { x as u64 } +#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +pub const fn usize_from_u32(x: u32) -> usize { + x as usize +} + #[cfg(all(target_arch = "aarch64", target_pointer_width = "64"))] #[allow(clippy::cast_possible_truncation)] pub fn usize_from_u64(x: u64) -> usize {