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

Add context pruning #112

Merged
merged 8 commits into from
Apr 2, 2024
Merged

Conversation

hratoanina
Copy link
Contributor

@hratoanina hratoanina commented Mar 15, 2024

Implements the pruning of stale contexts. A context is stale when it returns to its parent context; after it returns, it can never be accessed again and can be omitted from the memory of subsequent segments.

To detect when a context goes stale, we instrument SET_CONTEXT, and check if new_ctx < old_ctx. Since contexts are attributed in a strictly increasing order, this means that new_ctx is the parent context of old_ctx, and thus that old_ctx is stale.

This check is made in the CPU table. All the stale old_ctx values are then transmitted to the Memory table via a CTL.
In Memory, for each row, we check if ADDR_CONTEXT is contained in the PRUNED_CONTEXTS column via a lookup. If that's the case, it will be omitted from the CTL to MemAfter.

This PR contains #109 and #111. These are required fixes which aren't merged upstream yet. It's opened as a draft so people can look at it and run the test suite on it if they wish, but will wait for these PRs to be merged before opening it in earnest.
Edit: Done.

Closes #24

@github-actions github-actions bot added the crate: evm_arithmetization Anything related to the evm_arithmetization crate. label Mar 15, 2024
@vgnom vgnom added this to the zk-continuations - Q1 2024 milestone Mar 18, 2024
@hratoanina hratoanina marked this pull request as ready for review March 19, 2024 01:35
@4l0n50
Copy link
Contributor

4l0n50 commented Mar 20, 2024

and check if new_ctx < old_ctx.

It seems this happens if and only if you are in terminate_common? So maybe you can manually set pruning_flag if SET_CONTEXT expects scaled_ctx + prunning_flag on the top of the stack and we call SET_CONTEXT with pruning_flag == 1 iff we are in terminate_common. In this way you don't need to add the CTL with the arithmetic stark. Do you think this might work? or maybe I'm missing something?

@hratoanina
Copy link
Contributor Author

and check if new_ctx < old_ctx.

It seems this happens if and only if you are in terminate_common? So maybe you can manually set pruning_flag if SET_CONTEXT expects scaled_ctx + prunning_flag on the top of the stack and we call SET_CONTEXT with pruning_flag == 1 iff we are in terminate_common. In this way you don't need to add the CTL with the arithmetic stark. Do you think this might work? or maybe I'm missing something?

Hmm, that's a good idea. There's another case than terminate_common (for EXTCODECOPY and EXTCODESIZE), but you can do something similar. This would require a lot of changes and I don't think the benefits would be huge, so I'll keep it as a follow-up task. Thanks!

evm_arithmetization/src/cpu/columns/general.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/cpu/columns/general.rs Outdated Show resolved Hide resolved
Comment on lines +181 to +184
assert_eq!(
interpreter.stack(),
vec![U256::one() << CONTEXT_SCALING_FACTOR, code.len().into()]
);
Copy link
Contributor

Choose a reason for hiding this comment

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

DQ: why do we expect this to be the content of the stack here? And why only for EXTCODESIZE but not for other tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's because extcodesize (not sys_extcodesize) now returns code_size, codesize_ctx. I added the returned stack in a comment in the ASM.

evm_arithmetization/src/cpu/cpu_stark.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/generation/state.rs Outdated Show resolved Hide resolved
}
Some(mem)
} else {
None
Copy link
Contributor

Choose a reason for hiding this comment

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

For my education, what is the case when get_full_memory is None? What does that mean that GenerationState use the default impl and returns None?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When you're generating the proof (with the full trace), you don't need this memory. The Memory trace will be generated from the list of MemOps instead.

evm_arithmetization/src/generation/state.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/generation/state.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/memory_stark.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@LindaGuiga LindaGuiga left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

evm_arithmetization/src/cpu/kernel/asm/memory/syscalls.asm Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/memory_stark.rs Outdated Show resolved Hide resolved
@@ -112,6 +112,15 @@ calldataload_large_offset:
codecopy_within_bounds:
// stack: total_size, segment, src_ctx, kexit_info, dest_offset, offset, size
POP
// stack: segment, src_ctx, kexit_info, dest_offset, offset, size
Copy link
Contributor

@4l0n50 4l0n50 Mar 25, 2024

Choose a reason for hiding this comment

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

Why do you need to add these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

codecopy_within_bounds calls codecopy_after instead of wcopy_after. I agree that's more or less code duplication, but I didn't see an easy way to do it more cleanly.

Copy link
Contributor

Choose a reason for hiding this comment

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

My problem is that I can't see where are you changing the context here.

Copy link
Contributor

Choose a reason for hiding this comment

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

From my understanding, the problem with codecopy etc. is that they do not change contexts, but write code in another context. And the idea here is to still be able to prune that extra context when needed by setting the context in codecopy_after.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh, and why src_ctx will be never accessed again?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the case of extcodecopy and extcodesize, we generate src_ctx with next_context_id which increments @GLOBAL_METADATA_LARGEST_CONTEXT, meaning that any other created context will have a different id.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhh ok, I think see now. It's because you just need the new_ctx for writing the code there (and check the hash etc) but after you copy the code you can forget about it?

Copy link
Contributor

@4l0n50 4l0n50 left a comment

Choose a reason for hiding this comment

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

LGTM modulo some nits.

evm_arithmetization/src/memory/memory_stark.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/memory_stark.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/columns.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/columns.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/columns.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/cpu/columns/general.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/cpu/columns/general.rs Outdated Show resolved Hide resolved
@@ -112,6 +112,15 @@ calldataload_large_offset:
codecopy_within_bounds:
// stack: total_size, segment, src_ctx, kexit_info, dest_offset, offset, size
POP
// stack: segment, src_ctx, kexit_info, dest_offset, offset, size
Copy link
Contributor

Choose a reason for hiding this comment

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

My problem is that I can't see where are you changing the context here.

Copy link
Collaborator

@Nashtare Nashtare left a comment

Choose a reason for hiding this comment

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

Overall LGTM

evm_arithmetization/src/generation/state.rs Outdated Show resolved Hide resolved
evm_arithmetization/src/memory/memory_stark.rs Outdated Show resolved Hide resolved
Copy link

sonarcloud bot commented Apr 2, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@hratoanina hratoanina merged commit 867a172 into feat/continuations Apr 2, 2024
7 checks passed
@hratoanina hratoanina deleted the context_pruning_rebased branch April 2, 2024 15:43
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: Done
Development

Successfully merging this pull request may close these issues.

6 participants