Skip to content

Commit 84da93c

Browse files
noel2004Ho
andauthored
Verify subproof part in aggregate proof (#119)
* update dependencies from bellman_ce to franklin_crypto * induce verification of aggregation part in recursive proof Co-authored-by: Ho <ho@fluidex.com>
1 parent 764014b commit 84da93c

File tree

12 files changed

+224
-46
lines changed

12 files changed

+224
-46
lines changed

Cargo.lock

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ path = "src/bin/main.rs"
1414

1515
[dependencies]
1616
anyhow = "1.0.34"
17-
bellman_ce = { git = "https://github.com/matter-labs/bellman", branch = "beta", default-features = false, features = [ "plonk" ] } # active features depend on build type
17+
# bellman_ce = { git = "https://github.com/matter-labs/bellman", branch = "beta", default-features = false, features = [ "plonk" ] } # active features depend on build type
1818
bellman_vk_codegen = { git = "https://github.com/fluidex/solidity_plonk_verifier.git" }
1919
byteorder = "1"
2020
clap = { package = "clap-v3", version = "3.0.0-beta.1" } # todo: replace with official v3 when it's released to crates.io
@@ -23,13 +23,13 @@ franklin-crypto = { git = "https://github.com/matter-labs/franklin-crypto", bran
2323
hex-literal = "0.2.1"
2424
itertools = "0.8.1"
2525
log = "0.4.11"
26-
num-bigint = "0.2.3"
26+
num-bigint = "0.3.3"
2727
num-traits = "0.2.8"
2828
rand = "0.4"
29-
recursive_aggregation_circuit = { package = "recursive_aggregation_circuit", version = "1.0.0", git = "https://github.com/matter-labs/recursive_aggregation_circuit.git", branch = "master" }
29+
# recursive_aggregation_circuit = { package = "recursive_aggregation_circuit", version = "1.0.0", git = "https://github.com/matter-labs/recursive_aggregation_circuit.git", branch = "master" }
3030
serde = { version = "1.0", features = [ "derive" ] }
3131
serde_json = "1.0"
3232
recurisive_vk_codegen = { package = "solidity_recursive_plonk_verifier", git = "https://github.com/fluidex/solidity_recursive_plonk_verifier.git" }
3333

3434
[features]
35-
default = [ "bellman_ce/multicore" ]
35+
default = [ ]

src/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![cfg(not(tarpaulin_include))]
22

3-
extern crate bellman_ce;
43
extern crate bellman_vk_codegen;
54
extern crate clap;
65
extern crate plonkit;
@@ -10,7 +9,7 @@ use std::fs::File;
109
use std::path::Path;
1110
use std::str;
1211

13-
use bellman_ce::pairing::bn256::Bn256;
12+
use plonkit::bellman_ce::pairing::bn256::Bn256;
1413

1514
use plonkit::circom_circuit::CircomCircuit;
1615
use plonkit::plonk;

src/circom_circuit.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
#![allow(clippy::needless_range_loop)]
2-
extern crate bellman_ce;
32
extern crate rand;
43

54
use itertools::Itertools;
65
use std::collections::BTreeMap;
76
use std::str;
87

9-
use bellman_ce::{
8+
use crate::bellman_ce::{
109
pairing::{ff::PrimeField, ff::ScalarEngine, Engine},
1110
Circuit, ConstraintSystem, Index, LinearCombination, SynthesisError, Variable,
1211
};

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
extern crate serde;
55
#[macro_use]
66
extern crate hex_literal;
7-
extern crate bellman_ce;
87
extern crate bellman_vk_codegen;
98
extern crate byteorder;
9+
extern crate franklin_crypto;
1010
extern crate itertools;
1111
extern crate num_bigint;
1212
extern crate num_traits;
@@ -20,5 +20,7 @@ pub mod recursive;
2020
pub mod transpile;
2121
pub mod utils;
2222

23+
pub use franklin_crypto::bellman as bellman_ce;
24+
2325
#[cfg(test)]
2426
mod tests;

src/plonk.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Most of this file is forked from source codes of [Matter Labs's zkSync](https://github.com/matter-labs/zksync)
2-
use crate::circom_circuit::CircomCircuit;
3-
use crate::transpile::{transpile_with_gates_count, ConstraintStat, TranspilerWrapper};
4-
use bellman_ce::bn256::Bn256;
5-
use bellman_ce::{
2+
use crate::bellman_ce::bn256::Bn256;
3+
use crate::bellman_ce::{
64
kate_commitment::{Crs, CrsForLagrangeForm, CrsForMonomialForm},
75
pairing::Engine,
86
plonk::{
@@ -15,6 +13,8 @@ use bellman_ce::{
1513
worker::Worker,
1614
Circuit, ScalarEngine, SynthesisError,
1715
};
16+
use crate::circom_circuit::CircomCircuit;
17+
use crate::transpile::{transpile_with_gates_count, ConstraintStat, TranspilerWrapper};
1818

1919
type E = Bn256;
2020
use franklin_crypto::plonk::circuit::bigint::field::RnsParameters;
@@ -193,11 +193,15 @@ pub fn verify(
193193
) -> Result<bool, SynthesisError> {
194194
match transcript {
195195
"keccak" => {
196-
bellman_ce::plonk::better_cs::verifier::verify::<_, _, RollingKeccakTranscript<<E as ScalarEngine>::Fr>>(proof, vk, None)
196+
crate::bellman_ce::plonk::better_cs::verifier::verify::<_, _, RollingKeccakTranscript<<E as ScalarEngine>::Fr>>(proof, vk, None)
197197
}
198198
"rescue" => {
199199
let (bn256_param, rns_param) = get_default_rescue_transcript_params();
200-
bellman_ce::plonk::better_cs::verifier::verify::<_, _, RescueTranscriptForRNS<E>>(proof, vk, Some((&bn256_param, &rns_param)))
200+
crate::bellman_ce::plonk::better_cs::verifier::verify::<_, _, RescueTranscriptForRNS<E>>(
201+
proof,
202+
vk,
203+
Some((&bn256_param, &rns_param)),
204+
)
201205
}
202206
_ => {
203207
unimplemented!("invalid transcript. use 'keccak' or 'rescue'");

src/r1cs_file.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// some codes borrowed from https://github.com/poma/zkutil/blob/master/src/r1cs_reader.rs
22
#![allow(unused_variables, dead_code)]
3-
use crate::circom_circuit::Constraint;
4-
use bellman_ce::pairing::{
3+
use crate::bellman_ce::pairing::{
54
bn256::Bn256,
65
ff::{Field, PrimeField, PrimeFieldRepr},
76
Engine,
87
};
8+
use crate::circom_circuit::Constraint;
99
use byteorder::{LittleEndian, ReadBytesExt};
1010
use std::io::{Error, ErrorKind, Read, Result};
1111

@@ -192,7 +192,7 @@ mod tests {
192192
"
193193
);
194194

195-
use bellman_ce::pairing::ff;
195+
use crate::bellman_ce::pairing::ff;
196196
let file = from_reader(&data[..]).unwrap();
197197
assert_eq!(file.version, 1);
198198

src/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fs::{File, OpenOptions};
66
use std::io::{BufRead, BufReader, Read};
77
use std::str;
88

9-
use bellman_ce::{
9+
use crate::bellman_ce::{
1010
kate_commitment::{Crs, CrsForLagrangeForm, CrsForMonomialForm},
1111
pairing::{bn256::Bn256, ff::PrimeField, Engine},
1212
plonk::{

src/recursive/mod.rs

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
#![allow(clippy::needless_range_loop)]
2-
2+
use crate::{bellman_ce, utils};
33
use bellman_ce::kate_commitment::{Crs, CrsForMonomialForm};
4+
use bellman_ce::pairing::bn256;
5+
use bellman_ce::pairing::bn256::Bn256;
6+
use bellman_ce::pairing::ff::ScalarEngine;
7+
use bellman_ce::pairing::{CurveAffine, Engine};
8+
use bellman_ce::plonk::better_better_cs::cs::PlonkCsWidth4WithNextStepAndCustomGatesParams;
9+
use bellman_ce::plonk::better_better_cs::cs::ProvingAssembly;
10+
use bellman_ce::plonk::better_better_cs::cs::TrivialAssembly;
11+
use bellman_ce::plonk::better_better_cs::cs::Width4MainGateWithDNext;
12+
use bellman_ce::plonk::better_better_cs::cs::{Circuit, Setup};
13+
use bellman_ce::plonk::better_better_cs::setup::VerificationKey;
14+
use bellman_ce::plonk::better_better_cs::verifier::verify as core_verify;
15+
use bellman_ce::plonk::commitments::transcript::keccak_transcript::RollingKeccakTranscript;
416
use bellman_ce::plonk::{
517
better_cs::cs::PlonkCsWidth4WithNextStepParams,
618
better_cs::keys::{Proof as OldProof, VerificationKey as OldVerificationKey},
719
};
8-
use bellman_ce::SynthesisError;
9-
use franklin_crypto::bellman::pairing::bn256;
10-
use franklin_crypto::bellman::pairing::bn256::Bn256;
11-
use franklin_crypto::bellman::pairing::ff::ScalarEngine;
12-
use franklin_crypto::bellman::pairing::{CurveAffine, Engine};
13-
use franklin_crypto::bellman::plonk::better_better_cs::cs::PlonkCsWidth4WithNextStepAndCustomGatesParams;
14-
use franklin_crypto::bellman::plonk::better_better_cs::cs::ProvingAssembly;
15-
use franklin_crypto::bellman::plonk::better_better_cs::cs::TrivialAssembly;
16-
use franklin_crypto::bellman::plonk::better_better_cs::cs::Width4MainGateWithDNext;
17-
use franklin_crypto::bellman::plonk::better_better_cs::cs::{Circuit, Setup};
18-
use franklin_crypto::bellman::plonk::better_better_cs::setup::VerificationKey;
19-
use franklin_crypto::bellman::plonk::better_better_cs::verifier::verify as core_verify;
20-
use franklin_crypto::bellman::plonk::commitments::transcript::keccak_transcript::RollingKeccakTranscript;
21-
use franklin_crypto::bellman::worker::Worker;
20+
use bellman_ce::worker::Worker;
21+
use bellman_ce::{Field, SynthesisError};
2222
use franklin_crypto::plonk::circuit::bigint::field::RnsParameters;
2323
use franklin_crypto::plonk::circuit::verifier_circuit::affine_point_wrapper::aux_data::{AuxData, BN256AuxData};
2424
use franklin_crypto::plonk::circuit::verifier_circuit::data_structs::IntoLimbedWitness;
2525
use franklin_crypto::plonk::circuit::Width4WithCustomGates;
2626
use franklin_crypto::rescue::bn256::Bn256RescueParams;
2727
use itertools::Itertools;
28-
pub use recurisive_vk_codegen::types::{AggregatedProof, RecursiveVerificationKey};
29-
use recursive_aggregation_circuit::circuit::{
28+
use recurisive_vk_codegen::circuit::{
3029
create_recursive_circuit_setup, create_recursive_circuit_vk_and_setup, create_vks_tree, make_aggregate,
3130
make_public_input_and_limbed_aggregate, RecursiveAggregationCircuitBn256,
3231
};
32+
pub use recurisive_vk_codegen::types::{AggregatedProof, RecursiveVerificationKey};
3333

3434
// only support depth<8. different depths don't really make performance different
3535
const VK_TREE_DEPTH: usize = 7;
@@ -135,6 +135,42 @@ pub fn prove(
135135
})
136136
}
137137

138+
fn verify_subproof_limbs(
139+
proof: &AggregatedProof,
140+
vk: &VerificationKey<Bn256, RecursiveAggregationCircuitBn256>,
141+
) -> Result<bool, SynthesisError> {
142+
let mut rns_params = RnsParameters::<Bn256, <Bn256 as Engine>::Fq>::new_for_field(68, 110, 4);
143+
144+
//keep the behavior same as recursive_aggregation_circuit
145+
rns_params.set_prefer_single_limb_allocation(true);
146+
147+
let aggr_limbs_nums: Vec<utils::BigUint> = proof.aggr_limbs.iter().map(utils::fe_to_biguint).collect();
148+
//we need 4 Fr to build 2 G1Affine ...
149+
let num_consume = rns_params.num_limbs_for_in_field_representation;
150+
assert_eq!(num_consume * 4, aggr_limbs_nums.len());
151+
152+
let mut start = 0;
153+
let pg_x = utils::witness_to_field(&aggr_limbs_nums[start..start + num_consume], &rns_params);
154+
start += num_consume;
155+
let pg_y = utils::witness_to_field(&aggr_limbs_nums[start..start + num_consume], &rns_params);
156+
start += num_consume;
157+
let px_x = utils::witness_to_field(&aggr_limbs_nums[start..start + num_consume], &rns_params);
158+
start += num_consume;
159+
let px_y = utils::witness_to_field(&aggr_limbs_nums[start..start + num_consume], &rns_params);
160+
161+
let pair_with_generator = bn256::G1Affine::from_xy_checked(pg_x, pg_y).map_err(|_| SynthesisError::Unsatisfiable)?;
162+
let pair_with_x = bn256::G1Affine::from_xy_checked(px_x, px_y).map_err(|_| SynthesisError::Unsatisfiable)?;
163+
164+
let valid = Bn256::final_exponentiation(&Bn256::miller_loop(&[
165+
(&pair_with_generator.prepare(), &vk.g2_elements[0].prepare()),
166+
(&pair_with_x.prepare(), &vk.g2_elements[1].prepare()),
167+
]))
168+
.ok_or(SynthesisError::Unsatisfiable)?
169+
== <Bn256 as Engine>::Fqk::one();
170+
171+
Ok(valid)
172+
}
173+
138174
// verify a recursive proof by using a corresponding verification key
139175
pub fn verify(
140176
vk: VerificationKey<Bn256, RecursiveAggregationCircuitBn256>,
@@ -145,7 +181,15 @@ pub fn verify(
145181
inputs.push(chunk);
146182
}
147183
log::info!("individual_inputs: {:#?}", inputs);
148-
core_verify::<_, _, RollingKeccakTranscript<<Bn256 as ScalarEngine>::Fr>>(&vk, &aggregated_proof.proof, None)
184+
//notice in PlonkCore.sol the aggregate pairs from subproofs and recursive proofs are combined: 1 * inner + challenge * outer
185+
//and only one verify on pairing has been run to save some gas
186+
//here we just verify them respectively
187+
let valid = core_verify::<_, _, RollingKeccakTranscript<<Bn256 as ScalarEngine>::Fr>>(&vk, &aggregated_proof.proof, None)?;
188+
if !valid {
189+
return Ok(valid);
190+
}
191+
log::info!("aggregated proof is valid");
192+
verify_subproof_limbs(&aggregated_proof, &vk)
149193
}
150194

151195
// export a verification key for a recursion circuit

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::fs;
22

3+
use crate::bellman_ce::bn256::Bn256;
34
use crate::circom_circuit::CircomCircuit;
45
use crate::{plonk, reader};
5-
use bellman_ce::bn256::Bn256;
66

77
const CIRCUIT_FILE: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/test/circuits/simple/circuit.r1cs.json");
88
const WITNESS_FILE: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/test/circuits/simple/witness.json");

0 commit comments

Comments
 (0)