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

Enables optional verification of Keccak tables #657

Open
wants to merge 29 commits into
base: sai/conditionally_verify_in_root_circuit
Choose a base branch
from

Conversation

sai-deng
Copy link
Member

@sai-deng sai-deng commented Sep 24, 2024

Enables optional verification of Keccak tables in root circuits, saving unnecessary recursion time for empty Keccak tables. In my tests below, it reduces proving time by around 3 seconds when a segment contains no Keccak operations. #620

RUST_LOG=info /Users/sdeng/.cargo/bin/cargo test --color=always -r --lib fixed_recursive_verifier::tests::test_segment_proof_generation_without_keccak --no-fail-fast --manifest-path /Users/sdeng/Project/0xPolygonZero/zk_evm/evm_arithmetization/Cargo.toml -- --ignored
[2024-09-25T04:47:30Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:47:30Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 100, byte_packing_len: 3, cpu_len: 512, keccak_len: 24, keccak_sponge_len: 1, logic_len: 8, memory_len: 67556 }, mem_before_len: 66049, mem_after_len: 66113
[2024-09-25T04:47:46Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:47:46Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 102, byte_packing_len: 7, cpu_len: 512, keccak_len: 96, keccak_sponge_len: 4, logic_len: 23, memory_len: 67773 }, mem_before_len: 66113, mem_after_len: 66126
[2024-09-25T04:48:01Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:48:01Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 83, byte_packing_len: 5, cpu_len: 512, keccak_len: 48, keccak_sponge_len: 2, logic_len: 14, memory_len: 67631 }, mem_before_len: 66126, mem_after_len: 66136
test fixed_recursive_verifier::tests::test_segment_proof_generation_without_keccak has been running for over 60 seconds
[2024-09-25T04:48:16Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:48:16Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 121, byte_packing_len: 2, cpu_len: 512, keccak_len: 0, keccak_sponge_len: 0, logic_len: 4, memory_len: 67684 }, mem_before_len: 66136, mem_after_len: 66173
[2024-09-25T04:48:28Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:48:28Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 98, byte_packing_len: 7, cpu_len: 512, keccak_len: 24, keccak_sponge_len: 1, logic_len: 8, memory_len: 67759 }, mem_before_len: 66173, mem_after_len: 66244
[2024-09-25T04:48:44Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:48:44Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 89, byte_packing_len: 2, cpu_len: 512, keccak_len: 24, keccak_sponge_len: 1, logic_len: 8, memory_len: 67871 }, mem_before_len: 66244, mem_after_len: 66251
[2024-09-25T04:49:00Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:49:00Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 115, byte_packing_len: 2, cpu_len: 512, keccak_len: 0, keccak_sponge_len: 0, logic_len: 4, memory_len: 67796 }, mem_before_len: 66251, mem_after_len: 66251
[2024-09-25T04:49:12Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:49:12Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 121, byte_packing_len: 4, cpu_len: 512, keccak_len: 0, keccak_sponge_len: 0, logic_len: 4, memory_len: 67807 }, mem_before_len: 66251, mem_after_len: 66257
[2024-09-25T04:49:23Z INFO  evm_arithmetization::generation] CPU trace padded to 512 cycles
[2024-09-25T04:49:23Z INFO  evm_arithmetization::witness::traces] Trace lengths (before padding): TraceCheckpoint { arithmetic_len: 90, byte_packing_len: 4, cpu_len: 512, keccak_len: 48, keccak_sponge_len: 2, logic_len: 13, memory_len: 67831 }, mem_before_len: 66257, mem_after_len: 0
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] 150.4260s to Segment Proof Generation
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 22.9381s to Create all recursive circuits
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 0.0000s to Generate dummy payload
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 15.6231s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 15.1803s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 15.4374s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 11.6834s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 16.2813s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 15.7072s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 11.7241s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 11.6348s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 14.2030s to Prove segment
[2024-09-25T04:49:37Z INFO  plonky2::util::timing] | 0.0034s to Verify segment proof
test fixed_recursive_verifier::tests::test_segment_proof_generation_without_keccak ... ok

@sai-deng sai-deng changed the base branch from develop to sai/skip_table_in_root_circuit September 24, 2024 21:14
@github-actions github-actions bot added the crate: evm_arithmetization Anything related to the evm_arithmetization crate. label Sep 24, 2024
@sai-deng sai-deng changed the base branch from sai/skip_table_in_root_circuit to sai/conditionally_verify_in_root_circuit September 24, 2024 21:15
@sai-deng sai-deng self-assigned this Sep 25, 2024
@sai-deng sai-deng changed the title [WIP] Sai/enable keccak optional proving Enables Keccak optional proving Sep 25, 2024
@sai-deng sai-deng changed the title Enables Keccak optional proving Enables optional verification of Keccak tables Sep 25, 2024
@sai-deng sai-deng force-pushed the sai/enable_keccak_optional_proving branch from 34c9bad to ab2ec50 Compare September 26, 2024 23:10
self.root.index_verifier_data[table],
F::from_canonical_usize(*index_verifier_data),
);
let common_date = &table_circuits
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let common_date = &table_circuits
let common_data = &table_circuits

evm_arithmetization/src/fixed_recursive_verifier.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/fixed_recursive_verifier.rs Outdated Show resolved Hide resolved
@@ -486,7 +486,11 @@ fn get_all_memory_address_and_values(memory_before: &MemoryState) -> Vec<(Memory
res
}

type TablesWithPVsAndFinalMem<F> = ([Vec<PolynomialValues<F>>; NUM_TABLES], PublicValues<F>);
pub struct TablesWithPVsAndFinalMem<F: RichField> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: out of curiosity: why AndFinalMem?

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed it. Thanks for the catch!

Comment on lines +120 to +123
if KECCAK_TABLES_INDICES.contains(&i) && !use_keccak_tables {
// Observe zero merkle caps when skipping Keccak tables.
let zero_merkle_cap = cap.flatten().iter().map(|_| F::ZERO).collect::<Vec<F>>();
challenger.observe_elements(&zero_merkle_cap);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can they not just be ignored? (as in only observe if it's not a keccak table)?

Also, has a similar change been applied on the verifier side (in get_challenges.rs)?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is difficult to implement conditionally observe in root circuits, so I choose observe zero caps instead.

I was originally planning to implement it in the next PR (which requires changes on the Plonky2 side, and get_challenges.rs is only used for testing). However, I agree with you that we should make this PR complete. I will implement it in this PR instead.

evm_arithmetization/src/prover.rs Show resolved Hide resolved
@sai-deng
Copy link
Member Author

sai-deng commented Oct 2, 2024

@LindaGuiga Thanks for reviewing! I have synced the zkVM verifier with the prover.

In the next PR #690 , I will make the proofs optional in the proof table, but this requires changes on the Plonky2 side. This is also why I’m sending this PR for review before updating Plonky2.

@sai-deng sai-deng force-pushed the sai/enable_keccak_optional_proving branch from f61d7c0 to 503085b Compare October 2, 2024 21:59
@Nashtare Nashtare added this to the Performance Tuning milestone Oct 3, 2024
.by_stark_size
.keys()
.min()
.expect("No valid size in shrinking circuits");
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: it'd be best to return an error early than panicking in these calls

Comment on lines +588 to +589
let use_keccak_tables =
!state.traces.keccak_inputs.is_empty() || !state.traces.keccak_sponge_ops.is_empty();
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can't have one empty and the other non-empty, so no need for the second evaluation

let (keccak_proof, _) = prove_table!(keccak_stark, Table::Keccak);
let (keccak_sponge_proof, _) = prove_table!(keccak_sponge_stark, Table::KeccakSponge);
if !use_keccak_tables {
// We need to connect the challenger state of Logic and CPU tables when the
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit (for consistency with actual ordering)

Suggested change
// We need to connect the challenger state of Logic and CPU tables when the
// We need to connect the challenger state of CPU and Logic tables when the

Copy link
Collaborator

Choose a reason for hiding this comment

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

also would need to probably rework the way we mark empty tables once we include BP / Logic / MemAfter, as we may do multiple hops before finding a non-empty table, and I find the use of multiple use_foo fields in AllProof slightly ugly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate: evm_arithmetization Anything related to the evm_arithmetization crate.
Projects
Status: Ready to Review
Development

Successfully merging this pull request may close these issues.

3 participants