pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let mut d = 0u8;
for (x, y) in a.iter().zip(b) {
d |= x ^ y;
}
core::hint::black_box(d) == 0
}
#[inline(always)]
pub fn ct_mask(flag: u64) -> u64 {
simoom_sys::arch::opaque(flag).wrapping_neg()
}
#[inline(always)]
pub fn ct_eq_mask(a: u64, b: u64) -> u64 {
let d = a ^ b;
ct_mask(1 ^ ((d | d.wrapping_neg()) >> 63))
}
pub fn hex(data: &[u8]) -> String {
const D: &[u8; 16] = b"0123456789abcdef";
let mut s = String::with_capacity(data.len() * 2);
for b in data {
s.push(D[(b >> 4) as usize] as char);
s.push(D[(b & 15) as usize] as char);
}
s
}
pub fn unhex(s: &str) -> Vec<u8> {
let digits: Vec<u8> = s.bytes().filter(|c| !c.is_ascii_whitespace() && *c != b':').collect();
assert!(digits.len() % 2 == 0, "odd hex length");
let val = |c: u8| -> u8 {
match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 10,
b'A'..=b'F' => c - b'A' + 10,
_ => panic!("invalid hex digit"),
}
};
digits.chunks(2).map(|p| val(p[0]) << 4 | val(p[1])).collect()
}
pub fn zeroize(buf: &mut [u8]) {
for b in buf.iter_mut() {
*b = 0;
}
core::hint::black_box(buf);
}