Skip to content

Commit

Permalink
change encrypt_many_blocks to operate in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
sinui0 committed Oct 6, 2023
1 parent 5347d6e commit 142100a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions mpz-core/benches/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("aes::encrypt_many_blocks::<8>", move |bench| {
let key = rand::random::<Block>();
let aes = AesEncryptor::new(key);
let blks = rand::random::<[Block; 8]>();
let mut blks = rand::random::<[Block; 8]>();

bench.iter(|| {
let z = aes.encrypt_many_blocks(black_box(blks));
let z = aes.encrypt_many_blocks(black_box(&mut blks));
black_box(z);
});
});
Expand Down
5 changes: 2 additions & 3 deletions mpz-core/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,11 @@ impl AesEncryptor {
blk
}

/// Encrypt many blocks.
/// Encrypt many blocks in-place.
#[inline(always)]
pub fn encrypt_many_blocks<const N: usize>(&self, mut blks: [Block; N]) -> [Block; N] {
pub fn encrypt_many_blocks<const N: usize>(&self, blks: &mut [Block; N]) {
self.0
.encrypt_blocks(Block::as_generic_array_mut_slice(blks.as_mut_slice()));
blks
}

/// Encrypt slice of blocks in-place.
Expand Down
5 changes: 3 additions & 2 deletions mpz-core/src/prg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ impl BlockRngCore for PrgCore {
// Compute [AES(state)..AES(state+8)]
#[inline(always)]
fn generate(&mut self, results: &mut Self::Results) {
let states = [0; AesEncryptor::AES_BLOCK_COUNT].map(
let mut states = [0; AesEncryptor::AES_BLOCK_COUNT].map(
#[inline(always)]
|_| {
let x = self.state;
self.state += 1;
Block::from(bytemuck::cast::<_, [u8; 16]>([x, 0u64]))
},
);
*results = bytemuck::cast(self.aes.encrypt_many_blocks(states))
self.aes.encrypt_many_blocks(&mut states);
*results = bytemuck::cast(states);
}
}

Expand Down

0 comments on commit 142100a

Please sign in to comment.