kimchi_stubs/
pasta_fq_plonk_index.rs1use crate::{arkworks::CamlFq, gate_vector::fq::CamlPastaFqPlonkGateVectorPtr, srs::fq::CamlFqSrs};
2use ark_poly::EvaluationDomain;
3use kimchi::{
4 circuits::{
5 constraints::ConstraintSystem,
6 gate::CircuitGate,
7 lookup::{
8 runtime_tables::{caml::CamlRuntimeTableCfg, RuntimeTableCfg},
9 tables::{caml::CamlLookupTable, LookupTable},
10 },
11 },
12 linearization::expr_linearization,
13 prover_index::ProverIndex,
14};
15use mina_curves::pasta::{Fq, Pallas, PallasParameters, Vesta};
16use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSponge};
17use poly_commitment::{ipa::OpeningProof, lagrange_basis::WithLagrangeBasis, SRS as _};
18use serde::{Deserialize, Serialize};
19use std::{
20 fs::{File, OpenOptions},
21 io::{BufReader, BufWriter, Seek, SeekFrom::Start},
22};
23
24#[derive(ocaml_gen::CustomType)]
26pub struct CamlPastaFqPlonkIndex(pub Box<ProverIndex<Pallas, OpeningProof<Pallas>>>);
27pub type CamlPastaFqPlonkIndexPtr<'a> = ocaml::Pointer<'a, CamlPastaFqPlonkIndex>;
28
29extern "C" fn caml_pasta_fq_plonk_index_finalize(v: ocaml::Raw) {
30 unsafe {
31 let mut v: CamlPastaFqPlonkIndexPtr = v.as_pointer();
32 v.as_mut_ptr().drop_in_place();
33 }
34}
35
36impl ocaml::custom::Custom for CamlPastaFqPlonkIndex {
37 const NAME: &'static str = "CamlPastaFqPlonkIndex\0";
38 const USED: usize = 1;
39 const MAX: usize = 12;
41 const OPS: ocaml::custom::CustomOps = ocaml::custom::CustomOps {
42 identifier: Self::NAME.as_ptr() as *const ocaml::sys::Char,
43 finalize: Some(caml_pasta_fq_plonk_index_finalize),
44 ..ocaml::custom::DEFAULT_CUSTOM_OPS
45 };
46}
47
48#[ocaml_gen::func]
49#[ocaml::func]
50pub fn caml_pasta_fq_plonk_index_create(
51 gates: CamlPastaFqPlonkGateVectorPtr,
52 public: ocaml::Int,
53 lookup_tables: Vec<CamlLookupTable<CamlFq>>,
54 runtime_tables: Vec<CamlRuntimeTableCfg<CamlFq>>,
55 prev_challenges: ocaml::Int,
56 srs: CamlFqSrs,
57 lazy_mode: bool,
58) -> Result<CamlPastaFqPlonkIndex, ocaml::Error> {
59 let gates: Vec<_> = gates
60 .as_ref()
61 .0
62 .iter()
63 .map(|gate| CircuitGate::<Fq> {
64 typ: gate.typ,
65 wires: gate.wires,
66 coeffs: gate.coeffs.clone(),
67 })
68 .collect();
69
70 let runtime_tables: Vec<RuntimeTableCfg<Fq>> =
71 runtime_tables.into_iter().map(Into::into).collect();
72
73 let lookup_tables: Vec<LookupTable<Fq>> = lookup_tables.into_iter().map(Into::into).collect();
74
75 let cs = match ConstraintSystem::<Fq>::create(gates)
77 .public(public as usize)
78 .prev_challenges(prev_challenges as usize)
79 .lookup(lookup_tables)
80 .runtime(if runtime_tables.is_empty() {
81 None
82 } else {
83 Some(runtime_tables)
84 })
85 .lazy_mode(lazy_mode)
86 .build()
87 {
88 Err(e) => return Err(e.into()),
89 Ok(cs) => cs,
90 };
91
92 let (endo_q, _endo_r) = poly_commitment::ipa::endos::<Vesta>();
94
95 srs.0.with_lagrange_basis(cs.domain.d1);
96
97 let mut index =
99 ProverIndex::<Pallas, OpeningProof<Pallas>>::create(cs, endo_q, srs.clone(), lazy_mode);
100 index.compute_verifier_index_digest::<DefaultFqSponge<PallasParameters, PlonkSpongeConstantsKimchi>>();
102
103 Ok(CamlPastaFqPlonkIndex(Box::new(index)))
104}
105
106#[ocaml_gen::func]
107#[ocaml::func]
108pub fn caml_pasta_fq_plonk_index_max_degree(index: CamlPastaFqPlonkIndexPtr) -> ocaml::Int {
109 index.as_ref().0.srs.max_poly_size() as isize
110}
111
112#[ocaml_gen::func]
113#[ocaml::func]
114pub fn caml_pasta_fq_plonk_index_public_inputs(index: CamlPastaFqPlonkIndexPtr) -> ocaml::Int {
115 index.as_ref().0.cs.public as isize
116}
117
118#[ocaml_gen::func]
119#[ocaml::func]
120pub fn caml_pasta_fq_plonk_index_domain_d1_size(index: CamlPastaFqPlonkIndexPtr) -> ocaml::Int {
121 index.as_ref().0.cs.domain.d1.size() as isize
122}
123
124#[ocaml_gen::func]
125#[ocaml::func]
126pub fn caml_pasta_fq_plonk_index_domain_d4_size(index: CamlPastaFqPlonkIndexPtr) -> ocaml::Int {
127 index.as_ref().0.cs.domain.d4.size() as isize
128}
129
130#[ocaml_gen::func]
131#[ocaml::func]
132pub fn caml_pasta_fq_plonk_index_domain_d8_size(index: CamlPastaFqPlonkIndexPtr) -> ocaml::Int {
133 index.as_ref().0.cs.domain.d8.size() as isize
134}
135
136#[ocaml_gen::func]
137#[ocaml::func]
138pub fn caml_pasta_fq_plonk_index_read(
139 offset: Option<ocaml::Int>,
140 srs: CamlFqSrs,
141 path: String,
142) -> Result<CamlPastaFqPlonkIndex, ocaml::Error> {
143 let file = match File::open(path) {
145 Err(_) => {
146 return Err(
147 ocaml::Error::invalid_argument("caml_pasta_fp_plonk_index_read")
148 .err()
149 .unwrap(),
150 )
151 }
152 Ok(file) => file,
153 };
154 let mut r = BufReader::new(file);
155
156 if let Some(offset) = offset {
158 r.seek(Start(offset as u64))?;
159 }
160
161 let mut t = ProverIndex::<Pallas, OpeningProof<Pallas>>::deserialize(
163 &mut rmp_serde::Deserializer::new(r),
164 )?;
165 t.srs = srs.clone();
166
167 let (linearization, powers_of_alpha) = expr_linearization(Some(&t.cs.feature_flags), true);
168 t.linearization = linearization;
169 t.powers_of_alpha = powers_of_alpha;
170
171 Ok(CamlPastaFqPlonkIndex(Box::new(t)))
172}
173
174#[ocaml_gen::func]
175#[ocaml::func]
176pub fn caml_pasta_fq_plonk_index_write(
177 append: Option<bool>,
178 index: CamlPastaFqPlonkIndexPtr<'static>,
179 path: String,
180) -> Result<(), ocaml::Error> {
181 let file = OpenOptions::new()
182 .append(append.unwrap_or(true))
183 .open(path)
184 .map_err(|_| {
185 ocaml::Error::invalid_argument("caml_pasta_fq_plonk_index_write")
186 .err()
187 .unwrap()
188 })?;
189 let w = BufWriter::new(file);
190 index
191 .as_ref()
192 .0
193 .serialize(&mut rmp_serde::Serializer::new(w))
194 .map_err(|e| e.into())
195}