Skip to content

Commit 9e739fe

Browse files
authored
Merge pull request #3340 from o1-labs/native/napi-polycomm
(`proof-systems`) Native Prover with Napi - PolyComm
2 parents 1859dc8 + 6a9450a commit 9e739fe

File tree

7 files changed

+626
-8
lines changed

7 files changed

+626
-8
lines changed

plonk-napi/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
mod build_info;
22
mod circuit;
3-
pub mod plonk_verifier_index;
3+
mod plonk_verifier_index;
4+
mod poly_comm;
45
mod poseidon;
56
mod prover_index;
67
mod types;
7-
8-
pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_block_cipher};
8+
mod vector;
9+
mod wrappers;
910

1011
pub use circuit::prover_to_json;
12+
pub use plonk_verifier_index::{
13+
caml_pasta_fp_plonk_verifier_index_shifts, caml_pasta_fq_plonk_verifier_index_shifts,
14+
};
15+
pub use poly_comm::{
16+
pallas::NapiFqPolyComm as WasmFqPolyComm, vesta::NapiFpPolyComm as WasmFpPolyComm,
17+
};
18+
pub use poseidon::{caml_pasta_fp_poseidon_block_cipher, caml_pasta_fq_poseidon_block_cipher};
1119
pub use prover_index::{prover_index_from_bytes, prover_index_to_bytes};
1220
pub use types::WasmPastaFpPlonkIndex;
21+
pub use vector::{fp::NapiVecVecFp as WasmVecVecFp, fq::NapiVecVecFq as WasmVecVecFq};
22+
pub use wrappers::{
23+
field::{NapiPastaFp as WasmPastaFp, NapiPastaFq as WasmPastaFq},
24+
group::{NapiGPallas as WasmGPallas, NapiGVesta as WasmGVesta},
25+
};

plonk-napi/src/poly_comm.rs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use crate::vector::NapiVector;
2+
use napi::bindgen_prelude::{ClassInstance, FromNapiValue};
3+
use napi_derive::napi;
4+
use paste::paste;
5+
use poly_commitment::commitment::PolyComm;
6+
use serde::{Deserialize, Serialize};
7+
8+
macro_rules! impl_poly_comm {
9+
(
10+
$NapiG:ty,
11+
$g:ty,
12+
$field_name:ident
13+
) => {
14+
paste! {
15+
#[napi(js_name = [<"Wasm" $field_name "PolyComm">])]
16+
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
17+
pub struct [<Napi $field_name:camel PolyComm>] {
18+
#[napi(skip)]
19+
pub unshifted: NapiVector<$NapiG>,
20+
#[napi(skip)]
21+
pub shifted: Option<$NapiG>,
22+
}
23+
24+
#[napi]
25+
impl [<Napi $field_name:camel PolyComm>] {
26+
#[napi(constructor)]
27+
pub fn new(unshifted: NapiVector<$NapiG>, shifted: Option<$NapiG>) -> Self {
28+
assert!(
29+
shifted.is_none(),
30+
"mina#14628: Shifted commitments are deprecated and must not be used",
31+
);
32+
Self { unshifted, shifted }
33+
}
34+
35+
#[napi(getter)]
36+
pub fn unshifted(&self) -> NapiVector<$NapiG> {
37+
self.unshifted.clone()
38+
}
39+
40+
#[napi(setter, js_name = "set_unshifted")]
41+
pub fn set_unshifted(&mut self, x: NapiVector<$NapiG>) {
42+
self.unshifted = x;
43+
}
44+
45+
#[napi(getter)]
46+
pub fn shifted(&self) -> Option<$NapiG> {
47+
self.shifted.clone()
48+
}
49+
50+
#[napi(setter, js_name = "set_shifted")]
51+
pub fn set_shifted(&mut self, value: Option<$NapiG>) {
52+
self.shifted = value;
53+
}
54+
}
55+
56+
impl From<PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
57+
fn from(x: PolyComm<$g>) -> Self {
58+
let PolyComm { chunks } = x;
59+
let unshifted: Vec<$NapiG> = chunks.into_iter().map(Into::into).collect();
60+
Self {
61+
unshifted: unshifted.into(),
62+
shifted: None,
63+
}
64+
}
65+
}
66+
67+
impl From<&PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
68+
fn from(x: &PolyComm<$g>) -> Self {
69+
let unshifted: Vec<$NapiG> = x.chunks.iter().map(|chunk| (*chunk).into()).collect();
70+
Self {
71+
unshifted: unshifted.into(),
72+
shifted: None,
73+
}
74+
}
75+
}
76+
77+
impl From<[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
78+
fn from(x: [<Napi $field_name:camel PolyComm>]) -> Self {
79+
let [<Napi $field_name:camel PolyComm>] { unshifted, shifted } = x;
80+
assert!(
81+
shifted.is_none(),
82+
"mina#14628: Shifted commitments are deprecated and must not be used",
83+
);
84+
PolyComm {
85+
chunks: Vec::<$NapiG>::from(unshifted)
86+
.into_iter()
87+
.map(Into::into)
88+
.collect(),
89+
}
90+
}
91+
}
92+
93+
impl From<&[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
94+
fn from(x: &[<Napi $field_name:camel PolyComm>]) -> Self {
95+
assert!(
96+
x.shifted.is_none(),
97+
"mina#14628: Shifted commitments are deprecated and must not be used",
98+
);
99+
PolyComm {
100+
chunks: x
101+
.unshifted
102+
.iter()
103+
.cloned()
104+
.map(Into::into)
105+
.collect(),
106+
}
107+
}
108+
}
109+
110+
impl FromNapiValue for [<Napi $field_name:camel PolyComm>] {
111+
unsafe fn from_napi_value(
112+
env: napi::sys::napi_env,
113+
napi_val: napi::sys::napi_value,
114+
) -> napi::Result<Self> {
115+
let instance = <ClassInstance<[<Napi $field_name:camel PolyComm>]> as FromNapiValue>::from_napi_value(env, napi_val)?;
116+
Ok((*instance).clone())
117+
}
118+
}
119+
120+
}
121+
};
122+
}
123+
124+
pub mod pallas {
125+
use super::*;
126+
use crate::wrappers::group::NapiGPallas;
127+
use mina_curves::pasta::Pallas;
128+
129+
impl_poly_comm!(NapiGPallas, Pallas, Fq);
130+
}
131+
132+
pub mod vesta {
133+
use super::*;
134+
use crate::wrappers::group::NapiGVesta;
135+
use mina_curves::pasta::Vesta;
136+
137+
impl_poly_comm!(NapiGVesta, Vesta, Fp);
138+
}

plonk-napi/src/poseidon.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use arkworks::{WasmPastaFp, WasmPastaFq};
1+
use crate::wrappers::field::{NapiPastaFp, NapiPastaFq};
22
use mina_curves::pasta::{Fp, Fq};
33
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
44
use napi::bindgen_prelude::*;
@@ -15,7 +15,7 @@ pub fn caml_pasta_fp_poseidon_block_cipher(state: Uint8Array) -> Result<Uint8Arr
1515

1616
println!("from native rust");
1717

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

2828
let res: Vec<u8> = state_vec
2929
.into_iter()
30-
.map(WasmPastaFp)
30+
.map(NapiPastaFp)
3131
.flat_map(FlatVectorElem::flatten)
3232
.collect();
3333

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

4343
println!("from native rust");
4444

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

5555
let res: Vec<u8> = state_vec
5656
.into_iter()
57-
.map(WasmPastaFq)
57+
.map(NapiPastaFq)
5858
.flat_map(FlatVectorElem::flatten)
5959
.collect();
6060

0 commit comments

Comments
 (0)