Skip to content

Commit

Permalink
skip structural witin commitment & PCS(#654):
Browse files Browse the repository at this point in the history
- cargo fmt check
  • Loading branch information
10to4 committed Dec 12, 2024
1 parent 1812068 commit 55e3b6b
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 50 deletions.
7 changes: 5 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, StructuralWitIn},
expression::{Expression, Fixed, Instance, StructuralWitIn, WitIn},
structs::{ProgramParams, ProvingKey, RAMType, VerifyingKey, WitnessId},
witness::RowMajorMatrix,
};
Expand Down Expand Up @@ -243,7 +243,10 @@ impl<E: ExtensionField> ConstraintSystem<E> {
wit_in
}

pub fn create_structural_witin<NR: Into<String>, N: FnOnce() -> NR>(&mut self, n: N) -> StructuralWitIn{
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;
Expand Down
85 changes: 71 additions & 14 deletions ceno_zkvm/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,87 @@ impl<E: ExtensionField> Expression<E> {
Expression::Constant(scalar) => constant(*scalar),
Expression::Sum(a, b) => {
let a = a.evaluate_with_instance(
fixed_in, wit_in, structural_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, structural_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, structural_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, structural_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, structural_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, structural_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, structural_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 @@ -217,13 +273,14 @@ impl<E: ExtensionField> Neg for Expression<E> {
type Output = Expression<E>;
fn neg(self) -> Self::Output {
match self {
Expression::Fixed(_) | Expression::WitIn(_) | Expression::StructuralWitIn(_) | Expression::Instance(_) => {
Expression::ScaledSum(
Box::new(self),
Box::new(Expression::Constant(E::BaseField::ONE.neg())),
Box::new(Expression::Constant(E::BaseField::ZERO)),
)
}
Expression::Fixed(_)
| Expression::WitIn(_)
| Expression::StructuralWitIn(_)
| Expression::Instance(_) => Expression::ScaledSum(
Box::new(self),
Box::new(Expression::Constant(E::BaseField::ONE.neg())),
Box::new(Expression::Constant(E::BaseField::ZERO)),
),
Expression::Constant(c1) => Expression::Constant(c1.neg()),
Expression::Sum(a, b) => {
Expression::Sum(Box::new(-a.deref().clone()), Box::new(-b.deref().clone()))
Expand Down
4 changes: 3 additions & 1 deletion ceno_zkvm/src/expression/monomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ 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)
}
}
}
43 changes: 28 additions & 15 deletions ceno_zkvm/src/scheme/mock_prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ fn load_tables<E: ExtensionField>(cb: &CircuitBuilder<E>, challenge: [E; 2]) ->
(b as usize).into(),
(c as usize).into(),
]);
let rlc_record = eval_by_expr(&[], &[],&challenge, &rlc_record);
let rlc_record = eval_by_expr(&[], &[], &challenge, &rlc_record);
t_vec.push(rlc_record.to_canonical_u64_vec());
}
}
Expand Down Expand Up @@ -657,7 +657,8 @@ impl<'a, E: ExtensionField + Hash> MockProver<E> {
.iter()
.map(|v| unsafe { (*v).assume_init() }.into())
.collect::<Vec<_>>();
let rlc_record = eval_by_expr_with_fixed(&row, &[], &[], &challenge, &table_expr.values);
let rlc_record =
eval_by_expr_with_fixed(&row, &[], &[], &challenge, &table_expr.values);
t_vec.push(rlc_record.to_canonical_u64_vec());
}
}
Expand Down Expand Up @@ -847,15 +848,21 @@ Hints:
.iter()
.zip(cs.lk_expressions_items_map.clone().into_iter())
{
let lk_table =
wit_infer_by_expr(&fixed, &witness, &[], &pi_mles, &challenges, &expr.values)
.get_ext_field_vec()
.to_vec();
let lk_table = wit_infer_by_expr(
&fixed,
&witness,
&[],
&pi_mles,
&challenges,
&expr.values,
)
.get_ext_field_vec()
.to_vec();

let multiplicity = wit_infer_by_expr(
&fixed,
&witness,
&[],
&[],
&pi_mles,
&challenges,
&expr.multiplicity,
Expand Down Expand Up @@ -920,7 +927,7 @@ Hints:
eval_by_expr_with_instance(
&[],
&witness,
&[],
&[],
&instance,
challenges.as_slice(),
expr,
Expand Down Expand Up @@ -979,10 +986,16 @@ Hints:
.zip_eq(cs.w_ram_types.iter())
.filter(|((_, _), (ram_type, _))| *ram_type == $ram_type)
{
let write_rlc_records =
(wit_infer_by_expr(fixed, witness, &[], &pi_mles, &challenges, w_rlc_expr)
.get_ext_field_vec())[..*num_rows]
.to_vec();
let write_rlc_records = (wit_infer_by_expr(
fixed,
witness,
&[],
&pi_mles,
&challenges,
w_rlc_expr,
)
.get_ext_field_vec())[..*num_rows]
.to_vec();

if $ram_type == RAMType::GlobalState {
// w_exprs = [GlobalState, pc, timestamp]
Expand All @@ -994,7 +1007,7 @@ Hints:
let v = wit_infer_by_expr(
fixed,
witness,
&[],
&[],
&pi_mles,
&challenges,
expr,
Expand Down Expand Up @@ -1154,15 +1167,15 @@ Hints:
gs_rs.insert(eval_by_expr_with_instance(
&[],
&[],
&[],
&[],
&instance,
&challenges,
&gs_final,
));
gs_ws.insert(eval_by_expr_with_instance(
&[],
&[],
&[],
&[],
&instance,
&challenges,
&gs_init,
Expand Down
23 changes: 16 additions & 7 deletions ceno_zkvm/src/scheme/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
let commit_dur = std::time::Instant::now();
let num_instances = structural_witness.num_instances();
let structural_witness = structural_witness.into_mles();

tracing::info!(
"commit to {} traces took {:?}",
circuit_name,
Expand Down Expand Up @@ -134,7 +134,6 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
}
exit_span!(commit_to_traces_span);


// squeeze two challenges from transcript
let challenges = [
transcript.read_challenge().elements,
Expand All @@ -157,11 +156,11 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
continue;
}
let wits_commit = commitments.remove(circuit_name).unwrap();
let (structural_witness, structural_num_instances) =
let (structural_witness, structural_num_instances) =
if structural_wits.contains_key(circuit_name) {
structural_wits
.remove(circuit_name)
.ok_or(ZKVMError::WitnessNotFound(circuit_name.clone()))?
.remove(circuit_name)
.ok_or(ZKVMError::WitnessNotFound(circuit_name.clone()))?
} else {
(vec![], 0)
};
Expand Down Expand Up @@ -206,7 +205,10 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
pk,
witness.into_iter().map(|v| v.into()).collect_vec(),
wits_commit,
structural_witness.into_iter().map(|v| v.into()).collect_vec(),
structural_witness
.into_iter()
.map(|v| v.into())
.collect_vec(),
&pi,
transcript,
&challenges,
Expand Down Expand Up @@ -746,7 +748,14 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
.chain(cs.lk_table_expressions.par_iter().map(|lk| &lk.values))
.map(|expr| {
assert_eq!(expr.degree(), 1);
wit_infer_by_expr(&fixed, &witnesses, &structural_witnesses, pi, challenges, expr)
wit_infer_by_expr(
&fixed,
&witnesses,
&structural_witnesses,
pi,
challenges,
expr,
)
})
.collect();
let max_log2_num_instance = records_wit.iter().map(|mle| mle.num_vars()).max().unwrap();
Expand Down
3 changes: 2 additions & 1 deletion ceno_zkvm/src/scheme/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMVerifier<E, PCS>

// verify zero expression (degree = 1) statement, thus no sumcheck
if cs.assert_zero_expressions.iter().any(|expr| {
eval_by_expr_with_instance(&[], &proof.wits_in_evals, &[], pi, challenges, expr) != E::ZERO
eval_by_expr_with_instance(&[], &proof.wits_in_evals, &[], pi, challenges, expr)
!= E::ZERO
}) {
return Err(ZKVMError::VerifyError("zero expression != 0".into()));
}
Expand Down
11 changes: 9 additions & 2 deletions ceno_zkvm/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
self.witnesses_tables.get(name).cloned()
}

pub fn get_table_structural_witness(&self, name: &String) -> Option<RowMajorMatrix<E::BaseField>> {
pub fn get_table_structural_witness(
&self,
name: &String,
) -> Option<RowMajorMatrix<E::BaseField>> {
self.structural_witnesses_tables.get(name).cloned()
}

Expand Down Expand Up @@ -330,7 +333,11 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
self.combined_lk_mlt.as_ref().unwrap(),
input,
)?;
assert!(self.structural_witnesses_tables.insert(TC::name(), structural_witness).is_none());
assert!(
self.structural_witnesses_tables
.insert(TC::name(), structural_witness)
.is_none()
);

assert!(!self.witnesses_opcodes.contains_key(&TC::name()));

Expand Down
2 changes: 1 addition & 1 deletion ceno_zkvm/src/tables/ops/ops_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl<E: ExtensionField, OP: OpsTable> TableCircuit<E> for OpsTableCircuit<E, OP>
_num_witin: usize,
_multiplicity: &[HashMap<u64, usize>],
_final_v: &Self::WitnessInput,
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError>{
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {
Ok(RowMajorMatrix::new(0, 0))
}
}
2 changes: 1 addition & 1 deletion ceno_zkvm/src/tables/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<E: ExtensionField> TableCircuit<E> for ProgramTableCircuit<E> {
_num_witin: usize,
_multiplicity: &[HashMap<u64, usize>],
_final_v: &Self::WitnessInput,
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError>{
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {
Ok(RowMajorMatrix::new(0, 0))
}
}
Expand Down
5 changes: 2 additions & 3 deletions ceno_zkvm/src/tables/ram/ram_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<E: ExtensionField, NVRAM: NonVolatileTable + Send + Sync + Clone> TableCirc
num_witin: usize,
_multiplicity: &[HashMap<u64, usize>],
final_v: &Self::WitnessInput,
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError>{
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {
config.assign_structural_instances(num_witin, final_v)
}
}
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<E: ExtensionField, NVRAM: NonVolatileTable + Send + Sync + Clone> TableCirc
num_witin: usize,
_multiplicity: &[HashMap<u64, usize>],
final_cycles: &[Cycle],
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError>{
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {
config.assign_structural_instances(num_witin, final_cycles)
}
}
Expand Down Expand Up @@ -241,7 +241,6 @@ impl<E: ExtensionField, DVRAM: DynVolatileRamTable + Send + Sync + Clone> TableC
_multiplicity: &[HashMap<u64, usize>],
final_v: &Self::WitnessInput,
) -> Result<RowMajorMatrix<E::BaseField>, ZKVMError> {

config.assign_structural_instances(num_witin, final_v)
}
}
Loading

0 comments on commit 55e3b6b

Please sign in to comment.