Skip to content

Commit

Permalink
Additional methods for Integer and Monty (#533)
Browse files Browse the repository at this point in the history
* Add `Integer::from_limb_like()`, `one_like()`, `zero_like()`.
* Add `Monty::params()` and `as_montgomery()`
  • Loading branch information
fjarri authored Dec 29, 2023
1 parent 793d098 commit 553e72e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ impl Monty for BoxedMontyForm {
BoxedMontyForm::one(params)
}

fn params(&self) -> &Self::Params {
&self.params
}

fn as_montgomery(&self) -> &Self::Integer {
&self.montgomery_form
}

fn div_by_2(&self) -> Self {
BoxedMontyForm::div_by_2(self)
}
Expand Down
8 changes: 8 additions & 0 deletions src/modular/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ impl<const LIMBS: usize> Monty for MontyForm<LIMBS> {
MontyForm::one(params)
}

fn params(&self) -> &Self::Params {
&self.params
}

fn as_montgomery(&self) -> &Self::Integer {
&self.montgomery_form
}

fn div_by_2(&self) -> Self {
MontyForm::div_by_2(self)
}
Expand Down
24 changes: 24 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ pub trait Integer:
/// The value `1`.
fn one() -> Self;

/// The value `1` with the same precision as `other`.
fn one_like(other: &Self) -> Self {
Self::from_limb_like(Limb::ONE, other)
}

/// Returns an integer with the first limb set to `limb`, and the same precision as `other`.
fn from_limb_like(limb: Limb, other: &Self) -> Self;

/// Number of limbs in this integer.
fn nlimbs(&self) -> usize;

Expand Down Expand Up @@ -244,6 +252,16 @@ pub trait Zero: ConstantTimeEq + Sized {
fn set_zero(&mut self) {
*self = Zero::zero();
}

/// Return the value `0` with the same precision as `other`.
fn zero_like(other: &Self) -> Self
where
Self: Clone,
{
let mut ret = other.clone();
ret.set_zero();
ret
}
}

/// Trait for associating a constant representing zero.
Expand Down Expand Up @@ -787,6 +805,12 @@ pub trait Monty:
/// Returns one in this representation.
fn one(params: Self::Params) -> Self;

/// Returns the parameter struct used to initialize this object.
fn params(&self) -> &Self::Params;

/// Access the value in Montgomery form.
fn as_montgomery(&self) -> &Self::Integer;

/// Performs division by 2, that is returns `x` such that `x + x = self`.
fn div_by_2(&self) -> Self;
}
4 changes: 4 additions & 0 deletions src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
Self::ONE
}

fn from_limb_like(limb: Limb, _other: &Self) -> Self {
Self::from(limb)
}

fn nlimbs(&self) -> usize {
Self::LIMBS
}
Expand Down
6 changes: 6 additions & 0 deletions src/uint/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ impl Integer for BoxedUint {
Self::one()
}

fn from_limb_like(limb: Limb, other: &Self) -> Self {
let mut ret = Self::zero_with_precision(other.bits_precision());
ret.limbs[0] = limb;
ret
}

fn nlimbs(&self) -> usize {
self.nlimbs()
}
Expand Down

0 comments on commit 553e72e

Please sign in to comment.