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 ) ;
0 commit comments