-
Notifications
You must be signed in to change notification settings - Fork 17
vm core: update context #5
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,113 @@ | ||
| use crate::{context::run_context::RunContext, memory::manager::MemoryManager}; | ||
| use p3_field::PrimeField64; | ||
|
|
||
| use crate::{ | ||
| context::run_context::RunContext, | ||
| errors::{memory::MemoryError, vm::VirtualMachineError}, | ||
| memory::{manager::MemoryManager, val::MemoryValue}, | ||
| types::instruction::{Instruction, MemOrFp}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Default)] | ||
| pub struct VirtualMachine { | ||
| pub(crate) run_context: RunContext, | ||
| pub memory_manager: MemoryManager, | ||
| } | ||
|
|
||
| impl VirtualMachine { | ||
| /// Advances the program counter (`pc`) to the next instruction. | ||
| /// | ||
| /// This function embodies the control flow logic of the zkVM. For most instructions, | ||
| /// it performs a regular increment of the **`pc`**. However, for the `JumpIfNotZero` | ||
| /// instruction (`JUZ`), it implements conditional branching. | ||
| /// | ||
| /// ### `JumpIfNotZero` Logic | ||
| /// | ||
| /// When a `JumpIfNotZero` instruction is processed: | ||
| /// 1. The `condition` operand is resolved to a field element. | ||
| /// 2. If this value is **zero**, the program continues sequentially, and the **`pc`** is incremented by 1. | ||
| /// 3. If the value is **non-zero**, a jump is executed. The `dest` operand is resolved to find the | ||
| /// target `MemoryAddress`, which then becomes the new **`pc`**. | ||
| pub fn update_pc<F>( | ||
| &mut self, | ||
| instruction: &Instruction<F>, | ||
| ) -> Result<(), VirtualMachineError<F>> | ||
| where | ||
| F: PrimeField64, | ||
| { | ||
| // Determine the next program counter `pc` by checking if the instruction is a conditional jump. | ||
| let next_pc = if let Instruction::JumpIfNotZero { | ||
| condition, dest, .. | ||
| } = instruction | ||
| { | ||
| // For a `JumpIfNotZero` instruction, resolve the `condition` operand from memory or constants. | ||
| // This will return an error if the memory location is uninitialized. | ||
| let condition_val = self | ||
| .run_context | ||
| .get_value(condition, &self.memory_manager)?; | ||
|
|
||
| // A jump condition must be a field element. | ||
| // | ||
| // An address is considered non-zero by convention. | ||
| let is_zero = match condition_val { | ||
| MemoryValue::Int(felt) => felt.is_zero(), | ||
| MemoryValue::Address(_) => false, | ||
| }; | ||
|
|
||
| if is_zero { | ||
| // **Condition is zero**: The jump is not taken. Advance the `pc` by one. | ||
| (*self.run_context.pc() + 1)? | ||
| } else { | ||
| // **Condition is non-zero**: Execute the jump. | ||
| // | ||
| // First, resolve the `dest` operand to get the target address value. | ||
| let dest_val = self.run_context.get_value(dest, &self.memory_manager)?; | ||
|
|
||
| // The resolved destination value must be a valid address. | ||
| // | ||
| // Convert it and set it as the new `pc`. | ||
| dest_val.try_into()? | ||
| } | ||
| } else { | ||
| // For any instruction other than `JumpIfNotZero`, advance the `pc` by one. | ||
| (*self.run_context.pc() + 1)? | ||
| }; | ||
|
|
||
| // Update the virtual machine's program counter with the calculated next address. | ||
| self.run_context.pc = next_pc; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Updates the frame pointer (`fp`) based on the executed instruction. | ||
| pub fn update_fp<F>( | ||
| &mut self, | ||
| instruction: &Instruction<F>, | ||
| ) -> Result<(), VirtualMachineError<F>> | ||
| where | ||
| F: PrimeField64, | ||
| { | ||
| if let Instruction::JumpIfNotZero { updated_fp, .. } = instruction { | ||
| let new_fp = match updated_fp { | ||
| // The instruction specifies keeping the same `fp`. | ||
| MemOrFp::Fp => self.run_context.fp, | ||
| // The instruction specifies updating `fp` to a value from memory. | ||
| MemOrFp::MemoryAfterFp { shift } => { | ||
| let addr = (*self.run_context.fp() + *shift)?; | ||
| let value = self | ||
| .memory_manager | ||
| .get(addr) | ||
| .ok_or(MemoryError::UninitializedMemory(addr))?; | ||
|
|
||
| // The fetched value must be a valid memory address to become the new `fp`. | ||
| value.try_into()? | ||
| } | ||
| }; | ||
| self.run_context.fp = new_fp; | ||
| } | ||
|
|
||
| // For the other instructions, we do nothing for now. | ||
| // | ||
| // To be checked in the future. | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub mod math; | ||
| pub mod memory; | ||
| pub mod vm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| use std::fmt::Debug; | ||
|
|
||
| use thiserror::Error; | ||
|
|
||
| use super::{math::MathError, memory::MemoryError}; | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum VirtualMachineError<F> | ||
| where | ||
| F: Debug, | ||
| { | ||
| #[error(transparent)] | ||
| Memory(#[from] MemoryError<F>), | ||
| #[error(transparent)] | ||
| Math(#[from] MathError), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.