use dudect_bencher::rand::RngExt;
use dudect_bencher::{ctbench_main, BenchRng, Class, CtRunner};
use quantum_shield::{seal, Envelope, HybridCrypto};
const SAMPLES: usize = 100_000;
fn open_wrong_recipient(runner: &mut CtRunner, rng: &mut BenchRng) {
let recipient = HybridCrypto::generate().unwrap();
let other = HybridCrypto::generate().unwrap();
let inputs: Vec<(Envelope, Class)> = (0..SAMPLES)
.map(|_| {
if rng.random::<bool>() {
(
seal(b"constant-time probe", recipient.public_keys()).unwrap(),
Class::Left,
)
} else {
(
seal(b"constant-time probe", other.public_keys()).unwrap(),
Class::Right,
)
}
})
.collect();
for (envelope, class) in inputs {
runner.run_one(class, || {
let _ = recipient.open(&envelope);
});
}
}
fn open_tampered_ciphertext(runner: &mut CtRunner, rng: &mut BenchRng) {
let recipient = HybridCrypto::generate().unwrap();
let inputs: Vec<(Envelope, Class)> = (0..SAMPLES)
.map(|_| {
let envelope = seal(b"constant-time probe", recipient.public_keys()).unwrap();
if rng.random::<bool>() {
(envelope, Class::Left)
} else {
let mut bytes = envelope.to_bytes();
bytes[10] ^= 0x01;
(Envelope::from_bytes(&bytes).unwrap(), Class::Right)
}
})
.collect();
for (envelope, class) in inputs {
runner.run_one(class, || {
let _ = recipient.open(&envelope);
});
}
}
ctbench_main!(open_wrong_recipient, open_tampered_ciphertext);