Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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
19 changes: 19 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,19 @@ pub trait Integer:
/// The value `1`.
fn one() -> Self;

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

/// The value `1` with the same precision as `other`.
fn one_like(other: &Self) -> Self {
Self::from_limb_like(Limb::ONE, other)
}
Comment on lines +161 to +164
Copy link
Member

Choose a reason for hiding this comment

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

I've wondered if we should have a One trait, similar to num_traits::One.

The only reason we don't use num_traits::One is so is_one can return Choice, as it were.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems logical to have such a trait. If we're redefining num_traits::Zero, might as well redefine One too.


/// 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 @@ -787,6 +800,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