From ecc9b4e673557e3cab4c4327310e672eefed49ff Mon Sep 17 00:00:00 2001 From: gabrielbosio Date: Thu, 29 Feb 2024 10:14:02 -0300 Subject: [PATCH] Wrap KZG proof from Rust to OCaml --- Cargo.lock | 44 +++++++++--- kimchi/src/circuits/lookup/constraints.rs | 2 +- kimchi/src/prover.rs | 88 ++++++++++++++++++++++- poly-commitment/src/commitment.rs | 44 +++++++++++- 4 files changed, 163 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca34bf33a3..55eeb5a096 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -165,7 +165,7 @@ dependencies = [ "num-traits", "paste", "rayon", - "rustc_version", + "rustc_version 0.3.3", "zeroize", ] @@ -435,6 +435,15 @@ dependencies = [ "toml", ] +[[package]] +name = "cast" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c24dab4283a142afa2fdca129b80ad2c6284e073930f964c3a1293c225ee39a" +dependencies = [ + "rustc_version 0.4.0", +] + [[package]] name = "cast" version = "0.3.0" @@ -647,12 +656,12 @@ dependencies = [ [[package]] name = "criterion" -version = "0.3.6" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f" +checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23" dependencies = [ "atty", - "cast", + "cast 0.2.7", "clap 2.34.0", "criterion-plot", "csv", @@ -677,7 +686,7 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" dependencies = [ - "cast", + "cast 0.3.0", "itertools", ] @@ -1968,9 +1977,9 @@ dependencies = [ [[package]] name = "plotters" -version = "0.3.5" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c224ba00d7cadd4d5c660deaf2098e5e80e07846537c51f9cfa4be50c1fd45" +checksum = "45ca0ae5f169d0917a7c7f5a9c1a3d3d9598f18f529dd2b8373ed988efea307a" dependencies = [ "num-traits", "plotters-backend", @@ -2301,7 +2310,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" dependencies = [ - "semver", + "semver 0.11.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver 1.0.22", ] [[package]] @@ -2403,6 +2421,12 @@ dependencies = [ "semver-parser", ] +[[package]] +name = "semver" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" + [[package]] name = "semver-parser" version = "0.10.2" @@ -2976,9 +3000,9 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "web-sys" -version = "0.3.64" +version = "0.3.63" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +checksum = "3bdd9ef4e984da1187bf8110c5cf5b845fbc87a23602cdf912386a76fcd3a7c2" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/kimchi/src/circuits/lookup/constraints.rs b/kimchi/src/circuits/lookup/constraints.rs index 715b6f2c80..9cd92e4272 100644 --- a/kimchi/src/circuits/lookup/constraints.rs +++ b/kimchi/src/circuits/lookup/constraints.rs @@ -12,7 +12,7 @@ use crate::{ }; use ark_ff::{FftField, One, PrimeField, Zero}; use ark_poly::{EvaluationDomain, Evaluations, Radix2EvaluationDomain as D}; -use o1_utils::adjacent_pairs::AdjacentPairs; +use o1_utils::{adjacent_pairs::AdjacentPairs, FieldHelpers}; use rand::Rng; use serde::{Deserialize, Serialize}; use serde_with::serde_as; diff --git a/kimchi/src/prover.rs b/kimchi/src/prover.rs index 44c6571765..3b0bc69a1c 100644 --- a/kimchi/src/prover.rs +++ b/kimchi/src/prover.rs @@ -848,8 +848,9 @@ where } // public polynomial - let mut f = t4.interpolate() + t8.interpolate(); - f += &public_poly; + // let mut f = t4.interpolate() + t8.interpolate(); + // f += &public_poly; + let f = DensePolynomial::zero(); // divide contributions with vanishing polynomial let (mut quotient, res) = f @@ -1514,11 +1515,13 @@ internal_tracing::decl_traces!(internal_traces; #[cfg(feature = "ocaml_types")] pub mod caml { use super::*; + use crate::poly_commitment::commitment::caml::CamlPairingProof; use crate::proof::caml::{CamlProofEvaluations, CamlRecursionChallenge}; - use ark_ec::AffineCurve; + use ark_ec::{AffineCurve, PairingEngine}; use poly_commitment::{ commitment::caml::{CamlOpeningProof, CamlPolyComm}, evaluation_proof::OpeningProof, + pairing_proof::PairingProof, }; #[cfg(feature = "internal_tracing")] @@ -1530,6 +1533,12 @@ pub mod caml { pub proof: CamlProverProof, } + #[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)] + pub struct CamlKzgProofWithPublic { + pub public_evals: Option>>, + pub proof: CamlKzgProverProof, + } + // // CamlProverProof // @@ -1545,6 +1554,21 @@ pub mod caml { pub prev_challenges: Vec>, //Vec<(Vec, CamlPolyComm)>, } + // + // CamlKzgProverProof + // + + #[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)] + pub struct CamlKzgProverProof { + pub commitments: CamlProverCommitments, + pub proof: CamlPairingProof, + // OCaml doesn't have sized arrays, so we have to convert to a tuple.. + pub evals: CamlProofEvaluations, + pub ft_eval1: CamlF, + pub public: Vec, + pub prev_challenges: Vec>, //Vec<(Vec, CamlPolyComm)>, + } + // // CamlProverCommitments // @@ -1782,4 +1806,62 @@ pub mod caml { (proof, caml_pp.public.into_iter().map(Into::into).collect()) } } + + impl From<(ProverProof>, Vec)> + for CamlKzgProofWithPublic + where + Pair: PairingEngine, + G: AffineCurve, + CamlG: From + From<::G1Affine>, + CamlF: From + + From<<::G1Affine as AffineCurve>::ScalarField>, + { + fn from(pp: (ProverProof>, Vec)) -> Self { + let (public_evals, evals) = pp.0.evals.into(); + CamlKzgProofWithPublic { + public_evals, + proof: CamlKzgProverProof { + commitments: pp.0.commitments.into(), + proof: pp.0.proof.into(), + evals, + ft_eval1: pp.0.ft_eval1.into(), + public: pp.1.into_iter().map(Into::into).collect(), + prev_challenges: pp.0.prev_challenges.into_iter().map(Into::into).collect(), + }, + } + } + } + + impl From> + for (ProverProof>, Vec) + where + Pair: PairingEngine, + CamlF: Clone, + G: AffineCurve + From, + G::ScalarField: From, + ::G1Affine: From, + <::G1Affine as AffineCurve>::ScalarField: From, + { + fn from( + caml_pp: CamlKzgProofWithPublic, + ) -> (ProverProof>, Vec) { + let CamlKzgProofWithPublic { + public_evals, + proof: caml_pp, + } = caml_pp; + let proof = ProverProof { + commitments: caml_pp.commitments.into(), + proof: caml_pp.proof.into(), + evals: (public_evals, caml_pp.evals).into(), + ft_eval1: caml_pp.ft_eval1.into(), + prev_challenges: caml_pp + .prev_challenges + .into_iter() + .map(Into::into) + .collect(), + }; + + (proof, caml_pp.public.into_iter().map(Into::into).collect()) + } + } } diff --git a/poly-commitment/src/commitment.rs b/poly-commitment/src/commitment.rs index 3d6cdf2411..fe83d44dbf 100644 --- a/poly-commitment/src/commitment.rs +++ b/poly-commitment/src/commitment.rs @@ -1167,6 +1167,11 @@ mod tests { #[cfg(feature = "ocaml_types")] pub mod caml { + use ark_ec::PairingEngine; + + use crate::pairing_proof; + use crate::pairing_proof::PairingProof; + use super::*; // polynomial commitment @@ -1233,7 +1238,6 @@ pub mod caml { #[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)] pub struct CamlOpeningProof { - /// vector of rounds of L & R commitments pub lr: Vec<(G, G)>, pub delta: G, pub z1: F, @@ -1282,4 +1286,42 @@ pub mod caml { } } } + + // pairing proof + + #[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)] + pub struct CamlPairingProof { + pub quotient: G, + pub blinding: F, + } + + impl From> for CamlPairingProof + where + Pair: PairingEngine, + Pair::G1Affine: AffineCurve, + CamlG: From, + CamlF: From<::ScalarField>, + { + fn from(pairing_proof: PairingProof) -> Self { + Self { + quotient: pairing_proof.quotient.into(), + blinding: pairing_proof.blinding.into(), + } + } + } + + impl From> for PairingProof + where + Pair: PairingEngine, + Pair::G1Affine: AffineCurve, + CamlG: Into, + CamlF: Into<::ScalarField>, + { + fn from(caml: CamlPairingProof) -> Self { + Self { + quotient: caml.quotient.into(), + blinding: caml.blinding.into(), + } + } + } }