-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpoly_comm.rs
More file actions
138 lines (122 loc) · 4.8 KB
/
Copy pathpoly_comm.rs
File metadata and controls
138 lines (122 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::vector::NapiVector;
use napi::bindgen_prelude::{ClassInstance, FromNapiValue};
use napi_derive::napi;
use paste::paste;
use poly_commitment::commitment::PolyComm;
use serde::{Deserialize, Serialize};
macro_rules! impl_poly_comm {
(
$NapiG:ty,
$g:ty,
$field_name:ident
) => {
paste! {
#[napi(js_name = [<"Wasm" $field_name "PolyComm">])]
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct [<Napi $field_name:camel PolyComm>] {
#[napi(skip)]
pub unshifted: NapiVector<$NapiG>,
#[napi(skip)]
pub shifted: Option<$NapiG>,
}
#[napi]
impl [<Napi $field_name:camel PolyComm>] {
#[napi(constructor)]
pub fn new(unshifted: NapiVector<$NapiG>, shifted: Option<$NapiG>) -> Self {
assert!(
shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
Self { unshifted, shifted }
}
#[napi(getter)]
pub fn unshifted(&self) -> NapiVector<$NapiG> {
self.unshifted.clone()
}
#[napi(setter, js_name = "set_unshifted")]
pub fn set_unshifted(&mut self, x: NapiVector<$NapiG>) {
self.unshifted = x;
}
#[napi(getter)]
pub fn shifted(&self) -> Option<$NapiG> {
self.shifted.clone()
}
#[napi(setter, js_name = "set_shifted")]
pub fn set_shifted(&mut self, value: Option<$NapiG>) {
self.shifted = value;
}
}
impl From<PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
fn from(x: PolyComm<$g>) -> Self {
let PolyComm { chunks } = x;
let unshifted: Vec<$NapiG> = chunks.into_iter().map(Into::into).collect();
Self {
unshifted: unshifted.into(),
shifted: None,
}
}
}
impl From<&PolyComm<$g>> for [<Napi $field_name:camel PolyComm>] {
fn from(x: &PolyComm<$g>) -> Self {
let unshifted: Vec<$NapiG> = x.chunks.iter().map(|chunk| (*chunk).into()).collect();
Self {
unshifted: unshifted.into(),
shifted: None,
}
}
}
impl From<[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
fn from(x: [<Napi $field_name:camel PolyComm>]) -> Self {
let [<Napi $field_name:camel PolyComm>] { unshifted, shifted } = x;
assert!(
shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
PolyComm {
chunks: Vec::<$NapiG>::from(unshifted)
.into_iter()
.map(Into::into)
.collect(),
}
}
}
impl From<&[<Napi $field_name:camel PolyComm>]> for PolyComm<$g> {
fn from(x: &[<Napi $field_name:camel PolyComm>]) -> Self {
assert!(
x.shifted.is_none(),
"mina#14628: Shifted commitments are deprecated and must not be used",
);
PolyComm {
chunks: x
.unshifted
.iter()
.cloned()
.map(Into::into)
.collect(),
}
}
}
impl FromNapiValue for [<Napi $field_name:camel PolyComm>] {
unsafe fn from_napi_value(
env: napi::sys::napi_env,
napi_val: napi::sys::napi_value,
) -> napi::Result<Self> {
let instance = <ClassInstance<[<Napi $field_name:camel PolyComm>]> as FromNapiValue>::from_napi_value(env, napi_val)?;
Ok((*instance).clone())
}
}
}
};
}
pub mod pallas {
use super::*;
use crate::wrappers::group::NapiGPallas;
use mina_curves::pasta::Pallas;
impl_poly_comm!(NapiGPallas, Pallas, Fq);
}
pub mod vesta {
use super::*;
use crate::wrappers::group::NapiGVesta;
use mina_curves::pasta::Vesta;
impl_poly_comm!(NapiGVesta, Vesta, Fp);
}