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

fix/circuit-order: Reliable order of circuits in proofs #522

Merged
merged 11 commits into from
Nov 6, 2024
6 changes: 3 additions & 3 deletions ceno_zkvm/src/scheme/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ impl<E: ExtensionField, PCS: PolynomialCommitmentScheme<E>> ZKVMProver<E, PCS> {
// commit to main traces
let mut commitments = BTreeMap::new();
let mut wits = BTreeMap::new();
// sort by circuit name, and we rely on an assumption that
// table circuit witnesses come after opcode circuit witnesses
for (circuit_name, witness) in witnesses.witnesses {

// commit to opcode circuits first and then commit to table circuits, sorted by name
for (circuit_name, witness) in witnesses.into_iter_sorted() {
let commit_dur = std::time::Instant::now();
let num_instances = witness.num_instances();
let witness = witness.into_mles();
Expand Down
7 changes: 6 additions & 1 deletion ceno_zkvm/src/scheme/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ fn test_rw_lk_expression_combination() {
// get proof
let prover = ZKVMProver::new(pk);
let mut transcript = Transcript::new(b"test");
let wits_in = zkvm_witness.witnesses.remove(&name).unwrap().into_mles();
let wits_in = zkvm_witness
.into_iter_sorted()
.next()
Copy link
Collaborator

@matthiasgoergens matthiasgoergens Nov 5, 2024

Choose a reason for hiding this comment

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

Isn't that just first?

Seems a bit weird in any case, why not just ask for the one we know we want by name?

Copy link
Collaborator Author

@naure naure Nov 5, 2024

Choose a reason for hiding this comment

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

Or exactly_once but somehow didn't work.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, I'll have a look.

.unwrap()
.1
.into_mles();
// commit to main traces
let commit = Pcs::batch_commit_and_write(&prover.pk.pp, &wits_in, &mut transcript).unwrap();
let wits_in = wits_in.into_iter().map(|v| v.into()).collect_vec();
Expand Down
16 changes: 13 additions & 3 deletions ceno_zkvm/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ impl<E: ExtensionField> ZKVMFixedTraces<E> {

#[derive(Default)]
pub struct ZKVMWitnesses<E: ExtensionField> {
pub witnesses: BTreeMap<String, RowMajorMatrix<E::BaseField>>,
witnesses_opcodes: BTreeMap<String, RowMajorMatrix<E::BaseField>>,
Copy link
Collaborator

@matthiasgoergens matthiasgoergens Nov 5, 2024

Choose a reason for hiding this comment

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

Wouldn't we want the kinds instead of the opcodes?

Or what does witnesses_opcodes mean?

Copy link
Collaborator Author

@naure naure Nov 5, 2024

Choose a reason for hiding this comment

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

There can be other circuits despite the name. It is called "opcode" everywhere until we find a more precise name.

Copy link
Collaborator

Choose a reason for hiding this comment

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

So what's actually in there? The instructions or something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's what the caller of this type passed in assign_opcode_circuit, respectively assign_table_circuit.

witnesses_tables: BTreeMap<String, RowMajorMatrix<E::BaseField>>,
lk_mlts: BTreeMap<String, LkMultiplicity>,
combined_lk_mlt: Option<Vec<HashMap<u64, usize>>>,
}
Expand All @@ -222,7 +223,8 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
let cs = cs.get_cs(&OC::name()).unwrap();
let (witness, logup_multiplicity) =
OC::assign_instances(config, cs.num_witin as usize, records)?;
assert!(self.witnesses.insert(OC::name(), witness).is_none());
assert!(self.witnesses_opcodes.insert(OC::name(), witness).is_none());
assert!(!self.witnesses_tables.contains_key(&OC::name()));
assert!(
self.lk_mlts
.insert(OC::name(), logup_multiplicity)
Expand Down Expand Up @@ -273,10 +275,18 @@ impl<E: ExtensionField> ZKVMWitnesses<E> {
self.combined_lk_mlt.as_ref().unwrap(),
input,
)?;
assert!(self.witnesses.insert(TC::name(), witness).is_none());
assert!(self.witnesses_tables.insert(TC::name(), witness).is_none());
assert!(!self.witnesses_opcodes.contains_key(&TC::name()));

Ok(())
}

/// Iterate opcode circuits, then table circuits, sorted by name.
pub fn into_iter_sorted(self) -> impl Iterator<Item = (String, RowMajorMatrix<E::BaseField>)> {
self.witnesses_opcodes
naure marked this conversation as resolved.
Show resolved Hide resolved
.into_iter()
.chain(self.witnesses_tables)
}
}

#[derive(Debug)]
Expand Down
Loading