Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make selector polynomials optional #11

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1553,7 +1553,7 @@
/// TODO doc
#[derive(Debug, Clone)]
pub struct LookupTracker<F: Field> {
pub(crate) table: Vec<Expression<F>>,

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

View workflow job for this annotation

GitHub Actions / Bitrot check

fields `table` and `inputs` are never read
pub(crate) inputs: Vec<Vec<Expression<F>>>,
}

Expand Down Expand Up @@ -2127,7 +2127,11 @@
/// 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 @@
rotation: Rotation::cur(),
})
},
return_polys,
);

let mut selector_map = vec![None; selector_assignment.len()];
Expand All @@ -2197,6 +2202,7 @@
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 @@
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
Loading