Skip to content

Commit

Permalink
[TODO: use ConditionallySelectable for IteratedGreater!] save
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Jan 1, 2023
1 parent beb17be commit 7e8a0ad
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ pub trait ConstantTimeGreater {
///
///```
/// use subtle::{
/// Choice, IteratedOperation, ConstantTimeEq, IteratedEq, ConstantTimeGreater, IteratedGreater,
/// Choice, IteratedOperation, ConstantTimeEq, IteratedEq, ConstantTimeGreater, LexicographicIteratedGreater,
/// };
///
/// struct S { pub len: usize, pub live: bool };
Expand All @@ -885,7 +885,7 @@ pub trait ConstantTimeGreater {
/// }
/// impl ConstantTimeGreater for S {
/// fn ct_gt(&self, other: &Self) -> Choice {
/// let mut x = IteratedGreater::initiate();
/// let mut x = LexicographicIteratedGreater::initiate();
/// x.apply_gt(&(self.len as u64), &(other.len as u64));
/// x.apply_gt(&(self.live as u8), &(other.live as u8));
/// x.extract_result()
Expand All @@ -900,24 +900,24 @@ pub trait ConstantTimeGreater {
/// assert_eq!(1, s2.ct_gt(&s1).unwrap_u8());
/// assert_eq!(1, s3.ct_gt(&s2).unwrap_u8());
///```
pub struct IteratedGreater {
pub struct LexicographicIteratedGreater {
was_gt: Choice,
was_lt: Choice,
}

impl IteratedOperation for IteratedGreater {
impl IteratedOperation for LexicographicIteratedGreater {
fn initiate() -> Self {
Self {
was_gt: Choice::from(0),
was_lt: Choice::from(0),
was_gt: Choice::of_bool(false),
was_lt: Choice::of_bool(false),
}
}
fn extract_result(self) -> Choice {
self.was_gt.into()
}
}

impl IteratedGreater {
impl LexicographicIteratedGreater {
/// Unconditionally modify internal state with result of two directed "greater" comparisons.
#[inline]
pub fn apply_gt<T: ConstantTimeGreater + ?Sized>(&mut self, a: &T, b: &T) {
Expand Down Expand Up @@ -1022,7 +1022,7 @@ impl<T: ConstantTimeGreater + ConstantTimeEq> ConstantTimeGreater for [T] {
}
}

let mut x = IteratedGreater::initiate();
let mut x = LexicographicIteratedGreater::initiate();
for (ai, bi) in self.iter().zip(_rhs.iter()) {
x.apply_gt(ai, bi);
}
Expand Down Expand Up @@ -1077,8 +1077,8 @@ pub trait ConstantTimeLess: ConstantTimeGreater {
///
///```
/// use subtle::{
/// Choice, IteratedOperation, ConstantTimeEq, IteratedEq, ConstantTimeGreater, IteratedGreater,
/// ConstantTimeLess, IteratedLess,
/// Choice, IteratedOperation, ConstantTimeEq, IteratedEq, ConstantTimeGreater, LexicographicIteratedGreater,
/// ConstantTimeLess, LexicographicIteratedLess,
/// };
///
/// struct S { pub len: usize, pub live: bool };
Expand All @@ -1092,15 +1092,15 @@ pub trait ConstantTimeLess: ConstantTimeGreater {
/// }
/// impl ConstantTimeGreater for S {
/// fn ct_gt(&self, other: &Self) -> Choice {
/// let mut x = IteratedGreater::initiate();
/// let mut x = LexicographicIteratedGreater::initiate();
/// x.apply_gt(&(self.len as u64), &(other.len as u64));
/// x.apply_gt(&(self.live as u8), &(other.live as u8));
/// x.extract_result()
/// }
/// }
/// impl ConstantTimeLess for S {
/// fn ct_lt(&self, other: &Self) -> Choice {
/// let mut x = IteratedLess::initiate();
/// let mut x = LexicographicIteratedLess::initiate();
/// x.apply_lt(&(self.len as u64), &(other.len as u64));
/// x.apply_lt(&(self.live as u8), &(other.live as u8));
/// x.extract_result()
Expand All @@ -1115,24 +1115,24 @@ pub trait ConstantTimeLess: ConstantTimeGreater {
/// assert_eq!(1, s2.ct_gt(&s1).unwrap_u8());
/// assert_eq!(1, s2.ct_lt(&s3).unwrap_u8());
///```
pub struct IteratedLess {
pub struct LexicographicIteratedLess {
was_lt: Choice,
was_gt: Choice,
}

impl IteratedOperation for IteratedLess {
impl IteratedOperation for LexicographicIteratedLess {
fn initiate() -> Self {
Self {
was_lt: Choice::from(0),
was_gt: Choice::from(0),
was_lt: Choice::of_bool(false),
was_gt: Choice::of_bool(false),
}
}
fn extract_result(self) -> Choice {
self.was_lt.into()
}
}

impl IteratedLess {
impl LexicographicIteratedLess {
/// Unconditionally modify internal state with result of two directed "less" comparisons.
#[inline]
pub fn apply_lt<T: ConstantTimeLess + ?Sized>(&mut self, a: &T, b: &T) {
Expand Down

0 comments on commit 7e8a0ad

Please sign in to comment.