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

Feat/structural witin add #740

Merged
merged 36 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
87899de
init structural witin
hero78119 Dec 6, 2024
2679755
skip structural witin commitment & PCS(#654)
10to4 Dec 10, 2024
2c21443
skip structural witin commitment & PCS(#654):
10to4 Dec 12, 2024
9c9f4a5
Merge branch 'master' into feat/structural_witin_add
10to4 Dec 12, 2024
e9d24ab
Upgrade `pprof` (#706)
matthiasgoergens Dec 6, 2024
357595b
Update toolchain (#707)
matthiasgoergens Dec 7, 2024
614f385
Turn Transcript into trait (#692)
noel2004 Dec 9, 2024
774bab5
Further break down e2e logic (#703)
mcalancea Dec 9, 2024
be59026
Simplify SLTI tests (#710)
matthiasgoergens Dec 10, 2024
2c566e2
[FIX] Broken integration test caused by transcript (#715) (#721)
noel2004 Dec 10, 2024
2a35302
Enable debug assertions in integration tests (#714)
matthiasgoergens Dec 10, 2024
a45eced
Remove redundant format Make targets (#724)
matthiasgoergens Dec 10, 2024
cf15c14
Rename `mod divu` to `mod div` (#733)
matthiasgoergens Dec 11, 2024
e28e557
Introduce `Value::as_i32` (#732)
matthiasgoergens Dec 12, 2024
f3566bc
Remove broken features `non_pow2_rayon_thread` and `riv64` (#723)
matthiasgoergens Dec 12, 2024
5077eb1
Rename `AssertLTConfig` to `AssertLtConfig` (#731)
matthiasgoergens Dec 12, 2024
c07fa00
Remove unused and untested `u128`/`i128` implementations (#737)
matthiasgoergens Dec 12, 2024
1812068
Merge branch 'feat/structural_witin_add' of https://github.com/10to4/…
10to4 Dec 12, 2024
55e3b6b
skip structural witin commitment & PCS(#654):
10to4 Dec 12, 2024
65869f1
skip structural witin commitment & PCS(#654):
10to4 Dec 12, 2024
92687a4
skip structural witin commitment & PCS(#654):
10to4 Dec 16, 2024
3c9b862
refactor and support structural witness evaluation
hero78119 Dec 17, 2024
1f06873
cleanup multi-factor backend concept from ram circuit frontend
hero78119 Dec 17, 2024
601cc48
Merge pull request #1 from hero78119/feat/structural_witin_add
10to4 Dec 18, 2024
77b1887
skip structural witin commitment & PCS(#654):
10to4 Dec 20, 2024
103d82c
skip structural witin commitment & PCS(#654):
10to4 Dec 23, 2024
c040d45
Merge branch 'master' into feat/structural_witin_add
10to4 Dec 23, 2024
c05d3e2
skip structural witin commitment & PCS(#654):
10to4 Dec 23, 2024
669ae05
Merge branch 'master' into feat/structural_witin_add
10to4 Dec 24, 2024
e8eed3c
skip structural witin commitment & PCS(#654):
10to4 Dec 24, 2024
acf0d9f
skip structural witin commitment & PCS(#654):
10to4 Dec 24, 2024
88cd547
Less cloning
matthiasgoergens Dec 26, 2024
689691e
Merge branch 'matthias/less-cloning-2' into matthias/feat/structural_…
matthiasgoergens Dec 26, 2024
c60475b
Remove unnecessary wrapping in Expression
matthiasgoergens Dec 26, 2024
0601b4b
Unwrap instead of unreachable
matthiasgoergens Dec 26, 2024
606e1bf
Doc comment
matthiasgoergens Dec 26, 2024
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
10 changes: 9 additions & 1 deletion ceno_zkvm/src/chip_handler/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ff_ext::ExtensionField;
use crate::{
circuit_builder::{CircuitBuilder, ConstraintSystem, SetTableSpec},
error::ZKVMError,
expression::{Expression, Fixed, Instance, ToExpr, WitIn},
expression::{Expression, Fixed, Instance, StructuralWitIn, ToExpr, WitIn},
instructions::riscv::constants::{
END_CYCLE_IDX, END_PC_IDX, EXIT_CODE_IDX, INIT_CYCLE_IDX, INIT_PC_IDX, PUBLIC_IO_IDX,
UINT_LIMBS,
Expand All @@ -28,6 +28,14 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
self.cs.create_witin(name_fn)
}

pub fn create_structural_witin<NR, N>(&mut self, name_fn: N) -> StructuralWitIn
where
NR: Into<String>,
N: FnOnce() -> NR,
{
self.cs.create_structural_witin(name_fn)
}

pub fn create_fixed<NR, N>(&mut self, name_fn: N) -> Result<Fixed, ZKVMError>
where
NR: Into<String>,
Expand Down
23 changes: 21 additions & 2 deletions ceno_zkvm/src/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
ROMType,
chip_handler::utils::rlc_chip_record,
error::ZKVMError,
expression::{Expression, Fixed, Instance, WitIn},
expression::{Expression, Fixed, Instance, WitIn, StructuralWitIn},
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
witness::RowMajorMatrix,
};
Expand Down Expand Up @@ -106,10 +106,12 @@ pub struct SetTableExpression<E: ExtensionField> {
pub struct ConstraintSystem<E: ExtensionField> {
pub(crate) ns: NameSpace,

// pub platform: Platform,
pub num_witin: WitnessId,
pub witin_namespace_map: Vec<String>,

pub num_structural_witin: WitnessId,
pub structural_witin_namespace_map: Vec<String>,

pub num_fixed: usize,
pub fixed_namespace_map: Vec<String>,

Expand Down Expand Up @@ -166,6 +168,8 @@ impl<E: ExtensionField> ConstraintSystem<E> {
num_witin: 0,
// platform,
witin_namespace_map: vec![],
num_structural_witin: 0,
structural_witin_namespace_map: vec![],
num_fixed: 0,
fixed_namespace_map: vec![],
ns: NameSpace::new(root_name_fn),
Expand Down Expand Up @@ -239,6 +243,21 @@ impl<E: ExtensionField> ConstraintSystem<E> {
wit_in
}

pub fn create_structural_witin<NR: Into<String>, N: FnOnce() -> NR>(&mut self, n: N) -> StructuralWitIn{
let wit_in = StructuralWitIn {
id: {
let id = self.num_structural_witin;
self.num_structural_witin = self.num_structural_witin.strict_add(1);
id
},
};

let path = self.ns.compute_path(n().into());
self.witin_namespace_map.push(path);

wit_in
}

pub fn create_fixed<NR: Into<String>, N: FnOnce() -> NR>(
&mut self,
n: N,
Expand Down
84 changes: 74 additions & 10 deletions ceno_zkvm/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use crate::{
pub enum Expression<E: ExtensionField> {
/// WitIn(Id)
WitIn(WitnessId),
/// StructuralWitIn(WitnessId)
/// same as witin, but it's structual witness and be evaluated by verifier succinctly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have a bit of a hard time understanding what you are trying to say.

Could you please fix the grammar? That might already help.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

StructuralWitIn(WitnessId),
/// This multi-linear polynomial is known at the setup/keygen phase.
Fixed(Fixed),
/// Public Values
Expand Down Expand Up @@ -57,6 +60,7 @@ impl<E: ExtensionField> Expression<E> {
match self {
Expression::Fixed(_) => 1,
Expression::WitIn(_) => 1,
Expression::StructuralWitIn(_) => 1,
Expression::Instance(_) => 0,
Expression::Constant(_) => 0,
Expression::Sum(a_expr, b_expr) => max(a_expr.degree(), b_expr.degree()),
Expand All @@ -71,6 +75,7 @@ impl<E: ExtensionField> Expression<E> {
&self,
fixed_in: &impl Fn(&Fixed) -> T,
wit_in: &impl Fn(WitnessId) -> T, // witin id
structural_wit_in: &impl Fn(WitnessId) -> T,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arguments to this function are getting quite involved, I wonder whether we should make a struct to hold all the arguments.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, it doesn't seem to need other arguments yet.

constant: &impl Fn(E::BaseField) -> T,
challenge: &impl Fn(ChallengeId, usize, E, E) -> T,
sum: &impl Fn(T, T) -> T,
Expand All @@ -80,6 +85,7 @@ impl<E: ExtensionField> Expression<E> {
self.evaluate_with_instance(
fixed_in,
wit_in,
structural_wit_in,
&|_| unreachable!(),
constant,
challenge,
Expand All @@ -94,6 +100,7 @@ impl<E: ExtensionField> Expression<E> {
&self,
fixed_in: &impl Fn(&Fixed) -> T,
wit_in: &impl Fn(WitnessId) -> T, // witin id
structural_wit_in: &impl Fn(WitnessId) -> T,
instance: &impl Fn(Instance) -> T,
constant: &impl Fn(E::BaseField) -> T,
challenge: &impl Fn(ChallengeId, usize, E, E) -> T,
Expand All @@ -104,35 +111,36 @@ impl<E: ExtensionField> Expression<E> {
match self {
Expression::Fixed(f) => fixed_in(f),
Expression::WitIn(witness_id) => wit_in(*witness_id),
Expression::StructuralWitIn(witness_id) => structural_wit_in(*witness_id),
Expression::Instance(i) => instance(*i),
Expression::Constant(scalar) => constant(*scalar),
Expression::Sum(a, b) => {
let a = a.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
let b = b.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
sum(a, b)
}
Expression::Product(a, b) => {
let a = a.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
let b = b.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
product(a, b)
}
Expression::ScaledSum(x, a, b) => {
let x = x.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
let a = a.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
let b = b.evaluate_with_instance(
fixed_in, wit_in, instance, constant, challenge, sum, product, scaled,
fixed_in, wit_in, structural_wit_in, instance, constant, challenge, sum, product, scaled,
);
scaled(x, a, b)
}
Expand Down Expand Up @@ -161,6 +169,7 @@ impl<E: ExtensionField> Expression<E> {
match expr {
Expression::Fixed(_) => false,
Expression::WitIn(_) => false,
Expression::StructuralWitIn(_) => false,
Expression::Instance(_) => false,
Expression::Constant(c) => *c == E::BaseField::ZERO,
Expression::Sum(a, b) => Self::is_zero_expr(a) && Self::is_zero_expr(b),
Expand All @@ -177,6 +186,7 @@ impl<E: ExtensionField> Expression<E> {
(
Expression::Fixed(_)
| Expression::WitIn(_)
| Expression::StructuralWitIn(_)
| Expression::Challenge(..)
| Expression::Constant(_)
| Expression::Instance(_),
Expand Down Expand Up @@ -207,7 +217,7 @@ impl<E: ExtensionField> Neg for Expression<E> {
type Output = Expression<E>;
fn neg(self) -> Self::Output {
match self {
Expression::Fixed(_) | Expression::WitIn(_) | Expression::Instance(_) => {
Expression::Fixed(_) | Expression::WitIn(_) | Expression::StructuralWitIn(_) | Expression::Instance(_) => {
Expression::ScaledSum(
Box::new(self),
Box::new(Expression::Constant(E::BaseField::ONE.neg())),
Expand Down Expand Up @@ -726,6 +736,10 @@ pub struct WitIn {
pub id: WitnessId,
}

#[derive(Clone, Debug, Copy)]
pub struct StructuralWitIn {
pub id: WitnessId,
}
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Fixed(pub usize);

Expand Down Expand Up @@ -765,6 +779,39 @@ impl WitIn {
}
}

impl StructuralWitIn {
pub fn from_expr<E: ExtensionField, N, NR>(
name: N,
circuit_builder: &mut CircuitBuilder<E>,
input: Expression<E>,
debug: bool,
) -> Result<Self, ZKVMError>
where
NR: Into<String> + Clone,
N: FnOnce() -> NR,
{
circuit_builder.namespace(
|| "from_expr",
|cb| {
let name = name().into();
let wit = cb.create_structural_witin(|| name.clone());
if !debug {
cb.require_zero(|| name.clone(), wit.expr() - input)?;
}
Ok(wit)
},
)
}

pub fn assign<E: ExtensionField>(
&self,
instance: &mut [MaybeUninit<E::BaseField>],
value: E::BaseField,
) {
instance[self.id as usize] = MaybeUninit::new(value);
}
}

pub trait ToExpr<E: ExtensionField> {
type Output;
fn expr(&self) -> Self::Output;
Expand All @@ -784,6 +831,20 @@ impl<E: ExtensionField> ToExpr<E> for &WitIn {
}
}

impl<E: ExtensionField> ToExpr<E> for StructuralWitIn {
type Output = Expression<E>;
fn expr(&self) -> Expression<E> {
Expression::StructuralWitIn(self.id)
}
}

impl<E: ExtensionField> ToExpr<E> for &StructuralWitIn {
type Output = Expression<E>;
fn expr(&self) -> Expression<E> {
Expression::StructuralWitIn(self.id)
}
}

impl<E: ExtensionField> ToExpr<E> for Fixed {
type Output = Expression<E>;
fn expr(&self) -> Expression<E> {
Expand Down Expand Up @@ -823,8 +884,8 @@ macro_rules! impl_from_via_ToExpr {
)*
};
}
impl_from_via_ToExpr!(WitIn, Fixed, Instance);
impl_from_via_ToExpr!(&WitIn, &Fixed, &Instance);
impl_from_via_ToExpr!(WitIn, Fixed, StructuralWitIn, Instance);
impl_from_via_ToExpr!(&WitIn, &Fixed, &StructuralWitIn, &Instance);

// Implement From trait for unsigned types of at most 64 bits
macro_rules! impl_from_unsigned {
Expand Down Expand Up @@ -886,6 +947,9 @@ pub mod fmt {
}
format!("WitIn({})", wit_in)
}
Expression::StructuralWitIn(wit_in) => {
format!("StructuralWitIn({})", wit_in)
}
Expression::Challenge(id, pow, scaler, offset) => {
if *pow == 1 && *scaler == 1.into() && *offset == 0.into() {
format!("Challenge({})", id)
Expand Down
4 changes: 2 additions & 2 deletions ceno_zkvm/src/expression/monomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<E: ExtensionField> Expression<E> {
}]
}

Fixed(_) | WitIn(_) | Instance(_) | Challenge(..) => {
Fixed(_) | WitIn(_) | StructuralWitIn(_) | Instance(_) | Challenge(..) => {
vec![Term {
coeff: Expression::ONE,
vars: vec![self.clone()],
Expand Down Expand Up @@ -146,6 +146,6 @@ mod tests {
E::random(&mut rng),
E::random(&mut rng),
];
move |expr: &Expression<E>| eval_by_expr_with_fixed(&fixed, &witnesses, &challenges, expr)
move |expr: &Expression<E>| eval_by_expr_with_fixed(&fixed, &witnesses, &[], &challenges, expr)
}
}
Loading
Loading