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

In-place cloning of Montgomery form #566

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/modular/boxed_monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ impl BoxedMontyForm {
self.montgomery_form.clone()
}

/// Directly clone from another boxed monty. This method assumes that other has the same
/// parameter as self and will not check
pub fn clone_from_montgomery(&mut self, other: &Self) {
self.montgomery_form
.limbs
.copy_from_slice(other.montgomery_form.as_limbs())
}

/// Performs division by 2, that is returns `x` such that `x + x = self`.
pub fn div_by_2(&self) -> Self {
Self {
Expand Down Expand Up @@ -264,6 +272,10 @@ impl Monty for BoxedMontyForm {
&self.montgomery_form
}

fn clone_from_montgomery(&mut self, other: &Self) {
self.clone_from_montgomery(other)
}

fn div_by_2(&self) -> Self {
BoxedMontyForm::div_by_2(self)
}
Expand Down Expand Up @@ -300,4 +312,16 @@ mod tests {
assert_eq!(zero.div_by_2(), zero);
assert_eq!(one.div_by_2().mul(&two), one);
}

#[test]
fn inplace_cloning() {
let modulus = Odd::new(BoxedUint::from(9u8)).unwrap();
let params = BoxedMontyParams::new(modulus);
let zero = BoxedMontyForm::zero(params.clone());
let mut target = BoxedMontyForm::one(params.clone());

assert_ne!(target, zero);
target.clone_from_montgomery(&zero);
assert_eq!(target, zero);
}
}
28 changes: 28 additions & 0 deletions src/modular/monty_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ impl<const LIMBS: usize> MontyForm<LIMBS> {
self.montgomery_form
}

/// In-place cloning from another monty, assuming that the other has the same parameter as self
pub fn clone_from_montgomery(&mut self, other: &Self) {
self.montgomery_form
.limbs
.copy_from_slice(other.montgomery_form.as_limbs())
}

/// Performs division by 2, that is returns `x` such that `x + x = self`.
pub const fn div_by_2(&self) -> Self {
Self {
Expand Down Expand Up @@ -260,6 +267,10 @@ impl<const LIMBS: usize> Monty for MontyForm<LIMBS> {
&self.montgomery_form
}

fn clone_from_montgomery(&mut self, other: &Self) {
self.clone_from_montgomery(other)
}

fn div_by_2(&self) -> Self {
MontyForm::div_by_2(self)
}
Expand Down Expand Up @@ -302,3 +313,20 @@ impl<const LIMBS: usize> zeroize::Zeroize for MontyForm<LIMBS> {
self.montgomery_form.zeroize()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn inplace_cloning() {
let modulus = Odd::new(Uint::<4>::from(9u8)).unwrap();
let params = MontyParams::new(modulus);
let zero = MontyForm::zero(params.clone());
let mut target = MontyForm::one(params.clone());

assert_ne!(target, zero);
target.clone_from_montgomery(&zero);
assert_eq!(target, zero);
}
}
3 changes: 3 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,9 @@ pub trait Monty:
/// Access the value in Montgomery form.
fn as_montgomery(&self) -> &Self::Integer;

/// In-place cloning of Montgomery form, assuming tha parameters are equal
fn clone_from_montgomery(&mut self, other: &Self);

/// Performs division by 2, that is returns `x` such that `x + x = self`.
fn div_by_2(&self) -> Self;
}
Loading