From 2537d11f17d427a1ecea99445182b32b69474888 Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Sun, 18 Mar 2018 12:39:08 -0600 Subject: [PATCH] Add random beacon query code. --- Cargo.lock | 128 ++++++++++++++++++++++++++++++++++++----- Cargo.toml | 2 + src/bin/beacon.rs | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 258 insertions(+), 14 deletions(-) create mode 100644 src/bin/beacon.rs diff --git a/Cargo.lock b/Cargo.lock index 981f146..c1d40c4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,17 +1,3 @@ -[root] -name = "powersoftau" -version = "0.1.1" -dependencies = [ - "blake2 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", - "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "crossbeam 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "generic-array 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", - "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", - "pairing 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", - "typenum 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "bitflags" version = "0.7.0" @@ -81,6 +67,11 @@ dependencies = [ "bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "gcc" +version = "0.3.54" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "generic-array" version = "0.8.3" @@ -90,6 +81,23 @@ dependencies = [ "typenum 1.9.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.32" @@ -117,6 +125,35 @@ dependencies = [ "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "powersoftau" +version = "0.1.1" +dependencies = [ + "blake2 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "crossbeam 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-literal 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pairing 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum 1.9.0 (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.3.17" @@ -126,11 +163,62 @@ dependencies = [ "libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "redox_syscall" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "time" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.32 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "typenum" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum bitflags 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "aad18937a628ec6abcd26d1489012cc0e18c21798210f491af69ded9b881106d" "checksum blake2 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "53bf612c0f2839b7e764ebac65d6cb985f7c6812de399d0728038f4b1da141bc" @@ -142,10 +230,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum digest 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e5b29bf156f3f4b3c4f610a25ff69370616ae6e0657d416de22645483e72af0a" "checksum fuchsia-zircon 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f6c0581a4e363262e52b87f59ee2afe3415361c6ec35e665924eb08afe8ff159" "checksum fuchsia-zircon-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "43f3795b4bae048dc6123a6b972cadde2e676f9ded08aef6bb77f5f157684a82" +"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" "checksum generic-array 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "fceb69994e330afed50c93524be68c42fa898c2d9fd4ee8da03bd7363acd26f2" +"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.32 (registry+https://github.com/rust-lang/crates.io-index)" = "56cce3130fd040c28df6f495c8492e5ec5808fb4c9093c310df02b0c8f030148" "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum num_cpus 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "514f0d73e64be53ff320680ca671b64fe3fb91da01e1ae2ddc99eb51d453b20d" "checksum pairing 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c6cecc13d1871265fea9be96aa9f1cc027edacd55d2fb1fbc164e19725b88d70" +"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.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "61efcbcd9fa8d8fbb07c84e34a8af18a1ff177b449689ad38a6e9457ecc7b2ae" +"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" +"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098" "checksum typenum 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "13a99dc6780ef33c78780b826cf9d2a78840b72cae9474de4bcaf9051e60ebbd" +"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index b491296..9b150fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ generic-array = "0.8.3" typenum = "1.9.0" byteorder = "1.1.0" pairing = "0.13.0" +hex-literal = "0.1" +rust-crypto = "0.2" [features] u128-support = ["pairing/u128-support"] diff --git a/src/bin/beacon.rs b/src/bin/beacon.rs new file mode 100644 index 0000000..a76d0d2 --- /dev/null +++ b/src/bin/beacon.rs @@ -0,0 +1,142 @@ +extern crate powersoftau; +extern crate rand; +extern crate blake2; +extern crate byteorder; + +#[macro_use] +extern crate hex_literal; + +extern crate crypto; + +use powersoftau::*; + +use std::fs::OpenOptions; +use std::io::{Read, BufReader, Write, BufWriter}; + +fn main() { + // Create an RNG based on the outcome of the random beacon + let mut rng = { + use byteorder::{ReadBytesExt, BigEndian}; + use rand::{SeedableRng}; + use rand::chacha::ChaChaRng; + use crypto::sha2::Sha256; + use crypto::digest::Digest; + + // Place block hash here (this will change) + let mut cur_hash: [u8; 32] = hex!("000000000000000000357b7d5040ee362f037bdf873eb4cde615b2f368f2f2a6"); + + // Performs 2^n hash iterations over it + const N: usize = 40; + + for i in 0..(1u64<().expect("digest is large enough for this to work"); + } + + ChaChaRng::from_seed(&seed) + }; + + // Try to load `./challenge` from disk. + let reader = OpenOptions::new() + .read(true) + .open("challenge").expect("unable open `./challenge` in this directory"); + + { + let metadata = reader.metadata().expect("unable to get filesystem metadata for `./challenge`"); + if metadata.len() != (ACCUMULATOR_BYTE_SIZE as u64) { + panic!("The size of `./challenge` should be {}, but it's {}, so something isn't right.", ACCUMULATOR_BYTE_SIZE, metadata.len()); + } + } + + let reader = BufReader::new(reader); + let mut reader = HashReader::new(reader); + + // Create `./response` in this directory + let writer = OpenOptions::new() + .read(false) + .write(true) + .create_new(true) + .open("response").expect("unable to create `./response` in this directory"); + + let writer = BufWriter::new(writer); + let mut writer = HashWriter::new(writer); + + println!("Reading `./challenge` into memory..."); + + // Read the BLAKE2b hash of the previous contribution + { + // We don't need to do anything with it, but it's important for + // the hash chain. + let mut tmp = [0; 64]; + reader.read_exact(&mut tmp).expect("unable to read BLAKE2b hash of previous contribution"); + } + + // Load the current accumulator into memory + let mut current_accumulator = Accumulator::deserialize(&mut reader, UseCompression::No, CheckForCorrectness::No).expect("unable to read uncompressed accumulator"); + + // Get the hash of the current accumulator + let current_accumulator_hash = reader.into_hash(); + + // Construct our keypair using the RNG we created above + let (pubkey, privkey) = keypair(&mut rng, current_accumulator_hash.as_ref()); + + // Perform the transformation + println!("Computing, this could take a while..."); + current_accumulator.transform(&privkey); + println!("Writing your contribution to `./response`..."); + + // Write the hash of the input accumulator + writer.write_all(¤t_accumulator_hash.as_ref()).expect("unable to write BLAKE2b hash of input accumulator"); + + // Write the transformed accumulator (in compressed form, to save upload bandwidth for disadvantaged + // players.) + current_accumulator.serialize(&mut writer, UseCompression::Yes).expect("unable to write transformed accumulator"); + + // Write the public key + pubkey.serialize(&mut writer).expect("unable to write public key"); + + // Get the hash of the contribution, so the user can compare later + let contribution_hash = writer.into_hash(); + + print!("Done!\n\n\ + Your contribution has been written to `./response`\n\n\ + The BLAKE2b hash of `./response` is:\n"); + + for line in contribution_hash.as_slice().chunks(16) { + print!("\t"); + for section in line.chunks(4) { + for b in section { + print!("{:02x}", b); + } + print!(" "); + } + println!(""); + } + + println!("\n"); +}