@@ -20,7 +20,8 @@ use mina_poseidon::{constants::PlonkSpongeConstantsKimchi, sponge::DefaultFqSpon
2020use serde:: { Deserialize , Serialize } ;
2121use std:: {
2222 fs:: { File , OpenOptions } ,
23- io:: { BufReader , BufWriter , Seek , SeekFrom :: Start } ,
23+ io:: { BufReader , BufWriter , Cursor , Seek , SeekFrom :: Start } ,
24+ sync:: Arc ,
2425} ;
2526use wasm_bindgen:: prelude:: * ;
2627use wasm_types:: FlatVector as WasmFlatVector ;
@@ -35,6 +36,71 @@ pub struct WasmPastaFpPlonkIndex(
3536 #[ wasm_bindgen( skip) ] pub Box < ProverIndex < GAffine , OpeningProof < GAffine > > > ,
3637) ;
3738
39+ // TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around
40+ #[ derive( Serialize , Deserialize ) ]
41+ struct SerializedProverIndex {
42+ prover_index : Vec < u8 > ,
43+ srs : Vec < u8 > ,
44+ }
45+
46+ // TOOD: remove incl all dependencies when no longer needed and we only pass napi objects around
47+ #[ wasm_bindgen]
48+ impl WasmPastaFpPlonkIndex {
49+ #[ wasm_bindgen( js_name = "serialize" ) ]
50+ pub fn serialize ( & self ) -> Result < Vec < u8 > , JsError > {
51+ serialize_prover_index ( self . 0 . as_ref ( ) )
52+ . map_err ( |e| JsError :: new ( & format ! ( "WasmPastaFpPlonkIndex::serialize: {e}" ) ) )
53+ }
54+
55+ #[ wasm_bindgen( js_name = "deserialize" ) ]
56+ pub fn deserialize ( bytes : & [ u8 ] ) -> Result < WasmPastaFpPlonkIndex , JsError > {
57+ deserialize_prover_index ( bytes)
58+ . map ( WasmPastaFpPlonkIndex )
59+ . map_err ( |e| JsError :: new ( & format ! ( "WasmPastaFpPlonkIndex::deserialize: {e}" ) ) )
60+ }
61+ }
62+
63+ fn serialize_prover_index (
64+ index : & ProverIndex < GAffine , OpeningProof < GAffine > > ,
65+ ) -> Result < Vec < u8 > , String > {
66+ let prover_index = rmp_serde:: to_vec ( index) . map_err ( |e| e. to_string ( ) ) ?;
67+
68+ let mut srs = Vec :: new ( ) ;
69+ index
70+ . srs
71+ . serialize ( & mut rmp_serde:: Serializer :: new ( & mut srs) )
72+ . map_err ( |e| e. to_string ( ) ) ?;
73+
74+ let serialized = SerializedProverIndex { prover_index, srs } ;
75+
76+ rmp_serde:: to_vec ( & serialized) . map_err ( |e| e. to_string ( ) )
77+ }
78+
79+ fn deserialize_prover_index (
80+ bytes : & [ u8 ] ,
81+ ) -> Result < Box < ProverIndex < GAffine , OpeningProof < GAffine > > > , String > {
82+ let serialized: SerializedProverIndex =
83+ rmp_serde:: from_slice ( bytes) . map_err ( |e| e. to_string ( ) ) ?;
84+
85+ let mut index: ProverIndex < GAffine , OpeningProof < GAffine > > = ProverIndex :: deserialize (
86+ & mut rmp_serde:: Deserializer :: new ( Cursor :: new ( serialized. prover_index ) ) ,
87+ )
88+ . map_err ( |e| e. to_string ( ) ) ?;
89+
90+ let srs = poly_commitment:: ipa:: SRS :: < GAffine > :: deserialize ( & mut rmp_serde:: Deserializer :: new (
91+ Cursor :: new ( serialized. srs ) ,
92+ ) )
93+ . map_err ( |e| e. to_string ( ) ) ?;
94+
95+ index. srs = Arc :: new ( srs) ;
96+
97+ let ( linearization, powers_of_alpha) = expr_linearization ( Some ( & index. cs . feature_flags ) , true ) ;
98+ index. linearization = linearization;
99+ index. powers_of_alpha = powers_of_alpha;
100+
101+ Ok ( Box :: new ( index) )
102+ }
103+
38104// This should mimic LookupTable structure
39105#[ wasm_bindgen]
40106pub struct WasmPastaFpLookupTable {
0 commit comments