Skip to content

Commit e0480b5

Browse files
committed
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.
1 parent f07f4b9 commit e0480b5

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

lightning/src/util/chacha20.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ mod real_chacha {
4343
u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3)
4444
}
4545
}
46-
impl ::core::ops::Shr<u32x4> for u32x4 {
46+
impl ::core::ops::Shr<u8> for u32x4 {
4747
type Output = u32x4;
48-
fn shr(self, rhs: u32x4) -> u32x4 {
49-
u32x4(self.0 >> rhs.0, self.1 >> rhs.1, self.2 >> rhs.2, self.3 >> rhs.3)
48+
fn shr(self, shr: u8) -> u32x4 {
49+
u32x4(self.0 >> shr, self.1 >> shr, self.2 >> shr, self.3 >> shr)
5050
}
5151
}
52-
impl ::core::ops::Shl<u32x4> for u32x4 {
52+
impl ::core::ops::Shl<u8> for u32x4 {
5353
type Output = u32x4;
54-
fn shl(self, rhs: u32x4) -> u32x4 {
55-
u32x4(self.0 << rhs.0, self.1 << rhs.1, self.2 << rhs.2, self.3 << rhs.3)
54+
fn shl(self, shl: u8) -> u32x4 {
55+
u32x4(self.0 << shl, self.1 << shl, self.2 << shl, self.3 << shl)
5656
}
5757
}
5858
impl u32x4 {
@@ -118,31 +118,25 @@ mod real_chacha {
118118
macro_rules! round{
119119
($state: expr) => {{
120120
$state.a = $state.a + $state.b;
121-
rotate!($state.d, $state.a, S16);
121+
rotate!($state.d, $state.a, 16);
122122
$state.c = $state.c + $state.d;
123-
rotate!($state.b, $state.c, S12);
123+
rotate!($state.b, $state.c, 12);
124124
$state.a = $state.a + $state.b;
125-
rotate!($state.d, $state.a, S8);
125+
rotate!($state.d, $state.a, 8);
126126
$state.c = $state.c + $state.d;
127-
rotate!($state.b, $state.c, S7);
127+
rotate!($state.b, $state.c, 7);
128128
}}
129129
}
130130

131131
macro_rules! rotate {
132-
($a: expr, $b: expr, $c:expr) => {{
132+
($a: expr, $b: expr, $rot: expr) => {{
133133
let v = $a ^ $b;
134-
let r = S32 - $c;
134+
let r = 32 - $rot;
135135
let right = v >> r;
136-
$a = (v << $c) ^ right
136+
$a = (v << $rot) ^ right
137137
}}
138138
}
139139

140-
const S32:u32x4 = u32x4(32, 32, 32, 32);
141-
const S16:u32x4 = u32x4(16, 16, 16, 16);
142-
const S12:u32x4 = u32x4(12, 12, 12, 12);
143-
const S8:u32x4 = u32x4(8, 8, 8, 8);
144-
const S7:u32x4 = u32x4(7, 7, 7, 7);
145-
146140
impl ChaCha20 {
147141
pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
148142
assert!(key.len() == 16 || key.len() == 32);

0 commit comments

Comments
 (0)