Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BoxedUint: change internal representation to Box<[Limb]> #305

Merged
merged 1 commit into from
Nov 23, 2023
Merged
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
42 changes: 27 additions & 15 deletions src/boxed/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod cmp;
mod sub;

use crate::{Limb, Word};
use alloc::{vec, vec::Vec};
use alloc::{boxed::Box, vec, vec::Vec};
use core::fmt;

#[cfg(feature = "zeroize")]
Expand All @@ -21,8 +21,10 @@ use zeroize::Zeroize;
/// automatically growing.
#[derive(Clone, Default)]
pub struct BoxedUint {
/// Inner limb vector. Stored from least significant to most significant.
limbs: Vec<Limb>,
/// Boxed slice containing limbs.
///
/// Stored from least significant to most significant.
limbs: Box<[Limb]>,
}

impl BoxedUint {
Expand All @@ -34,7 +36,7 @@ impl BoxedUint {
/// Get the value `1`, represented as succinctly as possible.
pub fn one() -> Self {
Self {
limbs: vec![Limb::ONE; 1],
limbs: vec![Limb::ONE; 1].into(),
}
}

Expand All @@ -50,7 +52,7 @@ impl BoxedUint {
let nlimbs = bits_precision / Limb::BITS;

Some(Self {
limbs: vec![Limb::ZERO; nlimbs],
limbs: vec![Limb::ZERO; nlimbs].into(),
})
}

Expand All @@ -61,7 +63,7 @@ impl BoxedUint {
pub fn max(bits_precision: usize) -> Option<Self> {
let mut ret = Self::new(bits_precision)?;

for limb in &mut ret.limbs {
for limb in &mut *ret.limbs {
*limb = Limb::MAX;
}

Expand All @@ -77,28 +79,33 @@ impl BoxedUint {
}
}

/// Create an array of [`Word`]s (i.e. word-sized unsigned integers) from
/// Create a boxed slice of [`Word`]s (i.e. word-sized unsigned integers) from
/// a [`BoxedUint`].
#[inline]
pub fn to_words(&self) -> Vec<Word> {
self.limbs.iter().copied().map(Into::into).collect()
pub fn to_words(&self) -> Box<[Word]> {
self.limbs
.iter()
.copied()
.map(Into::into)
.collect::<Vec<_>>()
.into()
}

/// Borrow the inner limbs as a slice of [`Word`]s.
pub fn as_words(&self) -> &[Word] {
// SAFETY: `Limb` is a `repr(transparent)` newtype for `Word`
#[allow(trivial_casts, unsafe_code)]
unsafe {
&*((self.limbs.as_slice() as *const _) as *const [Word])
&*((&*self.limbs as *const _) as *const [Word])
}
}

/// Borrow the inner limbs as a mutable array of [`Word`]s.
/// Borrow the inner limbs as a mutable slice of [`Word`]s.
pub fn as_words_mut(&mut self) -> &mut [Word] {
// SAFETY: `Limb` is a `repr(transparent)` newtype for `Word`
#[allow(trivial_casts, unsafe_code)]
unsafe {
&mut *((self.limbs.as_mut_slice() as *mut _) as *mut [Word])
&mut *((&mut *self.limbs as *mut _) as *mut [Word])
}
}

Expand All @@ -113,12 +120,12 @@ impl BoxedUint {
}

/// Convert this [`BoxedUint`] into its inner limbs.
pub fn to_limbs(&self) -> Vec<Limb> {
pub fn to_limbs(&self) -> Box<[Limb]> {
self.limbs.clone()
}

/// Convert this [`BoxedUint`] into its inner limbs.
pub fn into_limbs(self) -> Vec<Limb> {
pub fn into_limbs(self) -> Box<[Limb]> {
self.limbs
}

Expand Down Expand Up @@ -158,7 +165,12 @@ impl BoxedUint {
carry = c;
}

(Self { limbs }, carry)
(
Self {
limbs: limbs.into(),
},
carry,
)
}
}

Expand Down