Skip to content

Commit

Permalink
Split collect_queries function
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Jan 16, 2024
1 parent c71fbfa commit 20c16dd
Showing 1 changed file with 117 additions and 94 deletions.
211 changes: 117 additions & 94 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,77 @@ pub struct ConstraintSystemV2Backend<F: Field> {
pub(crate) general_column_annotations: HashMap<metadata::Column, String>,
}

impl<F: Field> Into<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> {

Check warning on line 1812 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> halo2_proofs/src/plonk/circuit.rs:1812:1 | 1812 | impl<F: Field> Into<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: replace the `Into` implentation with `From<plonk::circuit::ConstraintSystem<F>>` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into

Check warning on line 1812 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> halo2_proofs/src/plonk/circuit.rs:1812:1 | 1812 | impl<F: Field> Into<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: replace the `Into` implentation with `From<plonk::circuit::ConstraintSystem<F>>` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into

Check failure on line 1812 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> halo2_proofs/src/plonk/circuit.rs:1812:1 | 1812 | impl<F: Field> Into<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: replace the `Into` implentation with `From<plonk::circuit::ConstraintSystem<F>>` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into

Check failure on line 1812 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

error: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> halo2_proofs/src/plonk/circuit.rs:1812:1 | 1812 | impl<F: Field> Into<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: replace the `Into` implentation with `From<plonk::circuit::ConstraintSystem<F>>` = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into
fn into(self) -> ConstraintSystemV2Backend<F> {
ConstraintSystemV2Backend {
num_fixed_columns: self.num_fixed_columns,
num_advice_columns: self.num_advice_columns,
num_instance_columns: self.num_instance_columns,
num_challenges: self.num_challenges,
unblinded_advice_columns: self.unblinded_advice_columns.clone(),
advice_column_phase: self.advice_column_phase.iter().map(|p| p.0).collect(),
challenge_phase: self.challenge_phase.iter().map(|p| p.0).collect(),
gates: self
.gates
.iter()
.map(|g| {
g.polys.clone().into_iter().enumerate().map(|(i, e)| {
let name = match g.constraint_name(i) {
"" => g.name.clone(),
constraint_name => format!("{}:{}", g.name, constraint_name),
};
GateV2Backend {
name,
poly: e.into(),
}
})
})
.flatten()

Check warning on line 1837 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

called `map(..).flatten()` on `Iterator`

warning: called `map(..).flatten()` on `Iterator` --> halo2_proofs/src/plonk/circuit.rs:1825:18 | 1825 | .map(|g| { | __________________^ 1826 | | g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 | | let name = match g.constraint_name(i) { 1828 | | "" => g.name.clone(), ... | 1836 | | }) 1837 | | .flatten() | |__________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-W clippy::map-flatten` implied by `-W clippy::all` help: try replacing `map` with `flat_map` and remove the `.flatten()` | 1825 ~ .flat_map(|g| { 1826 + g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 + let name = match g.constraint_name(i) { 1828 + "" => g.name.clone(), 1829 + constraint_name => format!("{}:{}", g.name, constraint_name), 1830 + }; 1831 + GateV2Backend { 1832 + name, 1833 + poly: e.into(), 1834 + } 1835 + }) 1836 + }) |

Check warning on line 1837 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

called `map(..).flatten()` on `Iterator`

warning: called `map(..).flatten()` on `Iterator` --> halo2_proofs/src/plonk/circuit.rs:1825:18 | 1825 | .map(|g| { | __________________^ 1826 | | g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 | | let name = match g.constraint_name(i) { 1828 | | "" => g.name.clone(), ... | 1836 | | }) 1837 | | .flatten() | |__________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-W clippy::map-flatten` implied by `-W clippy::all` help: try replacing `map` with `flat_map` and remove the `.flatten()` | 1825 ~ .flat_map(|g| { 1826 + g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 + let name = match g.constraint_name(i) { 1828 + "" => g.name.clone(), 1829 + constraint_name => format!("{}:{}", g.name, constraint_name), 1830 + }; 1831 + GateV2Backend { 1832 + name, 1833 + poly: e.into(), 1834 + } 1835 + }) 1836 + }) |

Check failure on line 1837 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

called `map(..).flatten()` on `Iterator`

error: called `map(..).flatten()` on `Iterator` --> halo2_proofs/src/plonk/circuit.rs:1825:18 | 1825 | .map(|g| { | __________________^ 1826 | | g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 | | let name = match g.constraint_name(i) { 1828 | | "" => g.name.clone(), ... | 1836 | | }) 1837 | | .flatten() | |__________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-D clippy::map-flatten` implied by `-D warnings` help: try replacing `map` with `flat_map` and remove the `.flatten()` | 1825 ~ .flat_map(|g| { 1826 + g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 + let name = match g.constraint_name(i) { 1828 + "" => g.name.clone(), 1829 + constraint_name => format!("{}:{}", g.name, constraint_name), 1830 + }; 1831 + GateV2Backend { 1832 + name, 1833 + poly: e.into(), 1834 + } 1835 + }) 1836 + }) |

Check failure on line 1837 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

called `map(..).flatten()` on `Iterator`

error: called `map(..).flatten()` on `Iterator` --> halo2_proofs/src/plonk/circuit.rs:1825:18 | 1825 | .map(|g| { | __________________^ 1826 | | g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 | | let name = match g.constraint_name(i) { 1828 | | "" => g.name.clone(), ... | 1836 | | }) 1837 | | .flatten() | |__________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-D clippy::map-flatten` implied by `-D warnings` help: try replacing `map` with `flat_map` and remove the `.flatten()` | 1825 ~ .flat_map(|g| { 1826 + g.polys.clone().into_iter().enumerate().map(|(i, e)| { 1827 + let name = match g.constraint_name(i) { 1828 + "" => g.name.clone(), 1829 + constraint_name => format!("{}:{}", g.name, constraint_name), 1830 + }; 1831 + GateV2Backend { 1832 + name, 1833 + poly: e.into(), 1834 + } 1835 + }) 1836 + }) |
.collect(),
permutation: self.permutation.clone(),
lookups: self
.lookups
.iter()
.map(|l| lookup::ArgumentV2 {
name: l.name.clone(),
input_expressions: l
.input_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
table_expressions: l
.table_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
})
.collect(),
shuffles: self
.shuffles
.iter()
.map(|s| shuffle::ArgumentV2 {
name: s.name.clone(),
input_expressions: s
.input_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
shuffle_expressions: s
.shuffle_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
})
.collect(),
general_column_annotations: self.general_column_annotations.clone(),
}
}
}

/// Witness calculator. Frontend function
#[derive(Debug)]
pub struct WitnessCalculator<'a, F: Field, ConcreteCircuit: Circuit<F>> {
Expand Down Expand Up @@ -1974,81 +2045,14 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
let mut fixed: Vec<_> = fixed.into_iter().map(|p| p.values).collect();
fixed.extend(selector_polys.into_iter());

let cs2 = ConstraintSystemV2Backend {
num_fixed_columns: cs.num_fixed_columns,
num_advice_columns: cs.num_advice_columns,
num_instance_columns: cs.num_instance_columns,
num_challenges: cs.num_challenges,
unblinded_advice_columns: cs.unblinded_advice_columns.clone(),
advice_column_phase: cs.advice_column_phase.iter().map(|p| p.0).collect(),
challenge_phase: cs.challenge_phase.iter().map(|p| p.0).collect(),
// TODO: Clean up all the Expression -> Expression conversions
gates: cs
.gates
.iter()
.map(|g| {
g.polys.clone().into_iter().enumerate().map(|(i, e)| {
let name = match g.constraint_name(i) {
"" => g.name.clone(),
constraint_name => format!("{}:{}", g.name, constraint_name),
};
GateV2Backend {
name,
poly: e.into(),
}
})
})
.flatten()
.collect(),
permutation: cs.permutation.clone(),
lookups: cs
.lookups
.iter()
.map(|l| lookup::ArgumentV2 {
name: l.name.clone(),
input_expressions: l
.input_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
table_expressions: l
.table_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
})
.collect(),
shuffles: cs
.shuffles
.iter()
.map(|s| shuffle::ArgumentV2 {
name: s.name.clone(),
input_expressions: s
.input_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
shuffle_expressions: s
.shuffle_expressions
.clone()
.into_iter()
.map(|e| e.into())
.collect(),
})
.collect(),
general_column_annotations: cs.general_column_annotations.clone(),
};
let preprocessing = PreprocessingV2 {
permutation: assembly.permutation,
fixed,
};

Ok((
CompiledCircuitV2 {
cs: cs2,
cs: cs.clone().into(),
preprocessing,
},
config,
Expand All @@ -2057,25 +2061,10 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
}

impl<F: Field> ConstraintSystemV2Backend<F> {
pub(crate) fn collect_queries(
&self,
) -> (
Queries,
Vec<Gate<F>>,
Vec<lookup::Argument<F>>,
Vec<shuffle::Argument<F>>,
) {
let mut queries = QueriesMap {
advice_map: HashMap::new(),
instance_map: HashMap::new(),
fixed_map: HashMap::new(),
advice: Vec::new(),
instance: Vec::new(),
fixed: Vec::new(),
};

let gates: Vec<_> = self
.gates
/// Collect queries used in gates while mapping those gates to equivalent ones with indexed
/// query references in the expressions.
fn collect_queries_gates(&self, queries: &mut QueriesMap) -> Vec<Gate<F>> {
self.gates
.iter()
.map(|gate| Gate {
name: gate.name.clone(),
Expand All @@ -2084,9 +2073,13 @@ impl<F: Field> ConstraintSystemV2Backend<F> {
queried_selectors: Vec::new(), // Unused?
queried_cells: Vec::new(), // Unused?
})
.collect();
let lookups: Vec<_> = self
.lookups
.collect()
}

/// Collect queries used in lookups while mapping those lookups to equivalent ones with indexed
/// query references in the expressions.
fn collect_queries_lookups(&self, queries: &mut QueriesMap) -> Vec<lookup::Argument<F>> {
self.lookups
.iter()
.map(|lookup| lookup::Argument {
name: lookup.name.clone(),
Expand All @@ -2101,9 +2094,13 @@ impl<F: Field> ConstraintSystemV2Backend<F> {
.map(|e| queries.as_expression(e))
.collect(),
})
.collect();
let shuffles: Vec<_> = self
.shuffles
.collect()
}

/// Collect queries used in shuffles while mapping those lookups to equivalent ones with indexed
/// query references in the expressions.
fn collect_queries_shuffles(&self, queries: &mut QueriesMap) -> Vec<shuffle::Argument<F>> {
self.shuffles
.iter()
.map(|shuffle| shuffle::Argument {
name: shuffle.name.clone(),
Expand All @@ -2118,8 +2115,34 @@ impl<F: Field> ConstraintSystemV2Backend<F> {
.map(|e| queries.as_expression(e))
.collect(),
})
.collect();
.collect()
}

/// Collect all queries used in the expressions of gates, lookups and shuffles. Map the
/// expressions of gates, lookups and shuffles into equivalent ones with indexed query
/// references.
pub(crate) fn collect_queries(
&self,
) -> (
Queries,
Vec<Gate<F>>,
Vec<lookup::Argument<F>>,
Vec<shuffle::Argument<F>>,
) {

Check warning on line 2131 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2126:10 | 2126 | ) -> ( | __________^ 2127 | | Queries, 2128 | | Vec<Gate<F>>, 2129 | | Vec<lookup::Argument<F>>, 2130 | | Vec<shuffle::Argument<F>>, 2131 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

Check warning on line 2131 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2126:10 | 2126 | ) -> ( | __________^ 2127 | | Queries, 2128 | | Vec<Gate<F>>, 2129 | | Vec<lookup::Argument<F>>, 2130 | | Vec<shuffle::Argument<F>>, 2131 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

Check failure on line 2131 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

very complex type used. Consider factoring parts into `type` definitions

error: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2126:10 | 2126 | ) -> ( | __________^ 2127 | | Queries, 2128 | | Vec<Gate<F>>, 2129 | | Vec<lookup::Argument<F>>, 2130 | | Vec<shuffle::Argument<F>>, 2131 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

Check failure on line 2131 in halo2_proofs/src/plonk/circuit.rs

View workflow job for this annotation

GitHub Actions / Clippy (1.56.1)

very complex type used. Consider factoring parts into `type` definitions

error: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2126:10 | 2126 | ) -> ( | __________^ 2127 | | Queries, 2128 | | Vec<Gate<F>>, 2129 | | Vec<lookup::Argument<F>>, 2130 | | Vec<shuffle::Argument<F>>, 2131 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
let mut queries = QueriesMap {
advice_map: HashMap::new(),
instance_map: HashMap::new(),
fixed_map: HashMap::new(),
advice: Vec::new(),
instance: Vec::new(),
fixed: Vec::new(),
};

let gates = self.collect_queries_gates(&mut queries);
let lookups = self.collect_queries_lookups(&mut queries);
let shuffles = self.collect_queries_shuffles(&mut queries);

// Each column used in a copy constraint involves a query at rotation current.
for column in self.permutation.get_columns() {
match column.column_type {
Any::Instance => {
Expand Down

0 comments on commit 20c16dd

Please sign in to comment.