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.

170 lines
4.8 KiB

PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year ago
PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year ago
PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year ago
PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year ago
PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year ago
PST/SQRT + Benches (#35) * first version of the sqrt PST without the MIPP * snarkpack integration * snarkpack integration * adding mipp as submodule directly * snarkpack integration * finalizing * snarkpack integration * update mipp with latestest optimisations and add preliminary documentation * improve codebase documentation * remove unused imports and apply cargo fix changes * passing v0.4 * adding gh action * correct workflow item * correct working dir and msrv * remove unnecessary stuff * wip * wip * remove circuit in fq as it's not needed now * done for tonight * wip * wip * sip * prallelise commitment and groth16 verification * finalise comments for mipp * wip * finalise comments * wip * compiling but test failing * putting back non random blinds * using absorb when we can * absorbing scalar * with bls12-381 * stuff * trying to bring ark-blst to testudo * correcting random implementation * with square in place * works with blst * works with blst * fix: don't require nightly Rust With removing the `test` feature, it can also be built with a stable Rust release and don't require a nightly Rust version. * using ark-blst main branch * started cleanup and added testudo benchmark * add testudo snark and nizk in separate files * rename functions that perform setups and add comments * prototyping * explain testudo-nizk * add support for odd case in sqrt_pst * add missing constraints and correct proof size for benchmarks * add support for odd case in sqrt_pst * fix typo in comment * Documentation #31 * fix typo in comment * Fix Cargo.toml and add benchmark for sqrt pst (#34) * add benchmark for sqrt pst * fix typo in comment * add README * comment from readme not executing --------- Co-authored-by: Mara Mihali <maramihali@google.com> Co-authored-by: Mara Mihali <mihalimara22@gmail.com> Co-authored-by: Volker Mische <volker.mische@gmail.com>
1 year 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_ec::pairing::Pairing;
  12. use ark_ff::{BigInteger, PrimeField};
  13. use ark_std::{One, UniformRand, Zero};
  14. use libtestudo::testudo_snark::{TestudoSnark, TestudoSnarkGens};
  15. use libtestudo::{
  16. parameters::poseidon_params, poseidon_transcript::PoseidonTranscript, InputsAssignment, Instance,
  17. VarsAssignment,
  18. };
  19. #[allow(non_snake_case)]
  20. fn produce_r1cs<E: Pairing>() -> (
  21. usize,
  22. usize,
  23. usize,
  24. usize,
  25. Instance<E::ScalarField>,
  26. VarsAssignment<E::ScalarField>,
  27. InputsAssignment<E::ScalarField>,
  28. ) {
  29. // parameters of the R1CS instance
  30. let num_cons = 4;
  31. let num_vars = 4;
  32. let num_inputs = 1;
  33. let num_non_zero_entries = 8;
  34. // We will encode the above constraints into three matrices, where
  35. // the coefficients in the matrix are in the little-endian byte order
  36. let mut A: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  37. let mut B: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  38. let mut C: Vec<(usize, usize, Vec<u8>)> = Vec::new();
  39. let one = E::ScalarField::one().into_bigint().to_bytes_le();
  40. // R1CS is a set of three sparse matrices A B C, where is a row for every
  41. // constraint and a column for every entry in z = (vars, 1, inputs)
  42. // An R1CS instance is satisfiable iff:
  43. // Az \circ Bz = Cz, where z = (vars, 1, inputs)
  44. // constraint 0 entries in (A,B,C)
  45. // constraint 0 is Z0 * Z0 - Z1 = 0.
  46. A.push((0, 0, one.clone()));
  47. B.push((0, 0, one.clone()));
  48. C.push((0, 1, one.clone()));
  49. // constraint 1 entries in (A,B,C)
  50. // constraint 1 is Z1 * Z0 - Z2 = 0.
  51. A.push((1, 1, one.clone()));
  52. B.push((1, 0, one.clone()));
  53. C.push((1, 2, one.clone()));
  54. // constraint 2 entries in (A,B,C)
  55. // constraint 2 is (Z2 + Z0) * 1 - Z3 = 0.
  56. A.push((2, 2, one.clone()));
  57. A.push((2, 0, one.clone()));
  58. B.push((2, num_vars, one.clone()));
  59. C.push((2, 3, one.clone()));
  60. // constraint 3 entries in (A,B,C)
  61. // constraint 3 is (Z3 + 5) * 1 - I0 = 0.
  62. A.push((3, 3, one.clone()));
  63. A.push((
  64. 3,
  65. num_vars,
  66. E::ScalarField::from(5u32).into_bigint().to_bytes_le(),
  67. ));
  68. B.push((3, num_vars, one.clone()));
  69. C.push((3, num_vars + 1, one));
  70. let inst = Instance::<E::ScalarField>::new(num_cons, num_vars, num_inputs, &A, &B, &C).unwrap();
  71. // compute a satisfying assignment
  72. let mut rng = ark_std::rand::thread_rng();
  73. let z0 = E::ScalarField::rand(&mut rng);
  74. let z1 = z0 * z0; // constraint 0
  75. let z2 = z1 * z0; // constraint 1
  76. let z3 = z2 + z0; // constraint 2
  77. let i0 = z3 + E::ScalarField::from(5u32); // constraint 3
  78. // create a VarsAssignment
  79. let mut vars = vec![E::ScalarField::zero().into_bigint().to_bytes_le(); num_vars];
  80. vars[0] = z0.into_bigint().to_bytes_le();
  81. vars[1] = z1.into_bigint().to_bytes_le();
  82. vars[2] = z2.into_bigint().to_bytes_le();
  83. vars[3] = z3.into_bigint().to_bytes_le();
  84. let assignment_vars = VarsAssignment::new(&vars).unwrap();
  85. // create an InputsAssignment
  86. let mut inputs = vec![E::ScalarField::zero().into_bigint().to_bytes_le(); num_inputs];
  87. inputs[0] = i0.into_bigint().to_bytes_le();
  88. let assignment_inputs = InputsAssignment::new(&inputs).unwrap();
  89. // check if the instance we created is satisfiable
  90. let res = inst.is_sat(&assignment_vars, &assignment_inputs);
  91. assert!(res.unwrap(), "should be satisfied");
  92. (
  93. num_cons,
  94. num_vars,
  95. num_inputs,
  96. num_non_zero_entries,
  97. inst,
  98. assignment_vars,
  99. assignment_inputs,
  100. )
  101. }
  102. type E = ark_bls12_377::Bls12_377;
  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::<E>();
  114. let params = poseidon_params();
  115. // produce public parameters
  116. let gens = TestudoSnarkGens::<E>::setup(
  117. num_cons,
  118. num_vars,
  119. num_inputs,
  120. num_non_zero_entries,
  121. params.clone(),
  122. );
  123. // create a commitment to the R1CS instance
  124. let (comm, decomm) = TestudoSnark::encode(&inst, &gens);
  125. // produce a proof of satisfiability
  126. let mut prover_transcript = PoseidonTranscript::new(&params);
  127. let proof = TestudoSnark::prove(
  128. &inst,
  129. &comm,
  130. &decomm,
  131. assignment_vars,
  132. &assignment_inputs,
  133. &gens,
  134. &mut prover_transcript,
  135. params.clone(),
  136. )
  137. .unwrap();
  138. // verify the proof of satisfiability
  139. let mut verifier_transcript = PoseidonTranscript::new(&params);
  140. assert!(proof
  141. .verify(
  142. &gens,
  143. &comm,
  144. &assignment_inputs,
  145. &mut verifier_transcript,
  146. params
  147. )
  148. .is_ok());
  149. println!("proof verification successful!");
  150. }