From 4690c5f14e066957cace7da8583b94f86cc1662b Mon Sep 17 00:00:00 2001 From: Cesar Eduardo Barros Date: Sat, 11 Jan 2020 19:06:58 -0300 Subject: [PATCH] Use fixed-size constant_time_eq The generic constant_time_eq has several branches on the slice length, which are not necessary when the slice length is known. However, the optimizer is not allowed to look into the core of constant_time_eq, so these branches cannot be elided. Use instead a fixed-size variant of constant_time_eq, which has no branches since the length is known. --- Cargo.toml | 2 +- src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 40caead..c429c1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ std = [] [dependencies] arrayref = "0.3.5" arrayvec = { version = "0.5.1", default-features = false, features = ["array-sizes-33-128"] } -constant_time_eq = "0.1.4" +constant_time_eq = "0.1.5" # A performance note for the "rayon" feature: Multi-threading can have # significant overhead for small inputs, particularly on x86 where individual # cores are very fast. On the other hand, on slower platforms like ARM, diff --git a/src/lib.rs b/src/lib.rs index c7300d2..3ea8401 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,14 +180,14 @@ impl From for [u8; OUT_LEN] { /// This implementation is constant-time. impl PartialEq for Hash { fn eq(&self, other: &Hash) -> bool { - constant_time_eq::constant_time_eq(&self.0[..], &other.0[..]) + constant_time_eq::constant_time_eq_32(&self.0, &other.0) } } /// This implementation is constant-time. impl PartialEq<[u8; OUT_LEN]> for Hash { fn eq(&self, other: &[u8; OUT_LEN]) -> bool { - constant_time_eq::constant_time_eq(&self.0[..], other) + constant_time_eq::constant_time_eq_32(&self.0, other) } }