Skip to content

Commit 37150b4

Browse files
Merge pull request #2764 from TheBlueMatt/2023-11-chacha-cleanup
Trivial ChaCha cleanups
2 parents 242e6ae + c79cf82 commit 37150b4

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

lightning/src/util/chacha20.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod real_chacha {
2121
struct u32x4(pub u32, pub u32, pub u32, pub u32);
2222
impl ::core::ops::Add for u32x4 {
2323
type Output = u32x4;
24+
#[inline]
2425
fn add(self, rhs: u32x4) -> u32x4 {
2526
u32x4(self.0.wrapping_add(rhs.0),
2627
self.1.wrapping_add(rhs.1),
@@ -30,6 +31,7 @@ mod real_chacha {
3031
}
3132
impl ::core::ops::Sub for u32x4 {
3233
type Output = u32x4;
34+
#[inline]
3335
fn sub(self, rhs: u32x4) -> u32x4 {
3436
u32x4(self.0.wrapping_sub(rhs.0),
3537
self.1.wrapping_sub(rhs.1),
@@ -39,23 +41,27 @@ mod real_chacha {
3941
}
4042
impl ::core::ops::BitXor for u32x4 {
4143
type Output = u32x4;
44+
#[inline]
4245
fn bitxor(self, rhs: u32x4) -> u32x4 {
4346
u32x4(self.0 ^ rhs.0, self.1 ^ rhs.1, self.2 ^ rhs.2, self.3 ^ rhs.3)
4447
}
4548
}
46-
impl ::core::ops::Shr<u32x4> for u32x4 {
49+
impl ::core::ops::Shr<u8> for u32x4 {
4750
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)
51+
#[inline]
52+
fn shr(self, shr: u8) -> u32x4 {
53+
u32x4(self.0 >> shr, self.1 >> shr, self.2 >> shr, self.3 >> shr)
5054
}
5155
}
52-
impl ::core::ops::Shl<u32x4> for u32x4 {
56+
impl ::core::ops::Shl<u8> for u32x4 {
5357
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)
58+
#[inline]
59+
fn shl(self, shl: u8) -> u32x4 {
60+
u32x4(self.0 << shl, self.1 << shl, self.2 << shl, self.3 << shl)
5661
}
5762
}
5863
impl u32x4 {
64+
#[inline]
5965
fn from_bytes(bytes: &[u8]) -> Self {
6066
assert_eq!(bytes.len(), 4*4);
6167
Self (
@@ -118,31 +124,25 @@ mod real_chacha {
118124
macro_rules! round{
119125
($state: expr) => {{
120126
$state.a = $state.a + $state.b;
121-
rotate!($state.d, $state.a, S16);
127+
rotate!($state.d, $state.a, 16);
122128
$state.c = $state.c + $state.d;
123-
rotate!($state.b, $state.c, S12);
129+
rotate!($state.b, $state.c, 12);
124130
$state.a = $state.a + $state.b;
125-
rotate!($state.d, $state.a, S8);
131+
rotate!($state.d, $state.a, 8);
126132
$state.c = $state.c + $state.d;
127-
rotate!($state.b, $state.c, S7);
133+
rotate!($state.b, $state.c, 7);
128134
}}
129135
}
130136

131137
macro_rules! rotate {
132-
($a: expr, $b: expr, $c:expr) => {{
138+
($a: expr, $b: expr, $rot: expr) => {{
133139
let v = $a ^ $b;
134-
let r = S32 - $c;
140+
let r = 32 - $rot;
135141
let right = v >> r;
136-
$a = (v << $c) ^ right
142+
$a = (v << $rot) ^ right
137143
}}
138144
}
139145

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-
146146
impl ChaCha20 {
147147
pub fn new(key: &[u8], nonce: &[u8]) -> ChaCha20 {
148148
assert!(key.len() == 16 || key.len() == 32);

0 commit comments

Comments
 (0)