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.

156 lines
4.6 KiB

2 years ago
  1. //! Demonstrates how to produces a proof for canonical cubic equation: `x^3 + x + 5 = y`.
  2. //! The example is described in detail [here].
  3. //!
  4. //! The R1CS for this problem consists of the following 4 constraints:
  5. //! `Z0 * Z0 - Z1 = 0`
  6. //! `Z1 * Z0 - Z2 = 0`
  7. //! `(Z2 + Z0) * 1 - Z3 = 0`
  8. //! `(Z3 + 5) * 1 - I0 = 0`
  9. //!
  10. //! [here]: https://medium.com/@VitalikButerin/quadratic-arithmetic-programs-from-zero-to-hero-f6d558cea649
  11. use ark_bls12_377::Fr as Scalar;
  12. use ark_ff::{BigInteger, PrimeField};
  13. use ark_std::{One, UniformRand, Zero};
  14. <<<<<<< HEAD
  15. use libspartan::{InputsAssignment, Instance, SNARKGens, VarsAssignment, SNARK};
  16. =======
  17. use libspartan::{
  18. parameters::poseidon_params,
  19. poseidon_transcript::{self, PoseidonTranscript},
  20. InputsAssignment, Instance, SNARKGens, VarsAssignment, SNARK,
  21. };
  22. >>>>>>> implement alternative transcript with poseidon backend
  23. use merlin::Transcript;
  24. #[allow(non_snake_case)]
  25. fn produce_r1cs() -> (
  26. usize,
  27. usize,
  28. usize,
  29. usize,
  30. Instance,
  31. VarsAssignment,
  32. InputsAssignment,
  33. ) {
  34. // parameters of the R1CS instance
  35. let num_cons = 4;
  36. let num_vars = 4;
  37. let num_inputs = 1;
  38. let num_non_zero_entries = 8;
  39. // We will encode the above constraints into three matrices, where
  40. // the coefficients in the matrix are in the little-endian byte order
  41. let mut A: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  42. let mut B: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  43. let mut C: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  44. let one = Scalar::one().into_repr().to_bytes_le();
  45. // R1CS is a set of three sparse matrices A B C, where is a row for every
  46. // constraint and a column for every entry in z = (vars, 1, inputs)
  47. // An R1CS instance is satisfiable iff:
  48. // Az \circ Bz = Cz, where z = (vars, 1, inputs)
  49. // constraint 0 entries in (A,B,C)
  50. // constraint 0 is Z0 * Z0 - Z1 = 0.
  51. A.push((0, 0, one.clone()));
  52. B.push((0, 0, one.clone()));
  53. C.push((0, 1, one.clone()));
  54. // constraint 1 entries in (A,B,C)
  55. // constraint 1 is Z1 * Z0 - Z2 = 0.
  56. A.push((1, 1, one.clone()));
  57. B.push((1, 0, one.clone()));
  58. C.push((1, 2, one.clone()));
  59. // constraint 2 entries in (A,B,C)
  60. // constraint 2 is (Z2 + Z0) * 1 - Z3 = 0.
  61. A.push((2, 2, one.clone()));
  62. A.push((2, 0, one.clone()));
  63. B.push((2, num_vars, one.clone()));
  64. C.push((2, 3, one.clone()));
  65. // constraint 3 entries in (A,B,C)
  66. // constraint 3 is (Z3 + 5) * 1 - I0 = 0.
  67. A.push((3, 3, one.clone()));
  68. A.push((3, num_vars, Scalar::from(5u32).into_repr().to_bytes_le()));
  69. B.push((3, num_vars, one.clone()));
  70. C.push((3, num_vars + 1, one.clone()));
  71. let inst = Instance::new(num_cons, num_vars, num_inputs, &A, &B, &C).unwrap();
  72. // compute a satisfying assignment
  73. let mut rng = ark_std::rand::thread_rng();
  74. let z0 = Scalar::rand(&mut rng);
  75. let z1 = z0 * z0; // constraint 0
  76. let z2 = z1 * z0; // constraint 1
  77. let z3 = z2 + z0; // constraint 2
  78. let i0 = z3 + Scalar::from(5u32); // constraint 3
  79. // create a VarsAssignment
  80. let mut vars = vec![Scalar::zero().into_repr().to_bytes_le(); num_vars];
  81. vars[0] = z0.into_repr().to_bytes_le();
  82. vars[1] = z1.into_repr().to_bytes_le();
  83. vars[2] = z2.into_repr().to_bytes_le();
  84. vars[3] = z3.into_repr().to_bytes_le();
  85. let assignment_vars = VarsAssignment::new(&vars).unwrap();
  86. // create an InputsAssignment
  87. let mut inputs = vec![Scalar::zero().into_repr().to_bytes_le(); num_inputs];
  88. inputs[0] = i0.into_repr().to_bytes_le();
  89. let assignment_inputs = InputsAssignment::new(&inputs).unwrap();
  90. // check if the instance we created is satisfiable
  91. let res = inst.is_sat(&assignment_vars, &assignment_inputs);
  92. assert!(res.unwrap(), "should be satisfied");
  93. (
  94. num_cons,
  95. num_vars,
  96. num_inputs,
  97. num_non_zero_entries,
  98. inst,
  99. assignment_vars,
  100. assignment_inputs,
  101. )
  102. }
  103. fn main() {
  104. // produce an R1CS instance
  105. let (
  106. num_cons,
  107. num_vars,
  108. num_inputs,
  109. num_non_zero_entries,
  110. inst,
  111. assignment_vars,
  112. assignment_inputs,
  113. ) = produce_r1cs();
  114. let params = poseidon_params();
  115. // produce public parameters
  116. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
  117. // create a commitment to the R1CS instance
  118. let (comm, decomm) = SNARK::encode(&inst, &gens);
  119. // produce a proof of satisfiability
  120. let mut prover_transcript = PoseidonTranscript::new(&params);
  121. let proof = SNARK::prove(
  122. &inst,
  123. &comm,
  124. &decomm,
  125. assignment_vars,
  126. &assignment_inputs,
  127. &gens,
  128. &mut prover_transcript,
  129. );
  130. // verify the proof of satisfiability
  131. let mut verifier_transcript = PoseidonTranscript::new(&params);
  132. assert!(proof
  133. .verify(&comm, &assignment_inputs, &mut verifier_transcript, &gens)
  134. .is_ok());
  135. println!("proof verification successful!");
  136. }