From 45883f2d2d7ca33e629444b87d79e19ff2dc1de4 Mon Sep 17 00:00:00 2001 From: zhenfei Date: Tue, 12 Dec 2023 19:17:19 -0500 Subject: [PATCH] remove multiphase mock prover --- halo2_proofs/src/dev.rs | 151 ++++++++++++---------------------------- 1 file changed, 44 insertions(+), 107 deletions(-) diff --git a/halo2_proofs/src/dev.rs b/halo2_proofs/src/dev.rs index 3273d24817..f821852b96 100644 --- a/halo2_proofs/src/dev.rs +++ b/halo2_proofs/src/dev.rs @@ -478,22 +478,12 @@ impl InstanceValue { } } -impl<'a, F: Field> MockProver<'a, F> { - fn in_phase(&self, phase: P) -> bool { - self.current_phase == phase.to_sealed() - } -} - impl<'a, F: Field> Assignment for MockProver<'a, F> { fn enter_region(&mut self, name: N) where NR: Into, N: FnOnce() -> NR, { - if !self.in_phase(FirstPhase) { - return; - } - assert!(self.current_region.is_none()); self.current_region = Some(Region { name: name().into(), @@ -507,10 +497,6 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { } fn exit_region(&mut self) { - if !self.in_phase(FirstPhase) { - return; - } - self.regions.push(self.current_region.take().unwrap()); } @@ -519,10 +505,6 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { A: FnOnce() -> AR, AR: Into, { - if !self.in_phase(FirstPhase) { - return; - } - if let Some(region) = self.current_region.as_mut() { region .annotations @@ -535,18 +517,10 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { A: FnOnce() -> AR, AR: Into, { - 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, @@ -700,20 +674,15 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { column: Column, row: usize, ) -> Result, 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( @@ -734,22 +703,8 @@ impl<'a, F: Field> Assignment 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) { @@ -762,6 +717,15 @@ impl<'a, F: Field> Assignment 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(); @@ -781,14 +745,11 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { #[cfg(feature = "mock-batch-inv")] let assigned = CellValue::from(val_res?); - 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 { @@ -822,18 +783,10 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { A: FnOnce() -> AR, AR: Into, { - 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, @@ -881,19 +834,10 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { right_column: Column, 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 => { @@ -921,18 +865,10 @@ impl<'a, F: Field> Assignment for MockProver<'a, F> { from_row: usize, to: circuit::Value>, ) -> 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)?; } @@ -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, @@ -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")] @@ -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()); }