mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-09 13:47:58 -05:00
Compare commits
9 Commits
rln-v0.3.1
...
zerokit_ut
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b51896c3a7 | ||
|
|
0c5ef6abcf | ||
|
|
a1c292cb2e | ||
|
|
c6c1bfde91 | ||
|
|
bf3d1d3309 | ||
|
|
7110e00674 | ||
|
|
99966d1a6e | ||
|
|
7d63912ace | ||
|
|
ef1da42d94 |
24
Cargo.lock
generated
24
Cargo.lock
generated
@@ -2412,6 +2412,15 @@ dependencies = [
|
||||
"plotters-backend",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pmtree"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e054322ee96d2ccd86cd47b87797166682e45f5d67571c48eaa864668d26f510"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.17"
|
||||
@@ -2686,7 +2695,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rln"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-circom 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@@ -3504,15 +3513,6 @@ version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dad5567ad0cf5b760e5665964bec1b47dfd077ba8a2544b513f3556d3d239a2"
|
||||
|
||||
[[package]]
|
||||
name = "vacp2p_pmtree"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bee78790de52d6774ea318eb747a488fd5d96055bfe75ec2e7f605b15e21c664"
|
||||
dependencies = [
|
||||
"rayon",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
@@ -4260,7 +4260,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "zerokit_utils"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-ff 0.4.1",
|
||||
@@ -4269,8 +4269,8 @@ dependencies = [
|
||||
"hex-literal",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"pmtree",
|
||||
"serde",
|
||||
"sled",
|
||||
"tiny-keccak",
|
||||
"vacp2p_pmtree",
|
||||
]
|
||||
|
||||
@@ -21,4 +21,4 @@ opt-level = 3
|
||||
opt-level = "s"
|
||||
|
||||
[profile.release.package."semaphore"]
|
||||
codegen-units = 1
|
||||
codegen-units = 1
|
||||
|
||||
@@ -15,6 +15,8 @@ pub(crate) enum Commands {
|
||||
/// Sets a custom config file
|
||||
#[arg(short, long)]
|
||||
config: PathBuf,
|
||||
#[arg(short, long)]
|
||||
tree_config_input: PathBuf,
|
||||
},
|
||||
SetTree {
|
||||
tree_height: usize,
|
||||
|
||||
@@ -34,6 +34,7 @@ fn main() -> Result<()> {
|
||||
Some(Commands::NewWithParams {
|
||||
tree_height,
|
||||
config,
|
||||
tree_config_input,
|
||||
}) => {
|
||||
let mut resources: Vec<Vec<u8>> = Vec::new();
|
||||
for filename in ["rln.wasm", "rln_final.zkey", "verification_key.json"] {
|
||||
@@ -44,11 +45,13 @@ fn main() -> Result<()> {
|
||||
file.read_exact(&mut buffer)?;
|
||||
resources.push(buffer);
|
||||
}
|
||||
let tree_config_input_file = File::open(&tree_config_input)?;
|
||||
state.rln = Some(RLN::new_with_params(
|
||||
*tree_height,
|
||||
resources[0].clone(),
|
||||
resources[1].clone(),
|
||||
resources[2].clone(),
|
||||
tree_config_input_file,
|
||||
)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use color_eyre::Result;
|
||||
use rln::public::RLN;
|
||||
use std::fs::File;
|
||||
|
||||
use crate::config::{InnerConfig, Config};
|
||||
use crate::config::{Config, InnerConfig};
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct State<'a> {
|
||||
|
||||
@@ -250,11 +250,7 @@ pub fn wasm_set_metadata(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(),
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[wasm_bindgen(js_name = getMetadata)]
|
||||
pub fn wasm_get_metadata(ctx: *mut RLNWrapper) -> Result<Uint8Array, String> {
|
||||
call_with_output_and_error_msg!(
|
||||
ctx,
|
||||
get_metadata,
|
||||
"could not get metadata".to_string()
|
||||
)
|
||||
call_with_output_and_error_msg!(ctx, get_metadata, "could not get metadata".to_string())
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
[package]
|
||||
name = "rln"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "APIs to manage, compute and verify zkSNARK proofs and RLN primitives"
|
||||
documentation = "https://github.com/vacp2p/zerokit"
|
||||
homepage = "https://vac.dev"
|
||||
repository = "https://github.com/vacp2p/zerokit"
|
||||
|
||||
[lib]
|
||||
crate-type = ["rlib", "staticlib"]
|
||||
@@ -39,7 +42,7 @@ once_cell = "=1.17.1"
|
||||
rand = "=0.8.5"
|
||||
rand_chacha = "=0.3.1"
|
||||
tiny-keccak = { version = "=2.0.2", features = ["keccak"] }
|
||||
utils = { package = "zerokit_utils", version = "=0.3.0", path = "../utils/", default-features = false }
|
||||
utils = { package = "zerokit_utils", version = "=0.3.2", path = "../utils/", default-features = false }
|
||||
|
||||
# serialization
|
||||
serde_json = "=1.0.96"
|
||||
|
||||
117
rln/src/ffi.rs
117
rln/src/ffi.rs
@@ -12,7 +12,15 @@ macro_rules! call {
|
||||
($instance:expr, $method:ident $(, $arg:expr)*) => {
|
||||
{
|
||||
let new_instance: &mut RLN = $instance.process();
|
||||
new_instance.$method($($arg.process()),*).is_ok()
|
||||
match new_instance.$method($($arg.process()),*) {
|
||||
Ok(()) => {
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,13 +38,17 @@ macro_rules! call_with_output_arg {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let new_instance = $instance.process();
|
||||
if new_instance.$method(&mut output_data).is_ok() {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
} else {
|
||||
std::mem::forget(output_data);
|
||||
false
|
||||
match new_instance.$method(&mut output_data) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -44,13 +56,17 @@ macro_rules! call_with_output_arg {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
let new_instance = $instance.process();
|
||||
if new_instance.$method($($arg.process()),*, &mut output_data).is_ok() {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
} else {
|
||||
std::mem::forget(output_data);
|
||||
false
|
||||
match new_instance.$method($($arg.process()),*, &mut output_data) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -66,13 +82,17 @@ macro_rules! no_ctx_call_with_output_arg {
|
||||
($method:ident, $output_arg:expr, $( $arg:expr ),* ) => {
|
||||
{
|
||||
let mut output_data: Vec<u8> = Vec::new();
|
||||
if $method($($arg.process()),*, &mut output_data).is_ok() {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
} else {
|
||||
std::mem::forget(output_data);
|
||||
false
|
||||
match $method($($arg.process()),*, &mut output_data) {
|
||||
Ok(()) => {
|
||||
unsafe { *$output_arg = Buffer::from(&output_data[..]) };
|
||||
std::mem::forget(output_data);
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
std::mem::forget(output_data);
|
||||
eprintln!("execution error: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,8 +109,11 @@ macro_rules! call_with_bool_arg {
|
||||
{
|
||||
let new_instance = $instance.process();
|
||||
if match new_instance.$method($($arg.process()),*,) {
|
||||
Ok(verified) => verified,
|
||||
Err(_) => return false,
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
eprintln!("execution error: {err}");
|
||||
return false
|
||||
},
|
||||
} {
|
||||
unsafe { *$bool_arg = true };
|
||||
} else {
|
||||
@@ -171,11 +194,15 @@ impl<'a> From<&Buffer> for &'a [u8] {
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn new(tree_height: usize, input_buffer: *const Buffer, ctx: *mut *mut RLN) -> bool {
|
||||
if let Ok(rln) = RLN::new(tree_height, input_buffer.process()) {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
true
|
||||
} else {
|
||||
false
|
||||
match RLN::new(tree_height, input_buffer.process()) {
|
||||
Ok(rln) => {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("could not instantiate rln: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,18 +213,24 @@ pub extern "C" fn new_with_params(
|
||||
circom_buffer: *const Buffer,
|
||||
zkey_buffer: *const Buffer,
|
||||
vk_buffer: *const Buffer,
|
||||
tree_config: *const Buffer,
|
||||
ctx: *mut *mut RLN,
|
||||
) -> bool {
|
||||
if let Ok(rln) = RLN::new_with_params(
|
||||
match RLN::new_with_params(
|
||||
tree_height,
|
||||
circom_buffer.process().to_vec(),
|
||||
zkey_buffer.process().to_vec(),
|
||||
vk_buffer.process().to_vec(),
|
||||
tree_config.process(),
|
||||
) {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
true
|
||||
} else {
|
||||
false
|
||||
Ok(rln) => {
|
||||
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
|
||||
true
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("could not instantiate rln: {err}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -261,6 +294,22 @@ pub extern "C" fn atomic_operation(
|
||||
call!(ctx, atomic_operation, index, leaves_buffer, indices_buffer)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn seq_atomic_operation(
|
||||
ctx: *mut RLN,
|
||||
leaves_buffer: *const Buffer,
|
||||
indices_buffer: *const Buffer,
|
||||
) -> bool {
|
||||
call!(
|
||||
ctx,
|
||||
atomic_operation,
|
||||
ctx.process().leaves_set(),
|
||||
leaves_buffer,
|
||||
indices_buffer
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn get_root(ctx: *const RLN, output_buffer: *mut Buffer) -> bool {
|
||||
|
||||
@@ -117,6 +117,7 @@ impl RLN<'_> {
|
||||
/// - `circom_vec`: a byte vector containing the ZK circuit (`rln.wasm`) as binary file
|
||||
/// - `zkey_vec`: a byte vector containing to the proving key (`rln_final.zkey`) as binary file
|
||||
/// - `vk_vec`: a byte vector containing to the verification key (`verification_key.json`) as binary file
|
||||
/// - `tree_config`: a reader for a string containing a json with the merkle tree configuration
|
||||
///
|
||||
/// Example:
|
||||
/// ```
|
||||
@@ -134,6 +135,8 @@ impl RLN<'_> {
|
||||
/// let mut buffer = vec![0; metadata.len() as usize];
|
||||
/// file.read_exact(&mut buffer).expect("buffer overflow");
|
||||
/// resources.push(buffer);
|
||||
/// let tree_config = "{}".to_string();
|
||||
/// let tree_config_buffer = &Buffer::from(tree_config.as_bytes());
|
||||
/// }
|
||||
///
|
||||
/// let mut rln = RLN::new_with_params(
|
||||
@@ -141,11 +144,51 @@ impl RLN<'_> {
|
||||
/// resources[0].clone(),
|
||||
/// resources[1].clone(),
|
||||
/// resources[2].clone(),
|
||||
/// tree_config_buffer,
|
||||
/// );
|
||||
/// ```
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn new_with_params<R: Read>(
|
||||
tree_height: usize,
|
||||
circom_vec: Vec<u8>,
|
||||
zkey_vec: Vec<u8>,
|
||||
vk_vec: Vec<u8>,
|
||||
mut tree_config_input: R,
|
||||
) -> Result<RLN<'static>> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
let witness_calculator = circom_from_raw(circom_vec)?;
|
||||
|
||||
let proving_key = zkey_from_raw(&zkey_vec)?;
|
||||
let verification_key = vk_from_raw(&vk_vec, &zkey_vec)?;
|
||||
|
||||
let mut tree_config_vec: Vec<u8> = Vec::new();
|
||||
tree_config_input.read_to_end(&mut tree_config_vec)?;
|
||||
let tree_config_str = String::from_utf8(tree_config_vec)?;
|
||||
let tree_config: <PoseidonTree as ZerokitMerkleTree>::Config = if tree_config_str.is_empty()
|
||||
{
|
||||
<PoseidonTree as ZerokitMerkleTree>::Config::default()
|
||||
} else {
|
||||
<PoseidonTree as ZerokitMerkleTree>::Config::from_str(&tree_config_str)?
|
||||
};
|
||||
|
||||
// We compute a default empty tree
|
||||
let tree = PoseidonTree::new(
|
||||
tree_height,
|
||||
<PoseidonTree as ZerokitMerkleTree>::Hasher::default_leaf(),
|
||||
tree_config,
|
||||
)?;
|
||||
|
||||
Ok(RLN {
|
||||
witness_calculator,
|
||||
proving_key,
|
||||
verification_key,
|
||||
tree,
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub fn new_with_params(
|
||||
tree_height: usize,
|
||||
#[cfg(not(target_arch = "wasm32"))] circom_vec: Vec<u8>,
|
||||
zkey_vec: Vec<u8>,
|
||||
vk_vec: Vec<u8>,
|
||||
) -> Result<RLN<'static>> {
|
||||
@@ -159,12 +202,9 @@ impl RLN<'_> {
|
||||
let tree = PoseidonTree::default(tree_height)?;
|
||||
|
||||
Ok(RLN {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
witness_calculator,
|
||||
proving_key,
|
||||
verification_key,
|
||||
tree,
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
_marker: PhantomData,
|
||||
})
|
||||
}
|
||||
@@ -366,6 +406,10 @@ impl RLN<'_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn leaves_set(&mut self) -> usize {
|
||||
self.tree.leaves_set()
|
||||
}
|
||||
|
||||
/// Sets a leaf value at the next available never-set leaf index.
|
||||
///
|
||||
/// This function updates the internal Merkle tree `next_index` value indicating the next available index corresponding to a never-set leaf as `next_index = next_index + 1`.
|
||||
|
||||
@@ -617,11 +617,14 @@ mod test {
|
||||
|
||||
// Creating a RLN instance passing the raw data
|
||||
let mut rln_pointer_raw_bytes = MaybeUninit::<*mut RLN>::uninit();
|
||||
let tree_config = "".to_string();
|
||||
let tree_config_buffer = &Buffer::from(tree_config.as_bytes());
|
||||
let success = new_with_params(
|
||||
tree_height,
|
||||
circom_data,
|
||||
zkey_data,
|
||||
vk_data,
|
||||
tree_config_buffer,
|
||||
rln_pointer_raw_bytes.as_mut_ptr(),
|
||||
);
|
||||
assert!(success, "RLN object creation failed");
|
||||
|
||||
@@ -47,4 +47,4 @@ opt-level = 3
|
||||
# Dependencies are optimized, even in a dev build. This improves dev performance
|
||||
# while having neglible impact on incremental build times.
|
||||
[profile.dev.package."*"]
|
||||
opt-level = 3
|
||||
opt-level = 3
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
[package]
|
||||
name = "zerokit_utils"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Various utilities for Zerokit"
|
||||
documentation = "https://github.com/vacp2p/zerokit"
|
||||
homepage = "https://vac.dev"
|
||||
repository = "https://github.com/vacp2p/zerokit"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
@@ -12,7 +15,7 @@ bench = false
|
||||
ark-ff = { version = "=0.4.1", default-features = false, features = ["asm"] }
|
||||
num-bigint = { version = "=0.4.3", default-features = false, features = ["rand"] }
|
||||
color-eyre = "=0.6.2"
|
||||
pmtree = { package = "vacp2p_pmtree", version = "=1.0.0", optional = true}
|
||||
pmtree = { package = "pmtree", version = "=2.0.0", optional = true}
|
||||
sled = "=0.34.7"
|
||||
serde = "=1.0.163"
|
||||
|
||||
|
||||
@@ -2,29 +2,51 @@ use pmtree::*;
|
||||
|
||||
use sled::Db as Sled;
|
||||
use std::collections::HashMap;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
pub struct SledDB(Sled);
|
||||
|
||||
impl SledDB {
|
||||
fn new_with_tries(config: <SledDB as Database>::Config, tries: u32) -> PmtreeResult<Self> {
|
||||
// If we've tried more than 10 times, we give up and return an error.
|
||||
if tries >= 10 {
|
||||
return Err(PmtreeErrorKind::DatabaseError(
|
||||
DatabaseErrorKind::CustomError(format!(
|
||||
"Cannot create database: exceeded maximum retry attempts. {config:#?}"
|
||||
)),
|
||||
));
|
||||
}
|
||||
match config.open() {
|
||||
Ok(db) => Ok(SledDB(db)),
|
||||
Err(e) if e.to_string().contains("WouldBlock") => {
|
||||
// try till the fd is freed
|
||||
// sleep for 10^tries milliseconds, then recursively try again
|
||||
thread::sleep(Duration::from_millis(10u64.pow(tries)));
|
||||
Self::new_with_tries(config, tries + 1)
|
||||
}
|
||||
Err(e) => {
|
||||
// On any other error, we return immediately.
|
||||
Err(PmtreeErrorKind::DatabaseError(
|
||||
DatabaseErrorKind::CustomError(format!(
|
||||
"Cannot create database: {e} {config:#?}"
|
||||
)),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Database for SledDB {
|
||||
type Config = sled::Config;
|
||||
|
||||
fn new(config: Self::Config) -> PmtreeResult<Self> {
|
||||
let db: Sled = match config.open() {
|
||||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
return Err(PmtreeErrorKind::DatabaseError(
|
||||
DatabaseErrorKind::CustomError(format!(
|
||||
"Cannot create database: {e} {config:#?}",
|
||||
)),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
Ok(SledDB(db))
|
||||
let db = Self::new_with_tries(config, 0)?;
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
fn load(config: Self::Config) -> PmtreeResult<Self> {
|
||||
let db: Sled = match sled::open(&config.path) {
|
||||
let db = match config.open() {
|
||||
Ok(db) => db,
|
||||
Err(e) => {
|
||||
return Err(PmtreeErrorKind::DatabaseError(
|
||||
|
||||
Reference in New Issue
Block a user