Skip to content

Commit

Permalink
feat: make Expression::{Fixed,Advice,Instance} to wrap their own `Q…
Browse files Browse the repository at this point in the history
…uery` struct
  • Loading branch information
han0110 authored and therealyingtong committed Jul 15, 2022
1 parent 5af2bd3 commit 8ff5b1e
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 171 deletions.
12 changes: 6 additions & 6 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,24 +673,24 @@ impl<F: FieldExt> MockProver<F> {
expression.evaluate(
&|scalar| Value::Real(scalar),
&|_| panic!("virtual selectors are removed during optimization"),
&|index, _, _| {
let query = self.cs.fixed_queries[index];
&|query| {
let query = self.cs.fixed_queries[query.index];
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()
},
&|index, _, _| {
let query = self.cs.advice_queries[index];
&|query| {
let query = self.cs.advice_queries[query.index];
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()
},
&|index, _, _| {
let query = self.cs.instance_queries[index];
&|query| {
let query = self.cs.instance_queries[query.index];
let column_index = query.0.index();
let rotation = query.1 .0;
Value::Real(
Expand Down
33 changes: 21 additions & 12 deletions halo2_proofs/src/dev/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use std::iter;
use group::ff::Field;
use pasta_curves::arithmetic::FieldExt;

use super::{metadata, util, MockProver, Region};
use super::{
metadata,
util::{self, AnyQuery},
MockProver, Region,
};
use crate::{
dev::Value,
plonk::{Any, Column, ConstraintSystem, Expression, Gate},
Expand Down Expand Up @@ -55,9 +59,9 @@ impl FailureLocation {
expression.evaluate(
&|_| vec![],
&|_| panic!("virtual selectors are removed during optimization"),
&|index, _, _| vec![cs.fixed_queries[index].0.into()],
&|index, _, _| vec![cs.advice_queries[index].0.into()],
&|index, _, _| vec![cs.instance_queries[index].0.into()],
&|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()],
&|a| a,
&|mut a, mut b| {
a.append(&mut b);
Expand Down Expand Up @@ -387,24 +391,29 @@ fn render_lookup<F: FieldExt>(
expr.evaluate(
&|_| panic!("no constants in table expressions"),
&|_| panic!("no selectors in table expressions"),
&|_, column, _| format!("F{}", column),
&|_, _, _| panic!("no advice columns in table expressions"),
&|_, _, _| panic!("no instance columns in table expressions"),
&|query| format!("F{}", query.column_index),
&|_| panic!("no advice columns in table expressions"),
&|_| panic!("no instance columns in table expressions"),
&|_| panic!("no negations in table expressions"),
&|_, _| panic!("no sums in table expressions"),
&|_, _| panic!("no products in table expressions"),
&|_, _| panic!("no scaling in table expressions"),
)
});

fn cell_value<'a, F: FieldExt>(
fn cell_value<'a, F: FieldExt, Q: Into<AnyQuery> + Copy>(
column_type: Any,
load: impl Fn(usize, usize, Rotation) -> Value<F> + 'a,
) -> impl Fn(usize, usize, Rotation) -> BTreeMap<metadata::VirtualCell, String> + 'a {
move |query_index, column_index, rotation| {
load: impl Fn(Q) -> Value<F> + 'a,
) -> impl Fn(Q) -> BTreeMap<metadata::VirtualCell, String> + 'a {
move |query| {
let AnyQuery {
column_index,
rotation,
..
} = query.into();
Some((
((column_type, column_index).into(), rotation.0).into(),
match load(query_index, column_index, rotation) {
match load(query) {
Value::Real(v) => util::format_value(v),
Value::Poison => unreachable!(),
},
Expand Down
24 changes: 12 additions & 12 deletions halo2_proofs/src/dev/failure/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,33 @@ pub(super) fn expression_to_string<F: Field>(
expr.evaluate(
&util::format_value,
&|_| panic!("virtual selectors are removed during optimization"),
&|query, column, rotation| {
&|query| {
if let Some(label) = layout
.get(&rotation.0)
.and_then(|row| row.get(&(Any::Fixed, column).into()))
.get(&query.rotation.0)
.and_then(|row| row.get(&(Any::Fixed, query.column_index).into()))
{
label.clone()
} else if rotation.0 == 0 {
} else if query.rotation.0 == 0 {
// This is most likely a merged selector
format!("S{}", query)
format!("S{}", query.index)
} else {
// No idea how we'd get here...
format!("F{}@{}", column, rotation.0)
format!("F{}@{}", query.column_index, query.rotation.0)
}
},
&|_, column, rotation| {
&|query| {
layout
.get(&rotation.0)
.get(&query.rotation.0)
.unwrap()
.get(&(Any::Advice, column).into())
.get(&(Any::Advice, query.column_index).into())
.unwrap()
.clone()
},
&|_, column, rotation| {
&|query| {
layout
.get(&rotation.0)
.get(&query.rotation.0)
.unwrap()
.get(&(Any::Instance, column).into())
.get(&(Any::Instance, query.column_index).into())
.unwrap()
.clone()
},
Expand Down
24 changes: 12 additions & 12 deletions halo2_proofs/src/dev/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ impl CircuitGates {
expression: constraint.evaluate(
&util::format_value,
&|selector| format!("S{}", selector.0),
&|_, column, rotation| format!("F{}@{}", column, rotation.0),
&|_, column, rotation| format!("A{}@{}", column, rotation.0),
&|_, column, rotation| format!("I{}@{}", column, rotation.0),
&|query| format!("F{}@{}", query.column_index, query.rotation.0),
&|query| format!("A{}@{}", query.column_index, query.rotation.0),
&|query| format!("I{}@{}", query.column_index, query.rotation.0),
&|a| {
if a.contains(' ') {
format!("-({})", a)
Expand Down Expand Up @@ -153,18 +153,18 @@ impl CircuitGates {
queries: constraint.evaluate(
&|_| BTreeSet::default(),
&|selector| vec![format!("S{}", selector.0)].into_iter().collect(),
&|_, column, rotation| {
vec![format!("F{}@{}", column, rotation.0)]
&|query| {
vec![format!("F{}@{}", query.column_index, query.rotation.0)]
.into_iter()
.collect()
},
&|_, column, rotation| {
vec![format!("A{}@{}", column, rotation.0)]
&|query| {
vec![format!("A{}@{}", query.column_index, query.rotation.0)]
.into_iter()
.collect()
},
&|_, column, rotation| {
vec![format!("I{}@{}", column, rotation.0)]
&|query| {
vec![format!("I{}@{}", query.column_index, query.rotation.0)]
.into_iter()
.collect()
},
Expand Down Expand Up @@ -192,9 +192,9 @@ impl CircuitGates {
poly.evaluate(
&|_| (0, 0, 0),
&|_| (0, 0, 0),
&|_, _, _| (0, 0, 0),
&|_, _, _| (0, 0, 0),
&|_, _, _| (0, 0, 0),
&|_| (0, 0, 0),
&|_| (0, 0, 0),
&|_| (0, 0, 0),
&|(a_n, a_a, a_m)| (a_n + 1, a_a, a_m),
&|(a_n, a_a, a_m), (b_n, b_a, b_m)| (a_n + b_n, a_a + b_a + 1, a_m + b_m),
&|(a_n, a_a, a_m), (b_n, b_a, b_m)| (a_n + b_n, a_a + b_a, a_m + b_m + 1),
Expand Down
94 changes: 73 additions & 21 deletions halo2_proofs/src/dev/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,57 @@ use pasta_curves::arithmetic::FieldExt;

use super::{metadata, CellValue, Value};
use crate::{
plonk::{Any, Column, ColumnType, Expression, Gate, VirtualCell},
plonk::{
AdviceQuery, Any, Column, ColumnType, Expression, FixedQuery, Gate, InstanceQuery,
VirtualCell,
},
poly::Rotation,
};

pub(crate) struct AnyQuery {
/// Query index
pub index: usize,
/// Column type
pub column_type: Any,
/// Column index
pub column_index: usize,
/// Rotation of this query
pub rotation: Rotation,
}

impl From<FixedQuery> for AnyQuery {
fn from(query: FixedQuery) -> Self {
Self {
index: query.index,
column_type: Any::Fixed,
column_index: query.column_index,
rotation: query.rotation,
}
}
}

impl From<AdviceQuery> for AnyQuery {
fn from(query: AdviceQuery) -> Self {
Self {
index: query.index,
column_type: Any::Advice,
column_index: query.column_index,
rotation: query.rotation,
}
}
}

impl From<InstanceQuery> for AnyQuery {
fn from(query: InstanceQuery) -> Self {
Self {
index: query.index,
column_type: Any::Instance,
column_index: query.column_index,
rotation: query.rotation,
}
}
}

pub(super) fn format_value<F: Field>(v: F) -> String {
if v.is_zero_vartime() {
"0".into()
Expand All @@ -26,38 +73,43 @@ pub(super) fn format_value<F: Field>(v: F) -> String {
}
}

pub(super) fn load<'a, F: FieldExt, T: ColumnType>(
pub(super) fn load<'a, F: FieldExt, T: ColumnType, Q: Into<AnyQuery> + Copy>(
n: i32,
row: i32,
queries: &'a [(Column<T>, Rotation)],
cells: &'a [Vec<CellValue<F>>],
) -> impl Fn(usize, usize, Rotation) -> Value<F> + 'a {
move |index, _, _| {
let (column, at) = &queries[index];
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let resolved_row = (row + at.0) % n;
cells[column.index()][resolved_row as usize].into()
}
}

pub(super) fn load_instance<'a, F: FieldExt, T: ColumnType>(
pub(super) fn load_instance<'a, F: FieldExt, T: ColumnType, Q: Into<AnyQuery> + Copy>(
n: i32,
row: i32,
queries: &'a [(Column<T>, Rotation)],
cells: &'a [Vec<F>],
) -> impl Fn(usize, usize, Rotation) -> Value<F> + 'a {
move |index, _, _| {
let (column, at) = &queries[index];
) -> impl Fn(Q) -> Value<F> + 'a {
move |query| {
let (column, at) = &queries[query.into().index];
let resolved_row = (row + at.0) % n;
Value::Real(cells[column.index()][resolved_row as usize])
}
}

fn cell_value<'a, F: FieldExt>(
fn cell_value<'a, F: FieldExt, Q: Into<AnyQuery> + Copy>(
virtual_cells: &'a [VirtualCell],
column_type: Any,
load: impl Fn(usize, usize, Rotation) -> Value<F> + 'a,
) -> impl Fn(usize, usize, Rotation) -> BTreeMap<metadata::VirtualCell, String> + 'a {
move |query_index, column_index, rotation| {
load: impl Fn(Q) -> Value<F> + 'a,
) -> impl Fn(Q) -> BTreeMap<metadata::VirtualCell, String> + 'a {
move |query| {
let AnyQuery {
column_type,
column_index,
rotation,
..
} = query.into();
virtual_cells
.iter()
.find(|c| {
Expand All @@ -69,7 +121,7 @@ fn cell_value<'a, F: FieldExt>(
.map(|cell| {
(
cell.clone().into(),
match load(query_index, column_index, rotation) {
match load(query) {
Value::Real(v) => format_value(v),
Value::Poison => unreachable!(),
},
Expand All @@ -83,17 +135,17 @@ fn cell_value<'a, F: FieldExt>(
pub(super) fn cell_values<'a, F: FieldExt>(
gate: &Gate<F>,
poly: &Expression<F>,
load_fixed: impl Fn(usize, usize, Rotation) -> Value<F> + 'a,
load_advice: impl Fn(usize, usize, Rotation) -> Value<F> + 'a,
load_instance: impl Fn(usize, usize, Rotation) -> Value<F> + 'a,
load_fixed: impl Fn(FixedQuery) -> Value<F> + 'a,
load_advice: impl Fn(AdviceQuery) -> Value<F> + 'a,
load_instance: impl Fn(InstanceQuery) -> Value<F> + 'a,
) -> Vec<(metadata::VirtualCell, String)> {
let virtual_cells = gate.queried_cells();
let cell_values = poly.evaluate(
&|_| BTreeMap::default(),
&|_| panic!("virtual selectors are removed during optimization"),
&cell_value(virtual_cells, Any::Fixed, load_fixed),
&cell_value(virtual_cells, Any::Advice, load_advice),
&cell_value(virtual_cells, Any::Instance, load_instance),
&cell_value(virtual_cells, load_fixed),
&cell_value(virtual_cells, load_advice),
&cell_value(virtual_cells, load_instance),
&|a| a,
&|mut a, mut b| {
a.append(&mut b);
Expand Down
Loading

0 comments on commit 8ff5b1e

Please sign in to comment.