Skip to content

Commit

Permalink
skip PCS of range table scroll-tech#789: structural fixed -> structur…
Browse files Browse the repository at this point in the history
…al witness
  • Loading branch information
10to4 committed Jan 8, 2025
1 parent 532baf5 commit 430ea83
Show file tree
Hide file tree
Showing 20 changed files with 124 additions and 208 deletions.
25 changes: 10 additions & 15 deletions 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, StructuralFixed, StructuralWitIn, 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 Down Expand Up @@ -51,18 +51,6 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
self.cs.create_fixed(name_fn)
}

pub fn create_structural_fixed<NR, N>(
&mut self,
name_fn: N,
table_len: usize,
) -> Result<StructuralFixed, ZKVMError>
where
NR: Into<String>,
N: FnOnce() -> NR,
{
self.cs.create_structural_fixed(name_fn, table_len)
}

pub fn query_exit_code(&mut self) -> Result<[Instance; UINT_LIMBS], ZKVMError> {
Ok([
self.cs.query_instance(|| "exit_code_low", EXIT_CODE_IDX)?,
Expand Down Expand Up @@ -108,6 +96,7 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
&mut self,
name_fn: N,
table_len: usize,
table_spec: SetTableSpec,
rom_type: ROMType,
record: Vec<Expression<E>>,
multiplicity: Expression<E>,
Expand All @@ -116,8 +105,14 @@ impl<'a, E: ExtensionField> CircuitBuilder<'a, E> {
NR: Into<String>,
N: FnOnce() -> NR,
{
self.cs
.lk_table_record(name_fn, table_len, rom_type, record, multiplicity)
self.cs.lk_table_record(
name_fn,
table_len,
table_spec,
rom_type,
record,
multiplicity,
)
}

pub fn r_table_record<NR, N>(
Expand Down
27 changes: 4 additions & 23 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, StructuralFixed, StructuralWitIn, WitIn},
expression::{Expression, Fixed, Instance, StructuralWitIn, WitIn},
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
witness::RowMajorMatrix,
};
Expand Down Expand Up @@ -57,6 +57,7 @@ pub struct LogupTableExpression<E: ExtensionField> {
pub multiplicity: Expression<E>,
pub values: Expression<E>,
pub table_len: usize,
pub table_spec: SetTableSpec,
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -94,10 +95,6 @@ pub struct ConstraintSystem<E: ExtensionField> {
pub num_fixed: usize,
pub fixed_namespace_map: Vec<String>,

pub num_structural_fixed: usize,
pub structural_fixed_namespace_map: Vec<String>,
pub structural_fixed_len: Vec<usize>,

pub instance_name_map: HashMap<Instance, String>,

pub r_expressions: Vec<Expression<E>>,
Expand Down Expand Up @@ -155,9 +152,6 @@ impl<E: ExtensionField> ConstraintSystem<E> {
structural_witin_namespace_map: vec![],
num_fixed: 0,
fixed_namespace_map: vec![],
num_structural_fixed: 0,
structural_fixed_namespace_map: vec![],
structural_fixed_len: vec![],
ns: NameSpace::new(root_name_fn),
instance_name_map: HashMap::new(),
r_expressions: vec![],
Expand Down Expand Up @@ -270,21 +264,6 @@ impl<E: ExtensionField> ConstraintSystem<E> {
Ok(f)
}

pub fn create_structural_fixed<NR: Into<String>, N: FnOnce() -> NR>(
&mut self,
n: N,
table_len: usize,
) -> Result<StructuralFixed, ZKVMError> {
let f = StructuralFixed(self.num_fixed, table_len);
self.num_structural_fixed += 1;

let path = self.ns.compute_path(n().into());
self.structural_fixed_namespace_map.push(path);
self.structural_fixed_len.push(table_len);

Ok(f)
}

pub fn query_instance<NR: Into<String>, N: FnOnce() -> NR>(
&mut self,
n: N,
Expand Down Expand Up @@ -336,6 +315,7 @@ impl<E: ExtensionField> ConstraintSystem<E> {
&mut self,
name_fn: N,
table_len: usize,
table_spec: SetTableSpec,
rom_type: ROMType,
record: Vec<Expression<E>>,
multiplicity: Expression<E>,
Expand All @@ -360,6 +340,7 @@ impl<E: ExtensionField> ConstraintSystem<E> {
values: rlc_record,
multiplicity,
table_len,
table_spec,
});
let path = self.ns.compute_path(name_fn().into());
self.lk_table_expressions_namespace_map.push(path);
Expand Down
21 changes: 0 additions & 21 deletions ceno_zkvm/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub enum Expression<E: ExtensionField> {
StructuralWitIn(WitnessId, usize, u32, usize),
/// This multi-linear polynomial is known at the setup/keygen phase.
Fixed(Fixed),
/// StructuralFixed is similar with Fixed, but it is structured.
StructuralFixed(StructuralFixed),
/// Public Values
Instance(Instance),
/// Constant poly
Expand Down Expand Up @@ -61,7 +59,6 @@ impl<E: ExtensionField> Expression<E> {
pub fn degree(&self) -> usize {
match self {
Expression::Fixed(_) => 1,
Expression::StructuralFixed(..) => 1,
Expression::WitIn(_) => 1,
Expression::StructuralWitIn(..) => 1,
Expression::Instance(_) => 0,
Expand All @@ -77,7 +74,6 @@ impl<E: ExtensionField> Expression<E> {
pub fn evaluate<T>(
&self,
fixed_in: &impl Fn(&Fixed) -> T,
structural_fixed_in: &impl Fn(&StructuralFixed) -> T,
wit_in: &impl Fn(WitnessId) -> T, // witin id
structural_wit_in: &impl Fn(WitnessId, usize, u32, usize) -> T,
constant: &impl Fn(E::BaseField) -> T,
Expand All @@ -88,7 +84,6 @@ impl<E: ExtensionField> Expression<E> {
) -> T {
self.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
&|_| unreachable!(),
Expand All @@ -104,7 +99,6 @@ impl<E: ExtensionField> Expression<E> {
pub fn evaluate_with_instance<T>(
&self,
fixed_in: &impl Fn(&Fixed) -> T,
structural_fixed_in: &impl Fn(&StructuralFixed) -> T,
wit_in: &impl Fn(WitnessId) -> T, // witin id
structural_wit_in: &impl Fn(WitnessId, usize, u32, usize) -> T,
instance: &impl Fn(Instance) -> T,
Expand All @@ -116,7 +110,6 @@ impl<E: ExtensionField> Expression<E> {
) -> T {
match self {
Expression::Fixed(f) => fixed_in(f),
Expression::StructuralFixed(f) => structural_fixed_in(f),
Expression::WitIn(witness_id) => wit_in(*witness_id),
Expression::StructuralWitIn(witness_id, max_len, offset, multi_factor) => {
structural_wit_in(*witness_id, *max_len, *offset, *multi_factor)
Expand All @@ -126,7 +119,6 @@ impl<E: ExtensionField> Expression<E> {
Expression::Sum(a, b) => {
let a = a.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -138,7 +130,6 @@ impl<E: ExtensionField> Expression<E> {
);
let b = b.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -153,7 +144,6 @@ impl<E: ExtensionField> Expression<E> {
Expression::Product(a, b) => {
let a = a.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -165,7 +155,6 @@ impl<E: ExtensionField> Expression<E> {
);
let b = b.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -180,7 +169,6 @@ impl<E: ExtensionField> Expression<E> {
Expression::ScaledSum(x, a, b) => {
let x = x.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -192,7 +180,6 @@ impl<E: ExtensionField> Expression<E> {
);
let a = a.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand All @@ -204,7 +191,6 @@ impl<E: ExtensionField> Expression<E> {
);
let b = b.evaluate_with_instance(
fixed_in,
structural_fixed_in,
wit_in,
structural_wit_in,
instance,
Expand Down Expand Up @@ -233,7 +219,6 @@ impl<E: ExtensionField> Expression<E> {
fn is_zero_expr(expr: &Expression<E>) -> bool {
match expr {
Expression::Fixed(_) => false,
Expression::StructuralFixed(..) => false,
Expression::WitIn(_) => false,
Expression::StructuralWitIn(..) => false,
Expression::Instance(_) => false,
Expand All @@ -251,7 +236,6 @@ impl<E: ExtensionField> Expression<E> {
match (expr, s) {
(
Expression::Fixed(_)
| Expression::StructuralFixed(..)
| Expression::WitIn(_)
| Expression::StructuralWitIn(..)
| Expression::Challenge(..)
Expand Down Expand Up @@ -285,7 +269,6 @@ impl<E: ExtensionField> Neg for Expression<E> {
fn neg(self) -> Self::Output {
match self {
Expression::Fixed(_)
| Expression::StructuralFixed(..)
| Expression::WitIn(_)
| Expression::StructuralWitIn(..)
| Expression::Instance(_) => Expression::ScaledSum(
Expand Down Expand Up @@ -808,9 +791,6 @@ pub struct StructuralWitIn {
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Fixed(pub usize);

#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct StructuralFixed(pub usize, pub usize);

#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Instance(pub usize);

Expand Down Expand Up @@ -1004,7 +984,6 @@ pub mod fmt {
base_field::<E::BaseField>(constant, true).to_string()
}
Expression::Fixed(fixed) => format!("{:?}", fixed),
Expression::StructuralFixed(fixed) => format!("{:?}", fixed),
Expression::Instance(i) => format!("{:?}", i),
Expression::Sum(left, right) => {
let s = format!("{} + {}", expr(left, wtns, false), expr(right, wtns, false));
Expand Down
5 changes: 2 additions & 3 deletions ceno_zkvm/src/expression/monomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ impl<E: ExtensionField> Expression<E> {
}]
}

Fixed(_) | StructuralFixed(_) | WitIn(_) | StructuralWitIn(..) | Instance(_)
| Challenge(..) => {
Fixed(_) | WitIn(_) | StructuralWitIn(..) | Instance(_) | Challenge(..) => {
vec![Term {
coeff: Expression::ONE,
vars: vec![self.clone()],
Expand Down Expand Up @@ -148,7 +147,7 @@ mod tests {
E::random(&mut rng),
];
move |expr: &Expression<E>| {
eval_by_expr_with_fixed(&fixed, &[], &witnesses, &[], &challenges, expr)
eval_by_expr_with_fixed(&fixed, &witnesses, &[], &challenges, expr)
}
}
}
2 changes: 1 addition & 1 deletion ceno_zkvm/src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<E: ExtensionField> ZKVMConstraintSystem<E> {
for (c_name, cs) in self.circuit_css {
// fixed_traces is optional
// verifier will check it existent if cs.num_fixed > 0
let fixed_traces = if cs.num_fixed + cs.num_structural_fixed > 0 {
let fixed_traces = if cs.num_fixed > 0 {
vm_fixed_traces
.circuit_fixed_traces
.remove(&c_name)
Expand Down
Loading

0 comments on commit 430ea83

Please sign in to comment.