Skip to content

Commit 553e72e

Browse files
authored
Additional methods for Integer and Monty (#533)
* Add `Integer::from_limb_like()`, `one_like()`, `zero_like()`. * Add `Monty::params()` and `as_montgomery()`
1 parent 793d098 commit 553e72e

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

src/modular/boxed_monty_form.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ impl Monty for BoxedMontyForm {
256256
BoxedMontyForm::one(params)
257257
}
258258

259+
fn params(&self) -> &Self::Params {
260+
&self.params
261+
}
262+
263+
fn as_montgomery(&self) -> &Self::Integer {
264+
&self.montgomery_form
265+
}
266+
259267
fn div_by_2(&self) -> Self {
260268
BoxedMontyForm::div_by_2(self)
261269
}

src/modular/monty_form.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ impl<const LIMBS: usize> Monty for MontyForm<LIMBS> {
250250
MontyForm::one(params)
251251
}
252252

253+
fn params(&self) -> &Self::Params {
254+
&self.params
255+
}
256+
257+
fn as_montgomery(&self) -> &Self::Integer {
258+
&self.montgomery_form
259+
}
260+
253261
fn div_by_2(&self) -> Self {
254262
MontyForm::div_by_2(self)
255263
}

src/traits.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ pub trait Integer:
158158
/// The value `1`.
159159
fn one() -> Self;
160160

161+
/// The value `1` with the same precision as `other`.
162+
fn one_like(other: &Self) -> Self {
163+
Self::from_limb_like(Limb::ONE, other)
164+
}
165+
166+
/// Returns an integer with the first limb set to `limb`, and the same precision as `other`.
167+
fn from_limb_like(limb: Limb, other: &Self) -> Self;
168+
161169
/// Number of limbs in this integer.
162170
fn nlimbs(&self) -> usize;
163171

@@ -244,6 +252,16 @@ pub trait Zero: ConstantTimeEq + Sized {
244252
fn set_zero(&mut self) {
245253
*self = Zero::zero();
246254
}
255+
256+
/// Return the value `0` with the same precision as `other`.
257+
fn zero_like(other: &Self) -> Self
258+
where
259+
Self: Clone,
260+
{
261+
let mut ret = other.clone();
262+
ret.set_zero();
263+
ret
264+
}
247265
}
248266

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

808+
/// Returns the parameter struct used to initialize this object.
809+
fn params(&self) -> &Self::Params;
810+
811+
/// Access the value in Montgomery form.
812+
fn as_montgomery(&self) -> &Self::Integer;
813+
790814
/// Performs division by 2, that is returns `x` such that `x + x = self`.
791815
fn div_by_2(&self) -> Self;
792816
}

src/uint.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ impl<const LIMBS: usize> Integer for Uint<LIMBS> {
249249
Self::ONE
250250
}
251251

252+
fn from_limb_like(limb: Limb, _other: &Self) -> Self {
253+
Self::from(limb)
254+
}
255+
252256
fn nlimbs(&self) -> usize {
253257
Self::LIMBS
254258
}

src/uint/boxed.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ impl Integer for BoxedUint {
307307
Self::one()
308308
}
309309

310+
fn from_limb_like(limb: Limb, other: &Self) -> Self {
311+
let mut ret = Self::zero_with_precision(other.bits_precision());
312+
ret.limbs[0] = limb;
313+
ret
314+
}
315+
310316
fn nlimbs(&self) -> usize {
311317
self.nlimbs()
312318
}

0 commit comments

Comments
 (0)