Skip to content

Commit

Permalink
Make all chip records serde
Browse files Browse the repository at this point in the history
  • Loading branch information
nyunyunyunyu committed Jan 8, 2025
1 parent 5d7c7fc commit 63438a8
Show file tree
Hide file tree
Showing 54 changed files with 537 additions and 252 deletions.
383 changes: 199 additions & 184 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ getset = "0.1.3"
rrs-lib = "0.1.0"
rand = { version = "0.8.5", default-features = false }
hex = { version = "0.4.3", default-features = false }
serde_arrays = "0.1.0"

# default-features = false for no_std for use in guest programs
itertools = { version = "0.13.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/circuits/mod-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ num-traits.workspace = true
tracing.workspace = true

itertools.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_with.workspace = true

[dev-dependencies]
openvm-circuit-primitives = { workspace = true }
Expand Down
5 changes: 5 additions & 0 deletions crates/circuits/mod-builder/src/core_chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use openvm_stark_backend::{
p3_matrix::{dense::RowMajorMatrix, Matrix},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};

use crate::{
utils::{biguint_to_limbs_vec, limbs_to_biguint},
Expand Down Expand Up @@ -161,7 +163,10 @@ where
}
}

#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct FieldExpressionRecord {
#[serde_as(as = "Vec<DisplayFromStr>")]
pub inputs: Vec<BigUint>,
pub flags: Vec<bool>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum_dispatch.workspace = true
backtrace.workspace = true
rand.workspace = true
serde.workspace = true
serde_arrays.workspace = true
toml.workspace = true
once_cell.workspace = true
cfg-if.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/vm/src/arch/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use openvm_instructions::{
instruction::Instruction, program::DEFAULT_PC_STEP, PhantomDiscriminant, VmOpcode,
};
use openvm_stark_backend::{interaction::InteractionBuilder, p3_field::FieldAlgebra};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::Streams;
Expand Down Expand Up @@ -105,7 +106,7 @@ impl<F, C: InstructionExecutor<F>> InstructionExecutor<F> for Rc<RefCell<C>> {
}
}

#[derive(Clone, Copy, Debug, PartialEq, Default, AlignedBorrow)]
#[derive(Clone, Copy, Debug, PartialEq, Default, AlignedBorrow, Serialize, Deserialize)]
#[repr(C)]
pub struct ExecutionState<T> {
pub pc: T,
Expand Down
8 changes: 5 additions & 3 deletions crates/vm/src/arch/integration_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use openvm_stark_backend::{
rap::{get_air_name, AnyRap, BaseAirWithPublicValues, PartitionedBaseAir},
Chip, ChipUsageGetter,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use super::{ExecutionState, InstructionExecutor, Result};
use crate::system::memory::{MemoryController, OfflineMemory};
Expand All @@ -40,9 +41,9 @@ pub trait VmAdapterInterface<T> {
/// The adapter AIR should also own `ExecutionBridge` and `MemoryBridge`.
pub trait VmAdapterChip<F> {
/// Records generated by adapter before main instruction execution
type ReadRecord: Send;
type ReadRecord: Send + Serialize + DeserializeOwned;
/// Records generated by adapter after main instruction execution
type WriteRecord: Send;
type WriteRecord: Send + Serialize + DeserializeOwned;
/// AdapterAir should not have public values
type Air: BaseAir<F> + Clone;

Expand Down Expand Up @@ -110,7 +111,7 @@ pub trait VmAdapterAir<AB: AirBuilder>: BaseAir<AB::F> {
/// Trait to be implemented on primitive chip to integrate with the machine.
pub trait VmCoreChip<F, I: VmAdapterInterface<F>> {
/// Minimum data that must be recorded to be able to generate trace for one row of `PrimitiveAir`.
type Record: Send;
type Record: Send + Serialize + DeserializeOwned;
/// The primitive AIR with main constraints that do not depend on memory and other architecture-specifics.
type Air: BaseAirWithPublicValues<F> + Clone;

Expand Down Expand Up @@ -481,6 +482,7 @@ impl<T, PI, const READ_CELLS: usize, const WRITE_CELLS: usize> VmAdapterInterfac

/// An interface that is fully determined during runtime. This should **only** be used as a last resort when static
/// compile-time guarantees cannot be made.
#[derive(Serialize, Deserialize)]
pub struct DynAdapterInterface<T>(PhantomData<T>);

impl<T> VmAdapterInterface<T> for DynAdapterInterface<T> {
Expand Down
3 changes: 2 additions & 1 deletion crates/vm/src/arch/testing/test_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use openvm_stark_backend::{
p3_air::BaseAir,
p3_field::{Field, FieldAlgebra, PrimeField32},
};
use serde::{Deserialize, Serialize};

use crate::{
arch::{
Expand Down Expand Up @@ -47,7 +48,7 @@ impl<F> TestAdapterChip<F> {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize, Deserialize)]
pub struct TestAdapterRecord<T> {
pub from_pc: u32,
pub operands: [T; 7],
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/system/memory/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub const MERKLE_AIR_OFFSET: usize = 1;
/// The offset of the boundary AIR in AIRs of MemoryController.
pub const BOUNDARY_AIR_OFFSET: usize = 0;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct RecordId(pub usize);

pub type MemoryImage<F> = FxHashMap<Address, F>;
Expand Down
8 changes: 6 additions & 2 deletions crates/vm/src/system/native_adapter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use openvm_stark_backend::{
p3_air::BaseAir,
p3_field::{Field, FieldAlgebra, PrimeField32},
};
use serde::{Deserialize, Serialize};

use crate::system::memory::{OfflineMemory, RecordId};

Expand Down Expand Up @@ -52,8 +53,9 @@ impl<F: PrimeField32, const R: usize, const W: usize> NativeAdapterChip<F, R, W>
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct NativeReadRecord<F: Field, const R: usize> {
#[serde(with = "serde_arrays")]
pub reads: [(RecordId, [F; 1]); R],
}

Expand All @@ -67,9 +69,11 @@ impl<F: Field, const R: usize> NativeReadRecord<F, R> {
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(bound = "F: Field")]
pub struct NativeWriteRecord<F: Field, const W: usize> {
pub from_state: ExecutionState<u32>,
#[serde(with = "serde_arrays")]
pub writes: [(RecordId, [F; 1]); W],
}

Expand Down
3 changes: 2 additions & 1 deletion crates/vm/src/system/public_values/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};

use crate::{
arch::{
Expand Down Expand Up @@ -95,7 +96,7 @@ impl<AB: InteractionBuilder + AirBuilderWithPublicValues> VmCoreAir<AB, AdapterI
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct PublicValuesRecord<F> {
value: F,
index: F,
Expand Down
1 change: 1 addition & 0 deletions extensions/algebra/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ strum = { workspace = true }
derive-new = { workspace = true }
serde.workspace = true
serde_with = { workspace = true }
serde_arrays = { workspace = true }

[dev-dependencies]
halo2curves-axiom = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions extensions/algebra/circuit/src/modular_chip/addsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::Deserialize;
use serde_with::serde_derive::Serialize;

/// The number of limbs and limb bits are determined at runtime.
#[derive(Clone)]
Expand Down Expand Up @@ -131,6 +133,7 @@ impl ModularAddSubCoreChip {
}
}

#[derive(Serialize, Deserialize)]
pub struct ModularAddSubCoreRecord {
pub x: BigUint,
pub y: BigUint,
Expand Down
7 changes: 5 additions & 2 deletions extensions/algebra/circuit/src/modular_chip/is_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};

use serde::{Deserialize, Serialize};
// Given two numbers b and c, we want to prove that a) b == c or b != c, depending on
// result of cmp_result and b) b, c < N for some modulus N that is passed into the AIR
// at runtime (i.e. when chip is instantiated).
Expand Down Expand Up @@ -234,12 +234,15 @@ where
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ModularIsEqualCoreRecord<T, const READ_LIMBS: usize> {
pub is_setup: bool,
#[serde(with = "serde_arrays")]
pub b: [T; READ_LIMBS],
#[serde(with = "serde_arrays")]
pub c: [T; READ_LIMBS],
pub cmp_result: T,
#[serde(with = "serde_arrays")]
pub eq_marker: [T; READ_LIMBS],
pub b_diff_idx: usize,
pub c_diff_idx: usize,
Expand Down
3 changes: 3 additions & 0 deletions extensions/algebra/circuit/src/modular_chip/muldiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::Deserialize;
use serde_with::serde_derive::Serialize;

/// The number of limbs and limb bits are determined at runtime.
#[derive(Clone)]
Expand Down Expand Up @@ -146,6 +148,7 @@ impl ModularMulDivCoreChip {
}
}

#[derive(Serialize, Deserialize)]
pub struct ModularMulDivCoreRecord {
pub x: BigUint,
pub y: BigUint,
Expand Down
2 changes: 2 additions & 0 deletions extensions/native/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ derive_more = { workspace = true, features = ["from"] }
rand.workspace = true
eyre.workspace = true
serde.workspace = true
serde_arrays.workspace = true
serde_with.workspace = true
rayon.workspace = true

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions extensions/native/circuit/src/adapters/convert_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ use openvm_stark_backend::{
p3_air::BaseAir,
p3_field::{Field, FieldAlgebra, PrimeField32},
};
use serde::{Deserialize, Serialize};

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct VectorReadRecord<const NUM_READS: usize, const READ_SIZE: usize> {
#[serde(with = "serde_arrays")]
pub reads: [RecordId; NUM_READS],
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct VectorWriteRecord<const WRITE_SIZE: usize> {
pub from_state: ExecutionState<u32>,
pub writes: [RecordId; 1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use openvm_stark_backend::{
p3_air::{AirBuilder, BaseAir},
p3_field::{Field, FieldAlgebra, PrimeField32},
};
use serde::{Deserialize, Serialize};

pub struct NativeLoadStoreInstruction<T> {
pub is_valid: T,
Expand Down Expand Up @@ -73,7 +74,8 @@ impl<F: PrimeField32, const NUM_CELLS: usize> NativeLoadStoreAdapterChip<F, NUM_
}
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "F: Field")]
pub struct NativeLoadStoreReadRecord<F: Field, const NUM_CELLS: usize> {
pub pointer1_read: RecordId,
pub pointer2_read: Option<RecordId>,
Expand All @@ -90,7 +92,8 @@ pub struct NativeLoadStoreReadRecord<F: Field, const NUM_CELLS: usize> {
pub g: F,
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "F: Field")]
pub struct NativeLoadStoreWriteRecord<F: Field, const NUM_CELLS: usize> {
pub from_state: ExecutionState<F>,
pub write_id: RecordId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use openvm_stark_backend::{
p3_air::BaseAir,
p3_field::{Field, FieldAlgebra, PrimeField32},
};
use serde::{Deserialize, Serialize};

#[allow(dead_code)]
#[derive(Debug)]
Expand All @@ -48,13 +49,13 @@ impl<F: PrimeField32, const N: usize> NativeVectorizedAdapterChip<F, N> {
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct NativeVectorizedReadRecord<const N: usize> {
pub b: RecordId,
pub c: RecordId,
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct NativeVectorizedWriteRecord<const N: usize> {
pub from_state: ExecutionState<u32>,
pub a: RecordId,
Expand Down
4 changes: 3 additions & 1 deletion extensions/native/circuit/src/castf/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};

// LIMB_BITS is the size of the limbs in bits.
pub(crate) const LIMB_BITS: usize = 8;
// the final limb has only 6 bits
Expand Down Expand Up @@ -95,7 +97,7 @@ where
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct CastFRecord<F> {
pub in_val: F,
pub out_val: [F; RV32_REGISTER_NUM_LIMBS],
Expand Down
3 changes: 2 additions & 1 deletion extensions/native/circuit/src/field_arithmetic/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};

#[repr(C)]
#[derive(AlignedBorrow)]
Expand Down Expand Up @@ -101,7 +102,7 @@ where
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct FieldArithmeticRecord<F> {
pub opcode: FieldArithmeticOpcode,
pub a: F,
Expand Down
3 changes: 2 additions & 1 deletion extensions/native/circuit/src/field_extension/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};

pub const BETA: usize = 11;
pub const EXT_DEG: usize = 4;
Expand Down Expand Up @@ -129,7 +130,7 @@ where
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct FieldExtensionRecord<F> {
pub opcode: FieldExtensionOpcode,
pub x: [F; EXT_DEG],
Expand Down
3 changes: 2 additions & 1 deletion extensions/native/circuit/src/jal/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use openvm_stark_backend::{
p3_field::{Field, FieldAlgebra, PrimeField32},
rap::BaseAirWithPublicValues,
};
use serde::{Deserialize, Serialize};

#[repr(C)]
#[derive(AlignedBorrow)]
Expand Down Expand Up @@ -64,7 +65,7 @@ where
}
}

#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct JalRecord<F> {
pub imm: F,
}
Expand Down
Loading

0 comments on commit 63438a8

Please sign in to comment.