Skip to content

Commit

Permalink
disable multiphase prover
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenfeizhang committed Dec 19, 2023
1 parent 3d44389 commit 5178dc4
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 54 deletions.
7 changes: 5 additions & 2 deletions halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ rand_core = { version = "0.6", default-features = false, features = ["getrandom"
getrandom = { version = "0.2", features = ["js"] }

[features]
default = ["batch", "gwc", "multicore", "parallel_syn"]
default = ["batch", "gwc", "multicore", "parallel_syn" ]
multicore = ["maybe-rayon/threads"]
dev-graph = ["plotters", "tabbycat"]
test-dev-graph = [
Expand All @@ -100,11 +100,14 @@ batch = ["rand_core/getrandom"]
shplonk = []
gwc = []
parallel_syn = []
phase-check = []
profile = ["ark-std/print-trace"]
mock-batch-inv = []
circuit-params = []

# todo: we have both mutliphase prover and phase check. consider merge them
phase-check = []
multiphase-mock-prover = []

[lib]
bench = false

Expand Down
171 changes: 121 additions & 50 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ use std::collections::HashSet;
use std::iter;
use std::ops::{Add, Mul, Neg, Range};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::Instant;

Check warning on line 8 in halo2_proofs/src/dev.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `std::time::Instant`

warning: unused import: `std::time::Instant` --> halo2_proofs/src/dev.rs:8:5 | 8 | use std::time::Instant; | ^^^^^^^^^^^^^^^^^^

use blake2b_simd::blake2b;
use ff::Field;
use ff::FromUniformBytes;
use ff::{BatchInvert, Field};
use group::Group;

use crate::plonk::permutation::keygen::Assembly;
use crate::{
circuit,
plonk::{
permutation,
sealed::{self, SealedPhase},
Advice, Any, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error,
Expression, FirstPhase, Fixed, FloorPlanner, Instance, Phase, Selector,
permutation, sealed, Advice, Any, Assigned, Assignment, Challenge, Circuit, Column,
ConstraintSystem, Error, Expression, Fixed, FloorPlanner, Instance, Selector,
},
};

#[cfg(feature = "multiphase-mock-prover")]
use crate::{plonk::sealed::SealedPhase, plonk::FirstPhase, plonk::Phase};
#[cfg(feature = "multiphase-mock-prover")]
use ff::BatchInvert;
#[cfg(feature = "multiphase-mock-prover")]
use group::Group;

#[cfg(feature = "multicore")]
use crate::multicore::{
IndexedParallelIterator, IntoParallelIterator, IntoParallelRefIterator, ParallelIterator,
Expand All @@ -49,7 +53,6 @@ pub use tfp::TracingFloorPlanner;
#[cfg(feature = "dev-graph")]
mod graph;

use crate::circuit::Cell;
use crate::helpers::CopyCell;
#[cfg(feature = "dev-graph")]
#[cfg_attr(docsrs, doc(cfg(feature = "dev-graph")))]
Expand Down Expand Up @@ -478,6 +481,7 @@ impl<F: Field> InstanceValue<F> {
}
}

#[cfg(feature = "multiphase-mock-prover")]
impl<'a, F: Field> MockProver<'a, F> {
fn in_phase<P: Phase>(&self, phase: P) -> bool {
self.current_phase == phase.to_sealed()
Expand All @@ -490,6 +494,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
NR: Into<String>,
N: FnOnce() -> NR,
{
#[cfg(feature = "multiphase-mock-prover")]
if !self.in_phase(FirstPhase) {
return;
}
Expand All @@ -507,6 +512,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
}

fn exit_region(&mut self) {
#[cfg(feature = "multiphase-mock-prover")]
if !self.in_phase(FirstPhase) {
return;
}
Expand All @@ -519,6 +525,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
#[cfg(feature = "multiphase-mock-prover")]
if !self.in_phase(FirstPhase) {
return;
}
Expand All @@ -535,17 +542,24 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
if !self.in_phase(FirstPhase) {
return Ok(());
#[cfg(feature = "multiphase-mock-prover")]
{
if !self.in_phase(FirstPhase) {
return Ok(());
}
assert!(
self.usable_rows.contains(&row),
"row={} not in usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
}

assert!(
self.usable_rows.contains(&row),
"row={} not in usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

if !self.rw_rows.contains(&row) {
return Err(Error::InvalidRange(
Expand Down Expand Up @@ -700,6 +714,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
column: Column<Instance>,
row: usize,
) -> Result<circuit::Value<F>, Error> {
#[cfg(feature = "multiphase-mock-prover")]
assert!(
self.usable_rows.contains(&row),
"row={}, usable_rows={:?}, k={}",
Expand All @@ -708,6 +723,11 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
self.k,
);

#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

Ok(self
.instance
.get(column.index())
Expand All @@ -734,6 +754,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
return Ok(());
}

#[cfg(feature = "multiphase-mock-prover")]
if self.in_phase(FirstPhase) {
assert!(
self.usable_rows.contains(&row),
Expand All @@ -752,6 +773,11 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
}
}

#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

if !self.rw_rows.contains(&row) {
return Err(Error::InvalidRange(
row,
Expand All @@ -762,6 +788,16 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
));
}

#[cfg(not(feature = "multiphase-mock-prover"))]
if let Some(region) = self.current_region.as_mut() {
region.update_extent(column.into(), row);
region
.cells
.entry((column.into(), row))
.and_modify(|count| *count += 1)
.or_default();
}

let advice_anno = anno().into();
#[cfg(not(feature = "mock-batch-inv"))]
let val_res = to().into_field().evaluate().assign();
Expand All @@ -781,6 +817,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
#[cfg(feature = "mock-batch-inv")]
let assigned = CellValue::from(val_res?);

Check failure on line 818 in halo2_proofs/src/dev.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

the trait bound `F: group::Group` is not satisfied

error[E0277]: the trait bound `F: group::Group` is not satisfied --> halo2_proofs/src/dev.rs:818:40 | 818 | let assigned = CellValue::from(val_res?); | --------------- ^^^^^^^^ the trait `group::Group` is not implemented for `F` | | | required by a bound introduced by this call | note: required for `dev::CellValue<F>` to implement `std::convert::From<plonk::assigned::Assigned<F>>` --> halo2_proofs/src/dev.rs:152:24 | 152 | impl<F: Group + Field> From<Assigned<F>> for CellValue<F> { | ----- ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ | | | unsatisfied trait bound introduced here help: consider further restricting this bound | 491 | impl<'a, F: Field + group::Group> Assignment<F> for MockProver<'a, F> { | ++++++++++++++

#[cfg(feature = "multiphase-mock-prover")]
if self.in_phase(column.column_type().phase) {
*self
.advice
Expand All @@ -789,6 +826,13 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
.expect("bounds failure") = assigned;
}

#[cfg(not(feature = "multiphase-mock-prover"))]
*self
.advice
.get_mut(column.index())
.and_then(|v| v.get_mut(row - self.rw_rows.start))
.ok_or(Error::BoundsFailure)? = assigned;

#[cfg(feature = "phase-check")]
// if false && self.current_phase.0 > column.column_type().phase.0 {
if false {
Expand Down Expand Up @@ -822,18 +866,25 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
if !self.in_phase(FirstPhase) {
return Ok(());
#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

assert!(
self.usable_rows.contains(&row),
"row={}, usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
#[cfg(feature = "multiphase-mock-prover")]
{
if !self.in_phase(FirstPhase) {
return Ok(());
}

assert!(
self.usable_rows.contains(&row),
"row={}, usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
}
if !self.rw_rows.contains(&row) {
return Err(Error::InvalidRange(
row,
Expand Down Expand Up @@ -881,18 +932,26 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
right_column: Column<Any>,
right_row: usize,
) -> Result<(), crate::plonk::Error> {
if !self.in_phase(FirstPhase) {
return Ok(());
#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&left_row) || !self.usable_rows.contains(&right_row) {
return Err(Error::not_enough_rows_available(self.k));
}

assert!(
self.usable_rows.contains(&left_row) && self.usable_rows.contains(&right_row),
"left_row={}, right_row={}, usable_rows={:?}, k={}",
left_row,
right_row,
self.usable_rows,
self.k,
);
#[cfg(feature = "multiphase-mock-prover")]
{
if !self.in_phase(FirstPhase) {
return Ok(());
}

assert!(
self.usable_rows.contains(&left_row) && self.usable_rows.contains(&right_row),
"left_row={}, right_row={}, usable_rows={:?}, k={}",
left_row,
right_row,
self.usable_rows,
self.k,
);
}

match self.permutation.as_mut() {
Some(permutation) => permutation.copy(left_column, left_row, right_column, right_row),
Expand Down Expand Up @@ -921,17 +980,24 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
from_row: usize,
to: circuit::Value<Assigned<F>>,
) -> Result<(), Error> {
if !self.in_phase(FirstPhase) {
return Ok(());
#[cfg(not(feature = "multiphase-mock-prover"))]
if !self.usable_rows.contains(&from_row) {
return Err(Error::not_enough_rows_available(self.k));
}
#[cfg(feature = "multiphase-mock-prover")]
{
if !self.in_phase(FirstPhase) {
return Ok(());
}

assert!(
self.usable_rows.contains(&from_row),
"row={}, usable_rows={:?}, k={}",
from_row,
self.usable_rows,
self.k,
);
assert!(
self.usable_rows.contains(&from_row),
"row={}, usable_rows={:?}, k={}",
from_row,
self.usable_rows,
self.k,
);
}

for row in self.usable_rows.clone().skip(from_row) {
self.assign_fixed(|| "", col, row, || to)?;
Expand Down Expand Up @@ -1045,10 +1111,10 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
.collect()
};

// #[cfg(feature = "phase-check")]
// let current_phase = FirstPhase.to_sealed();
// #[cfg(not(feature = "phase-check"))]
// let current_phase = crate::plonk::sealed::Phase(cs.max_phase());
#[cfg(feature = "phase-check")]
let current_phase = FirstPhase.to_sealed();
#[cfg(not(feature = "phase-check"))]
let current_phase = crate::plonk::sealed::Phase(cs.max_phase());

let mut prover = MockProver {
k,
Expand All @@ -1068,7 +1134,7 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
permutation: Some(permutation),
rw_rows: 0..usable_rows,
usable_rows: 0..usable_rows,
current_phase: FirstPhase.to_sealed(),
current_phase,
};

#[cfg(feature = "phase-check")]
Expand Down Expand Up @@ -1127,6 +1193,8 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
#[cfg(not(feature = "phase-check"))]
{
let syn_time = Instant::now();

#[cfg(feature = "multiphase-mock-prover")]
for current_phase in prover.cs.phases() {
prover.current_phase = current_phase;
ConcreteCircuit::FloorPlanner::synthesize(
Expand All @@ -1136,6 +1204,9 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
constants.clone(),
)?;
}

#[cfg(not(feature = "multiphase-mock-prover"))]
ConcreteCircuit::FloorPlanner::synthesize(&mut prover, circuit, config, constants)?;
log::info!("MockProver synthesize took {:?}", syn_time.elapsed());
}

Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::plonk::{Any, Column};
use crate::poly::Polynomial;
use ff::{Field, FromUniformBytes, PrimeField};
use ff::{FromUniformBytes, PrimeField};
use halo2curves::{serde::SerdeObject, CurveAffine};
use num_bigint::BigUint;
use std::io;
Expand Down Expand Up @@ -88,6 +88,7 @@ macro_rules! two_dim_vec_to_vec_of_slice {
#[cfg(test)]
mod test {
use super::*;
use ff::Field;
use halo2curves::bn256::{Fq, G1Affine};
use rand_core::OsRng;
#[test]
Expand Down
1 change: 1 addition & 0 deletions halo2_proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![allow(clippy::op_ref, clippy::many_single_char_names)]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_debug_implementations)]
#![feature(stmt_expr_attributes)]
// #![deny(missing_docs)]
// #![deny(unsafe_code)]

Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.66.0
nightly-2023-08-12

0 comments on commit 5178dc4

Please sign in to comment.