ZenGo-X / bulletproofs

Bulletproofs and Bulletproofs+ Rust implementation for Aggregated Range Proofs over multiple elliptic curves
GNU General Public License v3.0
72 stars 42 forks source link

build a javascript interface #15

Open pranavkirtani88 opened 5 years ago

omershlo commented 5 years ago

Hi, how about wasm? would it be good enough? also - can you check out the work Vitaly has done on ecdsa - https://github.com/KZen-networks/multi-party-ecdsa/pull/59 , see if this helps?

pranavkirtani88 commented 5 years ago

is the repo still active? .I am planning to use the rust library , I am new to rust but it looks like it can be converted to wasm ,any guidance on how?

omershlo commented 5 years ago

very much active yes! It is also very much possible to convert it to wasm. @vhnatyk is an expert on how to do it (have done it in other rust libraries that are using the same code base). To start with I suggest you follow any guide on how convert from rust to wasm and let us know if you stuck on an error.

pranavkirtani88 commented 5 years ago

Are there any steps to run the rust code? I cloned the repo and installed rust.I created a sample.rs file with the code provided in readme. How to to run this file I tried rustc,cargo build,cargo run. am I missing any step?

omershlo commented 5 years ago

cargo test

pranavkirtani88 commented 5 years ago

I had run cargo test and tests pass, What I wanted to know is how do I use the code provided as sample :

use curv::arithmetic::traits::{Converter, Samplable}; use curv::cryptographic_primitives::hashing::hash_sha512::HSha512; use curv::cryptographic_primitives::hashing::traits::; use curv::elliptic::curves::traits::; use curv::BigInt; use curv::{FE, GE}; use proofs::range_proof::generate_random_point; use proofs::range_proof::RangeProof;

    bit range
    let n = 8;
    // num of agg proofs
    let m = 4;
    let nm = n * m;
    let KZen: &[u8] = &[75, 90, 101, 110];
    let kzen_label = BigInt::from(KZen);

    let G: GE = ECPoint::generator();
    let label = BigInt::from(1);
    let hash = HSha512::create_hash(&[&label]);
    let H = generate_random_point(&Converter::to_vec(&hash));

    let g_vec = (0..nm)
        .map(|i| {
            let kzen_label_i = BigInt::from(i as u32) + &kzen_label;
            let hash_i = HSha512::create_hash(&[&kzen_label_i]);
            generate_random_point(&Converter::to_vec(&hash_i))
        }).collect::<Vec<GE>>();

    // can run in parallel to g_vec:
    let h_vec = (0..nm)
        .map(|i| {
            let kzen_label_j = BigInt::from(n as u32) + BigInt::from(i as u32) + &kzen_label;
            let hash_j = HSha512::create_hash(&[&kzen_label_j]);
            generate_random_point(&Converter::to_vec(&hash_j))
        }).collect::<Vec<GE>>();

    let range = BigInt::from(2).pow(n as u32);
    let v_vec = (0..m)
        .map(|_| ECScalar::from(&BigInt::sample_below(&range)))
        .collect::<Vec<FE>>();

    let r_vec = (0..m).map(|_| ECScalar::new_random()).collect::<Vec<FE>>();

    let ped_com_vec = (0..m)
        .map(|i| {
            let ped_com = G.clone() * &v_vec[i] + H.clone() * &r_vec[i];
            ped_com
        }).collect::<Vec<GE>>();

    let range_proof = RangeProof::prove(&g_vec, &h_vec, &G, &H, v_vec, &r_vec, n);
    let result = RangeProof::verify(&range_proof, &g_vec, &h_vec, &G, &H, &ped_com_vec, n);
    assert!(result.is_ok());
omershlo commented 5 years ago

just take the code from the tests "as is".

pranavkirtani88 commented 5 years ago

We have ped com for a secret value below: let ped_com_vec = (0..m) .map(|i| { let ped_com = &G &v_vec[i] + &H &r_vec[i]; ped_com }).collect::<Vec>(); I want to generate another pedcom say for another value and subtract it from this one. But it appears the https://github.com/KZen-networks/curv/blob/master/src/elliptic/curves/secp256_k1.rs does not allow addition or subtraction of EC points ,any workaround?

omershlo commented 5 years ago

ofcourse it allow to subtract: https://github.com/KZen-networks/curv/blob/master/src/elliptic/curves/secp256_k1.rs#L732

pranavkirtani88 commented 5 years ago

I had tried that earlier,I get the following error

no method named sub_point found for type std::vec::Vec<curv::elliptic::curves::secp256_k1::Secp256k1Point> in the current scope

For the code: //original let ped_com_vec = (0..m) .map(|i| { let ped_com = &G &v_vec[i] + &H &r_vec[i]; ped_com }).collect::<Vec>(); //new let new_ped_com_vec = (0..m) .map(|i| { let ped_com = &G &v_sub_vec[i] + &H &r_vec[i]; ped_com }).collect::<Vec>(); let newest_pc=new_ped_com_vec.clone(); //subtraction let final_ped_com=newest_pc.sub_point(ped_com_vec);

omershlo commented 5 years ago

looks like you are trying to subtract vectors of points instead of points. you should subtract elements of the vectors

pranavkirtani88 commented 5 years ago

@omershlo Thanks I will try that.

@vhnatyk when I try to build wasm i get the following error

not all trait items implemented, missing: encode --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1358:1 | 853 | fn encode(&self, s: &mut S) -> Result<(), S::Error>; | ---------------------------------------------------------------- encode from trait ... 1358 | impl Encodable for path::Path { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing encode in implementation

error[E0046]: not all trait items implemented, missing: decode --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/rustc-serialize-0.3.24/src/serialize.rs:1382:1 | 904 | fn decode(d: &mut D) -> Result<Self, D::Error>; | ----------------------------------------------------------- decode from trait ... 1382 | impl Decodable for path::PathBuf { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing decode in implementation

An investigation revealed it was due to this: https://github.com/rustwasm/wasm-bindgen/issues/1488

Anyway I can proceed?

vhnatyk commented 5 years ago

Hi @pranavkirtani88 - yep, there two ways to proceed with crates that don't support wasm - either to replace them or to make them work with wasm 🙂 Seems rustc-serialize is deprecated (may be wasm support is among top reasons) according to this

Not sure about bulletproofs - but I implemented wasm support for emerald city in my fork. Reason PR was not submitted is that constant time safety is sort of an issue for pure rust crates and for wasm as well. It's not something severe, but definitely worth keeping in mind regarding security. The bitcoin's secp256k1 crate got wasm support btw - so that branch is stale since uses pure rust libsecp256k1 crate, that is not well maintained anymore. But from the point of wasm it's fully functional and can give valid ideas how to proceed - like replacing with serde etc.