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.

145 lines
4.1 KiB

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