Menu
Public repository / master
AkurAI-Framework / crates/crypto/src/ct.rs
Latest change 3aae9f4e85241bcb775c31f5c03094becd3da56a - Pure-Rust UI layer: akurai-ui, akurai-crypto, Tailwind-grade akurai-css by Ólafur Búi Ólafsson
//! Constant-time comparison.
use std::hint::black_box;
/// `true` iff `a == b`, taking time that depends only on the lengths, not on
/// where the first difference is. Unequal lengths return `false` at once.
pub fn ct_eq(a: &[u8], b: &[u8]) -> bool {
if a.len() != b.len() {
return false;
}
let diff = a
.iter()
.zip(b)
.fold(0u8, |acc, (x, y)| black_box(acc | (x ^ y)));
diff == 0
}