From acec78f96b77a399859b7c3d7ed4c93420593b60 Mon Sep 17 00:00:00 2001 From: katat Date: Thu, 12 May 2022 07:31:00 +0000 Subject: [PATCH 1/7] [kimchi][doc] get the example pass through the compiling --- README.md | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d6f1863213..49cff79fe9 100644 --- a/README.md +++ b/README.md @@ -19,32 +19,50 @@ We assume that you already have: Then, you can create an URS for your circuit in the following way: -```rust,ignore -use kimchi::{circuits::constraints, verifier::verify}; +```rust +use std::sync::Arc; + +use kimchi::{circuits::{constraints::ConstraintSystem, polynomials::generic::testing::{create_circuit, fill_in_witness}, polynomial::COLUMNS}, prover_index::ProverIndex, proof::ProverProof, verifier::verify}; +use ark_ff::Zero; use mina_curves::pasta::{fp::Fp, vesta::{Affine, VestaParameters}, pallas::Affine as Other}; use oracle::{ constants::PlonkSpongeConstantsKimchi, sponge::{DefaultFqSponge, DefaultFrSponge}, }; -use commitment_dlog::commitment::{b_poly_coefficients, ceil_log2, CommitmentCurve}; +use commitment_dlog::{commitment::{CommitmentCurve}, srs::{SRS, endos}}; +use groupmap::GroupMap; +use array_init::array_init; + type SpongeParams = PlonkSpongeConstantsKimchi; type BaseSponge = DefaultFqSponge; type ScalarSponge = DefaultFrSponge; // compile the circuit +let gates = create_circuit(0, 0); + let fp_sponge_params = oracle::pasta::fp_kimchi::params(); -let cs = ConstraintSystem::::create(gates, vec![], fp_sponge_params, public_size).unwrap(); +let public = vec![Fp::from(0); 0]; +let mut witness: [Vec; COLUMNS] = array_init(|_| vec![Fp::zero(); gates.len()]); +fill_in_witness(0, &mut witness, &public); + +let cs = ConstraintSystem::::create( + gates, + vec![], + fp_sponge_params, + public.len() +).unwrap(); // create an URS -let mut urs = SRS::::create(cs.domain.d1.size as usize); +let mut srs = SRS::::create(cs.domain.d1.size as usize); srs.add_lagrange_basis(cs.domain.d1); +let srs = Arc::new(srs); // obtain a prover index let prover_index = { let fq_sponge_params = oracle::pasta::fq_kimchi::params(); let (endo_q, _endo_r) = endos::(); - Index::::create(cs, fq_sponge_params, endo_q, srs) + ProverIndex::::create(cs, fq_sponge_params, endo_q, srs) }; // obtain a verifier index @@ -53,10 +71,10 @@ let verifier_index = prover_index.verifier_index(); // create a proof let group_map = ::Map::setup(); let proof = ProverProof::create::( - &group_map, witness, &prover_index); + &group_map, witness, &prover_index).unwrap(); // verify a proof -verify::(&group_map, verifier_index, proof).unwrap(); +verify::(&group_map, &verifier_index, &proof).unwrap(); ``` Note that kimchi is specifically designed for use in a recursion proof system, like [pickles](https://medium.com/minaprotocol/meet-pickles-snark-enabling-smart-contract-on-coda-protocol-7ede3b54c250), but can also be used a stand alone for normal proofs. From 03ab234fc80d3be8250913724f613cf6a3562b97 Mon Sep 17 00:00:00 2001 From: kata Date: Fri, 10 Jun 2022 15:37:10 +0800 Subject: [PATCH 2/7] update readme example with circuit APIs --- README.md | 114 +++++++++++++++++++++++++++------------------- kimchi/Cargo.toml | 1 + 2 files changed, 67 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 49cff79fe9..9c2f10e594 100644 --- a/README.md +++ b/README.md @@ -21,60 +21,78 @@ Then, you can create an URS for your circuit in the following way: ```rust use std::sync::Arc; - -use kimchi::{circuits::{constraints::ConstraintSystem, polynomials::generic::testing::{create_circuit, fill_in_witness}, polynomial::COLUMNS}, prover_index::ProverIndex, proof::ProverProof, verifier::verify}; -use ark_ff::Zero; -use mina_curves::pasta::{fp::Fp, vesta::{Affine, VestaParameters}, pallas::Affine as Other}; +use ark_ff::{FftField, PrimeField}; +use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as D}; +use circuit_construction::*; +use commitment_dlog::{commitment::CommitmentCurve, srs::SRS}; +use groupmap::GroupMap; +use kimchi::verifier::verify; +use mina_curves::pasta::{ + fp::Fp, + vesta::{Affine, VestaParameters}, +}; use oracle::{ - constants::PlonkSpongeConstantsKimchi, + constants::*, sponge::{DefaultFqSponge, DefaultFrSponge}, }; -use commitment_dlog::{commitment::{CommitmentCurve}, srs::{SRS, endos}}; -use groupmap::GroupMap; -use array_init::array_init; - - -type SpongeParams = PlonkSpongeConstantsKimchi; -type BaseSponge = DefaultFqSponge; -type ScalarSponge = DefaultFrSponge; - -// compile the circuit -let gates = create_circuit(0, 0); - -let fp_sponge_params = oracle::pasta::fp_kimchi::params(); -let public = vec![Fp::from(0); 0]; -let mut witness: [Vec; COLUMNS] = array_init(|_| vec![Fp::zero(); gates.len()]); -fill_in_witness(0, &mut witness, &public); - -let cs = ConstraintSystem::::create( - gates, - vec![], - fp_sponge_params, - public.len() -).unwrap(); - -// create an URS -let mut srs = SRS::::create(cs.domain.d1.size as usize); -srs.add_lagrange_basis(cs.domain.d1); -let srs = Arc::new(srs); - -// obtain a prover index -let prover_index = { - let fq_sponge_params = oracle::pasta::fq_kimchi::params(); - let (endo_q, _endo_r) = endos::(); - ProverIndex::::create(cs, fq_sponge_params, endo_q, srs) +type SpongeQ = DefaultFqSponge; +type SpongeR = DefaultFrSponge; + +pub fn circuit< + F: PrimeField + FftField, + Sys: Cs, +>( + witness: Option, + sys: &mut Sys, + public_input: Vec>, +) { + let three = sys.constant(3u32.into()); + let first_public_input = public_input[0]; + let witness = sys.var(|| witness.unwrap()); + + //TODO replace the following free variable once the "add" gate constraint is ready to use. + let result = sys.var(|| { + first_public_input.val() + witness.val() + }); + sys.assert_eq(result, three); +} + +// create SRS +let srs = { + //TODO how to determine depth value? it throws error when the depth is too large + let mut srs = SRS::::create(1 << 3); // 2^3 = 8 + srs.add_lagrange_basis(D::new(srs.g.len()).unwrap()); + Arc::new(srs) }; - -// obtain a verifier index -let verifier_index = prover_index.verifier_index(); - -// create a proof +let public_inputs = vec![1i32.into()]; let group_map = ::Map::setup(); -let proof = ProverProof::create::( - &group_map, witness, &prover_index).unwrap(); -// verify a proof -verify::(&group_map, &verifier_index, &proof).unwrap(); +// generate circuit and index +let prover_index = generate_prover_index::( + //TODO do these arguments have to be provided? + srs, + &fp_constants(), + &oracle::pasta::fq_kimchi::params(), + //TODO should this be encapsulated? + public_inputs.len(), + |sys, p| circuit::<_, _>(None, sys, p), +); + +// create witness +let witness = 2i32; + +// generate proof +let proof = prove::( + &prover_index, + &group_map, + None, + public_inputs, + |sys, p| circuit::(Some(witness.into()), sys, p), +); + +// verify proof +let verifier_index = prover_index.verifier_index(); +verify::<_, SpongeQ, SpongeR>(&group_map, &verifier_index, &proof).unwrap(); ``` Note that kimchi is specifically designed for use in a recursion proof system, like [pickles](https://medium.com/minaprotocol/meet-pickles-snark-enabling-smart-contract-on-coda-protocol-7ede3b54c250), but can also be used a stand alone for normal proofs. diff --git a/kimchi/Cargo.toml b/kimchi/Cargo.toml index 9fce052287..734115b900 100644 --- a/kimchi/Cargo.toml +++ b/kimchi/Cargo.toml @@ -37,6 +37,7 @@ groupmap = { path = "../groupmap" } mina-curves = { path = "../curves" } o1-utils = { path = "../utils" } oracle = { path = "../oracle" } +circuit-construction = { path = "../circuit-construction" } ocaml = { version = "0.22.2", optional = true } ocaml-gen = { path = "../ocaml/ocaml-gen", optional = true} From 31e1b8f4fe1db48b98f5b443d068d9af530faa8a Mon Sep 17 00:00:00 2001 From: kata Date: Sat, 11 Jun 2022 11:16:28 +0800 Subject: [PATCH 3/7] update circuit-construction to be the main lib for the main readme --- circuit-construction/src/lib.rs | 2 ++ kimchi/Cargo.toml | 1 - kimchi/src/lib.rs | 2 -- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/circuit-construction/src/lib.rs b/circuit-construction/src/lib.rs index 82ff66b988..f4bd7f3374 100644 --- a/circuit-construction/src/lib.rs +++ b/circuit-construction/src/lib.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("../../README.md")] + use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{BigInteger, FftField, Field, One, PrimeField, SquareRootField, Zero}; use array_init::array_init; diff --git a/kimchi/Cargo.toml b/kimchi/Cargo.toml index 734115b900..9fce052287 100644 --- a/kimchi/Cargo.toml +++ b/kimchi/Cargo.toml @@ -37,7 +37,6 @@ groupmap = { path = "../groupmap" } mina-curves = { path = "../curves" } o1-utils = { path = "../utils" } oracle = { path = "../oracle" } -circuit-construction = { path = "../circuit-construction" } ocaml = { version = "0.22.2", optional = true } ocaml-gen = { path = "../ocaml/ocaml-gen", optional = true} diff --git a/kimchi/src/lib.rs b/kimchi/src/lib.rs index a1f033a4c5..a362e9b2aa 100644 --- a/kimchi/src/lib.rs +++ b/kimchi/src/lib.rs @@ -1,5 +1,3 @@ -#![doc = include_str!("../../README.md")] - #[macro_use] extern crate num_derive; From a0c945e575b0c21c43292d5447a7edc8097cb497 Mon Sep 17 00:00:00 2001 From: kata Date: Sat, 11 Jun 2022 11:16:28 +0800 Subject: [PATCH 4/7] update circuit-construction to be the main lib for the main readme --- circuit-construction/src/lib.rs | 2 ++ kimchi/Cargo.toml | 1 - kimchi/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/circuit-construction/src/lib.rs b/circuit-construction/src/lib.rs index 82ff66b988..f4bd7f3374 100644 --- a/circuit-construction/src/lib.rs +++ b/circuit-construction/src/lib.rs @@ -1,3 +1,5 @@ +#![doc = include_str!("../../README.md")] + use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{BigInteger, FftField, Field, One, PrimeField, SquareRootField, Zero}; use array_init::array_init; diff --git a/kimchi/Cargo.toml b/kimchi/Cargo.toml index 734115b900..9fce052287 100644 --- a/kimchi/Cargo.toml +++ b/kimchi/Cargo.toml @@ -37,7 +37,6 @@ groupmap = { path = "../groupmap" } mina-curves = { path = "../curves" } o1-utils = { path = "../utils" } oracle = { path = "../oracle" } -circuit-construction = { path = "../circuit-construction" } ocaml = { version = "0.22.2", optional = true } ocaml-gen = { path = "../ocaml/ocaml-gen", optional = true} diff --git a/kimchi/src/lib.rs b/kimchi/src/lib.rs index a1f033a4c5..fb34f842e6 100644 --- a/kimchi/src/lib.rs +++ b/kimchi/src/lib.rs @@ -1,4 +1,4 @@ -#![doc = include_str!("../../README.md")] +#![doc = include_str!("../README.md")] #[macro_use] extern crate num_derive; From a653e30846bc62a5f33cb04ffb8da59cf71159aa Mon Sep 17 00:00:00 2001 From: kata Date: Sat, 11 Jun 2022 11:27:15 +0800 Subject: [PATCH 5/7] remove TODO comments from readme example --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 9c2f10e594..fa1e9c2ace 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,6 @@ pub fn circuit< let first_public_input = public_input[0]; let witness = sys.var(|| witness.unwrap()); - //TODO replace the following free variable once the "add" gate constraint is ready to use. let result = sys.var(|| { first_public_input.val() + witness.val() }); @@ -59,7 +58,6 @@ pub fn circuit< // create SRS let srs = { - //TODO how to determine depth value? it throws error when the depth is too large let mut srs = SRS::::create(1 << 3); // 2^3 = 8 srs.add_lagrange_basis(D::new(srs.g.len()).unwrap()); Arc::new(srs) @@ -69,11 +67,9 @@ let group_map = ::Map::setup(); // generate circuit and index let prover_index = generate_prover_index::( - //TODO do these arguments have to be provided? srs, &fp_constants(), &oracle::pasta::fq_kimchi::params(), - //TODO should this be encapsulated? public_inputs.len(), |sys, p| circuit::<_, _>(None, sys, p), ); From 4dd34ca350ead6ba0728b5dac032b44c381158e6 Mon Sep 17 00:00:00 2001 From: kata Date: Sat, 11 Jun 2022 15:57:57 +0800 Subject: [PATCH 6/7] re-export modules via circuit-construction --- README.md | 30 ++++++++++++--------- circuit-construction/src/lib.rs | 6 +++++ circuit-construction/tests/example_proof.rs | 30 +++++++++++---------- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index fa1e9c2ace..612e144873 100644 --- a/README.md +++ b/README.md @@ -22,19 +22,23 @@ Then, you can create an URS for your circuit in the following way: ```rust use std::sync::Arc; use ark_ff::{FftField, PrimeField}; -use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as D}; -use circuit_construction::*; -use commitment_dlog::{commitment::CommitmentCurve, srs::SRS}; -use groupmap::GroupMap; -use kimchi::verifier::verify; -use mina_curves::pasta::{ - fp::Fp, - vesta::{Affine, VestaParameters}, -}; -use oracle::{ - constants::*, - sponge::{DefaultFqSponge, DefaultFrSponge}, +use ark_poly::{EvaluationDomain, Radix2EvaluationDomain}; +use circuit_construction::{ + *, + oracle::{ + constants::*, + poseidon::{Sponge}, + sponge::{DefaultFqSponge, DefaultFrSponge}, + }, + kimchi::verifier::verify, + commitment_dlog::{commitment::CommitmentCurve, srs::SRS}, + groupmap::GroupMap, + mina_curves::pasta::{ + fp::Fp, + vesta::{Affine, VestaParameters}, + } }; + type SpongeQ = DefaultFqSponge; type SpongeR = DefaultFrSponge; @@ -59,7 +63,7 @@ pub fn circuit< // create SRS let srs = { let mut srs = SRS::::create(1 << 3); // 2^3 = 8 - srs.add_lagrange_basis(D::new(srs.g.len()).unwrap()); + srs.add_lagrange_basis(Radix2EvaluationDomain::new(srs.g.len()).unwrap()); Arc::new(srs) }; let public_inputs = vec![1i32.into()]; diff --git a/circuit-construction/src/lib.rs b/circuit-construction/src/lib.rs index f4bd7f3374..f8e5381a46 100644 --- a/circuit-construction/src/lib.rs +++ b/circuit-construction/src/lib.rs @@ -1,5 +1,11 @@ #![doc = include_str!("../../README.md")] +pub use oracle; +pub use kimchi; +pub use commitment_dlog; +pub use groupmap; +pub use mina_curves; + use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{BigInteger, FftField, Field, One, PrimeField, SquareRootField, Zero}; use array_init::array_init; diff --git a/circuit-construction/tests/example_proof.rs b/circuit-construction/tests/example_proof.rs index dcf53ed938..8f5ad2abdf 100644 --- a/circuit-construction/tests/example_proof.rs +++ b/circuit-construction/tests/example_proof.rs @@ -1,21 +1,23 @@ +use std::sync::Arc; use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{FftField, PrimeField, UniformRand}; use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as D}; -use circuit_construction::*; -use commitment_dlog::{commitment::CommitmentCurve, srs::SRS}; -use groupmap::GroupMap; -use kimchi::verifier::verify; -use mina_curves::pasta::{ - fp::Fp, - pallas::Affine as Other, - vesta::{Affine, VestaParameters}, -}; -use oracle::{ - constants::*, - poseidon::{ArithmeticSponge, Sponge}, - sponge::{DefaultFqSponge, DefaultFrSponge}, +use circuit_construction::{ + *, + oracle::{ + constants::*, + poseidon::{ArithmeticSponge, Sponge}, + sponge::{DefaultFqSponge, DefaultFrSponge}, + }, + kimchi::verifier::verify, + commitment_dlog::{commitment::CommitmentCurve, srs::SRS}, + groupmap::GroupMap, + mina_curves::pasta::{ + fp::Fp, + pallas::Affine as Other, + vesta::{Affine, VestaParameters}, + }, }; -use std::sync::Arc; type SpongeQ = DefaultFqSponge; type SpongeR = DefaultFrSponge; From 10903907765379461afbaf4fb3e2c583278f45ad Mon Sep 17 00:00:00 2001 From: kata Date: Sun, 12 Jun 2022 08:59:31 +0800 Subject: [PATCH 7/7] cargo fmt --- circuit-construction/src/lib.rs | 4 ++-- circuit-construction/tests/example_proof.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/circuit-construction/src/lib.rs b/circuit-construction/src/lib.rs index f8e5381a46..5ba4cf0477 100644 --- a/circuit-construction/src/lib.rs +++ b/circuit-construction/src/lib.rs @@ -1,10 +1,10 @@ #![doc = include_str!("../../README.md")] -pub use oracle; -pub use kimchi; pub use commitment_dlog; pub use groupmap; +pub use kimchi; pub use mina_curves; +pub use oracle; use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{BigInteger, FftField, Field, One, PrimeField, SquareRootField, Zero}; diff --git a/circuit-construction/tests/example_proof.rs b/circuit-construction/tests/example_proof.rs index 8f5ad2abdf..876470d9ec 100644 --- a/circuit-construction/tests/example_proof.rs +++ b/circuit-construction/tests/example_proof.rs @@ -1,23 +1,23 @@ -use std::sync::Arc; use ark_ec::{AffineCurve, ProjectiveCurve}; use ark_ff::{FftField, PrimeField, UniformRand}; use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as D}; use circuit_construction::{ - *, - oracle::{ - constants::*, - poseidon::{ArithmeticSponge, Sponge}, - sponge::{DefaultFqSponge, DefaultFrSponge}, - }, - kimchi::verifier::verify, commitment_dlog::{commitment::CommitmentCurve, srs::SRS}, groupmap::GroupMap, + kimchi::verifier::verify, mina_curves::pasta::{ fp::Fp, pallas::Affine as Other, vesta::{Affine, VestaParameters}, }, + oracle::{ + constants::*, + poseidon::{ArithmeticSponge, Sponge}, + sponge::{DefaultFqSponge, DefaultFrSponge}, + }, + *, }; +use std::sync::Arc; type SpongeQ = DefaultFqSponge; type SpongeR = DefaultFrSponge;