|
1 | 1 | //! [`Uint`] division operations. |
2 | 2 |
|
3 | | -use super::div_limb::{div_rem_limb_with_reciprocal, Reciprocal}; |
4 | | -use crate::{CheckedDiv, ConstChoice, Limb, NonZero, Uint, Word, Wrapping}; |
| 3 | +use super::div_limb::{div_rem_limb_with_reciprocal, rem_limb_with_reciprocal, Reciprocal}; |
| 4 | +use crate::{CheckedDiv, ConstChoice, DivRemLimb, Limb, NonZero, RemLimb, Uint, Word, Wrapping}; |
5 | 5 | use core::ops::{Div, DivAssign, Rem, RemAssign}; |
6 | 6 | use subtle::CtOption; |
7 | 7 |
|
8 | 8 | impl<const LIMBS: usize> Uint<LIMBS> { |
9 | | - /// Computes `self` / `rhs` using a pre-made reciprocal, |
| 9 | + /// Computes `self / rhs` using a pre-made reciprocal, |
10 | 10 | /// returns the quotient (q) and remainder (r). |
11 | 11 | #[inline(always)] |
12 | 12 | pub const fn div_rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> (Self, Limb) { |
13 | 13 | div_rem_limb_with_reciprocal(self, reciprocal) |
14 | 14 | } |
15 | 15 |
|
16 | | - /// Computes `self` / `rhs`, returns the quotient (q) and remainder (r). |
| 16 | + /// Computes `self / rhs`, returns the quotient (q) and remainder (r). |
17 | 17 | #[inline(always)] |
18 | 18 | pub const fn div_rem_limb(&self, rhs: NonZero<Limb>) -> (Self, Limb) { |
19 | 19 | div_rem_limb_with_reciprocal(self, &Reciprocal::new(rhs)) |
20 | 20 | } |
21 | 21 |
|
| 22 | + /// Computes `self % rhs` using a pre-made reciprocal. |
| 23 | + #[inline(always)] |
| 24 | + pub const fn rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> Limb { |
| 25 | + rem_limb_with_reciprocal(self, reciprocal) |
| 26 | + } |
| 27 | + |
| 28 | + /// Computes `self % rhs`. |
| 29 | + #[inline(always)] |
| 30 | + pub const fn rem_limb(&self, rhs: NonZero<Limb>) -> Limb { |
| 31 | + rem_limb_with_reciprocal(self, &Reciprocal::new(rhs)) |
| 32 | + } |
| 33 | + |
22 | 34 | /// Computes `self` / `rhs`, returns the quotient (q) and the remainder (r) |
23 | 35 | /// |
24 | 36 | /// This function is constant-time with respect to both `self` and `rhs`. |
@@ -84,7 +96,7 @@ impl<const LIMBS: usize> Uint<LIMBS> { |
84 | 96 |
|
85 | 97 | /// Computes `self` % `rhs`, returns the remainder. |
86 | 98 | pub const fn rem(&self, rhs: &NonZero<Self>) -> Self { |
87 | | - self.div_rem_vartime(rhs).1 |
| 99 | + self.div_rem(rhs).1 |
88 | 100 | } |
89 | 101 |
|
90 | 102 | /// Computes `self` % `rhs`, returns the remainder in variable-time with respect to `rhs`. |
@@ -211,7 +223,7 @@ impl<const LIMBS: usize> Uint<LIMBS> { |
211 | 223 | /// Perform checked reduction, returning a [`CtOption`] which `is_some` |
212 | 224 | /// only if the rhs != 0 |
213 | 225 | pub fn checked_rem(&self, rhs: &Self) -> CtOption<Self> { |
214 | | - NonZero::new(*rhs).map(|rhs| self.rem_vartime(&rhs)) |
| 226 | + NonZero::new(*rhs).map(|rhs| self.rem(&rhs)) |
215 | 227 | } |
216 | 228 | } |
217 | 229 |
|
@@ -584,6 +596,18 @@ impl<const LIMBS: usize> RemAssign<&NonZero<Uint<LIMBS>>> for Wrapping<Uint<LIMB |
584 | 596 | } |
585 | 597 | } |
586 | 598 |
|
| 599 | +impl<const LIMBS: usize> DivRemLimb for Uint<LIMBS> { |
| 600 | + fn div_rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> (Self, Limb) { |
| 601 | + Self::div_rem_limb_with_reciprocal(self, reciprocal) |
| 602 | + } |
| 603 | +} |
| 604 | + |
| 605 | +impl<const LIMBS: usize> RemLimb for Uint<LIMBS> { |
| 606 | + fn rem_limb_with_reciprocal(&self, reciprocal: &Reciprocal) -> Limb { |
| 607 | + Self::rem_limb_with_reciprocal(self, reciprocal) |
| 608 | + } |
| 609 | +} |
| 610 | + |
587 | 611 | #[cfg(test)] |
588 | 612 | mod tests { |
589 | 613 | use crate::{Limb, NonZero, Uint, Word, U256}; |
|
0 commit comments