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.

150 lines
4.4 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. use libspartan::{
  15. parameters::poseidon_params, poseidon_transcript::PoseidonTranscript, InputsAssignment, Instance,
  16. SNARKGens, VarsAssignment, SNARK,
  17. };
  18. #[allow(non_snake_case)]
  19. fn produce_r1cs() -> (
  20. usize,
  21. usize,
  22. usize,
  23. usize,
  24. Instance,
  25. VarsAssignment,
  26. InputsAssignment,
  27. ) {
  28. // parameters of the R1CS instance
  29. let num_cons = 4;
  30. let num_vars = 4;
  31. let num_inputs = 1;
  32. let num_non_zero_entries = 8;
  33. // We will encode the above constraints into three matrices, where
  34. // the coefficients in the matrix are in the little-endian byte order
  35. let mut A: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  36. let mut B: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  37. let mut C: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  38. let one = Scalar::one().into_repr().to_bytes_le();
  39. // R1CS is a set of three sparse matrices A B C, where is a row for every
  40. // constraint and a column for every entry in z = (vars, 1, inputs)
  41. // An R1CS instance is satisfiable iff:
  42. // Az \circ Bz = Cz, where z = (vars, 1, inputs)
  43. // constraint 0 entries in (A,B,C)
  44. // constraint 0 is Z0 * Z0 - Z1 = 0.
  45. A.push((0, 0, one.clone()));
  46. B.push((0, 0, one.clone()));
  47. C.push((0, 1, one.clone()));
  48. // constraint 1 entries in (A,B,C)
  49. // constraint 1 is Z1 * Z0 - Z2 = 0.
  50. A.push((1, 1, one.clone()));
  51. B.push((1, 0, one.clone()));
  52. C.push((1, 2, one.clone()));
  53. // constraint 2 entries in (A,B,C)
  54. // constraint 2 is (Z2 + Z0) * 1 - Z3 = 0.
  55. A.push((2, 2, one.clone()));
  56. A.push((2, 0, one.clone()));
  57. B.push((2, num_vars, one.clone()));
  58. C.push((2, 3, one.clone()));
  59. // constraint 3 entries in (A,B,C)
  60. // constraint 3 is (Z3 + 5) * 1 - I0 = 0.
  61. A.push((3, 3, one.clone()));
  62. A.push((3, num_vars, Scalar::from(5u32).into_repr().to_bytes_le()));
  63. B.push((3, num_vars, one.clone()));
  64. C.push((3, num_vars + 1, one));
  65. let inst = Instance::new(num_cons, num_vars, num_inputs, &A, &B, &C).unwrap();
  66. // compute a satisfying assignment
  67. let mut rng = ark_std::rand::thread_rng();
  68. let z0 = Scalar::rand(&mut rng);
  69. let z1 = z0 * z0; // constraint 0
  70. let z2 = z1 * z0; // constraint 1
  71. let z3 = z2 + z0; // constraint 2
  72. let i0 = z3 + Scalar::from(5u32); // constraint 3
  73. // create a VarsAssignment
  74. let mut vars = vec![Scalar::zero().into_repr().to_bytes_le(); num_vars];
  75. vars[0] = z0.into_repr().to_bytes_le();
  76. vars[1] = z1.into_repr().to_bytes_le();
  77. vars[2] = z2.into_repr().to_bytes_le();
  78. vars[3] = z3.into_repr().to_bytes_le();
  79. let assignment_vars = VarsAssignment::new(&vars).unwrap();
  80. // create an InputsAssignment
  81. let mut inputs = vec![Scalar::zero().into_repr().to_bytes_le(); num_inputs];
  82. inputs[0] = i0.into_repr().to_bytes_le();
  83. let assignment_inputs = InputsAssignment::new(&inputs).unwrap();
  84. // check if the instance we created is satisfiable
  85. let res = inst.is_sat(&assignment_vars, &assignment_inputs);
  86. assert!(res.unwrap(), "should be satisfied");
  87. (
  88. num_cons,
  89. num_vars,
  90. num_inputs,
  91. num_non_zero_entries,
  92. inst,
  93. assignment_vars,
  94. assignment_inputs,
  95. )
  96. }
  97. fn main() {
  98. // produce an R1CS instance
  99. let (
  100. num_cons,
  101. num_vars,
  102. num_inputs,
  103. num_non_zero_entries,
  104. inst,
  105. assignment_vars,
  106. assignment_inputs,
  107. ) = produce_r1cs();
  108. let params = poseidon_params();
  109. // produce public parameters
  110. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_non_zero_entries);
  111. // create a commitment to the R1CS instance
  112. let (comm, decomm) = SNARK::encode(&inst, &gens);
  113. // produce a proof of satisfiability
  114. let mut prover_transcript = PoseidonTranscript::new(&params);
  115. let proof = SNARK::prove(
  116. &inst,
  117. &comm,
  118. &decomm,
  119. assignment_vars,
  120. &assignment_inputs,
  121. &gens,
  122. &mut prover_transcript,
  123. );
  124. // verify the proof of satisfiability
  125. let mut verifier_transcript = PoseidonTranscript::new(&params);
  126. assert!(proof
  127. .verify(&comm, &assignment_inputs, &mut verifier_transcript, &gens)
  128. .is_ok());
  129. println!("proof verification successful!");
  130. }