Skip to content
Open
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
9 changes: 7 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod div;
mod div_uint;
mod encoding;
mod from;
mod gcd;
pub(crate) mod gcd;
mod invert_mod;
mod mod_symbol;
mod mul;
Expand Down Expand Up @@ -151,11 +151,16 @@ impl<const LIMBS: usize> Int<LIMBS> {
ConstCtOption::new(NonZero(self), self.0.is_nonzero())
}

/// Whether this [`Int`] is odd.
pub const fn is_odd(self) -> ConstChoice {
self.0.is_odd()
}

/// Convert to a [`Odd<Int<LIMBS>>`].
///
/// Returns some if the original value is odd, and false otherwise.
pub const fn to_odd(self) -> ConstCtOption<Odd<Self>> {
ConstCtOption::new(Odd(self), self.0.is_odd())
ConstCtOption::new(Odd(self), self.is_odd())
}

/// Interpret the data in this object as a [`Uint`] instead.
Expand Down
14 changes: 7 additions & 7 deletions src/int/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ use crate::{ConstChoice, Int, Uint};
impl<const LIMBS: usize> Int<LIMBS> {
/// Return `b` if `c` is truthy, otherwise return `a`.
#[inline]
pub(crate) const fn select(a: &Self, b: &Self, c: ConstChoice) -> Self {
pub const fn select(a: &Self, b: &Self, c: ConstChoice) -> Self {
Self(Uint::select(&a.0, &b.0, c))
}

/// Swap `a` and `b` if `c` is truthy, otherwise, do nothing.
#[inline]
pub(crate) const fn conditional_swap(a: &mut Self, b: &mut Self, c: ConstChoice) {
pub const fn conditional_swap(a: &mut Self, b: &mut Self, c: ConstChoice) {
Uint::conditional_swap(&mut a.0, &mut b.0, c);
}

/// Returns the truthy value if `self`!=0 or the falsy value otherwise.
#[inline]
pub(crate) const fn is_nonzero(&self) -> ConstChoice {
pub const fn is_nonzero(&self) -> ConstChoice {
Uint::is_nonzero(&self.0)
}

/// Returns the truthy value if `self == rhs` or the falsy value otherwise.
#[inline]
pub(crate) const fn eq(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn eq(lhs: &Self, rhs: &Self) -> ConstChoice {
Uint::eq(&lhs.0, &rhs.0)
}

/// Returns the truthy value if `self < rhs` and the falsy value otherwise.
#[inline]
pub(crate) const fn lt(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn lt(lhs: &Self, rhs: &Self) -> ConstChoice {
Uint::lt(&lhs.invert_msb().0, &rhs.invert_msb().0)
}

/// Returns the truthy value if `self > rhs` and the falsy value otherwise.
#[inline]
pub(crate) const fn gt(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn gt(lhs: &Self, rhs: &Self) -> ConstChoice {
Uint::gt(&lhs.invert_msb().0, &rhs.invert_msb().0)
}

Expand All @@ -51,7 +51,7 @@ impl<const LIMBS: usize> Int<LIMBS> {
/// 0 is Equal
/// 1 is Greater
#[inline]
pub(crate) const fn cmp(lhs: &Self, rhs: &Self) -> i8 {
pub const fn cmp(lhs: &Self, rhs: &Self) -> i8 {
Uint::cmp(&lhs.invert_msb().0, &rhs.invert_msb().0)
}

Expand Down
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,21 @@ pub use crate::uint::boxed::BoxedUint;
pub use crate::{
checked::Checked,
const_choice::{ConstChoice, ConstCtOption},
int::{types::*, *},
int::{
gcd::{IntXgcdOutput, NonZeroIntXgcdOutput, OddIntXgcdOutput},
types::*,
*,
},
jacobi::JacobiSymbol,
limb::{Limb, WideWord, Word},
non_zero::*,
odd::*,
traits::*,
uint::{div_limb::Reciprocal, *},
uint::div_limb::{Reciprocal, div3by2},
uint::{
gcd::{NonZeroUintXgcdOutput, OddUintXgcdOutput, UintXgcdOutput},
*,
},
wrapping::Wrapping,
};

Expand Down
4 changes: 2 additions & 2 deletions src/limb/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Limb {

/// Return `b` if `c` is truthy, otherwise return `a`.
#[inline]
pub(crate) const fn select(a: Self, b: Self, c: ConstChoice) -> Self {
pub const fn select(a: Self, b: Self, c: ConstChoice) -> Self {
Self(c.select_word(a.0, b.0))
}

Expand All @@ -40,7 +40,7 @@ impl Limb {

/// Returns the truthy value if `self != 0` and the falsy value otherwise.
#[inline]
pub(crate) const fn is_nonzero(&self) -> ConstChoice {
pub const fn is_nonzero(&self) -> ConstChoice {
ConstChoice::from_word_nonzero(self.0)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {

/// Borrow the limbs of this [`Uint`] as a [`UintRef`].
#[inline(always)]
pub(crate) const fn as_uint_ref(&self) -> &UintRef {
pub const fn as_uint_ref(&self) -> &UintRef {
UintRef::new(&self.limbs)
}

/// Mutably borrow the limbs of this [`Uint`] as a [`UintRef`].
#[inline(always)]
pub(crate) const fn as_mut_uint_ref(&mut self) -> &mut UintRef {
pub const fn as_mut_uint_ref(&mut self) -> &mut UintRef {
UintRef::new_mut(&mut self.limbs)
}

Expand Down
23 changes: 14 additions & 9 deletions src/uint/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::Uint;
impl<const LIMBS: usize> Uint<LIMBS> {
/// Return `b` if `c` is truthy, otherwise return `a`.
#[inline]
pub(crate) const fn select(a: &Self, b: &Self, c: ConstChoice) -> Self {
pub const fn select(a: &Self, b: &Self, c: ConstChoice) -> Self {
let mut limbs = [Limb::ZERO; LIMBS];

let mut i = 0;
Expand All @@ -27,7 +27,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {

/// Swap `a` and `b` if `c` is truthy, otherwise, do nothing.
#[inline]
pub(crate) const fn conditional_swap(a: &mut Self, b: &mut Self, c: ConstChoice) {
pub const fn conditional_swap(a: &mut Self, b: &mut Self, c: ConstChoice) {
let mut i = 0;
let a = a.as_mut_limbs();
let b = b.as_mut_limbs();
Expand All @@ -39,13 +39,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {

/// Swap `a` and `b`
#[inline]
pub(crate) const fn swap(a: &mut Self, b: &mut Self) {
pub const fn swap(a: &mut Self, b: &mut Self) {
Self::conditional_swap(a, b, ConstChoice::TRUE)
}

/// Returns the truthy value if `self`!=0 or the falsy value otherwise.
#[inline]
pub(crate) const fn is_nonzero(&self) -> ConstChoice {
pub const fn is_nonzero(&self) -> ConstChoice {
let mut b = 0;
let mut i = 0;
while i < LIMBS {
Expand Down Expand Up @@ -73,9 +73,14 @@ impl<const LIMBS: usize> Uint<LIMBS> {
ConstChoice::from_word_lsb(self.limbs[0].0 & 1)
}

/// Returns the truthy value if `self` is odd or the falsy value otherwise.
pub const fn is_odd_const(&self) -> ConstChoice {
self.is_odd()
}

/// Returns the truthy value if `self == rhs` or the falsy value otherwise.
#[inline]
pub(crate) const fn eq(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn eq(lhs: &Self, rhs: &Self) -> ConstChoice {
let mut acc = 0;
let mut i = 0;

Expand All @@ -90,7 +95,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {

/// Returns the truthy value if `self < rhs` and the falsy value otherwise.
#[inline]
pub(crate) const fn lt(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn lt(lhs: &Self, rhs: &Self) -> ConstChoice {
// We could use the same approach as in Limb::ct_lt(),
// but since we have to use Uint::wrapping_sub(), which calls `borrowing_sub()`,
// there are no savings compared to just calling `borrowing_sub()` directly.
Expand All @@ -100,13 +105,13 @@ impl<const LIMBS: usize> Uint<LIMBS> {

/// Returns the truthy value if `self <= rhs` and the falsy value otherwise.
#[inline]
pub(crate) const fn lte(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn lte(lhs: &Self, rhs: &Self) -> ConstChoice {
Self::gt(lhs, rhs).not()
}

/// Returns the truthy value if `self > rhs` and the falsy value otherwise.
#[inline]
pub(crate) const fn gt(lhs: &Self, rhs: &Self) -> ConstChoice {
pub const fn gt(lhs: &Self, rhs: &Self) -> ConstChoice {
let (_res, borrow) = rhs.borrowing_sub(lhs, Limb::ZERO);
ConstChoice::from_word_mask(borrow.0)
}
Expand All @@ -117,7 +122,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
/// 0 is Equal
/// 1 is Greater
#[inline]
pub(crate) const fn cmp(lhs: &Self, rhs: &Self) -> i8 {
pub const fn cmp(lhs: &Self, rhs: &Self) -> i8 {
let mut i = 0;
let mut borrow = Limb::ZERO;
let mut diff = Limb::ZERO;
Expand Down
8 changes: 1 addition & 7 deletions src/uint/div_limb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ pub(crate) const fn div2by1(u1: Word, u0: Word, reciprocal: &Reciprocal) -> (Wor
/// In place of `v1` takes its reciprocal, and assumes that `v` was already pre-shifted
/// so that v1 has its most significant bit set (that is, the reciprocal's `shift` is 0).
#[inline(always)]
pub(crate) const fn div3by2(
u2: Word,
u1: Word,
u0: Word,
v1_reciprocal: &Reciprocal,
v0: Word,
) -> Word {
pub const fn div3by2(u2: Word, u1: Word, u0: Word, v1_reciprocal: &Reciprocal, v0: Word) -> Word {
Copy link
Contributor

Choose a reason for hiding this comment

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

Where do you use this function? It seems pretty specialized to me, with a bunch of assumptions about values coming from other parts of the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in vartime division. If we get that merged, I wouldn't need to export it

debug_assert!(v1_reciprocal.shift == 0);
debug_assert!(u2 <= v1_reciprocal.divisor_normalized);

Expand Down
2 changes: 1 addition & 1 deletion src/uint/shl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
/// Computes `self << 1` in constant-time, returning [`ConstChoice::TRUE`]
/// if the most significant bit was set, and [`ConstChoice::FALSE`] otherwise.
#[inline(always)]
pub(crate) const fn overflowing_shl1(&self) -> (Self, Limb) {
pub const fn overflowing_shl1(&self) -> (Self, Limb) {
self.carrying_shl1(Limb::ZERO)
}

Expand Down
2 changes: 1 addition & 1 deletion src/uint/shr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
}

/// Computes `self >> 1` in constant-time.
pub(crate) const fn shr1(&self) -> Self {
pub const fn shr1(&self) -> Self {
self.shr1_with_carry().0
}

Expand Down
Loading