Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/const_choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ impl ConstChoice {
}

#[inline]
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's some way to make an alias for that so that we could only spell out this condition once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also considered it, but I believe it'd require adding a build script to set a cfg for the desired word size. I didn't want to take that step when we have this single exception at this time.

Copy link
Member

@tarcieri tarcieri Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was intending to solve this with the vaporwarecpubits crate (which as it were uses a build script to set a cfg attribute)

RustCrypto/utils#826

Copy link
Member

@tarcieri tarcieri Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to take that step when we have this single exception at this time.

IMO this isn’t worth doing in a way that can’t be applied easily and in a DRY manner for multiple targets.

As noted in RustCrypto/utils#824 this same sort of 64-bit promotion should be applied to ARMv7 targets

pub(crate) const fn as_u64_mask(&self) -> u64 {
((self.0 as u64) << 32) | (self.0 as u64)
}

#[inline]
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub(crate) const fn as_u64_mask(&self) -> u64 {
self.0
}
Expand Down
4 changes: 2 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,14 @@ mod tests {

use crate::{ConstChoice, I128, U128};

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
#[test]
fn as_words() {
let n = I128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
assert_eq!(n.as_words(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
}

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
#[test]
fn as_words_mut() {
let mut n = I128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
Expand Down
8 changes: 4 additions & 4 deletions src/int/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ impl<const LIMBS: usize> Int<LIMBS> {

/// Create a [`Int`] from an `i64` (const-friendly)
// TODO(tarcieri): replace with `const impl From<i64>` when stable
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
pub const fn from_i64(n: i64) -> Self {
Uint::<{ I64::LIMBS }>::from_u64(n as u64).as_int().resize()
}

/// Create a [`Int`] from an `i64` (const-friendly)
// TODO(tarcieri): replace with `const impl From<i64>` when stable
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub const fn from_i64(n: i64) -> Self {
assert!(LIMBS >= 1, "number of limbs must be greater than zero");
Uint::new([Limb(n as Word)]).as_int().resize()
Expand Down Expand Up @@ -108,9 +108,9 @@ impl<const LIMBS: usize, const LIMBS2: usize> From<&Int<LIMBS>> for Int<LIMBS2>

#[cfg(test)]
mod tests {
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
use crate::I64 as IntEx;
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
use crate::I128 as IntEx;
use crate::{I128, Limb};

Expand Down
28 changes: 14 additions & 14 deletions src/int/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,58 @@

use crate::Int;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I64 = Int<1>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I128 = Int<2>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I256 = Int<4>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I512 = Int<8>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I1024 = Int<16>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I2048 = Int<32>;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
/// Signed bit integer.
pub type I4096 = Int<64>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I64 = Int<2>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I128 = Int<4>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I256 = Int<8>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I512 = Int<16>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I1024 = Int<32>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I2048 = Int<64>;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
/// Signed bit integer.
pub type I4096 = Int<128>;
20 changes: 10 additions & 10 deletions src/limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ compile_error!("this crate builds on 32-bit and 64-bit platforms only");
//

/// Inner integer type that the [`Limb`] newtype wraps.
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
pub type Word = u32;

/// Unsigned wide integer type: double the width of [`Word`].
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
pub type WideWord = u64;

//
// 64-bit definitions
//

/// Unsigned integer type that the [`Limb`] newtype wraps.
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub type Word = u64;

/// Wide integer type: double the width of [`Word`].
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub type WideWord = u128;

/// Big integers are represented as an array/vector of smaller CPU word-size integers called
Expand Down Expand Up @@ -80,19 +80,19 @@ impl Limb {
// 32-bit

/// Size of the inner integer in bits.
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
pub const BITS: u32 = 32;
/// Size of the inner integer in bytes.
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
pub const BYTES: usize = 4;

// 64-bit

/// Size of the inner integer in bits.
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub const BITS: u32 = 64;
/// Size of the inner integer in bytes.
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub const BYTES: usize = 8;

/// `floor(log2(Self::BITS))`.
Expand Down Expand Up @@ -239,10 +239,10 @@ mod tests {
#[cfg(feature = "alloc")]
#[test]
fn debug() {
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
assert_eq!(format!("{:?}", Limb(42)), "Limb(0x0000002A)");

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
assert_eq!(format!("{:?}", Limb(42)), "Limb(0x000000000000002A)");
}
}
8 changes: 4 additions & 4 deletions src/limb/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use super::{Limb, Word};
use crate::Encoding;

impl Encoding for Limb {
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
type Repr = [u8; 4];
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
type Repr = [u8; 8];

#[inline]
Expand Down Expand Up @@ -65,10 +65,10 @@ impl Limb {
mod test {
use super::*;

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
const LIMB: Limb = Limb(0x7654_3210);

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
const LIMB: Limb = Limb(0xFEDCBA9876543210);

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/limb/from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Limb {

/// Create a [`Limb`] from a `u64` integer (const-friendly)
// TODO(tarcieri): replace with `const impl From<u64>` when stable
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub const fn from_u64(n: u64) -> Self {
Limb(n)
}
Expand All @@ -51,7 +51,7 @@ impl From<u32> for Limb {
}
}

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
impl From<u64> for Limb {
#[inline]
fn from(n: u64) -> Limb {
Expand Down
4 changes: 2 additions & 2 deletions src/limb/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,14 @@ mod tests {
use super::{CheckedMul, Limb};

#[test]
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
fn checked_mul_ok() {
let n = Limb::from_u16(0xffff);
assert_eq!(n.checked_mul(&n).unwrap(), Limb::from_u32(0xfffe_0001));
}

#[test]
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
fn checked_mul_ok() {
let n = Limb::from_u32(0xffff_ffff);
assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/limb/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use subtle::ConstantTimeLess;

impl Random for Limb {
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
let val = rng.try_next_u32()?;
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
let val = rng.try_next_u64()?;

Ok(Self(val))
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! nlimbs {

#[cfg(test)]
mod tests {
#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
#[test]
fn nlimbs_for_bits_macro() {
assert_eq!(nlimbs!(64), 2);
Expand All @@ -20,7 +20,7 @@ mod tests {
assert_eq!(nlimbs!(256), 8);
}

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
#[test]
fn nlimbs_for_bits_macro() {
assert_eq!(nlimbs!(64), 1);
Expand Down
4 changes: 2 additions & 2 deletions src/non_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl NonZero<Limb> {

/// Create a [`NonZero<Limb>`] from a [`NonZeroU64`] (const-friendly)
// TODO(tarcieri): replace with `const impl From<NonZeroU64>` when stable
#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
pub const fn from_u64(n: NonZeroU64) -> Self {
Self(Limb::from_u64(n.get()))
}
Expand Down Expand Up @@ -367,7 +367,7 @@ impl From<NonZeroU32> for NonZero<Limb> {
}
}

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
impl From<NonZeroU64> for NonZero<Limb> {
fn from(integer: NonZeroU64) -> Self {
Self::from_u64(integer)
Expand Down
8 changes: 4 additions & 4 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,13 @@ impl_uint_aliases! {
(U32768, 32768, "32768-bit")
}

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
impl_uint_aliases! {
(U224, 224, "224-bit"), // For NIST P-224
(U544, 544, "544-bit") // For NIST P-521
}

#[cfg(target_pointer_width = "32")]
#[cfg(all(target_pointer_width = "32", not(target_family = "wasm")))]
impl_uint_concat_split_even! {
U64,
}
Expand Down Expand Up @@ -533,14 +533,14 @@ mod tests {
#[cfg(feature = "alloc")]
use alloc::format;

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
#[test]
fn as_words() {
let n = U128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
assert_eq!(n.as_words(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
}

#[cfg(target_pointer_width = "64")]
#[cfg(any(target_pointer_width = "64", target_family = "wasm"))]
#[test]
fn as_words_mut() {
let mut n = U128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
Expand Down
Loading