Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: bump to current Kimchi proof system revision #20

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,158 changes: 684 additions & 474 deletions Cargo.lock

Large diffs are not rendered by default.

39 changes: 20 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ description = "a programming language for writing zkapps"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ark-ec = "0.3.0" # elliptic curve library
ark-ff = "0.3.0" # finite field library
ark-serialize = "0.3.0" # serialization of arkworks types
ena = "0.14.0" # union-find implementation for the wiring
num-bigint = "0.4.3" # big int library
camino = "1.1.1" # to replace Path and PathBuf
clap = { version = "4.0.5", features = ["derive"] } # CLI library
dirs = "4.0.0" # helper functions (e.g. getting the home directory)
itertools = "0.10.3" # useful iter traits
kimchi = { git = "https://github.com/o1-labs/proof-systems", rev = "b9589626f834f9dbf9d587e73fd8176171231e90" } # ZKP
ark-ec = "0.3.0" # elliptic curve library
ark-ff = "0.3.0" # finite field library
ark-serialize = "0.3.0" # serialization of arkworks types
ena = "0.14.0" # union-find implementation for the wiring
num-bigint = "0.4.3" # big int library
camino = "1.1.1" # to replace Path and PathBuf
clap = { version = "4.0.5", features = ["derive"] } # CLI library
dirs = "4.0.0" # helper functions (e.g. getting the home directory)
itertools = "0.10.3" # useful iter traits
kimchi = { git = "https://github.com/o1-labs/proof-systems", rev = "a5d8883ddf649c22f38aaac122d368ecb9fa2230" } # ZKP - Dec 5th, 2023 revision
#kimchi = { git = "https://github.com/o1-labs/proof-systems", rev = "b9589626f834f9dbf9d587e73fd8176171231e90" } # ZKP
miette = { version = "5.0.0", features = ["fancy"] } # nice errors
num-traits = "0.2.15" # useful traits on big ints
once_cell = "1.15.0" # for lazy statics
regex = "1.6.0" # for regexes
rmp-serde = "1.1.1" # for serialization
serde_with = "2.0.1" # for serializing arkworks types
serde_json = "1.0.85" # to (de)serialize JSON
serde = "1.0.144" # to (de)serialize objects
thiserror = "1.0.31" # helpful error traits
toml = "0.5.9" # to parse manifest files
num-traits = "0.2.15" # useful traits on big ints
once_cell = "1.15.0" # for lazy statics
regex = "1.6.0" # for regexes
rmp-serde = "1.1.1" # for serialization
serde_with = "2.0.1" # for serializing arkworks types
serde_json = "1.0.85" # to (de)serialize JSON
serde = "1.0.144" # to (de)serialize objects
thiserror = "1.0.31" # helpful error traits
toml = "0.8.8" # to parse manifest files
3 changes: 2 additions & 1 deletion src/circuit_writer/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use ark_ff::{One, Zero};
use kimchi::circuits::polynomials::generic::{GENERIC_COEFFS, GENERIC_REGISTERS};
use kimchi::circuits::wires::Wire;
use num_bigint::BigUint;
use num_traits::Num as _;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -61,7 +62,7 @@ impl Gate {
pub fn to_kimchi_gate(&self, row: usize) -> kimchi::circuits::gate::CircuitGate<Field> {
kimchi::circuits::gate::CircuitGate {
typ: self.typ.into(),
wires: kimchi::circuits::wires::Wire::new(row),
wires: Wire::for_row(row),
coeffs: self.coeffs.clone(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ pub fn validate_package_and_get_manifest(path: &PathBuf, must_be_lib: bool) -> R

match (lib_path.exists(), main_path.exists()) {
(true, true) => miette::bail!(
"package `{}` has both a `lib.no` and a `main.no` file. Only one of them is allowed"),
"package has both a `lib.no` and a `main.no` file. Only one of them is allowed"),
(false, false) => miette::bail!(
"package `{}` has neither a `lib.no` nor a `main.no` file. At least one of them is required"),
(false, true) if must_be_lib => miette::bail!("package `{user}/{repo}` is missing a `lib.no` file"),
"package has neither a `lib.no` nor a `main.no` file. At least one of them is required"),
(false, true) if must_be_lib => miette::bail!("package is missing a `lib.no` file"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I'm wondering how that was compiling

_ => (),
}

Expand Down
2 changes: 1 addition & 1 deletion src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl CompiledCircuit {
// Helpers
//

pub trait ExtField: PrimeField {
pub trait ExtField /* : PrimeField*/ {
fn to_dec_string(&self) -> String;
}

Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ pub mod negative_tests;
//

pub mod helpers {
use kimchi::oracle::{constants::PlonkSpongeConstantsKimchi, poseidon::Sponge};
use kimchi::mina_poseidon::{
constants::PlonkSpongeConstantsKimchi,
pasta::fp_kimchi,
poseidon::{ArithmeticSponge, Sponge},
};

use crate::constants::Field;

Expand All @@ -54,12 +58,8 @@ pub mod helpers {
impl PrettyField for Field {}

pub fn poseidon(input: [Field; 2]) -> Field {
let mut sponge: kimchi::oracle::poseidon::ArithmeticSponge<
Field,
PlonkSpongeConstantsKimchi,
> = kimchi::oracle::poseidon::ArithmeticSponge::new(
kimchi::oracle::pasta::fp_kimchi::static_params(),
);
let mut sponge: ArithmeticSponge<Field, PlonkSpongeConstantsKimchi> =
ArithmeticSponge::new(fp_kimchi::static_params());
sponge.absorb(&input);
sponge.squeeze()
}
Expand Down
68 changes: 38 additions & 30 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ use crate::{
};

use itertools::chain;
use kimchi::{
commitment_dlog::commitment::CommitmentCurve, groupmap::GroupMap, proof::ProverProof,
};

use kimchi::circuits::constraints::ConstraintSystem;
use kimchi::groupmap::GroupMap;
use kimchi::mina_curves::pasta::{Pallas, Vesta, VestaParameters};
use kimchi::mina_poseidon::constants::PlonkSpongeConstantsKimchi;
use kimchi::mina_poseidon::sponge::{DefaultFqSponge, DefaultFrSponge};
use kimchi::poly_commitment::commitment::CommitmentCurve;
use kimchi::poly_commitment::evaluation_proof::OpeningProof;
use kimchi::poly_commitment::srs::SRS;
use kimchi::proof::ProverProof;

use miette::{Context, IntoDiagnostic};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
Expand All @@ -22,14 +30,11 @@ use serde::{Deserialize, Serialize};
// aliases
//

type Curve = kimchi::mina_curves::pasta::Vesta;
type OtherCurve = kimchi::mina_curves::pasta::Pallas;
type SpongeParams = kimchi::oracle::constants::PlonkSpongeConstantsKimchi;
type BaseSponge = kimchi::oracle::sponge::DefaultFqSponge<
kimchi::mina_curves::pasta::VestaParameters,
SpongeParams,
>;
type ScalarSponge = kimchi::oracle::sponge::DefaultFrSponge<Field, SpongeParams>;
type Curve = Vesta;
type OtherCurve = Pallas;
type SpongeParams = PlonkSpongeConstantsKimchi;
type BaseSponge = DefaultFqSponge<VestaParameters, SpongeParams>;
type ScalarSponge = DefaultFrSponge<Field, SpongeParams>;

//
// Lazy static
Expand All @@ -44,13 +49,13 @@ static GROUP_MAP: Lazy<<Curve as CommitmentCurve>::Map> =

//#[derive(Serialize, Deserialize)]
pub struct ProverIndex {
index: kimchi::prover_index::ProverIndex<Curve>,
index: kimchi::prover_index::ProverIndex<Curve, OpeningProof<Curve>>,
compiled_circuit: CompiledCircuit,
}

#[derive(Serialize, Deserialize)]
pub struct VerifierIndex {
index: kimchi::verifier_index::VerifierIndex<Curve>,
index: kimchi::verifier_index::VerifierIndex<Curve, OpeningProof<Curve>>,
}

//
Expand Down Expand Up @@ -91,23 +96,24 @@ pub fn compile_to_indexes(
}

// create constraint system
let cs = kimchi::circuits::constraints::ConstraintSystem::create(gates)
let cs = ConstraintSystem::create(gates)
.public(compiled_circuit.circuit.public_input_size)
.build()
.into_diagnostic()
.wrap_err("kimchi: could not create a constraint system with the given circuit and public input size")?;

// create SRS (for vesta, as the circuit is in Fp)
let mut srs = kimchi::commitment_dlog::srs::SRS::<Curve>::create(cs.domain.d1.size as usize);
let mut srs = SRS::<Curve>::create(cs.domain.d1.size as usize);
srs.add_lagrange_basis(cs.domain.d1);
let srs = std::sync::Arc::new(srs);

println!("using an SRS of size {}", srs.g.len());

// create indexes
let (endo_q, _endo_r) = kimchi::commitment_dlog::srs::endos::<OtherCurve>();
let (endo_q, _endo_r) = kimchi::poly_commitment::srs::endos::<OtherCurve>();

let prover_index = kimchi::prover_index::ProverIndex::<Curve>::create(cs, endo_q, srs);
let prover_index =
kimchi::prover_index::ProverIndex::<Curve, OpeningProof<Curve>>::create(cs, endo_q, srs);
let verifier_index = prover_index.verifier_index();

// wrap
Expand Down Expand Up @@ -149,7 +155,11 @@ impl ProverIndex {
public_inputs: JsonInputs,
private_inputs: JsonInputs,
debug: bool,
) -> miette::Result<(ProverProof<Curve>, Vec<Field>, Vec<Field>)> {
) -> miette::Result<(
ProverProof<Curve, OpeningProof<Curve>>,
Vec<Field>,
Vec<Field>,
)> {
// generate the witness
let (witness, full_public_inputs, public_output) = generate_witness(
&self.compiled_circuit,
Expand All @@ -168,10 +178,7 @@ impl ProverIndex {

// verify the witness
if debug {
self.index
.cs
.verify::<Curve>(&witness, &full_public_inputs)
.unwrap();
self.index.verify(&witness, &full_public_inputs).unwrap();
}

// create proof
Expand All @@ -193,15 +200,16 @@ impl VerifierIndex {
pub fn verify(
&self,
full_public_inputs: Vec<Field>,
proof: ProverProof<Curve>,
proof: ProverProof<Curve, OpeningProof<Curve>>,
) -> miette::Result<()> {
// pass the public input in the proof
let mut proof = proof;
proof.public = full_public_inputs;

// verify the proof
kimchi::verifier::verify::<Curve, BaseSponge, ScalarSponge>(&GROUP_MAP, &self.index, &proof)
.into_diagnostic()
.wrap_err("kimchi: failed to verify the proof")
kimchi::verifier::verify::<Curve, BaseSponge, ScalarSponge, OpeningProof<Curve>>(
&GROUP_MAP,
&self.index,
&proof,
&full_public_inputs,
)
.into_diagnostic()
.wrap_err("kimchi: failed to verify the proof")
}
}
15 changes: 5 additions & 10 deletions src/stdlib/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use ark_ff::Zero;
use kimchi::{
circuits::polynomials::poseidon::{POS_ROWS_PER_HASH, ROUNDS_PER_ROW},
oracle::{
self,
constants::{PlonkSpongeConstantsKimchi, SpongeConstants},
permutation::full_round,
},
};
use kimchi::circuits::polynomials::poseidon::{POS_ROWS_PER_HASH, ROUNDS_PER_ROW};
use kimchi::mina_poseidon::constants::{PlonkSpongeConstantsKimchi, SpongeConstants};
use kimchi::mina_poseidon::permutation::full_round;

use crate::{
circuit_writer::{CircuitWriter, GateKind, VarInfo},
Expand Down Expand Up @@ -64,7 +59,7 @@ pub fn poseidon(compiler: &mut CircuitWriter, vars: &[VarInfo], span: Span) -> R
}

// get constants needed for poseidon
let poseidon_params = oracle::pasta::fp_kimchi::params();
let poseidon_params = kimchi::mina_poseidon::pasta::fp_kimchi::params();

let rc = &poseidon_params.round_constants;
let width = PlonkSpongeConstantsKimchi::SPONGE_WIDTH;
Expand Down Expand Up @@ -103,7 +98,7 @@ pub fn poseidon(compiler: &mut CircuitWriter, vars: &[VarInfo], span: Span) -> R

// Do one full round on the previous value
full_round::<Field, PlonkSpongeConstantsKimchi>(
&oracle::pasta::fp_kimchi::params(),
&kimchi::mina_poseidon::pasta::fp_kimchi::params(),
&mut acc,
offset + i,
);
Expand Down
1 change: 1 addition & 0 deletions src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl CompiledCircuit {
{
// create the witness row
let mut witness_row = [Field::zero(); NUM_REGISTERS];

for (col, var) in row_of_vars.iter().enumerate() {
let val = if let Some(var) = var {
// if it's a public output, defer it's computation
Expand Down
Loading