1extern crate alloc;
2
3use alloc::vec::Vec;
4use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
5use mina_curves::pasta::Fq;
6use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi};
7
8#[repr(C)]
9#[derive(Clone, Copy, Debug)]
10pub struct WasmPastaFq(pub Fq);
11
12impl wasm_types::FlatVectorElem for WasmPastaFq {
13 const FLATTENED_SIZE: usize = core::mem::size_of::<Fq>();
14 fn flatten(self) -> Vec<u8> {
15 let mut bytes: Vec<u8> = Vec::with_capacity(Self::FLATTENED_SIZE);
16 self.0.serialize_compressed(&mut bytes).unwrap();
17 bytes
18 }
19 fn unflatten(flat: Vec<u8>) -> Self {
20 WasmPastaFq(Fq::deserialize_compressed(flat.as_slice()).unwrap())
21 }
22}
23
24impl From<Fq> for WasmPastaFq {
25 fn from(x: Fq) -> Self {
26 WasmPastaFq(x)
27 }
28}
29
30impl From<WasmPastaFq> for Fq {
31 fn from(x: WasmPastaFq) -> Self {
32 x.0
33 }
34}
35
36impl<'a> From<&'a WasmPastaFq> for &'a Fq {
37 fn from(x: &'a WasmPastaFq) -> Self {
38 &x.0
39 }
40}
41
42impl wasm_bindgen::describe::WasmDescribe for WasmPastaFq {
43 fn describe() {
44 <Vec<u8> as wasm_bindgen::describe::WasmDescribe>::describe();
45 }
46}
47
48impl FromWasmAbi for WasmPastaFq {
49 type Abi = <Vec<u8> as FromWasmAbi>::Abi;
50 unsafe fn from_abi(js: Self::Abi) -> Self {
51 let bytes: Vec<u8> = FromWasmAbi::from_abi(js);
52 WasmPastaFq(Fq::deserialize_compressed(bytes.as_slice()).unwrap())
53 }
54}
55
56impl IntoWasmAbi for WasmPastaFq {
57 type Abi = <Vec<u8> as FromWasmAbi>::Abi;
58 fn into_abi(self) -> Self::Abi {
59 let mut bytes: Vec<u8> = Vec::with_capacity(core::mem::size_of::<Self>());
60 self.0.serialize_compressed(&mut bytes).unwrap();
61 bytes.into_abi()
62 }
63}
64
65impl OptionIntoWasmAbi for WasmPastaFq {
66 fn none() -> Self::Abi {
67 <Vec<u8> as OptionIntoWasmAbi>::none()
68 }
69}
70
71impl OptionFromWasmAbi for WasmPastaFq {
72 fn is_none(abi: &Self::Abi) -> bool {
73 <Vec<u8> as OptionFromWasmAbi>::is_none(abi)
74 }
75}