Skip to content

Commit

Permalink
Add Helpers.isInPrimeSubgroup (#54)
Browse files Browse the repository at this point in the history
* Add Helpers.isInPrimeSubgroup

* Improving zk address checking

* Changing libzeropool dependency to libzeropool-zkbob

* Node lib core dependency updating

---------

Co-authored-by: EvgenKor <evgen2k7@yandex.ru>
  • Loading branch information
AllFi and EvgenKor authored Feb 17, 2023
1 parent c1410ef commit f643d90
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 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.3.0"
version = "1.0.0"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -11,7 +11,7 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libzkbob-rs = { version = "0.10.0", features = ["native"] }
libzkbob-rs = { version = "1.0.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
Expand Down
1 change: 1 addition & 0 deletions libzkbob-rs-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ declare class Helpers {
static parseDelta(delta: string): { v: string, e: string, index: string, poolId: string }
static numToStr(num: Buffer): string
static strToNum(str: string): Buffer
static isInPrimeSubgroup(num: Buffer): boolean
}

declare class Keys {
Expand Down
4 changes: 4 additions & 0 deletions libzkbob-rs-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ class Helpers {
static strToNum(str) {
return zp.helpersStrToNum(str)
}

static isInPrimeSubgroup(num) {
return zp.helpersIsInPrimeSubgroup(num)
}
}

class Keys {
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.3.0",
"version": "1.0.0",
"description": "Neon version of libzkbob-rs",
"main": "index.js",
"types": "index.d.ts",
Expand Down
13 changes: 13 additions & 0 deletions libzkbob-rs-node/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::str::FromStr;
use libzkbob_rs::libzeropool::constants::OUT;
use libzkbob_rs::libzeropool::fawkes_crypto::borsh::{BorshDeserialize, BorshSerialize};
use libzkbob_rs::libzeropool::fawkes_crypto::ff_uint::Num;
use libzkbob_rs::libzeropool::fawkes_crypto::native::ecc::EdwardsPoint;
use libzkbob_rs::libzeropool::native::params::PoolParams;
use libzkbob_rs::libzeropool::native::tx::{out_commitment_hash, parse_delta};
use libzkbob_rs::libzeropool::POOL_PARAMS;

Expand Down Expand Up @@ -81,3 +83,14 @@ pub fn str_to_num(mut cx: FunctionContext) -> JsResult<JsBuffer> {

Ok(buf)
}

pub fn is_in_prime_subgroup(mut cx: FunctionContext) -> JsResult<JsBoolean> {
let p: Num<Fr> = {
let buffer = cx.argument::<JsBuffer>(0)?;
Num::try_from_slice(buffer.as_slice(&cx)).unwrap()
};
match EdwardsPoint::subgroup_decompress(p, &*POOL_PARAMS.jubjub()) {
Some(_) => Ok(cx.boolean(true)),
None => Ok(cx.boolean(false))
}
}
1 change: 1 addition & 0 deletions libzkbob-rs-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ 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("helpersIsInPrimeSubgroup", helpers::is_in_prime_subgroup)?;

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

Expand Down
2 changes: 1 addition & 1 deletion libzkbob-rs-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs-wasm"
description = "A higher level zkBob API for Wasm"
version = "0.9.0"
version = "1.0.0"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions libzkbob-rs-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn get_constants() -> Constants {

#[wasm_bindgen(js_name = "validateAddress")]
pub fn validate_address(address: &str) -> bool {
parse_address::<PoolParams>(address).is_ok()
parse_address::<PoolParams>(address, &POOL_PARAMS).is_ok()
}

#[wasm_bindgen(js_name = "assembleAddress")]
Expand All @@ -90,7 +90,7 @@ pub fn assemble_address(d: &str, p_d: &str) -> String {

#[wasm_bindgen(js_name = "parseAddress")]
pub fn parse_address_(address: &str) -> IAddressComponents {
let (d, p_d) = parse_address::<PoolParams>(address).unwrap();
let (d, p_d) = parse_address::<PoolParams>(address, &POOL_PARAMS).unwrap();

#[derive(Serialize)]
struct Address {
Expand Down
6 changes: 3 additions & 3 deletions libzkbob-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "libzkbob-rs"
description = "A higher level zkBob API"
version = "0.10.0"
version = "1.0.0"
authors = ["Dmitry Vdovin <voidxnull@gmail.com>"]
repository = "https://github.com/zkBob/libzkbob-rs/"
license = "MIT OR Apache-2.0"
Expand All @@ -23,10 +23,10 @@ kvdb-memorydb = "0.9.0"
hex = { version = "0.4.3", features = ["serde"] }

[dependencies.libzeropool]
git = "https://github.com/zkbob/libzeropool"
git = "https://github.com/zkbob/libzeropool-zkbob"
branch = "master"
package = "libzeropool-zkbob"
version = "0.6.0"
version = "1.1.0"
default-features = false
features = ["in3out127"]

Expand Down
10 changes: 8 additions & 2 deletions libzkbob-rs/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use libzeropool::{
constants,
fawkes_crypto::{
borsh::{BorshDeserialize, BorshSerialize},
ff_uint::Num,
ff_uint::Num, native::ecc::EdwardsPoint,
},
native::boundednum::BoundedNum,
native::params::PoolParams,
Expand All @@ -16,6 +16,8 @@ const ADDR_LEN: usize = 46;
pub enum AddressParseError {
#[error("Invalid checksum")]
InvalidChecksum,
#[error("Pd does not belongs prime subgroup")]
InvalidNumber,
#[error("Decode error: {0}")]
Base58DecodeError(#[from] bs58::decode::Error),
#[error("Deserialization error: {0}")]
Expand All @@ -24,6 +26,7 @@ pub enum AddressParseError {

pub fn parse_address<P: PoolParams>(
address: &str,
params: &P,
) -> Result<
(
BoundedNum<P::Fr, { constants::DIVERSIFIER_SIZE_BITS }>,
Expand All @@ -45,7 +48,10 @@ pub fn parse_address<P: PoolParams>(
let d = BoundedNum::try_from_slice(&bytes[0..10])?;
let p_d = Num::try_from_slice(&bytes[10..42])?;

Ok((d, p_d))
match EdwardsPoint::subgroup_decompress(p_d, params.jubjub()) {
Some(_) => Ok((d, p_d)),
None => Err(AddressParseError::InvalidNumber)
}
}

pub fn format_address<P: PoolParams>(
Expand Down
4 changes: 2 additions & 2 deletions libzkbob-rs/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ where

pub fn is_own_address(&self, address: &str) -> bool {
let mut result = false;
if let Ok((d, p_d)) = parse_address::<P>(address) {
if let Ok((d, p_d)) = parse_address::<P>(address, &self.params) {
let own_p_d = derive_key_p_d(d.to_num(), self.keys.eta, &self.params).x;
result = own_p_d == p_d;
}
Expand Down Expand Up @@ -333,7 +333,7 @@ where
let out_notes = outputs
.iter()
.map(|dest| {
let (to_d, to_p_d) = parse_address::<P>(&dest.to)?;
let (to_d, to_p_d) = parse_address::<P>(&dest.to, &self.params)?;

output_value += dest.amount.to_num();

Expand Down

0 comments on commit f643d90

Please sign in to comment.