Skip to content

Commit a2ba567

Browse files
committed
wrapping operations are nice
1 parent 4a0f655 commit a2ba567

File tree

3 files changed

+75
-77
lines changed

3 files changed

+75
-77
lines changed

src/lib.rs

-59
Original file line numberDiff line numberDiff line change
@@ -19,62 +19,3 @@ mod vm_specs;
1919
// STARK tables -------------
2020
#[allow(dead_code)]
2121
mod stark_program_instructions;
22-
23-
#[cfg(test)]
24-
mod tests {
25-
use std::collections::HashMap;
26-
27-
use crate::{
28-
preflight_simulator::PreflightSimulation,
29-
vm_specs::{
30-
Instruction,
31-
MemoryLocation,
32-
Program,
33-
Register,
34-
},
35-
};
36-
37-
#[test]
38-
/// Tests whether two numbers in memory can be added together
39-
/// in the ZKVM
40-
fn test_preflight_add_memory() {
41-
let instructions = vec![
42-
Instruction::Lb(Register::R0, MemoryLocation(0x40)),
43-
Instruction::Lb(Register::R1, MemoryLocation(0x41)),
44-
Instruction::Add(Register::R0, Register::R1),
45-
Instruction::Sb(Register::R0, MemoryLocation(0x42)),
46-
Instruction::Halt,
47-
];
48-
49-
let code = instructions
50-
.into_iter()
51-
.enumerate()
52-
.map(|(idx, inst)| (idx as u8, inst))
53-
.collect::<HashMap<u8, Instruction>>();
54-
55-
let memory_init: HashMap<u8, u8> =
56-
HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]);
57-
58-
let program = Program {
59-
entry_point: 0,
60-
code,
61-
memory_init,
62-
};
63-
64-
let expected = (0x42, 0x65);
65-
66-
let simulation = PreflightSimulation::simulate(&program);
67-
assert!(simulation.is_ok());
68-
let simulation = simulation.unwrap();
69-
70-
assert_eq!(
71-
simulation.trace_rows[simulation
72-
.trace_rows
73-
.len()
74-
- 1]
75-
.get_memory_at(&expected.0)
76-
.unwrap(),
77-
expected.1
78-
);
79-
}
80-
}

src/preflight_simulator.rs

+71-14
Original file line numberDiff line numberDiff line change
@@ -98,28 +98,28 @@ impl SimulationRow {
9898

9999
match self.instruction {
100100
Instruction::Add(a, b) => {
101-
registers[usize::from(a)] += registers[usize::from(b)]
101+
registers[usize::from(a)] = registers[usize::from(a)]
102+
.wrapping_add(registers[usize::from(b)]);
102103
}
103104
Instruction::Sub(a, b) => {
104-
registers[usize::from(a)] -= registers[usize::from(b)]
105+
registers[usize::from(a)] = registers[usize::from(a)]
106+
.wrapping_sub(registers[usize::from(b)]);
105107
}
106108
Instruction::Mul(a, b) => {
107-
registers[usize::from(a)] *= registers[usize::from(b)]
109+
registers[usize::from(a)] = registers[usize::from(a)]
110+
.wrapping_mul(registers[usize::from(b)]);
108111
}
109112
Instruction::Div(a, b) => {
110-
registers[usize::from(a)] /= registers[usize::from(b)]
113+
registers[usize::from(a)] = registers[usize::from(a)]
114+
.wrapping_div(registers[usize::from(b)]);
111115
}
112-
Instruction::Bsl(reg, amount) => {
113-
if registers[usize::from(amount)] >= 8 {
114-
return Err(anyhow!("invalid shift amount"));
115-
}
116-
registers[usize::from(reg)] <<= registers[usize::from(amount)];
116+
Instruction::Shl(reg, amount) => {
117+
registers[usize::from(reg)] = registers[usize::from(reg)]
118+
.wrapping_shl(registers[usize::from(amount)].into());
117119
}
118-
Instruction::Bsr(reg, amount) => {
119-
if registers[usize::from(amount)] >= 8 {
120-
return Err(anyhow!("invalid shift amount"));
121-
}
122-
registers[usize::from(reg)] >>= registers[usize::from(amount)];
120+
Instruction::Shr(reg, amount) => {
121+
registers[usize::from(reg)] = registers[usize::from(reg)]
122+
.wrapping_shr(registers[usize::from(amount)].into());
123123
}
124124
Instruction::Lb(reg, memloc) => {
125125
registers[usize::from(reg)] = self
@@ -197,3 +197,60 @@ impl PreflightSimulation {
197197
Ok(Self { trace_rows })
198198
}
199199
}
200+
201+
#[cfg(test)]
202+
mod tests {
203+
use super::*;
204+
use std::collections::HashMap;
205+
206+
use crate::vm_specs::{
207+
Instruction,
208+
MemoryLocation,
209+
Program,
210+
Register,
211+
};
212+
213+
#[test]
214+
/// Tests whether two numbers in memory can be added together
215+
/// in the ZKVM
216+
fn test_preflight_add_memory() {
217+
let instructions = vec![
218+
Instruction::Lb(Register::R0, MemoryLocation(0x40)),
219+
Instruction::Lb(Register::R1, MemoryLocation(0x41)),
220+
Instruction::Add(Register::R0, Register::R1),
221+
Instruction::Sb(Register::R0, MemoryLocation(0x42)),
222+
Instruction::Halt,
223+
];
224+
225+
let code = instructions
226+
.into_iter()
227+
.enumerate()
228+
.map(|(idx, inst)| (idx as u8, inst))
229+
.collect::<HashMap<u8, Instruction>>();
230+
231+
let memory_init: HashMap<u8, u8> =
232+
HashMap::from_iter(vec![(0x40, 0x20), (0x41, 0x45)]);
233+
234+
let program = Program {
235+
entry_point: 0,
236+
code,
237+
memory_init,
238+
};
239+
240+
let expected = (0x42, 0x65);
241+
242+
let simulation = PreflightSimulation::simulate(&program);
243+
assert!(simulation.is_ok());
244+
let simulation = simulation.unwrap();
245+
246+
assert_eq!(
247+
simulation.trace_rows[simulation
248+
.trace_rows
249+
.len()
250+
- 1]
251+
.get_memory_at(&expected.0)
252+
.unwrap(),
253+
expected.1
254+
);
255+
}
256+
}

src/vm_specs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub enum Instruction {
3030
Sub(Register, Register),
3131
Mul(Register, Register),
3232
Div(Register, Register),
33-
Bsl(Register, Register),
34-
Bsr(Register, Register),
33+
Shl(Register, Register),
34+
Shr(Register, Register),
3535
Lb(Register, MemoryLocation),
3636
Sb(Register, MemoryLocation),
3737
#[default]
@@ -47,8 +47,8 @@ impl Instruction {
4747
Instruction::Sub(_, _) => 1,
4848
Instruction::Mul(_, _) => 2,
4949
Instruction::Div(_, _) => 3,
50-
Instruction::Bsl(_, _) => 4,
51-
Instruction::Bsr(_, _) => 5,
50+
Instruction::Shl(_, _) => 4,
51+
Instruction::Shr(_, _) => 5,
5252
Instruction::Lb(_, _) => 6,
5353
Instruction::Sb(_, _) => 7,
5454
Instruction::Halt => 8,

0 commit comments

Comments
 (0)