Skip to content

Commit

Permalink
Improve halo2 query calls (privacy-scaling-explorations#154)
Browse files Browse the repository at this point in the history
* return expression from cell

* add example

* selector

* recurse Expression to fill in index

* minimized changes from the original

* backword compatible meta.query_X & challange.expr()

* cargo fmt

* fixed lookup to pass all tests

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* Update comments

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* update

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>

* add primitives.rs back

* remove example2

* backward compatible meta.query_X & Column.cur(), next(), prev(), at(usize)

* impl Debug & make side effects only when query.index.is_none()

* change impl Debug for Expression instead & revert test in plonk_api

* upgrade rust-toolchain

* Update halo2_proofs/src/plonk/circuit.rs

Co-authored-by: Han <tinghan0110@gmail.com>

* Update halo2_proofs/src/plonk/circuit.rs

Co-authored-by: Han <tinghan0110@gmail.com>

* ran clippy

* Update halo2_proofs/src/plonk/circuit.rs

Co-authored-by: Han <tinghan0110@gmail.com>

---------

Co-authored-by: Brecht Devos <Brechtp.Devos@gmail.com>
Co-authored-by: Han <tinghan0110@gmail.com>
  • Loading branch information
3 people authored and Velaciela committed Oct 9, 2023
1 parent e577808 commit b05bc4d
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 102 deletions.
28 changes: 11 additions & 17 deletions halo2_proofs/examples/shuffle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use halo2_proofs::{
multiopen::{ProverIPA, VerifierIPA},
strategy::AccumulatorStrategy,
},
Rotation, VerificationStrategy,
VerificationStrategy,
},
transcript::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
Expand Down Expand Up @@ -63,29 +63,23 @@ impl<const W: usize> MyConfig<W> {
// Second phase
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());
meta.create_gate("z should start with 1", |_| {
let one = Expression::Constant(F::ONE);

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

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());
meta.create_gate("z should end with 1", |_| {
let one = Expression::Constant(F::ONE);

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

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));
meta.create_gate("z should have valid transition", |_| {
let q_shuffle = q_shuffle.expr();
let original = original.map(|advice| advice.cur());
let shuffled = shuffled.map(|advice| advice.cur());
let [theta, gamma] = [theta, gamma].map(|challenge| challenge.expr());

// Compress
let original = original
Expand All @@ -99,7 +93,7 @@ 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.cur() * (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 @@ -1303,23 +1303,23 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, 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()];
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 @@ -70,9 +70,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
12 changes: 6 additions & 6 deletions halo2_proofs/src/dev/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 @@ -79,7 +79,7 @@ pub(super) fn load<'a, F: Field, 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,8 +93,8 @@ pub(super) fn load_slice<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Copy>(
cells: &'a [&mut [CellValue<F>]],
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let resolved_row = (row + at.0 + n) % n;
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 @@ -106,8 +106,8 @@ pub(super) fn load_instance<'a, F: Field, T: ColumnType, Q: Into<AnyQuery> + Cop
cells: &'a [Vec<F>],
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let resolved_row = (row + at.0 + n) % n;
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

0 comments on commit b05bc4d

Please sign in to comment.