From 1ef0e48957c138375896b630e148afea48dc0ed2 Mon Sep 17 00:00:00 2001 From: Kobi Gurkan Date: Thu, 23 Jan 2020 16:42:21 +0200 Subject: [PATCH] adds small test parameters, generalizes repr_to_big --- phase2/src/bin/export_keys.rs | 10 +++------- phase2/src/bin/prove.rs | 18 ++++++------------ phase2/src/lib.rs | 2 ++ phase2/src/utils.rs | 11 +++++++---- powersoftau/Cargo.toml | 1 + powersoftau/src/bin/beacon_constrained.rs | 1 + powersoftau/src/bin/new_constrained.rs | 1 + powersoftau/src/bn256/mod.rs | 22 ++++++++++++++++++++++ powersoftau/test.sh | 20 ++++++++++---------- 9 files changed, 53 insertions(+), 33 deletions(-) diff --git a/phase2/src/bin/export_keys.rs b/phase2/src/bin/export_keys.rs index 50f7926..a7f3340 100644 --- a/phase2/src/bin/export_keys.rs +++ b/phase2/src/bin/export_keys.rs @@ -1,18 +1,17 @@ extern crate bellman_ce; extern crate rand; extern crate phase2; -extern crate num_bigint; -extern crate num_traits; extern crate exitcode; extern crate serde; extern crate serde_json; +extern crate num_bigint; +extern crate num_traits; use std::fs; use std::fs::OpenOptions; -use num_bigint::BigUint; -use num_traits::Num; use serde::{Deserialize, Serialize}; use phase2::parameters::MPCParameters; +use phase2::utils::repr_to_big; use bellman_ce::pairing::{ Engine, CurveAffine, @@ -85,9 +84,6 @@ fn main() { vk_delta_2: vec![], h: vec![], }; - let repr_to_big = |r| { - BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) - }; let p1_to_vec = |p : &::G1Affine| { vec![ diff --git a/phase2/src/bin/prove.rs b/phase2/src/bin/prove.rs index 0fabe7f..176b950 100644 --- a/phase2/src/bin/prove.rs +++ b/phase2/src/bin/prove.rs @@ -1,17 +1,16 @@ extern crate phase2; extern crate bellman_ce; -extern crate num_bigint; -extern crate num_traits; extern crate exitcode; extern crate serde; +extern crate num_bigint; +extern crate num_traits; use std::fs; use std::fs::OpenOptions; -use num_bigint::BigUint; -use num_traits::Num; use serde::{Deserialize, Serialize}; use phase2::parameters::MPCParameters; use phase2::circom_circuit::CircomCircuit; +use phase2::utils::repr_to_big; use bellman_ce::groth16::{prepare_verifying_key, create_random_proof, verify_proof}; use bellman_ce::pairing::{ Engine, @@ -32,6 +31,7 @@ struct ProofJson { pub pi_c: Vec, } + fn main() { let args: Vec = std::env::args().collect(); if args.len() != 6 { @@ -73,12 +73,6 @@ fn main() { ).unwrap(); assert!(result, "Proof is correct"); - let repr_to_big = |r| { - BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) - }; - let repr_to_big2 = |r| { - BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) - }; let p1_to_vec = |p : &::G1Affine| { vec![ repr_to_big(p.get_x().into_repr()), @@ -116,10 +110,10 @@ fn main() { let mut public_inputs = vec![]; for x in input[1..].iter() { - public_inputs.push(repr_to_big2(x.into_repr())); + public_inputs.push(repr_to_big(x.into_repr())); } let public_json = serde_json::to_string(&public_inputs).unwrap(); fs::write(public_filename, public_json.as_bytes()).unwrap(); println!("Done!") -} \ No newline at end of file +} diff --git a/phase2/src/lib.rs b/phase2/src/lib.rs index 28c52a0..83d23e5 100644 --- a/phase2/src/lib.rs +++ b/phase2/src/lib.rs @@ -8,6 +8,8 @@ extern crate byteorder; extern crate blake2_rfc; extern crate num_cpus; extern crate crossbeam; +extern crate num_bigint; +extern crate num_traits; extern crate cfg_if; use cfg_if::cfg_if; diff --git a/phase2/src/utils.rs b/phase2/src/utils.rs index 2bf789f..1a002b2 100644 --- a/phase2/src/utils.rs +++ b/phase2/src/utils.rs @@ -6,9 +6,9 @@ use byteorder::{ BigEndian, ReadBytesExt, }; - +use num_bigint::BigUint; +use num_traits::Num; use std::sync::Arc; - use bellman_ce::pairing::{ ff::{ PrimeField, @@ -20,7 +20,6 @@ use bellman_ce::pairing::{ G2, } }; - use rand::{ Rng, Rand, @@ -114,4 +113,8 @@ pub fn hash_to_g2(mut digest: &[u8]) -> G2 } ChaChaRng::from_seed(&seed).gen() -} \ No newline at end of file +} + +pub fn repr_to_big(r: T) -> String { + BigUint::from_str_radix(&format!("{}", r)[2..], 16).unwrap().to_str_radix(10) +} diff --git a/powersoftau/Cargo.toml b/powersoftau/Cargo.toml index 6be43a4..4df30c1 100644 --- a/powersoftau/Cargo.toml +++ b/powersoftau/Cargo.toml @@ -28,4 +28,5 @@ itertools = "0.8.0" bellman_ce = { path = "../bellman" } [features] +smalltest = [] diff --git a/powersoftau/src/bin/beacon_constrained.rs b/powersoftau/src/bin/beacon_constrained.rs index 8cf6efd..affd8dd 100644 --- a/powersoftau/src/bin/beacon_constrained.rs +++ b/powersoftau/src/bin/beacon_constrained.rs @@ -7,6 +7,7 @@ extern crate byteorder; extern crate crypto; use powersoftau::bn256::{Bn256CeremonyParameters}; + use powersoftau::batched_accumulator::{BatchedAccumulator}; use powersoftau::keypair::{keypair}; use powersoftau::parameters::{UseCompression, CheckForCorrectness}; diff --git a/powersoftau/src/bin/new_constrained.rs b/powersoftau/src/bin/new_constrained.rs index 1f6ac26..7839ea0 100644 --- a/powersoftau/src/bin/new_constrained.rs +++ b/powersoftau/src/bin/new_constrained.rs @@ -3,6 +3,7 @@ extern crate bellman_ce; extern crate memmap; use powersoftau::bn256::{Bn256CeremonyParameters}; + use powersoftau::batched_accumulator::{BatchedAccumulator}; use powersoftau::parameters::{UseCompression}; use powersoftau::utils::{blank_hash}; diff --git a/powersoftau/src/bn256/mod.rs b/powersoftau/src/bn256/mod.rs index 1c67010..4194fdc 100644 --- a/powersoftau/src/bn256/mod.rs +++ b/powersoftau/src/bn256/mod.rs @@ -24,11 +24,13 @@ use crate::parameters::*; use crate::keypair::*; use crate::utils::*; +#[cfg(not(feature = "smalltest"))] #[derive(Clone)] pub struct Bn256CeremonyParameters { } +#[cfg(not(feature = "smalltest"))] impl PowersOfTauParameters for Bn256CeremonyParameters { const REQUIRED_POWER: usize = 28; // generate to have roughly 64 million constraints @@ -39,6 +41,26 @@ impl PowersOfTauParameters for Bn256CeremonyParameters { const G2_COMPRESSED_BYTE_SIZE: usize = 64; } +#[cfg(feature = "smalltest")] +#[derive(Clone)] +pub struct Bn256CeremonyParameters { + +} + +#[cfg(feature = "smalltest")] +impl PowersOfTauParameters for Bn256CeremonyParameters { + const REQUIRED_POWER: usize = 12; + const EMPIRICAL_BATCH_SIZE: usize = 1 << 10; + + // This ceremony is based on the BN256 elliptic curve construction. + const G1_UNCOMPRESSED_BYTE_SIZE: usize = 64; + const G2_UNCOMPRESSED_BYTE_SIZE: usize = 128; + const G1_COMPRESSED_BYTE_SIZE: usize = 32; + const G2_COMPRESSED_BYTE_SIZE: usize = 64; +} + + + #[test] fn test_pubkey_serialization() { use self::rand::thread_rng; diff --git a/powersoftau/test.sh b/powersoftau/test.sh index 7445f71..c525ab6 100755 --- a/powersoftau/test.sh +++ b/powersoftau/test.sh @@ -8,18 +8,18 @@ rm tmp_* set -e -cargo run --release --bin new_constrained challenge1 -cargo run --release --bin compute_constrained challenge1 response1 -cargo run --release --bin verify_transform_constrained challenge1 response1 challenge2 +cargo run --release --bin new_constrained --features smalltest -- challenge1 +yes | cargo run --release --bin compute_constrained --features smalltest -- challenge1 response1 +cargo run --release --bin verify_transform_constrained --features smalltest -- challenge1 response1 challenge2 -cargo run --release --bin beacon_constrained challenge2 response2 -cargo run --release --bin verify_transform_constrained challenge2 response2 challenge3 +yes | cargo run --release --bin compute_constrained --features smalltest -- challenge2 response2 +cargo run --release --bin verify_transform_constrained --features smalltest -- challenge2 response2 challenge3 -cargo run --release --bin beacon_constrained challenge3 response3 -cargo run --release --bin verify_transform_constrained challenge3 response3 challenge4 +yes | cargo run --release --bin compute_constrained --features smalltest -- challenge3 response3 +cargo run --release --bin verify_transform_constrained --features smalltest -- challenge3 response3 challenge4 -cargo run --release --bin beacon_constrained challenge4 response4 -cargo run --release --bin verify_transform_constrained challenge4 response4 challenge5 +cargo run --release --bin beacon_constrained --features smalltest -- challenge4 response4 +cargo run --release --bin verify_transform_constrained --features smalltest -- challenge4 response4 challenge5 cat response1 response2 response3 response4 > transcript -cargo run --release --bin verify transcript \ No newline at end of file +cargo run --release --bin verify --features smalltest -- transcript