Skip to content

Commit e322137

Browse files
authored
Add BoxedMontyForm::as_montgomery_mut (#1014)
This PR adds a mutable access method for the underlying Montgomery form `BoxedUint` in `BoxedMontyForm` type.
1 parent 44ead85 commit e322137

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/modular/boxed_monty_form.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ impl BoxedMontyForm {
242242
&self.montgomery_form
243243
}
244244

245+
/// Mutably access the [`BoxedMontyForm`] value in Montgomery form.
246+
pub fn as_montgomery_mut(&mut self) -> &mut BoxedUint {
247+
&mut self.montgomery_form
248+
}
249+
245250
/// Create a [`BoxedMontyForm`] from a value in Montgomery form.
246251
pub fn from_montgomery(integer: BoxedUint, params: BoxedMontyParams) -> Self {
247252
debug_assert_eq!(integer.bits_precision(), params.bits_precision());
@@ -375,4 +380,18 @@ mod tests {
375380
assert_eq!(zero.div_by_2(), zero);
376381
assert_eq!(one.div_by_2().mul(&two), one);
377382
}
383+
384+
#[test]
385+
fn as_montgomery_mut() {
386+
let modulus = Odd::new(BoxedUint::from(9u8)).unwrap();
387+
let params = BoxedMontyParams::new(modulus);
388+
let one = BoxedMontyForm::one(params.clone());
389+
let two = one.add(&one);
390+
let four = two.mul(&two);
391+
let mut x = two.clone();
392+
393+
*x.as_montgomery_mut() = four.as_montgomery().clone();
394+
395+
assert_eq!(x, four);
396+
}
378397
}

0 commit comments

Comments
 (0)