arkworks/
pasta_fp.rs

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