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

Improve halo2 query calls #154

Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fed2c5c
return expression from cell
CeciliaZ030 Feb 21, 2023
d0450f6
add example
CeciliaZ030 Feb 21, 2023
680a583
selector
CeciliaZ030 Feb 21, 2023
4ac383d
recurse Expression to fill in index
CeciliaZ030 Feb 22, 2023
fb4ad83
merge taiko latest main
CeciliaZ030 Feb 22, 2023
e8c79c9
minimized changes from the original
CeciliaZ030 Feb 23, 2023
9d3296a
backword compatible meta.query_X & challange.expr()
CeciliaZ030 Feb 23, 2023
1f4d270
cargo fmt
CeciliaZ030 Feb 23, 2023
c2c89ad
fixed lookup to pass all tests
CeciliaZ030 Feb 23, 2023
a403415
Update comments
CeciliaZ030 Feb 24, 2023
3a0922e
Update comments
CeciliaZ030 Feb 24, 2023
f7b73c7
Update comments
CeciliaZ030 Feb 24, 2023
85445df
Update comments
CeciliaZ030 Feb 24, 2023
ee56f9c
Update comments
CeciliaZ030 Feb 24, 2023
411595c
Update comments
CeciliaZ030 Feb 24, 2023
9771238
update
CeciliaZ030 Feb 24, 2023
6152966
add primitives.rs back
CeciliaZ030 Feb 24, 2023
4303039
remove example2
CeciliaZ030 Feb 24, 2023
7b7fb1a
backward compatible meta.query_X & Column.cur(), next(), prev(), at(u…
CeciliaZ030 Mar 8, 2023
0200e30
impl Debug & make side effects only when query.index.is_none()
CeciliaZ030 Mar 8, 2023
6628236
change impl Debug for Expression instead & revert test in plonk_api
CeciliaZ030 Mar 8, 2023
502db56
upgrade rust-toolchain
CeciliaZ030 Mar 9, 2023
fe4a554
Update halo2_proofs/src/plonk/circuit.rs
CeciliaZ030 Mar 9, 2023
046ac79
Update halo2_proofs/src/plonk/circuit.rs
CeciliaZ030 Mar 9, 2023
e7237d5
merged with fcdd5b91
CeciliaZ030 Mar 10, 2023
d2b5798
ran clippy
CeciliaZ030 Mar 12, 2023
e7be268
Update halo2_proofs/src/plonk/circuit.rs
CeciliaZ030 Apr 26, 2023
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
22 changes: 9 additions & 13 deletions halo2_proofs/examples/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,28 +66,22 @@ impl<const W: usize> MyConfig<W> {
let z = meta.advice_column_in(SecondPhase);

meta.create_gate("z should start with 1", |meta| {
let q_first = meta.query_selector(q_first);
let z = meta.query_advice(z, Rotation::cur());
let one = Expression::Constant(F::one());

vec![q_first * (one - z)]
vec![q_first.expr() * (one - z.expr())]
});

meta.create_gate("z should end with 1", |meta| {
let q_last = meta.query_selector(q_last);
let z = meta.query_advice(z, Rotation::cur());
let one = Expression::Constant(F::one());

vec![q_last * (one - z)]
vec![q_last.expr() * (one - z.expr())]
});

meta.create_gate("z should have valid transition", |meta| {
let q_shuffle = meta.query_selector(q_shuffle);
let original = original.map(|advice| meta.query_advice(advice, Rotation::cur()));
let shuffled = shuffled.map(|advice| meta.query_advice(advice, Rotation::cur()));
let [theta, gamma] = [theta, gamma].map(|challenge| meta.query_challenge(challenge));
let [z, z_w] =
[Rotation::cur(), Rotation::next()].map(|rotation| meta.query_advice(z, rotation));
let q_shuffle = q_shuffle.expr();
let original = original.map(|advice| advice.expr());
let shuffled = shuffled.map(|advice| advice.expr());
let [theta, gamma] = [theta, gamma].map(|challenge| challenge.expr());

// Compress
let original = original
Expand All @@ -101,7 +95,9 @@ impl<const W: usize> MyConfig<W> {
.reduce(|acc, a| acc * theta.clone() + a)
.unwrap();

vec![q_shuffle * (z * (original + gamma.clone()) - z_w * (shuffled + gamma))]
vec![
q_shuffle * (z.expr() * (original + gamma.clone()) - z.next() * (shuffled + gamma)),
]
});

Self {
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,23 +829,23 @@ impl<F: FieldExt> MockProver<F> {
&|scalar| Value::Real(scalar),
&|_| panic!("virtual selectors are removed during optimization"),
&|query| {
let query = self.cs.fixed_queries[query.index];
let query = self.cs.fixed_queries[query.index.unwrap()];
let column_index = query.0.index();
let rotation = query.1 .0;
self.fixed[column_index]
[(row as i32 + n + rotation) as usize % n as usize]
.into()
},
&|query| {
let query = self.cs.advice_queries[query.index];
let query = self.cs.advice_queries[query.index.unwrap()];
let column_index = query.0.index();
let rotation = query.1 .0;
self.advice[column_index]
[(row as i32 + n + rotation) as usize % n as usize]
.into()
},
&|query| {
let query = self.cs.instance_queries[query.index];
let query = self.cs.instance_queries[query.index.unwrap()];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unwraps still bother me TBH.
But the overall solution is much better I agree.

let column_index = query.0.index();
let rotation = query.1 .0;
Value::Real(
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/src/dev/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ impl FailureLocation {
expression.evaluate(
&|_| vec![],
&|_| panic!("virtual selectors are removed during optimization"),
&|query| vec![cs.fixed_queries[query.index].0.into()],
&|query| vec![cs.advice_queries[query.index].0.into()],
&|query| vec![cs.instance_queries[query.index].0.into()],
&|query| vec![cs.fixed_queries[query.index.unwrap()].0.into()],
&|query| vec![cs.advice_queries[query.index.unwrap()].0.into()],
&|query| vec![cs.instance_queries[query.index.unwrap()].0.into()],
&|_| vec![],
&|a| a,
&|mut a, mut b| {
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/dev/failure/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub(super) fn expression_to_string<F: Field>(
label.clone()
} else if query.rotation.0 == 0 {
// This is most likely a merged selector
format!("S{}", query.index)
format!("S{}", query.index.unwrap())
} else {
// No idea how we'd get here...
format!("F{}@{}", query.column_index, query.rotation.0)
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/src/dev/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{

pub(crate) struct AnyQuery {
/// Query index
pub index: usize,
pub index: Option<usize>,
/// Column type
pub column_type: Any,
/// Column index
Expand Down Expand Up @@ -80,7 +80,7 @@ pub(super) fn load<'a, F: FieldExt, T: ColumnType, Q: Into<AnyQuery> + Copy>(
cells: &'a [Vec<CellValue<F>>],
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let (column, at) = &queries[query.into().index.unwrap()];
let resolved_row = (row + at.0) % n;
cells[column.index()][resolved_row as usize].into()
}
Expand All @@ -93,7 +93,7 @@ pub(super) fn load_instance<'a, F: FieldExt, T: ColumnType, Q: Into<AnyQuery> +
cells: &'a [Vec<F>],
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let (column, at) = &queries[query.into().index.unwrap()];
let resolved_row = (row + at.0) % n;
Value::Real(cells[column.index()][resolved_row as usize])
}
Expand Down
Loading