Skip to content

Commit

Permalink
Define native word size as constant_time::Word.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jun 12, 2024
1 parent 16e33e0 commit e88750b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
6 changes: 6 additions & 0 deletions src/constant_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 6 additions & 20 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down
5 changes: 5 additions & 0 deletions src/polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e88750b

Please sign in to comment.