arctic.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. use arctic::arctic;
  2. use arctic::R1Output;
  3. use curve25519_dalek::scalar::Scalar;
  4. use rand::RngCore;
  5. use std::env;
  6. use std::time::Instant;
  7. fn mean(vals: &[f64]) -> f64 {
  8. let num = vals.len();
  9. if num > 0 {
  10. vals.iter().sum::<f64>() / (num as f64)
  11. } else {
  12. 0f64
  13. }
  14. }
  15. fn stddev(vals: &[f64]) -> f64 {
  16. let num = vals.len();
  17. if num < 2 {
  18. return 0f64;
  19. }
  20. let avg = mean(vals);
  21. (vals
  22. .iter()
  23. .map(|v| v - avg)
  24. .map(|dev| dev * dev)
  25. .sum::<f64>()
  26. / ((num - 1) as f64))
  27. .sqrt()
  28. }
  29. fn main() {
  30. let args: Vec<String> = env::args().collect();
  31. if args.len() < 4 || args.len() > 5 {
  32. println!("Usage: {} n t Csize [reps]", args[0]);
  33. return;
  34. }
  35. let n: u32 = args[1].parse().unwrap();
  36. let t: u32 = args[2].parse().unwrap();
  37. let msglen: usize = 100;
  38. let coalitionsize: u32 = args[3].parse().unwrap();
  39. let mut reps = 1usize;
  40. if args.len() > 4 {
  41. reps = args[4].parse().unwrap();
  42. }
  43. let mut msg: Vec<u8> = Vec::new();
  44. let mut rng = rand::thread_rng();
  45. msg.resize(msglen, 0);
  46. assert!(t >= 1);
  47. assert!(coalitionsize >= 2 * t - 1);
  48. assert!(n >= coalitionsize);
  49. let (pubkey, _, mut seckeys) = arctic::keygen(n, t);
  50. let delta = seckeys[0].delta();
  51. let mut sign1_timings: Vec<f64> = Vec::new();
  52. let mut sign2_timings: Vec<f64> = Vec::new();
  53. let mut combine_timings: Vec<f64> = Vec::new();
  54. let coalition = (1..=coalitionsize).collect::<Vec<u32>>();
  55. seckeys.truncate(coalitionsize as usize);
  56. let polys = arctic::lagrange_polys(&coalition);
  57. rayon::ThreadPoolBuilder::new().build_global().unwrap();
  58. println!("# num_threads = {}", rayon::current_num_threads());
  59. for _ in 0..reps {
  60. rng.fill_bytes(&mut msg);
  61. let (r1_outputs, sign1_iter_timings): (Vec<R1Output>, Vec<f64>) = seckeys
  62. .iter()
  63. .map(|key| {
  64. let sign1start = Instant::now();
  65. let r1_output = arctic::sign1(key, &coalition, &msg);
  66. let sign1dur = sign1start.elapsed().as_micros() as f64;
  67. (r1_output, sign1dur)
  68. })
  69. .unzip();
  70. sign1_timings.extend(sign1_iter_timings);
  71. let (sigshares, sign2_iter_timings): (Vec<Scalar>, Vec<f64>) = seckeys
  72. .iter()
  73. .map(|key| {
  74. let sign2start = Instant::now();
  75. let sigshare =
  76. arctic::sign2_polys(&pubkey, key, &coalition, &polys, &msg, &r1_outputs)
  77. .unwrap();
  78. let sign2dur = sign2start.elapsed().as_micros() as f64;
  79. (sigshare, sign2dur)
  80. })
  81. .unzip();
  82. sign2_timings.extend(sign2_iter_timings);
  83. let combinestart = Instant::now();
  84. let sig = arctic::combine_polys(
  85. &pubkey,
  86. t,
  87. &coalition,
  88. &polys,
  89. &msg,
  90. &r1_outputs,
  91. &sigshares,
  92. )
  93. .unwrap();
  94. let combinedur = combinestart.elapsed().as_micros() as f64;
  95. combine_timings.push(combinedur);
  96. assert!(arctic::verify(&pubkey, &msg, &sig));
  97. }
  98. let sign1_mean = mean(&sign1_timings);
  99. let sign1_stddev = stddev(&sign1_timings);
  100. let sign2_mean = mean(&sign2_timings);
  101. let sign2_stddev = stddev(&sign2_timings);
  102. let combine_mean = mean(&combine_timings);
  103. let combine_stddev = stddev(&combine_timings);
  104. println!(
  105. "{} {} {} {} {} {:.1} ± {:.1} {:.1} ± {:.1} {:.1} ± {:.1}",
  106. n,
  107. t,
  108. coalitionsize,
  109. reps,
  110. delta,
  111. sign1_mean,
  112. sign1_stddev,
  113. sign2_mean,
  114. sign2_stddev,
  115. combine_mean,
  116. combine_stddev,
  117. );
  118. }