Skip to content

Commit

Permalink
fix: rename encrypt fns (#75)
Browse files Browse the repository at this point in the history
* rename encrypt fns

* change encrypt_many_blocks to operate in-place
  • Loading branch information
sinui0 authored Oct 9, 2023
1 parent 59e78e0 commit f6ae4b3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion garble/mpz-garble-core/src/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub(crate) fn and_gate(
let k = Block::new(((gid + 1) as u128).to_be_bytes());

let mut h = [x, y];
cipher.tccr_many_inplace(&[j, k], &mut h);
cipher.tccr_many(&[j, k], &mut h);

let [hx, hy] = h;

Expand Down
2 changes: 1 addition & 1 deletion garble/mpz-garble-core/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) fn and_gate(
let k = Block::new(((gid + 1) as u128).to_be_bytes());

let mut h = [x_0, y_0, x_1, y_1];
cipher.tccr_many_inplace(&[j, k, j, k], &mut h);
cipher.tccr_many(&[j, k, j, k], &mut h);

let [hx_0, hy_0, hx_1, hy_1] = h;

Expand Down
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
19 changes: 9 additions & 10 deletions mpz-core/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl FixedKeyAes {
/// * `tweaks` - The tweaks to use for each block in `blocks`.
/// * `blocks` - The blocks to hash in-place.
#[inline]
pub fn tccr_many_inplace<const N: usize>(&self, tweaks: &[Block; N], blocks: &mut [Block; N]) {
pub fn tccr_many<const N: usize>(&self, tweaks: &[Block; N], blocks: &mut [Block; N]) {
// Store π(x) in `blocks`
self.aes
.encrypt_blocks(Block::as_generic_array_mut_slice(blocks));
Expand Down Expand Up @@ -90,7 +90,7 @@ impl FixedKeyAes {
///
/// * `blocks` - The blocks to hash in-place.
#[inline]
pub fn cr_many_inplace<const N: usize>(&self, blocks: &mut [Block; N]) {
pub fn cr_many<const N: usize>(&self, blocks: &mut [Block; N]) {
let mut buf = *blocks;

self.aes
Expand Down Expand Up @@ -124,9 +124,9 @@ impl FixedKeyAes {
///
/// * `blocks` - The blocks to hash in-place.
#[inline]
pub fn ccr_many_inplace<const N: usize>(&self, blocks: &mut [Block; N]) {
pub fn ccr_many<const N: usize>(&self, blocks: &mut [Block; N]) {
blocks.iter_mut().for_each(|b| *b = Block::sigma(*b));
self.cr_many_inplace(blocks);
self.cr_many(blocks);
}
}

Expand All @@ -152,17 +152,16 @@ 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 many blocks in-place.
/// Encrypt slice of blocks in-place.
#[inline]
pub fn encrypt_many_blocks_inplace(&self, blks: &mut [Block]) {
pub fn encrypt_blocks(&self, blks: &mut [Block]) {
self.0
.encrypt_blocks(Block::as_generic_array_mut_slice(blks));
}
Expand All @@ -188,7 +187,7 @@ impl AesEncryptor {
keys.iter()
.zip(blks.chunks_exact_mut(NM))
.for_each(|(key, blks)| {
key.encrypt_many_blocks_inplace(blks);
key.encrypt_blocks(blks);
});
}
}
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 f6ae4b3

Please sign in to comment.