Skip to content

Commit

Permalink
feat: (de)serialize VMState
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Dec 17, 2023
1 parent a46dca2 commit 8df0723
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rand_core = "0.6.4"
rayon = "1.8"
serde = { version = "1", features = ["derive"] }
serde_derive = "1"
serde_json = "1.0"
strum = { version = "0.25", features = ["derive"] }
syn = "2.0"
test-strategy = "0.3.1"
Expand Down
1 change: 1 addition & 0 deletions triton-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cargo-husky.workspace = true
pretty_assertions.workspace = true
proptest.workspace = true
proptest-arbitrary-interop.workspace = true
serde_json.workspace = true
test-strategy.workspace = true

[[bench]]
Expand Down
5 changes: 2 additions & 3 deletions triton-vm/src/op_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use arbitrary::Arbitrary;
use get_size::GetSize;
use itertools::Itertools;
use num_traits::Zero;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use serde_derive::*;
use strum::EnumCount;
use strum::EnumIter;
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -41,7 +40,7 @@ pub const NUM_OP_STACK_REGISTERS: usize = OpStackElement::COUNT;
/// and the op-stack underflow memory. The op-stack registers are the first
/// [`OpStackElement::COUNT`] elements of the op-stack, and the op-stack underflow memory is the
/// remaining elements.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Arbitrary)]
// If the op stack is empty, things have gone horribly wrong. Suppressing this lint is preferred
// to implementing a basically useless `is_empty()` method.
#[allow(clippy::len_without_is_empty)]
Expand Down
3 changes: 2 additions & 1 deletion triton-vm/src/table/ram_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use ndarray::ArrayViewMut2;
use ndarray::Axis;
use num_traits::One;
use num_traits::Zero;
use serde_derive::*;
use strum::EnumCount;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::b_field_element::BFIELD_ONE;
Expand Down Expand Up @@ -38,7 +39,7 @@ pub const INSTRUCTION_TYPE_WRITE: BFieldElement = BFIELD_ZERO;
pub const INSTRUCTION_TYPE_READ: BFieldElement = BFIELD_ONE;
pub const PADDING_INDICATOR: BFieldElement = BFieldElement::new(2);

#[derive(Debug, Clone, PartialEq, Eq, Hash, Arbitrary)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, Arbitrary)]
pub struct RamTableCall {
pub clk: u32,
pub ram_pointer: BFieldElement,
Expand Down
13 changes: 12 additions & 1 deletion triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;

use arbitrary::Arbitrary;
use ndarray::Array1;
use num_traits::One;
use num_traits::Zero;
use serde_derive::*;
use twenty_first::shared_math::b_field_element::BFieldElement;
use twenty_first::shared_math::b_field_element::BFIELD_ZERO;
use twenty_first::shared_math::digest::Digest;
Expand Down Expand Up @@ -37,7 +39,7 @@ type Result<T> = std::result::Result<T, InstructionError>;
/// The number of helper variable registers
pub const NUM_HELPER_VARIABLE_REGISTERS: usize = 6;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Arbitrary)]
pub struct VMState {
/// The **program memory** stores the instructions (and their arguments) of the program
/// currently being executed by Triton VM. It is read-only.
Expand Down Expand Up @@ -2362,4 +2364,13 @@ pub(crate) mod tests {
let program = triton_program! { read_io 1 halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 0);
}

#[proptest]
fn serialize_deserialize_vm_state_to_and_from_json_is_identity(
#[strategy(arb())] vm_state: VMState,
) {
let serialized = serde_json::to_string(&vm_state).unwrap();
let deserialized = serde_json::from_str(&serialized).unwrap();
prop_assert_eq!(vm_state, deserialized);
}
}

0 comments on commit 8df0723

Please sign in to comment.