vdf_eval.rs
1 /* This file is part of DarkFi (https://dark.fi) 2 * 3 * Copyright (C) 2020-2025 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 19 //! Test unit for evaluating VDF speed 20 // cargo test --release --all-features --test vdf_eval -- --nocapture --include-ignored 21 use std::{ 22 collections::HashMap, 23 time::{Duration, Instant}, 24 }; 25 26 use darkfi_sdk::{crypto::mimc_vdf, num_bigint::BigUint, num_traits::Num}; 27 use prettytable::{format, row, Table}; 28 29 #[test] 30 #[ignore] 31 fn evaluate_vdf() { 32 let steps = [ 33 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000, 15000, 20000, 50000, 100000, 34 150000, 200000, 250000, 500000, 1000000, 1250000, 1500000, 1750000, 2000000, 35 ]; 36 37 let challenge = blake3::hash(b"69420").to_hex(); 38 let challenge = BigUint::from_str_radix(&challenge, 16).unwrap(); 39 40 let mut map: HashMap<u64, (Duration, Duration)> = HashMap::new(); 41 42 for n_steps in steps { 43 let now = Instant::now(); 44 print!("E with N={n_steps} ... "); 45 let witness = mimc_vdf::eval(&challenge, n_steps); 46 let eval_elapsed = now.elapsed(); 47 println!("{eval_elapsed:?}"); 48 49 let now = Instant::now(); 50 print!("V with N={n_steps} ... "); 51 assert!(mimc_vdf::verify(&challenge, n_steps, &witness)); 52 let verify_elapsed = now.elapsed(); 53 println!("{verify_elapsed:?}"); 54 55 map.insert(n_steps, (eval_elapsed, verify_elapsed)); 56 } 57 58 let mut table = Table::new(); 59 table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR); 60 table.set_titles(row!["n_steps", "eval time", "verify time"]); 61 for n_steps in steps { 62 let (eval, verify) = map.get(&n_steps).unwrap(); 63 table.add_row(row![format!("{n_steps}"), format!("{eval:?}"), format!("{verify:?}")]); 64 } 65 66 println!("\n\n{table}"); 67 }