Skip to content

Commit

Permalink
Merge pull request #52 from zkBob/develop
Browse files Browse the repository at this point in the history
Direct deposits, moving few libraries in zkBob domain
  • Loading branch information
EvgenKor authored Feb 15, 2023
2 parents 12cbd6d + 717b548 commit bd3a3e7
Show file tree
Hide file tree
Showing 29 changed files with 1,142 additions and 229 deletions.
17 changes: 11 additions & 6 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.2.2"
version = "0.3.0"
authors = ["Dmitry Vdovin <[email protected]>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -11,12 +11,17 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libzkbob-rs = { version = "0.9.1", features = ["native"] }
#libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
neon = { version = "0.10.0", default-features = false, features = ["channel-api", "napi-6", "promise-api"] }
libzkbob-rs = { version = "0.10.0", features = ["native"] }
#libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
neon = { version = "0.10.0", default-features = false, features = ["channel-api", "napi-6", "promise-api", "task-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"
neon-serde = { package = "neon-serde3", version = "0.10" }
serde = "1.0.136"
hex = "0.4.3"

[dependencies.fawkes-crypto]
git = "https://github.com/zkbob/fawkes-crypto"
branch = "master"
package = "fawkes-crypto-zkbob"
version = "4.5.0"
features = ["multicore"]
67 changes: 67 additions & 0 deletions libzkbob-rs-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export namespace Constants {
export const IN: number;
export const OUTLOG: number;
export const OUT: number;
export const DELEGATED_DEPOSITS_NUM: number;
}

declare class MerkleTree {
Expand Down Expand Up @@ -118,8 +119,10 @@ declare class Proof {

static tx(params: Params, tr_pub: TransferPub, tr_sec: TransferSec): Proof;
static tree(params: Params, tr_pub: TreePub, tr_sec: TreeSec): Proof;
static delegatedDeposit(params: Params, tr_pub: DelegatedDepositBatchPub, tr_sec: DelegatedDepositBatchSec): Proof;
static txAsync(params: Params, tr_pub: TransferPub, tr_sec: TransferSec): Promise<Proof>;
static treeAsync(params: Params, tr_pub: TreePub, tr_sec: TreeSec): Promise<Proof>;
static delegatedDepositAsync(params: Params, tr_pub: DelegatedDepositBatchPub, tr_sec: DelegatedDepositBatchSec): Promise<Proof>;
static verify(vk: VK, proof: SnarkProof, inputs: Array<string>): boolean;
}

Expand All @@ -129,3 +132,67 @@ declare class Helpers {
static numToStr(num: Buffer): string
static strToNum(str: string): Buffer
}

declare class Keys {
public sk: string;
public a: string;
public eta: string;

static derive(sk: string): Keys;
}

declare class TransactionData {
public: TransferPub;
secret: TransferSec;
ciphertext: Buffer;
memo: Buffer;
commitment_root: string;
out_hashes: string[];
}

interface FullDelegatedDeposit {
id: string | number;
owner: string | number;
receiver_d: string | number;
receiver_p: string | number;
denominated_amount: string | number;
denominated_fee: string | number;
expired: string | number;
}

interface MemoDelegatedDeposit {
id: string | number;
receiver_d: string | number;
receiver_p: string | number;
denominated_amount: string | number;
}

interface DelegatedDeposit {
d: string;
p_d: string;
b: string;
}

interface DelegatedDepositBatchPub {
keccak_sum: string;
}

interface DelegatedDepositBatchSec {
deposits: DelegatedDeposit[];
}

declare class DelegatedDepositsData {
public: DelegatedDepositBatchPub;
secret: DelegatedDepositBatchSec;
memo: Buffer;
out_commitment_hash: string;

static create(
deposits: MemoDelegatedDeposit[],
): Promise<DelegatedDepositsData>;
}

declare function delegatedDepositsToCommitment(
deposits: MemoDelegatedDeposit[],
): string;

20 changes: 20 additions & 0 deletions libzkbob-rs-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ const Params = {
const Proof = {
tx: zp.proveTx,
tree: zp.proveTree,
delegatedDeposit: zp.proveDelegatedDeposit,
txAsync: zp.proveTxAsync,
treeAsync: zp.proveTreeAsync,
delegatedDepositAsync: zp.proveDelegatedDepositAsync,
verify: zp.verify,
};

Expand All @@ -136,9 +138,27 @@ class Helpers {
}
}

class Keys {
static derive(sk) {
return zp.keysDerive(sk);
}
}

class DelegatedDepositsData {
static async create(
deposits,
) {
return await zp.createDelegatedDepositTxAsync(
deposits,
);
}
}

zp.MerkleTree = MerkleTree;
zp.TxStorage = TxStorage;
zp.Params = Params;
zp.Proof = Proof
zp.Helpers = Helpers;
zp.Keys = Keys;
zp.DelegatedDepositsData = DelegatedDepositsData;
module.exports = zp;
14 changes: 13 additions & 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.2.2",
"version": "0.3.0",
"description": "Neon version of libzkbob-rs",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -11,9 +11,21 @@
"install": "npm run build-release",
"test": "cargo test"
},
"files": [
"index.js",
"index.d.ts",
"src",
"Cargo.toml",
"LICENSE_APACHE",
"LICENSE_MIT",
"README.md"
],
"author": "Dmitry Vdovin <[email protected]>",
"license": "MIT OR Apache-2.0",
"dependencies": {
"cargo-cp-artifact": "^0.1"
},
"devDependencies": {
"@types/node": "18.13.0"
}
}
21 changes: 21 additions & 0 deletions libzkbob-rs-node/src/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::str::FromStr;

use libzkbob_rs::{
keys::Keys,
libzeropool::{fawkes_crypto::ff_uint::Num, POOL_PARAMS},
};
use neon::{
prelude::FunctionContext,
result::JsResult,
types::{JsString, JsValue},
};

pub fn keys_derive(mut cx: FunctionContext) -> JsResult<JsValue> {
let sk_js = cx.argument::<JsString>(0)?;
let sk_str = sk_js.value(&mut cx);
let sk = Num::from_str(&sk_str).unwrap();
let keys = Keys::derive(sk, &*POOL_PARAMS);
let res = neon_serde::to_value(&mut cx, &keys).unwrap();

Ok(res)
}
21 changes: 21 additions & 0 deletions libzkbob-rs-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use neon::prelude::*;
use serde::Serialize;

mod helpers;
mod keys;
mod merkle;
mod params;
mod proof;
mod storage;
mod tx;

pub type PoolParams = PoolBN256;
pub type Fr = <PoolParams as PoolParamsTrait>::Fr;
Expand All @@ -22,6 +24,7 @@ struct Constants {
IN: usize,
OUTLOG: usize,
OUT: usize,
DELEGATED_DEPOSITS_NUM: usize,
}

#[neon::main]
Expand All @@ -33,6 +36,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
IN: constants::IN,
OUTLOG: constants::OUTPLUSONELOG,
OUT: constants::OUT,
DELEGATED_DEPOSITS_NUM: constants::DELEGATED_DEPOSITS_NUM,
},
)
.unwrap();
Expand All @@ -44,8 +48,13 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {

cx.export_function("proveTx", proof::prove_tx)?;
cx.export_function("proveTree", proof::prove_tree)?;
cx.export_function("proveDelegatedDeposit", proof::prove_delegated_deposit)?;
cx.export_function("proveTxAsync", proof::prove_tx_async)?;
cx.export_function("proveTreeAsync", proof::prove_tree_async)?;
cx.export_function(
"proveDelegatedDepositAsync",
proof::prove_delegated_deposit_async,
)?;
cx.export_function("verify", proof::verify_proof)?;

cx.export_function("merkleNew", merkle::merkle_new)?;
Expand Down Expand Up @@ -79,6 +88,18 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("helpersParseDelta", helpers::parse_delta_string)?;
cx.export_function("helpersNumToStr", helpers::num_to_str)?;
cx.export_function("helpersStrToNum", helpers::str_to_num)?;

cx.export_function("keysDerive", keys::keys_derive)?;

cx.export_function(
"createDelegatedDepositTxAsync",
tx::create_delegated_deposit_tx_async,
)?;

cx.export_function(
"delegatedDepositsToCommitment",
tx::delegated_deposits_to_commitment,
)?;

Ok(())
}
76 changes: 52 additions & 24 deletions libzkbob-rs-node/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::prover::P
use libzkbob_rs::libzeropool::fawkes_crypto::backend::bellman_groth16::verifier::{verify, VK};
use libzkbob_rs::libzeropool::fawkes_crypto::ff_uint::Num;
use libzkbob_rs::libzeropool::POOL_PARAMS;
use libzkbob_rs::proof::{prove_tree as prove_tree_native, prove_tx as prove_tx_native};
use libzkbob_rs::proof::{
prove_delegated_deposit as prove_delegated_deposit_native, prove_tree as prove_tree_native,
prove_tx as prove_tx_native,
};
use neon::prelude::*;
use serde::{Deserialize, Serialize};

Expand All @@ -26,46 +29,53 @@ pub fn prove_tx_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
let tr_pub = neon_serde::from_value(&mut cx, tr_pub_js).unwrap();
let tr_sec = neon_serde::from_value(&mut cx, tr_sec_js).unwrap();

let channel = cx.channel();
let (deferred, promise) = cx.promise();

rayon::spawn(move || {
let pair = prove_tx_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let proof = SnarkProof {
inputs: pair.0,
proof: pair.1,
};

deferred.settle_with(&channel, move |mut cx| {
let promise = cx
.task(move || {
let (inputs, proof) = prove_tx_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
neon_serde::to_value(&mut cx, &proof).or_else(|err| cx.throw_error(err.to_string()))
});
});

Ok(promise)
}


pub fn prove_tree_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
let params: Arc<Params> = (*cx.argument::<BoxedParams>(0)?).clone();
let tr_pub_js = cx.argument::<JsValue>(1)?;
let tr_sec_js = cx.argument::<JsValue>(2)?;
let tr_pub = neon_serde::from_value(&mut cx, tr_pub_js).unwrap();
let tr_sec = neon_serde::from_value(&mut cx, tr_sec_js).unwrap();

let channel = cx.channel();
let (deferred, promise) = cx.promise();
let promise = cx
.task(move || {
let (inputs, proof) = prove_tree_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
neon_serde::to_value(&mut cx, &proof).or_else(|err| cx.throw_error(err.to_string()))
});

rayon::spawn(move || {
let pair = prove_tree_native(&params.inner, &*POOL_PARAMS, tr_pub, tr_sec);
let proof = SnarkProof {
inputs: pair.0,
proof: pair.1,
};
Ok(promise)
}

deferred.settle_with(&channel, move |mut cx| {
pub fn prove_delegated_deposit_async(mut cx: FunctionContext) -> JsResult<JsPromise> {
let params: Arc<Params> = (*cx.argument::<BoxedParams>(0)?).clone();
let d_pub_js = cx.argument::<JsValue>(1)?;
let d_sec_js = cx.argument::<JsValue>(2)?;
let d_pub = neon_serde::from_value(&mut cx, d_pub_js).unwrap();
let d_sec = neon_serde::from_value(&mut cx, d_sec_js).unwrap();

let promise = cx
.task(move || {
let (inputs, proof) =
prove_delegated_deposit_native(&params.inner, &*POOL_PARAMS, d_pub, d_sec);
SnarkProof { inputs, proof }
})
.promise(move |mut cx, proof| {
neon_serde::to_value(&mut cx, &proof).or_else(|err| cx.throw_error(err.to_string()))
});
});

Ok(promise)
}
Expand Down Expand Up @@ -110,6 +120,24 @@ pub fn prove_tree(mut cx: FunctionContext) -> JsResult<JsValue> {
Ok(result)
}

pub fn prove_delegated_deposit(mut cx: FunctionContext) -> JsResult<JsValue> {
let params = cx.argument::<BoxedParams>(0)?;

let d_pub_js = cx.argument::<JsValue>(1)?;
let d_sec_js = cx.argument::<JsValue>(2)?;
let d_pub = neon_serde::from_value(&mut cx, d_pub_js).unwrap();
let d_sec = neon_serde::from_value(&mut cx, d_sec_js).unwrap();

let (inputs, proof) =
prove_delegated_deposit_native(&params.inner, &*POOL_PARAMS, d_pub, d_sec);

let proof = SnarkProof { inputs, proof };

let result = neon_serde::to_value(&mut cx, &proof).unwrap();

Ok(result)
}

pub fn verify_proof(mut cx: FunctionContext) -> JsResult<JsValue> {
let vk_js = cx.argument::<JsValue>(0)?;
let proof_js = cx.argument::<JsValue>(1)?;
Expand Down
Loading

0 comments on commit bd3a3e7

Please sign in to comment.