Debian Codegraph @

debian/rust-vendor/num-prime/examples/profile_factorization.rs

raw
1 use std::fs::File;
2 use std::io::{Error, Write};
3 use std::time::Instant;
4
5 use num_prime::factor::{one_line, pollard_rho, squfof, SQUFOF_MULTIPLIERS};
6 use num_prime::RandPrime;
7 use rand::random;
8
9 /// Collect the best case of each factorization algorithm
10 fn profile_n_min(n: u128) -> Vec<(String, usize)> {
11 let k_squfof: Vec<u16> = SQUFOF_MULTIPLIERS.to_vec();
12 let k_oneline: Vec<u16> = vec![1, 360, 480];
13 const MAXITER: usize = 1 << 24;
14 const POLLARD_REPEATS: usize = 4;
15
16 let mut n_stats = Vec::new();
17
18 // pollard rho
19 let mut pollard_best = (MAXITER, u128::MAX);
20 for _ in 0..POLLARD_REPEATS {
21 let tstart = Instant::now();
22 let (result, iters) = pollard_rho(&n, random(), random(), pollard_best.0);
23 if result.is_some() {
24 pollard_best = pollard_best.min((iters, tstart.elapsed().as_micros()));
25 }
26 }
27 n_stats.push(("pollard_rho".to_string(), pollard_best.0));
28 n_stats.push(("time_pollard_rho".to_string(), pollard_best.1 as usize));
29
30 // squfof
31 let mut squfof_best = (MAXITER, u128::MAX);
32 for &k in &k_squfof {
33 if let Some(kn) = n.checked_mul(u128::from(k)) {
34 let tstart = Instant::now();
35 let (result, iters) = squfof(&n, kn, squfof_best.0);
36 if result.is_some() {
37 squfof_best = squfof_best.min((iters, tstart.elapsed().as_micros()));
38 }
39 }
40 }
41 n_stats.push(("squfof".to_string(), squfof_best.0));
42 n_stats.push(("time_squfof".to_string(), squfof_best.1 as usize));
43
44 // one line
45 let mut oneline_best = (MAXITER, u128::MAX);
46 for &k in &k_oneline {
47 if let Some(kn) = n.checked_mul(u128::from(k)) {
48 let tstart = Instant::now();
49 let (result, iters) = one_line(&n, kn, oneline_best.0);
50 if result.is_some() {
51 oneline_best = oneline_best.min((iters, tstart.elapsed().as_micros()));
52 }
53 }
54 }
55 n_stats.push(("one_line".to_string(), oneline_best.0));
56 n_stats.push(("time_one_line".to_string(), squfof_best.1 as usize));
57
58 n_stats
59 }
60
61 /// This program try various factorization methods, and log down their iterations number into a csv file
62 fn main() -> Result<(), Error> {
63 let mut rng = rand::thread_rng();
64 const REPEATS: u32 = 4;
65
66 let mut n_list = Vec::<(u128, f32)>::new(); // n and bits of n
67 let mut stats: Vec<Vec<(String, usize)>> = Vec::new();
68
69 for total_bits in 20..120 {
70 for _ in 0..REPEATS {
71 let p1: u128 = rng.gen_prime(total_bits / 2, None);
72 let p2: u128 =
73 rng.gen_prime_exact(total_bits - (128 - p1.leading_zeros()) as usize, None);
74 if p1 == p2 {
75 continue;
76 }
77
78 let n = p1 * p2;
79 n_list.push((n, (n as f64).log2() as f32));
80 println!("Semiprime ({total_bits}bits): {n} = {p1} * {p2}");
81 stats.push(profile_n_min(n));
82 }
83 }
84
85 // Log into the CSV file
86 let mut fout = File::create("profile_stats.csv")?;
87 fout.write_all(b"n,n_bits")?;
88 for k in stats[0].iter().map(|(k, _)| k) {
89 fout.write_all(b",")?;
90 fout.write_all(k.as_bytes())?;
91 }
92
93 for ((n, bits), n_stats) in n_list.iter().zip(stats) {
94 fout.write_all(b"\n")?;
95 fout.write_all(n.to_string().as_bytes())?;
96 fout.write_all(b",")?;
97 fout.write_all(bits.to_string().as_bytes())?;
98 for (_, v) in n_stats {
99 fout.write_all(b",")?;
100 fout.write_all(v.to_string().as_bytes())?;
101 }
102 }
103
104 Ok(())
105 }