vdf_eval.rs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Test unit for evaluating VDF speed
  19. // cargo test --release --all-features --test vdf_eval -- --nocapture --include-ignored
  20. use std::{
  21. collections::HashMap,
  22. time::{Duration, Instant},
  23. };
  24. use darkfi_sdk::{crypto::mimc_vdf, num_bigint::BigUint, num_traits::Num};
  25. use prettytable::{format, row, Table};
  26. #[test]
  27. #[ignore]
  28. fn evaluate_vdf() {
  29. let steps = [
  30. 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 15000, 20000, 50000, 100000,
  31. 150000, 200000, 250000, 500000, 1000000, 1250000, 1500000, 1750000, 2000000,
  32. ];
  33. let challenge = blake3::hash(b"69420").to_hex();
  34. let challenge = BigUint::from_str_radix(&challenge, 16).unwrap();
  35. let mut map: HashMap<u64, (Duration, Duration)> = HashMap::new();
  36. for n_steps in steps {
  37. let now = Instant::now();
  38. print!("E with N={} ... ", n_steps);
  39. let witness = mimc_vdf::eval(&challenge, n_steps);
  40. let eval_elapsed = now.elapsed();
  41. println!("{:?}", eval_elapsed);
  42. let now = Instant::now();
  43. print!("V with N={} ... ", n_steps);
  44. assert!(mimc_vdf::verify(&challenge, n_steps, &witness));
  45. let verify_elapsed = now.elapsed();
  46. println!("{:?}", verify_elapsed);
  47. map.insert(n_steps, (eval_elapsed, verify_elapsed));
  48. }
  49. let mut table = Table::new();
  50. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  51. table.set_titles(row!["n_steps", "eval time", "verify time"]);
  52. for n_steps in steps {
  53. let (eval, verify) = map.get(&n_steps).unwrap();
  54. table.add_row(row![format!("{}", n_steps), format!("{:?}", eval), format!("{:?}", verify)]);
  55. }
  56. println!("\n\n{}", table);
  57. }