#![no_std]
use core::mem::transmute as trans;
const MAX_U8: u8 = ::core::u8::MAX;
const MAX_U16: u16 = ::core::u16::MAX;
const MAX_U32: u32 = ::core::u32::MAX;
const MAX_U64: u64 = ::core::u64::MAX;
const MAX_USIZE: usize = ::core::usize::MAX;
#[test]
fn test_bool_representation() {
let t: bool = true;
let f: bool = false;
let t_val: u8 = unsafe{ ::core::mem::transmute(t) };
let f_val: u8 = unsafe{ ::core::mem::transmute(f) };
assert_eq!( t_val, 0x01u8);
assert_eq!( f_val, 0x00u8);
}
macro_rules! ct_eq_gen {
($name: ident, $code: ty, $max: expr, $($shr: expr),*
;; $test_name: ident, $test_v0: expr, $test_v1: expr) => {
#[no_mangle]
#[inline(never)]
pub extern "C" fn $name( x: $code, y: $code) -> bool {
let mut z: $code = $max ^ (x^y);
$(
z &= z.wrapping_shr($shr);
)*
let val = z as u8;
unsafe{trans(val)}
}
#[test]
fn $test_name() {
let x: $code = $test_v0;
let y: $code = $test_v1;
assert_eq!( $name($max,$max), true);
assert_eq!( $name(x,x), true);
assert_eq!( $name(y,y), true);
assert_eq!( $name(0,0), true);
assert_eq!( $name(1,1), true);
assert_eq!( $name($max,0), false);
assert_eq!( $name($max,1), false);
assert_eq!( $name($max,x), false);
assert_eq!( $name($max,y), false);
assert_eq!( $name(y,1), false);
assert_eq!( $name(x,1), false);
assert_eq!( $name(y,0), false);
assert_eq!( $name(x,0), false);
assert_eq!( $name(x,y), false);
$(
assert_eq!( $name($shr,$shr), true);
assert_eq!( $name($shr,0), false);
assert_eq!( $name($shr,$max), false);
)*
}
}
}
ct_eq_gen!(ct_u8_eq,u8,MAX_U8,4,2,1;;
test_ct_u8_eq, 155, 15);
ct_eq_gen!(ct_u16_eq,u16,MAX_U16,8,4,2,1;;
test_ct_u16_eq, 32000, 5);
ct_eq_gen!(ct_u32_eq,u32,MAX_U32,16,8,4,2,1;;
test_ct_u32_eq, 2000000, 15);
ct_eq_gen!(ct_u64_eq,u64,MAX_U64,32,16,8,4,2,1;;
test_ct_u64_eq, 25893654215879, 2);
#[cfg(target_pointer_width = "32")]
ct_eq_gen!(ct_usize_eq,usize,MAX_USIZE,16,8,4,2,1;;
test_ct_u32_eq, 2082600, 15);
#[cfg(target_pointer_width = "64")]
ct_eq_gen!(ct_usize_eq,usize,MAX_USIZE,32,16,8,4,2,1;;
test_ct_usize_eq, 859632175648921456, 5);
macro_rules! ct_eq_slice_gen {
($name:ident,$eq:ident,$code: ty;;$test_name:ident,$max: expr) => {
#[no_mangle]
pub extern "C" fn $name( x: &[$code], y: &[$code]) -> bool {
let x_len = x.len();
let y_len = y.len();
if x_len != y_len {
return false;
}
let mut flag: $code = 0;
for i in 0..x_len {
flag |= x[i] ^ y[i];
}
$eq(flag,0)
}
#[test]
fn $test_name() {
let x: [$code;10] = [0,0,0,0,0,0,0,0,0,0];
let y: [$code;10] = [$max,$max,$max,$max,$max,$max,$max,$max,$max,$max];
let z: [$code;10] = [1,1,1,1,1,1,1,1,1,1];
assert_eq!( $name( &x, &x), true);
assert_eq!( $name( &y, &y), true);
assert_eq!( $name( &z, &z), true);
assert_eq!( $name( &x, &y), false);
assert_eq!( $name( &x, &y), false);
assert_eq!( $name( &y, &z), false);
}
}
}
ct_eq_slice_gen!(ct_u8_slice_eq,ct_u8_eq,u8;;
test_ct_u8_slice_eq, MAX_U8);
ct_eq_slice_gen!(ct_u16_slice_eq,ct_u16_eq,u16;;
test_ct_u16_slice_eq, MAX_U16);
ct_eq_slice_gen!(ct_u32_slice_eq,ct_u32_eq,u32;;
test_ct_u32_slice_eq, MAX_U32);
ct_eq_slice_gen!(ct_u64_slice_eq,ct_u64_eq,u64;;
test_ct_u64_slice_eq, MAX_U64);
ct_eq_slice_gen!(ct_usize_slice_eq,ct_usize_eq,usize;;
test_ct_usize_slice_eq, MAX_USIZE);
macro_rules! ct_select_gen {
($name:ident,$max:expr,$code:ty;;$test_name:ident,$v0:expr,$v1:expr) => {
#[no_mangle]
#[inline(never)]
pub extern "C" fn $name(flag: bool, x: $code, y: $code) -> $code {
let val: u8 = unsafe{trans(flag)};
let flag = val as $code;
(($max ^ flag.wrapping_sub(1))&x)|(flag.wrapping_sub(1)&y)
}
#[test]
fn $test_name() {
assert_eq!( $name(true,$v0,$v1), $v0);
assert_eq!( $name(false,$v0,$v1), $v1);
assert_eq!( $name(true,$v1,$v0), $v1);
assert_eq!( $name(false,$v1,$v0), $v0);
assert_eq!( $name(true,$v0,$max), $v0);
assert_eq!( $name(false,$v0,$max), $max);
assert_eq!( $name(true,$max,$v0), $max);
assert_eq!( $name(false,$max,$v0), $v0);
assert_eq!( $name(true,$max,$v1), $max);
assert_eq!( $name(false,$max,$v1), $v1);
assert_eq!( $name(true,$v1,$max), $v1);
assert_eq!( $name(false,$v1,$max), $max);
}
}
}
ct_select_gen!(ct_select_u8,MAX_U8,u8;;
test_ct_select_u8,155,4);
ct_select_gen!(ct_select_u16,MAX_U16,u16;;
test_ct_select_u16,30597,4);
ct_select_gen!(ct_select_u32,MAX_U32,u32;;
test_ct_select_u32,0x0DD74AA2,4);
ct_select_gen!(ct_select_u64,MAX_U64,u64;;
test_ct_select_u64,155,4);
ct_select_gen!(ct_select_usize,MAX_USIZE,usize;;
test_ct_select_usize,155,4);
macro_rules! ct_constant_copy_gen {
($name:ident,$max:expr,$code:ty,$copy_symbol: ident
;;$test_name:ident,$sl_eq:ident,$other_test:ident) => {
#[no_mangle]
pub extern "C" fn $name(flag: bool, x: &mut [$code], y: &[$code]) {
let x_len = x.len();
let y_len = y.len();
if x_len != y_len {
panic!("Consistent Time: Attempted to copy between non-equal lens");
}
for i in 0..x_len {
let y_temp = y[i].clone();
let x_temp = x[i].clone();
x[i] = $copy_symbol(flag,y_temp,x_temp);
}
}
#[test]
fn $test_name() {
let base: [$code;10] = [0,0,0,0,0,0,0,0,0,0];
let mut x: [$code;10] = [0,0,0,0,0,0,0,0,0,0];
let y: [$code;10] = [$max,$max,$max,$max,$max,$max,$max,$max,$max,$max];
$name(false,&mut x, &y);
assert_eq!( $sl_eq(&x,&base), true);
$name(true,&mut x, &y);
assert_eq!( $sl_eq(&x,&base), false);
assert_eq!( $sl_eq(&x,&y), true);
}
#[test]
#[should_panic]
fn $other_test() {
let base: [$code;10] = [0,0,0,0,0,0,0,0,0,0];
let mut x: [$code;9] = [0,0,0,0,0,0,0,0,0];
$name(false,&mut x,&base);
}
}
}
ct_constant_copy_gen!(ct_copy_u8,MAX_U8,u8,ct_select_u8;;
test_ct_copy_u8,ct_u8_slice_eq,test_ct_copy_u8_panic);
ct_constant_copy_gen!(ct_copy_u16,MAX_U16,u16,ct_select_u16;;
test_ct_copy_u16,ct_u16_slice_eq,test_ct_copy_u16_panic);
ct_constant_copy_gen!(ct_copy_u32,MAX_U32,u32,ct_select_u32;;
test_ct_copy_u32,ct_u32_slice_eq,test_ct_copy_u32_panic);
ct_constant_copy_gen!(ct_copy_u64,MAX_U64,u64,ct_select_u64;;
test_ct_copy_u64,ct_u64_slice_eq,test_ct_copy_u64_panic);
ct_constant_copy_gen!(ct_copy_usize,MAX_USIZE,usize,ct_select_usize;;
test_ct_copy_usize,ct_usize_slice_eq,test_ct_copy_usize_panic);