use subtle::{Choice, ConstantTimeEq};
pub(crate) fn bool_to_choice(v: bool) -> Choice {
Choice::from(u8::from(v))
}
pub(crate) fn bytes_eq(a: &[u8], b: &[u8]) -> bool {
let choice = a.ct_eq(b);
choice.unwrap_u8() == 1
}
pub(crate) fn is_zero(x: &[u8]) -> bool {
x.iter()
.map(|b| bool_to_choice(*b == 0))
.fold(bool_to_choice(true), std::ops::BitAnd::bitand)
.unwrap_u8()
== 1
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_time_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
#[test]
fn test_bytes_eq() {
use super::bytes_eq;
assert!(bytes_eq(&b"123"[..], &b"1234"[..3]));
assert!(!bytes_eq(&b"123"[..], &b"1234"[..]));
assert!(bytes_eq(&b"45"[..], &b"45"[..]));
assert!(!bytes_eq(&b"hi"[..], &b"45"[..]));
assert!(bytes_eq(&b""[..], &b""[..]));
}
#[test]
fn test_is_zero() {
use super::is_zero;
assert!(is_zero(&[]));
assert!(is_zero(&[0]));
assert!(is_zero(&[0, 0]));
assert!(!is_zero(&[1, 0]));
assert!(!is_zero(&[0, 1]));
assert!(!is_zero(&[0, 1, 0]));
}
}