pub trait ConditionallySelectable: Copy {
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self;
fn conditional_assign(&mut self, other: &Self, choice: Choice) { ... }
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice) { ... }
}
Expand description
A type which can be conditionally selected in constant time.
This trait also provides generic implementations of conditional assignment and conditional swaps.
Required Methods
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self
Select a
or b
according to choice
.
Returns
a
ifchoice == Choice(0)
;b
ifchoice == Choice(1)
.
This function should execute in constant time.
Example
use subtle::ConditionallySelectable;
let x: u8 = 13;
let y: u8 = 42;
let z = u8::conditional_select(&x, &y, 0.into());
assert_eq!(z, x);
let z = u8::conditional_select(&x, &y, 1.into());
assert_eq!(z, y);
Provided Methods
fn conditional_assign(&mut self, other: &Self, choice: Choice)
fn conditional_assign(&mut self, other: &Self, choice: Choice)
Conditionally assign other
to self
, according to choice
.
This function should execute in constant time.
Example
use subtle::ConditionallySelectable;
let mut x: u8 = 13;
let mut y: u8 = 42;
x.conditional_assign(&y, 0.into());
assert_eq!(x, 13);
x.conditional_assign(&y, 1.into());
assert_eq!(x, 42);
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
fn conditional_swap(a: &mut Self, b: &mut Self, choice: Choice)
Conditionally swap self
and other
if choice == 1
; otherwise,
reassign both unto themselves.
This function should execute in constant time.
Example
use subtle::ConditionallySelectable;
let mut x: u8 = 13;
let mut y: u8 = 42;
u8::conditional_swap(&mut x, &mut y, 0.into());
assert_eq!(x, 13);
assert_eq!(y, 42);
u8::conditional_swap(&mut x, &mut y, 1.into());
assert_eq!(x, 42);
assert_eq!(y, 13);
Implementations on Foreign Types
sourceimpl ConditionallySelectable for i32
impl ConditionallySelectable for i32
fn conditional_select(a: &i32, b: &i32, choice: Choice) -> i32
fn conditional_assign(&mut self, other: &i32, choice: Choice)
fn conditional_swap(a: &mut i32, b: &mut i32, choice: Choice)
sourceimpl ConditionallySelectable for u16
impl ConditionallySelectable for u16
fn conditional_select(a: &u16, b: &u16, choice: Choice) -> u16
fn conditional_assign(&mut self, other: &u16, choice: Choice)
fn conditional_swap(a: &mut u16, b: &mut u16, choice: Choice)
sourceimpl ConditionallySelectable for u64
impl ConditionallySelectable for u64
fn conditional_select(a: &u64, b: &u64, choice: Choice) -> u64
fn conditional_assign(&mut self, other: &u64, choice: Choice)
fn conditional_swap(a: &mut u64, b: &mut u64, choice: Choice)
sourceimpl ConditionallySelectable for i64
impl ConditionallySelectable for i64
fn conditional_select(a: &i64, b: &i64, choice: Choice) -> i64
fn conditional_assign(&mut self, other: &i64, choice: Choice)
fn conditional_swap(a: &mut i64, b: &mut i64, choice: Choice)
sourceimpl ConditionallySelectable for i8
impl ConditionallySelectable for i8
fn conditional_select(a: &i8, b: &i8, choice: Choice) -> i8
fn conditional_assign(&mut self, other: &i8, choice: Choice)
fn conditional_swap(a: &mut i8, b: &mut i8, choice: Choice)
sourceimpl ConditionallySelectable for i128
impl ConditionallySelectable for i128
fn conditional_select(a: &i128, b: &i128, choice: Choice) -> i128
fn conditional_assign(&mut self, other: &i128, choice: Choice)
fn conditional_swap(a: &mut i128, b: &mut i128, choice: Choice)
sourceimpl ConditionallySelectable for i16
impl ConditionallySelectable for i16
fn conditional_select(a: &i16, b: &i16, choice: Choice) -> i16
fn conditional_assign(&mut self, other: &i16, choice: Choice)
fn conditional_swap(a: &mut i16, b: &mut i16, choice: Choice)
sourceimpl ConditionallySelectable for u32
impl ConditionallySelectable for u32
fn conditional_select(a: &u32, b: &u32, choice: Choice) -> u32
fn conditional_assign(&mut self, other: &u32, choice: Choice)
fn conditional_swap(a: &mut u32, b: &mut u32, choice: Choice)
sourceimpl ConditionallySelectable for u128
impl ConditionallySelectable for u128
fn conditional_select(a: &u128, b: &u128, choice: Choice) -> u128
fn conditional_assign(&mut self, other: &u128, choice: Choice)
fn conditional_swap(a: &mut u128, b: &mut u128, choice: Choice)
sourceimpl ConditionallySelectable for u8
impl ConditionallySelectable for u8
fn conditional_select(a: &u8, b: &u8, choice: Choice) -> u8
fn conditional_assign(&mut self, other: &u8, choice: Choice)
fn conditional_swap(a: &mut u8, b: &mut u8, choice: Choice)
sourceimpl ConditionallySelectable for Scalar
impl ConditionallySelectable for Scalar
sourceimpl ConditionallySelectable for EdwardsPoint
impl ConditionallySelectable for EdwardsPoint
fn conditional_select(
a: &EdwardsPoint,
b: &EdwardsPoint,
choice: Choice
) -> EdwardsPoint
sourceimpl ConditionallySelectable for RistrettoPoint
impl ConditionallySelectable for RistrettoPoint
sourcefn conditional_select(
a: &RistrettoPoint,
b: &RistrettoPoint,
choice: Choice
) -> RistrettoPoint
fn conditional_select(
a: &RistrettoPoint,
b: &RistrettoPoint,
choice: Choice
) -> RistrettoPoint
Conditionally select between self
and other
.
Example
use subtle::ConditionallySelectable;
use subtle::Choice;
let A = RistrettoPoint::identity();
let B = constants::RISTRETTO_BASEPOINT_POINT;
let mut P = A;
P = RistrettoPoint::conditional_select(&A, &B, Choice::from(0));
assert_eq!(P, A);
P = RistrettoPoint::conditional_select(&A, &B, Choice::from(1));
assert_eq!(P, B);