Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions plonk-napi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
mod build_info;
mod circuit;
pub mod plonk_verifier_index;
mod plonk_verifier_index;
mod poly_comm;
mod poseidon;
mod prover_index;
mod types;

pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_block_cipher};
mod vector;
mod wrappers;

pub use circuit::prover_to_json;
pub use plonk_verifier_index::{
caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_shifts,
};
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 prover_index::{prover_index_from_bytes, prover_index_to_bytes};
pub use types::WasmPastaFpPlonkIndex;
pub use vector::{fp::NapiVecVecFp as WasmVecVecFp, fq::NapiVecVecFq as WasmVecVecFq};
pub use wrappers::{
field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq},
group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta},
};
138 changes: 138 additions & 0 deletions plonk-napi/src/poly_comm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use crate::vector::NapiVector;
use napi::bindgen_prelude::{ClassInstance, FromNapiValue};
use napi_derive::napi;
use paste::paste;
use poly_commitment::commitment::PolyComm;
use serde::{Deserialize, Serialize};

macro_rules! impl_poly_comm {
(
$NapiG:ty,
$g:ty,
$field_name:ident
) => {
paste! {
#[napi(js_name = [<"Wasm" $field_name "PolyComm">])]
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct [<Napi $field_name:camel PolyComm>] {
#[napi(skip)]
pub unshifted: NapiVector<$NapiG>,
#[napi(skip)]
pub shifted: Option<$NapiG>,
}

#[napi]
impl [<Napi $field_name:camel PolyComm>] {
#[napi(constructor)]
pub fn new(unshifted: NapiVector<$NapiG>, shifted: Option<$NapiG>) -> Self {
assert!(
shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
Self { unshifted, shifted }
}

#[napi(getter)]
pub fn unshifted(&self) -> NapiVector<$NapiG> {
self.unshifted.clone()
}

#[napi(setter, js_name = "set_unshifted")]
pub fn set_unshifted(&mut self, x: NapiVector<$NapiG>) {
self.unshifted = x;
}

#[napi(getter)]
pub fn shifted(&self) -> Option<$NapiG> {
self.shifted.clone()
}

#[napi(setter, js_name = "set_shifted")]
pub fn set_shifted(&mut self, value: Option<$NapiG>) {
self.shifted = value;
}
}

impl From<PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
fn from(x: PolyComm<$g>) -> Self {
let PolyComm { chunks } = x;
let unshifted: Vec<$NapiG> = chunks.into_iter().map(Into::into).collect();
Self {
unshifted: unshifted.into(),
shifted: None,
}
}
}

impl From<&PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
fn from(x: &PolyComm<$g>) -> Self {
let unshifted: Vec<$NapiG> = x.chunks.iter().map(|chunk| (*chunk).into()).collect();
Self {
unshifted: unshifted.into(),
shifted: None,
}
}
}

impl From<[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
fn from(x: [<Napi $field_name:camel PolyComm>]) -> Self {
let [<Napi $field_name:camel PolyComm>] { unshifted, shifted } = x;
assert!(
shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
PolyComm {
chunks: Vec::<$NapiG>::from(unshifted)
.into_iter()
.map(Into::into)
.collect(),
}
}
}

impl From<&[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
fn from(x: &[<Napi $field_name:camel PolyComm>]) -> Self {
assert!(
x.shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
PolyComm {
chunks: x
.unshifted
.iter()
.cloned()
.map(Into::into)
.collect(),
}
}
}

impl FromNapiValue for [<Napi $field_name:camel PolyComm>] {
unsafe fn from_napi_value(
env: napi::sys::napi_env,
napi_val: napi::sys::napi_value,
) -> napi::Result<Self> {
let instance = <ClassInstance<[<Napi $field_name:camel PolyComm>]> as FromNapiValue>::from_napi_value(env, napi_val)?;
Ok((*instance).clone())
}
}

}
};
}

pub mod pallas {
use super::*;
use crate::wrappers::group::NapiGPallas;
use mina_curves::pasta::Pallas;

impl_poly_comm!(NapiGPallas, Pallas, Fq);
}

pub mod vesta {
use super::*;
use crate::wrappers::group::NapiGVesta;
use mina_curves::pasta::Vesta;

impl_poly_comm!(NapiGVesta, Vesta, Fp);
}
10 changes: 5 additions & 5 deletions plonk-napi/src/poseidon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arkworks::{WasmPastaFp, WasmPastaFq};
use crate::wrappers::field::{NapiPastaFp, NapiPastaFq};
use mina_curves::pasta::{Fp, Fq};
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
use napi::bindgen_prelude::*;
Expand All @@ -15,7 +15,7 @@ pub fn caml_pasta_fp_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Arr

println!("from native rust");

let mut state_vec: Vec<Fp> = FlatVector::<WasmPastaFp>::from_bytes(state.to_vec())
let mut state_vec: Vec<Fp> = FlatVector::<NapiPastaFp>::from_bytes(state.to_vec())
.into_iter()
.map(Into::into)
.collect();
Expand All @@ -27,7 +27,7 @@ pub fn caml_pasta_fp_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Arr

let res: Vec<u8> = state_vec
.into_iter()
.map(WasmPastaFp)
.map(NapiPastaFp)
.flat_map(FlatVectorElem::flatten)
.collect();

Expand All @@ -42,7 +42,7 @@ pub fn caml_pasta_fq_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Arr

println!("from native rust");

let mut state_vec: Vec<Fq> = FlatVector::<WasmPastaFq>::from_bytes(state.to_vec())
let mut state_vec: Vec<Fq> = FlatVector::<NapiPastaFq>::from_bytes(state.to_vec())
.into_iter()
.map(Into::into)
.collect();
Expand All @@ -54,7 +54,7 @@ pub fn caml_pasta_fq_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Arr

let res: Vec<u8> = state_vec
.into_iter()
.map(WasmPastaFq)
.map(NapiPastaFq)
.flat_map(FlatVectorElem::flatten)
.collect();

Expand Down
Loading
Loading