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.

99 lines
3.1 KiB

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