Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional methods for Integer and Monty #533

Merged
merged 3 commits into from
Dec 29, 2023
Merged
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
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)
}
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 @@ -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;
tarcieri marked this conversation as resolved.
Show resolved Hide resolved

/// 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