Skip to content

Commit

Permalink
feat: make selector polynomials optional
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto committed Mar 21, 2024
1 parent 4d7e6dd commit cc5f574
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ impl<F: FromUniformBytes<64> + Ord> MockProver<F> {
)?;
}

let (cs, selector_polys) = prover.cs.compress_selectors(prover.selectors.clone());
let (cs, selector_polys) = prover.cs.compress_selectors(prover.selectors.clone(), true);
prover.cs = cs;
prover.fixed.extend(selector_polys.into_iter().map(|poly| {
let mut v = vec![CellValue::Unassigned; n];
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/dev/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<G: PrimeGroup, ConcreteCircuit: Circuit<G::Scalar>> CircuitCost<G, Concrete
cs.constants.clone(),
)
.unwrap();
let (cs, _) = cs.compress_selectors(layout.selectors);
let (cs, _) = cs.compress_selectors(layout.selectors, false);

assert!((1 << k) >= cs.minimum_rows());

Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ where
Ok(selector)
})
.collect::<io::Result<_>>()?;
let (cs, _) = cs.compress_selectors(selectors.clone());
let (cs, _) = cs.compress_selectors(selectors.clone(), false);
(cs, selectors)
} else {
// we still need to replace selectors with fixed Expressions in `cs`
let fake_selectors = vec![vec![]; cs.num_selectors];
let (cs, _) = cs.directly_convert_selectors_to_fixed(fake_selectors);
let (cs, _) = cs.directly_convert_selectors_to_fixed(fake_selectors, false);
(cs, vec![])
};

Expand Down
20 changes: 15 additions & 5 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2127,7 +2127,11 @@ impl<F: Field> ConstraintSystem<F> {
/// find which fixed column corresponds with a given `Selector`.
///
/// Do not call this twice. Yes, this should be a builder pattern instead.
pub fn compress_selectors(mut self, selectors: Vec<Vec<bool>>) -> (Self, Vec<Vec<F>>) {
pub fn compress_selectors(
mut self,
selectors: Vec<Vec<bool>>,
return_polys: bool,
) -> (Self, Vec<Vec<F>>) {
// The number of provided selector assignments must be the number we
// counted for this constraint system.
assert_eq!(selectors.len(), self.num_selectors);
Expand Down Expand Up @@ -2171,6 +2175,7 @@ impl<F: Field> ConstraintSystem<F> {
rotation: Rotation::cur(),
})
},
return_polys,
);

let mut selector_map = vec![None; selector_assignment.len()];
Expand All @@ -2197,6 +2202,7 @@ impl<F: Field> ConstraintSystem<F> {
pub fn directly_convert_selectors_to_fixed(
mut self,
selectors: Vec<Vec<bool>>,
return_polys: bool,
) -> (Self, Vec<Vec<F>>) {
// The number of provided selector assignments must be the number we
// counted for this constraint system.
Expand All @@ -2205,10 +2211,14 @@ impl<F: Field> ConstraintSystem<F> {
let (polys, selector_replacements): (Vec<_>, Vec<_>) = selectors
.into_iter()
.map(|selector| {
let poly = selector
.iter()
.map(|b| if *b { F::ONE } else { F::ZERO })
.collect::<Vec<_>>();
let poly = if return_polys {
selector
.iter()
.map(|b| if *b { F::ONE } else { F::ZERO })
.collect::<Vec<_>>()
} else {
vec![]
};
let column = self.fixed_column();
let rotation = Rotation::cur();
let expr = Expression::Fixed(FixedQuery {
Expand Down
24 changes: 17 additions & 7 deletions halo2_proofs/src/plonk/circuit/compress_selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn process<F: Field, E>(
mut selectors: Vec<SelectorDescription>,
max_degree: usize,
mut allocate_fixed_column: E,
return_polys: bool,
) -> (Vec<Vec<F>>, Vec<SelectorAssignment<F>>)
where
E: FnMut() -> Expression<F>,
Expand All @@ -76,11 +77,16 @@ where
// gate constraint.
let expression = allocate_fixed_column();

let combination_assignment = selector
.activations
.iter()
.map(|b| if *b { F::ONE } else { F::ZERO })
.collect::<Vec<_>>();
let combination_assignment = if return_polys {
selector
.activations
.iter()
.map(|b| if *b { F::ONE } else { F::ZERO })
.collect::<Vec<_>>()
} else {
vec![]
};

let combination_index = combination_assignments.len();
combination_assignments.push(combination_assignment);
selector_assignments.push(SelectorAssignment {
Expand Down Expand Up @@ -220,7 +226,11 @@ where
expression,
}
}));
combination_assignments.push(combination_assignment);
if return_polys {
combination_assignments.push(combination_assignment);
} else {
combination_assignments.push(vec![]);
}
}

(combination_assignments, selector_assignments)
Expand Down Expand Up @@ -287,7 +297,7 @@ mod tests {
});
query += 1;
tmp
});
}, true);

{
let mut selectors_seen = vec![];
Expand Down
8 changes: 4 additions & 4 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ where

let mut fixed = batch_invert_assigned(assembly.fixed);
let (cs, selector_polys) = if compress_selectors {
cs.compress_selectors(assembly.selectors.clone())
cs.compress_selectors(assembly.selectors.clone(), true)
} else {
// After this, the ConstraintSystem should not have any selectors: `verify` does not need them, and `keygen_pk` regenerates `cs` from scratch anyways.
let selectors = std::mem::take(&mut assembly.selectors);
cs.directly_convert_selectors_to_fixed(selectors)
cs.directly_convert_selectors_to_fixed(selectors, true)
};
fixed.extend(
selector_polys
Expand Down Expand Up @@ -338,9 +338,9 @@ where

let mut fixed = batch_invert_assigned(assembly.fixed);
let (cs, selector_polys) = if vk.compress_selectors {
cs.compress_selectors(assembly.selectors)
cs.compress_selectors(assembly.selectors, true)
} else {
cs.directly_convert_selectors_to_fixed(assembly.selectors)
cs.directly_convert_selectors_to_fixed(assembly.selectors, true)
};
fixed.extend(
selector_polys
Expand Down

0 comments on commit cc5f574

Please sign in to comment.