Skip to content

Commit

Permalink
remove multiphase mock prover
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenfeizhang committed Dec 13, 2023
1 parent 3d44389 commit 45883f2
Showing 1 changed file with 44 additions and 107 deletions.
151 changes: 44 additions & 107 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,22 +478,12 @@ impl<F: Field> InstanceValue<F> {
}
}

impl<'a, F: Field> MockProver<'a, F> {
fn in_phase<P: Phase>(&self, phase: P) -> bool {
self.current_phase == phase.to_sealed()
}
}

impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
fn enter_region<NR, N>(&mut self, name: N)
where
NR: Into<String>,
N: FnOnce() -> NR,
{
if !self.in_phase(FirstPhase) {
return;
}

assert!(self.current_region.is_none());
self.current_region = Some(Region {
name: name().into(),
Expand All @@ -507,10 +497,6 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
}

fn exit_region(&mut self) {
if !self.in_phase(FirstPhase) {
return;
}

self.regions.push(self.current_region.take().unwrap());
}

Expand All @@ -519,10 +505,6 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
if !self.in_phase(FirstPhase) {
return;
}

if let Some(region) = self.current_region.as_mut() {
region
.annotations
Expand All @@ -535,18 +517,10 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
if !self.in_phase(FirstPhase) {
return Ok(());
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

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

if !self.rw_rows.contains(&row) {
return Err(Error::InvalidRange(
row,
Expand Down Expand Up @@ -700,20 +674,15 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
column: Column<Instance>,
row: usize,
) -> Result<circuit::Value<F>, Error> {
assert!(
self.usable_rows.contains(&row),
"row={}, usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

Ok(self
.instance
self.instance
.get(column.index())
.and_then(|column| column.get(row))
.map(|v| circuit::Value::known(v.value()))
.expect("bound failure"))
.ok_or(Error::BoundsFailure)
}

fn assign_advice<V, VR, A, AR>(
Expand All @@ -734,22 +703,8 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
return Ok(());
}

if self.in_phase(FirstPhase) {
assert!(
self.usable_rows.contains(&row),
"row={}, usable_rows={:?}, k={}",
row,
self.usable_rows,
self.k,
);
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();
}
if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
}

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

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,14 +745,11 @@ 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 746 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:746:40 | 746 | 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:149:24 | 149 | impl<F: Group + Field> From<Assigned<F>> for CellValue<F> { | ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^ help: consider further restricting this bound | 481 | impl<'a, F: Field + group::Group> Assignment<F> for MockProver<'a, F> { | ++++++++++++++

if self.in_phase(column.column_type().phase) {
*self
.advice
.get_mut(column.index())
.and_then(|v| v.get_mut(row - self.rw_rows.start))
.expect("bounds failure") = assigned;
}

*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 +783,10 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
A: FnOnce() -> AR,
AR: Into<String>,
{
if !self.in_phase(FirstPhase) {
return Ok(());
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,
);

if !self.rw_rows.contains(&row) {
return Err(Error::InvalidRange(
row,
Expand Down Expand Up @@ -881,19 +834,10 @@ 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(());
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,
);

match self.permutation.as_mut() {
Some(permutation) => permutation.copy(left_column, left_row, right_column, right_row),
None => {
Expand Down Expand Up @@ -921,18 +865,10 @@ 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(());
if !self.usable_rows.contains(&from_row) {
return Err(Error::not_enough_rows_available(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 +981,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 +1004,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,15 +1063,16 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
#[cfg(not(feature = "phase-check"))]
{
let syn_time = Instant::now();
for current_phase in prover.cs.phases() {
prover.current_phase = current_phase;
ConcreteCircuit::FloorPlanner::synthesize(
&mut prover,
circuit,
config.clone(),
constants.clone(),
)?;
}
ConcreteCircuit::FloorPlanner::synthesize(&mut prover, circuit, config, constants)?;
// for current_phase in prover.cs.phases() {
// prover.current_phase = current_phase;
// ConcreteCircuit::FloorPlanner::synthesize(
// &mut prover,
// circuit,
// config.clone(),
// constants.clone(),
// )?;
// }
log::info!("MockProver synthesize took {:?}", syn_time.elapsed());
}

Expand Down

0 comments on commit 45883f2

Please sign in to comment.