Skip to content

Commit

Permalink
Updating libzkbob-rs-node library to 0.2.0: getLeftSiblings routine
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgenKor committed Dec 5, 2022
1 parent dca5643 commit 16451dc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
7 changes: 4 additions & 3 deletions libzkbob-rs-node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libzkbob-rs-node"
version = "0.1.27"
version = "0.2.0"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -11,11 +11,12 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libzkbob-rs = { version = "0.3.16", features = ["native"] }
#libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
#libzkbob-rs = { version = "0.3.16", features = ["native"] }
libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
neon = { version = "0.10.0", default-features = false, features = ["channel-api", "napi-6", "promise-api"] }
# FIXME: Using a random fork for now
neon-serde = { git = "https://github.com/NZXTCorp/neon-serde.git", branch = "refactor/update-neon-0.10" }
rayon = "1.5.3"
serde = "1.0.136"
hex = "0.4.3"

4 changes: 4 additions & 0 deletions libzkbob-rs-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class MerkleTree {
rollback(index) {
return zp.merkleRollback(this.inner, index)
}

getLeftSiblings(index) {
return zp.merkleGetLeftSiblings(this.inner, index)
}
}

class TxStorage {
Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs-node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libzkbob-rs-node",
"version": "0.1.27",
"version": "0.2.0",
"description": "Neon version of libzkbob-rs",
"main": "index.js",
"types": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions libzkbob-rs-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("merkleGetAllNodes", merkle::merkle_get_all_nodes)?;
cx.export_function("merkleGetVirtualNode", merkle::merkle_get_virtual_node)?;
cx.export_function("merkleRollback", merkle::merkle_rollback)?;
cx.export_function("merkleGetLeftSiblings", merkle::merkle_get_left_siblings)?;

cx.export_function("txStorageNew", storage::tx_storage_new)?;
cx.export_function("txStorageAdd", storage::tx_storage_add)?;
Expand Down
57 changes: 54 additions & 3 deletions libzkbob-rs-node/src/merkle.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use std::collections::HashMap;
use std::sync::RwLock;
use std::vec::Vec;

use libzkbob_rs::libzeropool::fawkes_crypto::borsh::BorshDeserialize;
use libzkbob_rs::libzeropool::fawkes_crypto::borsh::{BorshSerialize, BorshDeserialize};
use libzkbob_rs::libzeropool::fawkes_crypto::ff_uint::Num;
use libzkbob_rs::libzeropool::{
constants::{HEIGHT, OUTPLUSONELOG},
constants::{HEIGHT, OUT, OUTPLUSONELOG},
POOL_PARAMS,
};
use libzkbob_rs::merkle::NativeMerkleTree;
use neon::prelude::*;
use neon::types::buffer::TypedArray;
//use serde::Serialize;
use hex;

use crate::PoolParams;

Expand Down Expand Up @@ -221,3 +222,53 @@ pub fn merkle_rollback(mut cx: FunctionContext) -> JsResult<JsUndefined> {

Ok(cx.undefined())
}

pub fn merkle_get_left_siblings(mut cx: FunctionContext) -> JsResult<JsValue> {
let tree = cx.argument::<BoxedMerkleTree>(0)?;
let index = {
let num = cx.argument::<JsNumber>(1)?;
num.value(&mut cx) as u64
};

if index & OUTPLUSONELOG as u64 != 0 {
return cx.throw_error(format!("Index to get sibling from should be multiple of {}", OUT + 1));
}

let siblings = tree.read().unwrap().inner.get_left_siblings(index);
let array = match siblings {
Some(val) => {
let result: Result<Vec<String>, String> = val
.into_iter()
.map(|node| {
let height = format!("{:#04x}", node.height);
let index = format!("{:#014x}", node.index);

let mut node_bytes = vec![];
node.value.serialize(&mut node_bytes).unwrap();
let node_string = hex::encode(node_bytes);


let res = format!("{}{}{}",
height.strip_prefix("0x").unwrap_or(&height),
index.strip_prefix("0x").unwrap_or(&index),
node_string
);

if res.len() != 78 {
return Err(format!("Internal error (sibling length {} is invalid)", res.len()));
}

Ok(res)
})
.collect();

result
},
None => Err(format!("Tree is undefined at index {}", index)) //cx.throw_error(&format!("Tree is undefined at index {}", index))
};

match array {
Ok(val) => Ok(neon_serde::to_value(&mut cx, &val).unwrap()),
Err(e) => cx.throw_error(e),
}
}
14 changes: 12 additions & 2 deletions libzkbob-rs-node/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ const zp = require('./index.js')

let tree = new zp.MerkleTree('./testdb');

for (let i = 0; i < 100; ++i) {
console.log('Building the tree...');
for (let i = 0; i < 512; ++i) {
tree.addHash(i, Buffer.alloc(32));
}

console.log('Calculating proof...')
let proof = tree.getProof(50);
console.log('Proof', proof);
console.log('Proof', proof);

console.log('Getting siblings...')
try {
let siblings = tree.getLeftSiblings(111);
console.log(siblings);
} catch(err) {
console.error(`Cannot get siblings: ${err}`);
}

0 comments on commit 16451dc

Please sign in to comment.