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.

121 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #[macro_use]
  2. extern crate criterion;
  3. use algebra::{curves::bls12_377::Bls12_377, fields::bls12_377::Fr, Field};
  4. use crypto_primitives::nizk::*;
  5. use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
  6. use criterion::Criterion;
  7. use rand::{thread_rng, Rng};
  8. type TestProofSystem = Gm17<Bls12_377, Bench<Fr>, Fr>;
  9. struct Bench<F: Field> {
  10. inputs: Vec<Option<F>>,
  11. num_constraints: usize,
  12. }
  13. impl<F: Field> ConstraintSynthesizer<F> for Bench<F> {
  14. fn generate_constraints<CS: ConstraintSystem<F>>(
  15. self,
  16. cs: &mut CS,
  17. ) -> Result<(), SynthesisError> {
  18. assert!(self.inputs.len() >= 2);
  19. assert!(self.num_constraints >= self.inputs.len());
  20. let mut variables: Vec<_> = Vec::with_capacity(self.inputs.len());
  21. for (i, input) in self.inputs.into_iter().enumerate() {
  22. let input_var = cs.alloc_input(
  23. || format!("Input {}", i),
  24. || input.ok_or(SynthesisError::AssignmentMissing),
  25. )?;
  26. variables.push((input, input_var));
  27. }
  28. for i in 0..self.num_constraints {
  29. let new_entry = {
  30. let (input_1_val, input_1_var) = variables[i];
  31. let (input_2_val, input_2_var) = variables[i + 1];
  32. let result_val =
  33. input_1_val.and_then(|input_1| input_2_val.map(|input_2| input_1 * &input_2));
  34. let result_var = cs.alloc(
  35. || format!("Result {}", i),
  36. || result_val.ok_or(SynthesisError::AssignmentMissing),
  37. )?;
  38. cs.enforce(
  39. || format!("Enforce constraint {}", i),
  40. |lc| lc + input_1_var,
  41. |lc| lc + input_2_var,
  42. |lc| lc + result_var,
  43. );
  44. (result_val, result_var)
  45. };
  46. variables.push(new_entry);
  47. }
  48. Ok(())
  49. }
  50. }
  51. fn gm17_setup(c: &mut Criterion) {
  52. let num_inputs = 100;
  53. let num_constraints = num_inputs;
  54. let rng = &mut thread_rng();
  55. let mut inputs: Vec<Option<Fr>> = Vec::with_capacity(num_inputs);
  56. for _ in 0..num_inputs {
  57. inputs.push(Some(rng.gen()));
  58. }
  59. c.bench_function("gm17_setup", move |b| {
  60. b.iter(|| {
  61. TestProofSystem::setup(
  62. Bench::<Fr> {
  63. inputs: vec![None; num_inputs],
  64. num_constraints,
  65. },
  66. rng,
  67. )
  68. .unwrap()
  69. })
  70. });
  71. }
  72. fn gm17_prove(c: &mut Criterion) {
  73. let num_inputs = 100;
  74. let num_constraints = num_inputs;
  75. let rng = &mut thread_rng();
  76. let mut inputs: Vec<Option<Fr>> = Vec::with_capacity(num_inputs);
  77. for _ in 0..num_inputs {
  78. inputs.push(Some(rng.gen()));
  79. }
  80. let params = TestProofSystem::setup(
  81. Bench::<Fr> {
  82. inputs: vec![None; num_inputs],
  83. num_constraints,
  84. },
  85. rng,
  86. )
  87. .unwrap();
  88. c.bench_function("gm17_prove", move |b| {
  89. b.iter(|| {
  90. TestProofSystem::prove(
  91. &params.0,
  92. Bench {
  93. inputs: inputs.clone(),
  94. num_constraints,
  95. },
  96. rng,
  97. )
  98. .unwrap()
  99. })
  100. });
  101. }
  102. criterion_group! {
  103. name = nizk_eval;
  104. config = Criterion::default().sample_size(10);
  105. targets = gm17_setup, gm17_prove
  106. }
  107. criterion_main!(nizk_eval);