-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaggregation.rs
56 lines (52 loc) · 2.09 KB
/
aggregation.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use ark_bls12_381::{Bls12_381, Fr};
use ark_ff::One;
use ark_groth16::{prepare_verifying_key, Groth16};
use snarkpack;
use snarkpack::transcript::Transcript;
mod constraints;
use crate::constraints::Benchmark;
use rand_core::SeedableRng;
#[test]
fn groth16_aggregation() {
let num_constraints = 1000;
let nproofs = 8;
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(1u64);
let params = {
let c = Benchmark::<Fr>::new(num_constraints);
Groth16::<Bls12_381>::generate_random_parameters_with_reduction(c, &mut rng).unwrap()
};
// prepare the verification key
let pvk = prepare_verifying_key(¶ms.vk);
// prepare the SRS needed for snarkpack - specialize after to the right
// number of proofs
let srs = snarkpack::srs::setup_fake_srs::<Bls12_381, _>(&mut rng, nproofs);
let (prover_srs, ver_srs) = srs.specialize(nproofs);
// create all the proofs
let proofs = (0..nproofs)
.map(|_| {
let c = Benchmark::new(num_constraints);
Groth16::<Bls12_381>::create_random_proof_with_reduction(c, ¶ms, &mut rng)
.expect("proof creation failed")
})
.collect::<Vec<_>>();
// verify we can at least verify one
let inputs: Vec<_> = [Fr::one(); 2].to_vec();
let all_inputs = (0..nproofs).map(|_| inputs.clone()).collect::<Vec<_>>();
let r = Groth16::<Bls12_381>::verify_proof(&pvk, &proofs[1], &inputs).unwrap();
assert!(r);
let mut prover_transcript = snarkpack::transcript::new_merlin_transcript(b"test aggregation");
prover_transcript.append(b"public-inputs", &all_inputs);
let aggregate_proof = snarkpack::aggregate_proofs(&prover_srs, &mut prover_transcript, &proofs)
.expect("error in aggregation");
let mut ver_transcript = snarkpack::transcript::new_merlin_transcript(b"test aggregation");
ver_transcript.append(b"public-inputs", &all_inputs);
snarkpack::verify_aggregate_proof(
&ver_srs,
&pvk,
&all_inputs,
&aggregate_proof,
&mut rng,
&mut ver_transcript,
)
.expect("error in verification");
}