Skip to content

Commit

Permalink
muhash: Improve benchmarks, and add inverse check in debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Dec 23, 2022
1 parent 04d52ff commit 6e64a9d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
18 changes: 9 additions & 9 deletions crypto/muhash/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ use muhash::MuHash;

fn bench_muhash(c: &mut Criterion) {
let mut rng = ChaCha8Rng::from_seed([42u8; 32]);
let mut rand_set = MuHash::new();

let mut data = [0u8; 100];
// Set the numerator and denominators.
rng.fill_bytes(&mut data);
rand_set.add_element(&data);
rng.fill_bytes(&mut data);
rand_set.remove_element(&data);

rng.fill_bytes(&mut data);
let mut rand_set_serialized = [0u8; 384];
rng.fill_bytes(&mut rand_set_serialized);
let mut rand_set = MuHash::deserialize(rand_set_serialized).unwrap();

c.bench_function("MuHash::add_element", |b| {
let mut muhash = MuHash::new();
Expand Down Expand Up @@ -62,14 +66,10 @@ fn bench_muhash(c: &mut Criterion) {
b.iter(|| black_box(muhash.clone()).serialize())
});

c.bench_function("MuHash::serialize rand", |b| {
let muhash = MuHash::deserialize(rand_set_serialized).unwrap();
b.iter(|| black_box(muhash.clone()).serialize())
});
c.bench_function("MuHash::serialize rand", |b| b.iter(|| black_box(rand_set.clone()).serialize()));

c.bench_function("MuHash::finalize", |b| {
let muhash = MuHash::deserialize(rand_set_serialized).unwrap();
b.iter(|| black_box(muhash.clone()).finalize());
b.iter(|| black_box(rand_set.clone()).finalize());
});
}

Expand Down
9 changes: 7 additions & 2 deletions crypto/muhash/src/u3072.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ impl U3072 {
a.full_reduce();
}
// The only value that doesn't have a multiplicative inverse is 0, and 0/x is 0.
let inv = Uint3072(a.limbs).mod_inverse(Self::UINT_PRIME).unwrap_or_default();
Self { limbs: inv.0 }
let inv = Self { limbs: Uint3072(a.limbs).mod_inverse(Self::UINT_PRIME).unwrap_or_default().0 };
if cfg!(debug_assertions) {
let mut one = inv;
one *= a;
assert_eq!(one, Self::one());
}
inv
}

fn div(&mut self, other: &Self) {
Expand Down

0 comments on commit 6e64a9d

Please sign in to comment.