Skip to content

Commit bd7b3da

Browse files
author
Gilad Chase
committed
Add types module
move [Packed]CasmState there, including rename VmState -> CasmState
1 parent 0e0ba19 commit bd7b3da

File tree

7 files changed

+58
-63
lines changed

7 files changed

+58
-63
lines changed

crates/prover/src/cairo_air/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::components::memory::{ClaimGenerator, Component as MemoryComponent, Ev
2424
use crate::input::instructions::VmState;
2525
use crate::input::CairoInput;
2626
use crate::relations::MemoryRelation;
27+
use crate::utils::types::CasmState;
2728

2829
#[derive(Serialize, Deserialize)]
2930
pub struct CairoProof<H: MerkleHasher> {
@@ -36,8 +37,8 @@ pub struct CairoProof<H: MerkleHasher> {
3637
pub struct CairoClaim {
3738
// Common claim values.
3839
pub public_memory: Vec<(M31, QM31)>,
39-
pub initial_state: VmState,
40-
pub final_state: VmState,
40+
pub initial_state: CasmState,
41+
pub final_state: CasmState,
4142

4243
pub addr_to_value: Claim,
4344
// pub ret: Vec<ret_opcode::Claim>,

crates/prover/src/components/add_mul_opcode/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ mod tests {
2727
use super::*;
2828
use crate::components::add_mul_opcode::component::INSTRUCTION_BASE;
2929
use crate::components::memory;
30-
use crate::input::instructions::VmState;
3130
use crate::relations;
31+
use crate::utils::types::CasmState;
3232

3333
#[test]
3434
fn test_add_mul_opcode() {
@@ -79,15 +79,15 @@ mod tests {
7979
let claim_generator = ClaimGenerator::new(
8080
chain!(
8181
vec![
82-
VmState {
82+
CasmState {
8383
pc: 0,
8484
ap: 2,
8585
fp: 4,
8686
};
8787
128
8888
],
8989
vec![
90-
VmState {
90+
CasmState {
9191
pc: 1,
9292
ap: 2,
9393
fp: 4,

crates/prover/src/components/add_mul_opcode/prover.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,19 @@ use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleChannel;
1515
use super::component::{Claim, InteractionClaim, INSTRUCTION_BASE};
1616
use crate::components::add_mul_opcode::component::N_TRACE_COLUMNS;
1717
use crate::components::memory;
18-
use crate::input::instructions::VmState;
1918
use crate::relations::{MemoryRelation, StateRelation, N_MEMORY_ELEMS, STATE_SIZE};
2019
use crate::utils::prover::decode_opcode;
20+
use crate::utils::types::{CasmState, PackedCasmState};
2121
use crate::utils::{Selector, SelectorTrait};
2222

2323
const N_MEMORY_LOOKUPS: usize = 4;
2424
const N_STATE_LOOKUPS: usize = 2;
2525

26-
// TODO(Ohad): take from prover_types and remove.
27-
pub struct PackedVmState {
28-
pub pc: PackedM31,
29-
pub ap: PackedM31,
30-
pub fp: PackedM31,
31-
}
32-
3326
pub struct ClaimGenerator {
34-
pub inputs: Vec<PackedVmState>,
27+
pub inputs: Vec<PackedCasmState>,
3528
}
3629
impl ClaimGenerator {
37-
pub fn new(mut inputs: Vec<VmState>) -> Self {
30+
pub fn new(mut inputs: Vec<CasmState>) -> Self {
3831
assert!(!inputs.is_empty());
3932

4033
// TODO(spapini): Split to multiple components.
@@ -44,7 +37,7 @@ impl ClaimGenerator {
4437
let inputs = inputs
4538
.into_iter()
4639
.array_chunks::<N_LANES>()
47-
.map(|chunk| PackedVmState {
40+
.map(|chunk| PackedCasmState {
4841
pc: PackedM31::from_array(std::array::from_fn(|i| {
4942
M31::from_u32_unchecked(chunk[i].pc)
5043
})),
@@ -157,7 +150,7 @@ impl InteractionClaimGenerator {
157150
}
158151

159152
fn write_trace_simd(
160-
inputs: &[PackedVmState],
153+
inputs: &[PackedCasmState],
161154
memory_trace_generator: &memory::ClaimGenerator,
162155
) -> (
163156
Vec<CircleEvaluation<SimdBackend, M31, BitReversedOrder>>,
@@ -199,7 +192,7 @@ fn write_trace_simd(
199192
// | State (3) | flags (5) | offsets (3) | addrs (3) | values (3 * 4) |
200193
fn write_trace_row(
201194
trace: &mut [Col<SimdBackend, M31>],
202-
input: &PackedVmState,
195+
input: &PackedCasmState,
203196
row_index: usize,
204197
interaction_claim_generator: &mut InteractionClaimGenerator,
205198
memory_trace_generator: &memory::ClaimGenerator,

crates/prover/src/components/ret_opcode/prover.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,19 @@ use stwo_prover::core::vcs::blake2_merkle::Blake2sMerkleChannel;
1515

1616
use super::component::{Claim, InteractionClaim, RET_INSTRUCTION};
1717
use crate::components::memory;
18-
use crate::input::instructions::VmState;
1918
use crate::relations::{MemoryRelation, StateRelation, N_MEMORY_ELEMS, STATE_SIZE};
19+
use crate::utils::types::{CasmState, PackedCasmState};
2020

2121
const N_TRACE_COLUMNS: usize = 5;
22-
2322
const N_MEMORY_LOOKUPS: usize = 3;
2423
const N_STATE_LOOKUPS: usize = 2;
2524

26-
// TODO(Ohad): take from prover_types and remove.
27-
#[derive(Debug, Clone)]
28-
pub struct PackedCasmState {
29-
pub pc: PackedM31,
30-
pub ap: PackedM31,
31-
pub fp: PackedM31,
32-
}
33-
3425
#[derive(Debug)]
3526
pub struct ClaimGenerator {
3627
pub inputs: Vec<PackedCasmState>,
3728
}
3829
impl ClaimGenerator {
39-
pub fn new(mut inputs: Vec<VmState>) -> Self {
30+
pub fn new(mut inputs: Vec<CasmState>) -> Self {
4031
assert!(!inputs.is_empty());
4132

4233
// TODO(spapini): Split to multiple components.

crates/prover/src/input/instructions.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
use serde::{Deserialize, Serialize};
2-
31
use super::decode::Instruction;
42
use super::mem::{MemoryBuilder, MemoryValue};
53
use super::vm_import::TraceEntry;
6-
7-
// TODO(spapini): Move this:
8-
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
9-
pub struct VmState {
10-
pub pc: u32,
11-
pub ap: u32,
12-
pub fp: u32,
13-
}
14-
impl From<TraceEntry> for VmState {
15-
fn from(entry: TraceEntry) -> Self {
16-
Self {
17-
pc: entry.pc as u32,
18-
ap: entry.ap as u32,
19-
fp: entry.fp as u32,
20-
}
21-
}
22-
}
4+
use crate::utils::types::CasmState;
235

246
// TODO(yuval/alonT): consider making the indexing mechanism more explicit in the code).
257
/// The instructions usage in the input, split to Stwo opcodes.
@@ -30,44 +12,44 @@ impl From<TraceEntry> for VmState {
3012
/// Note: for the flag "fp/ap", true means fp-based and false means ap-based.
3113
#[derive(Debug, Default)]
3214
pub struct Instructions {
33-
pub initial_state: VmState,
34-
pub final_state: VmState,
15+
pub initial_state: CasmState,
16+
pub final_state: CasmState,
3517

3618
/// ret.
37-
pub ret: Vec<VmState>,
19+
pub ret: Vec<CasmState>,
3820

3921
/// ap += imm.
40-
pub add_ap: Vec<VmState>,
22+
pub add_ap: Vec<CasmState>,
4123

4224
/// jump rel imm.
4325
/// Flags: ap++?.
44-
pub jmp_rel_imm: [Vec<VmState>; 2],
26+
pub jmp_rel_imm: [Vec<CasmState>; 2],
4527

4628
/// jump abs [fp/ap + offset].
4729
/// Flags: fp/ap, ap++?.
48-
pub jmp_abs: [Vec<VmState>; 4],
30+
pub jmp_abs: [Vec<CasmState>; 4],
4931

5032
/// call rel imm.
51-
pub call_rel_imm: Vec<VmState>,
33+
pub call_rel_imm: Vec<CasmState>,
5234

5335
/// call abs [fp/ap + offset].
5436
/// Flags: fp/ap.
55-
pub call_abs: [Vec<VmState>; 2],
37+
pub call_abs: [Vec<CasmState>; 2],
5638

5739
/// jump rel imm if [fp/ap + offset] != 0.
5840
/// Flags: fp/ap, taken?, ap++?.
59-
pub jnz_imm: [Vec<VmState>; 8],
41+
pub jnz_imm: [Vec<CasmState>; 8],
6042

6143
/// - [fp/ap + offset0] = [fp/ap + offset2]
62-
pub mov_mem: Vec<VmState>,
44+
pub mov_mem: Vec<CasmState>,
6345

6446
/// - [fp/ap + offset0] = [[fp/ap + offset1] + offset2]
65-
pub deref: Vec<VmState>,
47+
pub deref: Vec<CasmState>,
6648

6749
/// - [fp/ap + offset0] = imm
68-
pub push_imm: Vec<VmState>,
50+
pub push_imm: Vec<CasmState>,
6951

70-
pub generic: Vec<VmState>,
52+
pub generic: Vec<CasmState>,
7153
}
7254
impl Instructions {
7355
pub fn from_iter(mut iter: impl Iterator<Item = TraceEntry>, mem: &mut MemoryBuilder) -> Self {
@@ -86,8 +68,8 @@ impl Instructions {
8668
res
8769
}
8870

89-
fn push_instr(&mut self, mem: &mut MemoryBuilder, state: VmState) {
90-
let VmState { ap, fp, pc } = state;
71+
fn push_instr(&mut self, mem: &mut MemoryBuilder, state: CasmState) {
72+
let CasmState { ap, fp, pc } = state;
9173
let instruction = mem.get_inst(pc);
9274
let instruction = Instruction::decode(instruction);
9375
match instruction {

crates/prover/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod component;
22
pub mod prover;
3+
pub mod types;
34

45
use std::ops::{Add, Mul, Sub};
56

crates/prover/src/utils/types.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use serde::{Deserialize, Serialize};
2+
use stwo_prover::core::backend::simd::m31::PackedM31;
3+
4+
use crate::input::vm_import::TraceEntry;
5+
6+
#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
7+
pub struct CasmState {
8+
pub pc: u32,
9+
pub ap: u32,
10+
pub fp: u32,
11+
}
12+
impl From<TraceEntry> for CasmState {
13+
fn from(entry: TraceEntry) -> Self {
14+
Self {
15+
pc: entry.pc as u32,
16+
ap: entry.ap as u32,
17+
fp: entry.fp as u32,
18+
}
19+
}
20+
}
21+
22+
#[derive(Debug, Clone)]
23+
pub struct PackedCasmState {
24+
pub pc: PackedM31,
25+
pub ap: PackedM31,
26+
pub fp: PackedM31,
27+
}

0 commit comments

Comments
 (0)