-
Notifications
You must be signed in to change notification settings - Fork 18
/
browser.rs
306 lines (276 loc) · 9 KB
/
browser.rs
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! special supported structure and functions for browser and wasm
//! ===========
use std::str::FromStr;
use rings_snark::prelude::ff;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsError;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::future_to_promise;
use super::*;
use crate::backend::types::snark::SNARKProofTask;
use crate::backend::types::snark::SNARKVerifyTask;
use crate::backend::BackendMessageHandlerDynObj;
use crate::prelude::rings_core::utils::js_value;
use crate::provider::browser::ProviderRef;
/// We need this ref to pass Task ref to js_sys
#[wasm_bindgen]
#[derive(Deserialize, Serialize)]
pub struct SNARKProofTaskRef {
inner: Arc<SNARKProofTask>,
}
#[wasm_bindgen]
impl SNARKProofTaskRef {
/// Make snark proof task ref splitable
pub fn split(&self, n: usize) -> Vec<SNARKProofTaskRef> {
self.inner.split(n).into_iter().map(|t| t.into()).collect()
}
/// serialize SNARKProofTaskRef to json
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(self)?)
}
/// deserialize SNARKProofTaskRef from json
pub fn from_json(s: String) -> Result<SNARKProofTaskRef> {
Ok(serde_json::from_str(&s)?)
}
}
impl AsRef<SNARKProofTask> for SNARKProofTaskRef {
fn as_ref(&self) -> &SNARKProofTask {
self.inner.as_ref()
}
}
impl std::ops::Deref for SNARKProofTaskRef {
type Target = Arc<SNARKProofTask>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<SNARKProofTask> for SNARKProofTaskRef {
fn from(t: SNARKProofTask) -> SNARKProofTaskRef {
SNARKProofTaskRef { inner: Arc::new(t) }
}
}
/// We need this ref to pass Task ref to js_sys
#[wasm_bindgen]
#[derive(Deserialize, Serialize)]
pub struct SNARKVerifyTaskRef {
inner: Arc<SNARKVerifyTask>,
}
#[wasm_bindgen]
impl SNARKVerifyTaskRef {
/// serialize SNARKPVerifyRef to json
pub fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(self)?)
}
/// de serialize SNARKPVerifyRef from json
pub fn from_json(s: String) -> Result<SNARKVerifyTaskRef> {
Ok(serde_json::from_str(&s)?)
}
}
impl AsRef<SNARKVerifyTask> for SNARKVerifyTaskRef {
fn as_ref(&self) -> &SNARKVerifyTask {
self.inner.as_ref()
}
}
impl std::ops::Deref for SNARKVerifyTaskRef {
type Target = Arc<SNARKVerifyTask>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<SNARKVerifyTask> for SNARKVerifyTaskRef {
fn from(t: SNARKVerifyTask) -> SNARKVerifyTaskRef {
SNARKVerifyTaskRef { inner: Arc::new(t) }
}
}
#[wasm_bindgen]
impl SNARKVerifyTaskRef {
/// Clone snark verify ref, and hold the arc
/// this function is useful on js_sys
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> SNARKVerifyTaskRef {
SNARKVerifyTaskRef {
inner: self.inner.clone(),
}
}
}
#[wasm_bindgen]
impl SNARKProofTaskRef {
/// Clone snark proof ref, and hold the arc
/// this function is useful on js_sys
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> SNARKProofTaskRef {
SNARKProofTaskRef {
inner: self.inner.clone(),
}
}
}
#[wasm_bindgen]
impl SNARKBehaviour {
/// Get behaviour as dyn obj ref
pub fn as_dyn_obj(self) -> BackendMessageHandlerDynObj {
BackendMessageHandlerDynObj::new(self.into())
}
/// Handle js native message
pub fn handle_snark_task_message(
self,
provider: ProviderRef,
ctx: JsValue,
msg: JsValue,
) -> js_sys::Promise {
let ins = self.clone();
future_to_promise(async move {
let ctx = js_value::deserialize::<MessagePayload>(ctx)?;
let msg = js_value::deserialize::<SNARKTaskMessage>(msg)?;
ins.handle_message(provider.inner(), &ctx, &msg)
.await
.map_err(|e| Error::BackendError(e.to_string()))?;
Ok(JsValue::NULL)
})
}
/// gen proof task with circuits, this function is use for solo proof
/// you can call [SNARKBehaviour::handle_snark_proof_task_ref] later to finalize the proof
pub fn gen_proof_task_ref(circuits: Vec<Circuit>) -> Result<SNARKProofTaskRef> {
SNARKBehaviour::gen_proof_task(circuits).map(|t| t.into())
}
/// handle snark proof task ref, this function is helpful for js_sys
pub fn handle_snark_proof_task_ref(data: SNARKProofTaskRef) -> Result<SNARKVerifyTaskRef> {
Self::handle_snark_proof_task(data).map(|x| x.into())
}
/// handle snark verify task ref, this function is helpful for js_sys
pub fn handle_snark_verify_task_ref(
data: SNARKVerifyTaskRef,
snark: SNARKProofTaskRef,
) -> Result<bool> {
Self::handle_snark_verify_task(data, snark)
}
/// send proof task to did
pub fn send_proof_task_to(
&self,
provider: ProviderRef,
task: SNARKProofTaskRef,
did: String,
) -> js_sys::Promise {
let ins = self.clone();
future_to_promise(async move {
let ret = ins
.send_proof_task(
provider.inner().clone(),
task.as_ref(),
Did::from_str(&did)?,
)
.await
.map_err(JsError::from)?;
Ok(JsValue::from(ret))
})
}
/// Generate a proof task and send it to did
pub fn gen_and_send_proof_task_to(
&self,
provider: ProviderRef,
circuits: Vec<Circuit>,
did: String,
) -> js_sys::Promise {
let ins = self.clone();
future_to_promise(async move {
let ret = ins
.gen_and_send_proof_task(provider.inner().clone(), circuits, Did::from_str(&did)?)
.await
.map_err(JsError::from)?;
Ok(JsValue::from(ret))
})
}
/// create new instance for browser
/// which support syntax `new SNARKBehaviour` in browser env
#[wasm_bindgen(constructor)]
pub fn new_instance() -> SNARKBehaviour {
SNARKBehaviour::default()
}
/// Clone snarkbehaviour, and hold the arc
/// this function is useful on js_sys
#[allow(clippy::should_implement_trait)]
pub fn clone(&self) -> SNARKBehaviour {
SNARKBehaviour {
inner: self.inner.clone(),
}
}
}
#[wasm_bindgen]
impl SNARKTaskBuilder {
/// create new instance for browser
/// which support syntax `new SNARKTaskBuilder` in browser env
#[wasm_bindgen(constructor)]
pub fn new_instance(
r1cs_path: String,
witness_wasm_path: String,
field: SupportedPrimeField,
) -> js_sys::Promise {
future_to_promise(async move {
let ret = SNARKTaskBuilder::from_remote(r1cs_path, witness_wasm_path, field)
.await
.map_err(JsError::from)?;
Ok(JsValue::from(ret))
})
}
}
/// Convert biguint to finatefield
pub(crate) fn bigint2ff<F: ff::PrimeField>(v: js_sys::BigInt) -> Result<F> {
let repr = v
.to_string(10)
.map_err(|e| Error::SNARKFFRangeError(format!("{:?}", e)))?
.as_string();
if let Some(v) = &repr {
Ok(F::from_str_vartime(v).ok_or(Error::FailedToLoadFF())?)
} else {
Err(Error::SNARKBigIntValueEmpty())
}
}
/// Convert BigInt from js to [Field]
#[wasm_bindgen]
pub fn bigint_to_field(v: js_sys::BigInt, field: SupportedPrimeField) -> Result<Field> {
let ret = match field {
SupportedPrimeField::Vesta => {
type F = <provider::VestaEngine as Engine>::Scalar;
Field {
value: FieldEnum::Vesta(bigint2ff::<F>(v)?),
}
}
SupportedPrimeField::Pallas => {
type F = <provider::PallasEngine as Engine>::Scalar;
Field {
value: FieldEnum::Pallas(bigint2ff::<F>(v)?),
}
}
SupportedPrimeField::Bn256KZG => {
type F = <provider::hyperkzg::Bn256EngineKZG as Engine>::Scalar;
Field {
value: FieldEnum::Bn256KZG(bigint2ff::<F>(v)?),
}
}
};
Ok(ret)
}
#[wasm_bindgen]
impl Input {
/// Convert [["foo", [BigInt(2), BigInt(3)]], ["bar", [BigInt(4), BigInt(5)]]] to Input with given field
pub fn from_array(input: js_sys::Array, field: SupportedPrimeField) -> Input {
let data: Vec<(String, Vec<Field>)> = input
.into_iter()
.map(|s| {
let lst = js_sys::Array::from(&s);
let p = lst
.get(0)
.as_string()
.expect("first argument should be string like");
let v: Vec<Field> = js_sys::Array::from(&lst.get(1))
.into_iter()
.map(|p| {
bigint_to_field(p.into(), field.clone())
.expect("failed to cover bigint to field")
})
.collect();
(p, v)
})
.collect();
Input(data)
}
}