Skip to content

Commit

Permalink
feat(MockProver): replace errors by asserts(privacy-scaling-explorati…
Browse files Browse the repository at this point in the history
  • Loading branch information
Velaciela committed Oct 25, 2023
1 parent e5acb7d commit 2b92df9
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 58 deletions.
137 changes: 86 additions & 51 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,13 +414,15 @@ impl<F: Field> Mul<F> for Value<F> {
/// }])
/// );
///
/// // If we provide a too-small K, we get an error.
/// assert!(matches!(
/// MockProver::<Fp>::run(2, &circuit, vec![]).unwrap_err(),
/// Error::NotEnoughRowsAvailable {
/// current_k,
/// } if current_k == 2,
/// ));
/// // If we provide a too-small K, we get a panic.
/// use std::panic;
/// let result = panic::catch_unwind(|| {
/// MockProver::<Fp>::run(2, &circuit, vec![]).unwrap_err()
/// });
/// assert_eq!(
/// result.unwrap_err().downcast_ref::<String>().unwrap(),
/// "n=4, minimum_rows=8, k=2"
/// );
/// ```
#[derive(Debug)]
pub struct MockProver<'a, F: Field> {
Expand Down Expand Up @@ -537,9 +539,13 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
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(
Expand Down Expand Up @@ -694,15 +700,20 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
column: Column<Instance>,
row: usize,
) -> Result<circuit::Value<F>, Error> {
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,
);

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

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

if !self.usable_rows.contains(&row) {
return Err(Error::not_enough_rows_available(self.k));
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.rw_rows.contains(&row) {
Expand All @@ -737,15 +762,6 @@ 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 Down Expand Up @@ -808,9 +824,13 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
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(
Expand Down Expand Up @@ -863,9 +883,14 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
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),
Expand Down Expand Up @@ -898,9 +923,13 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
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 @@ -947,29 +976,35 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
let config = ConcreteCircuit::configure(&mut cs);
let cs = cs;

if n < cs.minimum_rows() {
return Err(Error::not_enough_rows_available(k));
}
assert!(
n >= cs.minimum_rows(),
"n={}, minimum_rows={}, k={}",
n,
cs.minimum_rows(),
k,
);

if instance.len() != cs.num_instance_columns {
return Err(Error::InvalidInstances);
}
assert_eq!(instance.len(), cs.num_instance_columns);

let instance = instance
.into_iter()
.map(|instance| {
if instance.len() > n - (cs.blinding_factors() + 1) {
return Err(Error::InstanceTooLarge);
}
assert!(
instance.len() <= n - (cs.blinding_factors() + 1),
"instance.len={}, n={}, cs.blinding_factors={}",
instance.len(),
n,
cs.blinding_factors()
);

let mut instance_values = vec![InstanceValue::Padding; n];
for (idx, value) in instance.into_iter().enumerate() {
instance_values[idx] = InstanceValue::Assigned(value);
}

Ok(instance_values)
instance_values
})
.collect::<Result<Vec<_>, _>>()?;
.collect::<Vec<_>>();

// Fixed columns contain no blinding factors.
let fixed_vec = Arc::new(vec![vec![CellValue::Unassigned; n]; cs.num_fixed_columns]);
Expand Down Expand Up @@ -1008,10 +1043,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 @@ -1031,7 +1066,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: FirstPhase.to_sealed(),
};

#[cfg(feature = "phase-check")]
Expand Down
7 changes: 2 additions & 5 deletions halo2_proofs/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<'a, F: Field> Assignment<F> for Assembly<'a, F> {
.fixed
.get_mut(column.index())
.and_then(|v| v.get_mut(row - self.rw_rows.start))
.ok_or(Error::BoundsFailure)? = to().into_field().assign()?;
.expect("bounds failure") = to().into_field().assign()?;

Ok(())
}
Expand Down Expand Up @@ -285,10 +285,7 @@ impl<'a, F: Field> Assignment<F> for Assembly<'a, F> {
return Err(Error::not_enough_rows_available(self.k));
}

let col = self
.fixed
.get_mut(column.index())
.ok_or(Error::BoundsFailure)?;
let col = self.fixed.get_mut(column.index()).expect("bounds failure");

let filler = to.assign()?;
for row in self.usable_rows.clone().skip(from_row) {
Expand Down
10 changes: 10 additions & 0 deletions halo2_proofs/src/plonk/permutation/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ impl Assembly {
if left_row >= self.mapping[left_column].len()
|| right_row >= self.mapping[right_column].len()
{
log::error!(
"BoundsFailure: left_row: {}, right_row: {}",
left_row,
right_row
);
log::error!(
"BoundsFailure: left_max: {}, right_max: {}",
self.mapping[left_column].len(),
self.mapping[right_column].len()
);
return Err(Error::BoundsFailure);
}

Expand Down
6 changes: 4 additions & 2 deletions halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,13 @@ where
return Err(Error::not_enough_rows_available(self.k));
}

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

fn assign_advice<V, VR, A, AR>(
Expand Down Expand Up @@ -299,7 +301,7 @@ where
.advice
.get_mut(column.index())
.and_then(|v| v.get_mut(row - self.rw_rows.start))
.ok_or(Error::BoundsFailure)? = to().into_field().assign()?;
.expect("bounds failure") = to().into_field().assign()?;

Ok(())
}
Expand Down

0 comments on commit 2b92df9

Please sign in to comment.