Skip to content

Commit 21ebb49

Browse files
authored
impl ConditionallyNegatable for BoxedUint (#795)
1 parent 0993978 commit 21ebb49

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ pub trait Monty:
930930
/// Allows one to perform inplace multiplication without allocations
931931
/// (important for the `BoxedUint` case).
932932
///
933-
/// NOTE: You will be operating with Montgomery represntations directly,
933+
/// NOTE: You will be operating with Montgomery representations directly,
934934
/// make sure they all correspond to the same set of parameters.
935935
pub trait MontyMultiplier<'a>: From<&'a <Self::Monty as Monty>::Params> {
936936
/// The associated Montgomery-representation integer.

src/uint/boxed/ct.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::BoxedUint;
44
use crate::{ConstantTimeSelect, Limb};
5-
use subtle::{Choice, ConditionallySelectable};
5+
use subtle::{Choice, ConditionallyNegatable, ConditionallySelectable};
66

77
/// NOTE: can't impl `subtle`'s [`ConditionallySelectable`] trait due to its `Copy` bound
88
impl ConstantTimeSelect for BoxedUint {
@@ -37,11 +37,18 @@ impl ConstantTimeSelect for BoxedUint {
3737
}
3838
}
3939

40+
impl ConditionallyNegatable for BoxedUint {
41+
#[inline]
42+
fn conditional_negate(&mut self, choice: Choice) {
43+
let self_neg = self.wrapping_neg();
44+
self.ct_assign(&self_neg, choice)
45+
}
46+
}
47+
4048
#[cfg(test)]
4149
mod tests {
42-
use super::BoxedUint;
43-
use crate::ConstantTimeSelect;
44-
use subtle::Choice;
50+
use crate::{BoxedUint, ConstantTimeSelect};
51+
use subtle::{Choice, ConditionallyNegatable};
4552

4653
#[test]
4754
fn conditional_select() {
@@ -51,4 +58,17 @@ mod tests {
5158
assert_eq!(a, BoxedUint::ct_select(&a, &b, Choice::from(0)));
5259
assert_eq!(b, BoxedUint::ct_select(&a, &b, Choice::from(1)));
5360
}
61+
62+
#[test]
63+
fn conditional_negate() {
64+
let mut a = BoxedUint::from(123u64);
65+
let control = a.clone();
66+
67+
a.conditional_negate(Choice::from(0));
68+
assert_eq!(a, control);
69+
70+
a.conditional_negate(Choice::from(1));
71+
assert_ne!(a, control);
72+
assert_eq!(a, control.wrapping_neg());
73+
}
5474
}

0 commit comments

Comments
 (0)