Skip to content

Commit 1aa32e9

Browse files
authored
Limb: simplify Ord impl (#411)
Uses the `ConditionallySelectable` impl on `Ordering` to simplify the implementation.
1 parent 8263c76 commit 1aa32e9

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

src/limb/cmp.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
33
use crate::{CtChoice, Limb};
44
use core::cmp::Ordering;
5-
use subtle::{Choice, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess};
5+
use subtle::{
6+
Choice, ConditionallySelectable, ConstantTimeEq, ConstantTimeGreater, ConstantTimeLess,
7+
};
68

79
impl Limb {
810
/// Is this limb an odd number?
@@ -62,19 +64,11 @@ impl Eq for Limb {}
6264

6365
impl Ord for Limb {
6466
fn cmp(&self, other: &Self) -> Ordering {
65-
let mut n = 0i8;
66-
n -= self.ct_lt(other).unwrap_u8() as i8;
67-
n += self.ct_gt(other).unwrap_u8() as i8;
68-
69-
match n {
70-
-1 => Ordering::Less,
71-
1 => Ordering::Greater,
72-
_ => {
73-
debug_assert_eq!(n, 0);
74-
debug_assert!(bool::from(self.ct_eq(other)));
75-
Ordering::Equal
76-
}
77-
}
67+
let mut ret = Ordering::Less;
68+
ret.conditional_assign(&Ordering::Equal, self.ct_eq(other));
69+
ret.conditional_assign(&Ordering::Greater, self.ct_gt(other));
70+
debug_assert_eq!(ret == Ordering::Less, self.ct_lt(other).into());
71+
ret
7872
}
7973
}
8074

0 commit comments

Comments
 (0)