You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
7.2 KiB

5 years ago
  1. #[macro_use]
  2. extern crate criterion;
  3. mod affine {
  4. use algebra::curves::edwards_bls12::EdwardsAffine as Edwards;
  5. use blake2::Blake2s;
  6. use criterion::Criterion;
  7. use crypto_primitives::signature::{schnorr::*, SignatureScheme};
  8. use rand::{self, Rng};
  9. type SchnorrEdwards = SchnorrSignature<Edwards, Blake2s>;
  10. fn schnorr_signature_setup(c: &mut Criterion) {
  11. c.bench_function("SchnorrEdwardsAffine: Setup", move |b| {
  12. b.iter(|| {
  13. let mut rng = &mut rand::thread_rng();
  14. SchnorrEdwards::setup(&mut rng).unwrap()
  15. })
  16. });
  17. }
  18. fn schnorr_signature_keygen(c: &mut Criterion) {
  19. let mut rng = &mut rand::thread_rng();
  20. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  21. c.bench_function("SchnorrEdwardsAffine: KeyGen", move |b| {
  22. b.iter(|| {
  23. let mut rng = &mut rand::thread_rng();
  24. SchnorrEdwards::keygen(&parameters, &mut rng).unwrap()
  25. })
  26. });
  27. }
  28. fn schnorr_signature_sign(c: &mut Criterion) {
  29. let mut rng = &mut rand::thread_rng();
  30. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  31. let (_, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  32. let message = [100u8; 128];
  33. c.bench_function("SchnorrEdwardsAffine: Sign", move |b| {
  34. b.iter(|| {
  35. let mut rng = &mut rand::thread_rng();
  36. SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap()
  37. })
  38. });
  39. }
  40. fn schnorr_signature_verify(c: &mut Criterion) {
  41. let mut rng = &mut rand::thread_rng();
  42. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  43. let (pk, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  44. let message = [100u8; 128];
  45. let signature = SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap();
  46. c.bench_function("SchnorrEdwardsAffine: Verify", move |b| {
  47. b.iter(|| SchnorrEdwards::verify(&parameters, &pk, &message, &signature).unwrap())
  48. });
  49. }
  50. fn schnorr_signature_randomize_pk(c: &mut Criterion) {
  51. let mut rng = &mut rand::thread_rng();
  52. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  53. let (pk, _) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  54. let randomness: [u8; 32] = rng.gen();
  55. c.bench_function("SchnorrEdwardsAffine: Randomize PubKey", move |b| {
  56. b.iter(|| SchnorrEdwards::randomize_public_key(&parameters, &pk, &randomness).unwrap())
  57. });
  58. }
  59. fn schnorr_signature_randomize_signature(c: &mut Criterion) {
  60. let mut rng = &mut rand::thread_rng();
  61. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  62. let (_, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  63. let randomness: [u8; 32] = rng.gen();
  64. let message = [100u8; 128];
  65. let signature = SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap();
  66. c.bench_function("SchnorrEdwardsAffine: Randomize Signature", move |b| {
  67. b.iter(|| {
  68. SchnorrEdwards::randomize_signature(&parameters, &signature, &randomness).unwrap()
  69. })
  70. });
  71. }
  72. criterion_group! {
  73. name = schnorr_sig_affine;
  74. config = Criterion::default().sample_size(20);
  75. targets = schnorr_signature_setup, schnorr_signature_keygen, schnorr_signature_sign,
  76. schnorr_signature_verify, schnorr_signature_randomize_pk, schnorr_signature_randomize_signature
  77. }
  78. }
  79. mod projective {
  80. use algebra::curves::edwards_bls12::EdwardsProjective as Edwards;
  81. use blake2::Blake2s;
  82. use criterion::Criterion;
  83. use crypto_primitives::signature::{schnorr::*, SignatureScheme};
  84. use rand::{self, Rng};
  85. type SchnorrEdwards = SchnorrSignature<Edwards, Blake2s>;
  86. fn schnorr_signature_setup(c: &mut Criterion) {
  87. c.bench_function("SchnorrEdwardsProjective: Setup", move |b| {
  88. b.iter(|| {
  89. let mut rng = &mut rand::thread_rng();
  90. SchnorrEdwards::setup(&mut rng).unwrap()
  91. })
  92. });
  93. }
  94. fn schnorr_signature_keygen(c: &mut Criterion) {
  95. let mut rng = &mut rand::thread_rng();
  96. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  97. c.bench_function("SchnorrEdwardsProjective: KeyGen", move |b| {
  98. b.iter(|| {
  99. let mut rng = &mut rand::thread_rng();
  100. SchnorrEdwards::keygen(&parameters, &mut rng).unwrap()
  101. })
  102. });
  103. }
  104. fn schnorr_signature_sign(c: &mut Criterion) {
  105. let mut rng = &mut rand::thread_rng();
  106. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  107. let (_, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  108. let message = [100u8; 128];
  109. c.bench_function("SchnorrEdwardsProjective: Sign", move |b| {
  110. b.iter(|| {
  111. let mut rng = &mut rand::thread_rng();
  112. SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap()
  113. })
  114. });
  115. }
  116. fn schnorr_signature_verify(c: &mut Criterion) {
  117. let mut rng = &mut rand::thread_rng();
  118. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  119. let (pk, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  120. let message = [100u8; 128];
  121. let signature = SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap();
  122. c.bench_function("SchnorrEdwardsProjective: Verify", move |b| {
  123. b.iter(|| SchnorrEdwards::verify(&parameters, &pk, &message, &signature).unwrap())
  124. });
  125. }
  126. fn schnorr_signature_randomize_pk(c: &mut Criterion) {
  127. let mut rng = &mut rand::thread_rng();
  128. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  129. let (pk, _) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  130. let randomness: [u8; 32] = rng.gen();
  131. c.bench_function("SchnorrEdwardsProjective: Randomize PubKey", move |b| {
  132. b.iter(|| SchnorrEdwards::randomize_public_key(&parameters, &pk, &randomness).unwrap())
  133. });
  134. }
  135. fn schnorr_signature_randomize_signature(c: &mut Criterion) {
  136. let mut rng = &mut rand::thread_rng();
  137. let parameters = SchnorrEdwards::setup(&mut rng).unwrap();
  138. let (_, sk) = SchnorrEdwards::keygen(&parameters, &mut rng).unwrap();
  139. let randomness: [u8; 32] = rng.gen();
  140. let message = [100u8; 128];
  141. let signature = SchnorrEdwards::sign(&parameters, &sk, &message, &mut rng).unwrap();
  142. c.bench_function("SchnorrEdwardsProjective: Randomize Signature", move |b| {
  143. b.iter(|| {
  144. SchnorrEdwards::randomize_signature(&parameters, &signature, &randomness).unwrap()
  145. })
  146. });
  147. }
  148. criterion_group! {
  149. name = schnorr_sig_projective;
  150. config = Criterion::default().sample_size(20);
  151. targets = schnorr_signature_setup, schnorr_signature_keygen, schnorr_signature_sign,
  152. schnorr_signature_verify, schnorr_signature_randomize_pk, schnorr_signature_randomize_signature
  153. }
  154. }
  155. use crate::{affine::schnorr_sig_affine, projective::schnorr_sig_projective};
  156. criterion_main!(schnorr_sig_affine, schnorr_sig_projective);