From b7d59787ed68baff98c21999faab0859063aa06b Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Sun, 10 Feb 2019 19:44:53 +0300 Subject: [PATCH] implement high-level helper api --- src/sonic/helped/adapted_helper.rs | 33 ++++++++++++++++++++++++++++++ src/sonic/helped/helper.rs | 24 ++++++++++++++++++++++ src/sonic/helped/mod.rs | 21 +++++++++++++------ tests/mimc.rs | 21 +++++++++---------- 4 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 src/sonic/helped/adapted_helper.rs diff --git a/src/sonic/helped/adapted_helper.rs b/src/sonic/helped/adapted_helper.rs new file mode 100644 index 0000000..87e99da --- /dev/null +++ b/src/sonic/helped/adapted_helper.rs @@ -0,0 +1,33 @@ +use ff::{Field}; +use pairing::{Engine, CurveProjective}; +use std::marker::PhantomData; + +use rand::{Rand, Rng}; + +use super::{Proof, SxyAdvice}; +use super::batch::Batch; +use super::poly::{SxEval, SyEval}; +use super::parameters::{Parameters}; +use super::helper::{Aggregate}; + +use crate::SynthesisError; + +use crate::sonic::transcript::{Transcript, TranscriptProtocol}; +use crate::sonic::util::*; +use crate::sonic::cs::{Backend, SynthesisDriver}; +use crate::{Circuit}; +use crate::sonic::sonic::AdaptorCircuit; +use crate::sonic::srs::SRS; +use crate::sonic::cs::Nonassigning; +use super::helper::create_aggregate as create_aggregate_sonic_circuit; + +pub fn create_aggregate + Clone>( + circuit: C, + inputs: &[(Proof, SxyAdvice)], + params: &Parameters, +) -> Aggregate +{ + let adapted_circuit = AdaptorCircuit(circuit); + + create_aggregate_sonic_circuit::<_, _, Nonassigning>(&adapted_circuit, inputs, params) +} diff --git a/src/sonic/helped/helper.rs b/src/sonic/helped/helper.rs index 44d695a..5b11f11 100644 --- a/src/sonic/helped/helper.rs +++ b/src/sonic/helped/helper.rs @@ -5,6 +5,7 @@ use std::marker::PhantomData; use super::{Proof, SxyAdvice}; use super::batch::Batch; use super::poly::{SxEval, SyEval}; +use super::Parameters; use crate::SynthesisError; @@ -27,6 +28,18 @@ pub struct Aggregate { } pub fn create_aggregate, S: SynthesisDriver>( + circuit: &C, + inputs: &[(Proof, SxyAdvice)], + params: &Parameters, +) -> Aggregate +{ + let n = params.vk.n; + let q = params.vk.q; + + create_aggregate_on_srs_using_information::(circuit, inputs, ¶ms.srs, n, q) +} + +pub fn create_aggregate_on_srs, S: SynthesisDriver>( circuit: &C, inputs: &[(Proof, SxyAdvice)], srs: &SRS, @@ -55,6 +68,17 @@ pub fn create_aggregate, S: SynthesisDriver>( (tmp.n, tmp.q) }; + create_aggregate_on_srs_using_information::(circuit, inputs, srs, n, q) +} + +pub fn create_aggregate_on_srs_using_information, S: SynthesisDriver>( + circuit: &C, + inputs: &[(Proof, SxyAdvice)], + srs: &SRS, + n: usize, + q: usize, +) -> Aggregate +{ let mut transcript = Transcript::new(&[]); let mut y_values: Vec = Vec::with_capacity(inputs.len()); for &(ref proof, ref sxyadvice) in inputs { diff --git a/src/sonic/helped/mod.rs b/src/sonic/helped/mod.rs index 05d2816..3a0be60 100644 --- a/src/sonic/helped/mod.rs +++ b/src/sonic/helped/mod.rs @@ -5,19 +5,18 @@ use ff::{Field}; use pairing::{Engine, CurveProjective}; use std::marker::PhantomData; -mod verifier; mod batch; mod poly; -mod helper; +pub mod prover; +pub mod verifier; +pub mod helper; mod parameters; mod generator; mod adapted_prover; mod adapted_verifier; - -pub mod prover; +mod adapted_helper; pub use self::batch::{Batch}; -pub use self::helper::{Aggregate, create_aggregate}; pub use self::verifier::{MultiVerifier}; pub use self::generator::{ @@ -29,7 +28,13 @@ pub use self::generator::{ generate_srs, get_circuit_parameters }; -pub use self::parameters::{Proof, SxyAdvice, Parameters, VerifyingKey, PreparedVerifyingKey}; +pub use self::parameters::{ + Proof, + SxyAdvice, + Parameters, + VerifyingKey, + PreparedVerifyingKey +}; pub use self::adapted_prover::{ create_advice, create_advice_on_srs, @@ -41,4 +46,8 @@ pub use self::adapted_prover::{ pub use self::adapted_verifier::{ verify_proofs, verify_aggregate +}; + +pub use self::adapted_helper::{ + create_aggregate }; \ No newline at end of file diff --git a/tests/mimc.rs b/tests/mimc.rs index fbaf382..8091f99 100644 --- a/tests/mimc.rs +++ b/tests/mimc.rs @@ -480,7 +480,8 @@ fn test_sonic_mimc() { use bellman::sonic::cs::Basic; use bellman::sonic::sonic::AdaptorCircuit; use bellman::sonic::helped::prover::{create_advice_on_srs, create_proof_on_srs}; - use bellman::sonic::helped::{create_aggregate, MultiVerifier}; + use bellman::sonic::helped::{MultiVerifier, get_circuit_parameters}; + use bellman::sonic::helped::helper::{create_aggregate_on_srs}; println!("creating proof"); let start = Instant::now(); @@ -495,7 +496,7 @@ fn test_sonic_mimc() { println!("creating aggregate for {} proofs", samples); let start = Instant::now(); let proofs: Vec<_> = (0..samples).map(|_| (proof.clone(), advice.clone())).collect(); - let aggregate = create_aggregate::(&AdaptorCircuit(circuit.clone()), &proofs, &srs); + let aggregate = create_aggregate_on_srs::(&AdaptorCircuit(circuit.clone()), &proofs, &srs); println!("done in {:?}", start.elapsed()); { @@ -583,7 +584,8 @@ fn test_inputs_into_sonic_mimc() { use bellman::sonic::cs::Basic; use bellman::sonic::sonic::AdaptorCircuit; use bellman::sonic::helped::prover::{create_advice_on_srs, create_proof_on_srs}; - use bellman::sonic::helped::{create_aggregate, MultiVerifier, get_circuit_parameters}; + use bellman::sonic::helped::{MultiVerifier, get_circuit_parameters}; + use bellman::sonic::helped::helper::{create_aggregate_on_srs}; let info = get_circuit_parameters::(circuit.clone()).expect("Must get circuit info"); println!("{:?}", info); @@ -601,7 +603,7 @@ fn test_inputs_into_sonic_mimc() { println!("creating aggregate for {} proofs", samples); let start = Instant::now(); let proofs: Vec<_> = (0..samples).map(|_| (proof.clone(), advice.clone())).collect(); - let aggregate = create_aggregate::(&AdaptorCircuit(circuit.clone()), &proofs, &srs); + let aggregate = create_aggregate_on_srs::(&AdaptorCircuit(circuit.clone()), &proofs, &srs); println!("done in {:?}", start.elapsed()); { @@ -651,19 +653,16 @@ fn test_inputs_into_sonic_mimc() { #[test] fn test_high_level_sonic_api() { - use ff::{Field, PrimeField}; - use pairing::{Engine, CurveAffine, CurveProjective}; - use pairing::bn256::{Bn256, Fr}; + use pairing::bn256::{Bn256}; use std::time::{Instant}; - use bellman::sonic::helped::{generate_random_parameters, + use bellman::sonic::helped::{ + generate_random_parameters, verify_aggregate, verify_proofs, create_proof, create_advice, create_aggregate }; - use bellman::sonic::cs::Basic; - use bellman::sonic::sonic::AdaptorCircuit; { // This may not be cryptographically safe, use @@ -701,7 +700,7 @@ fn test_high_level_sonic_api() { println!("creating aggregate for {} proofs", samples); let start = Instant::now(); let proofs: Vec<_> = (0..samples).map(|_| (proof.clone(), advice.clone())).collect(); - let aggregate = create_aggregate::(&AdaptorCircuit(circuit.clone()), &proofs, ¶ms.srs); + let aggregate = create_aggregate::(circuit.clone(), &proofs, ¶ms); println!("done in {:?}", start.elapsed()); {