From 6ad210706b824f4ab02f23d4932f93f84225ed26 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 23 Apr 2025 15:14:21 -0600 Subject: [PATCH] Rename `as_(limbs|words)_mut` => `as_mut_(limbs|words)` This better follows Rust conventions like `as_mut_slice`, where the ordering is like Rust's `mut T`. The old names are retained, but deprecated. --- src/int.rs | 26 +++++++++++++++++++------- src/modular/boxed_monty_form/mul.rs | 6 +++--- src/modular/safegcd/boxed.rs | 2 +- src/uint.rs | 22 +++++++++++++++++----- src/uint/boxed.rs | 20 ++++++++++++++++---- src/uint/boxed/inv_mod.rs | 4 ++-- src/uint/encoding.rs | 2 +- src/uint/rand.rs | 6 +++--- 8 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/int.rs b/src/int.rs index 553529db..a964cc8a 100644 --- a/src/int.rs +++ b/src/int.rs @@ -109,8 +109,14 @@ impl Int { } /// 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`]. @@ -119,8 +125,14 @@ impl Int { } /// 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. @@ -171,7 +183,7 @@ impl AsRef<[Word; LIMBS]> for Int { impl AsMut<[Word; LIMBS]> for Int { fn as_mut(&mut self) -> &mut [Word; LIMBS] { - self.as_words_mut() + self.as_mut_words() } } @@ -183,7 +195,7 @@ impl AsRef<[Limb]> for Int { impl AsMut<[Limb]> for Int { fn as_mut(&mut self) -> &mut [Limb] { - self.as_limbs_mut() + self.as_mut_limbs() } } @@ -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")] diff --git a/src/modular/boxed_monty_form/mul.rs b/src/modular/boxed_monty_form/mul.rs index d87d1ffd..c53d8e22 100644 --- a/src/modular/boxed_monty_form/mul.rs +++ b/src/modular/boxed_monty_form/mul.rs @@ -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, @@ -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(), @@ -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(), diff --git a/src/modular/safegcd/boxed.rs b/src/modular/safegcd/boxed.rs index 78f8cf15..934768c8 100644 --- a/src/modular/safegcd/boxed.rs +++ b/src/modular/safegcd/boxed.rs @@ -334,7 +334,7 @@ impl BoxedUnsatInt { &self.0, Word, Word::BITS as usize, - ret.as_words_mut() + ret.as_mut_words() ); if shorten { diff --git a/src/uint.rs b/src/uint.rs index 417056c9..5802d757 100644 --- a/src/uint.rs +++ b/src/uint.rs @@ -151,7 +151,7 @@ impl Uint { } /// 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 { @@ -159,16 +159,28 @@ impl Uint { } } + /// 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 @@ -202,7 +214,7 @@ impl AsRef<[Word; LIMBS]> for Uint { impl AsMut<[Word; LIMBS]> for Uint { fn as_mut(&mut self) -> &mut [Word; LIMBS] { - self.as_words_mut() + self.as_mut_words() } } @@ -214,7 +226,7 @@ impl AsRef<[Limb]> for Uint { impl AsMut<[Limb]> for Uint { fn as_mut(&mut self) -> &mut [Limb] { - self.as_limbs_mut() + self.as_mut_limbs() } } @@ -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")] diff --git a/src/uint/boxed.rs b/src/uint/boxed.rs index 946350e1..7379240d 100644 --- a/src/uint/boxed.rs +++ b/src/uint/boxed.rs @@ -142,7 +142,7 @@ 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 { @@ -150,16 +150,28 @@ impl BoxedUint { } } + /// 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() @@ -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() } } @@ -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() } } diff --git a/src/uint/boxed/inv_mod.rs b/src/uint/boxed/inv_mod.rs index 3f34d7d0..1cf881df 100644 --- a/src/uint/boxed/inv_mod.rs +++ b/src/uint/boxed/inv_mod.rs @@ -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(); @@ -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(); diff --git a/src/uint/encoding.rs b/src/uint/encoding.rs index 21b4715c..bdf13686 100644 --- a/src/uint/encoding.rs +++ b/src/uint/encoding.rs @@ -202,7 +202,7 @@ impl Uint { #[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()) } } diff --git a/src/uint/rand.rs b/src/uint/rand.rs index 39f28563..774f560d 100644 --- a/src/uint/rand.rs +++ b/src/uint/rand.rs @@ -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, @@ -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); }