Skip to content

Commit

Permalink
Merge branch 'develop' into develop-20230927-temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
dannywillems committed Oct 5, 2023
2 parents 33652eb + becddaa commit 774ed2e
Show file tree
Hide file tree
Showing 31 changed files with 109 additions and 355 deletions.
1 change: 0 additions & 1 deletion src/dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
(package (name pipe_lib))
(package (name pokolog))
(package (name ppx_annot))
(package (name ppx_dhall_type))
(package (name ppx_mina))
(package (name ppx_register_event))
(package (name ppx_representatives))
Expand Down
11 changes: 6 additions & 5 deletions src/lib/crypto/kimchi_bindings/stubs/src/oracles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use mina_poseidon::{
};
use paste::paste;
use poly_commitment::commitment::{caml::CamlPolyComm, shift_scalar, PolyComm};
use poly_commitment::evaluation_proof::OpeningProof;
use poly_commitment::SRS;

#[derive(ocaml::IntoValue, ocaml::FromValue, ocaml_gen::Struct)]
pub struct CamlOracles<F> {
Expand All @@ -25,7 +27,6 @@ pub struct CamlOracles<F> {

macro_rules! impl_oracles {
($CamlF: ty, $F: ty, $CamlG: ty, $G: ty, $index: ty, $curve_params: ty) => {

paste! {
#[ocaml_gen::func]
#[ocaml::func]
Expand All @@ -34,7 +35,7 @@ macro_rules! impl_oracles {
index: $index,
proof: CamlProofWithPublic<$CamlG, $CamlF>,
) -> Result<CamlOracles<$CamlF>, ocaml::Error> {
let index: VerifierIndex<$G> = index.into();
let index: VerifierIndex<$G, OpeningProof<$G>> = index.into();

let lgr_comm: Vec<PolyComm<$G>> = lgr_comm
.into_iter()
Expand Down Expand Up @@ -65,7 +66,7 @@ macro_rules! impl_oracles {
.commitment
};

let (proof, public_input): (ProverProof<$G>, Vec<$F>) = proof.into();
let (proof, public_input): (ProverProof<$G, OpeningProof<$G>>, Vec<$F>) = proof.into();

let oracles_result =
proof.oracles::<
Expand Down Expand Up @@ -110,7 +111,7 @@ macro_rules! impl_oracles {
public_evals: None,
};

let index: VerifierIndex<$G> = index.into();
let index: VerifierIndex<$G, OpeningProof<$G>> = index.into();

let lgr_comm: Vec<PolyComm<$G>> = lgr_comm
.into_iter()
Expand Down Expand Up @@ -141,7 +142,7 @@ macro_rules! impl_oracles {
.commitment
};

let (proof, public_input): (ProverProof<$G>, Vec<$F>) = proof.into();
let (proof, public_input): (ProverProof<$G, OpeningProof<$G>>, Vec<$F>) = proof.into();

let oracles_result =
proof.oracles::<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use kimchi::circuits::{constraints::ConstraintSystem, gate::CircuitGate};
use kimchi::{linearization::expr_linearization, prover_index::ProverIndex};
use mina_curves::pasta::{Fp, Pallas, Vesta, VestaParameters};
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSponge};
use poly_commitment::evaluation_proof::OpeningProof;
use serde::{Deserialize, Serialize};
use std::{
fs::{File, OpenOptions},
Expand All @@ -17,7 +18,7 @@ use std::{

/// Boxed so that we don't store large proving indexes in the OCaml heap.
#[derive(ocaml_gen::CustomType)]
pub struct CamlPastaFpPlonkIndex(pub Box<ProverIndex<Vesta>>);
pub struct CamlPastaFpPlonkIndex(pub Box<ProverIndex<Vesta, OpeningProof<Vesta>>>);
pub type CamlPastaFpPlonkIndexPtr<'a> = ocaml::Pointer<'a, CamlPastaFpPlonkIndex>;

extern "C" fn caml_pasta_fp_plonk_index_finalize(v: ocaml::Raw) {
Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn caml_pasta_fp_plonk_index_create(
}

// create index
let mut index = ProverIndex::<Vesta>::create(cs, endo_q, srs.clone());
let mut index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.clone());
// Compute and cache the verifier index digest
index.compute_verifier_index_digest::<DefaultFqSponge<VestaParameters, PlonkSpongeConstantsKimchi>>();

Expand Down Expand Up @@ -161,7 +162,9 @@ pub fn caml_pasta_fp_plonk_index_read(
}

// deserialize the index
let mut t = ProverIndex::<Vesta>::deserialize(&mut rmp_serde::Deserializer::new(r))?;
let mut t = ProverIndex::<Vesta, OpeningProof<Vesta>>::deserialize(
&mut rmp_serde::Deserializer::new(r),
)?;
t.srs = srs.clone();

let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true, 3);
Expand Down
34 changes: 21 additions & 13 deletions src/lib/crypto/kimchi_bindings/stubs/src/pasta_fp_plonk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn caml_pasta_fp_plonk_proof_create(
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.map_err(|_| ocaml::Error::Message("the witness should be a column of 15 vectors"))?;
let index: &ProverIndex<Vesta> = &index.as_ref().0;
let index: &ProverIndex<Vesta, OpeningProof<Vesta>> = &index.as_ref().0;
let runtime_tables: Vec<RuntimeTable<Fp>> =
runtime_tables.into_iter().map(Into::into).collect();

Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn caml_pasta_fp_plonk_proof_create_and_verify(
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.map_err(|_| ocaml::Error::Message("the witness should be a column of 15 vectors"))?;
let index: &ProverIndex<Vesta> = &index.as_ref().0;
let index: &ProverIndex<Vesta, OpeningProof<Vesta>> = &index.as_ref().0;
let runtime_tables: Vec<RuntimeTable<Fp>> =
runtime_tables.into_iter().map(Into::into).collect();

Expand Down Expand Up @@ -171,7 +171,12 @@ pub fn caml_pasta_fp_plonk_proof_create_and_verify(
let verifier_index = index.verifier_index();

// Verify proof
verify::<Vesta, EFqSponge, EFrSponge>(&group_map, &verifier_index, &proof, &public_input)?;
verify::<Vesta, EFqSponge, EFrSponge, OpeningProof<Vesta>>(
&group_map,
&verifier_index,
&proof,
&public_input,
)?;

Ok((proof, public_input).into())
})
Expand Down Expand Up @@ -261,7 +266,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_lookup(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let public_input = witness[0][0];
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
Expand Down Expand Up @@ -428,7 +433,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_foreign_field_mul(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
&group_map,
Expand Down Expand Up @@ -497,7 +502,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_range_check(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
&group_map,
Expand Down Expand Up @@ -572,7 +577,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_range_check0(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
&group_map,
Expand Down Expand Up @@ -697,7 +702,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_ffadd(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let public_input = witness[0][0];
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
Expand Down Expand Up @@ -786,7 +791,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_xor(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let public_input = (witness[0][0], witness[0][1]);
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
Expand Down Expand Up @@ -880,7 +885,7 @@ pub fn caml_pasta_fp_plonk_proof_example_with_rot(
ptr.add_lagrange_basis(cs.domain.d1);

let (endo_q, _endo_r) = endos::<Pallas>();
let index = ProverIndex::<Vesta>::create(cs, endo_q, srs.0);
let index = ProverIndex::<Vesta, OpeningProof<Vesta>>::create(cs, endo_q, srs.0);
let group_map = <Vesta as CommitmentCurve>::Map::setup();
let public_input = (witness[0][0], witness[0][1]);
let proof = ProverProof::create_recursive::<EFqSponge, EFrSponge>(
Expand Down Expand Up @@ -919,6 +924,7 @@ pub fn caml_pasta_fp_plonk_proof_verify(
Vesta,
DefaultFqSponge<VestaParameters, PlonkSpongeConstantsKimchi>,
DefaultFrSponge<Fp, PlonkSpongeConstantsKimchi>,
OpeningProof<Vesta>,
>(&group_map, &[context])
.is_ok()
}
Expand All @@ -933,12 +939,13 @@ pub fn caml_pasta_fp_plonk_proof_batch_verify(
.into_iter()
.zip(proofs.into_iter())
.map(|(caml_index, caml_proof)| {
let verifier_index: VerifierIndex<Vesta> = caml_index.into();
let (proof, public_input): (ProverProof<_>, Vec<_>) = caml_proof.into();
let verifier_index: VerifierIndex<Vesta, OpeningProof<Vesta>> = caml_index.into();
let (proof, public_input): (ProverProof<Vesta, OpeningProof<Vesta>>, Vec<_>) =
caml_proof.into();
(verifier_index, proof, public_input)
})
.collect();
let ts_ref: Vec<_> = ts
let ts_ref: Vec<Context<Vesta, OpeningProof<Vesta>>> = ts
.iter()
.map(|(verifier_index, proof, public_input)| Context {
verifier_index,
Expand All @@ -952,6 +959,7 @@ pub fn caml_pasta_fp_plonk_proof_batch_verify(
Vesta,
DefaultFqSponge<VestaParameters, PlonkSpongeConstantsKimchi>,
DefaultFrSponge<Fp, PlonkSpongeConstantsKimchi>,
OpeningProof<Vesta>,
>(&group_map, &ts_ref)
.is_ok()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ use kimchi::circuits::wires::{COLUMNS, PERMUTS};
use kimchi::{linearization::expr_linearization, verifier_index::VerifierIndex};
use mina_curves::pasta::{Fp, Pallas, Vesta};
use poly_commitment::commitment::caml::CamlPolyComm;
use poly_commitment::evaluation_proof::OpeningProof;
use poly_commitment::{commitment::PolyComm, srs::SRS};
use std::convert::TryInto;
use std::path::Path;
use std::sync::Arc;

pub type CamlPastaFpPlonkVerifierIndex =
CamlPlonkVerifierIndex<CamlFp, CamlFpSrs, CamlPolyComm<CamlGVesta>>;

impl From<VerifierIndex<Vesta>> for CamlPastaFpPlonkVerifierIndex {
fn from(vi: VerifierIndex<Vesta>) -> Self {
impl From<VerifierIndex<Vesta, OpeningProof<Vesta>>> for CamlPastaFpPlonkVerifierIndex {
fn from(vi: VerifierIndex<Vesta, OpeningProof<Vesta>>) -> Self {
Self {
domain: CamlPlonkDomain {
log_size_of_group: vi.domain.log_size_of_group as isize,
Expand All @@ -32,7 +34,7 @@ impl From<VerifierIndex<Vesta>> for CamlPastaFpPlonkVerifierIndex {
max_poly_size: vi.max_poly_size as isize,
public: vi.public as isize,
prev_challenges: vi.prev_challenges as isize,
srs: CamlFpSrs(vi.srs.get().expect("have an srs").clone()),
srs: CamlFpSrs(vi.srs.clone()),
evals: CamlPlonkVerificationEvals {
sigma_comm: vi.sigma_comm.to_vec().iter().map(Into::into).collect(),
coefficients_comm: vi
Expand Down Expand Up @@ -62,7 +64,7 @@ impl From<VerifierIndex<Vesta>> for CamlPastaFpPlonkVerifierIndex {
}

// TODO: This should really be a TryFrom or TryInto
impl From<CamlPastaFpPlonkVerifierIndex> for VerifierIndex<Vesta> {
impl From<CamlPastaFpPlonkVerifierIndex> for VerifierIndex<Vesta, OpeningProof<Vesta>> {
fn from(index: CamlPastaFpPlonkVerifierIndex) -> Self {
let evals = index.evals;
let shifts = index.shifts;
Expand Down Expand Up @@ -110,17 +112,13 @@ impl From<CamlPastaFpPlonkVerifierIndex> for VerifierIndex<Vesta> {
// TODO dummy_lookup_value ?
let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true, 3);

VerifierIndex::<Vesta> {
VerifierIndex::<Vesta, OpeningProof<Vesta>> {
domain,
max_poly_size: index.max_poly_size as usize,
public: index.public as usize,
prev_challenges: index.prev_challenges as usize,
powers_of_alpha,
srs: {
let res = once_cell::sync::OnceCell::new();
res.set(index.srs.0).unwrap();
res
},
srs: { Arc::clone(&index.srs.0) },

zk_rows: 3,

Expand Down Expand Up @@ -165,16 +163,20 @@ pub fn read_raw(
offset: Option<ocaml::Int>,
srs: CamlFpSrs,
path: String,
) -> Result<VerifierIndex<Vesta>, ocaml::Error> {
) -> Result<VerifierIndex<Vesta, OpeningProof<Vesta>>, ocaml::Error> {
let path = Path::new(&path);
let (endo_q, _endo_r) = poly_commitment::srs::endos::<Pallas>();
VerifierIndex::<Vesta>::from_file(Some(srs.0), path, offset.map(|x| x as u64), endo_q).map_err(
|_e| {
ocaml::Error::invalid_argument("caml_pasta_fp_plonk_verifier_index_raw_read")
.err()
.unwrap()
},
VerifierIndex::<Vesta, OpeningProof<Vesta>>::from_file(
srs.0,
path,
offset.map(|x| x as u64),
endo_q,
)
.map_err(|_e| {
ocaml::Error::invalid_argument("caml_pasta_fp_plonk_verifier_index_raw_read")
.err()
.unwrap()
})
}

//
Expand All @@ -199,7 +201,7 @@ pub fn caml_pasta_fp_plonk_verifier_index_write(
index: CamlPastaFpPlonkVerifierIndex,
path: String,
) -> Result<(), ocaml::Error> {
let index: VerifierIndex<Vesta> = index.into();
let index: VerifierIndex<Vesta, OpeningProof<Vesta>> = index.into();
let path = Path::new(&path);
index.to_file(path, append).map_err(|_e| {
ocaml::Error::invalid_argument("caml_pasta_fp_plonk_verifier_index_raw_read")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use kimchi::circuits::{constraints::ConstraintSystem, gate::CircuitGate};
use kimchi::{linearization::expr_linearization, prover_index::ProverIndex};
use mina_curves::pasta::{Fq, Pallas, PallasParameters, Vesta};
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSponge};
use poly_commitment::evaluation_proof::OpeningProof;
use serde::{Deserialize, Serialize};
use std::{
fs::{File, OpenOptions},
Expand All @@ -17,7 +18,7 @@ use std::{

/// Boxed so that we don't store large proving indexes in the OCaml heap.
#[derive(ocaml_gen::CustomType)]
pub struct CamlPastaFqPlonkIndex(pub Box<ProverIndex<Pallas>>);
pub struct CamlPastaFqPlonkIndex(pub Box<ProverIndex<Pallas, OpeningProof<Pallas>>>);
pub type CamlPastaFqPlonkIndexPtr<'a> = ocaml::Pointer<'a, CamlPastaFqPlonkIndex>;

extern "C" fn caml_pasta_fq_plonk_index_finalize(v: ocaml::Raw) {
Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn caml_pasta_fq_plonk_index_create(
}

// create index
let mut index = ProverIndex::<Pallas>::create(cs, endo_q, srs.clone());
let mut index = ProverIndex::<Pallas, OpeningProof<Pallas>>::create(cs, endo_q, srs.clone());
// Compute and cache the verifier index digest
index.compute_verifier_index_digest::<DefaultFqSponge<PallasParameters, PlonkSpongeConstantsKimchi>>();

Expand Down Expand Up @@ -161,7 +162,9 @@ pub fn caml_pasta_fq_plonk_index_read(
}

// deserialize the index
let mut t = ProverIndex::<Pallas>::deserialize(&mut rmp_serde::Deserializer::new(r))?;
let mut t = ProverIndex::<Pallas, OpeningProof<Pallas>>::deserialize(
&mut rmp_serde::Deserializer::new(r),
)?;
t.srs = srs.clone();

let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true, 3);
Expand Down
11 changes: 7 additions & 4 deletions src/lib/crypto/kimchi_bindings/stubs/src/pasta_fq_plonk_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn caml_pasta_fq_plonk_proof_create(
let witness: [Vec<_>; COLUMNS] = witness
.try_into()
.expect("the witness should be a column of 15 vectors");
let index: &ProverIndex<Pallas> = &index.as_ref().0;
let index: &ProverIndex<Pallas, OpeningProof<Pallas>> = &index.as_ref().0;

let runtime_tables: Vec<RuntimeTable<Fq>> =
runtime_tables.into_iter().map(Into::into).collect();
Expand Down Expand Up @@ -115,6 +115,7 @@ pub fn caml_pasta_fq_plonk_proof_verify(
Pallas,
DefaultFqSponge<PallasParameters, PlonkSpongeConstantsKimchi>,
DefaultFrSponge<Fq, PlonkSpongeConstantsKimchi>,
OpeningProof<Pallas>,
>(&group_map, &[context])
.is_ok()
}
Expand All @@ -129,12 +130,13 @@ pub fn caml_pasta_fq_plonk_proof_batch_verify(
.into_iter()
.zip(proofs.into_iter())
.map(|(caml_index, caml_proof)| {
let verifier_index: VerifierIndex<Pallas> = caml_index.into();
let (proof, public_input): (ProverProof<_>, Vec<_>) = caml_proof.into();
let verifier_index: VerifierIndex<Pallas, OpeningProof<Pallas>> = caml_index.into();
let (proof, public_input): (ProverProof<Pallas, OpeningProof<Pallas>>, Vec<_>) =
caml_proof.into();
(verifier_index, proof, public_input)
})
.collect();
let ts_ref: Vec<_> = ts
let ts_ref: Vec<Context<Pallas, OpeningProof<Pallas>>> = ts
.iter()
.map(|(verifier_index, proof, public_input)| Context {
verifier_index,
Expand All @@ -148,6 +150,7 @@ pub fn caml_pasta_fq_plonk_proof_batch_verify(
Pallas,
DefaultFqSponge<PallasParameters, PlonkSpongeConstantsKimchi>,
DefaultFrSponge<Fq, PlonkSpongeConstantsKimchi>,
OpeningProof<Pallas>,
>(&group_map, &ts_ref)
.is_ok()
}
Expand Down
Loading

0 comments on commit 774ed2e

Please sign in to comment.