mirror of https://github.com/vacp2p/zerokit.git
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 <seemenkina@gmail.com>
This commit is contained in:
parent
e6238fd722
commit
c4579e1917
|
@ -71,9 +71,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstyle"
|
name = "anstyle"
|
||||||
version = "1.0.0"
|
version = "1.0.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d"
|
checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anstyle-parse"
|
name = "anstyle-parse"
|
||||||
|
@ -1623,12 +1623,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.17"
|
version = "0.4.22"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "loupe"
|
name = "loupe"
|
||||||
|
|
|
@ -3,9 +3,6 @@ name = "rln-wasm"
|
||||||
version = "0.0.13"
|
version = "0.0.13"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT or Apache2"
|
license = "MIT or Apache2"
|
||||||
autobenches = false
|
|
||||||
autotests = false
|
|
||||||
autobins = false
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
@ -15,9 +12,12 @@ default = ["console_error_panic_hook"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rln = { path = "../rln", default-features = false, features = ["wasm"] }
|
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"] }
|
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"] }
|
getrandom = { version = "0.2.7", default-features = false, features = ["js"] }
|
||||||
wasm-bindgen = "0.2.63"
|
wasm-bindgen = "0.2.63"
|
||||||
serde-wasm-bindgen = "0.4"
|
serde-wasm-bindgen = "0.4"
|
||||||
|
|
|
@ -7,11 +7,7 @@ script = "sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/
|
||||||
|
|
||||||
[tasks.build]
|
[tasks.build]
|
||||||
clear = true
|
clear = true
|
||||||
dependencies = [
|
dependencies = ["pack-build", "pack-rename", "post-build"]
|
||||||
"pack-build",
|
|
||||||
"pack-rename",
|
|
||||||
"post-build"
|
|
||||||
]
|
|
||||||
|
|
||||||
[tasks.post-build]
|
[tasks.post-build]
|
||||||
command = "wasm-strip"
|
command = "wasm-strip"
|
||||||
|
|
|
@ -270,8 +270,12 @@ pub fn rln_witness_to_json(
|
||||||
ctx: *mut RLNWrapper,
|
ctx: *mut RLNWrapper,
|
||||||
serialized_witness: Uint8Array,
|
serialized_witness: Uint8Array,
|
||||||
) -> Result<Object, String> {
|
) -> Result<Object, String> {
|
||||||
let inputs = call!(ctx, get_rln_witness_json, &serialized_witness.to_vec()[..])
|
let inputs = call!(
|
||||||
.map_err(|err| err.to_string())?;
|
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())?;
|
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))
|
Object::from_entries(&js_value).map_err(|err| format!("{:#?}", err))
|
||||||
}
|
}
|
||||||
|
|
|
@ -739,11 +739,11 @@ where
|
||||||
a.map_err(serde::de::Error::custom)
|
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
|
/// # 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<RLNWitnessInput> {
|
pub fn rln_witness_from_json(input_json: serde_json::Value) -> Result<RLNWitnessInput> {
|
||||||
let rln_witness: RLNWitnessInput = serde_json::from_value(input_json).unwrap();
|
let rln_witness: RLNWitnessInput = serde_json::from_value(input_json).unwrap();
|
||||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
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<RLNWitness
|
||||||
Ok(rln_witness)
|
Ok(rln_witness)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a JSON value into [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object.
|
/// Converts a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
///
|
///
|
||||||
/// Returns an error if `rln_witness.message_id` is not within `rln_witness.user_message_limit`.
|
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||||
pub fn rln_witness_to_json(rln_witness: &RLNWitnessInput) -> Result<serde_json::Value> {
|
pub fn rln_witness_to_json(rln_witness: &RLNWitnessInput) -> Result<serde_json::Value> {
|
||||||
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
|
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<serde_json::
|
||||||
Ok(rln_witness_json)
|
Ok(rln_witness_json)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts a [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object to the corresponding JSON serialization.
|
||||||
|
/// Before serialisation the data should be translated into big int for further calculation in the witness calculator.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if `message_id` is not within `user_message_limit`.
|
||||||
|
pub fn rln_witness_to_bigint_json(rln_witness: &RLNWitnessInput) -> Result<serde_json::Value> {
|
||||||
|
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<()> {
|
pub fn message_id_range_check(message_id: &Fr, user_message_limit: &Fr) -> Result<()> {
|
||||||
if message_id > user_message_limit {
|
if message_id > user_message_limit {
|
||||||
return Err(color_eyre::Report::msg(
|
return Err(color_eyre::Report::msg(
|
||||||
|
|
|
@ -60,6 +60,7 @@ impl RLN {
|
||||||
/// Input parameters are
|
/// Input parameters are
|
||||||
/// - `tree_height`: the height of the internal Merkle tree
|
/// - `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
|
/// - `input_data`: include `tree_config` a reader for a string containing a json with the merkle tree configuration
|
||||||
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::io::Cursor;
|
/// use std::io::Cursor;
|
||||||
|
@ -693,7 +694,7 @@ impl RLN {
|
||||||
///
|
///
|
||||||
/// Input values are:
|
/// Input values are:
|
||||||
/// - `input_data`: a reader for the serialization of the RLN zkSNARK proof concatenated with a serialization of the circuit output values,
|
/// - `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.
|
/// 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 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,
|
/// - `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<var>]`, where <_> indicates the byte length.
|
/// i.e. `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal<var>]`, 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.
|
/// 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 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,
|
/// - `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>]`
|
/// 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<var> ]` (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))
|
/// or `[ proof<128> | root<32> | external_nullifier<32> | x<32> | y<32> | nullifier<32> | signal_len<8> | signal<var> ]` (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`
|
/// - `input_proof_data_2`: same as `input_proof_data_1`
|
||||||
///
|
///
|
||||||
/// Output values are:
|
/// Output values are:
|
||||||
|
@ -1273,6 +1274,21 @@ impl RLN {
|
||||||
rln_witness_to_json(&rln_witness)
|
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<serde_json::Value> {
|
||||||
|
let (rln_witness, _) = deserialize_witness(serialized_witness)?;
|
||||||
|
rln_witness_to_bigint_json(&rln_witness)
|
||||||
|
}
|
||||||
|
|
||||||
/// Closes the connection to the Merkle tree database.
|
/// Closes the connection to the Merkle tree database.
|
||||||
/// This function should be called before the RLN object is dropped.
|
/// 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.
|
/// If not called, the connection will be closed when the RLN object is dropped.
|
||||||
|
|
Loading…
Reference in New Issue