Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit d87fe2c

Browse files
committed
fmt and inline doc
1 parent 1211b4a commit d87fe2c

File tree

6 files changed

+119
-134
lines changed

6 files changed

+119
-134
lines changed

aggregator/src/recursion.rs

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,65 +44,70 @@ pub trait StateTransition: Sized {
4444
type Input: Clone;
4545
type Circuit: CircuitExt<Fr>;
4646

47+
/// Initialise a new type that implements the state transition behaviour.
4748
fn new(state: Self::Input) -> Self;
4849

50+
/// Transition to the next state.
4951
fn state_transition(&self, round: usize) -> Self::Input;
5052

51-
// the number of fields should be used for state transition
52-
// notice the pi take 2 times of the returned number (before -> after)
53+
/// Returns the number of fields used to represent state. The public input consists of twice
54+
/// this number as both the previous and current states are included in the public input.
5355
fn num_transition_instance() -> usize;
5456

55-
// in case the circuit still require more PI followed by
56-
// state transition
57+
/// Returns the number of fields required by the circuit in addition to the fields to represent
58+
/// its state.
5759
fn num_additional_instance() -> usize {
5860
0
5961
}
6062

63+
/// The number of instance cells for the circuit.
6164
fn num_instance() -> usize {
62-
Self::num_transition_instance() * 2
63-
+ Self::num_additional_instance()
64-
+ Self::Circuit::accumulator_indices()
65-
.map(|v|v.len()).unwrap_or_default()
65+
Self::num_accumulator_instance()
66+
+ Self::num_transition_instance() * 2
67+
+ Self::num_additional_instance()
6668
}
6769

70+
/// Returns the number of instance cells used to hold the accumulator.
6871
fn num_accumulator_instance() -> usize {
6972
Self::Circuit::accumulator_indices()
70-
.map(|v|v.len()).unwrap_or_default()
73+
.map(|v| v.len())
74+
.unwrap_or_default()
7175
}
7276

7377
/// Following is the indices of the layout of instance
7478
/// for StateTransition circuit, the default suppose
75-
/// single col of instance, and the layout is
76-
/// accmulator | prev_state | state | additional
77-
///
79+
/// single col of instance, and the layout is:
80+
/// accumulator | prev_state | state | additional
81+
///
7882
/// Notice we do not verify the layout of accumulator
7983
/// simply suppose they are put in the beginning
8084
fn accumulator_indices() -> Vec<usize> {
81-
(0..Self::num_accumulator_instance()).collect()
85+
let start = 0;
86+
let end = Self::num_accumulator_instance();
87+
(start..end).collect()
8288
}
8389

84-
fn state_indices() -> Vec<usize> {
85-
(
86-
Self::num_accumulator_instance() + Self::num_transition_instance()..
87-
Self::num_accumulator_instance() + Self::num_transition_instance()*2
88-
).collect()
90+
/// The accumulator is followed by the instance cells representing the previous state.
91+
fn state_prev_indices() -> Vec<usize> {
92+
let start = Self::num_accumulator_instance();
93+
let end = start + Self::num_transition_instance();
94+
(start..end).collect()
8995
}
9096

91-
fn state_prev_indices() -> Vec<usize> {
92-
(
93-
Self::num_accumulator_instance() ..
94-
Self::num_accumulator_instance() + Self::num_transition_instance()
95-
).collect()
97+
/// The previous state is followed by the instance cells representing the current state.
98+
fn state_indices() -> Vec<usize> {
99+
let start = Self::num_accumulator_instance() + Self::num_transition_instance();
100+
let end = start + Self::num_transition_instance();
101+
(start..end).collect()
96102
}
97103

98-
/// The indices of the accumulator
104+
/// The indices of any other instances cells in addition to the accumulator and state
105+
/// transition cells.
99106
fn additional_indices() -> Vec<usize> {
100-
(
101-
Self::num_accumulator_instance() + Self::num_transition_instance()*2..
102-
Self::num_instance()
103-
).collect()
107+
let start = Self::num_accumulator_instance() + 2 * Self::num_transition_instance();
108+
let end = Self::num_instance();
109+
(start..end).collect()
104110
}
105-
106111
}
107112

108113
pub use circuit::RecursionCircuit;

aggregator/src/recursion/circuit.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::*;
44
use crate::param::ConfigParams as BatchCircuitConfigParams;
55
use halo2_proofs::{
6-
circuit::{Layouter, SimpleFloorPlanner, Value},
6+
circuit::{Cell, Layouter, SimpleFloorPlanner, Value},
77
poly::{commitment::ParamsProver, kzg::commitment::ParamsKZG},
88
};
99
use snark_verifier::{
@@ -26,9 +26,9 @@ use sv_halo2_base::{
2626
gates::GateInstructions, halo2_proofs, AssignedValue, Context, ContextParams,
2727
QuantumCell::Existing,
2828
};
29+
2930
type Svk = KzgSuccinctVerifyingKey<G1Affine>;
3031
type Pcs = Kzg<Bn256, Bdfg21>;
31-
3232
type As = KzgAs<Pcs>;
3333

3434
fn select_accumulator<'a>(
@@ -80,8 +80,6 @@ pub struct RecursionCircuit<ST> {
8080
impl<ST: StateTransition> RecursionCircuit<ST> {
8181
const PREPROCESSED_DIGEST_ROW: usize = 4 * LIMBS;
8282
const INITIAL_STATE_ROW: usize = Self::PREPROCESSED_DIGEST_ROW + 1;
83-
//const STATE_ROW: usize = Self::INITIAL_STATE_ROW + ST;
84-
//const ROUND_ROW: usize = Self::STATE_ROW + ST;
8583

8684
pub fn new(
8785
params: &ParamsKZG<Bn256>,
@@ -90,7 +88,6 @@ impl<ST: StateTransition> RecursionCircuit<ST> {
9088
rng: impl Rng + Send,
9189
round: usize,
9290
) -> Self {
93-
9491
let svk = params.get_g()[0].into();
9592
let default_accumulator = KzgAccumulator::new(params.get_g()[1], params.get_g()[0]);
9693

@@ -122,20 +119,26 @@ impl<ST: StateTransition> RecursionCircuit<ST> {
122119

123120
let init_instances = if round > 0 {
124121
// pick from prev snark
125-
Vec::from(&previous.instances[0][
126-
Self::INITIAL_STATE_ROW..Self::INITIAL_STATE_ROW+ST::num_transition_instance()
127-
])
122+
Vec::from(
123+
&previous.instances[0][Self::INITIAL_STATE_ROW
124+
..Self::INITIAL_STATE_ROW + ST::num_transition_instance()],
125+
)
128126
} else {
129127
// pick from app
130128
ST::state_prev_indices()
131-
.into_iter().map(|i|app.instances[0][i])
132-
.collect::<Vec<_>>()
129+
.into_iter()
130+
.map(|i| app.instances[0][i])
131+
.collect::<Vec<_>>()
133132
};
134133

135134
let state_instances = ST::state_indices()
136-
.into_iter().map(|i|&app.instances[0][i])
137-
.chain(ST::additional_indices().into_iter()
138-
.map(|i|&app.instances[0][i]));
135+
.into_iter()
136+
.map(|i| &app.instances[0][i])
137+
.chain(
138+
ST::additional_indices()
139+
.into_iter()
140+
.map(|i| &app.instances[0][i]),
141+
);
139142

140143
let preprocessed_digest = {
141144
let inputs = previous
@@ -150,6 +153,7 @@ impl<ST: StateTransition> RecursionCircuit<ST> {
150153
hasher.update(&inputs);
151154
hasher.squeeze()
152155
};
156+
153157
let instances = [
154158
accumulator.lhs.x,
155159
accumulator.lhs.y,
@@ -179,17 +183,6 @@ impl<ST: StateTransition> RecursionCircuit<ST> {
179183
}
180184
}
181185

182-
// fn initial_snark(params: &ParamsKZG<Bn256>, vk: Option<&VerifyingKey<G1Affine>>) -> Snark {
183-
// let mut snark = gen_dummy_snark::<RecursionCircuit>(params, vk);
184-
// let g = params.get_g();
185-
// snark.instances = vec![[g[1].x, g[1].y, g[0].x, g[0].y]
186-
// .into_iter()
187-
// .flat_map(fe_to_limbs::<_, _, LIMBS, BITS>)
188-
// .chain([Fr::ZERO; 4])
189-
// .collect_vec()];
190-
// snark
191-
// }
192-
193186
fn as_proof(&self) -> Value<&[u8]> {
194187
self.as_proof.as_ref().map(Vec::as_slice)
195188
}
@@ -209,7 +202,7 @@ impl<ST: StateTransition> RecursionCircuit<ST> {
209202
Ok(KzgAccumulator::new(lhs, rhs))
210203
}
211204

212-
/// get the number of instance, help to refine the CircuitExt trait
205+
/// Returns the number of instance cells in the Recursion Circuit, help to refine the CircuitExt trait
213206
pub fn num_instance_fixed() -> usize {
214207
// [..lhs, ..rhs, preprocessed_digest, initial_state, state, round]
215208
4 * LIMBS + 2 * ST::num_transition_instance() + ST::num_additional_instance() + 2
@@ -255,13 +248,12 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
255248
let main_gate = config.gate();
256249

257250
let mut first_pass = halo2_base::SKIP_FIRST_PASS; // assume using simple floor planner
258-
let mut assigned_instances = Vec::new();
259-
layouter.assign_region(
251+
let assigned_instances = layouter.assign_region(
260252
|| "",
261-
|region| {
253+
|region| -> Result<Vec<Cell>, Error> {
262254
if first_pass {
263255
first_pass = false;
264-
return Ok(());
256+
return Ok(vec![]);
265257
}
266258
let mut ctx = Context::new(
267259
region,
@@ -324,14 +316,14 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
324316
Some(preprocessed_digest),
325317
);
326318

327-
let default_accmulator = self.load_default_accumulator(&loader)?;
319+
let default_accumulator = self.load_default_accumulator(&loader)?;
328320
let previous_accumulators = previous_accumulators
329321
.iter()
330322
.map(|previous_accumulator| {
331323
select_accumulator(
332324
&loader,
333325
&first_round,
334-
&default_accmulator,
326+
&default_accumulator,
335327
previous_accumulator,
336328
)
337329
})
@@ -353,7 +345,9 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
353345
.iter()
354346
.zip_eq(previous_instances[init_state_row_beg..state_row_beg].iter())
355347
.zip_eq(
356-
ST::state_prev_indices().into_iter().map(|i|&app_instances[i])
348+
ST::state_prev_indices()
349+
.into_iter()
350+
.map(|i| &app_instances[i]),
357351
)
358352
.flat_map(|((&st, &previous_st), &app_inst)| {
359353
[
@@ -375,10 +369,14 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
375369
let verify_app_state = state
376370
.iter()
377371
.zip_eq(
378-
ST::state_indices().into_iter().map(|i|&app_instances[i])
379-
.chain(
380-
ST::additional_indices().into_iter().map(|i|&app_instances[i])
381-
)
372+
ST::state_indices()
373+
.into_iter()
374+
.map(|i| &app_instances[i])
375+
.chain(
376+
ST::additional_indices()
377+
.into_iter()
378+
.map(|i| &app_instances[i]),
379+
),
382380
)
383381
.map(|(&st, &app_inst)| (st, app_inst))
384382
.collect::<Vec<_>>();
@@ -387,7 +385,9 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
387385
let verify_app_init_state = previous_instances[state_row_beg..addition_state_beg]
388386
.iter()
389387
.zip_eq(
390-
ST::state_prev_indices().into_iter().map(|i|&app_instances[i])
388+
ST::state_prev_indices()
389+
.into_iter()
390+
.map(|i| &app_instances[i]),
391391
)
392392
.map(|(&st, &app_inst)| {
393393
(
@@ -432,17 +432,15 @@ impl<ST: StateTransition> Circuit<Fr> for RecursionCircuit<ST> {
432432
#[cfg(feature = "display")]
433433
println!("Advice columns used: {}", ctx.advice_alloc[0][0].0 + 1);
434434

435-
assigned_instances.extend(
436-
[lhs.x(), lhs.y(), rhs.x(), rhs.y()]
437-
.into_iter()
438-
.flat_map(|coordinate| coordinate.limbs())
439-
.chain(iter::once(&preprocessed_digest))
440-
.chain(initial_state.iter())
441-
.chain(state.iter())
442-
.chain(iter::once(&round))
443-
.map(|assigned| assigned.cell()),
444-
);
445-
Ok(())
435+
Ok([lhs.x(), lhs.y(), rhs.x(), rhs.y()]
436+
.into_iter()
437+
.flat_map(|coordinate| coordinate.limbs())
438+
.chain(iter::once(&preprocessed_digest))
439+
.chain(initial_state.iter())
440+
.chain(state.iter())
441+
.chain(iter::once(&round))
442+
.map(|assigned| assigned.cell())
443+
.collect())
446444
},
447445
)?;
448446

aggregator/src/recursion/util.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ pub fn initial_recursion_snark<ST: StateTransition>(
158158
.chain(std::iter::repeat(Fr::ZERO))
159159
.take(RecursionCircuit::<ST>::num_instance_fixed())
160160
.collect_vec()];
161+
161162
snark
162163
}
163164

@@ -169,18 +170,12 @@ pub fn gen_recursion_pk<ST: StateTransition>(
169170
mut rng: impl Rng + Send,
170171
path: Option<&Path>,
171172
) -> ProvingKey<G1Affine> {
172-
173173
let app_snark =
174174
gen_dummy_snark::<ST::Circuit>(app_params, app_vk, &[ST::num_instance()], &mut rng);
175175

176176
let recursive_snark = initial_recursion_snark::<ST>(recursion_params, None, &mut rng);
177177

178-
let recursion = RecursionCircuit::<ST>::new(
179-
recursion_params,
180-
app_snark,
181-
recursive_snark,
182-
&mut rng,
183-
0,
184-
);
178+
let recursion =
179+
RecursionCircuit::<ST>::new(recursion_params, app_snark, recursive_snark, &mut rng, 0);
185180
gen_pk(recursion_params, &recursion, path)
186181
}

0 commit comments

Comments
 (0)