Skip to content

Commit 7496e9c

Browse files
committed
feat: add version byte retrieval methods for RLNProof and RLNWitnessInput, and add missing methods in FFI/WASM for RLNProof and RLNWitnessInput and RLNProofValues
1 parent c7f7488 commit 7496e9c

File tree

5 files changed

+521
-16
lines changed

5 files changed

+521
-16
lines changed

rln-wasm/src/wasm_rln.rs

Lines changed: 176 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ impl WasmRLNProof {
8383
WasmRLNProofValues(self.0.proof_values.clone())
8484
}
8585

86+
#[wasm_bindgen(js_name = getVersionByte)]
87+
pub fn get_version_byte(&self) -> u8 {
88+
self.0.version_byte()
89+
}
90+
8691
#[wasm_bindgen(js_name = toBytesLE)]
8792
pub fn to_bytes_le(&self) -> Result<Uint8Array, String> {
8893
let bytes = rln_proof_to_bytes_le(&self.0).map_err(|err| err.to_string())?;
@@ -166,6 +171,57 @@ impl WasmRLNProofValues {
166171
VecWasmFr::from(self.0.nullifiers().to_vec())
167172
}
168173

174+
#[wasm_bindgen(js_name = getVersionByte)]
175+
pub fn get_version_byte(&self) -> u8 {
176+
self.0.version_byte()
177+
}
178+
179+
#[wasm_bindgen(js_name = modifyRoot)]
180+
pub fn modify_root(&mut self, root: &WasmFr) {
181+
self.0.modify_root(root.inner());
182+
}
183+
184+
#[wasm_bindgen(js_name = modifyX)]
185+
pub fn modify_x(&mut self, x: &WasmFr) {
186+
self.0.modify_x(x.inner());
187+
}
188+
189+
#[wasm_bindgen(js_name = modifyExternalNullifier)]
190+
pub fn modify_external_nullifier(&mut self, external_nullifier: &WasmFr) {
191+
self.0.modify_external_nullifier(external_nullifier.inner());
192+
}
193+
194+
#[cfg(not(feature = "multi-message-id"))]
195+
#[wasm_bindgen(js_name = modifyY)]
196+
pub fn modify_y(&mut self, y: &WasmFr) {
197+
self.0.modify_y(y.inner());
198+
}
199+
200+
#[cfg(not(feature = "multi-message-id"))]
201+
#[wasm_bindgen(js_name = modifyNullifier)]
202+
pub fn modify_nullifier(&mut self, nullifier: &WasmFr) {
203+
self.0.modify_nullifier(nullifier.inner());
204+
}
205+
206+
#[cfg(feature = "multi-message-id")]
207+
#[wasm_bindgen(js_name = modifySelectorUsed)]
208+
pub fn modify_selector_used(&mut self, selector_used: &Uint8Array) {
209+
let selector_used: Vec<bool> = selector_used.to_vec().iter().map(|&b| b != 0).collect();
210+
self.0.modify_selector_used(selector_used);
211+
}
212+
213+
#[cfg(feature = "multi-message-id")]
214+
#[wasm_bindgen(js_name = modifyYs)]
215+
pub fn modify_ys(&mut self, ys: &VecWasmFr) {
216+
self.0.modify_ys(ys.inner());
217+
}
218+
219+
#[cfg(feature = "multi-message-id")]
220+
#[wasm_bindgen(js_name = modifyNullifiers)]
221+
pub fn modify_nullifiers(&mut self, nullifiers: &VecWasmFr) {
222+
self.0.modify_nullifiers(nullifiers.inner());
223+
}
224+
169225
#[wasm_bindgen(js_name = toBytesLE)]
170226
pub fn to_bytes_le(&self) -> Uint8Array {
171227
Uint8Array::from(&rln_proof_values_to_bytes_le(&self.0)[..])
@@ -272,18 +328,114 @@ impl WasmRLNWitnessInput {
272328
Ok(WasmRLNWitnessInput(witness))
273329
}
274330

275-
#[wasm_bindgen(js_name = toBigIntJson)]
276-
pub fn to_bigint_json(&self) -> Result<Object, String> {
277-
let bigint_json = rln_witness_to_bigint_json(&self.0).map_err(|err| err.to_string())?;
331+
#[wasm_bindgen(js_name = getVersionByte)]
332+
pub fn get_version_byte(&self) -> u8 {
333+
self.0.version_byte()
334+
}
278335

279-
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
280-
let js_value = bigint_json
281-
.serialize(&serializer)
282-
.map_err(|err| err.to_string())?;
336+
#[wasm_bindgen(js_name = getIdentitySecret)]
337+
pub fn get_identity_secret(&self) -> WasmFr {
338+
WasmFr::from(**self.0.identity_secret())
339+
}
283340

284-
js_value
285-
.dyn_into::<Object>()
286-
.map_err(|err| format!("{:#?}", err))
341+
#[wasm_bindgen(js_name = getUserMessageLimit)]
342+
pub fn get_user_message_limit(&self) -> WasmFr {
343+
WasmFr::from(*self.0.user_message_limit())
344+
}
345+
346+
#[cfg(not(feature = "multi-message-id"))]
347+
#[wasm_bindgen(js_name = getMessageId)]
348+
pub fn get_message_id(&self) -> WasmFr {
349+
WasmFr::from(*self.0.message_id())
350+
}
351+
352+
#[cfg(feature = "multi-message-id")]
353+
#[wasm_bindgen(js_name = getMessageIds)]
354+
pub fn get_message_ids(&self) -> VecWasmFr {
355+
VecWasmFr::from(self.0.message_ids().to_vec())
356+
}
357+
358+
#[wasm_bindgen(js_name = getPathElements)]
359+
pub fn get_path_elements(&self) -> VecWasmFr {
360+
VecWasmFr::from(self.0.path_elements().to_vec())
361+
}
362+
363+
#[wasm_bindgen(js_name = getIdentityPathIndex)]
364+
pub fn get_identity_path_index(&self) -> Uint8Array {
365+
Uint8Array::from(self.0.identity_path_index())
366+
}
367+
368+
#[wasm_bindgen(js_name = getX)]
369+
pub fn get_x(&self) -> WasmFr {
370+
WasmFr::from(*self.0.x())
371+
}
372+
373+
#[wasm_bindgen(js_name = getExternalNullifier)]
374+
pub fn get_external_nullifier(&self) -> WasmFr {
375+
WasmFr::from(*self.0.external_nullifier())
376+
}
377+
378+
#[cfg(feature = "multi-message-id")]
379+
#[wasm_bindgen(js_name = getSelectorUsed)]
380+
pub fn get_selector_used(&self) -> Uint8Array {
381+
let bytes: Vec<u8> = self
382+
.0
383+
.selector_used()
384+
.iter()
385+
.map(|&b| if b { 1u8 } else { 0u8 })
386+
.collect();
387+
Uint8Array::from(&bytes[..])
388+
}
389+
390+
#[wasm_bindgen(js_name = modifyIdentitySecret)]
391+
pub fn modify_identity_secret(&mut self, identity_secret: &WasmFr) {
392+
let mut fr = identity_secret.inner();
393+
self.0.modify_identity_secret(IdSecret::from(&mut fr));
394+
}
395+
396+
#[wasm_bindgen(js_name = modifyUserMessageLimit)]
397+
pub fn modify_user_message_limit(&mut self, user_message_limit: &WasmFr) {
398+
self.0.modify_user_message_limit(user_message_limit.inner());
399+
}
400+
401+
#[cfg(not(feature = "multi-message-id"))]
402+
#[wasm_bindgen(js_name = modifyMessageId)]
403+
pub fn modify_message_id(&mut self, message_id: &WasmFr) {
404+
self.0.modify_message_id(message_id.inner());
405+
}
406+
407+
#[cfg(feature = "multi-message-id")]
408+
#[wasm_bindgen(js_name = modifyMessageIds)]
409+
pub fn modify_message_ids(&mut self, message_ids: &VecWasmFr) {
410+
self.0.modify_message_ids(message_ids.inner());
411+
}
412+
413+
#[wasm_bindgen(js_name = modifyPathElements)]
414+
pub fn modify_path_elements(&mut self, path_elements: &VecWasmFr) {
415+
self.0.modify_path_elements(path_elements.inner());
416+
}
417+
418+
#[wasm_bindgen(js_name = modifyIdentityPathIndex)]
419+
pub fn modify_identity_path_index(&mut self, identity_path_index: &Uint8Array) {
420+
self.0
421+
.modify_identity_path_index(identity_path_index.to_vec());
422+
}
423+
424+
#[wasm_bindgen(js_name = modifyX)]
425+
pub fn modify_x(&mut self, x: &WasmFr) {
426+
self.0.modify_x(x.inner());
427+
}
428+
429+
#[wasm_bindgen(js_name = modifyExternalNullifier)]
430+
pub fn modify_external_nullifier(&mut self, external_nullifier: &WasmFr) {
431+
self.0.modify_external_nullifier(external_nullifier.inner());
432+
}
433+
434+
#[cfg(feature = "multi-message-id")]
435+
#[wasm_bindgen(js_name = modifySelectorUsed)]
436+
pub fn modify_selector_used(&mut self, selector_used: &Uint8Array) {
437+
let selector_used: Vec<bool> = selector_used.to_vec().iter().map(|&b| b != 0).collect();
438+
self.0.modify_selector_used(selector_used);
287439
}
288440

289441
#[wasm_bindgen(js_name = toBytesLE)]
@@ -311,4 +463,18 @@ impl WasmRLNWitnessInput {
311463
let (witness, _) = bytes_be_to_rln_witness(&bytes_vec).map_err(|err| err.to_string())?;
312464
Ok(WasmRLNWitnessInput(witness))
313465
}
466+
467+
#[wasm_bindgen(js_name = toBigIntJson)]
468+
pub fn to_bigint_json(&self) -> Result<Object, String> {
469+
let bigint_json = rln_witness_to_bigint_json(&self.0).map_err(|err| err.to_string())?;
470+
471+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
472+
let js_value = bigint_json
473+
.serialize(&serializer)
474+
.map_err(|err| err.to_string())?;
475+
476+
js_value
477+
.dyn_into::<Object>()
478+
.map_err(|err| format!("{:#?}", err))
479+
}
314480
}

rln/ffi_nim_examples/main.nim

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ type
8383
ok*: Vec_uint8
8484
err*: Vec_uint8
8585

86+
CResultVecBoolVecU8* = object
87+
ok*: Vec_bool
88+
err*: Vec_uint8
89+
8690
CBoolResult* = object
8791
ok*: bool
8892
err*: Vec_uint8
@@ -216,6 +220,61 @@ else:
216220
external_nullifier: ptr CFr
217221
): CResultWitnessInputPtrVecU8 {.importc: "ffi_rln_witness_input_new", cdecl,
218222
dynlib: RLN_LIB.}
223+
proc ffi_rln_witness_input_get_version_byte*(witness: ptr ptr FFI_RLNWitnessInput): uint8 {.importc: "ffi_rln_witness_input_get_version_byte",
224+
cdecl, dynlib: RLN_LIB.}
225+
proc ffi_rln_witness_input_get_identity_secret*(witness: ptr ptr FFI_RLNWitnessInput): ptr CFr {.importc: "ffi_rln_witness_input_get_identity_secret",
226+
cdecl, dynlib: RLN_LIB.}
227+
proc ffi_rln_witness_input_get_user_message_limit*(witness: ptr ptr FFI_RLNWitnessInput): ptr CFr {.importc: "ffi_rln_witness_input_get_user_message_limit",
228+
cdecl, dynlib: RLN_LIB.}
229+
when defined(ffiMultiMessageId):
230+
proc ffi_rln_witness_input_get_message_ids*(witness: ptr ptr FFI_RLNWitnessInput): Vec_CFr {.importc: "ffi_rln_witness_input_get_message_ids",
231+
cdecl, dynlib: RLN_LIB.}
232+
else:
233+
proc ffi_rln_witness_input_get_message_id*(witness: ptr ptr FFI_RLNWitnessInput): ptr CFr {.importc: "ffi_rln_witness_input_get_message_id",
234+
cdecl, dynlib: RLN_LIB.}
235+
proc ffi_rln_witness_input_get_path_elements*(witness: ptr ptr FFI_RLNWitnessInput): Vec_CFr {.importc: "ffi_rln_witness_input_get_path_elements",
236+
cdecl, dynlib: RLN_LIB.}
237+
proc ffi_rln_witness_input_get_identity_path_index*(witness: ptr ptr FFI_RLNWitnessInput): Vec_uint8 {.importc: "ffi_rln_witness_input_get_identity_path_index",
238+
cdecl, dynlib: RLN_LIB.}
239+
proc ffi_rln_witness_input_get_x*(witness: ptr ptr FFI_RLNWitnessInput): ptr CFr {.importc: "ffi_rln_witness_input_get_x",
240+
cdecl, dynlib: RLN_LIB.}
241+
proc ffi_rln_witness_input_get_external_nullifier*(witness: ptr ptr FFI_RLNWitnessInput): ptr CFr {.importc: "ffi_rln_witness_input_get_external_nullifier",
242+
cdecl, dynlib: RLN_LIB.}
243+
when defined(ffiMultiMessageId):
244+
proc ffi_rln_witness_input_get_selector_used*(witness: ptr ptr FFI_RLNWitnessInput): Vec_bool {.importc: "ffi_rln_witness_input_get_selector_used",
245+
cdecl, dynlib: RLN_LIB.}
246+
247+
proc ffi_rln_witness_input_modify_identity_secret*(witness: ptr ptr FFI_RLNWitnessInput,
248+
identity_secret: ptr CFr) {.importc: "ffi_rln_witness_input_modify_identity_secret",
249+
cdecl, dynlib: RLN_LIB.}
250+
proc ffi_rln_witness_input_modify_user_message_limit*(witness: ptr ptr FFI_RLNWitnessInput,
251+
user_message_limit: ptr CFr) {.importc: "ffi_rln_witness_input_modify_user_message_limit",
252+
cdecl, dynlib: RLN_LIB.}
253+
when defined(ffiMultiMessageId):
254+
proc ffi_rln_witness_input_modify_message_ids*(witness: ptr ptr FFI_RLNWitnessInput,
255+
message_ids: ptr Vec_CFr) {.importc: "ffi_rln_witness_input_modify_message_ids",
256+
cdecl, dynlib: RLN_LIB.}
257+
else:
258+
proc ffi_rln_witness_input_modify_message_id*(witness: ptr ptr FFI_RLNWitnessInput,
259+
message_id: ptr CFr) {.importc: "ffi_rln_witness_input_modify_message_id",
260+
cdecl, dynlib: RLN_LIB.}
261+
proc ffi_rln_witness_input_modify_path_elements*(witness: ptr ptr FFI_RLNWitnessInput,
262+
path_elements: ptr Vec_CFr) {.importc: "ffi_rln_witness_input_modify_path_elements",
263+
cdecl, dynlib: RLN_LIB.}
264+
proc ffi_rln_witness_input_modify_identity_path_index*(witness: ptr ptr FFI_RLNWitnessInput,
265+
identity_path_index: ptr Vec_uint8) {.importc: "ffi_rln_witness_input_modify_identity_path_index",
266+
cdecl, dynlib: RLN_LIB.}
267+
proc ffi_rln_witness_input_modify_x*(witness: ptr ptr FFI_RLNWitnessInput,
268+
x: ptr CFr) {.importc: "ffi_rln_witness_input_modify_x",
269+
cdecl, dynlib: RLN_LIB.}
270+
proc ffi_rln_witness_input_modify_external_nullifier*(witness: ptr ptr FFI_RLNWitnessInput,
271+
external_nullifier: ptr CFr) {.importc: "ffi_rln_witness_input_modify_external_nullifier",
272+
cdecl, dynlib: RLN_LIB.}
273+
when defined(ffiMultiMessageId):
274+
proc ffi_rln_witness_input_modify_selector_used*(witness: ptr ptr FFI_RLNWitnessInput,
275+
selector_used: ptr Vec_bool) {.importc: "ffi_rln_witness_input_modify_selector_used",
276+
cdecl, dynlib: RLN_LIB.}
277+
219278
proc ffi_rln_witness_to_bytes_le*(witness: ptr ptr FFI_RLNWitnessInput): CResultVecU8VecU8 {.importc: "ffi_rln_witness_to_bytes_le",
220279
cdecl, dynlib: RLN_LIB.}
221280
proc ffi_rln_witness_to_bytes_be*(witness: ptr ptr FFI_RLNWitnessInput): CResultVecU8VecU8 {.importc: "ffi_rln_witness_to_bytes_be",
@@ -314,7 +373,11 @@ proc ffi_recover_id_secret*(proof_values_1: ptr ptr FFI_RLNProofValues,
314373
proof_values_2: ptr ptr FFI_RLNProofValues): CResultCFrPtrVecU8 {.importc: "ffi_recover_id_secret",
315374
cdecl, dynlib: RLN_LIB.}
316375

317-
# RLNProof serialization
376+
# RLNProof functions
377+
proc ffi_rln_proof_get_version_byte*(proof: ptr ptr FFI_RLNProof): uint8 {.importc: "ffi_rln_proof_get_version_byte",
378+
cdecl, dynlib: RLN_LIB.}
379+
proc ffi_rln_proof_get_values*(proof: ptr ptr FFI_RLNProof): ptr FFI_RLNProofValues {.importc: "ffi_rln_proof_get_values",
380+
cdecl, dynlib: RLN_LIB.}
318381
proc ffi_rln_proof_to_bytes_le*(proof: ptr ptr FFI_RLNProof): CResultVecU8VecU8 {.importc: "ffi_rln_proof_to_bytes_le",
319382
cdecl, dynlib: RLN_LIB.}
320383
proc ffi_rln_proof_to_bytes_be*(proof: ptr ptr FFI_RLNProof): CResultVecU8VecU8 {.importc: "ffi_rln_proof_to_bytes_be",
@@ -325,9 +388,6 @@ proc ffi_bytes_be_to_rln_proof*(bytes: ptr Vec_uint8): CResultProofPtrVecU8 {.im
325388
cdecl, dynlib: RLN_LIB.}
326389

327390
# RLNProofValues functions
328-
proc ffi_rln_proof_get_values*(proof: ptr ptr FFI_RLNProof): ptr FFI_RLNProofValues {.importc: "ffi_rln_proof_get_values",
329-
cdecl, dynlib: RLN_LIB.}
330-
331391
when defined(ffiMultiMessageId):
332392
proc ffi_rln_proof_values_get_ys*(pv: ptr ptr FFI_RLNProofValues): CResultVecCFrVecU8 {.importc: "ffi_rln_proof_values_get_ys",
333393
cdecl, dynlib: RLN_LIB.}
@@ -338,12 +398,44 @@ else:
338398
cdecl, dynlib: RLN_LIB.}
339399
proc ffi_rln_proof_values_get_nullifier*(pv: ptr ptr FFI_RLNProofValues): CResultCFrPtrVecU8 {.importc: "ffi_rln_proof_values_get_nullifier",
340400
cdecl, dynlib: RLN_LIB.}
401+
when defined(ffiMultiMessageId):
402+
proc ffi_rln_proof_values_get_selector_used*(pv: ptr ptr FFI_RLNProofValues): CResultVecBoolVecU8 {.importc: "ffi_rln_proof_values_get_selector_used",
403+
cdecl, dynlib: RLN_LIB.}
404+
proc ffi_rln_proof_values_get_version_byte*(pv: ptr ptr FFI_RLNProofValues): uint8 {.importc: "ffi_rln_proof_values_get_version_byte",
405+
cdecl, dynlib: RLN_LIB.}
341406
proc ffi_rln_proof_values_get_root*(pv: ptr ptr FFI_RLNProofValues): ptr CFr {.importc: "ffi_rln_proof_values_get_root",
342407
cdecl, dynlib: RLN_LIB.}
343408
proc ffi_rln_proof_values_get_x*(pv: ptr ptr FFI_RLNProofValues): ptr CFr {.importc: "ffi_rln_proof_values_get_x",
344409
cdecl, dynlib: RLN_LIB.}
345410
proc ffi_rln_proof_values_get_external_nullifier*(pv: ptr ptr FFI_RLNProofValues): ptr CFr {.importc: "ffi_rln_proof_values_get_external_nullifier",
346411
cdecl, dynlib: RLN_LIB.}
412+
proc ffi_rln_proof_values_modify_root*(pv: ptr ptr FFI_RLNProofValues,
413+
root: ptr CFr) {.importc: "ffi_rln_proof_values_modify_root",
414+
cdecl, dynlib: RLN_LIB.}
415+
proc ffi_rln_proof_values_modify_x*(pv: ptr ptr FFI_RLNProofValues,
416+
x: ptr CFr) {.importc: "ffi_rln_proof_values_modify_x",
417+
cdecl, dynlib: RLN_LIB.}
418+
proc ffi_rln_proof_values_modify_external_nullifier*(pv: ptr ptr FFI_RLNProofValues,
419+
external_nullifier: ptr CFr) {.importc: "ffi_rln_proof_values_modify_external_nullifier",
420+
cdecl, dynlib: RLN_LIB.}
421+
when defined(ffiMultiMessageId):
422+
proc ffi_rln_proof_values_modify_selector_used*(pv: ptr ptr FFI_RLNProofValues,
423+
selector_used: ptr Vec_bool) {.importc: "ffi_rln_proof_values_modify_selector_used",
424+
cdecl, dynlib: RLN_LIB.}
425+
proc ffi_rln_proof_values_modify_ys*(pv: ptr ptr FFI_RLNProofValues,
426+
ys: ptr Vec_CFr) {.importc: "ffi_rln_proof_values_modify_ys",
427+
cdecl, dynlib: RLN_LIB.}
428+
proc ffi_rln_proof_values_modify_nullifiers*(pv: ptr ptr FFI_RLNProofValues,
429+
nullifiers: ptr Vec_CFr) {.importc: "ffi_rln_proof_values_modify_nullifiers",
430+
cdecl, dynlib: RLN_LIB.}
431+
else:
432+
proc ffi_rln_proof_values_modify_y*(pv: ptr ptr FFI_RLNProofValues,
433+
y: ptr CFr) {.importc: "ffi_rln_proof_values_modify_y",
434+
cdecl, dynlib: RLN_LIB.}
435+
proc ffi_rln_proof_values_modify_nullifier*(pv: ptr ptr FFI_RLNProofValues,
436+
nullifier: ptr CFr) {.importc: "ffi_rln_proof_values_modify_nullifier",
437+
cdecl, dynlib: RLN_LIB.}
438+
347439
proc ffi_rln_proof_values_to_bytes_le*(pv: ptr ptr FFI_RLNProofValues): Vec_uint8 {.importc: "ffi_rln_proof_values_to_bytes_le",
348440
cdecl, dynlib: RLN_LIB.}
349441
proc ffi_rln_proof_values_to_bytes_be*(pv: ptr ptr FFI_RLNProofValues): Vec_uint8 {.importc: "ffi_rln_proof_values_to_bytes_be",

0 commit comments

Comments
 (0)