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.

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