Skip to content

Commit

Permalink
test(crypto): Add a cheating dealer test
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Jan 29, 2024
1 parent ddda908 commit 82c6a46
Showing 1 changed file with 49 additions and 5 deletions.
Expand Up @@ -255,14 +255,24 @@ fn slightly_dishonest_dlog() {
assert_eq!(cheat_solver.solve(&(base * &answer)), Some(answer));
}

fn cheating_dlog_instance(m: usize) -> (Scalar, Gt) {
let rng = &mut reproducible_rng();

fn cheating_dlog_instance<R: rand::RngCore + rand::CryptoRng>(
m: usize,
rng: &mut R,
use_max_delta: bool,
) -> (Scalar, Gt) {
let z = 1069531200 * 16 * m as u64;

let s = Scalar::from_u64(rng.gen::<u64>() % z);

let delta = std::cmp::max(1, rng.gen::<u16>() % 10) as u64;
let delta = if use_max_delta {
// Instead of maximum delta (255) we use the largest
// delta that is prime. This leads to consistent results
// from run to run, and is only slightly faster than
// the most pessimal delta.
251
} else {
std::cmp::max(1, rng.gen::<u16>() % 10)
} as u64;
let delta = Scalar::from_u64(delta);

let delta_inv = delta.inverse().expect("Delta not invertible");
Expand All @@ -275,13 +285,47 @@ fn cheating_dlog_instance(m: usize) -> (Scalar, Gt) {

#[test]
fn test_that_cheating_dealer_solver_can_solve_instance() {
let rng = &mut reproducible_rng();

let m = 29;
let solver = CheatingDealerDlogSolver::new(m, 16);

let (solution, target) = cheating_dlog_instance(m);
let (solution, target) = cheating_dlog_instance(m, rng, false);

assert_eq!(
solution,
solver.solve(&target).expect("Unable to solve dlog")
);
}

#[test]
#[ignore]
fn print_time_for_cheating_dlog_solver_to_run() {
let rng = &mut reproducible_rng();

let subnet_size = 28;
let total_tests = 16; // one fully bad dealing

let table_start = std::time::SystemTime::now();
let solver = CheatingDealerDlogSolver::new(subnet_size, 16);
println!(
"Created table for {} nodes in {:?}",
subnet_size,
table_start.elapsed().unwrap()
);

let tests = (0..total_tests)
.map(|_| cheating_dlog_instance(subnet_size, rng, true))
.collect::<Vec<_>>();

for (solution, target) in tests {
let solve_start = std::time::SystemTime::now();

assert_eq!(
solution,
solver.solve(&target).expect("Unable to solve dlog")
);

println!("Solved an instance in {:?}", solve_start.elapsed().unwrap());
}
}

0 comments on commit 82c6a46

Please sign in to comment.