Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iquerejeta/pruning #17

Merged
merged 20 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/shuffle.rs
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {}

// use ff::{BatchInvert, FromUniformBytes, WithSmallOrderMulGroup};
// use ff::{BatchInvert, FromUniformBytes};
// use halo2_proofs::arithmetic::CurveExt;
// use halo2_proofs::helpers::SerdeCurveAffine;
// use halo2_proofs::poly::kzg::params::{KZGCommitmentScheme, ParamsKZG};
Expand Down
2 changes: 1 addition & 1 deletion examples/vector-ops-unblinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {}
// /// This is a simple example of how to use unblinded inputs to match up circuits that might be proved on different host machines.
// use std::marker::PhantomData;
//
// use ff::{FromUniformBytes, WithSmallOrderMulGroup};
// use ff::{FromUniformBytes};
// use halo2_proofs::arithmetic::CurveExt;
// use halo2_proofs::helpers::SerdeCurveAffine;
// use halo2_proofs::poly::kzg::params::{KZGCommitmentScheme, ParamsKZG};
Expand Down
23 changes: 11 additions & 12 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::{fmt, marker::PhantomData};

use ff::Field;

use crate::plonk::{
Advice, Any, Assigned, Challenge, Column, Error, Fixed, Instance, Selector, TableColumn,
};
use crate::plonk::{Advice, Any, Challenge, Column, Error, Fixed, Instance, Selector, TableColumn};

mod value;
pub use value::Value;
Expand All @@ -17,6 +15,7 @@ pub use floor_planner::single_pass::SimpleFloorPlanner;
pub mod layouter;
mod table_layouter;

use crate::rational::Rational;
pub use table_layouter::{SimpleTableLayouter, TableLayouter};

/// A chip implements a set of instructions that can be used by gadgets.
Expand Down Expand Up @@ -119,15 +118,15 @@ impl<V, F: Field> AssignedCell<V, F> {

impl<V, F: Field> AssignedCell<V, F>
where
for<'v> Assigned<F>: From<&'v V>,
for<'v> Rational<F>: From<&'v V>,
{
/// Returns the field element value of the [`AssignedCell`].
pub fn value_field(&self) -> Value<Assigned<F>> {
pub fn value_field(&self) -> Value<Rational<F>> {
self.value.to_field()
}
}

impl<F: Field> AssignedCell<Assigned<F>, F> {
impl<F: Field> AssignedCell<Rational<F>, F> {
/// Evaluates this assigned cell's value directly, performing an unbatched inversion
/// if necessary.
///
Expand All @@ -143,7 +142,7 @@ impl<F: Field> AssignedCell<Assigned<F>, F> {

impl<V: Clone, F: Field> AssignedCell<V, F>
where
for<'v> Assigned<F>: From<&'v V>,
for<'v> Rational<F>: From<&'v V>,
{
/// Copies the value to a given advice cell and constrains them to be equal.
///
Expand Down Expand Up @@ -232,7 +231,7 @@ impl<'r, F: Field> Region<'r, F> {
) -> Result<AssignedCell<VR, F>, Error>
where
V: FnMut() -> Value<VR> + 'v,
for<'vr> Assigned<F>: From<&'vr VR>,
for<'vr> Rational<F>: From<&'vr VR>,
A: Fn() -> AR,
AR: Into<String>,
{
Expand Down Expand Up @@ -267,7 +266,7 @@ impl<'r, F: Field> Region<'r, F> {
constant: VR,
) -> Result<AssignedCell<VR, F>, Error>
where
for<'vr> Assigned<F>: From<&'vr VR>,
for<'vr> Rational<F>: From<&'vr VR>,
A: Fn() -> AR,
AR: Into<String>,
{
Expand Down Expand Up @@ -341,7 +340,7 @@ impl<'r, F: Field> Region<'r, F> {
) -> Result<AssignedCell<VR, F>, Error>
where
V: FnMut() -> Value<VR> + 'v,
for<'vr> Assigned<F>: From<&'vr VR>,
for<'vr> Rational<F>: From<&'vr VR>,
A: Fn() -> AR,
AR: Into<String>,
{
Expand All @@ -367,7 +366,7 @@ impl<'r, F: Field> Region<'r, F> {
/// Returns an error if the cell is in a column where equality has not been enabled.
pub fn constrain_constant<VR>(&mut self, cell: Cell, constant: VR) -> Result<(), Error>
where
VR: Into<Assigned<F>>,
VR: Into<Rational<F>>,
{
self.region.constrain_constant(cell, constant.into())
}
Expand Down Expand Up @@ -408,7 +407,7 @@ impl<'r, F: Field> Table<'r, F> {
) -> Result<(), Error>
where
V: FnMut() -> Value<VR> + 'v,
VR: Into<Assigned<F>>,
VR: Into<Rational<F>>,
A: Fn() -> AR,
AR: Into<String>,
{
Expand Down
15 changes: 8 additions & 7 deletions src/circuit/floor_planner/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ use std::marker::PhantomData;

use ff::Field;

use crate::rational::Rational;
use crate::{
circuit::{
layouter::{RegionColumn, RegionLayouter, RegionShape, SyncDeps, TableLayouter},
table_layouter::{compute_table_lengths, SimpleTableLayouter},
Cell, Layouter, Region, RegionIndex, RegionStart, Table, Value,
},
plonk::{
Advice, Any, Assigned, Assignment, Challenge, Circuit, Column, Error, Fixed, FloorPlanner,
Instance, Selector, TableColumn,
Advice, Any, Assignment, Challenge, Circuit, Column, Error, Fixed, FloorPlanner, Instance,
Selector, TableColumn,
},
};

Expand Down Expand Up @@ -225,7 +226,7 @@ struct SingleChipLayouterRegion<'r, 'a, F: Field, CS: Assignment<F> + 'a> {
layouter: &'r mut SingleChipLayouter<'a, F, CS>,
region_index: RegionIndex,
/// Stores the constants to be assigned, and the cells to which they are copied.
constants: Vec<(Assigned<F>, Cell)>,
constants: Vec<(Rational<F>, Cell)>,
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug
Expand Down Expand Up @@ -278,7 +279,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.layouter.cs.assign_advice(
annotation,
Expand All @@ -299,7 +300,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
constant: Assigned<F>,
constant: Rational<F>,
) -> Result<Cell, Error> {
let advice =
self.assign_advice(annotation, column, offset, &mut || Value::known(constant))?;
Expand Down Expand Up @@ -343,7 +344,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Fixed>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.layouter.cs.assign_fixed(
annotation,
Expand All @@ -359,7 +360,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
})
}

fn constrain_constant(&mut self, cell: Cell, constant: Assigned<F>) -> Result<(), Error> {
fn constrain_constant(&mut self, cell: Cell, constant: Rational<F>) -> Result<(), Error> {
self.constants.push((constant, cell));
Ok(())
}
Expand Down
15 changes: 8 additions & 7 deletions src/circuit/floor_planner/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ use std::fmt;

use ff::Field;

use crate::rational::Rational;
use crate::{
circuit::{
layouter::{RegionColumn, RegionLayouter, RegionShape, SyncDeps, TableLayouter},
table_layouter::{compute_table_lengths, SimpleTableLayouter},
Cell, Layouter, Region, RegionIndex, RegionStart, Table, Value,
},
plonk::{
Advice, Any, Assigned, Assignment, Challenge, Circuit, Column, Error, Fixed, FloorPlanner,
Instance, Selector, TableColumn,
Advice, Any, Assignment, Challenge, Circuit, Column, Error, Fixed, FloorPlanner, Instance,
Selector, TableColumn,
},
};

Expand All @@ -32,7 +33,7 @@ struct V1Plan<'a, F: Field, CS: Assignment<F> + 'a> {
/// Stores the starting row for each region.
regions: Vec<RegionStart>,
/// Stores the constants to be assigned, and the cells to which they are copied.
constants: Vec<(Assigned<F>, Cell)>,
constants: Vec<(Rational<F>, Cell)>,
/// Stores the table fixed columns.
table_columns: Vec<TableColumn>,
}
Expand Down Expand Up @@ -386,7 +387,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + SyncDeps> RegionLayouter<F> for V1Reg
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.plan.cs.assign_advice(
annotation,
Expand All @@ -407,7 +408,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + SyncDeps> RegionLayouter<F> for V1Reg
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
constant: Assigned<F>,
constant: Rational<F>,
) -> Result<Cell, Error> {
let advice =
self.assign_advice(annotation, column, offset, &mut || Value::known(constant))?;
Expand Down Expand Up @@ -451,7 +452,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + SyncDeps> RegionLayouter<F> for V1Reg
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Fixed>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.plan.cs.assign_fixed(
annotation,
Expand All @@ -467,7 +468,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + SyncDeps> RegionLayouter<F> for V1Reg
})
}

fn constrain_constant(&mut self, cell: Cell, constant: Assigned<F>) -> Result<(), Error> {
fn constrain_constant(&mut self, cell: Cell, constant: Rational<F>) -> Result<(), Error> {
self.plan.constants.push((constant, cell));
Ok(())
}
Expand Down
19 changes: 10 additions & 9 deletions src/circuit/layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use ff::Field;

pub use super::table_layouter::TableLayouter;
use super::{Cell, RegionIndex, Value};
use crate::plonk::{Advice, Any, Assigned, Column, Error, Fixed, Instance, Selector};
use crate::plonk::{Advice, Any, Column, Error, Fixed, Instance, Selector};
use crate::rational::Rational;

/// Intermediate trait requirements for [`RegionLayouter`] when thread-safe regions are enabled.
#[cfg(feature = "thread-safe-region")]
Expand Down Expand Up @@ -79,7 +80,7 @@ pub trait RegionLayouter<F: Field>: fmt::Debug + SyncDeps {
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error>;

/// Assigns a constant value to the column `advice` at `offset` within this region.
Expand All @@ -93,7 +94,7 @@ pub trait RegionLayouter<F: Field>: fmt::Debug + SyncDeps {
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
constant: Assigned<F>,
constant: Rational<F>,
) -> Result<Cell, Error>;

/// Assign the value of the instance column's cell at absolute location
Expand All @@ -120,13 +121,13 @@ pub trait RegionLayouter<F: Field>: fmt::Debug + SyncDeps {
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Fixed>,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error>;

/// Constrains a cell to have a constant value.
///
/// Returns an error if the cell is in a column where equality has not been enabled.
fn constrain_constant(&mut self, cell: Cell, constant: Assigned<F>) -> Result<(), Error>;
fn constrain_constant(&mut self, cell: Cell, constant: Rational<F>) -> Result<(), Error>;

/// Constraint two cells to have the same value.
///
Expand Down Expand Up @@ -226,7 +227,7 @@ impl<F: Field> RegionLayouter<F> for RegionShape {
_: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
_to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
_to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.columns.insert(Column::<Any>::from(column).into());
self.row_count = cmp::max(self.row_count, offset + 1);
Expand All @@ -243,7 +244,7 @@ impl<F: Field> RegionLayouter<F> for RegionShape {
annotation: &'v (dyn Fn() -> String + 'v),
column: Column<Advice>,
offset: usize,
constant: Assigned<F>,
constant: Rational<F>,
) -> Result<Cell, Error> {
// The rest is identical to witnessing an advice cell.
self.assign_advice(annotation, column, offset, &mut || Value::known(constant))
Expand Down Expand Up @@ -283,7 +284,7 @@ impl<F: Field> RegionLayouter<F> for RegionShape {
_: &'v (dyn Fn() -> String + 'v),
column: Column<Fixed>,
offset: usize,
_to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
_to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<Cell, Error> {
self.columns.insert(Column::<Any>::from(column).into());
self.row_count = cmp::max(self.row_count, offset + 1);
Expand All @@ -303,7 +304,7 @@ impl<F: Field> RegionLayouter<F> for RegionShape {
// Do nothing
}

fn constrain_constant(&mut self, _cell: Cell, _constant: Assigned<F>) -> Result<(), Error> {
fn constrain_constant(&mut self, _cell: Cell, _constant: Rational<F>) -> Result<(), Error> {
// Global constants don't affect the region shape.
Ok(())
}
Expand Down
9 changes: 5 additions & 4 deletions src/circuit/table_layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use std::{

use ff::Field;

use crate::plonk::{Assigned, Assignment, Error, TableColumn, TableError};
use crate::plonk::{Assignment, Error, TableColumn, TableError};
use crate::rational::Rational;

use super::Value;

Expand All @@ -25,7 +26,7 @@ pub trait TableLayouter<F: Field>: std::fmt::Debug {
annotation: &'v (dyn Fn() -> String + 'v),
column: TableColumn,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<(), Error>;
}

Expand All @@ -36,7 +37,7 @@ pub trait TableLayouter<F: Field>: std::fmt::Debug {
/// assigned.
/// - The inner `Value` tracks whether the underlying `Assignment` is evaluating
/// witnesses or not.
type DefaultTableValue<F> = Option<Value<Assigned<F>>>;
type DefaultTableValue<F> = Option<Value<Rational<F>>>;

/// A table layouter that can be used to assign values to a table.
pub struct SimpleTableLayouter<'r, 'a, F: Field, CS: Assignment<F> + 'a> {
Expand Down Expand Up @@ -74,7 +75,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> TableLayouter<F>
annotation: &'v (dyn Fn() -> String + 'v),
column: TableColumn,
offset: usize,
to: &'v mut (dyn FnMut() -> Value<Assigned<F>> + 'v),
to: &'v mut (dyn FnMut() -> Value<Rational<F>> + 'v),
) -> Result<(), Error> {
if self.used_columns.contains(&column) {
return Err(Error::TableError(TableError::UsedColumn(column)));
Expand Down
Loading