From c4579e1917f78afd890d16109a36a5387e045f37 Mon Sep 17 00:00:00 2001 From: Aaryamann Challani <43716372+rymnc@users.noreply.github.com> Date: Tue, 20 Aug 2024 14:46:14 +0530 Subject: [PATCH] fix(rln-wasm): run tests again (#264) * fix(rln-wasm): run tests again * fix linter * fix serialization for rln-wasm * add comment --------- Co-authored-by: seemenkina --- Cargo.lock | 11 ++++------- rln-wasm/Cargo.toml | 10 +++++----- rln-wasm/Makefile.toml | 6 +----- rln-wasm/src/lib.rs | 8 ++++++-- rln/src/protocol.rs | 42 ++++++++++++++++++++++++++++++++++++++---- rln/src/public.rs | 24 ++++++++++++++++++++---- 6 files changed, 74 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6436b87..aaca583 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -71,9 +71,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.0" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" [[package]] name = "anstyle-parse" @@ -1623,12 +1623,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.17" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "loupe" diff --git a/rln-wasm/Cargo.toml b/rln-wasm/Cargo.toml index b1a27b4..4685a2b 100644 --- a/rln-wasm/Cargo.toml +++ b/rln-wasm/Cargo.toml @@ -3,9 +3,6 @@ name = "rln-wasm" version = "0.0.13" edition = "2021" license = "MIT or Apache2" -autobenches = false -autotests = false -autobins = false [lib] crate-type = ["cdylib", "rlib"] @@ -15,9 +12,12 @@ default = ["console_error_panic_hook"] [dependencies] rln = { path = "../rln", default-features = false, features = ["wasm"] } -num-bigint = { version = "0.4", default-features = false, features = ["rand", "serde"] } +num-bigint = { version = "0.4", default-features = false, features = [ + "rand", + "serde", +] } wasmer = { version = "2.3", default-features = false, features = ["js", "std"] } -web-sys = {version = "0.3", features=["console"]} +web-sys = { version = "0.3", features = ["console"] } getrandom = { version = "0.2.7", default-features = false, features = ["js"] } wasm-bindgen = "0.2.63" serde-wasm-bindgen = "0.4" diff --git a/rln-wasm/Makefile.toml b/rln-wasm/Makefile.toml index 77e07e9..3456a3c 100644 --- a/rln-wasm/Makefile.toml +++ b/rln-wasm/Makefile.toml @@ -7,11 +7,7 @@ script = "sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/ [tasks.build] clear = true -dependencies = [ - "pack-build", - "pack-rename", - "post-build" -] +dependencies = ["pack-build", "pack-rename", "post-build"] [tasks.post-build] command = "wasm-strip" diff --git a/rln-wasm/src/lib.rs b/rln-wasm/src/lib.rs index fcb8d25..c6b0702 100644 --- a/rln-wasm/src/lib.rs +++ b/rln-wasm/src/lib.rs @@ -270,8 +270,12 @@ pub fn rln_witness_to_json( ctx: *mut RLNWrapper, serialized_witness: Uint8Array, ) -> Result { - let inputs = call!(ctx, get_rln_witness_json, &serialized_witness.to_vec()[..]) - .map_err(|err| err.to_string())?; + let inputs = call!( + ctx, + get_rln_witness_bigint_json, + &serialized_witness.to_vec()[..] + ) + .map_err(|err| err.to_string())?; let js_value = serde_wasm_bindgen::to_value(&inputs).map_err(|err| err.to_string())?; Object::from_entries(&js_value).map_err(|err| format!("{:#?}", err)) } diff --git a/rln/src/protocol.rs b/rln/src/protocol.rs index 7eb05bd..46aa853 100644 --- a/rln/src/protocol.rs +++ b/rln/src/protocol.rs @@ -739,11 +739,11 @@ where a.map_err(serde::de::Error::custom) } -/// Converts a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization. +/// Converts a JSON value into [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object. /// /// # Errors /// -/// Returns an error if `message_id` is not within `user_message_limit`. +/// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`. pub fn rln_witness_from_json(input_json: serde_json::Value) -> Result { let rln_witness: RLNWitnessInput = serde_json::from_value(input_json).unwrap(); message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?; @@ -751,11 +751,11 @@ pub fn rln_witness_from_json(input_json: serde_json::Value) -> Result Result { message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?; @@ -763,6 +763,40 @@ pub fn rln_witness_to_json(rln_witness: &RLNWitnessInput) -> Result Result { + message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?; + + let mut path_elements = Vec::new(); + + for v in rln_witness.path_elements.iter() { + path_elements.push(to_bigint(v)?.to_str_radix(10)); + } + + let mut identity_path_index = Vec::new(); + rln_witness + .identity_path_index + .iter() + .for_each(|v| identity_path_index.push(BigInt::from(*v).to_str_radix(10))); + + let inputs = serde_json::json!({ + "identitySecret": to_bigint(&rln_witness.identity_secret)?.to_str_radix(10), + "userMessageLimit": to_bigint(&rln_witness.user_message_limit)?.to_str_radix(10), + "messageId": to_bigint(&rln_witness.message_id)?.to_str_radix(10), + "pathElements": path_elements, + "identityPathIndex": identity_path_index, + "x": to_bigint(&rln_witness.x)?.to_str_radix(10), + "externalNullifier": to_bigint(&rln_witness.external_nullifier)?.to_str_radix(10), + }); + + Ok(inputs) +} + pub fn message_id_range_check(message_id: &Fr, user_message_limit: &Fr) -> Result<()> { if message_id > user_message_limit { return Err(color_eyre::Report::msg( diff --git a/rln/src/public.rs b/rln/src/public.rs index 0ab5416..03a0dac 100644 --- a/rln/src/public.rs +++ b/rln/src/public.rs @@ -60,6 +60,7 @@ impl RLN { /// Input parameters are /// - `tree_height`: the height of the internal Merkle tree /// - `input_data`: include `tree_config` a reader for a string containing a json with the merkle tree configuration + /// /// Example: /// ``` /// use std::io::Cursor; @@ -693,7 +694,7 @@ impl RLN { /// /// Input values are: /// - `input_data`: a reader for the serialization of the RLN zkSNARK proof concatenated with a serialization of the circuit output values, - /// i.e. `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]`, where <_> indicates the byte length. + /// i.e. `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]`, where <_> indicates the byte length. /// /// The function returns true if the zkSNARK proof is valid with respect to the provided circuit output values, false otherwise. /// @@ -865,7 +866,7 @@ impl RLN { /// /// Input values are: /// - `input_data`: a reader for the serialization of the RLN zkSNARK proof concatenated with a serialization of the circuit output values and the signal information, - /// i.e. `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal]`, where <_> indicates the byte length. + /// i.e. `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal]`, where <_> indicates the byte length. /// /// The function returns true if the zkSNARK proof is valid with respect to the provided circuit output values and signal. Returns false otherwise. /// @@ -1175,8 +1176,8 @@ impl RLN { /// /// Input values are: /// - `input_proof_data_1`: a reader for the serialization of a RLN zkSNARK proof concatenated with a serialization of the circuit output values and -optionally- the signal information, - /// i.e. either `[proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]` - /// or `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal ]` (to maintain compatibility with both output of [`generate_rln_proof`](crate::public::RLN::generate_rln_proof) and input of [`verify_rln_proof`](crate::public::RLN::verify_rln_proof)) + /// i.e. either `[proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32>]` + /// or `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal ]` (to maintain compatibility with both output of [`generate_rln_proof`](crate::public::RLN::generate_rln_proof) and input of [`verify_rln_proof`](crate::public::RLN::verify_rln_proof)) /// - `input_proof_data_2`: same as `input_proof_data_1` /// /// Output values are: @@ -1273,6 +1274,21 @@ impl RLN { rln_witness_to_json(&rln_witness) } + /// Converts a byte serialization of a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization. + /// Before serialisation the data will be translated into big int for further calculation in the witness calculator. + /// + /// Input values are: + /// - `serialized_witness`: the byte serialization of a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object (serialization done with [`rln::protocol::serialize_witness`](crate::protocol::serialize_witness)). + /// + /// The function returns the corresponding JSON encoding of the input [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object. + pub fn get_rln_witness_bigint_json( + &mut self, + serialized_witness: &[u8], + ) -> Result { + let (rln_witness, _) = deserialize_witness(serialized_witness)?; + rln_witness_to_bigint_json(&rln_witness) + } + /// Closes the connection to the Merkle tree database. /// This function should be called before the RLN object is dropped. /// If not called, the connection will be closed when the RLN object is dropped.