Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ferret #202

Open
wants to merge 1 commit into
base: alpha.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions crates/mpz-core/benches/ggm.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use mpz_core::{block::Block, ggm_tree::GgmTree};
use mpz_core::{block::Block, ggm::GgmTree};

#[allow(clippy::all)]
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("ggm::gen::1K", move |bench| {
let depth = 10;
let ggm = GgmTree::new(depth);
let mut tree = vec![Block::ZERO; 1 << (depth)];
let mut k0 = vec![Block::ZERO; depth];
let mut k1 = vec![Block::ZERO; depth];
let seed = rand::random::<Block>();
let mut leaves = vec![Block::ZERO; 1 << depth];
bench.iter(|| {
black_box(ggm.gen(
black_box(seed),
black_box(&mut tree),
black_box(&mut k0),
black_box(&mut k1),
));
GgmTree::new_from_seed(depth, seed, &mut leaves);
black_box(&leaves);
});
});

c.bench_function("ggm::reconstruction::1K", move |bench| {
let depth = 10;
let ggm = GgmTree::new(depth);
let mut tree = vec![Block::ZERO; 1 << (depth)];
let k = vec![Block::ZERO; depth];
let alpha = vec![false; depth];
let sums = vec![Block::ZERO; depth];
let mut leaves = vec![Block::ZERO; 1 << depth];
bench.iter(|| {
black_box(ggm.reconstruct(black_box(&mut tree), black_box(&k), black_box(&alpha)))
GgmTree::new_partial(depth, &sums, 420, &mut leaves);
black_box(&leaves);
});
});
}
Expand Down
12 changes: 6 additions & 6 deletions crates/mpz-core/benches/lpn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,47 @@ fn criterion_benchmark(c: &mut Criterion) {
let seed = Block::ZERO;
let k = 5_060;
let n = 166_400;
let lpn = LpnEncoder::<10>::new(seed, k);
let lpn = LpnEncoder::<10>::new(k);
let mut x = vec![Block::ZERO; k as usize];
let mut y = vec![Block::ZERO; n];
let mut prg = Prg::new();
prg.random_blocks(&mut x);
prg.random_blocks(&mut y);
bench.iter(|| {
#[allow(clippy::unit_arg)]
black_box(lpn.compute(&mut y, &x));
black_box(lpn.compute(seed, &mut y, &x));
});
});

c.bench_function("lpn-rayon-medium", move |bench| {
let seed = Block::ZERO;
let k = 158_000;
let n = 10_168_320;
let lpn = LpnEncoder::<10>::new(seed, k);
let lpn = LpnEncoder::<10>::new(k);
let mut x = vec![Block::ZERO; k as usize];
let mut y = vec![Block::ZERO; n];
let mut prg = Prg::new();
prg.random_blocks(&mut x);
prg.random_blocks(&mut y);
bench.iter(|| {
#[allow(clippy::unit_arg)]
black_box(lpn.compute(&mut y, &x));
black_box(lpn.compute(seed, &mut y, &x));
});
});

c.bench_function("lpn-rayon-large", move |bench| {
let seed = Block::ZERO;
let k = 588_160;
let n = 10_616_092;
let lpn = LpnEncoder::<10>::new(seed, k);
let lpn = LpnEncoder::<10>::new(k);
let mut x = vec![Block::ZERO; k as usize];
let mut y = vec![Block::ZERO; n];
let mut prg = Prg::new();
prg.random_blocks(&mut x);
prg.random_blocks(&mut y);
bench.iter(|| {
#[allow(clippy::unit_arg)]
black_box(lpn.compute(&mut y, &x));
black_box(lpn.compute(seed, &mut y, &x));
});
});
}
Expand Down
16 changes: 11 additions & 5 deletions crates/mpz-core/src/aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ impl FixedKeyAes {
.for_each(|(a, b)| *a ^= *b);
}

/// Circular correlation-robust hash function instantiated using fixed-key AES
/// (cf.<https://eprint.iacr.org/2019/074>, §7.3).
/// Circular correlation-robust hash function instantiated using fixed-key
/// AES (cf.<https://eprint.iacr.org/2019/074>, §7.3).
///
/// `π(σ(x)) ⊕ σ(x)`, where `π` is instantiated using fixed-key AES
///
Expand All @@ -120,8 +120,8 @@ impl FixedKeyAes {
self.cr(Block::sigma(block))
}

/// Circular correlation-robust hash function instantiated using fixed-key AES
/// (cf.<https://eprint.iacr.org/2019/074>, §7.3).
/// Circular correlation-robust hash function instantiated using fixed-key
/// AES (cf.<https://eprint.iacr.org/2019/074>, §7.3).
///
/// `π(σ(x)) ⊕ σ(x)`, where `π` is instantiated using fixed-key AES
///
Expand Down Expand Up @@ -159,6 +159,11 @@ impl AesEncryptor {
blk
}

/// Encrypt a block in-place.
pub fn encrypt_block_inplace(&self, blk: &mut Block) {
self.0.encrypt_block(blk.as_generic_array_mut());
}

/// Encrypt many blocks in-place.
#[inline(always)]
pub fn encrypt_many_blocks<const N: usize>(&self, blks: &mut [Block; N]) {
Expand All @@ -177,7 +182,8 @@ impl AesEncryptor {
///
/// Each batch of NM blocks is encrypted by a corresponding AES key.
///
/// **Only the first NK * NM blocks of blks are handled, the rest are ignored.**
/// **Only the first NK * NM blocks of blks are handled, the rest are
/// ignored.**
///
/// # Arguments
///
Expand Down
37 changes: 37 additions & 0 deletions crates/mpz-core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use generic_array::{typenum::consts::U16, GenericArray};
use itybity::{BitIterable, BitLength, FromBitIterator, GetBit, Lsb0, Msb0};
use rand::{distributions::Standard, prelude::Distribution, CryptoRng, Rng};
use serde::{Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
slice::from_raw_parts,
};

/// A block of 128 bits
#[repr(transparent)]
Expand Down Expand Up @@ -147,6 +151,28 @@ impl Block {
bytemuck::cast([x[1], x[0]])
}

/// Converts a slice of blocks to a slice of bytes.
pub fn as_flattened_bytes(slice: &[Self]) -> &[u8] {
// This is equivalent to `<[[u8; 16]]>::as_flattened`

// SAFETY: `slice.len() * Block::LEN` cannot overflow because `slice` is
// already in the address space.
let len = unsafe { slice.len().unchecked_mul(Self::LEN) };
// SAFETY: `[u8]` is layout-identical to `[u8; 16]` of which block is a newtype.
unsafe { from_raw_parts(slice.as_ptr().cast(), len) }
}

/// Converts a slice of block arrays to a slice of bytes.
pub fn array_as_flattened_bytes<const N: usize>(slice: &[[Self; N]]) -> &[u8] {
// This is equivalent to `<[[u8; 16 * N]]>::as_flattened`

// SAFETY: `slice.len() * N * Block::LEN` cannot overflow because `slice` is
// already in the address space.
let len = unsafe { slice.len().unchecked_mul(N * Self::LEN) };
// SAFETY: `[u8]` is layout-identical to `[u8; 16]` of which block is a newtype.
unsafe { from_raw_parts(slice.as_ptr().cast(), len) }
}

/// Converts a block to a [`GenericArray<u8,
/// U16>`](cipher::generic_array::GenericArray) from the [`generic-array`](https://docs.rs/generic-array/latest/generic_array/) crate.
#[allow(dead_code)]
Expand Down Expand Up @@ -182,6 +208,17 @@ impl Block {
}
}

impl Display for Block {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Block(")?;
for byte in self.0.iter() {
write!(f, "{:02x}", byte)?;
}
f.write_str(")")?;
Ok(())
}
}

/// A trait for converting a type to blocks
pub trait BlockSerialize {
/// The block representation of the type
Expand Down
Loading