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.

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