Skip to content

Rename as_(limbs|words)_mut => as_mut_(limbs|words) #805

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

Merged
merged 1 commit into from
Apr 23, 2025
Merged
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
26 changes: 19 additions & 7 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ impl<const LIMBS: usize> Int<LIMBS> {
}

/// Borrow the inner limbs as a mutable array of [`Word`]s.
pub fn as_words_mut(&mut self) -> &mut [Word; LIMBS] {
self.0.as_words_mut()
pub fn as_mut_words(&mut self) -> &mut [Word; LIMBS] {
self.0.as_mut_words()
}

/// Borrow the inner limbs as a mutable slice of [`Word`]s.
#[deprecated(since = "0.7.0", note = "please use `as_mut_words` instead")]
pub fn as_words_mut(&mut self) -> &mut [Word] {
self.as_mut_words()
}

/// Borrow the limbs of this [`Int`].
Expand All @@ -119,8 +125,14 @@ impl<const LIMBS: usize> Int<LIMBS> {
}

/// Borrow the limbs of this [`Int`] mutably.
pub const fn as_limbs_mut(&mut self) -> &mut [Limb; LIMBS] {
self.0.as_limbs_mut()
pub const fn as_mut_limbs(&mut self) -> &mut [Limb; LIMBS] {
self.0.as_mut_limbs()
}

/// Borrow the limbs of this [`Int`] mutably.
#[deprecated(since = "0.7.0", note = "please use `as_mut_limbs` instead")]
pub const fn as_limbs_mut(&mut self) -> &mut [Limb] {
self.as_mut_limbs()
}

/// Convert this [`Int`] into its inner limbs.
Expand Down Expand Up @@ -171,7 +183,7 @@ impl<const LIMBS: usize> AsRef<[Word; LIMBS]> for Int<LIMBS> {

impl<const LIMBS: usize> AsMut<[Word; LIMBS]> for Int<LIMBS> {
fn as_mut(&mut self) -> &mut [Word; LIMBS] {
self.as_words_mut()
self.as_mut_words()
}
}

Expand All @@ -183,7 +195,7 @@ impl<const LIMBS: usize> AsRef<[Limb]> for Int<LIMBS> {

impl<const LIMBS: usize> AsMut<[Limb]> for Int<LIMBS> {
fn as_mut(&mut self) -> &mut [Limb] {
self.as_limbs_mut()
self.as_mut_limbs()
}
}

Expand Down Expand Up @@ -313,7 +325,7 @@ mod tests {
#[test]
fn as_words_mut() {
let mut n = I128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
assert_eq!(n.as_words_mut(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
assert_eq!(n.as_mut_words(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
}

#[cfg(feature = "alloc")]
Expand Down
6 changes: 3 additions & 3 deletions src/modular/boxed_monty_form/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'a> BoxedMontyMultiplier<'a> {

self.clear_product();
almost_montgomery_mul_by_one(
self.product.as_limbs_mut(),
self.product.as_mut_limbs(),
a.as_limbs(),
self.modulus.as_limbs(),
self.mod_neg_inv,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> BoxedMontyMultiplier<'a> {

self.clear_product();
almost_montgomery_mul(
self.product.as_limbs_mut(),
self.product.as_mut_limbs(),
a.as_limbs(),
b.as_limbs(),
self.modulus.as_limbs(),
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<'a> BoxedMontyMultiplier<'a> {
// TODO(tarcieri): optimized implementation
self.clear_product();
almost_montgomery_mul(
self.product.as_limbs_mut(),
self.product.as_mut_limbs(),
a.as_limbs(),
a.as_limbs(),
self.modulus.as_limbs(),
Expand Down
2 changes: 1 addition & 1 deletion src/modular/safegcd/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ impl BoxedUnsatInt {
&self.0,
Word,
Word::BITS as usize,
ret.as_words_mut()
ret.as_mut_words()
);

if shorten {
Expand Down
22 changes: 17 additions & 5 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,36 @@ impl<const LIMBS: usize> Uint<LIMBS> {
}

/// Borrow the inner limbs as a mutable array of [`Word`]s.
pub fn as_words_mut(&mut self) -> &mut [Word; LIMBS] {
pub const fn as_mut_words(&mut self) -> &mut [Word; LIMBS] {
// SAFETY: `Limb` is a `repr(transparent)` newtype for `Word`
#[allow(unsafe_code)]
unsafe {
&mut *self.limbs.as_mut_ptr().cast()
}
}

/// Borrow the inner limbs as a mutable slice of [`Word`]s.
#[deprecated(since = "0.7.0", note = "please use `as_mut_words` instead")]
pub const fn as_words_mut(&mut self) -> &mut [Word] {
self.as_mut_words()
}

/// Borrow the limbs of this [`Uint`].
pub const fn as_limbs(&self) -> &[Limb; LIMBS] {
&self.limbs
}

/// Borrow the limbs of this [`Uint`] mutably.
pub const fn as_limbs_mut(&mut self) -> &mut [Limb; LIMBS] {
pub const fn as_mut_limbs(&mut self) -> &mut [Limb; LIMBS] {
&mut self.limbs
}

/// Borrow the limbs of this [`Uint`] mutably.
#[deprecated(since = "0.7.0", note = "please use `as_mut_limbs` instead")]
pub const fn as_limbs_mut(&mut self) -> &mut [Limb] {
self.as_mut_limbs()
}

/// Convert this [`Uint`] into its inner limbs.
pub const fn to_limbs(self) -> [Limb; LIMBS] {
self.limbs
Expand Down Expand Up @@ -202,7 +214,7 @@ impl<const LIMBS: usize> AsRef<[Word; LIMBS]> for Uint<LIMBS> {

impl<const LIMBS: usize> AsMut<[Word; LIMBS]> for Uint<LIMBS> {
fn as_mut(&mut self) -> &mut [Word; LIMBS] {
self.as_words_mut()
self.as_mut_words()
}
}

Expand All @@ -214,7 +226,7 @@ impl<const LIMBS: usize> AsRef<[Limb]> for Uint<LIMBS> {

impl<const LIMBS: usize> AsMut<[Limb]> for Uint<LIMBS> {
fn as_mut(&mut self) -> &mut [Limb] {
self.as_limbs_mut()
self.as_mut_limbs()
}
}

Expand Down Expand Up @@ -497,7 +509,7 @@ mod tests {
#[test]
fn as_words_mut() {
let mut n = U128::from_be_hex("AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD");
assert_eq!(n.as_words_mut(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
assert_eq!(n.as_mut_words(), &[0xCCCCCCCCDDDDDDDD, 0xAAAAAAAABBBBBBBB]);
}

#[cfg(feature = "alloc")]
Expand Down
20 changes: 16 additions & 4 deletions src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,36 @@ impl BoxedUint {
}

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

/// Borrow the inner limbs as a mutable slice of [`Word`]s.
#[deprecated(since = "0.7.0", note = "please use `as_mut_words` instead")]
pub fn as_words_mut(&mut self) -> &mut [Word] {
self.as_mut_words()
}

/// Borrow the limbs of this [`BoxedUint`].
pub fn as_limbs(&self) -> &[Limb] {
self.limbs.as_ref()
}

/// Borrow the limbs of this [`BoxedUint`] mutably.
pub fn as_limbs_mut(&mut self) -> &mut [Limb] {
pub fn as_mut_limbs(&mut self) -> &mut [Limb] {
self.limbs.as_mut()
}

/// Borrow the limbs of this [`BoxedUint`] mutably.
#[deprecated(since = "0.7.0", note = "please use `as_mut_limbs` instead")]
pub fn as_limbs_mut(&mut self) -> &mut [Limb] {
self.as_mut_limbs()
}

/// Convert this [`BoxedUint`] into its inner limbs.
pub fn to_limbs(&self) -> Box<[Limb]> {
self.limbs.clone()
Expand Down Expand Up @@ -276,7 +288,7 @@ impl AsRef<[Word]> for BoxedUint {

impl AsMut<[Word]> for BoxedUint {
fn as_mut(&mut self) -> &mut [Word] {
self.as_words_mut()
self.as_mut_words()
}
}

Expand All @@ -288,7 +300,7 @@ impl AsRef<[Limb]> for BoxedUint {

impl AsMut<[Limb]> for BoxedUint {
fn as_mut(&mut self) -> &mut [Limb] {
self.as_limbs_mut()
self.as_mut_limbs()
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/uint/boxed/inv_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl BoxedUint {
let x_i = b.limbs[0].0 & 1;
let x_i_choice = Choice::from(x_i as u8);
// b_{i+1} = (b_i - a * X_i) / 2
b_opt.as_words_mut().copy_from_slice(b.as_words());
b_opt.as_mut_words().copy_from_slice(b.as_words());
b_opt.wrapping_sub_assign(self);
b.ct_assign(&b_opt, x_i_choice);
b.shr1_assign();
Expand Down Expand Up @@ -97,7 +97,7 @@ impl BoxedUint {
let x_i = b.limbs[0].0 & 1;
let x_i_choice = Choice::from(x_i as u8);
// b_{i+1} = (b_i - a * X_i) / 2
b_opt.as_words_mut().copy_from_slice(b.as_words());
b_opt.as_mut_words().copy_from_slice(b.as_words());
b_opt.wrapping_sub_assign(self);
b.ct_assign(&b_opt, x_i_choice);
b.shr1_assign();
Expand Down
2 changes: 1 addition & 1 deletion src/uint/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
#[cfg(feature = "alloc")]
pub fn to_string_radix_vartime(&self, radix: u32) -> String {
let mut buf = *self;
radix_encode_limbs_mut_to_string(radix, buf.as_limbs_mut())
radix_encode_limbs_mut_to_string(radix, buf.as_mut_limbs())
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/uint/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ mod tests {

let bit_length = 989;
let mut val = U1024::ZERO;
random_bits_core(&mut rng, val.as_limbs_mut(), bit_length).expect("safe");
random_bits_core(&mut rng, val.as_mut_limbs(), bit_length).expect("safe");

assert_eq!(
val,
Expand Down Expand Up @@ -298,8 +298,8 @@ mod tests {
let mut rng = get_four_sequential_rng();
let mut first = U1024::ZERO;
let mut second = U1024::ZERO;
random_bits_core(&mut rng, first.as_limbs_mut(), bit_length).expect("safe");
random_bits_core(&mut rng, second.as_limbs_mut(), U1024::BITS - bit_length)
random_bits_core(&mut rng, first.as_mut_limbs(), bit_length).expect("safe");
random_bits_core(&mut rng, second.as_mut_limbs(), U1024::BITS - bit_length)
.expect("safe");
assert_eq!(second.shl(bit_length).bitor(&first), RANDOM_OUTPUT);
}
Expand Down