Skip to content

Commit e913408

Browse files
committed
napi: impls for polycomm
1 parent b9363f4 commit e913408

File tree

4 files changed

+120
-2
lines changed

4 files changed

+120
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plonk-napi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ arkworks.workspace = true
2626
mina-curves = { path = "../curves" }
2727
mina-poseidon = { path = "../poseidon" }
2828
o1-utils = { path = "../utils" }
29+
poly-commitment = { path = "../poly-commitment" }
2930

3031
getrandom.workspace = true
3132
libc.workspace = true

plonk-napi/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
pub(crate) mod poseidon;
22
pub(crate) mod wrappers;
33
pub(crate) mod wasm_vector;
4+
pub(crate) mod poly_comm;
45

56
pub use poseidon::{
67
caml_pasta_fp_poseidon_block_cipher,
78
caml_pasta_fq_poseidon_block_cipher,
89
};
910

10-
pub use wrappers::field::{WasmPastaFp, WasmPastaFq};
1111
pub use wrappers::group::{WasmGPallas, WasmGVesta};
12-
pub use wasm_vector::{fp::WasmVecVecFp, fq::WasmVecVecFq};
12+
pub use wasm_vector::{fp::WasmVecVecFp, fq::WasmVecVecFq};
13+
pub use poly_comm::{pallas::WasmFqPolyComm, vesta::WasmFpPolyComm};

plonk-napi/src/poly_comm.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use crate::{
2+
wrappers::group::{WasmGPallas, WasmGVesta},
3+
wasm_vector::WasmVector,
4+
};
5+
use napi_derive::napi;
6+
use paste::paste;
7+
use poly_commitment::commitment::PolyComm;
8+
9+
macro_rules! impl_poly_comm {
10+
(
11+
$wasm_g:ty,
12+
$g:ty,
13+
$field_name:ident
14+
) => {
15+
paste! {
16+
#[napi]
17+
#[derive(Clone)]
18+
pub struct [<Wasm $field_name:camel PolyComm>] {
19+
#[napi(skip)]
20+
pub unshifted: WasmVector<$wasm_g>,
21+
pub shifted: Option<$wasm_g>,
22+
}
23+
24+
#[napi]
25+
impl [<Wasm $field_name:camel PolyComm>] {
26+
#[napi(constructor)]
27+
pub fn new(unshifted: WasmVector<$wasm_g>, shifted: Option<$wasm_g>) -> 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) -> WasmVector<$wasm_g> {
37+
self.unshifted.clone()
38+
}
39+
40+
#[napi(setter)]
41+
pub fn set_unshifted(&mut self, value: WasmVector<$wasm_g>) {
42+
self.unshifted = value;
43+
}
44+
}
45+
46+
impl From<PolyComm<$g>> for [<Wasm $field_name:camel PolyComm>] {
47+
fn from(value: PolyComm<$g>) -> Self {
48+
let PolyComm { chunks } = value;
49+
let unshifted: Vec<$wasm_g> = chunks.into_iter().map(Into::into).collect();
50+
Self {
51+
unshifted: unshifted.into(),
52+
shifted: None,
53+
}
54+
}
55+
}
56+
57+
impl From<&PolyComm<$g>> for [<Wasm $field_name:camel PolyComm>] {
58+
fn from(value: &PolyComm<$g>) -> Self {
59+
let unshifted: Vec<$wasm_g> = value.chunks.iter().map(|chunk| (*chunk).into()).collect();
60+
Self {
61+
unshifted: unshifted.into(),
62+
shifted: None,
63+
}
64+
}
65+
}
66+
67+
impl From<[<Wasm $field_name:camel PolyComm>]> for PolyComm<$g> {
68+
fn from(value: [<Wasm $field_name:camel PolyComm>]) -> Self {
69+
let [<Wasm $field_name:camel PolyComm>] { unshifted, shifted } = value;
70+
assert!(
71+
shifted.is_none(),
72+
"mina#14628: Shifted commitments are deprecated and must not be used",
73+
);
74+
PolyComm {
75+
chunks: Vec::<$wasm_g>::from(unshifted)
76+
.into_iter()
77+
.map(Into::into)
78+
.collect(),
79+
}
80+
}
81+
}
82+
83+
impl From<&[<Wasm $field_name:camel PolyComm>]> for PolyComm<$g> {
84+
fn from(value: &[<Wasm $field_name:camel PolyComm>]) -> Self {
85+
assert!(
86+
value.shifted.is_none(),
87+
"mina#14628: Shifted commitments are deprecated and must not be used",
88+
);
89+
PolyComm {
90+
chunks: value
91+
.unshifted
92+
.iter()
93+
.cloned()
94+
.map(Into::into)
95+
.collect(),
96+
}
97+
}
98+
}
99+
}
100+
};
101+
}
102+
103+
pub mod pallas {
104+
use super::*;
105+
use mina_curves::pasta::Pallas as GAffine;
106+
107+
impl_poly_comm!(WasmGPallas, GAffine, Fq);
108+
}
109+
110+
pub mod vesta {
111+
use super::*;
112+
use mina_curves::pasta::Vesta as GAffine;
113+
114+
impl_poly_comm!(WasmGVesta, GAffine, Fp);
115+
}

0 commit comments

Comments
 (0)