Skip to content

Commit c1cc82f

Browse files
committed
napi: wrapper for field types
1 parent cd24f57 commit c1cc82f

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

plonk-napi/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
mod poseidon;
1+
pub(crate) mod poseidon;
2+
pub(crate) mod wrappers;
23

34
pub use poseidon::{
45
caml_pasta_fp_poseidon_block_cipher,
56
caml_pasta_fq_poseidon_block_cipher,
67
};
8+
9+
pub use wrappers::field::{WasmPastaFp, WasmPastaFq};
10+

plonk-napi/src/poseidon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use arkworks::{WasmPastaFp, WasmPastaFq};
1+
use crate::wrappers::field::{WasmPastaFp, WasmPastaFq};
22
use mina_curves::pasta::{Fp, Fq};
33
use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, permutation::poseidon_block_cipher};
44
use napi::bindgen_prelude::*;

plonk-napi/src/wrappers/field.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
2+
use mina_curves::pasta::{Fp, Fq};
3+
use napi::bindgen_prelude::*;
4+
use wasm_types::FlatVectorElem;
5+
6+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7+
pub struct WasmPastaFp(pub Fp);
8+
9+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10+
pub struct WasmPastaFq(pub Fq);
11+
12+
macro_rules! impl_field_wrapper {
13+
($name:ident, $field:ty) => {
14+
impl $name {
15+
fn serialize(&self) -> Vec<u8> {
16+
let mut bytes = Vec::with_capacity(core::mem::size_of::<$field>());
17+
self.0
18+
.serialize_compressed(&mut bytes)
19+
.expect("serialization failure");
20+
bytes
21+
}
22+
23+
fn deserialize(bytes: &[u8]) -> Self {
24+
let value =
25+
<$field>::deserialize_compressed(bytes).expect("deserialization failure");
26+
Self(value)
27+
}
28+
}
29+
30+
impl From<$field> for $name {
31+
fn from(value: $field) -> Self {
32+
Self(value)
33+
}
34+
}
35+
36+
impl From<$name> for $field {
37+
fn from(value: $name) -> Self {
38+
value.0
39+
}
40+
}
41+
42+
impl<'a> From<&'a $name> for &'a $field {
43+
fn from(value: &'a $name) -> Self {
44+
&value.0
45+
}
46+
}
47+
48+
impl FlatVectorElem for $name {
49+
const FLATTENED_SIZE: usize = core::mem::size_of::<$field>();
50+
51+
fn flatten(self) -> Vec<u8> {
52+
self.serialize()
53+
}
54+
55+
fn unflatten(flat: Vec<u8>) -> Self {
56+
Self::deserialize(&flat)
57+
}
58+
}
59+
60+
impl TypeName for $name {
61+
fn type_name() -> &'static str {
62+
<Buffer as TypeName>::type_name()
63+
}
64+
65+
fn value_type() -> ValueType {
66+
<Buffer as TypeName>::value_type()
67+
}
68+
}
69+
70+
impl ValidateNapiValue for $name {
71+
unsafe fn validate(
72+
env: sys::napi_env,
73+
napi_val: sys::napi_value,
74+
) -> Result<sys::napi_value> {
75+
<Buffer as ValidateNapiValue>::validate(env, napi_val)
76+
}
77+
}
78+
79+
impl FromNapiValue for $name {
80+
unsafe fn from_napi_value(
81+
env: sys::napi_env,
82+
napi_val: sys::napi_value,
83+
) -> Result<Self> {
84+
let buffer = <Buffer as FromNapiValue>::from_napi_value(env, napi_val)?;
85+
Ok(Self::deserialize(buffer.as_ref()))
86+
}
87+
}
88+
89+
impl ToNapiValue for $name {
90+
unsafe fn to_napi_value(env: sys::napi_env, val: Self) -> Result<sys::napi_value> {
91+
let buffer = Buffer::from(val.serialize());
92+
<Buffer as ToNapiValue>::to_napi_value(env, buffer)
93+
}
94+
}
95+
};
96+
}
97+
98+
impl_field_wrapper!(WasmPastaFp, Fp);
99+
impl_field_wrapper!(WasmPastaFq, Fq);

plonk-napi/src/wrappers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod field;

0 commit comments

Comments
 (0)