From 4a31d146cecc2a3fa55007749025ef7fb08eeb6d Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 11 Nov 2025 18:10:02 +0100 Subject: [PATCH 01/45] napi: add plonk proof verify --- plonk-napi/src/lib.rs | 5 +- plonk-napi/src/plonk_verifier_index/fp.rs | 18 +- plonk-napi/src/plonk_verifier_index/fq.rs | 11 +- plonk-napi/src/proof.rs | 265 +++++++++++++++------- plonk-napi/src/wrappers/wires.rs | 10 +- 5 files changed, 201 insertions(+), 108 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 8d520f3db46..832ad01ac12 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -43,7 +43,10 @@ pub use poly_comm::{ pallas::NapiFqPolyComm as WasmFqPolyComm, vesta::NapiFpPolyComm as WasmFpPolyComm, }; pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_block_cipher}; -pub use proof::caml_pasta_fp_plonk_proof_create; +pub use proof::{ + fp::{caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_verify}, + fq::{caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_verify}, +}; pub use srs::{ caml_fp_srs_from_bytes, caml_fp_srs_from_bytes_external, caml_fp_srs_to_bytes, caml_fq_srs_from_bytes, caml_fq_srs_from_bytes_external, caml_fq_srs_to_bytes, diff --git a/plonk-napi/src/plonk_verifier_index/fp.rs b/plonk-napi/src/plonk_verifier_index/fp.rs index 8b8ce603f98..7931c8d62a5 100644 --- a/plonk-napi/src/plonk_verifier_index/fp.rs +++ b/plonk-napi/src/plonk_verifier_index/fp.rs @@ -16,15 +16,12 @@ use kimchi::{ }, }, linearization::expr_linearization, - verifier_index::{LookupVerifierIndex, VerifierIndex as DlogVerifierIndex}, + verifier_index::{LookupVerifierIndex, VerifierIndex}, }; use mina_curves::pasta::{Fp, Pallas as GAffineOther, Vesta as GAffine}; use napi::bindgen_prelude::{Error, Status}; use napi_derive::napi; -use poly_commitment::{ - commitment::PolyComm, - ipa::{OpeningProof, SRS}, -}; +use poly_commitment::{commitment::PolyComm, ipa::OpeningProof}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -35,6 +32,13 @@ pub struct NapiFpDomain { pub group_gen: NapiPastaFp, } +impl From for Domain { + fn from(domain: NapiFpDomain) -> Self { + let size = 1 << domain.log_size_of_group; + Domain::::new(size).expect("Failed to create evaluation domain") + } +} + #[napi(object, js_name = "WasmFpShifts")] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NapiFpShifts { @@ -223,7 +227,7 @@ pub fn caml_pasta_fp_plonk_verifier_index_shifts( }) } -impl From for DlogVerifierIndex> { +impl From for VerifierIndex> { fn from(index: NapiFpPlonkVerifierIndex) -> Self { let max_poly_size = index.max_poly_size; let public_ = index.public_; @@ -242,7 +246,7 @@ impl From for DlogVerifierIndex for DlogVerifierIndex> { +impl From for VerifierIndex> { fn from(index: NapiFqPlonkVerifierIndex) -> Self { let max_poly_size = index.max_poly_size; let public_ = index.public_; @@ -242,7 +239,7 @@ impl From for DlogVerifierIndex>, - pub public_input: Vec, -} - -#[napi] -pub fn caml_pasta_fp_plonk_proof_create( - index: &External, - witness: WasmVecVecFp, - runtime_tables: NapiVector, - prev_challenges: NapiFlatVector, - prev_sgs: NapiVector, -) -> Result> { - let (maybe_proof, public_input) = { - index - .0 - .srs - .as_ref() - .get_lagrange_basis(index.0.as_ref().cs.domain.d1); - let prev: Vec> = { - if prev_challenges.is_empty() { - Vec::new() - } else { - let challenges_per_sg = prev_challenges.len() / prev_sgs.len(); - let d = prev_sgs - .into_iter() - .map(Into::::into) - .enumerate() - .map(|(i, sg)| { - let chals = prev_challenges - [(i * challenges_per_sg)..(i + 1) * challenges_per_sg] - .iter() - .cloned() - .map(Into::into) - .collect(); - let comm = PolyComm:: { chunks: vec![sg] }; - RecursionChallenge { chals, comm } - }) - .collect(); - d +macro_rules! impl_proof { + ( + $NapiG: ty, + $G: ty, + $NapiF: ty, + $F: ty, + $NapiPolyComm: ty, + $NapiSrs: ty, + $NapiIndex: ty, + $NapiVerifierIndex: ty, + $field_name: ident + ) => { + paste! { + pub struct [] { + pub proof: ProverProof<$G, OpeningProof<$G>>, + pub public_input: Vec<$F>, } - }; - let rust_runtime_tables: Vec> = runtime_tables - .into_iter() - .flat_map(|table| { - let JsRuntimeTableFp { id, data } = table; - data.into_iter().map(move |column| { - let values = NapiFlatVector::::from_bytes(column.to_vec()) + /* + pub struct []( + ProofEvaluations>> + ); + */ + + //type NapiProofEvaluations = []; + type NapiVecVecF = []; + type JsRuntimeTableF = []; + type NapiProofF = []; + + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_create">])] + pub fn []( + index: &External<$NapiIndex>, + witness: NapiVecVecF, + runtime_tables: NapiVector, + prev_challenges: NapiFlatVector<$NapiF>, + prev_sgs: NapiVector<$NapiG>, + ) -> Result> { + let (maybe_proof, public_input) = { + index + .0 + .srs + .as_ref() + .get_lagrange_basis(index.0.as_ref().cs.domain.d1); + let prev: Vec> = { + if prev_challenges.is_empty() { + Vec::new() + } else { + let challenges_per_sg = prev_challenges.len() / prev_sgs.len(); + let d = prev_sgs + .into_iter() + .map(Into::<$G>::into) + .enumerate() + .map(|(i, sg)| { + let chals = prev_challenges + [(i * challenges_per_sg)..(i + 1) * challenges_per_sg] + .iter() + .cloned() + .map(Into::into) + .collect(); + let comm = PolyComm::<$G> { chunks: vec![sg] }; + RecursionChallenge { chals, comm } + }) + .collect(); + d + } + }; + + let rust_runtime_tables: Vec> = runtime_tables .into_iter() - .map(Into::into) + .flat_map(|table| { + let JsRuntimeTableF { id, data } = table; + data.into_iter().map(move |column| { + let values = NapiFlatVector::<$NapiF>::from_bytes(column.to_vec()) + .into_iter() + .map(Into::into) + .collect(); + RuntimeTable { id, data: values } + }) + }) .collect(); - RuntimeTable { id, data: values } - }) - }) - .collect(); - let witness: [Vec<_>; COLUMNS] = witness - .0 - .try_into() - .expect("the witness should be a column of 15 vectors"); + let witness: [Vec<_>; COLUMNS] = witness + .0 + .try_into() + .expect("the witness should be a column of 15 vectors"); + + let index: &ProverIndex<$G, OpeningProof<$G>> = &index.0.as_ref(); + + let public_input = witness[0][0..index.cs.public].to_vec(); + + // Release the runtime lock so that other threads can run using it while we generate the proof. + let group_map = GroupMap::<_>::setup(); + let maybe_proof = ProverProof::create_recursive::< + DefaultFqSponge<_, PlonkSpongeConstantsKimchi>, + DefaultFrSponge<_, PlonkSpongeConstantsKimchi>, + _, + >( + &group_map, + witness, + &rust_runtime_tables, + index, + prev, + None, + &mut rand::rngs::OsRng, + ); + (maybe_proof, public_input) + }; - let index: &ProverIndex> = &index.0.as_ref(); + match maybe_proof { + Ok(proof) => Ok(External::new([] { + proof, + public_input, + })), + Err(err) => Err(NapiError::new(Status::GenericFailure, err.to_string())), + } + } + + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_verify">])] + pub fn []( + index: $NapiVerifierIndex, + proof: &External, + ) -> bool { + let group_map = <$G as CommitmentCurve>::Map::setup(); + let verifier_index = &index.into(); + let (proof, public_input) = (&proof.as_ref().proof, &proof.as_ref().public_input); + batch_verify::< + $G, + DefaultFqSponge<_, PlonkSpongeConstantsKimchi>, + DefaultFrSponge<_, PlonkSpongeConstantsKimchi>, + OpeningProof<$G> + >( + &group_map, + &[Context { verifier_index, proof, public_input }] + ).is_ok() + } + } + }; +} + +pub mod fp { + use super::*; + use crate::{ + pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, + wrappers::{field::NapiPastaFp, group::NapiGVesta}, + NapiFpPlonkVerifierIndex, + }; + use mina_curves::pasta::{Fp, Vesta}; - let public_input = witness[0][0..index.cs.public].to_vec(); + impl_proof!( + NapiGVesta, + Vesta, + NapiPastaFp, + Fp, + NapiFpPolyComm, + NapiSrs, + NapiPastaFpPlonkIndex, + NapiFpPlonkVerifierIndex, + Fp + ); +} - // Release the runtime lock so that other threads can run using it while we generate the proof. - let group_map = GroupMap::<_>::setup(); - let maybe_proof = ProverProof::create_recursive::< - DefaultFqSponge<_, PlonkSpongeConstantsKimchi>, - DefaultFrSponge<_, PlonkSpongeConstantsKimchi>, - _, - >( - &group_map, - witness, - &rust_runtime_tables, - index, - prev, - None, - &mut rand::rngs::OsRng, - ); - (maybe_proof, public_input) +pub mod fq { + use super::*; + use crate::{ + pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, + wrappers::{field::NapiPastaFq, group::NapiGPallas}, + NapiFqPlonkVerifierIndex, }; + use mina_curves::pasta::{Fq, Pallas}; - match maybe_proof { - Ok(proof) => Ok(External::new(Proof { - proof, - public_input, - })), - Err(err) => Err(NapiError::new(Status::GenericFailure, err.to_string())), - } + impl_proof!( + NapiGPallas, + Pallas, + NapiPastaFq, + Fq, + NapiFqPolyComm, + NapiSrs, + NapiPastaFqPlonkIndex, + NapiFqPlonkVerifierIndex, + Fq + ); } diff --git a/plonk-napi/src/wrappers/wires.rs b/plonk-napi/src/wrappers/wires.rs index 9a2f67ea11a..399de6ba9e7 100644 --- a/plonk-napi/src/wrappers/wires.rs +++ b/plonk-napi/src/wrappers/wires.rs @@ -1,4 +1,4 @@ -use kimchi::circuits::wires::Wire as KimchiWire; +use kimchi::circuits::wires::Wire; use napi_derive::napi; #[napi(object)] @@ -8,17 +8,17 @@ pub struct NapiWire { pub col: u32, } -impl From for KimchiWire { +impl From for Wire { fn from(value: NapiWire) -> Self { - KimchiWire { + Wire { row: value.row as usize, col: value.col as usize, } } } -impl From for NapiWire { - fn from(value: KimchiWire) -> Self { +impl From for NapiWire { + fn from(value: Wire) -> Self { Self { row: value.row as u32, col: value.col as u32, From aac711f91ae1cdab0298ca4691f41234dc4f9968 Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 11 Nov 2025 20:07:50 +0100 Subject: [PATCH 02/45] napi: implement plonk proof batch verify --- plonk-napi/src/lib.rs | 10 ++++++++-- plonk-napi/src/proof.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 832ad01ac12..dd23ca49f7e 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -44,8 +44,14 @@ pub use poly_comm::{ }; pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_block_cipher}; pub use proof::{ - fp::{caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_verify}, - fq::{caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_verify}, + fp::{ + caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, + caml_pasta_fp_plonk_proof_verify, + }, + fq::{ + caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, + caml_pasta_fq_plonk_proof_verify, + }, }; pub use srs::{ caml_fp_srs_from_bytes, caml_fp_srs_from_bytes_external, caml_fp_srs_to_bytes, diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 31e425548ce..30c5b7b0527 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -157,6 +157,40 @@ macro_rules! impl_proof { &[Context { verifier_index, proof, public_input }] ).is_ok() } + + + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_batch_verify">])] + pub fn []( + indexes: NapiVector<$NapiVerifierIndex>, + proofs: &External>, + ) -> bool { + let indexes: Vec<_> = indexes.into_iter().map(Into::into).collect(); + let proofs_ref = proofs.as_ref(); + + if indexes.len() != proofs_ref.len() { + return false; + } + + let contexts: Vec<_> = indexes + .iter() + .zip(proofs_ref.iter()) + .map(|(index, proof)| Context { + verifier_index: index, + proof: &proof.proof, + public_input: &proof.public_input, + }) + .collect(); + + let group_map = GroupMap::<_>::setup(); + + batch_verify::< + $G, + DefaultFqSponge<_, PlonkSpongeConstantsKimchi>, + DefaultFrSponge<_, PlonkSpongeConstantsKimchi>, + OpeningProof<$G> + >(&group_map, &contexts) + .is_ok() + } } }; } From 5a2f6853923b37b29ed8a5b81701573b39eca8ca Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 11 Nov 2025 20:16:40 +0100 Subject: [PATCH 03/45] napi: plonk proof deep copy with external --- plonk-napi/src/lib.rs | 4 ++-- plonk-napi/src/proof.rs | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index dd23ca49f7e..c39546a4178 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -46,11 +46,11 @@ pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_b pub use proof::{ fp::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, - caml_pasta_fp_plonk_proof_verify, + caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_verify, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, - caml_pasta_fq_plonk_proof_verify, + caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_verify, }, }; pub use srs::{ diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 30c5b7b0527..06c3f95fb0e 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -35,6 +35,7 @@ macro_rules! impl_proof { $field_name: ident ) => { paste! { + #[derive(Clone)] pub struct [] { pub proof: ProverProof<$G, OpeningProof<$G>>, pub public_input: Vec<$F>, @@ -191,6 +192,13 @@ macro_rules! impl_proof { >(&group_map, &contexts) .is_ok() } + + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_deep_copy">])] + pub fn []( + x: &External + ) -> External { + External::new(x.as_ref().clone()) + } } }; } From ad8954c272b74006588b1067230c329ac8508f1b Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 11 Nov 2025 20:26:56 +0100 Subject: [PATCH 04/45] napi: plonk proof dummy with external --- plonk-napi/src/lib.rs | 6 ++-- plonk-napi/src/proof.rs | 80 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index c39546a4178..338aa1db4f3 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -46,11 +46,13 @@ pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_b pub use proof::{ fp::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, - caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_verify, + caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_dummy, + caml_pasta_fp_plonk_proof_verify, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, - caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_verify, + caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_dummy, + caml_pasta_fq_plonk_proof_verify, }, }; pub use srs::{ diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 06c3f95fb0e..70841a0cfdd 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -2,10 +2,15 @@ use crate::{ tables::{JsRuntimeTableFp, JsRuntimeTableFq}, vector::{fp::NapiVecVecFp, fq::NapiVecVecFq, NapiFlatVector, NapiVector}, }; +use ark_ec::AffineRepr; +use ark_ff::One; +use core::array; use kimchi::{ circuits::{lookup::runtime_tables::RuntimeTable, wires::COLUMNS}, groupmap::GroupMap, - proof::{PointEvaluations, ProofEvaluations, ProverProof, RecursionChallenge}, + proof::{ + PointEvaluations, ProofEvaluations, ProverCommitments, ProverProof, RecursionChallenge, + }, prover_index::ProverIndex, verifier::{batch_verify, Context}, }; @@ -193,6 +198,79 @@ macro_rules! impl_proof { .is_ok() } + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_dummy">])] + pub fn []() -> External { + fn comm() -> PolyComm<$G> { + let g = $G::generator(); + PolyComm { + chunks: vec![g, g, g], + } + } + + let prev = RecursionChallenge { + chals: vec![$F::one(), $F::one()], + comm: comm(), + }; + let prev_challenges = vec![prev.clone(), prev.clone(), prev.clone()]; + + let g = $G::generator(); + let proof = OpeningProof { + lr: vec![(g, g), (g, g), (g, g)], + z1: $F::one(), + z2: $F::one(), + delta: g, + sg: g, + }; + let eval = || PointEvaluations { + zeta: vec![$F::one()], + zeta_omega: vec![$F::one()], + }; + let evals = ProofEvaluations { + w: core::array::from_fn(|_| eval()), + coefficients: core::array::from_fn(|_| eval()), + z: eval(), + s: core::array::from_fn(|_| eval()), + generic_selector: eval(), + poseidon_selector: eval(), + complete_add_selector: eval(), + mul_selector: eval(), + emul_selector: eval(), + endomul_scalar_selector: eval(), + range_check0_selector: None, + range_check1_selector: None, + foreign_field_add_selector: None, + foreign_field_mul_selector: None, + xor_selector: None, + rot_selector: None, + lookup_aggregation: None, + lookup_table: None, + lookup_sorted: array::from_fn(|_| None), + runtime_lookup_table: None, + runtime_lookup_table_selector: None, + xor_lookup_selector: None, + lookup_gate_lookup_selector: None, + range_check_lookup_selector: None, + foreign_field_mul_lookup_selector: None, + public: None, + }; + + let dlogproof = ProverProof { + commitments: ProverCommitments { + w_comm: core::array::from_fn(|_| comm()), + z_comm: comm(), + t_comm: comm(), + lookup: None, + }, + proof, + evals, + ft_eval1: $F::one(), + prev_challenges, + }; + + let public = vec![$F::one(), $F::one()]; + External::new(NapiProofF{proof: dlogproof, public_input: public}) + } + #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_deep_copy">])] pub fn []( x: &External From a5dc0f7be561299e10bb1bccd484c207f994406c Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 13 Nov 2025 21:51:31 +0300 Subject: [PATCH 05/45] Napi conversions of proof-related types --- Cargo.lock | 3 +- plonk-napi/Cargo.toml | 3 +- plonk-napi/src/lib.rs | 4 +- plonk-napi/src/proof.rs | 494 ++++++++++++++++++++++++++++++- plonk-napi/src/wrappers/field.rs | 7 + 5 files changed, 496 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6658d5d1843..c443639dafe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -2979,6 +2979,7 @@ dependencies = [ "ark-poly", "ark-serialize", "arkworks", + "base64 0.21.7", "getrandom 0.2.15", "kimchi", "libc", diff --git a/plonk-napi/Cargo.toml b/plonk-napi/Cargo.toml index 74ee5691f19..c06aac89909 100644 --- a/plonk-napi/Cargo.toml +++ b/plonk-napi/Cargo.toml @@ -26,9 +26,10 @@ arkworks.workspace = true mina-curves = { path = "../curves" } mina-poseidon = { path = "../poseidon" } o1-utils = { path = "../utils" } -poly-commitment = { path = "../poly-commitment" } plonk_wasm.workspace = true +poly-commitment = { path = "../poly-commitment" } +base64.workspace = true getrandom.workspace = true kimchi.workspace = true libc.workspace = true diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 338aa1db4f3..4d963e00daf 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -47,12 +47,12 @@ pub use proof::{ fp::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_dummy, - caml_pasta_fp_plonk_proof_verify, + caml_pasta_fp_plonk_proof_verify, NapiFpProverProof, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_dummy, - caml_pasta_fq_plonk_proof_verify, + caml_pasta_fq_plonk_proof_verify, NapiFqProverProof, }, }; pub use srs::{ diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 70841a0cfdd..e6687f32f1a 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -9,7 +9,8 @@ use kimchi::{ circuits::{lookup::runtime_tables::RuntimeTable, wires::COLUMNS}, groupmap::GroupMap, proof::{ - PointEvaluations, ProofEvaluations, ProverCommitments, ProverProof, RecursionChallenge, + LookupCommitments, PointEvaluations, ProofEvaluations, ProverCommitments, ProverProof, + RecursionChallenge, }, prover_index::ProverIndex, verifier::{batch_verify, Context}, @@ -19,7 +20,7 @@ use mina_poseidon::{ sponge::{DefaultFqSponge, DefaultFrSponge}, }; use napi::{ - bindgen_prelude::{External, Result}, + bindgen_prelude::{sys, ClassInstance, External, FromNapiValue, Result}, Error as NapiError, Status, }; use napi_derive::napi; @@ -40,22 +41,491 @@ macro_rules! impl_proof { $field_name: ident ) => { paste! { + type NapiVecVecF = []; + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct []( + ProofEvaluations>> + ); + + type NapiProofEvaluations = []; + + impl From for ProofEvaluations>> { + fn from(x: NapiProofEvaluations) -> Self { + x.0 + } + } + + impl From>>> for NapiProofEvaluations { + fn from(x: ProofEvaluations>>) -> Self { + Self(x) + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] + { + #[napi(skip)] + pub sorted: NapiVector<$NapiPolyComm>, + #[napi(skip)] + pub aggreg: $NapiPolyComm, + #[napi(skip)] + pub runtime: Option<$NapiPolyComm>, + } + + type NapiLookupCommitments = []; + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + sorted: NapiVector<$NapiPolyComm>, + aggreg: $NapiPolyComm, + runtime: Option<$NapiPolyComm>) -> Self { + NapiLookupCommitments { sorted, aggreg, runtime } + } + + #[napi(getter)] + pub fn sorted(&self) -> NapiVector<$NapiPolyComm> { + self.sorted.clone() + } + + #[napi(getter)] + pub fn aggreg(&self) -> $NapiPolyComm { + self.aggreg.clone() + } + + #[napi(getter)] + pub fn runtime(&self) -> Option<$NapiPolyComm> { + self.runtime.clone() + } + + #[napi(setter, js_name="set_sorted")] + pub fn set_sorted(&mut self, s: NapiVector<$NapiPolyComm>) { + self.sorted = s + } + + #[napi(setter, js_name="set_aggreg")] + pub fn set_aggreg(&mut self, a: $NapiPolyComm) { + self.aggreg = a + } + + #[napi(setter, js_name="set_runtime")] + pub fn set_runtime(&mut self, r: Option<$NapiPolyComm>) { + self.runtime = r + } + } + + impl From> for NapiLookupCommitments { + fn from(x: LookupCommitments<$G>) -> Self { + NapiLookupCommitments { + sorted: x.sorted.into_iter().map(Into::into).collect(), + aggreg: x.aggreg.into(), + runtime: x.runtime.map(Into::into) + } + } + } + + impl From for LookupCommitments<$G> { + fn from(x: NapiLookupCommitments) -> Self { + LookupCommitments { + sorted: x.sorted.into_iter().map(Into::into).collect(), + aggreg: x.aggreg.into(), + runtime: x.runtime.map(Into::into) + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] + { + #[napi(skip)] + pub w_comm: NapiVector<$NapiPolyComm>, + #[napi(skip)] + pub z_comm: $NapiPolyComm, + #[napi(skip)] + pub t_comm: $NapiPolyComm, + #[napi(skip)] + pub lookup: Option, + } + + type NapiProverCommitments = []; + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + w_comm: NapiVector<$NapiPolyComm>, + z_comm: $NapiPolyComm, + t_comm: $NapiPolyComm, + lookup: Option + ) -> Self { + NapiProverCommitments { w_comm, z_comm, t_comm, lookup } + } + + #[napi(getter, js_name="w_comm")] + pub fn w_comm(&self) -> NapiVector<$NapiPolyComm> { + self.w_comm.clone() + } + #[napi(getter, js_name="z_comm")] + pub fn z_comm(&self) -> $NapiPolyComm { + self.z_comm.clone() + } + #[napi(getter, js_name="t_comm")] + pub fn t_comm(&self) -> $NapiPolyComm { + self.t_comm.clone() + } + + #[napi(getter)] + pub fn lookup(&self) -> Option { + self.lookup.clone() + } + + #[napi(setter, js_name="set_w_comm")] + pub fn set_w_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + self.w_comm = x + } + #[napi(setter, js_name="set_z_comm")] + pub fn set_z_comm(&mut self, x: $NapiPolyComm) { + self.z_comm = x + } + #[napi(setter, js_name="set_t_comm")] + pub fn set_t_comm(&mut self, x: $NapiPolyComm) { + self.t_comm = x + } + + #[napi(setter, js_name="set_lookup")] + pub fn set_lookup(&mut self, l: Option) { + self.lookup = l + } + } + + impl From> for NapiProverCommitments { + fn from(x: ProverCommitments<$G>) -> Self { + NapiProverCommitments { + w_comm: x.w_comm.iter().map(Into::into).collect(), + z_comm: x.z_comm.into(), + t_comm: x.t_comm.into(), + lookup: x.lookup.map(Into::into), + } + } + } + + impl From for ProverCommitments<$G> { + fn from(x: NapiProverCommitments) -> Self { + ProverCommitments { + w_comm: core::array::from_fn(|i| (&x.w_comm[i]).into()), + z_comm: x.z_comm.into(), + t_comm: x.t_comm.into(), + lookup: x.lookup.map(Into::into), + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi(js_name = [] )] + #[derive(Clone, Debug)] + pub struct [] { + #[napi(skip)] + pub lr_0: NapiVector<$NapiG>, // vector of rounds of L commitments + #[napi(skip)] + pub lr_1: NapiVector<$NapiG>, // vector of rounds of R commitments + #[napi(skip)] + pub delta: $NapiG, + pub z1: $NapiF, + pub z2: $NapiF, + #[napi(skip)] + pub sg: $NapiG, + } + + type NapiOpeningProof = []; + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + lr_0: NapiVector<$NapiG>, + lr_1: NapiVector<$NapiG>, + delta: $NapiG, + z1: $NapiF, + z2: $NapiF, + sg: $NapiG) -> Self { + NapiOpeningProof { lr_0, lr_1, delta, z1, z2, sg } + } + + #[napi(getter, js_name="lr_0")] + pub fn lr_0(&self) -> NapiVector<$NapiG> { + self.lr_0.clone() + } + #[napi(getter, js_name="lr_1")] + pub fn lr_1(&self) -> NapiVector<$NapiG> { + self.lr_1.clone() + } + #[napi(getter)] + pub fn delta(&self) -> $NapiG { + self.delta.clone() + } + #[napi(getter)] + pub fn sg(&self) -> $NapiG { + self.sg.clone() + } + + #[napi(setter, js_name="set_lr_0")] + pub fn set_lr_0(&mut self, lr_0: NapiVector<$NapiG>) { + self.lr_0 = lr_0 + } + #[napi(setter, js_name="set_lr_1")] + pub fn set_lr_1(&mut self, lr_1: NapiVector<$NapiG>) { + self.lr_1 = lr_1 + } + #[napi(setter, js_name="set_delta")] + pub fn set_delta(&mut self, delta: $NapiG) { + self.delta = delta + } + #[napi(setter, js_name="set_sg")] + pub fn set_sg(&mut self, sg: $NapiG) { + self.sg = sg + } + } + + impl From for OpeningProof<$G> { + fn from(x: NapiOpeningProof) -> Self { + let NapiOpeningProof {lr_0, lr_1, delta, z1, z2, sg} = x; + OpeningProof { + lr: lr_0.into_iter().zip(lr_1.into_iter()).map(|(x, y)| (x.into(), y.into())).collect(), + delta: delta.into(), + z1: z1.into(), + z2: z2.into(), + sg: sg.into(), + } + } + } + + impl From> for NapiOpeningProof { + fn from(x: OpeningProof<$G>) -> Self { + let (lr_0, lr_1) = x.lr.clone().into_iter().map(|(x, y)| (x.into(), y.into())).unzip(); + NapiOpeningProof { + lr_0, + lr_1, + delta: x.delta.clone().into(), + z1: x.z1.into(), + z2: x.z2.into(), + sg: x.sg.clone().into(), + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + #[napi(skip)] + pub commitments: NapiProverCommitments, + #[napi(skip)] + pub proof: NapiOpeningProof, + // OCaml doesn't have sized arrays, so we have to convert to a tuple.. + #[napi(skip)] + pub evals: NapiProofEvaluations, + pub ft_eval1: $NapiF, + #[napi(skip)] + pub public: NapiFlatVector<$NapiF>, + #[napi(skip)] + pub prev_challenges_scalars: Vec>, + #[napi(skip)] + pub prev_challenges_comms:NapiVector<$NapiPolyComm>, + } + + type NapiProverProof = []; + + impl From<&NapiProverProof> for (ProverProof<$G, OpeningProof<$G>>, Vec<$F>) { + fn from(x: &NapiProverProof) -> Self { + let proof = ProverProof { + commitments: x.commitments.clone().into(), + proof: x.proof.clone().into(), + evals: x.evals.clone().into(), + prev_challenges: + (&x.prev_challenges_scalars) + .into_iter() + .zip((&x.prev_challenges_comms).into_iter()) + .map(|(chals, comm)| { + RecursionChallenge { + chals: chals.clone(), + comm: comm.into(), + } + }) + .collect(), + ft_eval1: x.ft_eval1.clone().into() + }; + let public = x.public.clone().into_iter().map(Into::into).collect(); + (proof, public) + } + } + + impl From for (ProverProof<$G, OpeningProof<$G>>, Vec<$F>) { + fn from(x: NapiProverProof) -> Self { + let proof = ProverProof { + commitments: x.commitments.into(), + proof: x.proof.into(), + evals: x.evals.into(), + prev_challenges: + (x.prev_challenges_scalars) + .into_iter() + .zip((x.prev_challenges_comms).into_iter()) + .map(|(chals, comm)| { + RecursionChallenge { + chals: chals.into(), + comm: comm.into(), + } + }) + .collect(), + ft_eval1: x.ft_eval1.into() + }; + let public = x.public.into_iter().map(Into::into).collect(); + (proof, public) + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + commitments: NapiProverCommitments, // maybe remove FromNapiValue trait implementation and wrap it in External instead + proof: NapiOpeningProof, + evals: NapiProofEvaluations, // maybe remove FromNapiValue trait implementation and wrap it in External instead + ft_eval1: $NapiF, + public_: NapiFlatVector<$NapiF>, + prev_challenges_scalars: NapiVecVecF, + prev_challenges_comms: NapiVector<$NapiPolyComm>) -> Self { + NapiProverProof { + commitments, + proof, + evals, + ft_eval1, + public: public_, + prev_challenges_scalars: prev_challenges_scalars.0, + prev_challenges_comms, + } + } + + #[napi(getter)] + pub fn commitments(&self) -> NapiProverCommitments { + self.commitments.clone() + } + #[napi(getter)] + pub fn proof(&self) -> NapiOpeningProof { + self.proof.clone() + } + #[napi(getter)] + pub fn evals(&self) -> NapiProofEvaluations { + self.evals.clone() + } + #[napi(getter, js_name="public_")] + pub fn public_(&self) -> NapiFlatVector<$NapiF> { + self.public.clone() + } + #[napi(getter, js_name="prev_challenges_scalars")] + pub fn prev_challenges_scalars(&self) -> NapiVecVecF { + [](self.prev_challenges_scalars.clone()) + } + #[napi(getter, js_name="prev_challenges_comms")] + pub fn prev_challenges_comms(&self) -> NapiVector<$NapiPolyComm> { + self.prev_challenges_comms.clone() + } + + #[napi(setter, js_name="set_commitments")] + pub fn set_commitments(&mut self, commitments: NapiProverCommitments) { + self.commitments = commitments + } + #[napi(setter, js_name="set_proof")] + pub fn set_proof(&mut self, proof: NapiOpeningProof) { + self.proof = proof + } + #[napi(setter, js_name="set_evals")] + pub fn set_evals(&mut self, evals: NapiProofEvaluations) { + self.evals = evals + } + #[napi(setter, js_name="set_public_")] + pub fn set_public_(&mut self, public_: NapiFlatVector<$NapiF>) { + self.public = public_ + } + #[napi(setter, js_name="set_prev_challenges_scalars")] + pub fn set_prev_challenges_scalars(&mut self, prev_challenges_scalars: NapiVecVecF) { + self.prev_challenges_scalars = prev_challenges_scalars.0 + } + #[napi(setter, js_name="set_prev_challenges_comms")] + pub fn set_prev_challenges_comms(&mut self, prev_challenges_comms: NapiVector<$NapiPolyComm>) { + self.prev_challenges_comms = prev_challenges_comms + } + + #[napi] + #[allow(deprecated)] + pub fn serialize(&self) -> String { + let (proof, _public_input) = self.into(); + let serialized = rmp_serde::to_vec(&proof).unwrap(); + // Deprecated used on purpose: updating this leads to a bug in o1js + base64::encode(serialized) + } + } + #[derive(Clone)] pub struct [] { pub proof: ProverProof<$G, OpeningProof<$G>>, pub public_input: Vec<$F>, } - /* - pub struct []( - ProofEvaluations>> - ); - */ - - //type NapiProofEvaluations = []; - type NapiVecVecF = []; - type JsRuntimeTableF = []; type NapiProofF = []; + type JsRuntimeTableF = []; #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_create">])] pub fn []( @@ -285,6 +755,7 @@ pub mod fp { use super::*; use crate::{ pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, + poly_comm::vesta::NapiFpPolyComm, wrappers::{field::NapiPastaFp, group::NapiGVesta}, NapiFpPlonkVerifierIndex, }; @@ -307,6 +778,7 @@ pub mod fq { use super::*; use crate::{ pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, + poly_comm::pallas::NapiFqPolyComm, wrappers::{field::NapiPastaFq, group::NapiGPallas}, NapiFqPlonkVerifierIndex, }; diff --git a/plonk-napi/src/wrappers/field.rs b/plonk-napi/src/wrappers/field.rs index 022636f39d1..7ebc27763ef 100644 --- a/plonk-napi/src/wrappers/field.rs +++ b/plonk-napi/src/wrappers/field.rs @@ -113,6 +113,13 @@ macro_rules! impl_field_wrapper { ::to_napi_value(env, buffer) } } + + impl<'a> ToNapiValue for &'a mut $name { + unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result { + let buffer = Buffer::from(val.to_bytes()); + ::to_napi_value(env, buffer) + } + } }; } From c2464c9fc18393be3816d4d1906b0fd8170e3b2a Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 13 Nov 2025 22:21:31 +0300 Subject: [PATCH 06/45] Napi conversions for oracles --- plonk-napi/src/lib.rs | 5 + plonk-napi/src/oracles.rs | 331 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 336 insertions(+) create mode 100644 plonk-napi/src/oracles.rs diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 4d963e00daf..fd0be143702 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -1,6 +1,7 @@ mod build_info; mod circuit; mod gate_vector; +mod oracles; mod pasta_fp_plonk_index; mod pasta_fq_plonk_index; mod plonk_verifier_index; @@ -30,6 +31,10 @@ pub use gate_vector::{ NapiFpGateVector as WasmFpGateVector, NapiFqGate as WasmFqGate, NapiFqGateVector as WasmFqGateVector, }; +pub use oracles::{ + fp::{fp_oracles_create, fp_oracles_deep_copy, fp_oracles_dummy}, + fq::{fq_oracles_create, fq_oracles_deep_copy, fq_oracles_dummy}, +}; pub use pasta_fp_plonk_index::{ prover_index_fp_from_bytes, prover_index_fp_to_bytes, WasmPastaFpPlonkIndex, }; diff --git a/plonk-napi/src/oracles.rs b/plonk-napi/src/oracles.rs new file mode 100644 index 00000000000..3c46822d030 --- /dev/null +++ b/plonk-napi/src/oracles.rs @@ -0,0 +1,331 @@ +use crate::{ + vector::{NapiFlatVector, NapiVector}, + wrappers::field::{NapiPastaFp, NapiPastaFq}, +}; +use ark_ff::{One, Zero}; +use kimchi::{ + circuits::scalars::RandomOracles, proof::ProverProof, + verifier_index::VerifierIndex as DlogVerifierIndex, +}; +use mina_poseidon::{ + self, + constants::PlonkSpongeConstantsKimchi, + sponge::{DefaultFqSponge, DefaultFrSponge}, + FqSponge, +}; +use napi::{bindgen_prelude::*, Error as NapiError, Status}; +use napi_derive::napi; +use paste::paste; +use poly_commitment::{ + commitment::{shift_scalar, PolyComm}, + ipa::OpeningProof, + SRS, +}; + +macro_rules! impl_oracles { + ($NapiF: ty, + $F: ty, + $NapiG: ty, + $G: ty, + $NapiPolyComm: ty, + $NapiProverProof: ty, + $index: ty, + $curve_params: ty, + $field_name: ident) => { + + paste! { + use mina_poseidon::sponge::ScalarChallenge; + + #[napi(js_name = [])] + #[derive(Clone, Copy)] + pub struct [] { + pub joint_combiner_chal: Option<$NapiF>, + pub joint_combiner: Option<$NapiF>, + pub beta: $NapiF, + pub gamma: $NapiF, + pub alpha_chal: $NapiF, + pub alpha: $NapiF, + pub zeta: $NapiF, + pub v: $NapiF, + pub u: $NapiF, + pub zeta_chal: $NapiF, + pub v_chal: $NapiF, + pub u_chal: $NapiF, + } + type NapiRandomOracles = []; + + #[napi] + impl [] { + #[napi(constructor)] + #[allow(clippy::too_many_arguments)] + pub fn new( + joint_combiner_chal: Option<$NapiF>, + joint_combiner: Option<$NapiF>, + beta: $NapiF, + gamma: $NapiF, + alpha_chal: $NapiF, + alpha: $NapiF, + zeta: $NapiF, + v: $NapiF, + u: $NapiF, + zeta_chal: $NapiF, + v_chal: $NapiF, + u_chal: $NapiF) -> Self { + Self { + joint_combiner_chal, + joint_combiner, + beta, + gamma, + alpha_chal, + alpha, + zeta, + v, + u, + zeta_chal, + v_chal, + u_chal, + } + } + } + + impl From> for NapiRandomOracles + { + fn from(ro: RandomOracles<$F>) -> Self { + Self { + joint_combiner_chal: ro.joint_combiner.as_ref().map(|x| x.0.0.into()), + joint_combiner: ro.joint_combiner.as_ref().map(|x| x.1.into()), + beta: ro.beta.into(), + gamma: ro.gamma.into(), + alpha_chal: ro.alpha_chal.0.into(), + alpha: ro.alpha.into(), + zeta: ro.zeta.into(), + v: ro.v.into(), + u: ro.u.into(), + zeta_chal: ro.zeta_chal.0.into(), + v_chal: ro.v_chal.0.into(), + u_chal: ro.u_chal.0.into(), + } + } + } + + impl From for RandomOracles<$F> + { + fn from(ro: NapiRandomOracles) -> Self { + Self { + joint_combiner: ro.joint_combiner_chal.and_then(|x| { + ro.joint_combiner.map(|y| (ScalarChallenge(x.into()), y.into())) + }), + beta: ro.beta.into(), + gamma: ro.gamma.into(), + alpha_chal: ScalarChallenge(ro.alpha_chal.into()), + alpha: ro.alpha.into(), + zeta: ro.zeta.into(), + v: ro.v.into(), + u: ro.u.into(), + zeta_chal: ScalarChallenge(ro.zeta_chal.into()), + v_chal: ScalarChallenge(ro.v_chal.into()), + u_chal: ScalarChallenge(ro.u_chal.into()), + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + impl<'a> ToNapiValue for &'a mut [] { + unsafe fn to_napi_value( + env: sys::napi_env, + val: Self, + ) -> Result { + <[] as ToNapiValue>::to_napi_value(env, val.clone()) + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + pub o: [], + pub p_eval0: $NapiF, + pub p_eval1: $NapiF, + #[napi(skip)] + pub opening_prechallenges: NapiFlatVector<$NapiF>, + pub digest_before_evaluations: $NapiF, + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + o: NapiRandomOracles, + p_eval0: $NapiF, + p_eval1: $NapiF, + opening_prechallenges: NapiFlatVector<$NapiF>, + digest_before_evaluations: $NapiF) -> Self { + Self {o, p_eval0, p_eval1, opening_prechallenges, digest_before_evaluations} + } + + #[napi(getter, js_name="opening_prechallenges")] + pub fn opening_prechallenges(&self) -> NapiFlatVector<$NapiF> { + self.opening_prechallenges.clone() + } + + #[napi(setter, js_name="set_opening_prechallenges")] + pub fn set_opening_prechallenges(&mut self, x: NapiFlatVector<$NapiF>) { + self.opening_prechallenges = x; + } + } + + #[napi(js_name = [<$F:snake _oracles_create>])] + pub fn [<$F:snake _oracles_create>]( + lgr_comm: NapiVector<$NapiPolyComm>, // the bases to commit polynomials + index: $index, // parameters + proof: $NapiProverProof, // the final proof (contains public elements at the beginning) + ) -> Result<[]> { + // conversions + let result: Result<(RandomOracles<$F>, [Vec<$F>; 2], NapiFlatVector<$NapiF>, $F), String> = { + let index: DlogVerifierIndex<$G, OpeningProof<$G>> = index.into(); + + let lgr_comm: Vec> = lgr_comm + .into_iter() + .take(proof.public.len()) + .map(Into::into) + .collect(); + let lgr_comm_refs: Vec<_> = lgr_comm.iter().collect(); + + let p_comm = PolyComm::<$G>::multi_scalar_mul( + &lgr_comm_refs, + &proof + .public + .iter() + .map(|a| a.clone().into()) + .map(|s: $F| -s) + .collect::>(), + ); + let p_comm = { + index + .srs() + .mask_custom( + p_comm.clone(), + &p_comm.map(|_| $F::one()), + ) + .unwrap() + .commitment + }; + + let (proof, public_input): (ProverProof<$G, OpeningProof<$G>>, Vec<$F>) = proof.into(); + + let oracles_result = + proof.oracles::< + DefaultFqSponge<$curve_params, PlonkSpongeConstantsKimchi>, + DefaultFrSponge<$F, PlonkSpongeConstantsKimchi> + >(&index, &p_comm, Some(&public_input)); + let oracles_result = match oracles_result { + Err(e) => { + return Err(NapiError::new(Status::GenericFailure, format!("oracles_create: {}", e))); + } + Ok(cs) => cs, + }; + + let (mut sponge, combined_inner_product, p_eval, digest, oracles) = ( + oracles_result.fq_sponge, + oracles_result.combined_inner_product, + oracles_result.public_evals, + oracles_result.digest, + oracles_result.oracles, + ); + + sponge.absorb_fr(&[shift_scalar::<$G>(combined_inner_product)]); + + let opening_prechallenges = proof + .proof + .prechallenges(&mut sponge) + .into_iter() + .map(|x| x.0.into()) + .collect(); + + Ok((oracles, p_eval, opening_prechallenges, digest)) + }; + + match result { + Ok((oracles, p_eval, opening_prechallenges, digest)) => Ok([] { + o: oracles.into(), + p_eval0: p_eval[0][0].into(), + p_eval1: p_eval[1][0].into(), + opening_prechallenges, + digest_before_evaluations: digest.into() + }), + Err(err) => Err(NapiError::new(Status::GenericFailure, err)), + } + } + + #[napi(js_name = [<$F:snake _oracles_dummy>])] + pub fn [<$F:snake _oracles_dummy>]() -> [] { + [] { + o: RandomOracles::<$F>::default().into(), + p_eval0: $F::zero().into(), + p_eval1: $F::zero().into(), + opening_prechallenges: vec![].into(), + digest_before_evaluations: $F::zero().into(), + } + } + + #[napi(js_name = [<$F:snake _oracles_deep_copy>])] + pub fn [<$F:snake _oracles_deep_copy>]( + x: $NapiProverProof, + ) -> $NapiProverProof { + x + } + } + } +} + +pub mod fp { + use super::*; + use crate::{ + plonk_verifier_index::fp::NapiFpPlonkVerifierIndex as WasmPlonkVerifierIndex, + poly_comm::vesta::NapiFpPolyComm as WasmPolyComm, + proof::fp::NapiFpProverProof as WasmProverProof, + }; + use mina_curves::pasta::{Fp, Vesta as GAffine, VestaParameters}; + + impl_oracles!( + NapiPastaFp, + Fp, + WasmGVesta, + GAffine, + WasmPolyComm, + WasmProverProof, + WasmPlonkVerifierIndex, + VestaParameters, + Fp + ); +} + +pub mod fq { + use super::*; + use crate::{ + plonk_verifier_index::fq::NapiFqPlonkVerifierIndex as WasmPlonkVerifierIndex, + poly_comm::pallas::NapiFqPolyComm as WasmPolyComm, + proof::fq::NapiFqProverProof as WasmProverProof, + }; + use mina_curves::pasta::{Fq, Pallas as GAffine, PallasParameters}; + + impl_oracles!( + NapiPastaFq, + Fq, + WasmGPallas, + GAffine, + WasmPolyComm, + WasmProverProof, + WasmPlonkVerifierIndex, + PallasParameters, + Fq + ); +} From ecb68549f116db47fd4a4a086ed0413fee13004a Mon Sep 17 00:00:00 2001 From: querolita Date: Fri, 14 Nov 2025 23:08:17 +0100 Subject: [PATCH 07/45] napi: export proof types --- plonk-napi/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 4d963e00daf..1e4ef05da0f 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -47,12 +47,14 @@ pub use proof::{ fp::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_dummy, - caml_pasta_fp_plonk_proof_verify, NapiFpProverProof, + caml_pasta_fp_plonk_proof_verify, NapiFpLookupCommitments, NapiFpOpeningProof, + NapiFpProofEvaluations, NapiFpProverCommitments, NapiFpProverProof, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_dummy, - caml_pasta_fq_plonk_proof_verify, NapiFqProverProof, + caml_pasta_fq_plonk_proof_verify, NapiFqLookupCommitments, NapiFqOpeningProof, + NapiFqProofEvaluations, NapiFqProverCommitments, NapiFqProverProof, }, }; pub use srs::{ From 31cc9fcbadf8879ac66160991447a774f1fe5253 Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 19 Nov 2025 00:46:01 +0100 Subject: [PATCH 08/45] napi: add missing srs functions --- plonk-napi/src/lib.rs | 5 +++-- plonk-napi/src/srs.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 1e4ef05da0f..94934d64938 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -59,8 +59,9 @@ pub use proof::{ }; pub use srs::{ caml_fp_srs_from_bytes, caml_fp_srs_from_bytes_external, caml_fp_srs_to_bytes, - caml_fq_srs_from_bytes, caml_fq_srs_from_bytes_external, caml_fq_srs_to_bytes, - fp::NapiFpSrs as WasmFpSrs, fq::NapiFqSrs as WasmFqSrs, *, + caml_fp_srs_to_bytes_external, caml_fq_srs_from_bytes, caml_fq_srs_from_bytes_external, + caml_fq_srs_to_bytes, caml_fq_srs_to_bytes_external, fp::NapiFpSrs as WasmFpSrs, + fq::NapiFqSrs as WasmFqSrs, *, }; pub use tables::{JsLookupTableFp, JsLookupTableFq, JsRuntimeTableCfgFp, JsRuntimeTableCfgFq}; pub use vector::{ diff --git a/plonk-napi/src/srs.rs b/plonk-napi/src/srs.rs index dcfe743447a..dd1961f38c7 100644 --- a/plonk-napi/src/srs.rs +++ b/plonk-napi/src/srs.rs @@ -164,6 +164,32 @@ macro_rules! impl_srs { } } + #[napi(js_name = [<"caml_" $name:snake "_srs_lagrange_commitment">])] + pub fn []( + srs: &External<[]>, + domain_size: i32, + i: i32, + ) -> Result<[<$NapiPolyComm>]> { + let x_domain = EvaluationDomain::<$F>::new(domain_size as usize) + .ok_or_else(invalid_domain_error)?; + let basis = srs.get_lagrange_basis(x_domain); + Ok(basis[i as usize].clone().into()) + } + + // Fake overwrite of the plonk_wasm equivalent, but without pointers. + // In the srs bindings, the same symbol will be used to either provide + // the pointer for wasm, or the actual data for napi + #[napi(js_name = [<"caml_" $name:snake "_srs_lagrange_commitments_whole_domain_ptr">])] + pub fn []( + srs: &External<[]>, + domain_size: i32, + ) -> Result> { + let domain = EvaluationDomain::<$F>::new(domain_size as usize) + .ok_or_else(invalid_domain_error)?; + let basis = srs.0.get_lagrange_basis(domain); + Ok(basis.iter().cloned().map(Into::into).collect()) + } + #[napi(js_name = [<"caml_" $name:snake "_srs_get">])] pub fn [](srs: &External<[]>) -> Vec<$NapiG> { println!("Getting SRS with napi"); @@ -322,6 +348,11 @@ pub fn caml_fp_srs_to_bytes(srs: &fp::NapiFpSrs) -> Result { srs.serialize() } +#[napi(js_name = "caml_fp_srs_to_bytes_external")] +pub fn caml_fp_srs_to_bytes_external(srs: &External) -> Uint8Array { + caml_fp_srs_to_bytes(srs).expect("failed to serialize external fp srs") +} + #[napi(js_name = "caml_fp_srs_from_bytes")] pub fn caml_fp_srs_from_bytes(bytes: Uint8Array) -> Result { fp::NapiFpSrs::deserialize(bytes) @@ -338,6 +369,11 @@ pub fn caml_fq_srs_to_bytes(srs: &fq::NapiFqSrs) -> Result { srs.serialize() } +#[napi(js_name = "caml_fq_srs_to_bytes_external")] +pub fn caml_fq_srs_to_bytes_external(srs: &External) -> Uint8Array { + caml_fq_srs_to_bytes(srs).expect("failed to serialize external fq srs") +} + #[napi(js_name = "caml_fq_srs_from_bytes")] pub fn caml_fq_srs_from_bytes(bytes: Uint8Array) -> Result { fq::NapiFqSrs::deserialize(bytes) From 6fcc8cefaee22e5c3a51012f024ceae50304c95e Mon Sep 17 00:00:00 2001 From: querolita Date: Thu, 20 Nov 2025 21:14:24 +0100 Subject: [PATCH 09/45] napi: go back to using externals with srs after testng --- plonk-napi/src/srs.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/plonk-napi/src/srs.rs b/plonk-napi/src/srs.rs index dd1961f38c7..3981d2e0401 100644 --- a/plonk-napi/src/srs.rs +++ b/plonk-napi/src/srs.rs @@ -109,15 +109,15 @@ macro_rules! impl_srs { } #[napi(js_name = [<"caml_" $name:snake "_srs_create">])] - pub fn [](depth: i32) -> External<[]> { + pub fn [](depth: i32) -> [] { println!("Creating SRS with napi"); - External::new(Arc::new(SRS::<$G>::create(depth as usize)).into()) + Arc::new(SRS::<$G>::create(depth as usize)).into() } #[napi(js_name = [<"caml_" $name:snake "_srs_create_parallel">])] - pub fn [](depth: i32) -> External<[]> { + pub fn [](depth: i32) -> [] { println!("Creating SRS in parallel with napi"); - External::new(Arc::new(SRS::<$G>::create_parallel(depth as usize)).into()) + Arc::new(SRS::<$G>::create_parallel(depth as usize)).into() } #[napi(js_name = [<"caml_" $name:snake "_srs_add_lagrange_basis">])] @@ -166,7 +166,7 @@ macro_rules! impl_srs { #[napi(js_name = [<"caml_" $name:snake "_srs_lagrange_commitment">])] pub fn []( - srs: &External<[]>, + srs: &[], domain_size: i32, i: i32, ) -> Result<[<$NapiPolyComm>]> { @@ -199,14 +199,14 @@ macro_rules! impl_srs { } #[napi(js_name = [<"caml_" $name:snake "_srs_set">])] - pub fn [](h_and_gs: Vec<$NapiG>) -> External<[]> { + pub fn [](h_and_gs: Vec<$NapiG>) -> [] { println!("Setting SRS with napi"); let mut h_and_gs: Vec<$G> = h_and_gs.into_iter().map(Into::into).collect(); let h = h_and_gs.remove(0); let g = h_and_gs; let srs = SRS::<$G> { h, g, lagrange_bases: HashMapCache::new() }; - External::new(Arc::new(srs).into()) + Arc::new(srs).into() } #[napi(js_name = [<"caml_" $name:snake "_srs_maybe_lagrange_commitment">])] @@ -249,15 +249,6 @@ macro_rules! impl_srs { Ok(basis.iter().cloned().map(Into::into).collect()) } - #[napi(js_name = [<"caml_" $name:snake "_srs_to_bytes">])] - pub fn [](srs: &[]) -> Result { - srs.serialize() - } - - #[napi(js_name = [<"caml_" $name:snake "_srs_from_bytes">])] - pub fn [](bytes: Uint8Array) -> Result<[]> { - []::deserialize(bytes) - } #[napi(js_name = [<"caml_" $name:snake "_srs_commit_evaluations">])] pub fn [](srs: &[], From 709fad792058c34ac85586ae000a73e4a3a64e34 Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 25 Nov 2025 19:27:37 +0100 Subject: [PATCH 10/45] napi: first version of macro-styled vk --- plonk-napi/src/plonk_verifier_index.rs | 1013 ++++++++++++++++++++++++ 1 file changed, 1013 insertions(+) create mode 100644 plonk-napi/src/plonk_verifier_index.rs diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs new file mode 100644 index 00000000000..ef9c949f010 --- /dev/null +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -0,0 +1,1013 @@ +use crate::wrappers::lookups::NapiLookupInfo; +use ark_ec::AffineRepr; +use ark_ff::One; +use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as Domain}; +use kimchi::{ + circuits::{ + constraints::FeatureFlags, + lookup::{ + index::LookupSelectors, + lookups::{LookupFeatures, LookupInfo, LookupPatterns}, + }, + polynomials::permutation::{ + permutation_vanishing_polynomial, zk_w, Shifts, Shifts as KimchiShifts, + }, + wires::{COLUMNS, PERMUTS}, + }, + linearization::expr_linearization, + verifier_index::{LookupVerifierIndex, VerifierIndex as DlogVerifierIndex, VerifierIndex}, +}; +use napi::bindgen_prelude::{Error, External, Status}; +use napi_derive::napi; +use paste::paste; +use poly_commitment::{ + commitment::PolyComm, + ipa::{OpeningProof, SRS}, + SRS as _, +}; +use serde::{Deserialize, Serialize}; +use std::{path::Path, sync::Arc}; + +macro_rules! impl_verification_key { + ( + $NapiG: ty, + $G: ty, + $NapiF: ty, + $F: ty, + $NapiPolyComm: ty, + $NapiSrs: ty, + $GOther: ty, + $FrSpongeParams: path, + $FqSpongeParams: path, + $NapiIndex: ty, + $field_name: ident + ) => { + paste! { + #[napi(object, js_name = [])] + #[derive(Clone, Debug, Serialize, Deserialize, Default)] + pub struct [] { + pub log_size_of_group: i32, + pub group_gen: $NapiF, + } + type NapiDomain = []; + + impl From for Domain<[<$F>]> { + fn from(domain: NapiDomain) -> Self { + let size = 1 << domain.log_size_of_group; + Domain::<[<$F>]>::new(size).expect("Failed to create evaluation domain") + } + } + + impl From<&Domain<$F>> for NapiDomain { + fn from(domain: &Domain<$F>) -> Self { + Self { + log_size_of_group: domain.log_size_of_group as i32, + group_gen: domain.group_gen.into(), + } + } + } + + #[napi(object, js_name = [])] + #[derive(Clone, Debug, Serialize, Deserialize, Default)] + pub struct [] { + #[napi(skip)] + pub sigma_comm: Vec<$NapiPolyComm>, + #[napi(skip)] + pub coefficients_comm: Vec<$NapiPolyComm>, + #[napi(skip)] + pub generic_comm: $NapiPolyComm, + #[napi(skip)] + pub psm_comm: $NapiPolyComm, + #[napi(skip)] + pub complete_add_comm: $NapiPolyComm, + #[napi(skip)] + pub mul_comm: $NapiPolyComm, + #[napi(skip)] + pub emul_comm: $NapiPolyComm, + #[napi(skip)] + pub endomul_scalar_comm: $NapiPolyComm, + #[napi(skip)] + pub xor_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check0_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check1_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub foreign_field_add_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub foreign_field_mul_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub rot_comm: Option<$NapiPolyComm>, + } + type NapiPlonkVerificationEvals = []; + + impl From<&VerifierIndex<$G, OpeningProof<$G>>> for NapiPlonkVerificationEvals { + fn from(index: &VerifierIndex<$G, OpeningProof<$G>>) -> Self { + Self { + sigma_comm: index.sigma_comm.iter().map(Into::into).collect(), + coefficients_comm: index.coefficients_comm.iter().map(Into::into).collect(), + generic_comm: index.generic_comm.clone().into(), + psm_comm: index.psm_comm.clone().into(), + complete_add_comm: index.complete_add_comm.clone().into(), + mul_comm: index.mul_comm.clone().into(), + emul_comm: index.emul_comm.clone().into(), + endomul_scalar_comm: index.endomul_scalar_comm.clone().into(), + xor_comm: index.xor_comm.clone().map(Into::into), + range_check0_comm: index.range_check0_comm.clone().map(Into::into), + range_check1_comm: index.range_check1_comm.clone().map(Into::into), + foreign_field_add_comm: index.foreign_field_add_comm.clone().map(Into::into), + foreign_field_mul_comm: index.foreign_field_mul_comm.clone().map(Into::into), + rot_comm: index.rot_comm.clone().map(Into::into), + } + } + } + + #[napi] + impl [] { + #[allow(clippy::too_many_arguments)] + #[napi(constructor)] + pub fn new( + sigma_comm: Vec<$NapiPolyComm>, + coefficients_comm: Vec<$NapiPolyComm>, + generic_comm: &$NapiPolyComm, + psm_comm: &$NapiPolyComm, + complete_add_comm: &$NapiPolyComm, + mul_comm: &$NapiPolyComm, + emul_comm: &$NapiPolyComm, + endomul_scalar_comm: &$NapiPolyComm, + xor_comm: Option<$NapiPolyComm>, + range_check0_comm: Option<$NapiPolyComm>, + range_check1_comm: Option<$NapiPolyComm>, + foreign_field_add_comm: Option<$NapiPolyComm>, + foreign_field_mul_comm: Option<$NapiPolyComm>, + rot_comm: Option<$NapiPolyComm>, + ) -> Self { + NapiPlonkVerificationEvals { + sigma_comm: sigma_comm.clone(), + coefficients_comm: coefficients_comm.clone(), + generic_comm: generic_comm.clone(), + psm_comm: psm_comm.clone(), + complete_add_comm: complete_add_comm.clone(), + mul_comm: mul_comm.clone(), + emul_comm: emul_comm.clone(), + endomul_scalar_comm: endomul_scalar_comm.clone(), + xor_comm: xor_comm.clone(), + range_check0_comm: range_check0_comm.clone(), + range_check1_comm: range_check1_comm.clone(), + foreign_field_mul_comm: foreign_field_mul_comm.clone(), + foreign_field_add_comm: foreign_field_add_comm.clone(), + rot_comm: rot_comm.clone(), + } + } + + #[napi(getter)] + pub fn sigma_comm(&self) -> Vec<$NapiPolyComm> { + self.sigma_comm.clone() + } + + #[napi(setter)] + pub fn set_sigma_comm(&mut self, x: Vec<$NapiPolyComm>) { + self.sigma_comm = x; + } + + #[napi(getter)] + pub fn coefficients_comm(&self) -> Vec<$NapiPolyComm> { + self.coefficients_comm.clone() + } + + #[napi(setter)] + pub fn set_coefficients_comm(&mut self, x: Vec<$NapiPolyComm>) { + self.coefficients_comm = x; + } + + #[napi(getter)] + pub fn generic_comm(&self) -> $NapiPolyComm { + self.generic_comm.clone() + } + + #[napi(setter)] + pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { + self.generic_comm = x; + } + + #[napi(getter)] + pub fn psm_comm(&self) -> $NapiPolyComm { + self.psm_comm.clone() + } + + #[napi(setter)] + pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { + self.psm_comm = x; + } + + #[napi(getter)] + pub fn complete_add_comm(&self) -> $NapiPolyComm { + self.complete_add_comm.clone() + } + + #[napi(setter)] + pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { + self.complete_add_comm = x; + } + + #[napi(getter)] + pub fn mul_comm(&self) -> $NapiPolyComm { + self.mul_comm.clone() + } + + #[napi(setter)] + pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { + self.mul_comm = x; + } + + #[napi(getter)] + pub fn emul_comm(&self) -> $NapiPolyComm { + self.emul_comm.clone() + } + + #[napi(setter)] + pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { + self.emul_comm = x; + } + + #[napi(getter)] + pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { + self.endomul_scalar_comm.clone() + } + + #[napi(setter)] + pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { + self.endomul_scalar_comm = x; + } + + #[napi(getter)] + pub fn xor_comm(&self) -> Option<$NapiPolyComm> { + self.xor_comm.clone() + } + + #[napi(setter)] + pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { + self.xor_comm = x; + } + + #[napi(getter)] + pub fn rot_comm(&self) -> Option<$NapiPolyComm> { + self.rot_comm.clone() + } + + #[napi(setter)] + pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { + self.rot_comm = x; + } + + #[napi(getter)] + pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { + self.range_check0_comm.clone() + } + + #[napi(setter)] + pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check0_comm = x; + } + + #[napi(getter)] + pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { + self.range_check1_comm.clone() + } + + #[napi(setter)] + pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check1_comm = x; + } + + #[napi(getter)] + pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_add_comm.clone() + } + + #[napi(setter)] + pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_add_comm = x; + } + + #[napi(getter)] + pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_mul_comm.clone() + } + + #[napi(setter)] + pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_mul_comm = x; + } + + } + + #[derive(Clone, Debug, Serialize, Deserialize, Default)] + #[napi(object, js_name = [])] + pub struct [] { + pub s0: $NapiF, + pub s1: $NapiF, + pub s2: $NapiF, + pub s3: $NapiF, + pub s4: $NapiF, + pub s5: $NapiF, + pub s6: $NapiF, + } + type NapiShifts = []; + + impl From<&[$F; 7]> for NapiShifts { + fn from(shifts: &[$F; 7]) -> Self { + Self { + s0: shifts[0].into(), + s1: shifts[1].into(), + s2: shifts[2].into(), + s3: shifts[3].into(), + s4: shifts[4].into(), + s5: shifts[5].into(), + s6: shifts[6].into(), + } + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + s0: $NapiF, + s1: $NapiF, + s2: $NapiF, + s3: $NapiF, + s4: $NapiF, + s5: $NapiF, + s6: $NapiF, + ) -> Self { + Self { s0, s1, s2, s3, s4, s5, s6} + } + } + + #[derive(Clone, Debug, Serialize, Deserialize, Default)] + #[napi(object, js_name = [])] + pub struct [] { + #[napi(skip)] + pub xor: Option<$NapiPolyComm>, + #[napi(skip)] + pub lookup: Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check: Option<$NapiPolyComm>, + #[napi(skip)] + pub ffmul: Option<$NapiPolyComm>, + } + type NapiLookupSelectors = []; + + impl From for LookupSelectors> { + fn from(x: NapiLookupSelectors) -> Self { + Self { + xor: x.xor.map(Into::into), + lookup: x.lookup.map(Into::into), + range_check: x.range_check.map(Into::into), + ffmul: x.ffmul.map(Into::into), + } + } + } + + impl From<&NapiLookupSelectors> for LookupSelectors> { + fn from(x: &NapiLookupSelectors) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + impl From<&LookupSelectors>> for NapiLookupSelectors { + fn from(x: &LookupSelectors>) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + impl From>> for NapiLookupSelectors { + fn from(x: LookupSelectors>) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + xor: Option<$NapiPolyComm>, + lookup: Option<$NapiPolyComm>, + range_check: Option<$NapiPolyComm>, + ffmul: Option<$NapiPolyComm> + ) -> Self { + Self { + xor, + lookup, + range_check, + ffmul + } + } + + #[napi(getter)] + pub fn xor(&self) -> Option<$NapiPolyComm> { + self.xor.clone() + } + + #[napi(setter)] + pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { + self.xor = x + } + + #[napi(getter)] + pub fn lookup(&self) -> Option<$NapiPolyComm> { + self.lookup.clone() + } + + #[napi(setter)] + pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { + self.lookup = x + } + + #[napi(getter)] + pub fn ffmul(&self) -> Option<$NapiPolyComm> { + self.ffmul.clone() + } + + #[napi(setter)] + pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { + self.ffmul = x + } + + #[napi(getter)] + pub fn range_check(&self) -> Option<$NapiPolyComm> { + self.range_check.clone() + } + + #[napi(setter)] + pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { + self.range_check = x + } + } + + #[napi(object, js_name = [])] + #[derive(Clone, Debug, Serialize, Deserialize, Default)] + pub struct [] { + pub joint_lookup_used: bool, + + #[napi(skip)] + pub lookup_table: Vec<$NapiPolyComm>, + + #[napi(skip)] + pub lookup_selectors: NapiLookupSelectors, + + #[napi(skip)] + pub table_ids: Option<$NapiPolyComm>, + + #[napi(skip)] + pub lookup_info: NapiLookupInfo, + + #[napi(skip)] + pub runtime_tables_selector: Option<$NapiPolyComm>, + } + type NapiLookupVerifierIndex = []; + + impl From<&LookupVerifierIndex<$G>> for NapiLookupVerifierIndex { + fn from(x: &LookupVerifierIndex<$G>) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.clone().iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.clone().into(), + table_ids: x.table_ids.clone().map(Into::into), + lookup_info: x.lookup_info.clone().into(), + runtime_tables_selector: x.runtime_tables_selector.clone().map(Into::into) + } + } + } + + impl From> for NapiLookupVerifierIndex { + fn from(x: LookupVerifierIndex<$G>) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.into(), + table_ids: x.table_ids.map(Into::into), + lookup_info: x.lookup_info.into(), + runtime_tables_selector: x.runtime_tables_selector.map(Into::into) + } + } + } + + + impl From<&NapiLookupVerifierIndex> for LookupVerifierIndex<$G> { + fn from(x: &NapiLookupVerifierIndex) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.clone().iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.clone().into(), + table_ids: x.table_ids.clone().map(Into::into), + lookup_info: x.lookup_info.clone().into(), + runtime_tables_selector: x.runtime_tables_selector.clone().map(Into::into) + } + } + } + + impl From for LookupVerifierIndex<$G> { + fn from(x: NapiLookupVerifierIndex) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.into(), + table_ids: x.table_ids.map(Into::into), + lookup_info: x.lookup_info.clone().into(), + runtime_tables_selector: x.runtime_tables_selector.map(Into::into) + } + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + joint_lookup_used: bool, + lookup_table: Vec<$NapiPolyComm>, + lookup_selectors: NapiLookupSelectors, + table_ids: Option<$NapiPolyComm>, + lookup_info: NapiLookupInfo, + runtime_tables_selector: Option<$NapiPolyComm> + ) -> NapiLookupVerifierIndex { + NapiLookupVerifierIndex { + joint_lookup_used, + lookup_table, + lookup_selectors, + table_ids, + lookup_info: lookup_info.clone(), + runtime_tables_selector + } + } + + #[napi(getter)] + pub fn lookup_table(&self) -> Vec<$NapiPolyComm> { + self.lookup_table.clone() + } + + #[napi(setter)] + pub fn set_lookup_table(&mut self, x: Vec<$NapiPolyComm>) { + self.lookup_table = x + } + + #[napi(getter)] + pub fn lookup_selectors(&self) -> NapiLookupSelectors { + self.lookup_selectors.clone() + } + + #[napi(setter)] + pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { + self.lookup_selectors = x + } + + #[napi(getter)] + pub fn table_ids(&self) -> Option<$NapiPolyComm>{ + self.table_ids.clone() + } + + #[napi(setter)] + pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { + self.table_ids = x + } + + #[napi(getter)] + pub fn lookup_info(&self) -> NapiLookupInfo { + self.lookup_info.clone() + } + + #[napi(setter)] + pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { + self.lookup_info = x + } + + #[napi(getter)] + pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { + self.runtime_tables_selector.clone() + } + + #[napi(setter)] + pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { + self.runtime_tables_selector = x + } + } + + #[napi(object, js_name = [])] + #[derive(Clone, Debug, Default)] + pub struct [] { + pub domain: NapiDomain, + pub max_poly_size: i32, + pub public_: i32, + pub prev_challenges: i32, + #[napi(skip)] + pub srs: $NapiSrs, + #[napi(skip)] + pub evals: NapiPlonkVerificationEvals, + pub shifts: NapiShifts, + #[napi(skip)] + pub lookup_index: Option, + pub zk_rows: i32, + } + type NapiPlonkVerifierIndex = []; + + #[napi] + impl [] { + #[napi(constructor)] + #[allow(clippy::too_many_arguments)] + pub fn new( + domain: NapiDomain, + max_poly_size: i32, + public_: i32, + prev_challenges: i32, + srs: &$NapiSrs, + evals: NapiPlonkVerificationEvals, + shifts: NapiShifts, + lookup_index: Option, + zk_rows: i32, + ) -> Self { + NapiPlonkVerifierIndex { + domain: domain.clone(), + max_poly_size, + public_, + prev_challenges, + srs: srs.clone(), + evals: evals.clone(), + shifts: shifts.clone(), + lookup_index: lookup_index.cloned(), + zk_rows, + } + } + + #[napi(getter)] + pub fn srs(&self) -> $NapiSrs { + self.srs.clone() + } + + #[napi(setter)] + pub fn set_srs(&mut self, x: $NapiSrs) { + self.srs = x + } + + #[napi(getter)] + pub fn evals(&self) -> NapiPlonkVerificationEvals { + self.evals.clone() + } + + #[napi(setter)] + pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { + self.evals = x + } + + #[napi(getter)] + pub fn lookup_index(&self) -> Option { + self.lookup_index.clone() + } + + #[napi(setter)] + pub fn set_lookup_index(&mut self, li: Option) { + self.lookup_index = li + } + } + + fn compute_feature_flags(index: &NapiPlonkVerifierIndex) -> FeatureFlags { + let xor = index.evals.xor_comm.is_some(); + let range_check0 = index.evals.range_check0_comm.is_some(); + let range_check1 = index.evals.range_check1_comm.is_some(); + let foreign_field_add = index.evals.foreign_field_add_comm.is_some(); + let foreign_field_mul = index.evals.foreign_field_mul_comm.is_some(); + let rot = index.evals.rot_comm.is_some(); + + let lookup = index + .lookup_index.as_ref() + .map_or(false, |li| li.lookup_info.features.patterns.lookup); + + let runtime_tables = index + .lookup_index.as_ref() + .map_or(false, |li| li.runtime_tables_selector.is_some()); + + let patterns = LookupPatterns { + xor, + lookup, + range_check: range_check0 || range_check1 || rot, + foreign_field_mul, + }; + + FeatureFlags { + range_check0, + range_check1, + foreign_field_add, + foreign_field_mul, + xor, + rot, + lookup_features: LookupFeatures { + patterns, + joint_lookup_used: patterns.joint_lookups_used(), + uses_runtime_tables: runtime_tables, + }, + } + } + + impl From for VerifierIndex<$G, OpeningProof<$G>> { + fn from(index: NapiPlonkVerifierIndex) -> Self { + let of_napi = + { + let max_poly_size = index.max_poly_size; + let public_ = index.public_; + let prev_challenges = index.prev_challenges; + let log_size_of_group = index.domain.log_size_of_group; + let srs = &index.srs; + let evals = &index.evals; + let shifts = &index.shifts; + + let (endo_q, _endo_r) = poly_commitment::ipa::endos::<$GOther>(); + let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap(); + + let feature_flags = compute_feature_flags(&index); + let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true); + + let index = { + let zk_rows = index.zk_rows as u64; + + VerifierIndex { + domain, + + sigma_comm: core::array::from_fn(|i| (&evals.sigma_comm[i]).into()), + generic_comm: (&evals.generic_comm).into(), + coefficients_comm: core::array::from_fn(|i| (&evals.coefficients_comm[i]).into()), + + psm_comm: (&evals.psm_comm).into(), + + complete_add_comm: (&evals.complete_add_comm).into(), + mul_comm: (&evals.mul_comm).into(), + emul_comm: (&evals.emul_comm).into(), + + endomul_scalar_comm: (&evals.endomul_scalar_comm).into(), + xor_comm: (&evals.xor_comm).as_ref().map(Into::into), + range_check0_comm: (&evals.range_check0_comm).as_ref().map(Into::into), + range_check1_comm: (&evals.range_check1_comm).as_ref().map(Into::into), + foreign_field_add_comm: (&evals.foreign_field_add_comm).as_ref().map(Into::into), + foreign_field_mul_comm: (&evals.foreign_field_mul_comm).as_ref().map(Into::into), + rot_comm: (&evals.rot_comm).as_ref().map(Into::into), + + w: { + let res = once_cell::sync::OnceCell::new(); + res.set(zk_w(domain, zk_rows)).unwrap(); + res + }, + endo: endo_q, + max_poly_size: max_poly_size as usize, + public: public_ as usize, + prev_challenges: prev_challenges as usize, + permutation_vanishing_polynomial_m: { + let res = once_cell::sync::OnceCell::new(); + res.set(permutation_vanishing_polynomial(domain, zk_rows)).unwrap(); + res + }, + shift: [ + shifts.s0.into(), + shifts.s1.into(), + shifts.s2.into(), + shifts.s3.into(), + shifts.s4.into(), + shifts.s5.into(), + shifts.s6.into() + ], + srs: { + Arc::clone(&srs.0) + }, + + zk_rows, + + linearization, + powers_of_alpha, + lookup_index: index.lookup_index.map(Into::into), + } + }; + (index, srs.0.clone()) + }; + return of_napi.0; + } + } + + pub fn read_raw( + offset: Option, + srs: &$NapiSrs, + path: String, + ) -> Result>, String> { + let path = Path::new(&path); + let (endo_q, _endo_r) = poly_commitment::ipa::endos::<$GOther>(); + VerifierIndex::<$G, OpeningProof<$G>>::from_file( + srs.0.clone(), + path, + offset.map(|x| x as u64), + endo_q, + ).map_err(|e| format!("read_raw: {}", e).as_str()) + } + + #[napi(js_name = [])] + pub fn []( + offset: Option, + srs: &$NapiSrs, + path: String, + ) -> napi::bindgen_prelude::Result { + let vi = read_raw(offset, srs, path)?; + Ok(NapiPlonkVerifierIndex::from(vi)) + } + + #[napi(js_name = [])] + pub fn []( + append: Option, + index: NapiPlonkVerifierIndex, + path: String, + ) -> napi::bindgen_prelude::Result<(), String> { + let index: VerifierIndex<$G, OpeningProof<$G>> = index.into(); + let path = Path::new(&path); + index.to_file(path, append).map_err(|e| { + println!("{}", e); + "caml_pasta_plonk_verifier_index_write" + }) + } + + #[napi(js_name = [])] + pub fn []( + index: NapiPlonkVerifierIndex, + ) -> String { + let index: VerifierIndex<$G, OpeningProof<$G>> = index.into(); + serde_json::to_string(&index).unwrap() + } + + #[napi(js_name = [])] + pub fn []( + srs: &$NapiSrs, + index: String, + ) -> napi::bindgen_prelude::Result { + let vi: Result>, serde_json::Error> = serde_json::from_str(&index); + match vi { + Ok(vi) => Ok(to_napi(srs, vi)), + Err(e) => Err(e.to_string()), + } + } + + #[napi(js_name = [])] + pub fn []( + index: &External<$NapiIndex>, + ) -> NapiPlonkVerifierIndex { + index.0.srs.get_lagrange_basis(index.0.as_ref().cs.domain.d1); + let verifier_index = index.0.as_ref().verifier_index(); + NapiPlonkVerifierIndex::from(&verifier_index) + } + + #[napi(js_name = [])] + pub fn [](log2_size: i32) -> napi::bindgen_prelude::Result { + println!( + "from napi! caml_pasta_plonk_verifier_index_shifts with log2_size {}", + log2_size + ); + + let size = 1usize << (log2_size as u32); + let domain = Domain::<$F>::new(size) + .ok_or_else(|| Error::new(Status::InvalidArg, "failed to create evaluation domain"))?; + + let shifts = KimchiShifts::new(&domain); + let s = shifts.shifts(); + + Ok(NapiShifts { + s0: s[0].clone().into(), + s1: s[1].clone().into(), + s2: s[2].clone().into(), + s3: s[3].clone().into(), + s4: s[4].clone().into(), + s5: s[5].clone().into(), + s6: s[6].clone().into(), + }) + } + + #[napi(js_name = [])] + pub fn []() -> NapiPlonkVerifierIndex { + fn comm() -> $NapiPolyComm { + let g: $NapiG = $G::generator().into(); + $NapiPolyComm { + shifted: None, + unshifted: vec![g].into(), + } + } + fn vec_comm(num: usize) -> Vec<$NapiPolyComm> { + (0..num).map(|_| comm()).collect() + } + + NapiPlonkVerifierIndex { + domain: NapiDomain { + log_size_of_group: 1, + group_gen: $F::one().into(), + }, + max_poly_size: 0, + public_: 0, + prev_challenges: 0, + srs: $NapiSrs(Arc::new(SRS::create(0))), + evals: NapiPlonkVerificationEvals { + sigma_comm: vec_comm(PERMUTS), + coefficients_comm: vec_comm(COLUMNS), + generic_comm: comm(), + psm_comm: comm(), + complete_add_comm: comm(), + mul_comm: comm(), + emul_comm: comm(), + endomul_scalar_comm: comm(), + xor_comm: None, + range_check0_comm: None, + range_check1_comm: None, + foreign_field_add_comm: None, + foreign_field_mul_comm: None, + rot_comm: None, + }, + shifts: + NapiShifts { + s0: $F::one().into(), + s1: $F::one().into(), + s2: $F::one().into(), + s3: $F::one().into(), + s4: $F::one().into(), + s5: $F::one().into(), + s6: $F::one().into(), + }, + lookup_index: None, + zk_rows: 3, + } + } + + #[napi(js_name = [])] + pub fn []( + x: &NapiPlonkVerifierIndex, + ) -> NapiPlonkVerifierIndex { + x.clone() + } + + } + } +} + +pub mod fp { + use super::*; + use crate::{ + pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, + poly_comm::vesta::NapiFpPolyComm as NapiPolyComm, + srs::fp::NapiFpSrs, + wrappers::{field::NapiPastaFp, group::NapiGVesta}, + }; + use mina_curves::pasta::{Fp, Pallas as GAffineOther, Vesta as GAffine}; + + impl_verification_key!( + NapiGVesta, + GAffine, + NapiPastaFp, + Fp, + NapiPolyComm, + NapiFpSrs, + GAffineOther, + mina_poseidon::pasta::fp_kimchi, + mina_poseidon::pasta::fq_kimchi, + NapiPastaFpPlonkIndex, + fp + ); +} + +pub mod fq { + use super::*; + use crate::{ + pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, + poly_comm::pallas::NapiFqPolyComm as NapiPolyComm, + srs::fq::NapiFqSrs, + wrappers::{field::NapiPastaFq, group::NapiGPallas}, + }; + use mina_curves::pasta::{Fq, Pallas as GAffine, Vesta as GAffineOther}; + + impl_verification_key!( + NapiGPallas, + GAffine, + NapiPastaFq, + Fq, + NapiPolyComm, + NapiFqSrs, + GAffineOther, + mina_poseidon::pasta::fq_kimchi, + mina_poseidon::pasta::fp_kimchi, + NapiPastaFqPlonkIndex, + fq + ); +} From 1ae987ef343ce5924c74304958cf291017388b6b Mon Sep 17 00:00:00 2001 From: querolita Date: Tue, 25 Nov 2025 22:51:23 +0100 Subject: [PATCH 11/45] napi: fix compilation of verifier index macro implementation --- plonk-napi/src/lib.rs | 16 +- .../fp.rs | 87 ++- .../fq.rs | 84 ++- .../mod.rs | 0 plonk-napi/src/plonk_verifier_index.rs | 581 ++++-------------- plonk-napi/src/proof.rs | 4 +- 6 files changed, 285 insertions(+), 487 deletions(-) rename plonk-napi/src/{plonk_verifier_index => old_plonk_verifier_index}/fp.rs (79%) rename plonk-napi/src/{plonk_verifier_index => old_plonk_verifier_index}/fq.rs (79%) rename plonk-napi/src/{plonk_verifier_index => old_plonk_verifier_index}/mod.rs (100%) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index c7be89aa28c..3e104b816d7 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -41,8 +41,20 @@ pub use pasta_fp_plonk_index::{ pub use pasta_fq_plonk_index::{ prover_index_fq_from_bytes, prover_index_fq_to_bytes, WasmPastaFqPlonkIndex, }; +// pub use plonk_verifier_index::{fp::*, fq::*}; pub use plonk_verifier_index::{ - caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_shifts, + fp::{ + caml_pasta_fp_plonk_verifier_index_create, caml_pasta_fp_plonk_verifier_index_deep_copy, + caml_pasta_fp_plonk_verifier_index_deserialize, caml_pasta_fp_plonk_verifier_index_dummy, + caml_pasta_fp_plonk_verifier_index_read, caml_pasta_fp_plonk_verifier_index_serialize, + caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, + }, + fq::{ + caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, + caml_pasta_fq_plonk_verifier_index_deserialize, caml_pasta_fq_plonk_verifier_index_dummy, + caml_pasta_fq_plonk_verifier_index_read, caml_pasta_fq_plonk_verifier_index_serialize, + caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, + }, }; pub use poly_comm::{ pallas::NapiFqPolyComm as WasmFqPolyComm, vesta::NapiFpPolyComm as WasmFpPolyComm, @@ -76,5 +88,3 @@ pub use wrappers::{ field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq}, group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta}, }; - -pub use plonk_verifier_index::{fp::*, fq::*}; diff --git a/plonk-napi/src/plonk_verifier_index/fp.rs b/plonk-napi/src/old_plonk_verifier_index/fp.rs similarity index 79% rename from plonk-napi/src/plonk_verifier_index/fp.rs rename to plonk-napi/src/old_plonk_verifier_index/fp.rs index a39acbad910..356892558ab 100644 --- a/plonk-napi/src/plonk_verifier_index/fp.rs +++ b/plonk-napi/src/old_plonk_verifier_index/fp.rs @@ -2,6 +2,7 @@ use crate::{ poly_comm::vesta::NapiFpPolyComm, srs::fp::NapiFpSrs, wrappers::{field::NapiPastaFp, lookups::NapiLookupInfo}, + WasmPastaFpPlonkIndex, }; use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as Domain}; use kimchi::{ @@ -19,9 +20,9 @@ use kimchi::{ verifier_index::{LookupVerifierIndex, VerifierIndex}, }; use mina_curves::pasta::{Fp, Pallas as GAffineOther, Vesta as GAffine}; -use napi::bindgen_prelude::{Error, Status}; +use napi::bindgen_prelude::{Error, External, Status}; use napi_derive::napi; -use poly_commitment::{commitment::PolyComm, ipa::OpeningProof}; +use poly_commitment::{commitment::PolyComm, ipa::OpeningProof, SRS}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -39,6 +40,15 @@ impl From for Domain { } } +impl From<&Domain> for NapiFpDomain { + fn from(domain: &Domain) -> Self { + Self { + log_size_of_group: domain.log_size_of_group as i32, + group_gen: domain.group_gen.into(), + } + } +} + #[napi(object, js_name = "WasmFpShifts")] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NapiFpShifts { @@ -51,6 +61,20 @@ pub struct NapiFpShifts { pub s6: NapiPastaFp, } +impl From<&[Fp; 7]> for NapiFpShifts { + fn from(shifts: &[Fp; 7]) -> Self { + Self { + s0: shifts[0].into(), + s1: shifts[1].into(), + s2: shifts[2].into(), + s3: shifts[3].into(), + s4: shifts[4].into(), + s5: shifts[5].into(), + s6: shifts[6].into(), + } + } +} + #[napi(object, js_name = "WasmFpLookupSelectors")] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NapiFpLookupSelectors { @@ -186,6 +210,27 @@ pub struct NapiFpPlonkVerificationEvals { pub rot_comm: Option, } +impl From<&VerifierIndex>> for NapiFpPlonkVerificationEvals { + fn from(index: &VerifierIndex>) -> Self { + Self { + sigma_comm: index.sigma_comm.iter().map(Into::into).collect(), + coefficients_comm: index.coefficients_comm.iter().map(Into::into).collect(), + generic_comm: index.generic_comm.clone().into(), + psm_comm: index.psm_comm.clone().into(), + complete_add_comm: index.complete_add_comm.clone().into(), + mul_comm: index.mul_comm.clone().into(), + emul_comm: index.emul_comm.clone().into(), + endomul_scalar_comm: index.endomul_scalar_comm.clone().into(), + xor_comm: index.xor_comm.clone().map(Into::into), + range_check0_comm: index.range_check0_comm.clone().map(Into::into), + range_check1_comm: index.range_check1_comm.clone().map(Into::into), + foreign_field_add_comm: index.foreign_field_add_comm.clone().map(Into::into), + foreign_field_mul_comm: index.foreign_field_mul_comm.clone().map(Into::into), + rot_comm: index.rot_comm.clone().map(Into::into), + } + } +} + #[napi(object, js_name = "WasmFpPlonkVerifierIndex")] #[derive(Clone, Debug, Default)] pub struct NapiFpPlonkVerifierIndex { @@ -200,6 +245,28 @@ pub struct NapiFpPlonkVerifierIndex { pub zk_rows: i32, } +#[napi(js_name = "caml_pasta_fp_plonk_verifier_index_create")] +pub fn caml_pasta_fp_plonk_verifier_index_create( + index: &External, +) -> NapiFpPlonkVerifierIndex { + index + .0 + .srs + .get_lagrange_basis(index.0.as_ref().cs.domain.d1); + let verifier_index = index.0.as_ref().verifier_index(); + NapiFpPlonkVerifierIndex::from(&verifier_index) +} + +#[napi(js_name = "caml_pasta_fp_plonk_verifier_index_read")] +pub fn caml_pasta_fp_plonk_verifier_index_read( + offset: Option, + srs: &External, + path: String, +) -> NapiFpPlonkVerifierIndex { + let vi = read_raw(offset, srs, path)?; + Ok(to_wasm(srs, vi.into())) +} + #[napi(js_name = "caml_pasta_fp_plonk_verifier_index_shifts")] pub fn caml_pasta_fp_plonk_verifier_index_shifts( log2_size: i32, @@ -304,6 +371,22 @@ impl From for VerifierIndex>> for NapiFpPlonkVerifierIndex { + fn from(index: &VerifierIndex>) -> Self { + Self { + domain: (&index.domain).into(), + max_poly_size: index.max_poly_size as i32, + public_: index.public as i32, + prev_challenges: index.prev_challenges as i32, + srs: (&index.srs).into(), + evals: index.into(), + shifts: (&index.shift).into(), + lookup_index: index.lookup_index.as_ref().map(Into::into), + zk_rows: index.zk_rows as i32, + } + } +} + fn compute_feature_flags(index: &NapiFpPlonkVerifierIndex) -> FeatureFlags { let xor = index.evals.xor_comm.is_some(); let range_check0 = index.evals.range_check0_comm.is_some(); diff --git a/plonk-napi/src/plonk_verifier_index/fq.rs b/plonk-napi/src/old_plonk_verifier_index/fq.rs similarity index 79% rename from plonk-napi/src/plonk_verifier_index/fq.rs rename to plonk-napi/src/old_plonk_verifier_index/fq.rs index fe1c3b51137..222f71e4763 100644 --- a/plonk-napi/src/plonk_verifier_index/fq.rs +++ b/plonk-napi/src/old_plonk_verifier_index/fq.rs @@ -2,6 +2,7 @@ use crate::{ poly_comm::pallas::NapiFqPolyComm, srs::fq::NapiFqSrs, wrappers::{field::NapiPastaFq, lookups::NapiLookupInfo}, + WasmPastaFqPlonkIndex, }; use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as Domain}; use kimchi::{ @@ -19,9 +20,9 @@ use kimchi::{ verifier_index::{LookupVerifierIndex, VerifierIndex}, }; use mina_curves::pasta::{Fq, Pallas as GAffine, Vesta as GAffineOther}; -use napi::bindgen_prelude::{Error, Status}; +use napi::bindgen_prelude::{Error, External, Status}; use napi_derive::napi; -use poly_commitment::{commitment::PolyComm, ipa::OpeningProof}; +use poly_commitment::{commitment::PolyComm, ipa::OpeningProof, SRS}; use serde::{Deserialize, Serialize}; use std::sync::Arc; @@ -32,6 +33,22 @@ pub struct NapiFqDomain { pub group_gen: NapiPastaFq, } +impl From for Domain { + fn from(domain: NapiFqDomain) -> Self { + let size = 1 << domain.log_size_of_group; + Domain::::new(size).expect("Failed to create evaluation domain") + } +} + +impl From<&Domain> for NapiFqDomain { + fn from(domain: &Domain) -> Self { + Self { + log_size_of_group: domain.log_size_of_group as i32, + group_gen: domain.group_gen.into(), + } + } +} + #[napi(object, js_name = "WasmFqShifts")] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NapiFqShifts { @@ -44,6 +61,20 @@ pub struct NapiFqShifts { pub s6: NapiPastaFq, } +impl From<&[Fq; 7]> for NapiFqShifts { + fn from(shifts: &[Fq; 7]) -> Self { + Self { + s0: shifts[0].into(), + s1: shifts[1].into(), + s2: shifts[2].into(), + s3: shifts[3].into(), + s4: shifts[4].into(), + s5: shifts[5].into(), + s6: shifts[6].into(), + } + } +} + #[napi(object, js_name = "WasmFqLookupSelectors")] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct NapiFqLookupSelectors { @@ -179,6 +210,27 @@ pub struct NapiFqPlonkVerificationEvals { pub rot_comm: Option, } +impl From<&VerifierIndex>> for NapiFqPlonkVerificationEvals { + fn from(index: &VerifierIndex>) -> Self { + Self { + sigma_comm: index.sigma_comm.iter().map(Into::into).collect(), + coefficients_comm: index.coefficients_comm.iter().map(Into::into).collect(), + generic_comm: index.generic_comm.clone().into(), + psm_comm: index.psm_comm.clone().into(), + complete_add_comm: index.complete_add_comm.clone().into(), + mul_comm: index.mul_comm.clone().into(), + emul_comm: index.emul_comm.clone().into(), + endomul_scalar_comm: index.endomul_scalar_comm.clone().into(), + xor_comm: index.xor_comm.clone().map(Into::into), + range_check0_comm: index.range_check0_comm.clone().map(Into::into), + range_check1_comm: index.range_check1_comm.clone().map(Into::into), + foreign_field_add_comm: index.foreign_field_add_comm.clone().map(Into::into), + foreign_field_mul_comm: index.foreign_field_mul_comm.clone().map(Into::into), + rot_comm: index.rot_comm.clone().map(Into::into), + } + } +} + #[napi(object, js_name = "WasmFqPlonkVerifierIndex")] #[derive(Clone, Debug, Default)] pub struct NapiFqPlonkVerifierIndex { @@ -193,6 +245,18 @@ pub struct NapiFqPlonkVerifierIndex { pub zk_rows: i32, } +#[napi(js_name = "caml_pasta_fq_plonk_verifier_index_create")] +pub fn caml_pasta_fq_plonk_verifier_index_create( + index: &External, +) -> NapiFqPlonkVerifierIndex { + index + .0 + .srs + .get_lagrange_basis(index.0.as_ref().cs.domain.d1); + let verifier_index = index.0.as_ref().verifier_index(); + NapiFqPlonkVerifierIndex::from(&verifier_index) +} + #[napi(js_name = "caml_pasta_fq_plonk_verifier_index_shifts")] pub fn caml_pasta_fq_plonk_verifier_index_shifts( log2_size: i32, @@ -297,6 +361,22 @@ impl From for VerifierIndex>> for NapiFqPlonkVerifierIndex { + fn from(index: &VerifierIndex>) -> Self { + Self { + domain: (&index.domain).into(), + max_poly_size: index.max_poly_size as i32, + public_: index.public as i32, + prev_challenges: index.prev_challenges as i32, + srs: (&index.srs).into(), + evals: index.into(), + shifts: (&index.shift).into(), + lookup_index: index.lookup_index.as_ref().map(Into::into), + zk_rows: index.zk_rows as i32, + } + } +} + fn compute_feature_flags(index: &NapiFqPlonkVerifierIndex) -> FeatureFlags { let xor = index.evals.xor_comm.is_some(); let range_check0 = index.evals.range_check0_comm.is_some(); diff --git a/plonk-napi/src/plonk_verifier_index/mod.rs b/plonk-napi/src/old_plonk_verifier_index/mod.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index/mod.rs rename to plonk-napi/src/old_plonk_verifier_index/mod.rs diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index ef9c949f010..b1ae1cf8337 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -7,15 +7,15 @@ use kimchi::{ constraints::FeatureFlags, lookup::{ index::LookupSelectors, - lookups::{LookupFeatures, LookupInfo, LookupPatterns}, + lookups::{LookupFeatures, LookupPatterns}, }, polynomials::permutation::{ - permutation_vanishing_polynomial, zk_w, Shifts, Shifts as KimchiShifts, + permutation_vanishing_polynomial, zk_w, Shifts as KimchiShifts, }, wires::{COLUMNS, PERMUTS}, }, linearization::expr_linearization, - verifier_index::{LookupVerifierIndex, VerifierIndex as DlogVerifierIndex, VerifierIndex}, + verifier_index::{LookupVerifierIndex, VerifierIndex}, }; use napi::bindgen_prelude::{Error, External, Status}; use napi_derive::napi; @@ -122,186 +122,6 @@ macro_rules! impl_verification_key { } } - #[napi] - impl [] { - #[allow(clippy::too_many_arguments)] - #[napi(constructor)] - pub fn new( - sigma_comm: Vec<$NapiPolyComm>, - coefficients_comm: Vec<$NapiPolyComm>, - generic_comm: &$NapiPolyComm, - psm_comm: &$NapiPolyComm, - complete_add_comm: &$NapiPolyComm, - mul_comm: &$NapiPolyComm, - emul_comm: &$NapiPolyComm, - endomul_scalar_comm: &$NapiPolyComm, - xor_comm: Option<$NapiPolyComm>, - range_check0_comm: Option<$NapiPolyComm>, - range_check1_comm: Option<$NapiPolyComm>, - foreign_field_add_comm: Option<$NapiPolyComm>, - foreign_field_mul_comm: Option<$NapiPolyComm>, - rot_comm: Option<$NapiPolyComm>, - ) -> Self { - NapiPlonkVerificationEvals { - sigma_comm: sigma_comm.clone(), - coefficients_comm: coefficients_comm.clone(), - generic_comm: generic_comm.clone(), - psm_comm: psm_comm.clone(), - complete_add_comm: complete_add_comm.clone(), - mul_comm: mul_comm.clone(), - emul_comm: emul_comm.clone(), - endomul_scalar_comm: endomul_scalar_comm.clone(), - xor_comm: xor_comm.clone(), - range_check0_comm: range_check0_comm.clone(), - range_check1_comm: range_check1_comm.clone(), - foreign_field_mul_comm: foreign_field_mul_comm.clone(), - foreign_field_add_comm: foreign_field_add_comm.clone(), - rot_comm: rot_comm.clone(), - } - } - - #[napi(getter)] - pub fn sigma_comm(&self) -> Vec<$NapiPolyComm> { - self.sigma_comm.clone() - } - - #[napi(setter)] - pub fn set_sigma_comm(&mut self, x: Vec<$NapiPolyComm>) { - self.sigma_comm = x; - } - - #[napi(getter)] - pub fn coefficients_comm(&self) -> Vec<$NapiPolyComm> { - self.coefficients_comm.clone() - } - - #[napi(setter)] - pub fn set_coefficients_comm(&mut self, x: Vec<$NapiPolyComm>) { - self.coefficients_comm = x; - } - - #[napi(getter)] - pub fn generic_comm(&self) -> $NapiPolyComm { - self.generic_comm.clone() - } - - #[napi(setter)] - pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { - self.generic_comm = x; - } - - #[napi(getter)] - pub fn psm_comm(&self) -> $NapiPolyComm { - self.psm_comm.clone() - } - - #[napi(setter)] - pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { - self.psm_comm = x; - } - - #[napi(getter)] - pub fn complete_add_comm(&self) -> $NapiPolyComm { - self.complete_add_comm.clone() - } - - #[napi(setter)] - pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { - self.complete_add_comm = x; - } - - #[napi(getter)] - pub fn mul_comm(&self) -> $NapiPolyComm { - self.mul_comm.clone() - } - - #[napi(setter)] - pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { - self.mul_comm = x; - } - - #[napi(getter)] - pub fn emul_comm(&self) -> $NapiPolyComm { - self.emul_comm.clone() - } - - #[napi(setter)] - pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { - self.emul_comm = x; - } - - #[napi(getter)] - pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { - self.endomul_scalar_comm.clone() - } - - #[napi(setter)] - pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { - self.endomul_scalar_comm = x; - } - - #[napi(getter)] - pub fn xor_comm(&self) -> Option<$NapiPolyComm> { - self.xor_comm.clone() - } - - #[napi(setter)] - pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { - self.xor_comm = x; - } - - #[napi(getter)] - pub fn rot_comm(&self) -> Option<$NapiPolyComm> { - self.rot_comm.clone() - } - - #[napi(setter)] - pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { - self.rot_comm = x; - } - - #[napi(getter)] - pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { - self.range_check0_comm.clone() - } - - #[napi(setter)] - pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check0_comm = x; - } - - #[napi(getter)] - pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { - self.range_check1_comm.clone() - } - - #[napi(setter)] - pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check1_comm = x; - } - - #[napi(getter)] - pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_add_comm.clone() - } - - #[napi(setter)] - pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_add_comm = x; - } - - #[napi(getter)] - pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_mul_comm.clone() - } - - #[napi(setter)] - pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_mul_comm = x; - } - - } - #[derive(Clone, Debug, Serialize, Deserialize, Default)] #[napi(object, js_name = [])] pub struct [] { @@ -329,22 +149,6 @@ macro_rules! impl_verification_key { } } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - s0: $NapiF, - s1: $NapiF, - s2: $NapiF, - s3: $NapiF, - s4: $NapiF, - s5: $NapiF, - s6: $NapiF, - ) -> Self { - Self { s0, s1, s2, s3, s4, s5, s6} - } - } - #[derive(Clone, Debug, Serialize, Deserialize, Default)] #[napi(object, js_name = [])] pub struct [] { @@ -403,64 +207,6 @@ macro_rules! impl_verification_key { } } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - xor: Option<$NapiPolyComm>, - lookup: Option<$NapiPolyComm>, - range_check: Option<$NapiPolyComm>, - ffmul: Option<$NapiPolyComm> - ) -> Self { - Self { - xor, - lookup, - range_check, - ffmul - } - } - - #[napi(getter)] - pub fn xor(&self) -> Option<$NapiPolyComm> { - self.xor.clone() - } - - #[napi(setter)] - pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { - self.xor = x - } - - #[napi(getter)] - pub fn lookup(&self) -> Option<$NapiPolyComm> { - self.lookup.clone() - } - - #[napi(setter)] - pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { - self.lookup = x - } - - #[napi(getter)] - pub fn ffmul(&self) -> Option<$NapiPolyComm> { - self.ffmul.clone() - } - - #[napi(setter)] - pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { - self.ffmul = x - } - - #[napi(getter)] - pub fn range_check(&self) -> Option<$NapiPolyComm> { - self.range_check.clone() - } - - #[napi(setter)] - pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { - self.range_check = x - } - } - #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { @@ -536,78 +282,6 @@ macro_rules! impl_verification_key { } } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - joint_lookup_used: bool, - lookup_table: Vec<$NapiPolyComm>, - lookup_selectors: NapiLookupSelectors, - table_ids: Option<$NapiPolyComm>, - lookup_info: NapiLookupInfo, - runtime_tables_selector: Option<$NapiPolyComm> - ) -> NapiLookupVerifierIndex { - NapiLookupVerifierIndex { - joint_lookup_used, - lookup_table, - lookup_selectors, - table_ids, - lookup_info: lookup_info.clone(), - runtime_tables_selector - } - } - - #[napi(getter)] - pub fn lookup_table(&self) -> Vec<$NapiPolyComm> { - self.lookup_table.clone() - } - - #[napi(setter)] - pub fn set_lookup_table(&mut self, x: Vec<$NapiPolyComm>) { - self.lookup_table = x - } - - #[napi(getter)] - pub fn lookup_selectors(&self) -> NapiLookupSelectors { - self.lookup_selectors.clone() - } - - #[napi(setter)] - pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { - self.lookup_selectors = x - } - - #[napi(getter)] - pub fn table_ids(&self) -> Option<$NapiPolyComm>{ - self.table_ids.clone() - } - - #[napi(setter)] - pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { - self.table_ids = x - } - - #[napi(getter)] - pub fn lookup_info(&self) -> NapiLookupInfo { - self.lookup_info.clone() - } - - #[napi(setter)] - pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { - self.lookup_info = x - } - - #[napi(getter)] - pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { - self.runtime_tables_selector.clone() - } - - #[napi(setter)] - pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { - self.runtime_tables_selector = x - } - } - #[napi(object, js_name = [])] #[derive(Clone, Debug, Default)] pub struct [] { @@ -626,65 +300,6 @@ macro_rules! impl_verification_key { } type NapiPlonkVerifierIndex = []; - #[napi] - impl [] { - #[napi(constructor)] - #[allow(clippy::too_many_arguments)] - pub fn new( - domain: NapiDomain, - max_poly_size: i32, - public_: i32, - prev_challenges: i32, - srs: &$NapiSrs, - evals: NapiPlonkVerificationEvals, - shifts: NapiShifts, - lookup_index: Option, - zk_rows: i32, - ) -> Self { - NapiPlonkVerifierIndex { - domain: domain.clone(), - max_poly_size, - public_, - prev_challenges, - srs: srs.clone(), - evals: evals.clone(), - shifts: shifts.clone(), - lookup_index: lookup_index.cloned(), - zk_rows, - } - } - - #[napi(getter)] - pub fn srs(&self) -> $NapiSrs { - self.srs.clone() - } - - #[napi(setter)] - pub fn set_srs(&mut self, x: $NapiSrs) { - self.srs = x - } - - #[napi(getter)] - pub fn evals(&self) -> NapiPlonkVerificationEvals { - self.evals.clone() - } - - #[napi(setter)] - pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { - self.evals = x - } - - #[napi(getter)] - pub fn lookup_index(&self) -> Option { - self.lookup_index.clone() - } - - #[napi(setter)] - pub fn set_lookup_index(&mut self, li: Option) { - self.lookup_index = li - } - } - fn compute_feature_flags(index: &NapiPlonkVerifierIndex) -> FeatureFlags { let xor = index.evals.xor_comm.is_some(); let range_check0 = index.evals.range_check0_comm.is_some(); @@ -725,83 +340,94 @@ macro_rules! impl_verification_key { impl From for VerifierIndex<$G, OpeningProof<$G>> { fn from(index: NapiPlonkVerifierIndex) -> Self { - let of_napi = - { - let max_poly_size = index.max_poly_size; - let public_ = index.public_; - let prev_challenges = index.prev_challenges; - let log_size_of_group = index.domain.log_size_of_group; - let srs = &index.srs; - let evals = &index.evals; - let shifts = &index.shifts; - - let (endo_q, _endo_r) = poly_commitment::ipa::endos::<$GOther>(); - let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap(); - - let feature_flags = compute_feature_flags(&index); - let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true); - - let index = { - let zk_rows = index.zk_rows as u64; - - VerifierIndex { - domain, - - sigma_comm: core::array::from_fn(|i| (&evals.sigma_comm[i]).into()), - generic_comm: (&evals.generic_comm).into(), - coefficients_comm: core::array::from_fn(|i| (&evals.coefficients_comm[i]).into()), - - psm_comm: (&evals.psm_comm).into(), - - complete_add_comm: (&evals.complete_add_comm).into(), - mul_comm: (&evals.mul_comm).into(), - emul_comm: (&evals.emul_comm).into(), - - endomul_scalar_comm: (&evals.endomul_scalar_comm).into(), - xor_comm: (&evals.xor_comm).as_ref().map(Into::into), - range_check0_comm: (&evals.range_check0_comm).as_ref().map(Into::into), - range_check1_comm: (&evals.range_check1_comm).as_ref().map(Into::into), - foreign_field_add_comm: (&evals.foreign_field_add_comm).as_ref().map(Into::into), - foreign_field_mul_comm: (&evals.foreign_field_mul_comm).as_ref().map(Into::into), - rot_comm: (&evals.rot_comm).as_ref().map(Into::into), - - w: { - let res = once_cell::sync::OnceCell::new(); - res.set(zk_w(domain, zk_rows)).unwrap(); - res - }, - endo: endo_q, - max_poly_size: max_poly_size as usize, - public: public_ as usize, - prev_challenges: prev_challenges as usize, - permutation_vanishing_polynomial_m: { - let res = once_cell::sync::OnceCell::new(); - res.set(permutation_vanishing_polynomial(domain, zk_rows)).unwrap(); - res - }, - shift: [ - shifts.s0.into(), - shifts.s1.into(), - shifts.s2.into(), - shifts.s3.into(), - shifts.s4.into(), - shifts.s5.into(), - shifts.s6.into() - ], - srs: { - Arc::clone(&srs.0) - }, - - zk_rows, - - linearization, - powers_of_alpha, - lookup_index: index.lookup_index.map(Into::into), + let max_poly_size = index.max_poly_size; + let public_ = index.public_; + let prev_challenges = index.prev_challenges; + let log_size_of_group = index.domain.log_size_of_group; + let srs = &index.srs; + let evals = &index.evals; + let shifts = &index.shifts; + + let (endo_q, _endo_r) = poly_commitment::ipa::endos::(); + let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap(); + + let feature_flags = compute_feature_flags(&index); + let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true); + + let index = { + let zk_rows = index.zk_rows as u64; + + VerifierIndex { + domain, + + sigma_comm: core::array::from_fn(|i| (&evals.sigma_comm[i]).into()), + generic_comm: (&evals.generic_comm).into(), + coefficients_comm: core::array::from_fn(|i| (&evals.coefficients_comm[i]).into()), + + psm_comm: (&evals.psm_comm).into(), + + complete_add_comm: (&evals.complete_add_comm).into(), + mul_comm: (&evals.mul_comm).into(), + emul_comm: (&evals.emul_comm).into(), + + endomul_scalar_comm: (&evals.endomul_scalar_comm).into(), + xor_comm: (&evals.xor_comm).as_ref().map(Into::into), + range_check0_comm: (&evals.range_check0_comm).as_ref().map(Into::into), + range_check1_comm: (&evals.range_check1_comm).as_ref().map(Into::into), + foreign_field_add_comm: (&evals.foreign_field_add_comm).as_ref().map(Into::into), + foreign_field_mul_comm: (&evals.foreign_field_mul_comm).as_ref().map(Into::into), + rot_comm: (&evals.rot_comm).as_ref().map(Into::into), + + w: { + let res = once_cell::sync::OnceCell::new(); + res.set(zk_w(domain, zk_rows)).unwrap(); + res + }, + endo: endo_q, + max_poly_size: max_poly_size as usize, + public: public_ as usize, + prev_challenges: prev_challenges as usize, + permutation_vanishing_polynomial_m: { + let res = once_cell::sync::OnceCell::new(); + res.set(permutation_vanishing_polynomial(domain, zk_rows)) + .unwrap(); + res + }, + shift: [ + shifts.s0.into(), + shifts.s1.into(), + shifts.s2.into(), + shifts.s3.into(), + shifts.s4.into(), + shifts.s5.into(), + shifts.s6.into(), + ], + srs: { Arc::clone(&srs.0) }, + + zk_rows, + + linearization, + powers_of_alpha, + lookup_index: index.lookup_index.map(Into::into), + } + }; + (index, srs.0.clone()).0 + } + } + + impl From<&VerifierIndex<$G, OpeningProof<$G>>> for NapiPlonkVerifierIndex { + fn from(index: &VerifierIndex<$G, OpeningProof<$G>>) -> Self { + Self { + domain: (&index.domain).into(), + max_poly_size: index.max_poly_size as i32, + public_: index.public as i32, + prev_challenges: index.prev_challenges as i32, + srs: (&index.srs).into(), + evals: index.into(), + shifts: (&index.shift).into(), + lookup_index: index.lookup_index.as_ref().map(Into::into), + zk_rows: index.zk_rows as i32, } - }; - (index, srs.0.clone()) - }; - return of_napi.0; } } @@ -809,7 +435,7 @@ macro_rules! impl_verification_key { offset: Option, srs: &$NapiSrs, path: String, - ) -> Result>, String> { + ) -> napi::Result>> { let path = Path::new(&path); let (endo_q, _endo_r) = poly_commitment::ipa::endos::<$GOther>(); VerifierIndex::<$G, OpeningProof<$G>>::from_file( @@ -817,7 +443,7 @@ macro_rules! impl_verification_key { path, offset.map(|x| x as u64), endo_q, - ).map_err(|e| format!("read_raw: {}", e).as_str()) + ).map_err(|e| Error::new(Status::GenericFailure, format!("read_raw: {}", e))) } #[napi(js_name = [])] @@ -825,9 +451,9 @@ macro_rules! impl_verification_key { offset: Option, srs: &$NapiSrs, path: String, - ) -> napi::bindgen_prelude::Result { + ) -> napi::Result { let vi = read_raw(offset, srs, path)?; - Ok(NapiPlonkVerifierIndex::from(vi)) + Ok(NapiPlonkVerifierIndex::from(&vi)) } #[napi(js_name = [])] @@ -835,13 +461,12 @@ macro_rules! impl_verification_key { append: Option, index: NapiPlonkVerifierIndex, path: String, - ) -> napi::bindgen_prelude::Result<(), String> { + ) -> napi::Result<()> { let index: VerifierIndex<$G, OpeningProof<$G>> = index.into(); let path = Path::new(&path); - index.to_file(path, append).map_err(|e| { - println!("{}", e); - "caml_pasta_plonk_verifier_index_write" - }) + index + .to_file(path, append) + .map_err(|e| Error::new(Status::GenericFailure, format!("plonk_verifier_index_write: {}", e))) } #[napi(js_name = [])] @@ -854,13 +479,13 @@ macro_rules! impl_verification_key { #[napi(js_name = [])] pub fn []( - srs: &$NapiSrs, + _srs: &$NapiSrs, index: String, - ) -> napi::bindgen_prelude::Result { + ) -> napi::Result { let vi: Result>, serde_json::Error> = serde_json::from_str(&index); match vi { - Ok(vi) => Ok(to_napi(srs, vi)), - Err(e) => Err(e.to_string()), + Ok(vi) => Ok(NapiPlonkVerifierIndex::from(&vi)), + Err(e) => Err(Error::new(Status::GenericFailure, format!("deserialize: {}", e))), } } @@ -953,7 +578,7 @@ macro_rules! impl_verification_key { #[napi(js_name = [])] pub fn []( - x: &NapiPlonkVerifierIndex, + x: NapiPlonkVerifierIndex, ) -> NapiPlonkVerifierIndex { x.clone() } diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index e6687f32f1a..88321684b91 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -755,9 +755,9 @@ pub mod fp { use super::*; use crate::{ pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, + plonk_verifier_index::fp::NapiFpPlonkVerifierIndex, poly_comm::vesta::NapiFpPolyComm, wrappers::{field::NapiPastaFp, group::NapiGVesta}, - NapiFpPlonkVerifierIndex, }; use mina_curves::pasta::{Fp, Vesta}; @@ -778,9 +778,9 @@ pub mod fq { use super::*; use crate::{ pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, + plonk_verifier_index::fq::NapiFqPlonkVerifierIndex, poly_comm::pallas::NapiFqPolyComm, wrappers::{field::NapiPastaFq, group::NapiGPallas}, - NapiFqPlonkVerifierIndex, }; use mina_curves::pasta::{Fq, Pallas}; From f6e03288fec1b480d854aca612d77b1635b851ab Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Tue, 2 Dec 2025 16:48:00 -0300 Subject: [PATCH 12/45] Full napi conversion for verifier index --- plonk-napi/src/lib.rs | 12 +- plonk-napi/src/pasta_fp_plonk_index.rs | 11 +- plonk-napi/src/plonk_verifier_index.rs | 1093 +++++++++++++++++ .../fp.rs | 0 .../fq.rs | 0 .../mod.rs | 0 6 files changed, 1109 insertions(+), 7 deletions(-) create mode 100644 plonk-napi/src/plonk_verifier_index.rs rename plonk-napi/src/{plonk_verifier_index => plonk_verifier_index_old}/fp.rs (100%) rename plonk-napi/src/{plonk_verifier_index => plonk_verifier_index_old}/fq.rs (100%) rename plonk-napi/src/{plonk_verifier_index => plonk_verifier_index_old}/mod.rs (100%) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index c7be89aa28c..fc769fe99b4 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -42,8 +42,18 @@ pub use pasta_fq_plonk_index::{ prover_index_fq_from_bytes, prover_index_fq_to_bytes, WasmPastaFqPlonkIndex, }; pub use plonk_verifier_index::{ - caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_shifts, + fp::{ + caml_pasta_fp_plonk_verifier_index_create, caml_pasta_fp_plonk_verifier_index_deep_copy, + caml_pasta_fp_plonk_verifier_index_dummy, caml_pasta_fp_plonk_verifier_index_read, + caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, + }, + fq::{ + caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, + caml_pasta_fq_plonk_verifier_index_dummy, caml_pasta_fq_plonk_verifier_index_read, + caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, + }, }; + pub use poly_comm::{ pallas::NapiFqPolyComm as WasmFqPolyComm, vesta::NapiFpPolyComm as WasmFpPolyComm, }; diff --git a/plonk-napi/src/pasta_fp_plonk_index.rs b/plonk-napi/src/pasta_fp_plonk_index.rs index 7aac5dba3dd..285a93db46b 100644 --- a/plonk-napi/src/pasta_fp_plonk_index.rs +++ b/plonk-napi/src/pasta_fp_plonk_index.rs @@ -26,7 +26,7 @@ use std::{ use crate::tables::{ lookup_table_fp_from_js, runtime_table_cfg_fp_from_js, JsLookupTableFp, JsRuntimeTableCfgFp, }; -pub struct WasmPastaFpPlonkIndex(pub Box>>); +pub struct WasmPastaFpPlonkIndex(pub External>>); #[derive(Serialize, Deserialize)] struct SerializedProverIndex { @@ -74,7 +74,7 @@ impl WasmPastaFpPlonkIndex { DefaultFqSponge, >(); - Ok(WasmPastaFpPlonkIndex(Box::new(index))) + Ok(WasmPastaFpPlonkIndex(External::new(index))) } } @@ -183,7 +183,7 @@ pub fn caml_pasta_fp_plonk_index_create( ); index.compute_verifier_index_digest::>(); - Ok(External::new(WasmPastaFpPlonkIndex(Box::new(index)))) + Ok(External::new(WasmPastaFpPlonkIndex(External::new(index)))) } #[napi(js_name = "caml_pasta_fp_plonk_index_decode")] @@ -205,7 +205,7 @@ pub fn caml_pasta_fp_plonk_index_decode( index.linearization = linearization; index.powers_of_alpha = powers_of_alpha; - Ok(External::new(WasmPastaFpPlonkIndex(Box::new(index)))) + Ok(External::new(WasmPastaFpPlonkIndex(External::new(index)))) } #[napi(js_name = "caml_pasta_fp_plonk_index_encode")] @@ -288,6 +288,5 @@ pub fn caml_pasta_fp_plonk_index_read( t.linearization = linearization; t.powers_of_alpha = powers_of_alpha; - // - Ok(External::new(WasmPastaFpPlonkIndex(Box::new(t)))) + Ok(External::new(WasmPastaFpPlonkIndex(External::new(t)))) } diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs new file mode 100644 index 00000000000..7b7c5dcab31 --- /dev/null +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -0,0 +1,1093 @@ +use crate::{vector::NapiVector, wrappers::lookups::NapiLookupInfo}; + +use ark_ec::AffineRepr; +use ark_ff::One; +use ark_poly::{EvaluationDomain, Radix2EvaluationDomain as Domain}; +use kimchi::{ + circuits::{ + constraints::FeatureFlags, + lookup::{ + index::LookupSelectors, + lookups::{LookupFeatures, LookupPatterns}, + }, + polynomials::permutation::{permutation_vanishing_polynomial, zk_w, Shifts}, + wires::{COLUMNS, PERMUTS}, + }, + linearization::expr_linearization, + verifier_index::{LookupVerifierIndex, VerifierIndex as DlogVerifierIndex}, +}; +use napi::{bindgen_prelude::*, Error as NapiError, Status}; +use napi_derive::napi; +use paste::paste; +use poly_commitment::{ + commitment::PolyComm, + ipa::{OpeningProof, SRS}, + SRS as _, +}; +use std::{path::Path, sync::Arc}; + +macro_rules! impl_verification_key { + ( + $name: ident, + $NapiG: ty, + $G: ty, + $NapiF: ty, + $F: ty, + $NapiPolyComm: ty, + $NapiSrs: ty, + $GOther: ty, + $FrSpongeParams: path, + $FqSpongeParams: path, + $NapiIndex: ty, + $field_name: ident + ) => { + paste! { + #[napi(js_name = [])] + #[derive(Clone, Copy)] + pub struct [] { + pub log_size_of_group: i32, + pub group_gen: $NapiF, + } + type NapiDomain = []; + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { + NapiDomain {log_size_of_group, group_gen} + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + impl<'a> ToNapiValue for &'a mut [] { + unsafe fn to_napi_value( + env: sys::napi_env, + val: Self, + ) -> Result { + <[] as ToNapiValue>::to_napi_value(env, val.clone()) + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + #[napi(skip)] + pub sigma_comm: NapiVector<$NapiPolyComm>, + #[napi(skip)] + pub coefficients_comm: NapiVector<$NapiPolyComm>, + #[napi(skip)] + pub generic_comm: $NapiPolyComm, + #[napi(skip)] + pub psm_comm: $NapiPolyComm, + #[napi(skip)] + pub complete_add_comm: $NapiPolyComm, + #[napi(skip)] + pub mul_comm: $NapiPolyComm, + #[napi(skip)] + pub emul_comm: $NapiPolyComm, + #[napi(skip)] + pub endomul_scalar_comm: $NapiPolyComm, + #[napi(skip)] + pub xor_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check0_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check1_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub foreign_field_add_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub foreign_field_mul_comm: Option<$NapiPolyComm>, + #[napi(skip)] + pub rot_comm: Option<$NapiPolyComm> + } + + type NapiPlonkVerificationEvals = []; + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi] + impl [] { + #[allow(clippy::too_many_arguments)] + #[napi(constructor)] + pub fn new( + sigma_comm: NapiVector<$NapiPolyComm>, + coefficients_comm: NapiVector<$NapiPolyComm>, + generic_comm: &$NapiPolyComm, + psm_comm: &$NapiPolyComm, + complete_add_comm: &$NapiPolyComm, + mul_comm: &$NapiPolyComm, + emul_comm: &$NapiPolyComm, + endomul_scalar_comm: &$NapiPolyComm, + xor_comm: Option<$NapiPolyComm>, + range_check0_comm: Option<$NapiPolyComm>, + range_check1_comm: Option<$NapiPolyComm>, + foreign_field_add_comm: Option<$NapiPolyComm>, + foreign_field_mul_comm: Option<$NapiPolyComm>, + rot_comm: Option<$NapiPolyComm>, + ) -> Self { + NapiPlonkVerificationEvals { + sigma_comm: sigma_comm.clone(), + coefficients_comm: coefficients_comm.clone(), + generic_comm: generic_comm.clone(), + psm_comm: psm_comm.clone(), + complete_add_comm: complete_add_comm.clone(), + mul_comm: mul_comm.clone(), + emul_comm: emul_comm.clone(), + endomul_scalar_comm: endomul_scalar_comm.clone(), + xor_comm: xor_comm.clone(), + range_check0_comm: range_check0_comm.clone(), + range_check1_comm: range_check1_comm.clone(), + foreign_field_mul_comm: foreign_field_mul_comm.clone(), + foreign_field_add_comm: foreign_field_add_comm.clone(), + rot_comm: rot_comm.clone(), + } + } + + #[napi(getter, js_name = "sigma_comm")] + pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { + self.sigma_comm.clone() + } + + #[napi(setter, js_name = "set_sigma_comm")] + pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + self.sigma_comm = x; + } + + #[napi(getter, js_name = "coefficients_comm")] + pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { + self.coefficients_comm.clone() + } + + #[napi(setter, js_name = "set_coefficients_comm")] + pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + self.coefficients_comm = x; + } + + #[napi(getter, js_name = "generic_comm")] + pub fn generic_comm(&self) -> $NapiPolyComm { + self.generic_comm.clone() + } + + #[napi(setter, js_name = "set_generic_comm")] + pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { + self.generic_comm = x; + } + + #[napi(getter, js_name = "psm_comm")] + pub fn psm_comm(&self) -> $NapiPolyComm { + self.psm_comm.clone() + } + + #[napi(setter, js_name = "set_psm_comm")] + pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { + self.psm_comm = x; + } + + #[napi(getter, js_name = "complete_add_comm")] + pub fn complete_add_comm(&self) -> $NapiPolyComm { + self.complete_add_comm.clone() + } + + #[napi(setter, js_name = "set_complete_add_comm")] + pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { + self.complete_add_comm = x; + } + + #[napi(getter, js_name = "mul_comm")] + pub fn mul_comm(&self) -> $NapiPolyComm { + self.mul_comm.clone() + } + + #[napi(setter, js_name = "set_mul_comm")] + pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { + self.mul_comm = x; + } + + #[napi(getter, js_name = "emul_comm")] + pub fn emul_comm(&self) -> $NapiPolyComm { + self.emul_comm.clone() + } + + #[napi(setter, js_name = "set_emul_comm")] + pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { + self.emul_comm = x; + } + + #[napi(getter, js_name = "endomul_scalar_comm")] + pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { + self.endomul_scalar_comm.clone() + } + + #[napi(setter, js_name = "set_endomul_scalar_comm")] + pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { + self.endomul_scalar_comm = x; + } + + #[napi(getter, js_name = "xor_comm")] + pub fn xor_comm(&self) -> Option<$NapiPolyComm> { + self.xor_comm.clone() + } + + #[napi(setter, js_name = "set_xor_comm")] + pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { + self.xor_comm = x; + } + + #[napi(getter, js_name = "rot_comm")] + pub fn rot_comm(&self) -> Option<$NapiPolyComm> { + self.rot_comm.clone() + } + + #[napi(setter, js_name = "set_rot_comm")] + pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { + self.rot_comm = x; + } + + #[napi(getter, js_name = "range_check0_comm")] + pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { + self.range_check0_comm.clone() + } + + #[napi(setter, js_name = "set_range_check0_comm")] + pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check0_comm = x; + } + + #[napi(getter, js_name = "range_check1_comm")] + pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { + self.range_check1_comm.clone() + } + + #[napi(setter, js_name = "set_range_check1_comm")] + pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check1_comm = x; + } + + #[napi(getter, js_name = "foreign_field_add_comm")] + pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_add_comm.clone() + } + + #[napi(setter, js_name = "set_foreign_field_add_comm")] + pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_add_comm = x; + } + + #[napi(getter, js_name = "foreign_field_mul_comm")] + pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_mul_comm.clone() + } + + #[napi(setter, js_name = "set_foreign_field_mul_comm")] + pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_mul_comm = x; + } + + } + + #[derive(Clone, Copy)] + #[napi(js_name = [])] + pub struct [] { + pub s0: $NapiF, + pub s1: $NapiF, + pub s2: $NapiF, + pub s3: $NapiF, + pub s4: $NapiF, + pub s5: $NapiF, + pub s6: $NapiF, + } + + type NapiShifts = []; + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + impl<'a> ToNapiValue for &'a mut [] { + unsafe fn to_napi_value( + env: sys::napi_env, + val: Self, + ) -> Result { + <[] as ToNapiValue>::to_napi_value(env, val.clone()) + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + s0: $NapiF, + s1: $NapiF, + s2: $NapiF, + s3: $NapiF, + s4: $NapiF, + s5: $NapiF, + s6: $NapiF + ) -> Self { + Self { s0, s1, s2, s3, s4, s5, s6 } + } + } + + #[napi] + #[derive(Clone)] + pub struct [] { + #[napi(skip)] + pub xor: Option<$NapiPolyComm>, + #[napi(skip)] + pub lookup : Option<$NapiPolyComm>, + #[napi(skip)] + pub range_check: Option<$NapiPolyComm>, + #[napi(skip)] + pub ffmul: Option<$NapiPolyComm>, + } + + type NapiLookupSelectors = []; + + impl From for LookupSelectors> { + fn from(x: NapiLookupSelectors) -> Self { + Self { + xor: x.xor.map(Into::into), + lookup: x.lookup.map(Into::into), + range_check: x.range_check.map(Into::into), + ffmul: x.ffmul.map(Into::into), + } + } + } + + impl From<&NapiLookupSelectors> for LookupSelectors> { + fn from(x: &NapiLookupSelectors) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + impl From<&LookupSelectors>> for NapiLookupSelectors { + fn from(x: &LookupSelectors>) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + impl From>> for NapiLookupSelectors { + fn from(x: LookupSelectors>) -> Self { + Self { + xor: x.xor.clone().map(Into::into), + lookup: x.lookup.clone().map(Into::into), + range_check: x.range_check.clone().map(Into::into), + ffmul: x.ffmul.clone().map(Into::into), + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + xor: Option<$NapiPolyComm>, + lookup: Option<$NapiPolyComm>, + range_check: Option<$NapiPolyComm>, + ffmul: Option<$NapiPolyComm> + ) -> Self { + Self { + xor, + lookup, + range_check, + ffmul + } + } + + #[napi(getter)] + pub fn xor(&self) -> Option<$NapiPolyComm> { + self.xor.clone() + } + + #[napi(setter, js_name = "set_xor")] + pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { + self.xor = x + } + + #[napi(getter)] + pub fn lookup(&self) -> Option<$NapiPolyComm> { + self.lookup.clone() + } + + #[napi(setter, js_name = "set_lookup")] + pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { + self.lookup = x + } + + #[napi(getter)] + pub fn ffmul(&self) -> Option<$NapiPolyComm> { + self.ffmul.clone() + } + + #[napi(setter, js_name = "set_ffmul")] + pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { + self.ffmul = x + } + + #[napi(getter, js_name = "range_check")] + pub fn range_check(&self) -> Option<$NapiPolyComm> { + self.range_check.clone() + } + + #[napi(setter, js_name = "set_range_check")] + pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { + self.range_check = x + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + pub joint_lookup_used: bool, + + #[napi(skip)] + pub lookup_table: NapiVector<$NapiPolyComm>, + + #[napi(skip)] + pub lookup_selectors: NapiLookupSelectors, + + #[napi(skip)] + pub table_ids: Option<$NapiPolyComm>, + + #[napi(skip)] + pub lookup_info: NapiLookupInfo, + + #[napi(skip)] + pub runtime_tables_selector: Option<$NapiPolyComm>, + } + + type NapiLookupVerifierIndex = []; + + impl From<&LookupVerifierIndex<$G>> for NapiLookupVerifierIndex { + fn from(x: &LookupVerifierIndex<$G>) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.clone().iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.clone().into(), + table_ids: x.table_ids.clone().map(Into::into), + lookup_info: x.lookup_info.into(), + runtime_tables_selector: x.runtime_tables_selector.clone().map(Into::into) + } + } + } + + impl From> for NapiLookupVerifierIndex { + fn from(x: LookupVerifierIndex<$G>) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.into(), + table_ids: x.table_ids.map(Into::into), + lookup_info: x.lookup_info.into(), + runtime_tables_selector: x.runtime_tables_selector.map(Into::into) + } + } + } + + impl From<&NapiLookupVerifierIndex> for LookupVerifierIndex<$G> { + fn from(x: &NapiLookupVerifierIndex) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.clone().iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.clone().into(), + table_ids: x.table_ids.clone().map(Into::into), + lookup_info: x.lookup_info.clone().into(), + runtime_tables_selector: x.runtime_tables_selector.clone().map(Into::into) + } + } + } + + impl From for LookupVerifierIndex<$G> { + fn from(x: NapiLookupVerifierIndex) -> Self { + Self { + joint_lookup_used: x.joint_lookup_used.into(), + lookup_table: x.lookup_table.iter().map(Into::into).collect(), + lookup_selectors: x.lookup_selectors.into(), + table_ids: x.table_ids.map(Into::into), + lookup_info: x.lookup_info.into(), + runtime_tables_selector: x.runtime_tables_selector.map(Into::into) + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + joint_lookup_used: bool, + lookup_table: NapiVector<$NapiPolyComm>, + lookup_selectors: NapiLookupSelectors, + table_ids: Option<$NapiPolyComm>, + lookup_info: NapiLookupInfo, + runtime_tables_selector: Option<$NapiPolyComm> + ) -> NapiLookupVerifierIndex { + NapiLookupVerifierIndex { + joint_lookup_used, + lookup_table, + lookup_selectors, + table_ids, + lookup_info: lookup_info.clone(), + runtime_tables_selector + } + } + + #[napi(getter, js_name = "lookup_table")] + pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { + self.lookup_table.clone() + } + + #[napi(setter, js_name = "set_lookup_table")] + pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { + self.lookup_table = x + } + + #[napi(getter, js_name = "lookup_selectors")] + pub fn lookup_selectors(&self) -> NapiLookupSelectors { + self.lookup_selectors.clone() + } + + #[napi(setter, js_name = "set_lookup_selectors")] + pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { + self.lookup_selectors = x + } + + #[napi(getter, js_name = "table_ids")] + pub fn table_ids(&self) -> Option<$NapiPolyComm>{ + self.table_ids.clone() + } + + #[napi(setter, js_name = "set_table_ids")] + pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { + self.table_ids = x + } + + #[napi(getter, js_name = "lookup_info")] + pub fn lookup_info(&self) -> NapiLookupInfo { + self.lookup_info.clone() + } + + #[napi(setter, js_name = "set_lookup_info")] + pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { + self.lookup_info = x + } + + #[napi(getter, js_name = "runtime_tables_selector")] + pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { + self.runtime_tables_selector.clone() + } + + #[napi(setter, js_name = "set_runtime_tables_selector")] + pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { + self.runtime_tables_selector = x + } + } + + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + pub domain: NapiDomain, + pub max_poly_size: i32, + pub public_: i32, + pub prev_challenges: i32, + #[napi(skip)] + pub srs: $NapiSrs, + #[napi(skip)] + pub evals: NapiPlonkVerificationEvals, + pub shifts: NapiShifts, + #[napi(skip)] + pub lookup_index: Option, + pub zk_rows: i32, + } + + type NapiPlonkVerifierIndex = []; + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + + #[napi] + impl [] { + #[napi(constructor)] + #[allow(clippy::too_many_arguments)] + pub fn new( + domain: &NapiDomain, + max_poly_size: i32, + public_: i32, + prev_challenges: i32, + srs: &$NapiSrs, + evals: &NapiPlonkVerificationEvals, + shifts: &NapiShifts, + lookup_index: Option, + zk_rows: i32, + ) -> Self { + NapiPlonkVerifierIndex { + domain: domain.clone(), + max_poly_size, + public_, + prev_challenges, + srs: srs.clone(), + evals: evals.clone(), + shifts: shifts.clone(), + lookup_index: lookup_index.clone(), + zk_rows, + } + } + + #[napi(getter)] + pub fn srs(&self) -> $NapiSrs { + self.srs.clone() + } + + #[napi(setter, js_name = "set_srs")] + pub fn set_srs(&mut self, x: $NapiSrs) { + self.srs = x + } + + #[napi(getter)] + pub fn evals(&self) -> NapiPlonkVerificationEvals { + self.evals.clone() + } + + #[napi(setter, js_name = "set_evals")] + pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { + self.evals = x + } + + #[napi(getter, js_name = "lookup_index")] + pub fn lookup_index(&self) -> Option { + self.lookup_index.clone() + } + + #[napi(setter, js_name = "set_lookup_index")] + pub fn set_lookup_index(&mut self, li: Option) { + self.lookup_index = li + } + } + + pub fn to_napi( + srs: &Arc>, + vi: DlogVerifierIndex<$G, OpeningProof<$G>>, + ) -> NapiPlonkVerifierIndex { + NapiPlonkVerifierIndex { + domain: NapiDomain { + log_size_of_group: vi.domain.log_size_of_group as i32, + group_gen: vi.domain.group_gen.into(), + }, + max_poly_size: vi.max_poly_size as i32, + public_: vi.public as i32, + prev_challenges: vi.prev_challenges as i32, + srs: srs.into(), + evals: NapiPlonkVerificationEvals { + sigma_comm: IntoIterator::into_iter(vi.sigma_comm).map(From::from).collect(), + coefficients_comm: IntoIterator::into_iter(vi.coefficients_comm).map(From::from).collect(), + generic_comm: vi.generic_comm.into(), + psm_comm: vi.psm_comm.into(), + complete_add_comm: vi.complete_add_comm.into(), + mul_comm: vi.mul_comm.into(), + emul_comm: vi.emul_comm.into(), + endomul_scalar_comm: vi.endomul_scalar_comm.into(), + xor_comm: vi.xor_comm.map(|v| v.into()), + range_check0_comm: vi.range_check0_comm.map(|v| v.into()), + range_check1_comm: vi.range_check1_comm.map(|v| v.into()), + foreign_field_add_comm: vi.foreign_field_add_comm.map(|v| v.into()), + foreign_field_mul_comm: vi.foreign_field_mul_comm.map(|v| v.into()), + rot_comm: vi.rot_comm.map(|v| v.into()) + }, + shifts: + NapiShifts { + s0: vi.shift[0].into(), + s1: vi.shift[1].into(), + s2: vi.shift[2].into(), + s3: vi.shift[3].into(), + s4: vi.shift[4].into(), + s5: vi.shift[5].into(), + s6: vi.shift[6].into(), + }, + lookup_index: vi.lookup_index.map(Into::into), + zk_rows: vi.zk_rows as i32, + } + } + + fn compute_feature_flags(index: &NapiPlonkVerifierIndex) -> FeatureFlags { + let xor = index.evals.xor_comm.is_some(); + let range_check0 = index.evals.range_check0_comm.is_some(); + let range_check1 = index.evals.range_check1_comm.is_some(); + let foreign_field_add = index.evals.foreign_field_add_comm.is_some(); + let foreign_field_mul = index.evals.foreign_field_mul_comm.is_some(); + let rot = index.evals.rot_comm.is_some(); + + let lookup = index + .lookup_index.as_ref() + .map_or(false, |li| li.lookup_info.features.patterns.lookup); + + let runtime_tables = index + .lookup_index.as_ref() + .map_or(false, |li| li.runtime_tables_selector.is_some()); + + let patterns = LookupPatterns { + xor, + lookup, + range_check: range_check0 || range_check1 || rot, + foreign_field_mul, + }; + + FeatureFlags { + range_check0, + range_check1, + foreign_field_add, + foreign_field_mul, + xor, + rot, + lookup_features: LookupFeatures { + patterns, + joint_lookup_used: patterns.joint_lookups_used(), + uses_runtime_tables: runtime_tables, + }, + } + } + + pub fn of_napi( + index: NapiPlonkVerifierIndex, + ) -> (DlogVerifierIndex>, Arc>) { + let max_poly_size = index.max_poly_size; + let public_ = index.public_; + let prev_challenges = index.prev_challenges; + let log_size_of_group = index.domain.log_size_of_group; + let srs = &index.srs; + let evals = &index.evals; + let shifts = &index.shifts; + + let (endo_q, _endo_r) = poly_commitment::ipa::endos::<$GOther>(); + let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap(); + + let feature_flags = compute_feature_flags(&index); + let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true); + + let index = { + let zk_rows = index.zk_rows as u64; + + DlogVerifierIndex { + domain, + + sigma_comm: core::array::from_fn(|i| (&evals.sigma_comm[i]).into()), + generic_comm: (&evals.generic_comm).into(), + coefficients_comm: core::array::from_fn(|i| (&evals.coefficients_comm[i]).into()), + + psm_comm: (&evals.psm_comm).into(), + + complete_add_comm: (&evals.complete_add_comm).into(), + mul_comm: (&evals.mul_comm).into(), + emul_comm: (&evals.emul_comm).into(), + + endomul_scalar_comm: (&evals.endomul_scalar_comm).into(), + xor_comm: (&evals.xor_comm).as_ref().map(Into::into), + range_check0_comm: (&evals.range_check0_comm).as_ref().map(Into::into), + range_check1_comm: (&evals.range_check1_comm).as_ref().map(Into::into), + foreign_field_add_comm: (&evals.foreign_field_add_comm).as_ref().map(Into::into), + foreign_field_mul_comm: (&evals.foreign_field_mul_comm).as_ref().map(Into::into), + rot_comm: (&evals.rot_comm).as_ref().map(Into::into), + + w: { + let res = once_cell::sync::OnceCell::new(); + res.set(zk_w(domain, zk_rows)).unwrap(); + res + }, + endo: endo_q, + max_poly_size: max_poly_size as usize, + public: public_ as usize, + prev_challenges: prev_challenges as usize, + permutation_vanishing_polynomial_m: { + let res = once_cell::sync::OnceCell::new(); + res.set(permutation_vanishing_polynomial(domain, zk_rows)).unwrap(); + res + }, + shift: [ + shifts.s0.into(), + shifts.s1.into(), + shifts.s2.into(), + shifts.s3.into(), + shifts.s4.into(), + shifts.s5.into(), + shifts.s6.into() + ], + srs: { + Arc::clone(&srs.0) + }, + + zk_rows, + + linearization, + powers_of_alpha, + lookup_index: index.lookup_index.map(Into::into), + } + }; + (index, srs.0.clone()) + } + + impl From for DlogVerifierIndex<$G, OpeningProof<$G>> { + fn from(index: NapiPlonkVerifierIndex) -> Self { + of_napi(index).0 + } + } + + pub fn read_raw( + offset: Option, + srs: &$NapiSrs, + path: String, + ) -> Result>> { + let path = Path::new(&path); + let (endo_q, _endo_r) = poly_commitment::ipa::endos::(); + DlogVerifierIndex::<$G, OpeningProof<$G>>::from_file( + srs.0.clone(), + path, + offset.map(|x| x as u64), + endo_q, + ).map_err(|e| NapiError::new(Status::GenericFailure, format!("read_raw: {}", e).as_str())) + } + + #[napi(js_name = [<$name:snake _read>])] + pub fn [<$name:snake _read>]( + offset: Option, + srs: &$NapiSrs, + path: String, + ) -> Result { + let vi = read_raw(offset, srs, path)?; + Ok(to_napi(srs, vi.into())) + } + + #[napi(js_name = [<$name:snake _write>])] + pub fn [<$name:snake _write>]( + append: Option, + index: NapiPlonkVerifierIndex, + path: String, + ) -> Result<()> { + let index: DlogVerifierIndex<$G, OpeningProof<$G>> = index.into(); + let path = Path::new(&path); + index.to_file(path, append).map_err(|e| { + println!("{}", e); + NapiError::new(Status::GenericFailure, "caml_pasta_fp_plonk_verifier_index_raw_read") + }) + } + + #[napi(js_name = [<$name:snake _serialize>])] + pub fn [<$name:snake _serialize>]( + index: NapiPlonkVerifierIndex, + ) -> String { + let index: DlogVerifierIndex<$G, OpeningProof<$G>> = index.into(); + serde_json::to_string(&index).unwrap() + } + + // #[napi(js_name = [<$name:snake _deserialize>])] + // pub fn [<$name:snake _deserialize>]( + // srs: &$NapiSrs, + // index: String, + // ) -> Result { + // let vi: Result>, serde_json::Error> = serde_json::from_str(&index); + // match vi { + // Ok(vi) => Ok(to_napi(srs, vi)), + // Err(e) => Err(JsError::new(&(e.to_string()))), + // } + // } + + #[napi(js_name = [<$name:snake _create>])] + pub fn [<$name:snake _create>]( + index: &External<$NapiIndex>, + ) -> NapiPlonkVerifierIndex { + index.0.srs.get_lagrange_basis(index.0.as_ref().cs.domain.d1); + let verifier_index = index.0.as_ref().verifier_index(); + to_napi(&index.0.as_ref().srs, verifier_index) + } + + #[napi(js_name = [<$name:snake _shifts>])] + pub fn [<$name:snake _shifts>](log2_size: i32) -> NapiShifts { + let domain = Domain::<$F>::new(1 << log2_size).unwrap(); + let shifts = Shifts::new(&domain); + let s = shifts.shifts(); + NapiShifts { + s0: s[0].clone().into(), + s1: s[1].clone().into(), + s2: s[2].clone().into(), + s3: s[3].clone().into(), + s4: s[4].clone().into(), + s5: s[5].clone().into(), + s6: s[6].clone().into(), + } + } + + #[napi(js_name = [<$name:snake _dummy>])] + pub fn [<$name:snake _dummy>]() -> NapiPlonkVerifierIndex { + fn comm() -> $NapiPolyComm { + let g: $NapiG = $G::generator().into(); + $NapiPolyComm { + shifted: None, + unshifted: vec![g].into(), + } + } + fn vec_comm(num: usize) -> NapiVector<$NapiPolyComm> { + (0..num).map(|_| comm()).collect() + } + + NapiPlonkVerifierIndex { + domain: NapiDomain { + log_size_of_group: 1, + group_gen: $F::one().into(), + }, + max_poly_size: 0, + public_: 0, + prev_challenges: 0, + srs: $NapiSrs(Arc::new(SRS::create(0))), + evals: NapiPlonkVerificationEvals { + sigma_comm: vec_comm(PERMUTS), + coefficients_comm: vec_comm(COLUMNS), + generic_comm: comm(), + psm_comm: comm(), + complete_add_comm: comm(), + mul_comm: comm(), + emul_comm: comm(), + endomul_scalar_comm: comm(), + xor_comm: None, + range_check0_comm: None, + range_check1_comm: None, + foreign_field_add_comm: None, + foreign_field_mul_comm: None, + rot_comm: None, + }, + shifts: + NapiShifts { + s0: $F::one().into(), + s1: $F::one().into(), + s2: $F::one().into(), + s3: $F::one().into(), + s4: $F::one().into(), + s5: $F::one().into(), + s6: $F::one().into(), + }, + lookup_index: None, + zk_rows: 3, + } + } + + #[napi(js_name = [<$name:snake _deep_copy>])] + pub fn [<$name:snake _deep_copy>]( + x: &NapiPlonkVerifierIndex, + ) -> NapiPlonkVerifierIndex { + x.clone() + } + + } + } + +} + +pub mod fp { + use super::*; + use crate::{ + pasta_fp_plonk_index::WasmPastaFpPlonkIndex, + poly_comm::vesta::NapiFpPolyComm as WasmPolyComm, + srs::fp::NapiFpSrs as WasmFpSrs, + wrappers::{field::NapiPastaFp as WasmPastaFp, group::NapiGVesta as WasmGVesta}, + }; + // use arkworks::WasmGVesta; + use mina_curves::pasta::{Fp, Pallas as GAffineOther, Vesta as GAffine}; + + impl_verification_key!( + caml_pasta_fp_plonk_verifier_index, + WasmGVesta, + GAffine, + WasmPastaFp, + Fp, + WasmPolyComm, + WasmFpSrs, + GAffineOther, + mina_poseidon::pasta::fp_kimchi, + mina_poseidon::pasta::fq_kimchi, + WasmPastaFpPlonkIndex, + Fp + ); +} + +pub mod fq { + use super::*; + use crate::{ + pasta_fq_plonk_index::WasmPastaFqPlonkIndex, + poly_comm::pallas::NapiFqPolyComm as WasmPolyComm, + srs::fq::NapiFqSrs as WasmFqSrs, + wrappers::{field::NapiPastaFq as WasmPastaFq, group::NapiGPallas as WasmGPallas}, + }; + // use arkworks::WasmGPallas; + use mina_curves::pasta::{Fq, Pallas as GAffine, Vesta as GAffineOther}; + + impl_verification_key!( + caml_pasta_fq_plonk_verifier_index, + WasmGPallas, + GAffine, + WasmPastaFq, + Fq, + WasmPolyComm, + WasmFqSrs, + GAffineOther, + mina_poseidon::pasta::fq_kimchi, + mina_poseidon::pasta::fp_kimchi, + WasmPastaFqPlonkIndex, + Fq + ); +} diff --git a/plonk-napi/src/plonk_verifier_index/fp.rs b/plonk-napi/src/plonk_verifier_index_old/fp.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index/fp.rs rename to plonk-napi/src/plonk_verifier_index_old/fp.rs diff --git a/plonk-napi/src/plonk_verifier_index/fq.rs b/plonk-napi/src/plonk_verifier_index_old/fq.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index/fq.rs rename to plonk-napi/src/plonk_verifier_index_old/fq.rs diff --git a/plonk-napi/src/plonk_verifier_index/mod.rs b/plonk-napi/src/plonk_verifier_index_old/mod.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index/mod.rs rename to plonk-napi/src/plonk_verifier_index_old/mod.rs From 6f8c8f11e38f6bc953df6957c582702255c56c8b Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 08:35:47 -0300 Subject: [PATCH 13/45] Export napi domain --- plonk-napi/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index fc769fe99b4..f7958da3be9 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -46,11 +46,13 @@ pub use plonk_verifier_index::{ caml_pasta_fp_plonk_verifier_index_create, caml_pasta_fp_plonk_verifier_index_deep_copy, caml_pasta_fp_plonk_verifier_index_dummy, caml_pasta_fp_plonk_verifier_index_read, caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, + NapiFpDomain as WasmFpDomain, }, fq::{ caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, caml_pasta_fq_plonk_verifier_index_dummy, caml_pasta_fq_plonk_verifier_index_read, caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, + NapiFqDomain as WasmFqDomain, }, }; From 64c7a76f6f688f75611c8a205575f73dfbddb646 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 08:57:45 -0300 Subject: [PATCH 14/45] export napi plonk-verifier-index --- plonk-napi/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index f7958da3be9..d676d21d2c9 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -46,13 +46,13 @@ pub use plonk_verifier_index::{ caml_pasta_fp_plonk_verifier_index_create, caml_pasta_fp_plonk_verifier_index_deep_copy, caml_pasta_fp_plonk_verifier_index_dummy, caml_pasta_fp_plonk_verifier_index_read, caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, - NapiFpDomain as WasmFpDomain, + NapiFpDomain as WasmFpDomain, NapiFpPlonkVerifierIndex as WasmFpPlonkVerifierIndex, }, fq::{ caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, caml_pasta_fq_plonk_verifier_index_dummy, caml_pasta_fq_plonk_verifier_index_read, caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, - NapiFqDomain as WasmFqDomain, + NapiFqDomain as WasmFqDomain, NapiFqPlonkVerifierIndex as WasmFqPlonkVerifierIndex, }, }; From e47bad2eab374af869206364bfa9d893faf01054 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 10:15:01 -0300 Subject: [PATCH 15/45] Revert napi shifts --- plonk-napi/src/plonk_verifier_index.rs | 33 ++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 7b7c5dcab31..2abfe716dd6 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -958,11 +958,22 @@ macro_rules! impl_verification_key { } #[napi(js_name = [<$name:snake _shifts>])] - pub fn [<$name:snake _shifts>](log2_size: i32) -> NapiShifts { - let domain = Domain::<$F>::new(1 << log2_size).unwrap(); + pub fn [<$name:snake _shifts>]( + log2_size: i32, + ) -> napi::bindgen_prelude::Result { + println!( + "from napi! caml_pasta_fp_plonk_verifier_index_shifts with log2_size {}", + log2_size + ); + + let size = 1usize << (log2_size as u32); + let domain = Domain::<$F>::new(size) + .ok_or_else(|| Error::new(Status::InvalidArg, "failed to create evaluation domain"))?; + let shifts = Shifts::new(&domain); let s = shifts.shifts(); - NapiShifts { + + Ok(NapiShifts { s0: s[0].clone().into(), s1: s[1].clone().into(), s2: s[2].clone().into(), @@ -970,8 +981,22 @@ macro_rules! impl_verification_key { s4: s[4].clone().into(), s5: s[5].clone().into(), s6: s[6].clone().into(), - } + }) } + // pub fn [<$name:snake _shifts>](log2_size: i32) -> NapiShifts { + // let domain = Domain::<$F>::new(1 << log2_size).unwrap(); + // let shifts = Shifts::new(&domain); + // let s = shifts.shifts(); + // NapiShifts { + // s0: s[0].clone().into(), + // s1: s[1].clone().into(), + // s2: s[2].clone().into(), + // s3: s[3].clone().into(), + // s4: s[4].clone().into(), + // s5: s[5].clone().into(), + // s6: s[6].clone().into(), + // } + // } #[napi(js_name = [<$name:snake _dummy>])] pub fn [<$name:snake _dummy>]() -> NapiPlonkVerifierIndex { From 496142923c47efb08638069ee0bf1d652f079f2b Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 10:53:48 -0300 Subject: [PATCH 16/45] small verifier index fix --- plonk-napi/src/plonk_verifier_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 2abfe716dd6..0cce56cffe4 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -953,7 +953,7 @@ macro_rules! impl_verification_key { index: &External<$NapiIndex>, ) -> NapiPlonkVerifierIndex { index.0.srs.get_lagrange_basis(index.0.as_ref().cs.domain.d1); - let verifier_index = index.0.as_ref().verifier_index(); + let verifier_index = index.0.verifier_index(); to_napi(&index.0.as_ref().srs, verifier_index) } From c985d4f9aa16a969f9c553c2c85e6a0031802a41 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 11:07:09 -0300 Subject: [PATCH 17/45] another fix --- plonk-napi/src/plonk_verifier_index.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 0cce56cffe4..76df8114861 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -58,6 +58,13 @@ macro_rules! impl_verification_key { } } + impl From for Domain<$F> { + fn from(domain: NapiDomain) -> Self { + let size = 1 << domain.log_size_of_group; + Domain::<$F>::new(size).expect("Failed to create evaluation domain") + } + } + impl FromNapiValue for [] { unsafe fn from_napi_value( env: sys::napi_env, From 51a65b2b8f798b842dfea1045b63b8906f760134 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Wed, 3 Dec 2025 11:42:32 -0300 Subject: [PATCH 18/45] add names to napi domain --- plonk-napi/src/plonk_verifier_index.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 76df8114861..d1ab27ac478 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -45,7 +45,9 @@ macro_rules! impl_verification_key { #[napi(js_name = [])] #[derive(Clone, Copy)] pub struct [] { + #[napi(js_name = "log_size_of_group")] pub log_size_of_group: i32, + #[napi(js_name = "group_gen")] pub group_gen: $NapiF, } type NapiDomain = []; From f71f28ecbf9fc18aad16f4486c116ac2af11e67f Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 3 Dec 2025 19:51:17 +0100 Subject: [PATCH 19/45] napi: rename prover index serde functions --- plonk-napi/src/lib.rs | 4 +-- plonk-napi/src/pasta_fp_plonk_index.rs | 36 ++++++++++++++------------ plonk-napi/src/pasta_fq_plonk_index.rs | 35 +++++++++++++------------ 3 files changed, 41 insertions(+), 34 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 3e104b816d7..4c69b5ec8d6 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -36,10 +36,10 @@ pub use oracles::{ fq::{fq_oracles_create, fq_oracles_deep_copy, fq_oracles_dummy}, }; pub use pasta_fp_plonk_index::{ - prover_index_fp_from_bytes, prover_index_fp_to_bytes, WasmPastaFpPlonkIndex, + prover_index_fp_deserialize, prover_index_fp_serialize, WasmPastaFpPlonkIndex, }; pub use pasta_fq_plonk_index::{ - prover_index_fq_from_bytes, prover_index_fq_to_bytes, WasmPastaFqPlonkIndex, + prover_index_fq_deserialize, prover_index_fq_serialize, WasmPastaFqPlonkIndex, }; // pub use plonk_verifier_index::{fp::*, fq::*}; pub use plonk_verifier_index::{ diff --git a/plonk-napi/src/pasta_fp_plonk_index.rs b/plonk-napi/src/pasta_fp_plonk_index.rs index 7aac5dba3dd..58b02ebc45d 100644 --- a/plonk-napi/src/pasta_fp_plonk_index.rs +++ b/plonk-napi/src/pasta_fp_plonk_index.rs @@ -26,7 +26,11 @@ use std::{ use crate::tables::{ lookup_table_fp_from_js, runtime_table_cfg_fp_from_js, JsLookupTableFp, JsRuntimeTableCfgFp, }; -pub struct WasmPastaFpPlonkIndex(pub Box>>); + +#[napi(js_name = "WasmPastaFpPlonkIndex")] +pub struct WasmPastaFpPlonkIndex( + #[napi(skip)] pub Box>>, +); #[derive(Serialize, Deserialize)] struct SerializedProverIndex { @@ -78,21 +82,9 @@ impl WasmPastaFpPlonkIndex { } } -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fp_from_bytes")] -pub fn prover_index_fp_from_bytes( - bytes: Uint8Array, -) -> napi::bindgen_prelude::Result> { - report_native_call(); - - let index = WasmPastaFpPlonkIndex::deserialize_inner(bytes.as_ref()) - .map_err(|e| Error::new(Status::InvalidArg, e))?; - Ok(External::new(index)) -} - -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fp_to_bytes")] -pub fn prover_index_fp_to_bytes( +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fp_serialize")] +pub fn prover_index_fp_serialize( index: &External, ) -> napi::bindgen_prelude::Result { report_native_call(); @@ -103,6 +95,18 @@ pub fn prover_index_fp_to_bytes( Ok(Uint8Array::from(bytes)) } +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fp_deserialize")] +pub fn prover_index_fp_deserialize( + bytes: Uint8Array, +) -> napi::bindgen_prelude::Result> { + report_native_call(); + + let index = WasmPastaFpPlonkIndex::deserialize_inner(bytes.as_ref()) + .map_err(|e| Error::new(Status::InvalidArg, e))?; + Ok(External::new(index)) +} + #[napi(js_name = "caml_pasta_fp_plonk_index_max_degree")] pub fn caml_pasta_fp_plonk_index_max_degree(index: &External) -> i32 { index.0.srs.max_poly_size() as i32 diff --git a/plonk-napi/src/pasta_fq_plonk_index.rs b/plonk-napi/src/pasta_fq_plonk_index.rs index c1fee9f6b1b..dd59c84e537 100644 --- a/plonk-napi/src/pasta_fq_plonk_index.rs +++ b/plonk-napi/src/pasta_fq_plonk_index.rs @@ -22,7 +22,10 @@ use std::{ use crate::tables::{ lookup_table_fq_from_js, runtime_table_cfg_fq_from_js, JsLookupTableFq, JsRuntimeTableCfgFq, }; -pub struct WasmPastaFqPlonkIndex(pub Box>>); +#[napi(js_name = "WasmPastaFqPlonkIndex")] +pub struct WasmPastaFqPlonkIndex( + #[napi(skip)] pub Box>>, +); #[derive(Serialize, Deserialize)] struct SerializedProverIndex { @@ -74,21 +77,9 @@ impl WasmPastaFqPlonkIndex { } } -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fq_from_bytes")] -pub fn prover_index_fq_from_bytes( - bytes: Uint8Array, -) -> napi::bindgen_prelude::Result> { - report_native_call(); - - let index = WasmPastaFqPlonkIndex::deserialize_inner(bytes.as_ref()) - .map_err(|e| Error::new(Status::InvalidArg, e))?; - Ok(External::new(index)) -} - -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fq_to_bytes")] -pub fn prover_index_fq_to_bytes( +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fq_serialize")] +pub fn prover_index_fq_serialize( index: &External, ) -> napi::bindgen_prelude::Result { report_native_call(); @@ -99,6 +90,18 @@ pub fn prover_index_fq_to_bytes( Ok(Uint8Array::from(bytes)) } +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fq_deserialize")] +pub fn prover_index_fq_deserialize( + bytes: Uint8Array, +) -> napi::bindgen_prelude::Result> { + report_native_call(); + + let index = WasmPastaFqPlonkIndex::deserialize_inner(bytes.as_ref()) + .map_err(|e| Error::new(Status::InvalidArg, e))?; + Ok(External::new(index)) +} + #[napi(js_name = "caml_pasta_fq_plonk_index_max_degree")] pub fn caml_pasta_fq_plonk_index_max_degree(index: &External) -> i32 { index.0.srs.max_poly_size() as i32 From da2f09fc8d37b616b075ea2b9fdcd3ed1c0dd8b0 Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 3 Dec 2025 20:02:51 +0100 Subject: [PATCH 20/45] napi: force snake case for struct fields --- plonk-napi/src/plonk_verifier_index.rs | 33 ++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index b1ae1cf8337..e9fa5039494 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -46,7 +46,9 @@ macro_rules! impl_verification_key { #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { + #[napi(js_name = "log_size_of_group")] pub log_size_of_group: i32, + #[napi(js_name = "group_gen")] pub group_gen: $NapiF, } type NapiDomain = []; @@ -70,33 +72,33 @@ macro_rules! impl_verification_key { #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { - #[napi(skip)] + #[napi(skip, js_name = "sigma_comm")] pub sigma_comm: Vec<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "coefficients_comm")] pub coefficients_comm: Vec<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "generic_comm")] pub generic_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "psm_comm")] pub psm_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "complete_add_comm")] pub complete_add_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "mul_comm")] pub mul_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "emul_comm")] pub emul_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "endomul_scalar_comm")] pub endomul_scalar_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "xor_comm")] pub xor_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check0_comm")] pub range_check0_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check1_comm")] pub range_check1_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "foreign_field_add_comm")] pub foreign_field_add_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "foreign_field_mul_comm")] pub foreign_field_mul_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "rot_comm")] pub rot_comm: Option<$NapiPolyComm>, } type NapiPlonkVerificationEvals = []; @@ -156,7 +158,7 @@ macro_rules! impl_verification_key { pub xor: Option<$NapiPolyComm>, #[napi(skip)] pub lookup: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check")] pub range_check: Option<$NapiPolyComm>, #[napi(skip)] pub ffmul: Option<$NapiPolyComm>, @@ -286,6 +288,7 @@ macro_rules! impl_verification_key { #[derive(Clone, Debug, Default)] pub struct [] { pub domain: NapiDomain, + #[napi(js_name = "max_poly_size")] pub max_poly_size: i32, pub public_: i32, pub prev_challenges: i32, From 202e2d022ba63f6fb01ae8404ea5f147cc65b33e Mon Sep 17 00:00:00 2001 From: querolita Date: Wed, 3 Dec 2025 20:04:26 +0100 Subject: [PATCH 21/45] napi: fix h function name --- plonk-napi/src/srs.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plonk-napi/src/srs.rs b/plonk-napi/src/srs.rs index 3981d2e0401..3646d6fd92f 100644 --- a/plonk-napi/src/srs.rs +++ b/plonk-napi/src/srs.rs @@ -215,7 +215,7 @@ macro_rules! impl_srs { domain_size: i32, i: i32, ) -> Option<$NapiPolyComm> { - println!("Getting maybe lagrange commitment with napi"); + println!("Getting maybe lagrange commitment with napi rust"); if !srs .0 .lagrange_bases @@ -223,9 +223,10 @@ macro_rules! impl_srs { { return None; } + println!("Lagrange basis found in cache"); let basis = srs .get_lagrange_basis_from_domain_size(domain_size as usize); - Some(basis[i as usize].clone().into()) + basis.get(i as usize).map(Into::into) } #[napi(js_name = [<"caml_" $name:snake "_srs_set_lagrange_basis">])] @@ -325,8 +326,8 @@ macro_rules! impl_srs { Ok(points.into_iter().map(Into::into).collect()) } - #[napi(js_name = [<"caml_" $name:snake "_srs_get_h">])] - pub fn h(srs: &[]) -> $NapiG { + #[napi(js_name = [<"caml_" $name:snake "_srs_h">])] + pub fn [](srs: &[]) -> $NapiG { println!("Getting h point with napi"); srs.h.into() } From 96f481e3b5399db5a90c4ff599c61652fd353ddd Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 09:29:01 -0300 Subject: [PATCH 22/45] Fix property names of lookup verifier index --- plonk-napi/src/plonk_verifier_index.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index d1ab27ac478..117cb7c68a9 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -490,12 +490,13 @@ macro_rules! impl_verification_key { #[napi(js_name = [])] #[derive(Clone)] pub struct [] { + #[napi(js_name = "joint_lookup_used")] pub joint_lookup_used: bool, #[napi(skip)] pub lookup_table: NapiVector<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "lookup_selectors")] pub lookup_selectors: NapiLookupSelectors, #[napi(skip)] From d363502ec3858a14c22fbd27a92a037de9bce49e Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 10:01:18 -0300 Subject: [PATCH 23/45] Update imports in napi proof.rs --- plonk-napi/src/proof.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index e6687f32f1a..88321684b91 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -755,9 +755,9 @@ pub mod fp { use super::*; use crate::{ pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, + plonk_verifier_index::fp::NapiFpPlonkVerifierIndex, poly_comm::vesta::NapiFpPolyComm, wrappers::{field::NapiPastaFp, group::NapiGVesta}, - NapiFpPlonkVerifierIndex, }; use mina_curves::pasta::{Fp, Vesta}; @@ -778,9 +778,9 @@ pub mod fq { use super::*; use crate::{ pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, + plonk_verifier_index::fq::NapiFqPlonkVerifierIndex, poly_comm::pallas::NapiFqPolyComm, wrappers::{field::NapiPastaFq, group::NapiGPallas}, - NapiFqPlonkVerifierIndex, }; use mina_curves::pasta::{Fq, Pallas}; From e1e44ffce0d0f4087d21ab296ae76ecccdccac71 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 10:54:31 -0300 Subject: [PATCH 24/45] Add plonk verifier index deserialize function --- plonk-napi/src/lib.rs | 8 ++++---- plonk-napi/src/plonk_verifier_index.rs | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index d676d21d2c9..4ea5b1eb01a 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -44,15 +44,15 @@ pub use pasta_fq_plonk_index::{ pub use plonk_verifier_index::{ fp::{ caml_pasta_fp_plonk_verifier_index_create, caml_pasta_fp_plonk_verifier_index_deep_copy, - caml_pasta_fp_plonk_verifier_index_dummy, caml_pasta_fp_plonk_verifier_index_read, + caml_pasta_fp_plonk_verifier_index_deserialize, caml_pasta_fp_plonk_verifier_index_dummy, + caml_pasta_fp_plonk_verifier_index_read, caml_pasta_fp_plonk_verifier_index_serialize, caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, - NapiFpDomain as WasmFpDomain, NapiFpPlonkVerifierIndex as WasmFpPlonkVerifierIndex, }, fq::{ caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, - caml_pasta_fq_plonk_verifier_index_dummy, caml_pasta_fq_plonk_verifier_index_read, + caml_pasta_fq_plonk_verifier_index_deserialize, caml_pasta_fq_plonk_verifier_index_dummy, + caml_pasta_fq_plonk_verifier_index_read, caml_pasta_fq_plonk_verifier_index_serialize, caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, - NapiFqDomain as WasmFqDomain, NapiFqPlonkVerifierIndex as WasmFqPlonkVerifierIndex, }, }; diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 117cb7c68a9..7bc5893015e 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -946,17 +946,17 @@ macro_rules! impl_verification_key { serde_json::to_string(&index).unwrap() } - // #[napi(js_name = [<$name:snake _deserialize>])] - // pub fn [<$name:snake _deserialize>]( - // srs: &$NapiSrs, - // index: String, - // ) -> Result { - // let vi: Result>, serde_json::Error> = serde_json::from_str(&index); - // match vi { - // Ok(vi) => Ok(to_napi(srs, vi)), - // Err(e) => Err(JsError::new(&(e.to_string()))), - // } - // } + #[napi(js_name = [<$name:snake _deserialize>])] + pub fn [<$name:snake _deserialize>]( + srs: &$NapiSrs, + index: String, + ) -> napi::Result { + let vi = serde_json::from_str::>>(&index); + match vi { + Ok(vi) => Ok(to_napi(srs, vi)), + Err(e) => Err(NapiError::new(Status::GenericFailure, format!("deserialize: {}", e))), + } + } #[napi(js_name = [<$name:snake _create>])] pub fn [<$name:snake _create>]( From 77e147f41b3e86c6f0bad0897bc046d525ca8e8d Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 11:38:44 -0300 Subject: [PATCH 25/45] napi fixes for past plonk index --- plonk-napi/src/lib.rs | 4 +-- plonk-napi/src/pasta_fp_plonk_index.rs | 45 ++++++++++++++------------ plonk-napi/src/pasta_fq_plonk_index.rs | 35 +++++++++++--------- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 4ea5b1eb01a..0557447fe92 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -36,10 +36,10 @@ pub use oracles::{ fq::{fq_oracles_create, fq_oracles_deep_copy, fq_oracles_dummy}, }; pub use pasta_fp_plonk_index::{ - prover_index_fp_from_bytes, prover_index_fp_to_bytes, WasmPastaFpPlonkIndex, + prover_index_fp_deserialize, prover_index_fp_serialize, WasmPastaFpPlonkIndex, }; pub use pasta_fq_plonk_index::{ - prover_index_fq_from_bytes, prover_index_fq_to_bytes, WasmPastaFqPlonkIndex, + prover_index_fq_deserialize, prover_index_fq_serialize, WasmPastaFqPlonkIndex, }; pub use plonk_verifier_index::{ fp::{ diff --git a/plonk-napi/src/pasta_fp_plonk_index.rs b/plonk-napi/src/pasta_fp_plonk_index.rs index 285a93db46b..58b02ebc45d 100644 --- a/plonk-napi/src/pasta_fp_plonk_index.rs +++ b/plonk-napi/src/pasta_fp_plonk_index.rs @@ -26,7 +26,11 @@ use std::{ use crate::tables::{ lookup_table_fp_from_js, runtime_table_cfg_fp_from_js, JsLookupTableFp, JsRuntimeTableCfgFp, }; -pub struct WasmPastaFpPlonkIndex(pub External>>); + +#[napi(js_name = "WasmPastaFpPlonkIndex")] +pub struct WasmPastaFpPlonkIndex( + #[napi(skip)] pub Box>>, +); #[derive(Serialize, Deserialize)] struct SerializedProverIndex { @@ -74,25 +78,13 @@ impl WasmPastaFpPlonkIndex { DefaultFqSponge, >(); - Ok(WasmPastaFpPlonkIndex(External::new(index))) + Ok(WasmPastaFpPlonkIndex(Box::new(index))) } } -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fp_from_bytes")] -pub fn prover_index_fp_from_bytes( - bytes: Uint8Array, -) -> napi::bindgen_prelude::Result> { - report_native_call(); - - let index = WasmPastaFpPlonkIndex::deserialize_inner(bytes.as_ref()) - .map_err(|e| Error::new(Status::InvalidArg, e))?; - Ok(External::new(index)) -} - -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fp_to_bytes")] -pub fn prover_index_fp_to_bytes( +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fp_serialize")] +pub fn prover_index_fp_serialize( index: &External, ) -> napi::bindgen_prelude::Result { report_native_call(); @@ -103,6 +95,18 @@ pub fn prover_index_fp_to_bytes( Ok(Uint8Array::from(bytes)) } +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fp_deserialize")] +pub fn prover_index_fp_deserialize( + bytes: Uint8Array, +) -> napi::bindgen_prelude::Result> { + report_native_call(); + + let index = WasmPastaFpPlonkIndex::deserialize_inner(bytes.as_ref()) + .map_err(|e| Error::new(Status::InvalidArg, e))?; + Ok(External::new(index)) +} + #[napi(js_name = "caml_pasta_fp_plonk_index_max_degree")] pub fn caml_pasta_fp_plonk_index_max_degree(index: &External) -> i32 { index.0.srs.max_poly_size() as i32 @@ -183,7 +187,7 @@ pub fn caml_pasta_fp_plonk_index_create( ); index.compute_verifier_index_digest::>(); - Ok(External::new(WasmPastaFpPlonkIndex(External::new(index)))) + Ok(External::new(WasmPastaFpPlonkIndex(Box::new(index)))) } #[napi(js_name = "caml_pasta_fp_plonk_index_decode")] @@ -205,7 +209,7 @@ pub fn caml_pasta_fp_plonk_index_decode( index.linearization = linearization; index.powers_of_alpha = powers_of_alpha; - Ok(External::new(WasmPastaFpPlonkIndex(External::new(index)))) + Ok(External::new(WasmPastaFpPlonkIndex(Box::new(index)))) } #[napi(js_name = "caml_pasta_fp_plonk_index_encode")] @@ -288,5 +292,6 @@ pub fn caml_pasta_fp_plonk_index_read( t.linearization = linearization; t.powers_of_alpha = powers_of_alpha; - Ok(External::new(WasmPastaFpPlonkIndex(External::new(t)))) + // + Ok(External::new(WasmPastaFpPlonkIndex(Box::new(t)))) } diff --git a/plonk-napi/src/pasta_fq_plonk_index.rs b/plonk-napi/src/pasta_fq_plonk_index.rs index c1fee9f6b1b..dd59c84e537 100644 --- a/plonk-napi/src/pasta_fq_plonk_index.rs +++ b/plonk-napi/src/pasta_fq_plonk_index.rs @@ -22,7 +22,10 @@ use std::{ use crate::tables::{ lookup_table_fq_from_js, runtime_table_cfg_fq_from_js, JsLookupTableFq, JsRuntimeTableCfgFq, }; -pub struct WasmPastaFqPlonkIndex(pub Box>>); +#[napi(js_name = "WasmPastaFqPlonkIndex")] +pub struct WasmPastaFqPlonkIndex( + #[napi(skip)] pub Box>>, +); #[derive(Serialize, Deserialize)] struct SerializedProverIndex { @@ -74,21 +77,9 @@ impl WasmPastaFqPlonkIndex { } } -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fq_from_bytes")] -pub fn prover_index_fq_from_bytes( - bytes: Uint8Array, -) -> napi::bindgen_prelude::Result> { - report_native_call(); - - let index = WasmPastaFqPlonkIndex::deserialize_inner(bytes.as_ref()) - .map_err(|e| Error::new(Status::InvalidArg, e))?; - Ok(External::new(index)) -} - -// TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around -#[napi(js_name = "prover_index_fq_to_bytes")] -pub fn prover_index_fq_to_bytes( +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fq_serialize")] +pub fn prover_index_fq_serialize( index: &External, ) -> napi::bindgen_prelude::Result { report_native_call(); @@ -99,6 +90,18 @@ pub fn prover_index_fq_to_bytes( Ok(Uint8Array::from(bytes)) } +// TODO: remove incl all dependencies when no longer needed and we only pass napi objects around +#[napi(js_name = "prover_index_fq_deserialize")] +pub fn prover_index_fq_deserialize( + bytes: Uint8Array, +) -> napi::bindgen_prelude::Result> { + report_native_call(); + + let index = WasmPastaFqPlonkIndex::deserialize_inner(bytes.as_ref()) + .map_err(|e| Error::new(Status::InvalidArg, e))?; + Ok(External::new(index)) +} + #[napi(js_name = "caml_pasta_fq_plonk_index_max_degree")] pub fn caml_pasta_fq_plonk_index_max_degree(index: &External) -> i32 { index.0.srs.max_poly_size() as i32 From 9241d26ba83b975811e98cfc151856cd00873c66 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 11:51:30 -0300 Subject: [PATCH 26/45] Export napi pasta plonk index functions --- plonk-napi/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 0557447fe92..5f9eb9a15c5 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -36,10 +36,20 @@ pub use oracles::{ fq::{fq_oracles_create, fq_oracles_deep_copy, fq_oracles_dummy}, }; pub use pasta_fp_plonk_index::{ - prover_index_fp_deserialize, prover_index_fp_serialize, WasmPastaFpPlonkIndex, + caml_pasta_fp_plonk_index_create, caml_pasta_fp_plonk_index_decode, + caml_pasta_fp_plonk_index_domain_d1_size, caml_pasta_fp_plonk_index_domain_d4_size, + caml_pasta_fp_plonk_index_domain_d8_size, caml_pasta_fp_plonk_index_encode, + caml_pasta_fp_plonk_index_max_degree, caml_pasta_fp_plonk_index_public_inputs, + caml_pasta_fp_plonk_index_read, caml_pasta_fp_plonk_index_write, prover_index_fp_deserialize, + prover_index_fp_serialize, WasmPastaFpPlonkIndex, }; pub use pasta_fq_plonk_index::{ - prover_index_fq_deserialize, prover_index_fq_serialize, WasmPastaFqPlonkIndex, + caml_pasta_fq_plonk_index_create, caml_pasta_fq_plonk_index_decode, + caml_pasta_fq_plonk_index_domain_d1_size, caml_pasta_fq_plonk_index_domain_d4_size, + caml_pasta_fq_plonk_index_domain_d8_size, caml_pasta_fq_plonk_index_encode, + caml_pasta_fq_plonk_index_max_degree, caml_pasta_fq_plonk_index_public_inputs, + caml_pasta_fq_plonk_index_read, caml_pasta_fq_plonk_index_write, prover_index_fq_deserialize, + prover_index_fq_serialize, WasmPastaFqPlonkIndex, }; pub use plonk_verifier_index::{ fp::{ From 01d188b285dec735f6470c6d3921e2da57c5e9a3 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 19:18:27 -0300 Subject: [PATCH 27/45] Add attribute to napi lookup verifier index --- plonk-napi/src/plonk_verifier_index.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 7bc5893015e..29f7b249eda 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -24,6 +24,7 @@ use poly_commitment::{ ipa::{OpeningProof, SRS}, SRS as _, }; +use serde::{Deserialize, Serialize}; use std::{path::Path, sync::Arc}; macro_rules! impl_verification_key { @@ -361,7 +362,7 @@ macro_rules! impl_verification_key { } #[napi] - #[derive(Clone)] + #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(skip)] pub xor: Option<$NapiPolyComm>, @@ -488,7 +489,7 @@ macro_rules! impl_verification_key { } #[napi(js_name = [])] - #[derive(Clone)] + #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(js_name = "joint_lookup_used")] pub joint_lookup_used: bool, @@ -657,7 +658,7 @@ macro_rules! impl_verification_key { #[napi(skip)] pub evals: NapiPlonkVerificationEvals, pub shifts: NapiShifts, - #[napi(skip)] + #[napi(skip, js_name = "lookup_index")] pub lookup_index: Option, pub zk_rows: i32, } From c22f062ec592a77ba53863dc2e490a1764092f24 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 19:33:42 -0300 Subject: [PATCH 28/45] Define NapiLookupVerifierIndex and PlonkVerifierIndex as napi objects --- plonk-napi/src/plonk_verifier_index.rs | 269 +++++++++++++------------ 1 file changed, 135 insertions(+), 134 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 29f7b249eda..47d4a93c3d5 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -488,7 +488,7 @@ macro_rules! impl_verification_key { } } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(js_name = "joint_lookup_used")] @@ -564,92 +564,93 @@ macro_rules! impl_verification_key { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - joint_lookup_used: bool, - lookup_table: NapiVector<$NapiPolyComm>, - lookup_selectors: NapiLookupSelectors, - table_ids: Option<$NapiPolyComm>, - lookup_info: NapiLookupInfo, - runtime_tables_selector: Option<$NapiPolyComm> - ) -> NapiLookupVerifierIndex { - NapiLookupVerifierIndex { - joint_lookup_used, - lookup_table, - lookup_selectors, - table_ids, - lookup_info: lookup_info.clone(), - runtime_tables_selector - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new( + // joint_lookup_used: bool, + // lookup_table: NapiVector<$NapiPolyComm>, + // lookup_selectors: NapiLookupSelectors, + // table_ids: Option<$NapiPolyComm>, + // lookup_info: NapiLookupInfo, + // runtime_tables_selector: Option<$NapiPolyComm> + // ) -> NapiLookupVerifierIndex { + // NapiLookupVerifierIndex { + // joint_lookup_used, + // lookup_table, + // lookup_selectors, + // table_ids, + // lookup_info: lookup_info.clone(), + // runtime_tables_selector + // } + // } - #[napi(getter, js_name = "lookup_table")] - pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { - self.lookup_table.clone() - } + // #[napi(getter, js_name = "lookup_table")] + // pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { + // self.lookup_table.clone() + // } - #[napi(setter, js_name = "set_lookup_table")] - pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { - self.lookup_table = x - } + // #[napi(setter, js_name = "set_lookup_table")] + // pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.lookup_table = x + // } - #[napi(getter, js_name = "lookup_selectors")] - pub fn lookup_selectors(&self) -> NapiLookupSelectors { - self.lookup_selectors.clone() - } + // #[napi(getter, js_name = "lookup_selectors")] + // pub fn lookup_selectors(&self) -> NapiLookupSelectors { + // self.lookup_selectors.clone() + // } - #[napi(setter, js_name = "set_lookup_selectors")] - pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { - self.lookup_selectors = x - } + // #[napi(setter, js_name = "set_lookup_selectors")] + // pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { + // self.lookup_selectors = x + // } - #[napi(getter, js_name = "table_ids")] - pub fn table_ids(&self) -> Option<$NapiPolyComm>{ - self.table_ids.clone() - } + // #[napi(getter, js_name = "table_ids")] + // pub fn table_ids(&self) -> Option<$NapiPolyComm>{ + // self.table_ids.clone() + // } - #[napi(setter, js_name = "set_table_ids")] - pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { - self.table_ids = x - } + // #[napi(setter, js_name = "set_table_ids")] + // pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { + // self.table_ids = x + // } - #[napi(getter, js_name = "lookup_info")] - pub fn lookup_info(&self) -> NapiLookupInfo { - self.lookup_info.clone() - } + // #[napi(getter, js_name = "lookup_info")] + // pub fn lookup_info(&self) -> NapiLookupInfo { + // self.lookup_info.clone() + // } - #[napi(setter, js_name = "set_lookup_info")] - pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { - self.lookup_info = x - } + // #[napi(setter, js_name = "set_lookup_info")] + // pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { + // self.lookup_info = x + // } - #[napi(getter, js_name = "runtime_tables_selector")] - pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { - self.runtime_tables_selector.clone() - } + // #[napi(getter, js_name = "runtime_tables_selector")] + // pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { + // self.runtime_tables_selector.clone() + // } - #[napi(setter, js_name = "set_runtime_tables_selector")] - pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { - self.runtime_tables_selector = x - } - } + // #[napi(setter, js_name = "set_runtime_tables_selector")] + // pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { + // self.runtime_tables_selector = x + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { pub domain: NapiDomain, + #[napi(js_name = "max_poly_size")] pub max_poly_size: i32, pub public_: i32, pub prev_challenges: i32, @@ -665,74 +666,74 @@ macro_rules! impl_verification_key { type NapiPlonkVerifierIndex = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - #[allow(clippy::too_many_arguments)] - pub fn new( - domain: &NapiDomain, - max_poly_size: i32, - public_: i32, - prev_challenges: i32, - srs: &$NapiSrs, - evals: &NapiPlonkVerificationEvals, - shifts: &NapiShifts, - lookup_index: Option, - zk_rows: i32, - ) -> Self { - NapiPlonkVerifierIndex { - domain: domain.clone(), - max_poly_size, - public_, - prev_challenges, - srs: srs.clone(), - evals: evals.clone(), - shifts: shifts.clone(), - lookup_index: lookup_index.clone(), - zk_rows, - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // #[allow(clippy::too_many_arguments)] + // pub fn new( + // domain: &NapiDomain, + // max_poly_size: i32, + // public_: i32, + // prev_challenges: i32, + // srs: &$NapiSrs, + // evals: &NapiPlonkVerificationEvals, + // shifts: &NapiShifts, + // lookup_index: Option, + // zk_rows: i32, + // ) -> Self { + // NapiPlonkVerifierIndex { + // domain: domain.clone(), + // max_poly_size, + // public_, + // prev_challenges, + // srs: srs.clone(), + // evals: evals.clone(), + // shifts: shifts.clone(), + // lookup_index: lookup_index.clone(), + // zk_rows, + // } + // } - #[napi(getter)] - pub fn srs(&self) -> $NapiSrs { - self.srs.clone() - } + // #[napi(getter)] + // pub fn srs(&self) -> $NapiSrs { + // self.srs.clone() + // } - #[napi(setter, js_name = "set_srs")] - pub fn set_srs(&mut self, x: $NapiSrs) { - self.srs = x - } + // #[napi(setter, js_name = "set_srs")] + // pub fn set_srs(&mut self, x: $NapiSrs) { + // self.srs = x + // } - #[napi(getter)] - pub fn evals(&self) -> NapiPlonkVerificationEvals { - self.evals.clone() - } + // #[napi(getter)] + // pub fn evals(&self) -> NapiPlonkVerificationEvals { + // self.evals.clone() + // } - #[napi(setter, js_name = "set_evals")] - pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { - self.evals = x - } + // #[napi(setter, js_name = "set_evals")] + // pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { + // self.evals = x + // } - #[napi(getter, js_name = "lookup_index")] - pub fn lookup_index(&self) -> Option { - self.lookup_index.clone() - } + // #[napi(getter, js_name = "lookup_index")] + // pub fn lookup_index(&self) -> Option { + // self.lookup_index.clone() + // } - #[napi(setter, js_name = "set_lookup_index")] - pub fn set_lookup_index(&mut self, li: Option) { - self.lookup_index = li - } - } + // #[napi(setter, js_name = "set_lookup_index")] + // pub fn set_lookup_index(&mut self, li: Option) { + // self.lookup_index = li + // } + // } pub fn to_napi( srs: &Arc>, @@ -1064,7 +1065,7 @@ macro_rules! impl_verification_key { #[napi(js_name = [<$name:snake _deep_copy>])] pub fn [<$name:snake _deep_copy>]( - x: &NapiPlonkVerifierIndex, + x: NapiPlonkVerifierIndex, ) -> NapiPlonkVerifierIndex { x.clone() } From c5eb21dd953a3f015d16d80ac036b8309fce042c Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 19:45:07 -0300 Subject: [PATCH 29/45] Update napi exports --- plonk-napi/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 5f9eb9a15c5..477794aebf6 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -57,12 +57,14 @@ pub use plonk_verifier_index::{ caml_pasta_fp_plonk_verifier_index_deserialize, caml_pasta_fp_plonk_verifier_index_dummy, caml_pasta_fp_plonk_verifier_index_read, caml_pasta_fp_plonk_verifier_index_serialize, caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fp_plonk_verifier_index_write, + NapiFpLookupVerifierIndex, NapiFpPlonkVerifierIndex, }, fq::{ caml_pasta_fq_plonk_verifier_index_create, caml_pasta_fq_plonk_verifier_index_deep_copy, caml_pasta_fq_plonk_verifier_index_deserialize, caml_pasta_fq_plonk_verifier_index_dummy, caml_pasta_fq_plonk_verifier_index_read, caml_pasta_fq_plonk_verifier_index_serialize, caml_pasta_fq_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_write, + NapiFqLookupVerifierIndex, NapiFqPlonkVerifierIndex, }, }; From c5f0a6e28ec8199a760fa94700025637cb6aa156 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 20:05:28 -0300 Subject: [PATCH 30/45] Unskip lookup_info property --- plonk-napi/src/plonk_verifier_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 47d4a93c3d5..613d20683e5 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -659,7 +659,7 @@ macro_rules! impl_verification_key { #[napi(skip)] pub evals: NapiPlonkVerificationEvals, pub shifts: NapiShifts, - #[napi(skip, js_name = "lookup_index")] + #[napi(js_name = "lookup_index")] pub lookup_index: Option, pub zk_rows: i32, } From 4e55e521b800bf0bee5603275831580af4c9c177 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 20:50:34 -0300 Subject: [PATCH 31/45] Forgot name for lookup selectors and converted other napi classes to objects --- plonk-napi/src/plonk_verifier_index.rs | 466 ++++++++++++------------- 1 file changed, 233 insertions(+), 233 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 613d20683e5..6fffcf82a05 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -43,7 +43,7 @@ macro_rules! impl_verification_key { $field_name: ident ) => { paste! { - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone, Copy)] pub struct [] { #[napi(js_name = "log_size_of_group")] @@ -53,13 +53,13 @@ macro_rules! impl_verification_key { } type NapiDomain = []; - #[napi] - impl [] { - #[napi(constructor)] - pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { - NapiDomain {log_size_of_group, group_gen} - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { + // NapiDomain {log_size_of_group, group_gen} + // } + // } impl From for Domain<$F> { fn from(domain: NapiDomain) -> Self { @@ -68,252 +68,252 @@ macro_rules! impl_verification_key { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - impl<'a> ToNapiValue for &'a mut [] { - unsafe fn to_napi_value( - env: sys::napi_env, - val: Self, - ) -> Result { - <[] as ToNapiValue>::to_napi_value(env, val.clone()) - } - } + // impl<'a> ToNapiValue for &'a mut [] { + // unsafe fn to_napi_value( + // env: sys::napi_env, + // val: Self, + // ) -> Result { + // <[] as ToNapiValue>::to_napi_value(env, val.clone()) + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { - #[napi(skip)] + #[napi(skip, js_name = "sigma_comm")] pub sigma_comm: NapiVector<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "coefficients_comm")] pub coefficients_comm: NapiVector<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "generic_comm")] pub generic_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "psm_comm")] pub psm_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "complete_add_comm")] pub complete_add_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "mul_comm")] pub mul_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "emul_comm")] pub emul_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "endomul_scalar_comm")] pub endomul_scalar_comm: $NapiPolyComm, - #[napi(skip)] + #[napi(skip, js_name = "xor_comm")] pub xor_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check0_comm")] pub range_check0_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check1_comm")] pub range_check1_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "foreign_field_add_comm")] pub foreign_field_add_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "foreign_field_mul_comm")] pub foreign_field_mul_comm: Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "rot_comm")] pub rot_comm: Option<$NapiPolyComm> } type NapiPlonkVerificationEvals = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[allow(clippy::too_many_arguments)] - #[napi(constructor)] - pub fn new( - sigma_comm: NapiVector<$NapiPolyComm>, - coefficients_comm: NapiVector<$NapiPolyComm>, - generic_comm: &$NapiPolyComm, - psm_comm: &$NapiPolyComm, - complete_add_comm: &$NapiPolyComm, - mul_comm: &$NapiPolyComm, - emul_comm: &$NapiPolyComm, - endomul_scalar_comm: &$NapiPolyComm, - xor_comm: Option<$NapiPolyComm>, - range_check0_comm: Option<$NapiPolyComm>, - range_check1_comm: Option<$NapiPolyComm>, - foreign_field_add_comm: Option<$NapiPolyComm>, - foreign_field_mul_comm: Option<$NapiPolyComm>, - rot_comm: Option<$NapiPolyComm>, - ) -> Self { - NapiPlonkVerificationEvals { - sigma_comm: sigma_comm.clone(), - coefficients_comm: coefficients_comm.clone(), - generic_comm: generic_comm.clone(), - psm_comm: psm_comm.clone(), - complete_add_comm: complete_add_comm.clone(), - mul_comm: mul_comm.clone(), - emul_comm: emul_comm.clone(), - endomul_scalar_comm: endomul_scalar_comm.clone(), - xor_comm: xor_comm.clone(), - range_check0_comm: range_check0_comm.clone(), - range_check1_comm: range_check1_comm.clone(), - foreign_field_mul_comm: foreign_field_mul_comm.clone(), - foreign_field_add_comm: foreign_field_add_comm.clone(), - rot_comm: rot_comm.clone(), - } - } + // #[napi] + // impl [] { + // #[allow(clippy::too_many_arguments)] + // #[napi(constructor)] + // pub fn new( + // sigma_comm: NapiVector<$NapiPolyComm>, + // coefficients_comm: NapiVector<$NapiPolyComm>, + // generic_comm: &$NapiPolyComm, + // psm_comm: &$NapiPolyComm, + // complete_add_comm: &$NapiPolyComm, + // mul_comm: &$NapiPolyComm, + // emul_comm: &$NapiPolyComm, + // endomul_scalar_comm: &$NapiPolyComm, + // xor_comm: Option<$NapiPolyComm>, + // range_check0_comm: Option<$NapiPolyComm>, + // range_check1_comm: Option<$NapiPolyComm>, + // foreign_field_add_comm: Option<$NapiPolyComm>, + // foreign_field_mul_comm: Option<$NapiPolyComm>, + // rot_comm: Option<$NapiPolyComm>, + // ) -> Self { + // NapiPlonkVerificationEvals { + // sigma_comm: sigma_comm.clone(), + // coefficients_comm: coefficients_comm.clone(), + // generic_comm: generic_comm.clone(), + // psm_comm: psm_comm.clone(), + // complete_add_comm: complete_add_comm.clone(), + // mul_comm: mul_comm.clone(), + // emul_comm: emul_comm.clone(), + // endomul_scalar_comm: endomul_scalar_comm.clone(), + // xor_comm: xor_comm.clone(), + // range_check0_comm: range_check0_comm.clone(), + // range_check1_comm: range_check1_comm.clone(), + // foreign_field_mul_comm: foreign_field_mul_comm.clone(), + // foreign_field_add_comm: foreign_field_add_comm.clone(), + // rot_comm: rot_comm.clone(), + // } + // } - #[napi(getter, js_name = "sigma_comm")] - pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { - self.sigma_comm.clone() - } + // #[napi(getter, js_name = "sigma_comm")] + // pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { + // self.sigma_comm.clone() + // } - #[napi(setter, js_name = "set_sigma_comm")] - pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - self.sigma_comm = x; - } + // #[napi(setter, js_name = "set_sigma_comm")] + // pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.sigma_comm = x; + // } - #[napi(getter, js_name = "coefficients_comm")] - pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { - self.coefficients_comm.clone() - } + // #[napi(getter, js_name = "coefficients_comm")] + // pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { + // self.coefficients_comm.clone() + // } - #[napi(setter, js_name = "set_coefficients_comm")] - pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - self.coefficients_comm = x; - } + // #[napi(setter, js_name = "set_coefficients_comm")] + // pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.coefficients_comm = x; + // } - #[napi(getter, js_name = "generic_comm")] - pub fn generic_comm(&self) -> $NapiPolyComm { - self.generic_comm.clone() - } + // #[napi(getter, js_name = "generic_comm")] + // pub fn generic_comm(&self) -> $NapiPolyComm { + // self.generic_comm.clone() + // } - #[napi(setter, js_name = "set_generic_comm")] - pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { - self.generic_comm = x; - } + // #[napi(setter, js_name = "set_generic_comm")] + // pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { + // self.generic_comm = x; + // } - #[napi(getter, js_name = "psm_comm")] - pub fn psm_comm(&self) -> $NapiPolyComm { - self.psm_comm.clone() - } + // #[napi(getter, js_name = "psm_comm")] + // pub fn psm_comm(&self) -> $NapiPolyComm { + // self.psm_comm.clone() + // } - #[napi(setter, js_name = "set_psm_comm")] - pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { - self.psm_comm = x; - } + // #[napi(setter, js_name = "set_psm_comm")] + // pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { + // self.psm_comm = x; + // } - #[napi(getter, js_name = "complete_add_comm")] - pub fn complete_add_comm(&self) -> $NapiPolyComm { - self.complete_add_comm.clone() - } + // #[napi(getter, js_name = "complete_add_comm")] + // pub fn complete_add_comm(&self) -> $NapiPolyComm { + // self.complete_add_comm.clone() + // } - #[napi(setter, js_name = "set_complete_add_comm")] - pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { - self.complete_add_comm = x; - } + // #[napi(setter, js_name = "set_complete_add_comm")] + // pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { + // self.complete_add_comm = x; + // } - #[napi(getter, js_name = "mul_comm")] - pub fn mul_comm(&self) -> $NapiPolyComm { - self.mul_comm.clone() - } + // #[napi(getter, js_name = "mul_comm")] + // pub fn mul_comm(&self) -> $NapiPolyComm { + // self.mul_comm.clone() + // } - #[napi(setter, js_name = "set_mul_comm")] - pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { - self.mul_comm = x; - } + // #[napi(setter, js_name = "set_mul_comm")] + // pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { + // self.mul_comm = x; + // } - #[napi(getter, js_name = "emul_comm")] - pub fn emul_comm(&self) -> $NapiPolyComm { - self.emul_comm.clone() - } + // #[napi(getter, js_name = "emul_comm")] + // pub fn emul_comm(&self) -> $NapiPolyComm { + // self.emul_comm.clone() + // } - #[napi(setter, js_name = "set_emul_comm")] - pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { - self.emul_comm = x; - } + // #[napi(setter, js_name = "set_emul_comm")] + // pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { + // self.emul_comm = x; + // } - #[napi(getter, js_name = "endomul_scalar_comm")] - pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { - self.endomul_scalar_comm.clone() - } + // #[napi(getter, js_name = "endomul_scalar_comm")] + // pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { + // self.endomul_scalar_comm.clone() + // } - #[napi(setter, js_name = "set_endomul_scalar_comm")] - pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { - self.endomul_scalar_comm = x; - } + // #[napi(setter, js_name = "set_endomul_scalar_comm")] + // pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { + // self.endomul_scalar_comm = x; + // } - #[napi(getter, js_name = "xor_comm")] - pub fn xor_comm(&self) -> Option<$NapiPolyComm> { - self.xor_comm.clone() - } + // #[napi(getter, js_name = "xor_comm")] + // pub fn xor_comm(&self) -> Option<$NapiPolyComm> { + // self.xor_comm.clone() + // } - #[napi(setter, js_name = "set_xor_comm")] - pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { - self.xor_comm = x; - } + // #[napi(setter, js_name = "set_xor_comm")] + // pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.xor_comm = x; + // } - #[napi(getter, js_name = "rot_comm")] - pub fn rot_comm(&self) -> Option<$NapiPolyComm> { - self.rot_comm.clone() - } + // #[napi(getter, js_name = "rot_comm")] + // pub fn rot_comm(&self) -> Option<$NapiPolyComm> { + // self.rot_comm.clone() + // } - #[napi(setter, js_name = "set_rot_comm")] - pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { - self.rot_comm = x; - } + // #[napi(setter, js_name = "set_rot_comm")] + // pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.rot_comm = x; + // } - #[napi(getter, js_name = "range_check0_comm")] - pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { - self.range_check0_comm.clone() - } + // #[napi(getter, js_name = "range_check0_comm")] + // pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { + // self.range_check0_comm.clone() + // } - #[napi(setter, js_name = "set_range_check0_comm")] - pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check0_comm = x; - } + // #[napi(setter, js_name = "set_range_check0_comm")] + // pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.range_check0_comm = x; + // } - #[napi(getter, js_name = "range_check1_comm")] - pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { - self.range_check1_comm.clone() - } + // #[napi(getter, js_name = "range_check1_comm")] + // pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { + // self.range_check1_comm.clone() + // } - #[napi(setter, js_name = "set_range_check1_comm")] - pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check1_comm = x; - } + // #[napi(setter, js_name = "set_range_check1_comm")] + // pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.range_check1_comm = x; + // } - #[napi(getter, js_name = "foreign_field_add_comm")] - pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_add_comm.clone() - } + // #[napi(getter, js_name = "foreign_field_add_comm")] + // pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { + // self.foreign_field_add_comm.clone() + // } - #[napi(setter, js_name = "set_foreign_field_add_comm")] - pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_add_comm = x; - } + // #[napi(setter, js_name = "set_foreign_field_add_comm")] + // pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.foreign_field_add_comm = x; + // } - #[napi(getter, js_name = "foreign_field_mul_comm")] - pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_mul_comm.clone() - } + // #[napi(getter, js_name = "foreign_field_mul_comm")] + // pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { + // self.foreign_field_mul_comm.clone() + // } - #[napi(setter, js_name = "set_foreign_field_mul_comm")] - pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_mul_comm = x; - } + // #[napi(setter, js_name = "set_foreign_field_mul_comm")] + // pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.foreign_field_mul_comm = x; + // } - } + // } #[derive(Clone, Copy)] - #[napi(js_name = [])] + #[napi(object, js_name = [])] pub struct [] { pub s0: $NapiF, pub s1: $NapiF, @@ -326,42 +326,42 @@ macro_rules! impl_verification_key { type NapiShifts = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - impl<'a> ToNapiValue for &'a mut [] { - unsafe fn to_napi_value( - env: sys::napi_env, - val: Self, - ) -> Result { - <[] as ToNapiValue>::to_napi_value(env, val.clone()) - } - } + // impl<'a> ToNapiValue for &'a mut [] { + // unsafe fn to_napi_value( + // env: sys::napi_env, + // val: Self, + // ) -> Result { + // <[] as ToNapiValue>::to_napi_value(env, val.clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - s0: $NapiF, - s1: $NapiF, - s2: $NapiF, - s3: $NapiF, - s4: $NapiF, - s5: $NapiF, - s6: $NapiF - ) -> Self { - Self { s0, s1, s2, s3, s4, s5, s6 } - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new( + // s0: $NapiF, + // s1: $NapiF, + // s2: $NapiF, + // s3: $NapiF, + // s4: $NapiF, + // s5: $NapiF, + // s6: $NapiF + // ) -> Self { + // Self { s0, s1, s2, s3, s4, s5, s6 } + // } + // } - #[napi] + #[napi(js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(skip)] From 35123e4d06d2a7b3228e9460316552056bd6a81b Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 20:56:59 -0300 Subject: [PATCH 32/45] Fix napi srs_maybe_lagrange_commitment --- plonk-napi/src/srs.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plonk-napi/src/srs.rs b/plonk-napi/src/srs.rs index 3981d2e0401..3646d6fd92f 100644 --- a/plonk-napi/src/srs.rs +++ b/plonk-napi/src/srs.rs @@ -215,7 +215,7 @@ macro_rules! impl_srs { domain_size: i32, i: i32, ) -> Option<$NapiPolyComm> { - println!("Getting maybe lagrange commitment with napi"); + println!("Getting maybe lagrange commitment with napi rust"); if !srs .0 .lagrange_bases @@ -223,9 +223,10 @@ macro_rules! impl_srs { { return None; } + println!("Lagrange basis found in cache"); let basis = srs .get_lagrange_basis_from_domain_size(domain_size as usize); - Some(basis[i as usize].clone().into()) + basis.get(i as usize).map(Into::into) } #[napi(js_name = [<"caml_" $name:snake "_srs_set_lagrange_basis">])] @@ -325,8 +326,8 @@ macro_rules! impl_srs { Ok(points.into_iter().map(Into::into).collect()) } - #[napi(js_name = [<"caml_" $name:snake "_srs_get_h">])] - pub fn h(srs: &[]) -> $NapiG { + #[napi(js_name = [<"caml_" $name:snake "_srs_h">])] + pub fn [](srs: &[]) -> $NapiG { println!("Getting h point with napi"); srs.h.into() } From 57e99b5d849052089bfced17a077ce93cb6d3693 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 21:21:50 -0300 Subject: [PATCH 33/45] Revert plonk verifier index objects to classes --- plonk-napi/src/plonk_verifier_index.rs | 706 ++++++++++++------------- 1 file changed, 352 insertions(+), 354 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 6fffcf82a05..9f97ecca9e3 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -43,7 +43,7 @@ macro_rules! impl_verification_key { $field_name: ident ) => { paste! { - #[napi(object, js_name = [])] + #[napi(js_name = [])] #[derive(Clone, Copy)] pub struct [] { #[napi(js_name = "log_size_of_group")] @@ -53,13 +53,13 @@ macro_rules! impl_verification_key { } type NapiDomain = []; - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { - // NapiDomain {log_size_of_group, group_gen} - // } - // } + #[napi] + impl [] { + #[napi(constructor)] + pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { + NapiDomain {log_size_of_group, group_gen} + } + } impl From for Domain<$F> { fn from(domain: NapiDomain) -> Self { @@ -68,26 +68,26 @@ macro_rules! impl_verification_key { } } - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } - // impl<'a> ToNapiValue for &'a mut [] { - // unsafe fn to_napi_value( - // env: sys::napi_env, - // val: Self, - // ) -> Result { - // <[] as ToNapiValue>::to_napi_value(env, val.clone()) - // } - // } + impl<'a> ToNapiValue for &'a mut [] { + unsafe fn to_napi_value( + env: sys::napi_env, + val: Self, + ) -> Result { + <[] as ToNapiValue>::to_napi_value(env, val.clone()) + } + } - #[napi(object, js_name = [])] + #[napi(js_name = [])] #[derive(Clone)] pub struct [] { #[napi(skip, js_name = "sigma_comm")] @@ -122,198 +122,197 @@ macro_rules! impl_verification_key { type NapiPlonkVerificationEvals = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // #[napi] - // impl [] { - // #[allow(clippy::too_many_arguments)] - // #[napi(constructor)] - // pub fn new( - // sigma_comm: NapiVector<$NapiPolyComm>, - // coefficients_comm: NapiVector<$NapiPolyComm>, - // generic_comm: &$NapiPolyComm, - // psm_comm: &$NapiPolyComm, - // complete_add_comm: &$NapiPolyComm, - // mul_comm: &$NapiPolyComm, - // emul_comm: &$NapiPolyComm, - // endomul_scalar_comm: &$NapiPolyComm, - // xor_comm: Option<$NapiPolyComm>, - // range_check0_comm: Option<$NapiPolyComm>, - // range_check1_comm: Option<$NapiPolyComm>, - // foreign_field_add_comm: Option<$NapiPolyComm>, - // foreign_field_mul_comm: Option<$NapiPolyComm>, - // rot_comm: Option<$NapiPolyComm>, - // ) -> Self { - // NapiPlonkVerificationEvals { - // sigma_comm: sigma_comm.clone(), - // coefficients_comm: coefficients_comm.clone(), - // generic_comm: generic_comm.clone(), - // psm_comm: psm_comm.clone(), - // complete_add_comm: complete_add_comm.clone(), - // mul_comm: mul_comm.clone(), - // emul_comm: emul_comm.clone(), - // endomul_scalar_comm: endomul_scalar_comm.clone(), - // xor_comm: xor_comm.clone(), - // range_check0_comm: range_check0_comm.clone(), - // range_check1_comm: range_check1_comm.clone(), - // foreign_field_mul_comm: foreign_field_mul_comm.clone(), - // foreign_field_add_comm: foreign_field_add_comm.clone(), - // rot_comm: rot_comm.clone(), - // } - // } + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } - // #[napi(getter, js_name = "sigma_comm")] - // pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { - // self.sigma_comm.clone() - // } + #[napi] + impl [] { + #[allow(clippy::too_many_arguments)] + #[napi(constructor)] + pub fn new( + sigma_comm: NapiVector<$NapiPolyComm>, + coefficients_comm: NapiVector<$NapiPolyComm>, + generic_comm: &$NapiPolyComm, + psm_comm: &$NapiPolyComm, + complete_add_comm: &$NapiPolyComm, + mul_comm: &$NapiPolyComm, + emul_comm: &$NapiPolyComm, + endomul_scalar_comm: &$NapiPolyComm, + xor_comm: Option<$NapiPolyComm>, + range_check0_comm: Option<$NapiPolyComm>, + range_check1_comm: Option<$NapiPolyComm>, + foreign_field_add_comm: Option<$NapiPolyComm>, + foreign_field_mul_comm: Option<$NapiPolyComm>, + rot_comm: Option<$NapiPolyComm>, + ) -> Self { + NapiPlonkVerificationEvals { + sigma_comm: sigma_comm.clone(), + coefficients_comm: coefficients_comm.clone(), + generic_comm: generic_comm.clone(), + psm_comm: psm_comm.clone(), + complete_add_comm: complete_add_comm.clone(), + mul_comm: mul_comm.clone(), + emul_comm: emul_comm.clone(), + endomul_scalar_comm: endomul_scalar_comm.clone(), + xor_comm: xor_comm.clone(), + range_check0_comm: range_check0_comm.clone(), + range_check1_comm: range_check1_comm.clone(), + foreign_field_mul_comm: foreign_field_mul_comm.clone(), + foreign_field_add_comm: foreign_field_add_comm.clone(), + rot_comm: rot_comm.clone(), + } + } - // #[napi(setter, js_name = "set_sigma_comm")] - // pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.sigma_comm = x; - // } + #[napi(getter, js_name = "sigma_comm")] + pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { + self.sigma_comm.clone() + } - // #[napi(getter, js_name = "coefficients_comm")] - // pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { - // self.coefficients_comm.clone() - // } + #[napi(setter, js_name = "set_sigma_comm")] + pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + self.sigma_comm = x; + } - // #[napi(setter, js_name = "set_coefficients_comm")] - // pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.coefficients_comm = x; - // } + #[napi(getter, js_name = "coefficients_comm")] + pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { + self.coefficients_comm.clone() + } - // #[napi(getter, js_name = "generic_comm")] - // pub fn generic_comm(&self) -> $NapiPolyComm { - // self.generic_comm.clone() - // } + #[napi(setter, js_name = "set_coefficients_comm")] + pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + self.coefficients_comm = x; + } - // #[napi(setter, js_name = "set_generic_comm")] - // pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { - // self.generic_comm = x; - // } + #[napi(getter, js_name = "generic_comm")] + pub fn generic_comm(&self) -> $NapiPolyComm { + self.generic_comm.clone() + } - // #[napi(getter, js_name = "psm_comm")] - // pub fn psm_comm(&self) -> $NapiPolyComm { - // self.psm_comm.clone() - // } + #[napi(setter, js_name = "set_generic_comm")] + pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { + self.generic_comm = x; + } - // #[napi(setter, js_name = "set_psm_comm")] - // pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { - // self.psm_comm = x; - // } + #[napi(getter, js_name = "psm_comm")] + pub fn psm_comm(&self) -> $NapiPolyComm { + self.psm_comm.clone() + } - // #[napi(getter, js_name = "complete_add_comm")] - // pub fn complete_add_comm(&self) -> $NapiPolyComm { - // self.complete_add_comm.clone() - // } + #[napi(setter, js_name = "set_psm_comm")] + pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { + self.psm_comm = x; + } - // #[napi(setter, js_name = "set_complete_add_comm")] - // pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { - // self.complete_add_comm = x; - // } + #[napi(getter, js_name = "complete_add_comm")] + pub fn complete_add_comm(&self) -> $NapiPolyComm { + self.complete_add_comm.clone() + } - // #[napi(getter, js_name = "mul_comm")] - // pub fn mul_comm(&self) -> $NapiPolyComm { - // self.mul_comm.clone() - // } + #[napi(setter, js_name = "set_complete_add_comm")] + pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { + self.complete_add_comm = x; + } - // #[napi(setter, js_name = "set_mul_comm")] - // pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { - // self.mul_comm = x; - // } + #[napi(getter, js_name = "mul_comm")] + pub fn mul_comm(&self) -> $NapiPolyComm { + self.mul_comm.clone() + } - // #[napi(getter, js_name = "emul_comm")] - // pub fn emul_comm(&self) -> $NapiPolyComm { - // self.emul_comm.clone() - // } + #[napi(setter, js_name = "set_mul_comm")] + pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { + self.mul_comm = x; + } - // #[napi(setter, js_name = "set_emul_comm")] - // pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { - // self.emul_comm = x; - // } + #[napi(getter, js_name = "emul_comm")] + pub fn emul_comm(&self) -> $NapiPolyComm { + self.emul_comm.clone() + } - // #[napi(getter, js_name = "endomul_scalar_comm")] - // pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { - // self.endomul_scalar_comm.clone() - // } + #[napi(setter, js_name = "set_emul_comm")] + pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { + self.emul_comm = x; + } - // #[napi(setter, js_name = "set_endomul_scalar_comm")] - // pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { - // self.endomul_scalar_comm = x; - // } + #[napi(getter, js_name = "endomul_scalar_comm")] + pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { + self.endomul_scalar_comm.clone() + } - // #[napi(getter, js_name = "xor_comm")] - // pub fn xor_comm(&self) -> Option<$NapiPolyComm> { - // self.xor_comm.clone() - // } + #[napi(setter, js_name = "set_endomul_scalar_comm")] + pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { + self.endomul_scalar_comm = x; + } - // #[napi(setter, js_name = "set_xor_comm")] - // pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.xor_comm = x; - // } + #[napi(getter, js_name = "xor_comm")] + pub fn xor_comm(&self) -> Option<$NapiPolyComm> { + self.xor_comm.clone() + } - // #[napi(getter, js_name = "rot_comm")] - // pub fn rot_comm(&self) -> Option<$NapiPolyComm> { - // self.rot_comm.clone() - // } + #[napi(setter, js_name = "set_xor_comm")] + pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { + self.xor_comm = x; + } - // #[napi(setter, js_name = "set_rot_comm")] - // pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.rot_comm = x; - // } + #[napi(getter, js_name = "rot_comm")] + pub fn rot_comm(&self) -> Option<$NapiPolyComm> { + self.rot_comm.clone() + } - // #[napi(getter, js_name = "range_check0_comm")] - // pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { - // self.range_check0_comm.clone() - // } + #[napi(setter, js_name = "set_rot_comm")] + pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { + self.rot_comm = x; + } - // #[napi(setter, js_name = "set_range_check0_comm")] - // pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.range_check0_comm = x; - // } + #[napi(getter, js_name = "range_check0_comm")] + pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { + self.range_check0_comm.clone() + } - // #[napi(getter, js_name = "range_check1_comm")] - // pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { - // self.range_check1_comm.clone() - // } + #[napi(setter, js_name = "set_range_check0_comm")] + pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check0_comm = x; + } - // #[napi(setter, js_name = "set_range_check1_comm")] - // pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.range_check1_comm = x; - // } + #[napi(getter, js_name = "range_check1_comm")] + pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { + self.range_check1_comm.clone() + } - // #[napi(getter, js_name = "foreign_field_add_comm")] - // pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { - // self.foreign_field_add_comm.clone() - // } + #[napi(setter, js_name = "set_range_check1_comm")] + pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { + self.range_check1_comm = x; + } - // #[napi(setter, js_name = "set_foreign_field_add_comm")] - // pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.foreign_field_add_comm = x; - // } + #[napi(getter, js_name = "foreign_field_add_comm")] + pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_add_comm.clone() + } - // #[napi(getter, js_name = "foreign_field_mul_comm")] - // pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { - // self.foreign_field_mul_comm.clone() - // } + #[napi(setter, js_name = "set_foreign_field_add_comm")] + pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_add_comm = x; + } - // #[napi(setter, js_name = "set_foreign_field_mul_comm")] - // pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.foreign_field_mul_comm = x; - // } + #[napi(getter, js_name = "foreign_field_mul_comm")] + pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { + self.foreign_field_mul_comm.clone() + } - // } + #[napi(setter, js_name = "set_foreign_field_mul_comm")] + pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { + self.foreign_field_mul_comm = x; + } + } #[derive(Clone, Copy)] - #[napi(object, js_name = [])] + #[napi(js_name = [])] pub struct [] { pub s0: $NapiF, pub s1: $NapiF, @@ -326,40 +325,40 @@ macro_rules! impl_verification_key { type NapiShifts = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } - // impl<'a> ToNapiValue for &'a mut [] { - // unsafe fn to_napi_value( - // env: sys::napi_env, - // val: Self, - // ) -> Result { - // <[] as ToNapiValue>::to_napi_value(env, val.clone()) - // } - // } + impl<'a> ToNapiValue for &'a mut [] { + unsafe fn to_napi_value( + env: sys::napi_env, + val: Self, + ) -> Result { + <[] as ToNapiValue>::to_napi_value(env, val.clone()) + } + } - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new( - // s0: $NapiF, - // s1: $NapiF, - // s2: $NapiF, - // s3: $NapiF, - // s4: $NapiF, - // s5: $NapiF, - // s6: $NapiF - // ) -> Self { - // Self { s0, s1, s2, s3, s4, s5, s6 } - // } - // } + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + s0: $NapiF, + s1: $NapiF, + s2: $NapiF, + s3: $NapiF, + s4: $NapiF, + s5: $NapiF, + s6: $NapiF + ) -> Self { + Self { s0, s1, s2, s3, s4, s5, s6 } + } + } #[napi(js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] @@ -488,7 +487,7 @@ macro_rules! impl_verification_key { } } - #[napi(object, js_name = [])] + #[napi(js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(js_name = "joint_lookup_used")] @@ -564,89 +563,89 @@ macro_rules! impl_verification_key { } } - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new( - // joint_lookup_used: bool, - // lookup_table: NapiVector<$NapiPolyComm>, - // lookup_selectors: NapiLookupSelectors, - // table_ids: Option<$NapiPolyComm>, - // lookup_info: NapiLookupInfo, - // runtime_tables_selector: Option<$NapiPolyComm> - // ) -> NapiLookupVerifierIndex { - // NapiLookupVerifierIndex { - // joint_lookup_used, - // lookup_table, - // lookup_selectors, - // table_ids, - // lookup_info: lookup_info.clone(), - // runtime_tables_selector - // } - // } + #[napi] + impl [] { + #[napi(constructor)] + pub fn new( + joint_lookup_used: bool, + lookup_table: NapiVector<$NapiPolyComm>, + lookup_selectors: NapiLookupSelectors, + table_ids: Option<$NapiPolyComm>, + lookup_info: NapiLookupInfo, + runtime_tables_selector: Option<$NapiPolyComm> + ) -> NapiLookupVerifierIndex { + NapiLookupVerifierIndex { + joint_lookup_used, + lookup_table, + lookup_selectors, + table_ids, + lookup_info: lookup_info.clone(), + runtime_tables_selector + } + } - // #[napi(getter, js_name = "lookup_table")] - // pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { - // self.lookup_table.clone() - // } + #[napi(getter, js_name = "lookup_table")] + pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { + self.lookup_table.clone() + } - // #[napi(setter, js_name = "set_lookup_table")] - // pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.lookup_table = x - // } + #[napi(setter, js_name = "set_lookup_table")] + pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { + self.lookup_table = x + } - // #[napi(getter, js_name = "lookup_selectors")] - // pub fn lookup_selectors(&self) -> NapiLookupSelectors { - // self.lookup_selectors.clone() - // } + #[napi(getter, js_name = "lookup_selectors")] + pub fn lookup_selectors(&self) -> NapiLookupSelectors { + self.lookup_selectors.clone() + } - // #[napi(setter, js_name = "set_lookup_selectors")] - // pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { - // self.lookup_selectors = x - // } + #[napi(setter, js_name = "set_lookup_selectors")] + pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { + self.lookup_selectors = x + } - // #[napi(getter, js_name = "table_ids")] - // pub fn table_ids(&self) -> Option<$NapiPolyComm>{ - // self.table_ids.clone() - // } + #[napi(getter, js_name = "table_ids")] + pub fn table_ids(&self) -> Option<$NapiPolyComm>{ + self.table_ids.clone() + } - // #[napi(setter, js_name = "set_table_ids")] - // pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { - // self.table_ids = x - // } + #[napi(setter, js_name = "set_table_ids")] + pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { + self.table_ids = x + } - // #[napi(getter, js_name = "lookup_info")] - // pub fn lookup_info(&self) -> NapiLookupInfo { - // self.lookup_info.clone() - // } + #[napi(getter, js_name = "lookup_info")] + pub fn lookup_info(&self) -> NapiLookupInfo { + self.lookup_info.clone() + } - // #[napi(setter, js_name = "set_lookup_info")] - // pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { - // self.lookup_info = x - // } + #[napi(setter, js_name = "set_lookup_info")] + pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { + self.lookup_info = x + } - // #[napi(getter, js_name = "runtime_tables_selector")] - // pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { - // self.runtime_tables_selector.clone() - // } + #[napi(getter, js_name = "runtime_tables_selector")] + pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { + self.runtime_tables_selector.clone() + } - // #[napi(setter, js_name = "set_runtime_tables_selector")] - // pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { - // self.runtime_tables_selector = x - // } - // } + #[napi(setter, js_name = "set_runtime_tables_selector")] + pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { + self.runtime_tables_selector = x + } + } - #[napi(object, js_name = [])] + #[napi(js_name = [])] #[derive(Clone)] pub struct [] { pub domain: NapiDomain, @@ -659,81 +658,81 @@ macro_rules! impl_verification_key { #[napi(skip)] pub evals: NapiPlonkVerificationEvals, pub shifts: NapiShifts, - #[napi(js_name = "lookup_index")] + #[napi(skip, js_name = "lookup_index")] pub lookup_index: Option, pub zk_rows: i32, } type NapiPlonkVerifierIndex = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } - // #[napi] - // impl [] { - // #[napi(constructor)] - // #[allow(clippy::too_many_arguments)] - // pub fn new( - // domain: &NapiDomain, - // max_poly_size: i32, - // public_: i32, - // prev_challenges: i32, - // srs: &$NapiSrs, - // evals: &NapiPlonkVerificationEvals, - // shifts: &NapiShifts, - // lookup_index: Option, - // zk_rows: i32, - // ) -> Self { - // NapiPlonkVerifierIndex { - // domain: domain.clone(), - // max_poly_size, - // public_, - // prev_challenges, - // srs: srs.clone(), - // evals: evals.clone(), - // shifts: shifts.clone(), - // lookup_index: lookup_index.clone(), - // zk_rows, - // } - // } + #[napi] + impl [] { + #[napi(constructor)] + #[allow(clippy::too_many_arguments)] + pub fn new( + domain: &NapiDomain, + max_poly_size: i32, + public_: i32, + prev_challenges: i32, + srs: &$NapiSrs, + evals: &NapiPlonkVerificationEvals, + shifts: &NapiShifts, + lookup_index: Option, + zk_rows: i32, + ) -> Self { + NapiPlonkVerifierIndex { + domain: domain.clone(), + max_poly_size, + public_, + prev_challenges, + srs: srs.clone(), + evals: evals.clone(), + shifts: shifts.clone(), + lookup_index: lookup_index.clone(), + zk_rows, + } + } - // #[napi(getter)] - // pub fn srs(&self) -> $NapiSrs { - // self.srs.clone() - // } + #[napi(getter)] + pub fn srs(&self) -> $NapiSrs { + self.srs.clone() + } - // #[napi(setter, js_name = "set_srs")] - // pub fn set_srs(&mut self, x: $NapiSrs) { - // self.srs = x - // } + #[napi(setter, js_name = "set_srs")] + pub fn set_srs(&mut self, x: $NapiSrs) { + self.srs = x + } - // #[napi(getter)] - // pub fn evals(&self) -> NapiPlonkVerificationEvals { - // self.evals.clone() - // } + #[napi(getter)] + pub fn evals(&self) -> NapiPlonkVerificationEvals { + self.evals.clone() + } - // #[napi(setter, js_name = "set_evals")] - // pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { - // self.evals = x - // } + #[napi(setter, js_name = "set_evals")] + pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { + self.evals = x + } - // #[napi(getter, js_name = "lookup_index")] - // pub fn lookup_index(&self) -> Option { - // self.lookup_index.clone() - // } + #[napi(getter, js_name = "lookup_index")] + pub fn lookup_index(&self) -> Option { + self.lookup_index.clone() + } - // #[napi(setter, js_name = "set_lookup_index")] - // pub fn set_lookup_index(&mut self, li: Option) { - // self.lookup_index = li - // } - // } + #[napi(setter, js_name = "set_lookup_index")] + pub fn set_lookup_index(&mut self, li: Option) { + self.lookup_index = li + } + } pub fn to_napi( srs: &Arc>, @@ -1083,7 +1082,6 @@ pub mod fp { srs::fp::NapiFpSrs as WasmFpSrs, wrappers::{field::NapiPastaFp as WasmPastaFp, group::NapiGVesta as WasmGVesta}, }; - // use arkworks::WasmGVesta; use mina_curves::pasta::{Fp, Pallas as GAffineOther, Vesta as GAffine}; impl_verification_key!( From bd455b9d25ca1c6ff7d991d5222ebf51320ed809 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 21:45:49 -0300 Subject: [PATCH 34/45] Convert all napi classes to objects for plonk verifier index --- plonk-napi/src/plonk_verifier_index.rs | 823 ++++++++++++------------- 1 file changed, 411 insertions(+), 412 deletions(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 9f97ecca9e3..2515f4580fa 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -43,7 +43,7 @@ macro_rules! impl_verification_key { $field_name: ident ) => { paste! { - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone, Copy)] pub struct [] { #[napi(js_name = "log_size_of_group")] @@ -53,13 +53,13 @@ macro_rules! impl_verification_key { } type NapiDomain = []; - #[napi] - impl [] { - #[napi(constructor)] - pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { - NapiDomain {log_size_of_group, group_gen} - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { + // NapiDomain {log_size_of_group, group_gen} + // } + // } impl From for Domain<$F> { fn from(domain: NapiDomain) -> Self { @@ -68,26 +68,26 @@ macro_rules! impl_verification_key { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - impl<'a> ToNapiValue for &'a mut [] { - unsafe fn to_napi_value( - env: sys::napi_env, - val: Self, - ) -> Result { - <[] as ToNapiValue>::to_napi_value(env, val.clone()) - } - } + // impl<'a> ToNapiValue for &'a mut [] { + // unsafe fn to_napi_value( + // env: sys::napi_env, + // val: Self, + // ) -> Result { + // <[] as ToNapiValue>::to_napi_value(env, val.clone()) + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { #[napi(skip, js_name = "sigma_comm")] @@ -122,197 +122,197 @@ macro_rules! impl_verification_key { type NapiPlonkVerificationEvals = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[allow(clippy::too_many_arguments)] - #[napi(constructor)] - pub fn new( - sigma_comm: NapiVector<$NapiPolyComm>, - coefficients_comm: NapiVector<$NapiPolyComm>, - generic_comm: &$NapiPolyComm, - psm_comm: &$NapiPolyComm, - complete_add_comm: &$NapiPolyComm, - mul_comm: &$NapiPolyComm, - emul_comm: &$NapiPolyComm, - endomul_scalar_comm: &$NapiPolyComm, - xor_comm: Option<$NapiPolyComm>, - range_check0_comm: Option<$NapiPolyComm>, - range_check1_comm: Option<$NapiPolyComm>, - foreign_field_add_comm: Option<$NapiPolyComm>, - foreign_field_mul_comm: Option<$NapiPolyComm>, - rot_comm: Option<$NapiPolyComm>, - ) -> Self { - NapiPlonkVerificationEvals { - sigma_comm: sigma_comm.clone(), - coefficients_comm: coefficients_comm.clone(), - generic_comm: generic_comm.clone(), - psm_comm: psm_comm.clone(), - complete_add_comm: complete_add_comm.clone(), - mul_comm: mul_comm.clone(), - emul_comm: emul_comm.clone(), - endomul_scalar_comm: endomul_scalar_comm.clone(), - xor_comm: xor_comm.clone(), - range_check0_comm: range_check0_comm.clone(), - range_check1_comm: range_check1_comm.clone(), - foreign_field_mul_comm: foreign_field_mul_comm.clone(), - foreign_field_add_comm: foreign_field_add_comm.clone(), - rot_comm: rot_comm.clone(), - } - } + // #[napi] + // impl [] { + // #[allow(clippy::too_many_arguments)] + // #[napi(constructor)] + // pub fn new( + // sigma_comm: NapiVector<$NapiPolyComm>, + // coefficients_comm: NapiVector<$NapiPolyComm>, + // generic_comm: &$NapiPolyComm, + // psm_comm: &$NapiPolyComm, + // complete_add_comm: &$NapiPolyComm, + // mul_comm: &$NapiPolyComm, + // emul_comm: &$NapiPolyComm, + // endomul_scalar_comm: &$NapiPolyComm, + // xor_comm: Option<$NapiPolyComm>, + // range_check0_comm: Option<$NapiPolyComm>, + // range_check1_comm: Option<$NapiPolyComm>, + // foreign_field_add_comm: Option<$NapiPolyComm>, + // foreign_field_mul_comm: Option<$NapiPolyComm>, + // rot_comm: Option<$NapiPolyComm>, + // ) -> Self { + // NapiPlonkVerificationEvals { + // sigma_comm: sigma_comm.clone(), + // coefficients_comm: coefficients_comm.clone(), + // generic_comm: generic_comm.clone(), + // psm_comm: psm_comm.clone(), + // complete_add_comm: complete_add_comm.clone(), + // mul_comm: mul_comm.clone(), + // emul_comm: emul_comm.clone(), + // endomul_scalar_comm: endomul_scalar_comm.clone(), + // xor_comm: xor_comm.clone(), + // range_check0_comm: range_check0_comm.clone(), + // range_check1_comm: range_check1_comm.clone(), + // foreign_field_mul_comm: foreign_field_mul_comm.clone(), + // foreign_field_add_comm: foreign_field_add_comm.clone(), + // rot_comm: rot_comm.clone(), + // } + // } - #[napi(getter, js_name = "sigma_comm")] - pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { - self.sigma_comm.clone() - } + // #[napi(getter, js_name = "sigma_comm")] + // pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { + // self.sigma_comm.clone() + // } - #[napi(setter, js_name = "set_sigma_comm")] - pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - self.sigma_comm = x; - } + // #[napi(setter, js_name = "set_sigma_comm")] + // pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.sigma_comm = x; + // } - #[napi(getter, js_name = "coefficients_comm")] - pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { - self.coefficients_comm.clone() - } + // #[napi(getter, js_name = "coefficients_comm")] + // pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { + // self.coefficients_comm.clone() + // } - #[napi(setter, js_name = "set_coefficients_comm")] - pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - self.coefficients_comm = x; - } + // #[napi(setter, js_name = "set_coefficients_comm")] + // pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.coefficients_comm = x; + // } - #[napi(getter, js_name = "generic_comm")] - pub fn generic_comm(&self) -> $NapiPolyComm { - self.generic_comm.clone() - } + // #[napi(getter, js_name = "generic_comm")] + // pub fn generic_comm(&self) -> $NapiPolyComm { + // self.generic_comm.clone() + // } - #[napi(setter, js_name = "set_generic_comm")] - pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { - self.generic_comm = x; - } + // #[napi(setter, js_name = "set_generic_comm")] + // pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { + // self.generic_comm = x; + // } - #[napi(getter, js_name = "psm_comm")] - pub fn psm_comm(&self) -> $NapiPolyComm { - self.psm_comm.clone() - } + // #[napi(getter, js_name = "psm_comm")] + // pub fn psm_comm(&self) -> $NapiPolyComm { + // self.psm_comm.clone() + // } - #[napi(setter, js_name = "set_psm_comm")] - pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { - self.psm_comm = x; - } + // #[napi(setter, js_name = "set_psm_comm")] + // pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { + // self.psm_comm = x; + // } - #[napi(getter, js_name = "complete_add_comm")] - pub fn complete_add_comm(&self) -> $NapiPolyComm { - self.complete_add_comm.clone() - } + // #[napi(getter, js_name = "complete_add_comm")] + // pub fn complete_add_comm(&self) -> $NapiPolyComm { + // self.complete_add_comm.clone() + // } - #[napi(setter, js_name = "set_complete_add_comm")] - pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { - self.complete_add_comm = x; - } + // #[napi(setter, js_name = "set_complete_add_comm")] + // pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { + // self.complete_add_comm = x; + // } - #[napi(getter, js_name = "mul_comm")] - pub fn mul_comm(&self) -> $NapiPolyComm { - self.mul_comm.clone() - } + // #[napi(getter, js_name = "mul_comm")] + // pub fn mul_comm(&self) -> $NapiPolyComm { + // self.mul_comm.clone() + // } - #[napi(setter, js_name = "set_mul_comm")] - pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { - self.mul_comm = x; - } + // #[napi(setter, js_name = "set_mul_comm")] + // pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { + // self.mul_comm = x; + // } - #[napi(getter, js_name = "emul_comm")] - pub fn emul_comm(&self) -> $NapiPolyComm { - self.emul_comm.clone() - } + // #[napi(getter, js_name = "emul_comm")] + // pub fn emul_comm(&self) -> $NapiPolyComm { + // self.emul_comm.clone() + // } - #[napi(setter, js_name = "set_emul_comm")] - pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { - self.emul_comm = x; - } + // #[napi(setter, js_name = "set_emul_comm")] + // pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { + // self.emul_comm = x; + // } - #[napi(getter, js_name = "endomul_scalar_comm")] - pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { - self.endomul_scalar_comm.clone() - } + // #[napi(getter, js_name = "endomul_scalar_comm")] + // pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { + // self.endomul_scalar_comm.clone() + // } - #[napi(setter, js_name = "set_endomul_scalar_comm")] - pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { - self.endomul_scalar_comm = x; - } + // #[napi(setter, js_name = "set_endomul_scalar_comm")] + // pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { + // self.endomul_scalar_comm = x; + // } - #[napi(getter, js_name = "xor_comm")] - pub fn xor_comm(&self) -> Option<$NapiPolyComm> { - self.xor_comm.clone() - } + // #[napi(getter, js_name = "xor_comm")] + // pub fn xor_comm(&self) -> Option<$NapiPolyComm> { + // self.xor_comm.clone() + // } - #[napi(setter, js_name = "set_xor_comm")] - pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { - self.xor_comm = x; - } + // #[napi(setter, js_name = "set_xor_comm")] + // pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.xor_comm = x; + // } - #[napi(getter, js_name = "rot_comm")] - pub fn rot_comm(&self) -> Option<$NapiPolyComm> { - self.rot_comm.clone() - } + // #[napi(getter, js_name = "rot_comm")] + // pub fn rot_comm(&self) -> Option<$NapiPolyComm> { + // self.rot_comm.clone() + // } - #[napi(setter, js_name = "set_rot_comm")] - pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { - self.rot_comm = x; - } + // #[napi(setter, js_name = "set_rot_comm")] + // pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.rot_comm = x; + // } - #[napi(getter, js_name = "range_check0_comm")] - pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { - self.range_check0_comm.clone() - } + // #[napi(getter, js_name = "range_check0_comm")] + // pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { + // self.range_check0_comm.clone() + // } - #[napi(setter, js_name = "set_range_check0_comm")] - pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check0_comm = x; - } + // #[napi(setter, js_name = "set_range_check0_comm")] + // pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.range_check0_comm = x; + // } - #[napi(getter, js_name = "range_check1_comm")] - pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { - self.range_check1_comm.clone() - } + // #[napi(getter, js_name = "range_check1_comm")] + // pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { + // self.range_check1_comm.clone() + // } - #[napi(setter, js_name = "set_range_check1_comm")] - pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { - self.range_check1_comm = x; - } + // #[napi(setter, js_name = "set_range_check1_comm")] + // pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.range_check1_comm = x; + // } - #[napi(getter, js_name = "foreign_field_add_comm")] - pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_add_comm.clone() - } + // #[napi(getter, js_name = "foreign_field_add_comm")] + // pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { + // self.foreign_field_add_comm.clone() + // } - #[napi(setter, js_name = "set_foreign_field_add_comm")] - pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_add_comm = x; - } + // #[napi(setter, js_name = "set_foreign_field_add_comm")] + // pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.foreign_field_add_comm = x; + // } - #[napi(getter, js_name = "foreign_field_mul_comm")] - pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { - self.foreign_field_mul_comm.clone() - } + // #[napi(getter, js_name = "foreign_field_mul_comm")] + // pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { + // self.foreign_field_mul_comm.clone() + // } - #[napi(setter, js_name = "set_foreign_field_mul_comm")] - pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { - self.foreign_field_mul_comm = x; - } - } + // #[napi(setter, js_name = "set_foreign_field_mul_comm")] + // pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { + // self.foreign_field_mul_comm = x; + // } + // } #[derive(Clone, Copy)] - #[napi(js_name = [])] + #[napi(object, js_name = [])] pub struct [] { pub s0: $NapiF, pub s1: $NapiF, @@ -325,49 +325,49 @@ macro_rules! impl_verification_key { type NapiShifts = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - impl<'a> ToNapiValue for &'a mut [] { - unsafe fn to_napi_value( - env: sys::napi_env, - val: Self, - ) -> Result { - <[] as ToNapiValue>::to_napi_value(env, val.clone()) - } - } + // impl<'a> ToNapiValue for &'a mut [] { + // unsafe fn to_napi_value( + // env: sys::napi_env, + // val: Self, + // ) -> Result { + // <[] as ToNapiValue>::to_napi_value(env, val.clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - s0: $NapiF, - s1: $NapiF, - s2: $NapiF, - s3: $NapiF, - s4: $NapiF, - s5: $NapiF, - s6: $NapiF - ) -> Self { - Self { s0, s1, s2, s3, s4, s5, s6 } - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new( + // s0: $NapiF, + // s1: $NapiF, + // s2: $NapiF, + // s3: $NapiF, + // s4: $NapiF, + // s5: $NapiF, + // s6: $NapiF + // ) -> Self { + // Self { s0, s1, s2, s3, s4, s5, s6 } + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { #[napi(skip)] pub xor: Option<$NapiPolyComm>, #[napi(skip)] pub lookup : Option<$NapiPolyComm>, - #[napi(skip)] + #[napi(skip, js_name = "range_check")] pub range_check: Option<$NapiPolyComm>, #[napi(skip)] pub ffmul: Option<$NapiPolyComm>, @@ -419,78 +419,77 @@ macro_rules! impl_verification_key { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - xor: Option<$NapiPolyComm>, - lookup: Option<$NapiPolyComm>, - range_check: Option<$NapiPolyComm>, - ffmul: Option<$NapiPolyComm> - ) -> Self { - Self { - xor, - lookup, - range_check, - ffmul - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new( + // xor: Option<$NapiPolyComm>, + // lookup: Option<$NapiPolyComm>, + // range_check: Option<$NapiPolyComm>, + // ffmul: Option<$NapiPolyComm> + // ) -> Self { + // Self { + // xor, + // lookup, + // range_check, + // ffmul + // } + // } - #[napi(getter)] - pub fn xor(&self) -> Option<$NapiPolyComm> { - self.xor.clone() - } + // #[napi(getter)] + // pub fn xor(&self) -> Option<$NapiPolyComm> { + // self.xor.clone() + // } - #[napi(setter, js_name = "set_xor")] - pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { - self.xor = x - } + // #[napi(setter, js_name = "set_xor")] + // pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { + // self.xor = x + // } - #[napi(getter)] - pub fn lookup(&self) -> Option<$NapiPolyComm> { - self.lookup.clone() - } + // #[napi(getter)] + // pub fn lookup(&self) -> Option<$NapiPolyComm> { + // self.lookup.clone() + // } - #[napi(setter, js_name = "set_lookup")] - pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { - self.lookup = x - } + // #[napi(setter, js_name = "set_lookup")] + // pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { + // self.lookup = x + // } - #[napi(getter)] - pub fn ffmul(&self) -> Option<$NapiPolyComm> { - self.ffmul.clone() - } + // #[napi(getter)] + // pub fn ffmul(&self) -> Option<$NapiPolyComm> { + // self.ffmul.clone() + // } - #[napi(setter, js_name = "set_ffmul")] - pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { - self.ffmul = x - } + // #[napi(setter, js_name = "set_ffmul")] + // pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { + // self.ffmul = x + // } - #[napi(getter, js_name = "range_check")] - pub fn range_check(&self) -> Option<$NapiPolyComm> { - self.range_check.clone() - } + // #[napi(getter, js_name = "range_check")] + // pub fn range_check(&self) -> Option<$NapiPolyComm> { + // self.range_check.clone() + // } - #[napi(setter, js_name = "set_range_check")] - pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { - self.range_check = x - } - } + // #[napi(setter, js_name = "set_range_check")] + // pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { + // self.range_check = x + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { - #[napi(js_name = "joint_lookup_used")] pub joint_lookup_used: bool, #[napi(skip)] @@ -563,89 +562,89 @@ macro_rules! impl_verification_key { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - pub fn new( - joint_lookup_used: bool, - lookup_table: NapiVector<$NapiPolyComm>, - lookup_selectors: NapiLookupSelectors, - table_ids: Option<$NapiPolyComm>, - lookup_info: NapiLookupInfo, - runtime_tables_selector: Option<$NapiPolyComm> - ) -> NapiLookupVerifierIndex { - NapiLookupVerifierIndex { - joint_lookup_used, - lookup_table, - lookup_selectors, - table_ids, - lookup_info: lookup_info.clone(), - runtime_tables_selector - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new( + // joint_lookup_used: bool, + // lookup_table: NapiVector<$NapiPolyComm>, + // lookup_selectors: NapiLookupSelectors, + // table_ids: Option<$NapiPolyComm>, + // lookup_info: NapiLookupInfo, + // runtime_tables_selector: Option<$NapiPolyComm> + // ) -> NapiLookupVerifierIndex { + // NapiLookupVerifierIndex { + // joint_lookup_used, + // lookup_table, + // lookup_selectors, + // table_ids, + // lookup_info: lookup_info.clone(), + // runtime_tables_selector + // } + // } - #[napi(getter, js_name = "lookup_table")] - pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { - self.lookup_table.clone() - } + // #[napi(getter, js_name = "lookup_table")] + // pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { + // self.lookup_table.clone() + // } - #[napi(setter, js_name = "set_lookup_table")] - pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { - self.lookup_table = x - } + // #[napi(setter, js_name = "set_lookup_table")] + // pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { + // self.lookup_table = x + // } - #[napi(getter, js_name = "lookup_selectors")] - pub fn lookup_selectors(&self) -> NapiLookupSelectors { - self.lookup_selectors.clone() - } + // #[napi(getter, js_name = "lookup_selectors")] + // pub fn lookup_selectors(&self) -> NapiLookupSelectors { + // self.lookup_selectors.clone() + // } - #[napi(setter, js_name = "set_lookup_selectors")] - pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { - self.lookup_selectors = x - } + // #[napi(setter, js_name = "set_lookup_selectors")] + // pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { + // self.lookup_selectors = x + // } - #[napi(getter, js_name = "table_ids")] - pub fn table_ids(&self) -> Option<$NapiPolyComm>{ - self.table_ids.clone() - } + // #[napi(getter, js_name = "table_ids")] + // pub fn table_ids(&self) -> Option<$NapiPolyComm>{ + // self.table_ids.clone() + // } - #[napi(setter, js_name = "set_table_ids")] - pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { - self.table_ids = x - } + // #[napi(setter, js_name = "set_table_ids")] + // pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { + // self.table_ids = x + // } - #[napi(getter, js_name = "lookup_info")] - pub fn lookup_info(&self) -> NapiLookupInfo { - self.lookup_info.clone() - } + // #[napi(getter, js_name = "lookup_info")] + // pub fn lookup_info(&self) -> NapiLookupInfo { + // self.lookup_info.clone() + // } - #[napi(setter, js_name = "set_lookup_info")] - pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { - self.lookup_info = x - } + // #[napi(setter, js_name = "set_lookup_info")] + // pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { + // self.lookup_info = x + // } - #[napi(getter, js_name = "runtime_tables_selector")] - pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { - self.runtime_tables_selector.clone() - } + // #[napi(getter, js_name = "runtime_tables_selector")] + // pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { + // self.runtime_tables_selector.clone() + // } - #[napi(setter, js_name = "set_runtime_tables_selector")] - pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { - self.runtime_tables_selector = x - } - } + // #[napi(setter, js_name = "set_runtime_tables_selector")] + // pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { + // self.runtime_tables_selector = x + // } + // } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { pub domain: NapiDomain, @@ -665,74 +664,74 @@ macro_rules! impl_verification_key { type NapiPlonkVerifierIndex = []; - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } - #[napi] - impl [] { - #[napi(constructor)] - #[allow(clippy::too_many_arguments)] - pub fn new( - domain: &NapiDomain, - max_poly_size: i32, - public_: i32, - prev_challenges: i32, - srs: &$NapiSrs, - evals: &NapiPlonkVerificationEvals, - shifts: &NapiShifts, - lookup_index: Option, - zk_rows: i32, - ) -> Self { - NapiPlonkVerifierIndex { - domain: domain.clone(), - max_poly_size, - public_, - prev_challenges, - srs: srs.clone(), - evals: evals.clone(), - shifts: shifts.clone(), - lookup_index: lookup_index.clone(), - zk_rows, - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // #[allow(clippy::too_many_arguments)] + // pub fn new( + // domain: &NapiDomain, + // max_poly_size: i32, + // public_: i32, + // prev_challenges: i32, + // srs: &$NapiSrs, + // evals: &NapiPlonkVerificationEvals, + // shifts: &NapiShifts, + // lookup_index: Option, + // zk_rows: i32, + // ) -> Self { + // NapiPlonkVerifierIndex { + // domain: domain.clone(), + // max_poly_size, + // public_, + // prev_challenges, + // srs: srs.clone(), + // evals: evals.clone(), + // shifts: shifts.clone(), + // lookup_index: lookup_index.clone(), + // zk_rows, + // } + // } - #[napi(getter)] - pub fn srs(&self) -> $NapiSrs { - self.srs.clone() - } + // #[napi(getter)] + // pub fn srs(&self) -> $NapiSrs { + // self.srs.clone() + // } - #[napi(setter, js_name = "set_srs")] - pub fn set_srs(&mut self, x: $NapiSrs) { - self.srs = x - } + // #[napi(setter, js_name = "set_srs")] + // pub fn set_srs(&mut self, x: $NapiSrs) { + // self.srs = x + // } - #[napi(getter)] - pub fn evals(&self) -> NapiPlonkVerificationEvals { - self.evals.clone() - } + // #[napi(getter)] + // pub fn evals(&self) -> NapiPlonkVerificationEvals { + // self.evals.clone() + // } - #[napi(setter, js_name = "set_evals")] - pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { - self.evals = x - } + // #[napi(setter, js_name = "set_evals")] + // pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { + // self.evals = x + // } - #[napi(getter, js_name = "lookup_index")] - pub fn lookup_index(&self) -> Option { - self.lookup_index.clone() - } + // #[napi(getter, js_name = "lookup_index")] + // pub fn lookup_index(&self) -> Option { + // self.lookup_index.clone() + // } - #[napi(setter, js_name = "set_lookup_index")] - pub fn set_lookup_index(&mut self, li: Option) { - self.lookup_index = li - } - } + // #[napi(setter, js_name = "set_lookup_index")] + // pub fn set_lookup_index(&mut self, li: Option) { + // self.lookup_index = li + // } + // } pub fn to_napi( srs: &Arc>, From ef52c659fcb8bfbea16b4b8863d62bdfe16d55c9 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 22:14:16 -0300 Subject: [PATCH 35/45] Rename NapiVecVec to WasmVecVec --- plonk-napi/src/lib.rs | 4 +--- plonk-napi/src/proof.rs | 5 ++++- plonk-napi/src/vector.rs | 4 ++-- plonk-napi/src/wrappers/lookups.rs | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 477794aebf6..7747a742c10 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -93,9 +93,7 @@ pub use srs::{ fq::NapiFqSrs as WasmFqSrs, *, }; pub use tables::{JsLookupTableFp, JsLookupTableFq, JsRuntimeTableCfgFp, JsRuntimeTableCfgFq}; -pub use vector::{ - fp::NapiVecVecFp as WasmVecVecFp, fq::NapiVecVecFq as WasmVecVecFq, NapiFlatVector, -}; +pub use vector::{fp::WasmVecVecFp, fq::WasmVecVecFq, NapiFlatVector}; pub use wrappers::{ field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq}, group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta}, diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 88321684b91..07e2c00c4a9 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -1,6 +1,9 @@ use crate::{ tables::{JsRuntimeTableFp, JsRuntimeTableFq}, - vector::{fp::NapiVecVecFp, fq::NapiVecVecFq, NapiFlatVector, NapiVector}, + vector::{ + fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq, NapiFlatVector, + NapiVector, + }, }; use ark_ec::AffineRepr; use ark_ff::One; diff --git a/plonk-napi/src/vector.rs b/plonk-napi/src/vector.rs index 6883ccbe697..83e5390b960 100644 --- a/plonk-napi/src/vector.rs +++ b/plonk-napi/src/vector.rs @@ -394,7 +394,7 @@ pub mod fp { use mina_curves::pasta::Fp; use napi_derive::napi; - impl_vec_vec_fp!(NapiVecVecFp, Fp, NapiPastaFp); + impl_vec_vec_fp!(WasmVecVecFp, Fp, NapiPastaFp); } pub mod fq { @@ -403,5 +403,5 @@ pub mod fq { use mina_curves::pasta::Fq; use napi_derive::napi; - impl_vec_vec_fp!(NapiVecVecFq, Fq, NapiPastaFq); + impl_vec_vec_fp!(WasmVecVecFq, Fq, NapiPastaFq); } diff --git a/plonk-napi/src/wrappers/lookups.rs b/plonk-napi/src/wrappers/lookups.rs index a33fcb4f617..bdaab369780 100644 --- a/plonk-napi/src/wrappers/lookups.rs +++ b/plonk-napi/src/wrappers/lookups.rs @@ -1,5 +1,5 @@ use crate::{ - vector::{fp::NapiVecVecFp, fq::NapiVecVecFq}, + vector::{fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq}, wrappers::field::{NapiPastaFp, NapiPastaFq}, }; use kimchi::circuits::lookup::{ From 84373a1ca92831f3278819dcd74ad63692451efd Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 22:29:58 -0300 Subject: [PATCH 36/45] Fix NapiVecVec in napi proof.rs --- plonk-napi/src/proof.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 07e2c00c4a9..3f9fdb2f43e 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -41,10 +41,11 @@ macro_rules! impl_proof { $NapiSrs: ty, $NapiIndex: ty, $NapiVerifierIndex: ty, + $NapiVecVec: ty, $field_name: ident ) => { paste! { - type NapiVecVecF = []; + // type NapiVecVecF = []; #[napi(js_name = [])] #[derive(Clone)] @@ -448,7 +449,7 @@ macro_rules! impl_proof { evals: NapiProofEvaluations, // maybe remove FromNapiValue trait implementation and wrap it in External instead ft_eval1: $NapiF, public_: NapiFlatVector<$NapiF>, - prev_challenges_scalars: NapiVecVecF, + prev_challenges_scalars: $NapiVecVec, prev_challenges_comms: NapiVector<$NapiPolyComm>) -> Self { NapiProverProof { commitments, @@ -478,7 +479,7 @@ macro_rules! impl_proof { self.public.clone() } #[napi(getter, js_name="prev_challenges_scalars")] - pub fn prev_challenges_scalars(&self) -> NapiVecVecF { + pub fn prev_challenges_scalars(&self) -> $NapiVecVec { [](self.prev_challenges_scalars.clone()) } #[napi(getter, js_name="prev_challenges_comms")] @@ -503,7 +504,7 @@ macro_rules! impl_proof { self.public = public_ } #[napi(setter, js_name="set_prev_challenges_scalars")] - pub fn set_prev_challenges_scalars(&mut self, prev_challenges_scalars: NapiVecVecF) { + pub fn set_prev_challenges_scalars(&mut self, prev_challenges_scalars: $NapiVecVec) { self.prev_challenges_scalars = prev_challenges_scalars.0 } #[napi(setter, js_name="set_prev_challenges_comms")] @@ -533,7 +534,7 @@ macro_rules! impl_proof { #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_create">])] pub fn []( index: &External<$NapiIndex>, - witness: NapiVecVecF, + witness: $NapiVecVec, runtime_tables: NapiVector, prev_challenges: NapiFlatVector<$NapiF>, prev_sgs: NapiVector<$NapiG>, @@ -773,6 +774,7 @@ pub mod fp { NapiSrs, NapiPastaFpPlonkIndex, NapiFpPlonkVerifierIndex, + NapiVecVecFp, Fp ); } @@ -796,6 +798,7 @@ pub mod fq { NapiSrs, NapiPastaFqPlonkIndex, NapiFqPlonkVerifierIndex, + NapiVecVecFq, Fq ); } From a1addffca42d00ec75451ef6a48f2ac2558bd42f Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 23:02:01 -0300 Subject: [PATCH 37/45] Another fix for vector.rs --- plonk-napi/src/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plonk-napi/src/vector.rs b/plonk-napi/src/vector.rs index 83e5390b960..889a5454bab 100644 --- a/plonk-napi/src/vector.rs +++ b/plonk-napi/src/vector.rs @@ -306,7 +306,7 @@ macro_rules! impl_vec_vec_fp { impl $name { #[napi(constructor)] pub fn create(capacity: i32) -> Self { - Self(Vec::with_capacity(capacity as usize)) + $name(Vec::with_capacity(capacity as usize)) } #[napi] From 5734152d842b7a99ac7e06f2f74568e8f15c5865 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Thu, 4 Dec 2025 23:44:12 -0300 Subject: [PATCH 38/45] Add println for napi vecvec --- plonk-napi/src/vector.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/plonk-napi/src/vector.rs b/plonk-napi/src/vector.rs index 889a5454bab..0b013b0da5b 100644 --- a/plonk-napi/src/vector.rs +++ b/plonk-napi/src/vector.rs @@ -306,6 +306,7 @@ macro_rules! impl_vec_vec_fp { impl $name { #[napi(constructor)] pub fn create(capacity: i32) -> Self { + println!("Creating napi VecVec"); $name(Vec::with_capacity(capacity as usize)) } From 92146278b57e7187f4829b8f337e09258bcf0d98 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Fri, 5 Dec 2025 00:16:23 -0300 Subject: [PATCH 39/45] debugging napi vecvec --- plonk-napi/src/vector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plonk-napi/src/vector.rs b/plonk-napi/src/vector.rs index 0b013b0da5b..8fa0e66c9d7 100644 --- a/plonk-napi/src/vector.rs +++ b/plonk-napi/src/vector.rs @@ -305,7 +305,7 @@ macro_rules! impl_vec_vec_fp { #[napi] impl $name { #[napi(constructor)] - pub fn create(capacity: i32) -> Self { + pub fn new(capacity: i32) -> Self { println!("Creating napi VecVec"); $name(Vec::with_capacity(capacity as usize)) } From 84440c4040df23bfd946dbd7d312e5cdb2fde563 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Fri, 5 Dec 2025 15:41:43 -0300 Subject: [PATCH 40/45] Add napi conversion for runtime table --- plonk-napi/src/proof.rs | 68 +++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 3f9fdb2f43e..4fd69a19b1d 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -528,14 +528,49 @@ macro_rules! impl_proof { pub public_input: Vec<$F>, } + #[napi(js_name = [])] + #[derive(Clone)] + pub struct [] { + id: i32, + data: NapiFlatVector<$NapiF> + } + type NapiRuntimeTable = []; + + #[napi] + impl [] { + #[napi(constructor)] + pub fn new(id: i32, data: NapiFlatVector<$NapiF>) -> NapiRuntimeTable { + NapiRuntimeTable {id, data} + } + } + + impl From<[]> for RuntimeTable<$F> { + fn from(wasm_rt: NapiRuntimeTable) -> RuntimeTable<$F> { + RuntimeTable { + id: wasm_rt.id.into(), + data: wasm_rt.data.into_iter().map(Into::into).collect() + } + } + } + + impl FromNapiValue for [] { + unsafe fn from_napi_value( + env: sys::napi_env, + napi_val: sys::napi_value, + ) -> Result { + let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + Ok((*instance).clone()) + } + } + type NapiProofF = []; - type JsRuntimeTableF = []; + // type JsRuntimeTableF = []; #[napi(js_name = [<"caml_pasta_" $field_name:snake "_plonk_proof_create">])] pub fn []( index: &External<$NapiIndex>, witness: $NapiVecVec, - runtime_tables: NapiVector, + runtime_tables: NapiVector, prev_challenges: NapiFlatVector<$NapiF>, prev_sgs: NapiVector<$NapiG>, ) -> Result> { @@ -568,20 +603,21 @@ macro_rules! impl_proof { d } }; - - let rust_runtime_tables: Vec> = runtime_tables - .into_iter() - .flat_map(|table| { - let JsRuntimeTableF { id, data } = table; - data.into_iter().map(move |column| { - let values = NapiFlatVector::<$NapiF>::from_bytes(column.to_vec()) - .into_iter() - .map(Into::into) - .collect(); - RuntimeTable { id, data: values } - }) - }) - .collect(); + let rust_runtime_tables: Vec> = runtime_tables.into_iter().map(Into::into).collect(); + + // let rust_runtime_tables: Vec> = runtime_tables + // .into_iter() + // .flat_map(|table| { + // let NapiRuntimeTable { id, data } = table; + // data.into_iter().map(move |column| { + // let values = NapiFlatVector::<$NapiF>::from_bytes(column.to_vec()) + // .into_iter() + // .map(Into::into) + // .collect(); + // RuntimeTable { id, data: values } + // }) + // }) + // .collect(); let witness: [Vec<_>; COLUMNS] = witness .0 From 43756cf75bcf7cc27de21421f373b5c758f1ff73 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Sun, 7 Dec 2025 16:36:01 -0300 Subject: [PATCH 41/45] Use napi runtime tables as object --- plonk-napi/src/proof.rs | 46 +++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 4fd69a19b1d..c9e8507cc2c 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -1,9 +1,5 @@ -use crate::{ - tables::{JsRuntimeTableFp, JsRuntimeTableFq}, - vector::{ - fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq, NapiFlatVector, - NapiVector, - }, +use crate::vector::{ + fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq, NapiFlatVector, NapiVector, }; use ark_ec::AffineRepr; use ark_ff::One; @@ -528,21 +524,21 @@ macro_rules! impl_proof { pub public_input: Vec<$F>, } - #[napi(js_name = [])] + #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { - id: i32, - data: NapiFlatVector<$NapiF> + pub id: i32, + pub data: NapiFlatVector<$NapiF> } type NapiRuntimeTable = []; - #[napi] - impl [] { - #[napi(constructor)] - pub fn new(id: i32, data: NapiFlatVector<$NapiF>) -> NapiRuntimeTable { - NapiRuntimeTable {id, data} - } - } + // #[napi] + // impl [] { + // #[napi(constructor)] + // pub fn new(id: i32, data: NapiFlatVector<$NapiF>) -> NapiRuntimeTable { + // NapiRuntimeTable {id, data} + // } + // } impl From<[]> for RuntimeTable<$F> { fn from(wasm_rt: NapiRuntimeTable) -> RuntimeTable<$F> { @@ -553,15 +549,15 @@ macro_rules! impl_proof { } } - impl FromNapiValue for [] { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } + // impl FromNapiValue for [] { + // unsafe fn from_napi_value( + // env: sys::napi_env, + // napi_val: sys::napi_value, + // ) -> Result { + // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; + // Ok((*instance).clone()) + // } + // } type NapiProofF = []; // type JsRuntimeTableF = []; From e072aeb5ea5a8ff61fdd0dcd927ef41321b56977 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Sun, 7 Dec 2025 16:47:06 -0300 Subject: [PATCH 42/45] export napi runtime tables --- plonk-napi/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 7747a742c10..8c21b2ffa9f 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -77,13 +77,13 @@ pub use proof::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_dummy, caml_pasta_fp_plonk_proof_verify, NapiFpLookupCommitments, NapiFpOpeningProof, - NapiFpProofEvaluations, NapiFpProverCommitments, NapiFpProverProof, + NapiFpProofEvaluations, NapiFpProverCommitments, NapiFpProverProof, NapiFpRuntimeTable, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_dummy, caml_pasta_fq_plonk_proof_verify, NapiFqLookupCommitments, NapiFqOpeningProof, - NapiFqProofEvaluations, NapiFqProverCommitments, NapiFqProverProof, + NapiFqProofEvaluations, NapiFqProverCommitments, NapiFqProverProof, NapiFqRuntimeTable, }, }; pub use srs::{ From 1af2adc88312dfa2eadbdb59b266d5607bcf1858 Mon Sep 17 00:00:00 2001 From: Shigoto-dev19 Date: Sun, 7 Dec 2025 19:38:53 -0300 Subject: [PATCH 43/45] Code cleanings --- plonk-napi/src/lib.rs | 2 - .../fp.rs | 0 .../fq.rs | 0 .../mod.rs | 0 plonk-napi/src/plonk_verifier_index.rs | 483 ------------------ plonk-napi/src/proof.rs | 14 - plonk-napi/src/vector.rs | 12 - 7 files changed, 511 deletions(-) rename plonk-napi/src/{plonk_verifier_index_old => old_plonk_verifier_index}/fp.rs (100%) rename plonk-napi/src/{plonk_verifier_index_old => old_plonk_verifier_index}/fq.rs (100%) rename plonk-napi/src/{plonk_verifier_index_old => old_plonk_verifier_index}/mod.rs (100%) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 8c21b2ffa9f..41ccb2279d7 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -98,5 +98,3 @@ pub use wrappers::{ field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq}, group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta}, }; - -pub use plonk_verifier_index::{fp::*, fq::*}; diff --git a/plonk-napi/src/plonk_verifier_index_old/fp.rs b/plonk-napi/src/old_plonk_verifier_index/fp.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index_old/fp.rs rename to plonk-napi/src/old_plonk_verifier_index/fp.rs diff --git a/plonk-napi/src/plonk_verifier_index_old/fq.rs b/plonk-napi/src/old_plonk_verifier_index/fq.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index_old/fq.rs rename to plonk-napi/src/old_plonk_verifier_index/fq.rs diff --git a/plonk-napi/src/plonk_verifier_index_old/mod.rs b/plonk-napi/src/old_plonk_verifier_index/mod.rs similarity index 100% rename from plonk-napi/src/plonk_verifier_index_old/mod.rs rename to plonk-napi/src/old_plonk_verifier_index/mod.rs diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 2515f4580fa..16707e44e0b 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -53,14 +53,6 @@ macro_rules! impl_verification_key { } type NapiDomain = []; - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new(log_size_of_group: i32, group_gen: $NapiF) -> Self { - // NapiDomain {log_size_of_group, group_gen} - // } - // } - impl From for Domain<$F> { fn from(domain: NapiDomain) -> Self { let size = 1 << domain.log_size_of_group; @@ -68,25 +60,6 @@ macro_rules! impl_verification_key { } } - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // impl<'a> ToNapiValue for &'a mut [] { - // unsafe fn to_napi_value( - // env: sys::napi_env, - // val: Self, - // ) -> Result { - // <[] as ToNapiValue>::to_napi_value(env, val.clone()) - // } - // } - #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { @@ -122,194 +95,6 @@ macro_rules! impl_verification_key { type NapiPlonkVerificationEvals = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // #[napi] - // impl [] { - // #[allow(clippy::too_many_arguments)] - // #[napi(constructor)] - // pub fn new( - // sigma_comm: NapiVector<$NapiPolyComm>, - // coefficients_comm: NapiVector<$NapiPolyComm>, - // generic_comm: &$NapiPolyComm, - // psm_comm: &$NapiPolyComm, - // complete_add_comm: &$NapiPolyComm, - // mul_comm: &$NapiPolyComm, - // emul_comm: &$NapiPolyComm, - // endomul_scalar_comm: &$NapiPolyComm, - // xor_comm: Option<$NapiPolyComm>, - // range_check0_comm: Option<$NapiPolyComm>, - // range_check1_comm: Option<$NapiPolyComm>, - // foreign_field_add_comm: Option<$NapiPolyComm>, - // foreign_field_mul_comm: Option<$NapiPolyComm>, - // rot_comm: Option<$NapiPolyComm>, - // ) -> Self { - // NapiPlonkVerificationEvals { - // sigma_comm: sigma_comm.clone(), - // coefficients_comm: coefficients_comm.clone(), - // generic_comm: generic_comm.clone(), - // psm_comm: psm_comm.clone(), - // complete_add_comm: complete_add_comm.clone(), - // mul_comm: mul_comm.clone(), - // emul_comm: emul_comm.clone(), - // endomul_scalar_comm: endomul_scalar_comm.clone(), - // xor_comm: xor_comm.clone(), - // range_check0_comm: range_check0_comm.clone(), - // range_check1_comm: range_check1_comm.clone(), - // foreign_field_mul_comm: foreign_field_mul_comm.clone(), - // foreign_field_add_comm: foreign_field_add_comm.clone(), - // rot_comm: rot_comm.clone(), - // } - // } - - // #[napi(getter, js_name = "sigma_comm")] - // pub fn sigma_comm(&self) -> NapiVector<$NapiPolyComm> { - // self.sigma_comm.clone() - // } - - // #[napi(setter, js_name = "set_sigma_comm")] - // pub fn set_sigma_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.sigma_comm = x; - // } - - // #[napi(getter, js_name = "coefficients_comm")] - // pub fn coefficients_comm(&self) -> NapiVector<$NapiPolyComm> { - // self.coefficients_comm.clone() - // } - - // #[napi(setter, js_name = "set_coefficients_comm")] - // pub fn set_coefficients_comm(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.coefficients_comm = x; - // } - - // #[napi(getter, js_name = "generic_comm")] - // pub fn generic_comm(&self) -> $NapiPolyComm { - // self.generic_comm.clone() - // } - - // #[napi(setter, js_name = "set_generic_comm")] - // pub fn set_generic_comm(&mut self, x: $NapiPolyComm) { - // self.generic_comm = x; - // } - - // #[napi(getter, js_name = "psm_comm")] - // pub fn psm_comm(&self) -> $NapiPolyComm { - // self.psm_comm.clone() - // } - - // #[napi(setter, js_name = "set_psm_comm")] - // pub fn set_psm_comm(&mut self, x: $NapiPolyComm) { - // self.psm_comm = x; - // } - - // #[napi(getter, js_name = "complete_add_comm")] - // pub fn complete_add_comm(&self) -> $NapiPolyComm { - // self.complete_add_comm.clone() - // } - - // #[napi(setter, js_name = "set_complete_add_comm")] - // pub fn set_complete_add_comm(&mut self, x: $NapiPolyComm) { - // self.complete_add_comm = x; - // } - - // #[napi(getter, js_name = "mul_comm")] - // pub fn mul_comm(&self) -> $NapiPolyComm { - // self.mul_comm.clone() - // } - - // #[napi(setter, js_name = "set_mul_comm")] - // pub fn set_mul_comm(&mut self, x: $NapiPolyComm) { - // self.mul_comm = x; - // } - - // #[napi(getter, js_name = "emul_comm")] - // pub fn emul_comm(&self) -> $NapiPolyComm { - // self.emul_comm.clone() - // } - - // #[napi(setter, js_name = "set_emul_comm")] - // pub fn set_emul_comm(&mut self, x: $NapiPolyComm) { - // self.emul_comm = x; - // } - - // #[napi(getter, js_name = "endomul_scalar_comm")] - // pub fn endomul_scalar_comm(&self) -> $NapiPolyComm { - // self.endomul_scalar_comm.clone() - // } - - // #[napi(setter, js_name = "set_endomul_scalar_comm")] - // pub fn set_endomul_scalar_comm(&mut self, x: $NapiPolyComm) { - // self.endomul_scalar_comm = x; - // } - - // #[napi(getter, js_name = "xor_comm")] - // pub fn xor_comm(&self) -> Option<$NapiPolyComm> { - // self.xor_comm.clone() - // } - - // #[napi(setter, js_name = "set_xor_comm")] - // pub fn set_xor_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.xor_comm = x; - // } - - // #[napi(getter, js_name = "rot_comm")] - // pub fn rot_comm(&self) -> Option<$NapiPolyComm> { - // self.rot_comm.clone() - // } - - // #[napi(setter, js_name = "set_rot_comm")] - // pub fn set_rot_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.rot_comm = x; - // } - - // #[napi(getter, js_name = "range_check0_comm")] - // pub fn range_check0_comm(&self) -> Option<$NapiPolyComm> { - // self.range_check0_comm.clone() - // } - - // #[napi(setter, js_name = "set_range_check0_comm")] - // pub fn set_range_check0_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.range_check0_comm = x; - // } - - // #[napi(getter, js_name = "range_check1_comm")] - // pub fn range_check1_comm(&self) -> Option<$NapiPolyComm> { - // self.range_check1_comm.clone() - // } - - // #[napi(setter, js_name = "set_range_check1_comm")] - // pub fn set_range_check1_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.range_check1_comm = x; - // } - - // #[napi(getter, js_name = "foreign_field_add_comm")] - // pub fn foreign_field_add_comm(&self) -> Option<$NapiPolyComm> { - // self.foreign_field_add_comm.clone() - // } - - // #[napi(setter, js_name = "set_foreign_field_add_comm")] - // pub fn set_foreign_field_add_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.foreign_field_add_comm = x; - // } - - // #[napi(getter, js_name = "foreign_field_mul_comm")] - // pub fn foreign_field_mul_comm(&self) -> Option<$NapiPolyComm> { - // self.foreign_field_mul_comm.clone() - // } - - // #[napi(setter, js_name = "set_foreign_field_mul_comm")] - // pub fn set_foreign_field_mul_comm(&mut self, x: Option<$NapiPolyComm>) { - // self.foreign_field_mul_comm = x; - // } - // } #[derive(Clone, Copy)] #[napi(object, js_name = [])] @@ -325,41 +110,6 @@ macro_rules! impl_verification_key { type NapiShifts = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // impl<'a> ToNapiValue for &'a mut [] { - // unsafe fn to_napi_value( - // env: sys::napi_env, - // val: Self, - // ) -> Result { - // <[] as ToNapiValue>::to_napi_value(env, val.clone()) - // } - // } - - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new( - // s0: $NapiF, - // s1: $NapiF, - // s2: $NapiF, - // s3: $NapiF, - // s4: $NapiF, - // s5: $NapiF, - // s6: $NapiF - // ) -> Self { - // Self { s0, s1, s2, s3, s4, s5, s6 } - // } - // } - #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct [] { @@ -419,73 +169,6 @@ macro_rules! impl_verification_key { } } - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new( - // xor: Option<$NapiPolyComm>, - // lookup: Option<$NapiPolyComm>, - // range_check: Option<$NapiPolyComm>, - // ffmul: Option<$NapiPolyComm> - // ) -> Self { - // Self { - // xor, - // lookup, - // range_check, - // ffmul - // } - // } - - // #[napi(getter)] - // pub fn xor(&self) -> Option<$NapiPolyComm> { - // self.xor.clone() - // } - - // #[napi(setter, js_name = "set_xor")] - // pub fn set_xor(&mut self, x: Option<$NapiPolyComm>) { - // self.xor = x - // } - - // #[napi(getter)] - // pub fn lookup(&self) -> Option<$NapiPolyComm> { - // self.lookup.clone() - // } - - // #[napi(setter, js_name = "set_lookup")] - // pub fn set_lookup(&mut self, x: Option<$NapiPolyComm>) { - // self.lookup = x - // } - - // #[napi(getter)] - // pub fn ffmul(&self) -> Option<$NapiPolyComm> { - // self.ffmul.clone() - // } - - // #[napi(setter, js_name = "set_ffmul")] - // pub fn set_ffmul(&mut self, x: Option<$NapiPolyComm>) { - // self.ffmul = x - // } - - // #[napi(getter, js_name = "range_check")] - // pub fn range_check(&self) -> Option<$NapiPolyComm> { - // self.range_check.clone() - // } - - // #[napi(setter, js_name = "set_range_check")] - // pub fn set_range_check(&mut self, x: Option<$NapiPolyComm>) { - // self.range_check = x - // } - // } #[napi(object, js_name = [])] #[derive(Clone, Debug, Serialize, Deserialize, Default)] @@ -562,88 +245,6 @@ macro_rules! impl_verification_key { } } - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new( - // joint_lookup_used: bool, - // lookup_table: NapiVector<$NapiPolyComm>, - // lookup_selectors: NapiLookupSelectors, - // table_ids: Option<$NapiPolyComm>, - // lookup_info: NapiLookupInfo, - // runtime_tables_selector: Option<$NapiPolyComm> - // ) -> NapiLookupVerifierIndex { - // NapiLookupVerifierIndex { - // joint_lookup_used, - // lookup_table, - // lookup_selectors, - // table_ids, - // lookup_info: lookup_info.clone(), - // runtime_tables_selector - // } - // } - - // #[napi(getter, js_name = "lookup_table")] - // pub fn lookup_table(&self) -> NapiVector<$NapiPolyComm> { - // self.lookup_table.clone() - // } - - // #[napi(setter, js_name = "set_lookup_table")] - // pub fn set_lookup_table(&mut self, x: NapiVector<$NapiPolyComm>) { - // self.lookup_table = x - // } - - // #[napi(getter, js_name = "lookup_selectors")] - // pub fn lookup_selectors(&self) -> NapiLookupSelectors { - // self.lookup_selectors.clone() - // } - - // #[napi(setter, js_name = "set_lookup_selectors")] - // pub fn set_lookup_selectors(&mut self, x: NapiLookupSelectors) { - // self.lookup_selectors = x - // } - - // #[napi(getter, js_name = "table_ids")] - // pub fn table_ids(&self) -> Option<$NapiPolyComm>{ - // self.table_ids.clone() - // } - - // #[napi(setter, js_name = "set_table_ids")] - // pub fn set_table_ids(&mut self, x: Option<$NapiPolyComm>) { - // self.table_ids = x - // } - - // #[napi(getter, js_name = "lookup_info")] - // pub fn lookup_info(&self) -> NapiLookupInfo { - // self.lookup_info.clone() - // } - - // #[napi(setter, js_name = "set_lookup_info")] - // pub fn set_lookup_info(&mut self, x: NapiLookupInfo) { - // self.lookup_info = x - // } - - // #[napi(getter, js_name = "runtime_tables_selector")] - // pub fn runtime_tables_selector(&self) -> Option<$NapiPolyComm> { - // self.runtime_tables_selector.clone() - // } - - // #[napi(setter, js_name = "set_runtime_tables_selector")] - // pub fn set_runtime_tables_selector(&mut self, x: Option<$NapiPolyComm>) { - // self.runtime_tables_selector = x - // } - // } - #[napi(object, js_name = [])] #[derive(Clone)] pub struct [] { @@ -664,75 +265,6 @@ macro_rules! impl_verification_key { type NapiPlonkVerifierIndex = []; - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - - // #[napi] - // impl [] { - // #[napi(constructor)] - // #[allow(clippy::too_many_arguments)] - // pub fn new( - // domain: &NapiDomain, - // max_poly_size: i32, - // public_: i32, - // prev_challenges: i32, - // srs: &$NapiSrs, - // evals: &NapiPlonkVerificationEvals, - // shifts: &NapiShifts, - // lookup_index: Option, - // zk_rows: i32, - // ) -> Self { - // NapiPlonkVerifierIndex { - // domain: domain.clone(), - // max_poly_size, - // public_, - // prev_challenges, - // srs: srs.clone(), - // evals: evals.clone(), - // shifts: shifts.clone(), - // lookup_index: lookup_index.clone(), - // zk_rows, - // } - // } - - // #[napi(getter)] - // pub fn srs(&self) -> $NapiSrs { - // self.srs.clone() - // } - - // #[napi(setter, js_name = "set_srs")] - // pub fn set_srs(&mut self, x: $NapiSrs) { - // self.srs = x - // } - - // #[napi(getter)] - // pub fn evals(&self) -> NapiPlonkVerificationEvals { - // self.evals.clone() - // } - - // #[napi(setter, js_name = "set_evals")] - // pub fn set_evals(&mut self, x: NapiPlonkVerificationEvals) { - // self.evals = x - // } - - // #[napi(getter, js_name = "lookup_index")] - // pub fn lookup_index(&self) -> Option { - // self.lookup_index.clone() - // } - - // #[napi(setter, js_name = "set_lookup_index")] - // pub fn set_lookup_index(&mut self, li: Option) { - // self.lookup_index = li - // } - // } - pub fn to_napi( srs: &Arc>, vi: DlogVerifierIndex<$G, OpeningProof<$G>>, @@ -993,20 +525,6 @@ macro_rules! impl_verification_key { s6: s[6].clone().into(), }) } - // pub fn [<$name:snake _shifts>](log2_size: i32) -> NapiShifts { - // let domain = Domain::<$F>::new(1 << log2_size).unwrap(); - // let shifts = Shifts::new(&domain); - // let s = shifts.shifts(); - // NapiShifts { - // s0: s[0].clone().into(), - // s1: s[1].clone().into(), - // s2: s[2].clone().into(), - // s3: s[3].clone().into(), - // s4: s[4].clone().into(), - // s5: s[5].clone().into(), - // s6: s[6].clone().into(), - // } - // } #[napi(js_name = [<$name:snake _dummy>])] pub fn [<$name:snake _dummy>]() -> NapiPlonkVerifierIndex { @@ -1107,7 +625,6 @@ pub mod fq { srs::fq::NapiFqSrs as WasmFqSrs, wrappers::{field::NapiPastaFq as WasmPastaFq, group::NapiGPallas as WasmGPallas}, }; - // use arkworks::WasmGPallas; use mina_curves::pasta::{Fq, Pallas as GAffine, Vesta as GAffineOther}; impl_verification_key!( diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index c9e8507cc2c..87953053679 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -601,20 +601,6 @@ macro_rules! impl_proof { }; let rust_runtime_tables: Vec> = runtime_tables.into_iter().map(Into::into).collect(); - // let rust_runtime_tables: Vec> = runtime_tables - // .into_iter() - // .flat_map(|table| { - // let NapiRuntimeTable { id, data } = table; - // data.into_iter().map(move |column| { - // let values = NapiFlatVector::<$NapiF>::from_bytes(column.to_vec()) - // .into_iter() - // .map(Into::into) - // .collect(); - // RuntimeTable { id, data: values } - // }) - // }) - // .collect(); - let witness: [Vec<_>; COLUMNS] = witness .0 .try_into() diff --git a/plonk-napi/src/vector.rs b/plonk-napi/src/vector.rs index 8fa0e66c9d7..d429dc693fe 100644 --- a/plonk-napi/src/vector.rs +++ b/plonk-napi/src/vector.rs @@ -374,18 +374,6 @@ macro_rules! impl_vec_vec_fp { value.0 } } - /* - impl FromNapiValue for $name { - unsafe fn from_napi_value( - env: sys::napi_env, - napi_val: sys::napi_value, - ) -> Result { - let instance = - as FromNapiValue>::from_napi_value(env, napi_val)?; - Ok((*instance).clone()) - } - } - */ }; } From d52417a55ebb1a73bfaeb46049af97e62e0d7c6c Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 8 Dec 2025 18:53:06 +0100 Subject: [PATCH 44/45] napi: string typo --- plonk-napi/src/plonk_verifier_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plonk-napi/src/plonk_verifier_index.rs b/plonk-napi/src/plonk_verifier_index.rs index 6d34c4bb331..c966814bcef 100644 --- a/plonk-napi/src/plonk_verifier_index.rs +++ b/plonk-napi/src/plonk_verifier_index.rs @@ -576,7 +576,7 @@ macro_rules! impl_verification_key { } #[napi(js_name = [])] - pub fn [<"caml_pasta_" $field_name:snake "_plonk_verifier_index_deep_copy">]( + pub fn []( x: NapiPlonkVerifierIndex, ) -> NapiPlonkVerifierIndex { x.clone() From 1d1d5248027972bffc553a146877423e2a02854d Mon Sep 17 00:00:00 2001 From: querolita Date: Mon, 8 Dec 2025 21:52:09 +0100 Subject: [PATCH 45/45] napi: unify lookup table definition --- plonk-napi/src/lib.rs | 5 +- plonk-napi/src/proof.rs | 50 ++++--------------- plonk-napi/src/wrappers/lookups.rs | 79 +++++++++--------------------- 3 files changed, 35 insertions(+), 99 deletions(-) diff --git a/plonk-napi/src/lib.rs b/plonk-napi/src/lib.rs index 41ccb2279d7..e46f862eab1 100644 --- a/plonk-napi/src/lib.rs +++ b/plonk-napi/src/lib.rs @@ -77,13 +77,13 @@ pub use proof::{ caml_pasta_fp_plonk_proof_batch_verify, caml_pasta_fp_plonk_proof_create, caml_pasta_fp_plonk_proof_deep_copy, caml_pasta_fp_plonk_proof_dummy, caml_pasta_fp_plonk_proof_verify, NapiFpLookupCommitments, NapiFpOpeningProof, - NapiFpProofEvaluations, NapiFpProverCommitments, NapiFpProverProof, NapiFpRuntimeTable, + NapiFpProofEvaluations, NapiFpProverCommitments, NapiFpProverProof, }, fq::{ caml_pasta_fq_plonk_proof_batch_verify, caml_pasta_fq_plonk_proof_create, caml_pasta_fq_plonk_proof_deep_copy, caml_pasta_fq_plonk_proof_dummy, caml_pasta_fq_plonk_proof_verify, NapiFqLookupCommitments, NapiFqOpeningProof, - NapiFqProofEvaluations, NapiFqProverCommitments, NapiFqProverProof, NapiFqRuntimeTable, + NapiFqProofEvaluations, NapiFqProverCommitments, NapiFqProverProof, }, }; pub use srs::{ @@ -97,4 +97,5 @@ pub use vector::{fp::WasmVecVecFp, fq::WasmVecVecFq, NapiFlatVector}; pub use wrappers::{ field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq}, group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta}, + lookups::{NapiFpRuntimeTable, NapiFqRuntimeTable}, }; diff --git a/plonk-napi/src/proof.rs b/plonk-napi/src/proof.rs index 87953053679..e9c8b487b6f 100644 --- a/plonk-napi/src/proof.rs +++ b/plonk-napi/src/proof.rs @@ -38,7 +38,8 @@ macro_rules! impl_proof { $NapiIndex: ty, $NapiVerifierIndex: ty, $NapiVecVec: ty, - $field_name: ident + $field_name: ident, + $NapiRuntimeTable: ty, ) => { paste! { // type NapiVecVecF = []; @@ -524,41 +525,6 @@ macro_rules! impl_proof { pub public_input: Vec<$F>, } - #[napi(object, js_name = [])] - #[derive(Clone)] - pub struct [] { - pub id: i32, - pub data: NapiFlatVector<$NapiF> - } - type NapiRuntimeTable = []; - - // #[napi] - // impl [] { - // #[napi(constructor)] - // pub fn new(id: i32, data: NapiFlatVector<$NapiF>) -> NapiRuntimeTable { - // NapiRuntimeTable {id, data} - // } - // } - - impl From<[]> for RuntimeTable<$F> { - fn from(wasm_rt: NapiRuntimeTable) -> RuntimeTable<$F> { - RuntimeTable { - id: wasm_rt.id.into(), - data: wasm_rt.data.into_iter().map(Into::into).collect() - } - } - } - - // impl FromNapiValue for [] { - // unsafe fn from_napi_value( - // env: sys::napi_env, - // napi_val: sys::napi_value, - // ) -> Result { - // let instance = ]> as FromNapiValue>::from_napi_value(env, napi_val)?; - // Ok((*instance).clone()) - // } - // } - type NapiProofF = []; // type JsRuntimeTableF = []; @@ -566,7 +532,7 @@ macro_rules! impl_proof { pub fn []( index: &External<$NapiIndex>, witness: $NapiVecVec, - runtime_tables: NapiVector, + runtime_tables: NapiVector<$NapiRuntimeTable>, prev_challenges: NapiFlatVector<$NapiF>, prev_sgs: NapiVector<$NapiG>, ) -> Result> { @@ -779,7 +745,7 @@ pub mod fp { pasta_fp_plonk_index::WasmPastaFpPlonkIndex as NapiPastaFpPlonkIndex, plonk_verifier_index::fp::NapiFpPlonkVerifierIndex, poly_comm::vesta::NapiFpPolyComm, - wrappers::{field::NapiPastaFp, group::NapiGVesta}, + wrappers::{field::NapiPastaFp, group::NapiGVesta, lookups::NapiFpRuntimeTable}, }; use mina_curves::pasta::{Fp, Vesta}; @@ -793,7 +759,8 @@ pub mod fp { NapiPastaFpPlonkIndex, NapiFpPlonkVerifierIndex, NapiVecVecFp, - Fp + Fp, + NapiFpRuntimeTable, ); } @@ -803,7 +770,7 @@ pub mod fq { pasta_fq_plonk_index::WasmPastaFqPlonkIndex as NapiPastaFqPlonkIndex, plonk_verifier_index::fq::NapiFqPlonkVerifierIndex, poly_comm::pallas::NapiFqPolyComm, - wrappers::{field::NapiPastaFq, group::NapiGPallas}, + wrappers::{field::NapiPastaFq, group::NapiGPallas, lookups::NapiFqRuntimeTable}, }; use mina_curves::pasta::{Fq, Pallas}; @@ -817,6 +784,7 @@ pub mod fq { NapiPastaFqPlonkIndex, NapiFqPlonkVerifierIndex, NapiVecVecFq, - Fq + Fq, + NapiFqRuntimeTable, ); } diff --git a/plonk-napi/src/wrappers/lookups.rs b/plonk-napi/src/wrappers/lookups.rs index bdaab369780..50cb7a82a9c 100644 --- a/plonk-napi/src/wrappers/lookups.rs +++ b/plonk-napi/src/wrappers/lookups.rs @@ -1,5 +1,5 @@ use crate::{ - vector::{fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq}, + vector::{fp::WasmVecVecFp as NapiVecVecFp, fq::WasmVecVecFq as NapiVecVecFq, NapiFlatVector}, wrappers::field::{NapiPastaFp, NapiPastaFq}, }; use kimchi::circuits::lookup::{ @@ -110,17 +110,17 @@ impl From for LookupInfo { // ----------------- macro_rules! impl_lookup_wrappers { - ($name:ident, $field:ty, $NapiF:ty, $vec_vec:ty) => { + ($field_name:ident, $field:ty, $NapiF:ty, $vec_vec:ty) => { paste! { - #[napi(js_name = [<"WasmPasta" $name:camel "LookupTable">])] + #[napi(js_name = [<"WasmPasta" $field_name:camel "LookupTable">])] #[derive(Clone)] - pub struct [] { + pub struct [] { id: i32, data: $vec_vec, } #[napi] - impl [] { + impl [] { #[napi(constructor)] pub fn new(id: i32, data: $vec_vec) -> Self { Self { @@ -150,7 +150,7 @@ macro_rules! impl_lookup_wrappers { } } - impl From> for [] { + impl From> for [] { fn from(value: LookupTable<$field>) -> Self { Self { id: value.id, @@ -159,8 +159,8 @@ macro_rules! impl_lookup_wrappers { } } - impl From<[]> for LookupTable<$field> { - fn from(value: []) -> Self { + impl From<[]> for LookupTable<$field> { + fn from(value: []) -> Self { Self { id: value.id, data: value.data.into(), @@ -168,15 +168,15 @@ macro_rules! impl_lookup_wrappers { } } - #[napi(js_name = [<"WasmPasta" $name:camel "RuntimeTableCfg">])] + #[napi(js_name = [<"WasmPasta" $field_name:camel "RuntimeTableCfg">])] #[derive(Clone)] - pub struct [] { + pub struct [] { id: i32, first_column: Vec<$field>, } #[napi] - impl [] { + impl [] { #[napi(constructor)] pub fn new(id: i32, first_column: Uint8Array) -> Result { let bytes = first_column.as_ref().to_vec(); @@ -208,7 +208,7 @@ macro_rules! impl_lookup_wrappers { } } - impl From> for [] { + impl From> for [] { fn from(value: RuntimeTableCfg<$field>) -> Self { Self { id: value.id, @@ -217,8 +217,8 @@ macro_rules! impl_lookup_wrappers { } } - impl From<[]> for RuntimeTableCfg<$field> { - fn from(value: []) -> Self { + impl From<[]> for RuntimeTableCfg<$field> { + fn from(value: []) -> Self { Self { id: value.id, first_column: value.first_column, @@ -226,60 +226,27 @@ macro_rules! impl_lookup_wrappers { } } - #[napi(js_name = [<"Wasm" $name:camel "RuntimeTable">])] + #[napi(object, js_name = [<"Wasm" $field_name:camel "RuntimeTable">])] #[derive(Clone)] - pub struct [] { - id: i32, - data: Vec<$field>, - } - - #[napi] - impl [] { - #[napi(constructor)] - pub fn new(id: i32, data: Uint8Array) -> Result { - let bytes = data.as_ref().to_vec(); - let elements: Vec<$field> = FlatVector::<$NapiF>::from_bytes(bytes) - .into_iter() - .map(Into::into) - .collect(); - Ok(Self { id, data: elements }) - } - - #[napi(getter)] - pub fn id(&self) -> i32 { - self.id - } - - #[napi(setter)] - pub fn set_id(&mut self, id: i32) { - self.id = id; - } - - #[napi(getter)] - pub fn data(&self) -> Result { - let mut bytes = Vec::with_capacity(self.data.len() * <$NapiF>::FLATTENED_SIZE); - for value in &self.data { - let element = <$NapiF>::from(*value); - bytes.extend(element.flatten()); - } - Ok(Uint8Array::from(bytes)) - } + pub struct [] { + pub id: i32, + pub data: NapiFlatVector<$NapiF>, } - impl From> for [] { + impl From> for [] { fn from(value: RuntimeTable<$field>) -> Self { Self { id: value.id, - data: value.data, + data: value.data.into_iter().map(Into::into).collect(), } } } - impl From<[]> for RuntimeTable<$field> { - fn from(value: []) -> Self { + impl From<[]> for RuntimeTable<$field> { + fn from(value: []) -> Self { Self { id: value.id, - data: value.data, + data: value.data.into_iter().map(Into::into).collect(), } } }