subtle_ng

Trait ConstantTimeGreater

Source
pub trait ConstantTimeGreater {
    // Required method
    fn ct_gt(&self, other: &Self) -> Choice;
}
Expand description

A type which can be compared in some manner and be determined to be greater than another of the same type.

Required Methods§

Source

fn ct_gt(&self, other: &Self) -> Choice

Determine whether self > other.

The bitwise-NOT of the return value of this function should be usable to determine if self <= other.

This function should execute in constant time.

§Returns

A Choice with a set bit if self > other, and with no set bits otherwise.

§Example
use subtle_ng::ConstantTimeGreater;

let x: u8 = 13;
let y: u8 = 42;

let x_gt_y = x.ct_gt(&y);

assert_eq!(x_gt_y.unwrap_u8(), 0);

let y_gt_x = y.ct_gt(&x);

assert_eq!(y_gt_x.unwrap_u8(), 1);

let x_gt_x = x.ct_gt(&x);

assert_eq!(x_gt_x.unwrap_u8(), 0);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ConstantTimeGreater for u8

Source§

fn ct_gt(&self, other: &u8) -> Choice

Returns Choice::from(1) iff x > y, and Choice::from(0) iff x <= y.

§Note

This algoritm would also work for signed integers if we first flip the top bit, e.g. let x: u8 = x ^ 0x80, etc.

Source§

impl ConstantTimeGreater for u16

Source§

fn ct_gt(&self, other: &u16) -> Choice

Returns Choice::from(1) iff x > y, and Choice::from(0) iff x <= y.

§Note

This algoritm would also work for signed integers if we first flip the top bit, e.g. let x: u8 = x ^ 0x80, etc.

Source§

impl ConstantTimeGreater for u32

Source§

fn ct_gt(&self, other: &u32) -> Choice

Returns Choice::from(1) iff x > y, and Choice::from(0) iff x <= y.

§Note

This algoritm would also work for signed integers if we first flip the top bit, e.g. let x: u8 = x ^ 0x80, etc.

Source§

impl ConstantTimeGreater for u64

Source§

fn ct_gt(&self, other: &u64) -> Choice

Returns Choice::from(1) iff x > y, and Choice::from(0) iff x <= y.

§Note

This algoritm would also work for signed integers if we first flip the top bit, e.g. let x: u8 = x ^ 0x80, etc.

Source§

impl ConstantTimeGreater for u128

Source§

fn ct_gt(&self, other: &u128) -> Choice

Returns Choice::from(1) iff x > y, and Choice::from(0) iff x <= y.

§Note

This algoritm would also work for signed integers if we first flip the top bit, e.g. let x: u8 = x ^ 0x80, etc.

Implementors§