Skip to content

Commit

Permalink
Merge branch 'main' into gr@verify-api-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
guorong009 committed Sep 26, 2024
2 parents c4bfe60 + 7c368af commit 6061bf6
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: crate-ci/typos@v1.22.7
- uses: crate-ci/typos@v1.24.6
with:
config: ./typos.toml
isolated: true
Expand Down
18 changes: 14 additions & 4 deletions halo2_frontend/src/circuit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits and structs for implementing circuit components.
use crate::plonk::{self, AssignmentError};
use crate::plonk::{self, AssignError};
use crate::plonk::{
permutation,
sealed::{self, SealedPhase},
Expand Down Expand Up @@ -154,7 +154,7 @@ impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {

fn query_instance(&self, column: Column<Instance>, row: usize) -> Result<Value<F>, Error> {
if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::QueryInstance {
return Err(Error::AssignError(AssignError::QueryInstance {
col: column.into(),
row,
usable_rows: (0, self.usable_rows.end),
Expand Down Expand Up @@ -189,7 +189,7 @@ impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {
}

if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::AssignAdvice {
return Err(Error::AssignError(AssignError::AssignAdvice {
desc: desc().into(),
col: column.into(),
row,
Expand All @@ -198,11 +198,21 @@ impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {
}));
}

let value = match to().into_field().assign() {
Ok(v) => v,
Err(_) => {
return Err(Error::AssignError(AssignError::WitnessMissing {
func: "assign_advice".to_string(),
desc: desc().into(),
}))
}
};

*self
.advice
.get_mut(column.index())
.and_then(|v| v.get_mut(row))
.ok_or(Error::BoundsFailure)? = to().into_field().assign()?;
.ok_or(Error::BoundsFailure)? = value;

Ok(())
}
Expand Down
33 changes: 23 additions & 10 deletions halo2_frontend/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::ops::{Add, Mul, Neg, Range};

use blake2b_simd::blake2b;

use crate::plonk::AssignmentError;
use crate::plonk::AssignError;
use crate::{
circuit,
plonk::{
Expand Down Expand Up @@ -415,7 +415,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
}

if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::EnableSelector {
return Err(Error::AssignError(AssignError::EnableSelector {
desc: desc().into(),
selector: *selector,
row,
Expand Down Expand Up @@ -445,7 +445,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
row: usize,
) -> Result<circuit::Value<F>, Error> {
if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::QueryInstance {
return Err(Error::AssignError(AssignError::QueryInstance {
col: column.into(),
row,
usable_rows: (self.usable_rows.start, self.usable_rows.end),
Expand Down Expand Up @@ -476,7 +476,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
{
if self.in_phase(FirstPhase) {
if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::AssignAdvice {
return Err(Error::AssignError(AssignError::AssignAdvice {
desc: desc().into(),
col: column.into(),
row,
Expand Down Expand Up @@ -504,11 +504,14 @@ impl<F: Field> Assignment<F> for MockProver<F> {
.expect("bounds failure");
*value = CellValue::Assigned(to);
}
Err(err) => {
Err(_) => {
// Propagate `assign` error if the column is in current phase.
let phase = self.cs.advice_column_phase[column.index];
if self.in_phase(phase) {
return Err(err);
return Err(Error::AssignError(AssignError::WitnessMissing {
func: "assign_advice".to_string(),
desc: desc().into(),
}));
}
}
}
Expand All @@ -534,7 +537,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
}

if !self.usable_rows.contains(&row) {
return Err(Error::AssignmentError(AssignmentError::AssignFixed {
return Err(Error::AssignError(AssignError::AssignFixed {
desc: desc().into(),
col: column.into(),
row,
Expand All @@ -552,11 +555,21 @@ impl<F: Field> Assignment<F> for MockProver<F> {
.or_default();
}

let value = match to().into_field().evaluate().assign() {
Ok(v) => CellValue::Assigned(v),
Err(_) => {
return Err(Error::AssignError(AssignError::WitnessMissing {
func: "assign_fixed".to_string(),
desc: desc().into(),
}))
}
};

*self
.fixed
.get_mut(column.index())
.and_then(|v| v.get_mut(row))
.expect("bounds failure") = CellValue::Assigned(to().into_field().evaluate().assign()?);
.expect("bounds failure") = value;

Ok(())
}
Expand All @@ -573,7 +586,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
}

if !self.usable_rows.contains(&left_row) || !self.usable_rows.contains(&right_row) {
return Err(Error::AssignmentError(AssignmentError::Copy {
return Err(Error::AssignError(AssignError::Copy {
left_col: left_column,
left_row,
right_col: right_column,
Expand All @@ -598,7 +611,7 @@ impl<F: Field> Assignment<F> for MockProver<F> {
}

if !self.usable_rows.contains(&from_row) {
return Err(Error::AssignmentError(AssignmentError::FillFromRow {
return Err(Error::AssignError(AssignError::FillFromRow {
col: col.into(),
from_row,
usable_rows: (self.usable_rows.start, self.usable_rows.end),
Expand Down
29 changes: 17 additions & 12 deletions halo2_frontend/src/plonk/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use halo2_middleware::circuit::Any;

/// This is an error that could occur during circuit synthesis.
///
/// **NOTE**: [`AssignmentError`] is introduced to provide more debugging info
/// **NOTE**: [`AssignError`] is introduced to provide more debugging info
/// to developers when assigning witnesses to circuit cells.
/// Hence, they are used for [`MockProver`] and [`WitnessCollection`].
/// The [`keygen`] process use the [`NotEnoughRowsAvailable`], since it is just enough.
Expand All @@ -32,8 +32,8 @@ pub enum Error {
ColumnNotInPermutation(Column<Any>),
/// An error relating to a lookup table.
TableError(TableError),
/// An error relating to an `Assignment`.
AssignmentError(AssignmentError),
/// An error relating to a circuit assignment.
AssignError(AssignError),
/// Generic error not covered by previous cases
Other(String),
}
Expand Down Expand Up @@ -65,7 +65,7 @@ impl fmt::Display for Error {
"Column {column:?} must be included in the permutation. Help: try applying `meta.enable_equalty` on the column",
),
Error::TableError(error) => write!(f, "{error}"),
Error::AssignmentError(error) => write!(f, "{error}"),
Error::AssignError(error) => write!(f, "{error}"),
Error::Other(error) => write!(f, "Other: {error}"),
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl fmt::Display for TableError {

/// This is an error that could occur during `assign_advice`, `assign_fixed`, `copy`, etc.
#[derive(Debug)]
pub enum AssignmentError {
pub enum AssignError {
AssignAdvice {
desc: String,
col: Column<Any>,
Expand Down Expand Up @@ -154,12 +154,16 @@ pub enum AssignmentError {
usable_rows: (usize, usize),
k: u32,
},
WitnessMissing {
func: String,
desc: String,
},
}

impl fmt::Display for AssignmentError {
impl fmt::Display for AssignError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AssignmentError::AssignAdvice { desc, col, row, usable_rows:(start, end), k } => write!(
AssignError::AssignAdvice { desc, col, row, usable_rows:(start, end), k } => write!(
f,
"assign_advice `{}` error: column={:?}({}), row={}, usable_rows={}..{}, k={}",
desc,
Expand All @@ -169,7 +173,7 @@ impl fmt::Display for AssignmentError {
start, end,
k,
),
AssignmentError::AssignFixed {desc, col, row, usable_rows: (start, end), k } => write!(
AssignError::AssignFixed {desc, col, row, usable_rows: (start, end), k } => write!(
f,
"assign_fixed `{}` error: column={:?}({}), row={}, usable_rows={}..{}, k={}",
desc,
Expand All @@ -179,7 +183,7 @@ impl fmt::Display for AssignmentError {
start, end,
k,
),
AssignmentError::EnableSelector { desc, selector, row, usable_rows: (start, end), k } => write!(
AssignError::EnableSelector { desc, selector, row, usable_rows: (start, end), k } => write!(
f,
"enable_selector `{}` error: column=Selector({:?}), row={}, usable_rows={}..{}, k={}",
desc,
Expand All @@ -188,7 +192,7 @@ impl fmt::Display for AssignmentError {
start, end,
k,
),
AssignmentError::QueryInstance { col, row, usable_rows:(start, end), k } => write!(
AssignError::QueryInstance { col, row, usable_rows:(start, end), k } => write!(
f,
"query_instance error: column={:?}({}), row={}, usable_rows={}..{}, k={}",
col.column_type,
Expand All @@ -198,7 +202,7 @@ impl fmt::Display for AssignmentError {
end,
k,
),
AssignmentError::Copy { left_col, left_row, right_col, right_row, usable_rows:(start, end), k } => write!(
AssignError::Copy { left_col, left_row, right_col, right_row, usable_rows:(start, end), k } => write!(
f,
"copy error: left_column={:?}({}), left_row={}, right_column={:?}({}), right_row={}, usable_rows={}..{}, k={}",
left_col.column_type(),
Expand All @@ -210,7 +214,7 @@ impl fmt::Display for AssignmentError {
start, end,
k,
),
AssignmentError::FillFromRow { col, from_row, usable_rows:(start, end), k } => write!(
AssignError::FillFromRow { col, from_row, usable_rows:(start, end), k } => write!(
f,
"fill_from_row error: column={:?}({}), from_row={}, usable_rows={}..{}, k={}",
col.column_type(),
Expand All @@ -219,6 +223,7 @@ impl fmt::Display for AssignmentError {
start, end,
k,
),
AssignError::WitnessMissing { func, desc } => write!(f, "witness missing/unknown when {} `{}`", func, desc),
}
}
}
27 changes: 23 additions & 4 deletions halo2_frontend/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use halo2_middleware::ff::Field;

use crate::circuit::Value;
use crate::plonk::{
permutation, Advice, Assigned, Assignment, Challenge, Column, Error, Fixed, Instance, Selector,
permutation, Advice, AssignError, Assigned, Assignment, Challenge, Column, Error, Fixed,
Instance, Selector,
};

/// Assembly to be used in circuit synthesis.
Expand Down Expand Up @@ -75,7 +76,7 @@ impl<F: Field> Assignment<F> for Assembly<F> {

fn assign_fixed<V, VR, A, AR>(
&mut self,
_: A,
desc: A,
column: Column<Fixed>,
row: usize,
to: V,
Expand All @@ -90,11 +91,21 @@ impl<F: Field> Assignment<F> for Assembly<F> {
return Err(Error::not_enough_rows_available(self.k));
}

let value = match to().into_field().assign() {
Ok(v) => v,
Err(_) => {
return Err(Error::AssignError(AssignError::WitnessMissing {
func: "assign_fixed".to_string(),
desc: desc().into(),
}))
}
};

*self
.fixed
.get_mut(column.index())
.and_then(|v| v.get_mut(row))
.ok_or(Error::BoundsFailure)? = to().into_field().assign()?;
.ok_or(Error::BoundsFailure)? = value;

Ok(())
}
Expand Down Expand Up @@ -129,7 +140,15 @@ impl<F: Field> Assignment<F> for Assembly<F> {
.get_mut(column.index())
.ok_or(Error::BoundsFailure)?;

let filler = to.assign()?;
let filler = match to.assign() {
Ok(v) => v,
Err(_) => {
return Err(Error::AssignError(AssignError::WitnessMissing {
func: "fill_from_row".to_string(),
desc: "".to_string(),
}))
}
};
for row in self.usable_rows.clone().skip(from_row) {
col[row] = filler;
}
Expand Down

0 comments on commit 6061bf6

Please sign in to comment.