Skip to content

Commit

Permalink
Get it working
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Jan 3, 2024
1 parent 6037910 commit 19298c2
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 178 deletions.
5 changes: 2 additions & 3 deletions halo2_proofs/src/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ impl<C: CurveAffine> VerifyingKeyV2<C> {
hasher.update(s.as_bytes());

// Hash in final Blake2bState
// TODO: Uncomment
// vk.transcript_repr = C::Scalar::from_uniform_bytes(hasher.finalize().as_array());
dbg!(&vk.transcript_repr);
vk.transcript_repr = C::Scalar::from_uniform_bytes(hasher.finalize().as_array());
// dbg!(&vk.transcript_repr);

vk
}
Expand Down
268 changes: 188 additions & 80 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,43 +1704,84 @@ pub struct CompiledCircuitV2<F: Field> {
pub(crate) cs: ConstraintSystemV2Backend<F>,
}

struct QueriesSet {
advice: BTreeSet<(Column<Advice>, Rotation)>,
instance: BTreeSet<(Column<Instance>, Rotation)>,
fixed: BTreeSet<(Column<Fixed>, Rotation)>,
}

fn collect_queries<F: Field>(expr: &ExpressionMid<F>, queries: &mut QueriesSet) {
match expr {
ExpressionMid::Constant(_) => (),
ExpressionMid::Fixed(query) => {
queries
.fixed
.insert((Column::new(query.column_index, Fixed), query.rotation));
}
ExpressionMid::Advice(query) => {
queries.advice.insert((
Column::new(query.column_index, Advice { phase: query.phase }),
query.rotation,
));
}
ExpressionMid::Instance(query) => {
queries
.instance
.insert((Column::new(query.column_index, Instance), query.rotation));
}
ExpressionMid::Challenge(_) => (),
ExpressionMid::Negated(a) => collect_queries(a, queries),
ExpressionMid::Sum(a, b) => {
collect_queries(a, queries);
collect_queries(b, queries);
}
ExpressionMid::Product(a, b) => {
collect_queries(a, queries);
collect_queries(b, queries);
struct QueriesMap {
advice_map: HashMap<(Column<Advice>, Rotation), usize>,
instance_map: HashMap<(Column<Instance>, Rotation), usize>,
fixed_map: HashMap<(Column<Fixed>, Rotation), usize>,
advice: Vec<(Column<Advice>, Rotation)>,
instance: Vec<(Column<Instance>, Rotation)>,
fixed: Vec<(Column<Fixed>, Rotation)>,
}

impl QueriesMap {
fn add_advice(&mut self, col: Column<Advice>, rot: Rotation) -> usize {
*self.advice_map.entry((col, rot)).or_insert_with(|| {
self.advice.push((col, rot));
self.advice.len() - 1
})
}
fn add_instance(&mut self, col: Column<Instance>, rot: Rotation) -> usize {
*self.instance_map.entry((col, rot)).or_insert_with(|| {
self.instance.push((col, rot));
self.instance.len() - 1
})
}
fn add_fixed(&mut self, col: Column<Fixed>, rot: Rotation) -> usize {
*self.fixed_map.entry((col, rot)).or_insert_with(|| {
self.fixed.push((col, rot));
self.fixed.len() - 1
})
}
}

impl QueriesMap {
fn to_expression<F: Field>(&mut self, expr: &ExpressionMid<F>) -> Expression<F> {

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference

warning: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference --> halo2_proofs/src/plonk/circuit.rs:1738:32 | 1738 | fn to_expression<F: Field>(&mut self, expr: &ExpressionMid<F>) -> Expression<F> { | ^^^^^^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention = note: `-W clippy::wrong-self-convention` implied by `-W clippy::all`

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference

warning: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference --> halo2_proofs/src/plonk/circuit.rs:1738:32 | 1738 | fn to_expression<F: Field>(&mut self, expr: &ExpressionMid<F>) -> Expression<F> { | ^^^^^^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention = note: `-W clippy::wrong-self-convention` implied by `-W clippy::all`
match expr {
ExpressionMid::Constant(c) => Expression::Constant(*c),
ExpressionMid::Fixed(query) => {
let (col, rot) = (Column::new(query.column_index, Fixed), query.rotation);
let index = self.add_fixed(col, rot);
Expression::Fixed(FixedQuery {
index: Some(index),
column_index: query.column_index,
rotation: query.rotation,
})
}
ExpressionMid::Advice(query) => {
let (col, rot) = (
Column::new(query.column_index, Advice { phase: query.phase }),
query.rotation,
);
let index = self.add_advice(col, rot);
Expression::Advice(AdviceQuery {
index: Some(index),
column_index: query.column_index,
rotation: query.rotation,
phase: query.phase,
})
}
ExpressionMid::Instance(query) => {
let (col, rot) = (Column::new(query.column_index, Instance), query.rotation);
let index = self.add_instance(col, rot);
Expression::Instance(InstanceQuery {
index: Some(index),
column_index: query.column_index,
rotation: query.rotation,
})
}
ExpressionMid::Challenge(c) => Expression::Challenge(*c),
ExpressionMid::Negated(e) => Expression::Negated(Box::new(self.to_expression(e))),
ExpressionMid::Sum(lhs, rhs) => Expression::Sum(
Box::new(self.to_expression(lhs)),
Box::new(self.to_expression(rhs)),
),
ExpressionMid::Product(lhs, rhs) => Expression::Product(
Box::new(self.to_expression(lhs)),
Box::new(self.to_expression(rhs)),
),
ExpressionMid::Scaled(e, c) => Expression::Scaled(Box::new(self.to_expression(e)), *c),
}
ExpressionMid::Scaled(a, _) => collect_queries(a, queries),
};
}
}

/*
Expand Down Expand Up @@ -2138,63 +2179,100 @@ impl<F: Field> ConstraintSystemV2Backend<F> {
(0..=max_phase).collect()
}

pub(crate) fn collect_queries(&self) -> Queries {
let mut queries = QueriesSet {
advice: BTreeSet::new(),
instance: BTreeSet::new(),
fixed: BTreeSet::new(),
pub(crate) fn collect_queries(
&self,
) -> (
Queries,
Vec<Gate<F>>,
Vec<lookup::Argument<F>>,
Vec<shuffle::Argument<F>>,
) {

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2184:10 | 2184 | ) -> ( | __________^ 2185 | | Queries, 2186 | | Vec<Gate<F>>, 2187 | | Vec<lookup::Argument<F>>, 2188 | | Vec<shuffle::Argument<F>>, 2189 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

very complex type used. Consider factoring parts into `type` definitions

warning: very complex type used. Consider factoring parts into `type` definitions --> halo2_proofs/src/plonk/circuit.rs:2184:10 | 2184 | ) -> ( | __________^ 2185 | | Queries, 2186 | | Vec<Gate<F>>, 2187 | | Vec<lookup::Argument<F>>, 2188 | | Vec<shuffle::Argument<F>>, 2189 | | ) { | |_____^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#type_complexity
let mut queries = QueriesMap {
advice_map: HashMap::new(),
instance_map: HashMap::new(),
fixed_map: HashMap::new(),
advice: Vec::new(),
instance: Vec::new(),
fixed: Vec::new(),
};
let mut num_advice_queries = vec![0; self.num_advice_columns];

for gate in &self.gates {
for expr in gate.polynomials() {
collect_queries(expr, &mut queries);
}
}
for lookup in &self.lookups {
for expr in lookup
.input_expressions
.iter()
.chain(lookup.table_expressions.iter())
{
collect_queries(expr, &mut queries);
}
}
for shuffle in &self.shuffles {
for expr in shuffle
.input_expressions
.iter()
.chain(shuffle.shuffle_expressions.iter())
{
collect_queries(expr, &mut queries);
}
}
let gates: Vec<_> = self
.gates
.iter()
.map(|gate| Gate {
name: gate.name.clone(),
constraint_names: gate.constraint_names.clone(),
polys: gate
.polynomials()
.iter()
.map(|e| queries.to_expression(e))
.collect(),
queried_selectors: Vec::new(), // Unused?
queried_cells: Vec::new(), // Unused?
})
.collect();
let lookups: Vec<_> = self
.lookups
.iter()
.map(|lookup| lookup::Argument {
name: lookup.name.clone(),
input_expressions: lookup
.input_expressions
.iter()
.map(|e| queries.to_expression(e))
.collect(),
table_expressions: lookup
.table_expressions
.iter()
.map(|e| queries.to_expression(e))
.collect(),
})
.collect();
let shuffles: Vec<_> = self
.shuffles
.iter()
.map(|shuffle| shuffle::Argument {
name: shuffle.name.clone(),
input_expressions: shuffle
.input_expressions
.iter()
.map(|e| queries.to_expression(e))
.collect(),
shuffle_expressions: shuffle
.shuffle_expressions
.iter()
.map(|e| queries.to_expression(e))
.collect(),
})
.collect();

for column in self.permutation.get_columns() {
match column.column_type {
Any::Instance => queries
.instance
.insert((Column::new(column.index(), Instance), Rotation::cur())),
Any::Fixed => queries
.fixed
.insert((Column::new(column.index(), Fixed), Rotation::cur())),
Any::Advice(advice) => queries
.advice
.insert((Column::new(column.index(), advice), Rotation::cur())),
Any::Instance => {
queries.add_instance(Column::new(column.index(), Instance), Rotation::cur())
}
Any::Fixed => {
queries.add_fixed(Column::new(column.index(), Fixed), Rotation::cur())
}
Any::Advice(advice) => {
queries.add_advice(Column::new(column.index(), advice), Rotation::cur())
}
};
}

let mut num_advice_queries = vec![0; self.num_advice_columns];
for (column, _) in queries.advice.iter() {
num_advice_queries[column.index()] += 1;
}

let queries = Queries {
advice: queries.advice.into_iter().collect(),
instance: queries.instance.into_iter().collect(),
fixed: queries.fixed.into_iter().collect(),
advice: queries.advice,
instance: queries.instance,
fixed: queries.fixed,
num_advice_queries,
};
// println!("DBG collected queries\n{:#?}", queries);
queries
(queries, gates, lookups, shuffles)
}
}

Expand Down Expand Up @@ -2252,8 +2330,38 @@ pub struct ConstraintSystem<F: Field> {
}

impl<F: Field> From<ConstraintSystemV2Backend<F>> for ConstraintSystem<F> {
fn from(circuit: ConstraintSystemV2Backend<F>) -> Self {
todo!()
fn from(cs2: ConstraintSystemV2Backend<F>) -> Self {
let (queries, gates, lookups, shuffles) = cs2.collect_queries();
ConstraintSystem {
num_fixed_columns: cs2.num_fixed_columns,
num_advice_columns: cs2.num_advice_columns,
num_instance_columns: cs2.num_instance_columns,
num_selectors: 0,
num_challenges: cs2.num_challenges,
unblinded_advice_columns: cs2.unblinded_advice_columns,
advice_column_phase: cs2
.advice_column_phase
.into_iter()
.map(|p| sealed::Phase(p))

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

redundant closure

warning: redundant closure --> halo2_proofs/src/plonk/circuit.rs:2345:22 | 2345 | .map(|p| sealed::Phase(p)) | ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `sealed::Phase` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure = note: `-W clippy::redundant-closure` implied by `-W clippy::all`

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

redundant closure

warning: redundant closure --> halo2_proofs/src/plonk/circuit.rs:2345:22 | 2345 | .map(|p| sealed::Phase(p)) | ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `sealed::Phase` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure = note: `-W clippy::redundant-closure` implied by `-W clippy::all`
.collect(),
challenge_phase: cs2
.challenge_phase
.into_iter()
.map(|p| sealed::Phase(p))

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

redundant closure

warning: redundant closure --> halo2_proofs/src/plonk/circuit.rs:2350:22 | 2350 | .map(|p| sealed::Phase(p)) | ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `sealed::Phase` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

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

View workflow job for this annotation

GitHub Actions / Clippy (beta)

redundant closure

warning: redundant closure --> halo2_proofs/src/plonk/circuit.rs:2350:22 | 2350 | .map(|p| sealed::Phase(p)) | ^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `sealed::Phase` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
.collect(),
selector_map: Vec::new(),
gates,
advice_queries: queries.advice,
num_advice_queries: queries.num_advice_queries,
instance_queries: queries.instance,
fixed_queries: queries.fixed,
permutation: cs2.permutation,
lookups,
shuffles,
general_column_annotations: cs2.general_column_annotations,
constants: Vec::new(),
minimum_degree: None,
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl<
)
})
.collect();
dbg!(&advice_evals);
// dbg!(&advice_evals);

// Hash each advice column evaluation
for eval in advice_evals.iter() {
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/plonk/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ where
let advice_evals = (0..num_proofs)
.map(|_| -> Result<Vec<_>, _> { read_n_scalars(transcript, vk.cs.advice_queries.len()) })
.collect::<Result<Vec<_>, _>>()?;
dbg!(&advice_evals);
// dbg!(&advice_evals);

let fixed_evals = read_n_scalars(transcript, vk.cs.fixed_queries.len())?;

Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl<'a, F: Field, B: Basis> Sub<F> for &'a Polynomial<F, B> {
/// Describes the relative rotation of a vector. Negative numbers represent
/// reverse (leftmost) rotations and positive numbers represent forward (rightmost)
/// rotations. Zero represents no rotation.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Rotation(pub i32);

impl Rotation {
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ where
fn write_point(&mut self, point: C) -> io::Result<()> {
self.common_point(point)?;
let compressed = point.to_bytes();
println!("DBG write_point\n{:02x?}", compressed.as_ref());
// println!("DBG write_point\n{:02x?}", compressed.as_ref());
self.writer.write_all(compressed.as_ref())
}
fn write_scalar(&mut self, scalar: C::Scalar) -> io::Result<()> {
self.common_scalar(scalar)?;
let data = scalar.to_repr();
println!("DBG write_scalar\n{:02x?}", data.as_ref());
// println!("DBG write_scalar\n{:02x?}", data.as_ref());
self.writer.write_all(data.as_ref())
}
}
Expand Down
Loading

0 comments on commit 19298c2

Please sign in to comment.