plonk_wasm/
poly_comm.rs

1use crate::wasm_vector::WasmVector;
2use paste::paste;
3macro_rules! impl_poly_comm {
4    (
5     $WasmG: ty,
6     $G: ty,
7     $field_name: ident
8     ) => {
9        paste! {
10            use wasm_bindgen::prelude::*;
11            use poly_commitment::commitment::PolyComm;
12
13            #[wasm_bindgen]
14            #[derive(Clone)]
15            pub struct [<Wasm $field_name:camel PolyComm>] {
16                #[wasm_bindgen(skip)]
17                pub unshifted: WasmVector<$WasmG>,
18                pub shifted: Option<$WasmG>,
19            }
20
21            type WasmPolyComm = [<Wasm $field_name:camel PolyComm>];
22
23            #[wasm_bindgen]
24            impl [<Wasm $field_name:camel PolyComm>] {
25                #[wasm_bindgen(constructor)]
26                pub fn new(unshifted: WasmVector<$WasmG>, shifted: Option<$WasmG>) -> Self {
27                    assert!(
28                        shifted.is_none(),
29                        "mina#14628: Shifted commitments are deprecated and must not be used"
30                    );
31                    WasmPolyComm { unshifted, shifted }
32                }
33
34                #[wasm_bindgen(getter)]
35                pub fn unshifted(&self) -> WasmVector<$WasmG> {
36                    self.unshifted.clone()
37                }
38
39                #[wasm_bindgen(setter)]
40                pub fn set_unshifted(&mut self, x: WasmVector<$WasmG>) {
41                    self.unshifted = x
42                }
43            }
44
45            impl From<PolyComm<$G>> for WasmPolyComm {
46                fn from(x: PolyComm<$G>) -> Self {
47                    let PolyComm { chunks } = x;
48                    let unshifted: Vec<$WasmG> =
49                        chunks.into_iter().map(|x| x.into()).collect();
50                    WasmPolyComm {
51                        unshifted: unshifted.into(),
52                        shifted: None
53                    }
54                }
55            }
56
57            impl From<&PolyComm<$G>> for WasmPolyComm {
58                fn from(x: &PolyComm<$G>) -> Self {
59                    let unshifted: Vec<$WasmG> =
60                        x.chunks.iter().map(|x| x.into()).collect();
61                    WasmPolyComm {
62                        unshifted: unshifted.into(),
63                        shifted: None,
64                    }
65                }
66            }
67
68            impl From<WasmPolyComm> for PolyComm<$G> {
69                fn from(x: WasmPolyComm) -> Self {
70                    let WasmPolyComm {unshifted, shifted} = x;
71                    assert!(
72                        shifted.is_none(),
73                        "mina#14628: Shifted commitments are deprecated and must not be used"
74                    );
75                    PolyComm {
76                        chunks: (*unshifted).iter().map(|x| { (*x).into() }).collect(),
77                    }
78                }
79            }
80
81            impl From<&WasmPolyComm> for PolyComm<$G> {
82                fn from(x: &WasmPolyComm) -> Self {
83                    assert!(
84                        x.shifted.is_none(),
85                        "mina#14628: Shifted commitments are deprecated and must not be used"
86                    );
87                    PolyComm {
88                        chunks: x.unshifted.iter().map(|x| { (*x).into() }).collect(),
89                    }
90                }
91            }
92        }
93    };
94}
95
96pub mod pallas {
97    use super::*;
98    use crate::arkworks::group_affine::WasmGPallas;
99    use mina_curves::pasta::Pallas as GAffine;
100
101    impl_poly_comm!(WasmGPallas, GAffine, Fq);
102}
103
104pub mod vesta {
105    use super::*;
106    use crate::arkworks::group_affine::WasmGVesta;
107    use mina_curves::pasta::Vesta as GAffine;
108
109    impl_poly_comm!(WasmGVesta, GAffine, Fp);
110}