From: Matt Corallo Date: Thu, 30 Nov 2023 23:35:43 +0000 (+0000) Subject: Drop unnecessary SIMD subtraction in ChaCha20 `round` X-Git-Tag: v0.0.119~28^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=e0480b531d4814ef744aa89a4eaa7bc2f7420d33;p=rust-lightning Drop unnecessary SIMD subtraction in ChaCha20 `round` While its all constant arithmetic to calculate the shift, which LLVM likely optimizes out for us, there's no reason to do it four times, which just makes the code harder to read. --- diff --git a/lightning/src/util/chacha20.rs b/lightning/src/util/chacha20.rs index f46b344f2..87500d88d 100644 --- a/lightning/src/util/chacha20.rs +++ b/lightning/src/util/chacha20.rs @@ -43,16 +43,16 @@ mod real_chacha { u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3) } } - impl ::core::ops::Shr for u32x4 { + impl ::core::ops::Shr for u32x4 { type Output = u32x4; - fn shr(self, rhs: u32x4) -> u32x4 { - u32x4(self.0 >> rhs.0, self.1 >> rhs.1, self.2 >> rhs.2, self.3 >> rhs.3) + fn shr(self, shr: u8) -> u32x4 { + u32x4(self.0 >> shr, self.1 >> shr, self.2 >> shr, self.3 >> shr) } } - impl ::core::ops::Shl for u32x4 { + impl ::core::ops::Shl for u32x4 { type Output = u32x4; - fn shl(self, rhs: u32x4) -> u32x4 { - u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3) + fn shl(self, shl: u8) -> u32x4 { + u32x4(self.0 << shl, self.1 << shl, self.2 << shl, self.3 << shl) } } impl u32x4 { @@ -118,31 +118,25 @@ mod real_chacha { macro_rules! round{ ($state: expr) => {{ $state.a = $state.a + $state.b; - rotate!($state.d, $state.a, S16); + rotate!($state.d, $state.a, 16); $state.c = $state.c + $state.d; - rotate!($state.b, $state.c, S12); + rotate!($state.b, $state.c, 12); $state.a = $state.a + $state.b; - rotate!($state.d, $state.a, S8); + rotate!($state.d, $state.a, 8); $state.c = $state.c + $state.d; - rotate!($state.b, $state.c, S7); + rotate!($state.b, $state.c, 7); }} } macro_rules! rotate { - ($a: expr, $b: expr, $c:expr) => {{ + ($a: expr, $b: expr, $rot: expr) => {{ let v = $a ^ $b; - let r = S32 - $c; + let r = 32 - $rot; let right = v >> r; - $a = (v << $c) ^ right + $a = (v << $rot) ^ right }} } - const S32:u32x4 = u32x4(32, 32, 32, 32); - const S16:u32x4 = u32x4(16, 16, 16, 16); - const S12:u32x4 = u32x4(12, 12, 12, 12); - const S8:u32x4 = u32x4(8, 8, 8, 8); - const S7:u32x4 = u32x4(7, 7, 7, 7); - impl ChaCha20 { pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 { assert!(key.len() == 16 || key.len() == 32);