From c2ada36aa7308ac75f08964ddeb1aa1bd8dbd731 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Thu, 2 Aug 2018 00:59:22 -0600 Subject: [PATCH] Add implementation of random beacon for phase 2 of Sapling MPC. --- Cargo.lock | 36 ++++++++++++++++++++ Cargo.toml | 13 ++++++++ src/bin/beacon.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/bin/beacon.rs diff --git a/Cargo.lock b/Cargo.lock index 68f5d78..14f4232 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,6 +109,23 @@ dependencies = [ "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "hex-literal" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hex-literal-impl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex-literal-impl" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libc" version = "0.2.40" @@ -150,6 +167,19 @@ dependencies = [ "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro-hack" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack-impl 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-hack-impl" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rand" version = "0.4.2" @@ -178,6 +208,8 @@ name = "sapling-mpc" version = "0.2.0" dependencies = [ "blake2-rfc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)", "phase2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -224,11 +256,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "1a70b146671de62ec8c8ed572219ca5d594d9b06c0b364d5e67b722fc559b48c" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" "checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4da5f0e01bd8a71a224a4eedecaacfcabda388dbb7a80faf04d3514287572d95" +"checksum hex-literal-impl 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1d340b6514f232f6db1bd16db65302a5278a04fef9ce867cb932e7e5fa21130a" "checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" "checksum pairing 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ceda21136251c6d5a422d3d798d8ac22515a6e8d3521bb60c59a8349d36d0d57" "checksum phase2 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "606ceb505d6e61f0dfbbc1cd1df498c3e58ad17001f06cb874c3324dd5700e77" +"checksum proc-macro-hack 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ba8d4f9257b85eb6cdf13f055cea3190520aab1409ca2ab43493ea4820c25f0" +"checksum proc-macro-hack-impl 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d5cb6f960ad471404618e9817c0e5d10b1ae74cfdf01fab89ea0641fe7fb2892" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum sapling-crypto 0.0.1 (git+https://github.com/zcash-hackworks/sapling-crypto?rev=eb409fa3cf8df215ae8d35f5e385751a0c5ffb85)" = "" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" diff --git a/Cargo.toml b/Cargo.toml index 1c46377..3c523b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,10 @@ required-features = ["verification"] name = "verify" required-features = ["verification"] +[[bin]] +name = "beacon" +required-features = ["beacon"] + [[bin]] name = "compute" @@ -26,11 +30,20 @@ pairing = "0.14" rand = "0.4" blake2-rfc = "0.2" +[dependencies.byteorder] +version = "1" +optional = true + [dependencies.sapling-crypto] git = "https://github.com/zcash-hackworks/sapling-crypto" rev = "eb409fa3cf8df215ae8d35f5e385751a0c5ffb85" optional = true +[dependencies.hex-literal] +version = "0.1" +optional = true + [features] verification = ["sapling-crypto"] u128-support = ["pairing/u128-support"] +beacon = ["byteorder", "hex-literal"] diff --git a/src/bin/beacon.rs b/src/bin/beacon.rs new file mode 100644 index 0000000..dfef368 --- /dev/null +++ b/src/bin/beacon.rs @@ -0,0 +1,85 @@ +extern crate phase2; +extern crate pairing; +extern crate rand; +extern crate blake2_rfc; +extern crate byteorder; + +#[macro_use] +extern crate hex_literal; + +use std::fs::File; +use std::io::{BufWriter, BufReader}; +use blake2_rfc::blake2b::Blake2b; + +fn main() { + let current_params = File::open("params").expect("couldn't open `./params`"); + let mut current_params = BufReader::with_capacity(1024*1024, current_params); + + let new_params = File::create("new_params").expect("couldn't create `./new_params`"); + let mut new_params = BufWriter::with_capacity(1024*1024, new_params); + + let mut sapling_spend = phase2::MPCParameters::read(&mut current_params, false) + .expect("couldn't deserialize Sapling Spend params"); + + let mut sapling_output = phase2::MPCParameters::read(&mut current_params, false) + .expect("couldn't deserialize Sapling Output params"); + + let mut sprout_joinsplit = phase2::MPCParameters::read(&mut current_params, false) + .expect("couldn't deserialize Sprout JoinSplit params"); + + // Create an RNG based on the outcome of the random beacon + let rng = &mut { + use byteorder::{ReadBytesExt, BigEndian}; + use rand::{SeedableRng}; + use rand::chacha::ChaChaRng; + + // Place beacon value here (2^42 SHA256 hash of Bitcoin block hash #534861) + let beacon_value: [u8; 32] = hex!("0000000000000000000000000000000000000000000000000000000000000000"); + + print!("Final result of beacon: "); + for b in beacon_value.iter() { + print!("{:02x}", b); + } + println!(""); + + let mut digest = &beacon_value[..]; + + let mut seed = [0u32; 8]; + for i in 0..8 { + seed[i] = digest.read_u32::().expect("digest is large enough for this to work"); + } + + ChaChaRng::from_seed(&seed) + }; + + let h1 = sapling_spend.contribute(rng); + let h2 = sapling_output.contribute(rng); + let h3 = sprout_joinsplit.contribute(rng); + + sapling_spend.write(&mut new_params).expect("couldn't write new Sapling Spend params"); + sapling_output.write(&mut new_params).expect("couldn't write new Sapling Spend params"); + sprout_joinsplit.write(&mut new_params).expect("couldn't write new Sapling Spend params"); + + let mut h = Blake2b::new(64); + h.update(&h1); + h.update(&h2); + h.update(&h3); + let h = h.finalize(); + + print!("Done!\n\n\ + Your contribution has been written to `./new_params`\n\n\ + The contribution you made is bound to the following hash:\n"); + + for line in h.as_ref().chunks(16) { + print!("\t"); + for section in line.chunks(4) { + for b in section { + print!("{:02x}", b); + } + print!(" "); + } + println!(""); + } + + println!("\n"); +}