Skip to content

Commit f636ed7

Browse files
authored
ctutils: add *Slice trait impls to Choice/CtOption (#1394)
Adds impls of `CtAssignSlice` and `CtEqSlice` to the `Choice` and `CtOption` types.
1 parent c8e53ed commit f636ed7

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

ctutils/src/choice.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use crate::{CtAssign, CtEq, CtSelect};
1+
use crate::{CtAssign, CtAssignSlice, CtEq, CtEqSlice, CtSelectUsingCtAssign};
22
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
33

4+
#[cfg(feature = "subtle")]
5+
use crate::CtSelect;
6+
47
/// Bitwise less-than-or equal: returns `1` if `x <= y`, and otherwise returns `0`.
58
///
69
/// See "Hacker's Delight" 2nd edition, section 2-12 (Comparison predicates)
@@ -529,20 +532,16 @@ impl CtAssign for Choice {
529532
self.0.ct_assign(&other.0, choice);
530533
}
531534
}
535+
impl CtAssignSlice for Choice {}
536+
impl CtSelectUsingCtAssign for Choice {}
532537

533538
impl CtEq for Choice {
534539
#[inline]
535540
fn ct_eq(&self, other: &Self) -> Self {
536541
self.0.ct_eq(&other.0)
537542
}
538543
}
539-
540-
impl CtSelect for Choice {
541-
#[inline]
542-
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
543-
Choice(self.0.ct_select(&other.0, choice))
544-
}
545-
}
544+
impl CtEqSlice for Choice {}
546545

547546
/// DEPRECATED: this exists to aid migrating code from `subtle`. Use `Choice::from_u8_lsb` instead.
548547
///

ctutils/src/ct_option.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Choice, CtAssign, CtEq, CtSelect};
1+
use crate::{Choice, CtAssign, CtAssignSlice, CtEq, CtEqSlice, CtSelect};
22
use core::ops::{Deref, DerefMut};
33

44
/// Helper macro for providing behavior like the [`CtOption::map`] combinator that works in
@@ -565,6 +565,7 @@ impl<T: CtAssign> CtAssign for CtOption<T> {
565565
self.is_some.ct_assign(&other.is_some, choice);
566566
}
567567
}
568+
impl<T: CtAssign> CtAssignSlice for CtOption<T> {}
568569

569570
impl<T: CtEq> CtEq for CtOption<T> {
570571
#[inline]
@@ -574,11 +575,14 @@ impl<T: CtEq> CtEq for CtOption<T> {
574575
}
575576
}
576577

578+
impl<T: CtEq> CtEqSlice for CtOption<T> {}
579+
577580
impl<T: CtSelect> CtSelect for CtOption<T> {
578581
fn ct_select(&self, other: &Self, choice: Choice) -> Self {
579-
let value = self.value.ct_select(&other.value, choice);
580-
let is_some = self.is_some.ct_select(&other.is_some, choice);
581-
CtOption::new(value, is_some)
582+
Self {
583+
value: self.value.ct_select(&other.value, choice),
584+
is_some: self.is_some.ct_select(&other.is_some, choice),
585+
}
582586
}
583587
}
584588

0 commit comments

Comments
 (0)