Skip to content

Commit

Permalink
feat: make selector polynomials optional (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto authored Mar 22, 2024
1 parent 4d7e6dd commit 30f6fbf
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 50 deletions.
2 changes: 1 addition & 1 deletion halo2_gadgets/benches/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
let message_word = |i: usize| {
let value = self.message.map(|message_vals| message_vals[i]);
region.assign_advice(
|| format!("load message_{}", i),
|| format!("load message_{i}"),
config.input[i],
0,
|| value,
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<
.enumerate()
{
self.sponge
.absorb(layouter.namespace(|| format!("absorb_{}", i)), value)?;
.absorb(layouter.namespace(|| format!("absorb_{i}")), value)?;
}
self.sponge
.finish_absorbing(layouter.namespace(|| "finish absorbing"))?
Expand Down
24 changes: 12 additions & 12 deletions halo2_gadgets/src/poseidon/pow5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ impl<
let mut state = Vec::with_capacity(WIDTH);
let mut load_state_word = |i: usize, value: F| -> Result<_, Error> {
let var = region.assign_advice_from_constant(
|| format!("state_{}", i),
|| format!("state_{i}"),
config.state[i],
0,
value,
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<
initial_state[i]
.0
.copy_advice(
|| format!("load state_{}", i),
|| format!("load state_{i}"),
&mut region,
config.state[i],
0,
Expand All @@ -341,7 +341,7 @@ impl<
let constraint_var = match input.0[i].clone() {
Some(PaddedWord::Message(word)) => word,
Some(PaddedWord::Padding(padding_value)) => region.assign_fixed(
|| format!("load pad_{}", i),
|| format!("load pad_{i}"),
config.rc_b[i],
1,
|| Value::known(padding_value),
Expand All @@ -350,7 +350,7 @@ impl<
};
constraint_var
.copy_advice(
|| format!("load input_{}", i),
|| format!("load input_{i}"),
&mut region,
config.state[i],
1,
Expand All @@ -370,7 +370,7 @@ impl<
.unwrap_or_else(|| Value::known(F::ZERO));
region
.assign_advice(
|| format!("load output_{}", i),
|| format!("load output_{i}"),
config.state[i],
2,
|| value,
Expand Down Expand Up @@ -474,7 +474,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
});

region.assign_advice(
|| format!("round_{} partial_sbox", round),
|| format!("round_{round} partial_sbox"),
config.partial_sbox,
offset,
|| r.as_ref().map(|r| r[0]),
Expand Down Expand Up @@ -536,7 +536,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
let load_state_word = |i: usize| {
initial_state[i]
.0
.copy_advice(|| format!("load state_{}", i), region, config.state[i], 0)
.copy_advice(|| format!("load state_{i}"), region, config.state[i], 0)
.map(StateWord)
};

Expand All @@ -558,7 +558,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
// Load the round constants.
let mut load_round_constant = |i: usize| {
region.assign_fixed(
|| format!("round_{} rc_{}", round, i),
|| format!("round_{round} rc_{i}"),
config.rc_a[i],
offset,
|| Value::known(config.round_constants[round][i]),
Expand All @@ -574,7 +574,7 @@ impl<F: Field, const WIDTH: usize> Pow5State<F, WIDTH> {
let next_state_word = |i: usize| {
let value = next_state[i];
let var = region.assign_advice(
|| format!("round_{} state_{}", next_round, i),
|| format!("round_{next_round} state_{i}"),
config.state[i],
offset + 1,
|| value,
Expand Down Expand Up @@ -649,7 +649,7 @@ mod tests {
let state_word = |i: usize| {
let value = Value::known(Fp::from(i as u64));
let var = region.assign_advice(
|| format!("load state_{}", i),
|| format!("load state_{i}"),
config.state[i],
0,
|| value,
Expand Down Expand Up @@ -688,7 +688,7 @@ mod tests {
|mut region| {
let mut final_state_word = |i: usize| {
let var = region.assign_advice(
|| format!("load final_state_{}", i),
|| format!("load final_state_{i}"),
config.state[i],
0,
|| Value::known(expected_final_state[i]),
Expand Down Expand Up @@ -774,7 +774,7 @@ mod tests {
let message_word = |i: usize| {
let value = self.message.map(|message_vals| message_vals[i]);
region.assign_advice(
|| format!("load message_{}", i),
|| format!("load message_{i}"),
config.state[i],
0,
|| value,
Expand Down
2 changes: 1 addition & 1 deletion halo2_gadgets/src/poseidon/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ impl<F: PrimeField, const RATE: usize, const L: usize> Domain<F, RATE> for Const
type Padding = iter::Take<iter::Repeat<F>>;

fn name() -> String {
format!("ConstantLength<{}>", L)
format!("ConstantLength<{L}>")
}

fn initial_capacity_element() -> F {
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/benches/lookups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn criterion_benchmark(c: &mut Criterion) {
|mut table| {
for row in 0u64..(1 << 8) {
table.assign_cell(
|| format!("row {}", row),
|| format!("row {row}"),
config.table,
row as usize,
|| Value::known(F::from(row)),
Expand All @@ -131,7 +131,7 @@ fn criterion_benchmark(c: &mut Criterion) {
for offset in 0u64..(1 << 10) {
config.selector.enable(&mut region, offset as usize)?;
region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.advice,
offset as usize,
|| Value::known(F::from(offset % 256)),
Expand All @@ -140,7 +140,7 @@ fn criterion_benchmark(c: &mut Criterion) {
for offset in 1u64..(1 << 10) {
config.selector.enable(&mut region, offset as usize)?;
region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.other_advice,
offset as usize - 1,
|| Value::known(F::from(offset % 256)),
Expand Down
12 changes: 6 additions & 6 deletions halo2_proofs/examples/simple-lookup-unblinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn main() {

let diff = lhs - rhs;

let constraint = diff.clone() * (Expression::Constant(F::ZERO) - diff.clone());
let constraint = diff.clone() * (Expression::Constant(F::ZERO) - diff);

Constraints::with_selector(s_mul, vec![constraint])
});
Expand All @@ -118,14 +118,14 @@ fn main() {
|mut table| {
for row in 0u64..2_u64.pow(K - 1) {
table.assign_cell(
|| format!("input row {}", row),
|| format!("input row {row}"),
config.table_input,
row as usize,
|| Value::known(F::from(row)),
)?;
// table output (2x the input) -- yeehaw
table.assign_cell(
|| format!("output row {}", row),
|| format!("output row {row}"),
config.table_output,
row as usize,
|| Value::known(F::from(2 * row)),
Expand All @@ -144,14 +144,14 @@ fn main() {
config.qlookup.enable(&mut region, offset as usize)?;
// input
region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.advice,
offset as usize,
|| Value::known(F::from(offset)),
)?;
// 2x
let cell = region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.other_advice,
offset as usize,
|| Value::known(F::from(2 * offset)),
Expand Down Expand Up @@ -247,7 +247,7 @@ fn main() {

env_logger::init();

println!("k = {}", K);
println!("k = {K}");
// time it
println!("keygen");
let start = std::time::Instant::now();
Expand Down
12 changes: 6 additions & 6 deletions halo2_proofs/examples/simple-lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn main() {

let diff = lhs - rhs;

let constraint = diff.clone() * (Expression::Constant(F::ZERO) - diff.clone());
let constraint = diff.clone() * (Expression::Constant(F::ZERO) - diff);

Constraints::with_selector(s_mul, vec![constraint])
});
Expand All @@ -118,14 +118,14 @@ fn main() {
|mut table| {
for row in 0u64..2_u64.pow(K - 1) {
table.assign_cell(
|| format!("input row {}", row),
|| format!("input row {row}"),
config.table_input,
row as usize,
|| Value::known(F::from(row)),
)?;
// table output (2x the input) -- yeehaw
table.assign_cell(
|| format!("output row {}", row),
|| format!("output row {row}"),
config.table_output,
row as usize,
|| Value::known(F::from(2 * row)),
Expand All @@ -144,14 +144,14 @@ fn main() {
config.qlookup.enable(&mut region, offset as usize)?;
// input
region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.advice,
offset as usize,
|| Value::known(F::from(offset)),
)?;
// 2x
let cell = region.assign_advice(
|| format!("offset {}", offset),
|| format!("offset {offset}"),
config.other_advice,
offset as usize,
|| Value::known(F::from(2 * offset)),
Expand Down Expand Up @@ -247,7 +247,7 @@ fn main() {

env_logger::init();

println!("k = {}", K);
println!("k = {K}");
// time it
println!("keygen");
let start = std::time::Instant::now();
Expand Down
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
Loading

0 comments on commit 30f6fbf

Please sign in to comment.