shine.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. use arctic::shine;
  2. use curve25519_dalek::constants as dalek_constants;
  3. use curve25519_dalek::ristretto::RistrettoPoint;
  4. use curve25519_dalek::scalar::Scalar;
  5. use rand::RngCore;
  6. use std::env;
  7. use std::time::Instant;
  8. fn mean(vals: &[f64]) -> f64 {
  9. let num = vals.len();
  10. if num > 0 {
  11. vals.iter().sum::<f64>() / (num as f64)
  12. } else {
  13. 0f64
  14. }
  15. }
  16. fn stddev(vals: &[f64]) -> f64 {
  17. let num = vals.len();
  18. if num < 2 {
  19. return 0f64;
  20. }
  21. let avg = mean(vals);
  22. (vals
  23. .iter()
  24. .map(|v| v - avg)
  25. .map(|dev| dev * dev)
  26. .sum::<f64>()
  27. / ((num - 1) as f64))
  28. .sqrt()
  29. }
  30. fn main() {
  31. let args: Vec<String> = env::args().collect();
  32. if args.len() < 4 || args.len() > 5 {
  33. println!("Usage: {} n t wlen [reps]", args[0]);
  34. return;
  35. }
  36. let n: u32 = args[1].parse().unwrap();
  37. let t: u32 = args[2].parse().unwrap();
  38. let wlen: usize = args[3].parse().unwrap();
  39. let mut reps = 1usize;
  40. if args.len() > 4 {
  41. reps = args[4].parse().unwrap();
  42. }
  43. let mut wvec: Vec<u8> = Vec::new();
  44. let mut rng = rand::thread_rng();
  45. wvec.resize(wlen, 0);
  46. let generator = dalek_constants::RISTRETTO_BASEPOINT_TABLE;
  47. let keys: Vec<shine::PreprocKey> = shine::Key::keygen(n, t)
  48. .iter()
  49. .map(shine::PreprocKey::preproc)
  50. .collect();
  51. let delta = keys[0].delta();
  52. let mut eval_timings: Vec<f64> = Vec::new();
  53. let mut commit_timings: Vec<f64> = Vec::new();
  54. let mut combinecomm_timings: Vec<f64> = Vec::new();
  55. let coalition = (1..=n).collect::<Vec<u32>>();
  56. for _ in 0..reps {
  57. rng.fill_bytes(&mut wvec);
  58. let (evaluations, eval_iter_timings): (Vec<Scalar>, Vec<f64>) = keys
  59. .iter()
  60. .map(|rk| {
  61. let evalstart = Instant::now();
  62. let evaluation = rk.gen(&wvec).0;
  63. let evaldur = evalstart.elapsed().as_micros() as f64;
  64. (evaluation, evaldur)
  65. })
  66. .unzip();
  67. eval_timings.extend(eval_iter_timings);
  68. let (commitments, commit_iter_timings): (Vec<RistrettoPoint>, Vec<f64>) = evaluations
  69. .iter()
  70. .map(|ev| {
  71. let commstart = Instant::now();
  72. let commitment = ev * &generator;
  73. let commdur = commstart.elapsed().as_micros() as f64;
  74. (commitment, commdur)
  75. })
  76. .unzip();
  77. commit_timings.extend(commit_iter_timings);
  78. let combinecommstart = Instant::now();
  79. let _comb = shine::combinecomm(t, &coalition, &commitments);
  80. let combinecommdur = combinecommstart.elapsed().as_micros() as f64;
  81. combinecomm_timings.push(combinecommdur);
  82. }
  83. let eval_mean = mean(&eval_timings);
  84. let eval_stddev = stddev(&eval_timings);
  85. let commit_mean = mean(&commit_timings);
  86. let commit_stddev = stddev(&commit_timings);
  87. let combinecomm_mean = mean(&combinecomm_timings);
  88. let combinecomm_stddev = stddev(&combinecomm_timings);
  89. println!(
  90. "{} {} {} {} {} {:.1} ± {:.1} {:.1} ± {:.1} {:.1} ± {:.1} {:.2} ± {:.2}",
  91. n,
  92. t,
  93. wlen,
  94. reps,
  95. delta,
  96. eval_mean,
  97. eval_stddev,
  98. commit_mean,
  99. commit_stddev,
  100. combinecomm_mean,
  101. combinecomm_stddev,
  102. eval_mean / (delta as f64),
  103. eval_stddev / (delta as f64)
  104. );
  105. }