Skip to content

Commit 6d8b2e2

Browse files
committed
age: Use rayon for processing STREAM chunks in parallel
1 parent d992e13 commit 6d8b2e2

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

age/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ rust-embed = "5"
7777

7878
# Performance
7979
num_cpus = "1.0"
80+
rayon = "1.5"
8081

8182
# Common CLI dependencies
8283
console = { version = "0.14", optional = true }

age/src/primitives/stream.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use chacha20poly1305::{
66
};
77
use lazy_static::lazy_static;
88
use pin_project::pin_project;
9+
use rayon::prelude::*;
910
use secrecy::{ExposeSecret, SecretVec};
1011
use std::cmp;
1112
use std::convert::TryInto;
@@ -53,9 +54,9 @@ impl Nonce {
5354
self.0 = u128::from(val) << 8;
5455
}
5556

56-
fn increment_counter(&mut self) {
57+
fn increment_counter(&mut self, by: usize) {
5758
// Increment the 11-byte counter
58-
self.0 += 1 << 8;
59+
self.0 += (by as u128) << 8;
5960
if self.0 >> (8 * 12) != 0 {
6061
panic!("We overflowed the nonce!");
6162
}
@@ -196,26 +197,29 @@ impl Stream {
196197
let num_chunks = chunks.len();
197198
let mut encrypted = vec![0; chunks_len + TAG_SIZE * num_chunks];
198199

199-
for (i, (encrypted, chunk)) in encrypted
200+
encrypted
200201
.chunks_mut(ENCRYPTED_CHUNK_SIZE)
201202
.zip(chunks)
202203
.enumerate()
203-
{
204-
if i + 1 == num_chunks {
205-
self.nonce.set_last(last).unwrap();
206-
}
204+
.par_bridge()
205+
.for_each_with(self.nonce, |nonce, (i, (encrypted, chunk))| {
206+
nonce.increment_counter(i);
207+
if i + 1 == num_chunks {
208+
nonce.set_last(last).unwrap();
209+
}
207210

208-
let (buffer, tag) = encrypted.split_at_mut(chunk.len());
209-
buffer.copy_from_slice(chunk);
210-
tag.copy_from_slice(
211-
self.aead
212-
.encrypt_in_place_detached(&self.nonce.to_bytes().into(), &[], buffer)
213-
.expect("we will never hit chacha20::MAX_BLOCKS because of the chunk size")
214-
.as_slice(),
215-
);
211+
let (buffer, tag) = encrypted.split_at_mut(chunk.len());
212+
buffer.copy_from_slice(chunk);
213+
tag.copy_from_slice(
214+
self.aead
215+
.encrypt_in_place_detached(&nonce.to_bytes().into(), &[], buffer)
216+
.expect("we will never hit chacha20::MAX_BLOCKS because of the chunk size")
217+
.as_slice(),
218+
);
219+
});
216220

217-
self.nonce.increment_counter();
218-
}
221+
self.nonce.increment_counter(num_chunks);
222+
self.nonce.set_last(last).unwrap();
219223

220224
Ok(encrypted)
221225
}
@@ -250,7 +254,7 @@ impl Stream {
250254
)
251255
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "decryption error"))?;
252256

253-
self.nonce.increment_counter();
257+
self.nonce.increment_counter(1);
254258
}
255259

256260
Ok(SecretVec::new(decrypted))

0 commit comments

Comments
 (0)