Skip to content

Commit e70ee3b

Browse files
committed
Define a BLOCK_SIZE constant for chacha20
1 parent fa9fbcd commit e70ee3b

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lightning/src/util/chacha20.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ mod real_chacha {
5656
}
5757
}
5858

59+
const BLOCK_SIZE: usize = 64;
60+
5961
#[derive(Clone,Copy)]
6062
struct ChaChaState {
6163
a: u32x4,
@@ -67,7 +69,7 @@ mod real_chacha {
6769
#[derive(Copy)]
6870
pub struct ChaCha20 {
6971
state : ChaChaState,
70-
output : [u8; 64],
72+
output : [u8; BLOCK_SIZE],
7173
offset : usize,
7274
}
7375

@@ -135,7 +137,7 @@ mod real_chacha {
135137
assert!(key.len() == 16 || key.len() == 32);
136138
assert!(nonce.len() == 8 || nonce.len() == 12);
137139

138-
ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; 64], offset: 64 }
140+
ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; BLOCK_SIZE], offset: 64 }
139141
}
140142

141143
fn expand(key: &[u8], nonce: &[u8]) -> ChaChaState {
@@ -197,7 +199,7 @@ mod real_chacha {
197199
}
198200
}
199201

200-
// put the the next 64 keystream bytes into self.output
202+
// put the the next BLOCK_SIZE keystream bytes into self.output
201203
fn update(&mut self) {
202204
let mut state = self.state;
203205

@@ -234,12 +236,12 @@ mod real_chacha {
234236
while i < len {
235237
// If there is no keystream available in the output buffer,
236238
// generate the next block.
237-
if self.offset == 64 {
239+
if self.offset == BLOCK_SIZE {
238240
self.update();
239241
}
240242

241243
// Process the min(available keystream, remaining input length).
242-
let count = cmp::min(64 - self.offset, len - i);
244+
let count = cmp::min(BLOCK_SIZE - self.offset, len - i);
243245
// explicitly assert lengths to avoid bounds checks:
244246
assert!(output.len() >= i + count);
245247
assert!(input.len() >= i + count);
@@ -258,12 +260,12 @@ mod real_chacha {
258260
while i < len {
259261
// If there is no keystream available in the output buffer,
260262
// generate the next block.
261-
if self.offset == 64 {
263+
if self.offset == BLOCK_SIZE {
262264
self.update();
263265
}
264266

265267
// Process the min(available keystream, remaining input length).
266-
let count = cmp::min(64 - self.offset, len - i);
268+
let count = cmp::min(BLOCK_SIZE - self.offset, len - i);
267269
// explicitly assert lengths to avoid bounds checks:
268270
assert!(input_output.len() >= i + count);
269271
assert!(self.output.len() >= self.offset + count);

0 commit comments

Comments
 (0)