mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-09 13:47:58 -05:00
Compare commits
6 Commits
zerokit_ut
...
debug-wasm
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c9fc07cfe | ||
|
|
7dd019b0a5 | ||
|
|
78bec51fba | ||
|
|
b0be32943e | ||
|
|
09da5304c1 | ||
|
|
d5e09524d7 |
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -134,4 +134,4 @@ jobs:
|
||||
- uses: boa-dev/criterion-compare-action@v3
|
||||
with:
|
||||
branchName: ${{ github.base_ref }}
|
||||
cwd: ${{ matrix.crate }}
|
||||
cwd: ${{ matrix.crate }}
|
||||
|
||||
32
Cargo.lock
generated
32
Cargo.lock
generated
@@ -1919,9 +1919,9 @@ checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.62"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68c16e1bfd491478ab155fd8b4896b86f9ede344949b641e61501e07c2b8b4d5"
|
||||
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
@@ -2695,7 +2695,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rln"
|
||||
version = "0.3.4"
|
||||
version = "0.3.6"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-circom 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -2707,6 +2707,7 @@ dependencies = [
|
||||
"ark-std 0.4.0",
|
||||
"cfg-if",
|
||||
"color-eyre 0.6.2",
|
||||
"console_error_panic_hook",
|
||||
"criterion 0.4.0",
|
||||
"include_dir",
|
||||
"num-bigint",
|
||||
@@ -2720,6 +2721,7 @@ dependencies = [
|
||||
"thiserror",
|
||||
"tiny-keccak",
|
||||
"wasmer",
|
||||
"web-sys",
|
||||
"zerokit_utils",
|
||||
]
|
||||
|
||||
@@ -3543,9 +3545,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.85"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b6cb788c4e39112fbe1822277ef6fb3c55cd86b95cb3d3c4c1c9597e4ac74b4"
|
||||
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
@@ -3553,9 +3555,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.85"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35e522ed4105a9d626d885b35d62501b30d9666283a5c8be12c14a8bdafe7822"
|
||||
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
@@ -3580,9 +3582,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.85"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "358a79a0cb89d21db8120cbfb91392335913e4890665b1a7981d9e956903b434"
|
||||
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
@@ -3590,9 +3592,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.85"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4783ce29f09b9d93134d41297aded3a712b7b979e9c6f28c32cb88c973a94869"
|
||||
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -3603,9 +3605,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.85"
|
||||
version = "0.2.92"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a901d592cafaa4d711bc324edfaff879ac700b19c3dfd60058d2b445be2691eb"
|
||||
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-test"
|
||||
@@ -3995,9 +3997,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.62"
|
||||
version = "0.3.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "16b5f940c7edfdc6d12126d98c9ef4d1b3d470011c47c76a6581df47ad9ba721"
|
||||
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
|
||||
@@ -19,6 +19,7 @@ opt-level = 3
|
||||
[profile.release.package."rln-wasm"]
|
||||
# Tell `rustc` to optimize for small code size.
|
||||
opt-level = "s"
|
||||
debug = true
|
||||
|
||||
[profile.release.package."semaphore"]
|
||||
codegen-units = 1
|
||||
|
||||
@@ -29,4 +29,7 @@ image = "ghcr.io/cross-rs/mips64-unknown-linux-gnuabi64:latest"
|
||||
image = "ghcr.io/cross-rs/mips64el-unknown-linux-gnuabi64:latest"
|
||||
|
||||
[target.mipsel-unknown-linux-gnu]
|
||||
image = "ghcr.io/cross-rs/mipsel-unknown-linux-gnu:latest"
|
||||
image = "ghcr.io/cross-rs/mipsel-unknown-linux-gnu:latest"
|
||||
|
||||
[target.aarch64-linux-android]
|
||||
image = "ghcr.io/cross-rs/aarch64-linux-android:edge"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rln"
|
||||
version = "0.3.4"
|
||||
version = "0.3.6"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "APIs to manage, compute and verify zkSNARK proofs and RLN primitives"
|
||||
@@ -49,6 +49,9 @@ serde_json = "=1.0.96"
|
||||
serde = { version = "=1.0.163", features = ["derive"] }
|
||||
|
||||
include_dir = "=0.7.3"
|
||||
console_error_panic_hook = "0.1.7"
|
||||
web-sys = { version = "0.3.69", features = ["console"] }
|
||||
|
||||
|
||||
[dev-dependencies]
|
||||
sled = "=0.34.7"
|
||||
|
||||
@@ -361,6 +361,21 @@ pub extern "C" fn generate_rln_proof(
|
||||
call_with_output_arg!(ctx, generate_rln_proof, output_buffer, input_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn generate_rln_proof_with_witness(
|
||||
ctx: *mut RLN,
|
||||
input_buffer: *const Buffer,
|
||||
output_buffer: *mut Buffer,
|
||||
) -> bool {
|
||||
call_with_output_arg!(
|
||||
ctx,
|
||||
generate_rln_proof_with_witness,
|
||||
output_buffer,
|
||||
input_buffer
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn verify_rln_proof(
|
||||
|
||||
@@ -246,7 +246,8 @@ impl ZerokitMerkleTree for PmTree {
|
||||
let data = self.tree.db.get(METADATA_KEY)?;
|
||||
|
||||
if data.is_none() {
|
||||
return Err(Report::msg("metadata does not exist"));
|
||||
// send empty Metadata
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Ok(data.unwrap())
|
||||
}
|
||||
|
||||
@@ -103,29 +103,50 @@ pub fn serialize_witness(rln_witness: &RLNWitnessInput) -> Result<Vec<u8>> {
|
||||
Ok(serialized)
|
||||
}
|
||||
|
||||
extern crate web_sys;
|
||||
|
||||
pub fn deserialize_witness(serialized: &[u8]) -> Result<(RLNWitnessInput, usize)> {
|
||||
let mut all_read: usize = 0;
|
||||
|
||||
let (identity_secret, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read identity secret".into());
|
||||
web_sys::console::log_1(&identity_secret.to_string().into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
let (path_elements, read) = bytes_le_to_vec_fr(&serialized[all_read..])?;
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read path elements".into());
|
||||
web_sys::console::log_1(&format!("{:?}", path_elements).into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
let (identity_path_index, read) = bytes_le_to_vec_u8(&serialized[all_read..])?;
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read identity path index".into());
|
||||
web_sys::console::log_1(&format!("{:?}", identity_path_index).into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
let (x, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read x".into());
|
||||
web_sys::console::log_1(&x.to_string().into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
let (epoch, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read epoch".into());
|
||||
web_sys::console::log_1(&epoch.to_string().into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
let (rln_identifier, read) = bytes_le_to_fr(&serialized[all_read..]);
|
||||
all_read += read;
|
||||
web_sys::console::log_1(&"Read rln identifier".into());
|
||||
web_sys::console::log_1(&rln_identifier.to_string().into());
|
||||
web_sys::console::log_1(&all_read.to_string().into());
|
||||
|
||||
// TODO: check rln_identifier against public::RLN_IDENTIFIER
|
||||
if serialized.len() != all_read {
|
||||
web_sys::console::log_1(&"serialized length is not equal to all_read".into());
|
||||
return Err(Report::msg("serialized length is not equal to all_read"));
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,9 @@ use color_eyre::{Report, Result};
|
||||
use num_bigint::BigInt;
|
||||
use std::io::Cursor;
|
||||
use utils::{ZerokitMerkleProof, ZerokitMerkleTree};
|
||||
extern crate console_error_panic_hook;
|
||||
use std::panic;
|
||||
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(not(target_arch = "wasm32"))] {
|
||||
@@ -720,9 +723,9 @@ impl RLN<'_> {
|
||||
mut output_data: W,
|
||||
) -> Result<()> {
|
||||
// We read input RLN witness and we serialize_compressed it
|
||||
let mut witness_byte: Vec<u8> = Vec::new();
|
||||
input_data.read_to_end(&mut witness_byte)?;
|
||||
let (rln_witness, _) = proof_inputs_to_rln_witness(&mut self.tree, &witness_byte)?;
|
||||
let mut input_byte: Vec<u8> = Vec::new();
|
||||
input_data.read_to_end(&mut input_byte)?;
|
||||
let (rln_witness, _) = proof_inputs_to_rln_witness(&mut self.tree, &input_byte)?;
|
||||
let proof_values = proof_values_from_witness(&rln_witness);
|
||||
|
||||
let proof = generate_proof(self.witness_calculator, &self.proving_key, &rln_witness)?;
|
||||
@@ -735,12 +738,33 @@ impl RLN<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: this function seems to use redundant witness (as bigint and serialized) and should be refactored
|
||||
// Generate RLN Proof using a witness calculated from outside zerokit
|
||||
//
|
||||
// output_data is [ proof<128> | root<32> | epoch<32> | share_x<32> | share_y<32> | nullifier<32> | rln_identifier<32> ]
|
||||
// we skip it from documentation for now
|
||||
#[doc(hidden)]
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn generate_rln_proof_with_witness<R: Read, W: Write>(
|
||||
&mut self,
|
||||
mut input_data: R,
|
||||
mut output_data: W,
|
||||
) -> Result<()> {
|
||||
let mut witness_byte: Vec<u8> = Vec::new();
|
||||
input_data.read_to_end(&mut witness_byte)?;
|
||||
let (rln_witness, _) = deserialize_witness(&witness_byte)?;
|
||||
let proof_values = proof_values_from_witness(&rln_witness);
|
||||
|
||||
let proof = generate_proof(self.witness_calculator, &self.proving_key, &rln_witness)?;
|
||||
|
||||
// Note: we export a serialization of ark-groth16::Proof not semaphore::Proof
|
||||
// This proof is compressed, i.e. 128 bytes long
|
||||
proof.serialize_compressed(&mut output_data)?;
|
||||
output_data.write_all(&serialize_proof_values(&proof_values))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Different in wasm since witness_calculator is not available
|
||||
// See: https://github.com/vacp2p/zerokit/blob/b903d8d740e0b8b82057bcc5377ddce05ae5676b/rln/src/public.rs#L47-L49
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub fn generate_rln_proof_with_witness<W: Write>(
|
||||
&mut self,
|
||||
calculated_witness: Vec<BigInt>,
|
||||
@@ -1170,6 +1194,7 @@ impl RLN<'_> {
|
||||
///
|
||||
/// The function returns the corresponding JSON encoding of the input [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object.
|
||||
pub fn get_rln_witness_json(&mut self, serialized_witness: &[u8]) -> Result<serde_json::Value> {
|
||||
panic::set_hook(Box::new(console_error_panic_hook::hook));
|
||||
let (rln_witness, _) = deserialize_witness(serialized_witness)?;
|
||||
get_json_inputs(&rln_witness)
|
||||
}
|
||||
@@ -1800,13 +1825,10 @@ mod test {
|
||||
.collect();
|
||||
|
||||
// Generating the proof
|
||||
let mut input_buffer = Cursor::new(serialized_witness);
|
||||
let mut output_buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.generate_rln_proof_with_witness(
|
||||
calculated_witness_vec,
|
||||
serialized_witness,
|
||||
&mut output_buffer,
|
||||
)
|
||||
.unwrap();
|
||||
rln.generate_rln_proof_with_witness(&mut input_buffer, &mut output_buffer)
|
||||
.unwrap();
|
||||
|
||||
// output_data is [ proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32> ]
|
||||
let mut proof_data = output_buffer.into_inner();
|
||||
@@ -2090,4 +2112,19 @@ mod test {
|
||||
|
||||
assert_eq!(arbitrary_metadata, received_metadata);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_metadata() {
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
|
||||
let input_buffer =
|
||||
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
|
||||
let rln = RLN::new(tree_height, input_buffer).unwrap();
|
||||
|
||||
let mut buffer = Cursor::new(Vec::<u8>::new());
|
||||
rln.get_metadata(&mut buffer).unwrap();
|
||||
let received_metadata = buffer.into_inner();
|
||||
|
||||
assert_eq!(received_metadata.len(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1209,7 +1209,7 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_metadata() {
|
||||
fn test_valid_metadata() {
|
||||
// We create a RLN instance
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
|
||||
@@ -1235,4 +1235,25 @@ mod test {
|
||||
|
||||
assert_eq!(result_data, seed_bytes.to_vec());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_metadata() {
|
||||
// We create a RLN instance
|
||||
let tree_height = TEST_TREE_HEIGHT;
|
||||
|
||||
let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit();
|
||||
let input_config = json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string();
|
||||
let input_buffer = &Buffer::from(input_config.as_bytes());
|
||||
let success = new(tree_height, input_buffer, rln_pointer.as_mut_ptr());
|
||||
assert!(success, "RLN object creation failed");
|
||||
let rln_pointer = unsafe { &mut *rln_pointer.assume_init() };
|
||||
|
||||
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
|
||||
let success = get_metadata(rln_pointer, output_buffer.as_mut_ptr());
|
||||
assert!(success, "get_metadata call failed");
|
||||
|
||||
let output_buffer = unsafe { output_buffer.assume_init() };
|
||||
|
||||
assert_eq!(output_buffer.len, 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user