-
Notifications
You must be signed in to change notification settings - Fork 35
/
vm.rs
390 lines (351 loc) Β· 13.4 KB
/
vm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use crate::tables::BrainfuckColumn;
use crate::tables::InputBaseColumn;
use crate::tables::InstructionBaseColumn;
use crate::tables::MemoryBaseColumn;
use crate::tables::OutputBaseColumn;
use crate::tables::ProcessorBaseColumn;
use crate::trace::into_columns;
use crate::BrainfuckTrace;
use ark_ff::Field;
use ark_ff::One;
use ark_ff::Zero;
use ministark::Matrix;
type Fp = <BrainfuckTrace as ministark::Trace>::Fp;
/// Opcodes determined by the lexer
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OpCode {
IncrementPointer = b'>' as isize,
DecrementPointer = b'<' as isize,
Increment = b'+' as isize,
Decrement = b'-' as isize,
Write = b'.' as isize,
Read = b',' as isize,
LoopBegin = b'[' as isize,
LoopEnd = b']' as isize,
}
impl OpCode {
pub const VALUES: [OpCode; 8] = [
OpCode::IncrementPointer,
OpCode::DecrementPointer,
OpCode::Increment,
OpCode::Decrement,
OpCode::Write,
OpCode::Read,
OpCode::LoopBegin,
OpCode::LoopEnd,
];
}
/// Lexer turns the source code into a sequence of opcodes
fn lex(source: &str) -> Vec<OpCode> {
let mut operations = Vec::new();
for symbol in source.chars() {
let op = match symbol {
'>' => Some(OpCode::IncrementPointer),
'<' => Some(OpCode::DecrementPointer),
'+' => Some(OpCode::Increment),
'-' => Some(OpCode::Decrement),
'.' => Some(OpCode::Write),
',' => Some(OpCode::Read),
'[' => Some(OpCode::LoopBegin),
']' => Some(OpCode::LoopEnd),
_ => None,
};
// Non-opcode characters are comments
if let Some(op) = op {
operations.push(op);
}
}
operations
}
pub fn compile(source: &str) -> Vec<usize> {
let opcodes = lex(source);
let mut program = Vec::new();
let mut stack = Vec::new();
for opcode in opcodes.into_iter() {
program.push(opcode as usize);
match opcode {
OpCode::LoopBegin => {
// Placeholder for position of loop end
program.push(0);
stack.push(program.len() - 1);
}
OpCode::LoopEnd => {
let last = stack.pop().expect("loop has no beginning");
program.push(last + 1); // loop end
program[last] = program.len(); // loop beginning
}
_ => (),
}
}
program
}
/// Registers of the brainfuck VM
#[derive(Default)]
struct Register {
/// Cycle
cycle: usize,
/// Instruction pointer
ip: usize,
/// Current instruction
curr_instr: usize,
/// Next instruction
next_instr: usize,
/// Memory pointer
mp: usize,
/// Memory value
mem_val: usize,
}
// Outputs base execution trace
pub fn simulate(
source_code: &str,
input: &mut impl std::io::Read,
output: &mut impl std::io::Write,
) -> BrainfuckTrace {
let program = compile(source_code);
let mut tape = [0u8; 1024];
let mut register = Register {
curr_instr: program[0],
next_instr: if program.len() == 1 { 0 } else { program[1] },
..Default::default()
};
let mut input_symbols = Vec::new();
let mut output_symbols = Vec::new();
// execution trace tables in row major
let mut processor_rows = Vec::new();
let mut instruction_rows = Vec::new();
let mut input_rows = Vec::new();
let mut output_rows = Vec::new();
// load BF code
for i in 0..program.len() {
use InstructionBaseColumn::*;
let mut row = [Fp::zero(); InstructionBaseColumn::NUM_TRACE_COLUMNS];
row[Ip as usize] = Fp::from(i as u64);
row[CurrInstr as usize] = Fp::from(program[i] as u64);
row[NextInstr as usize] = Fp::from(program.get(i + 1).map_or(0, |&x| x as u64));
instruction_rows.push(row);
}
// main loop
while register.ip < program.len() {
let mem_val = Fp::from(register.mem_val as u64);
processor_rows.push({
use ProcessorBaseColumn::*;
let mut row = [Fp::zero(); ProcessorBaseColumn::NUM_TRACE_COLUMNS];
row[Cycle as usize] = Fp::from(register.cycle as u64);
row[Ip as usize] = Fp::from(register.ip as u64);
row[CurrInstr as usize] = Fp::from(register.curr_instr as u64);
row[NextInstr as usize] = Fp::from(register.next_instr as u64);
row[Mp as usize] = Fp::from(register.mp as u64);
row[MemVal as usize] = mem_val;
row[MemValInv as usize] = mem_val.inverse().unwrap_or_else(Fp::zero);
row[Dummy as usize] = Fp::from(register.curr_instr == 0);
row
});
instruction_rows.push({
use InstructionBaseColumn::*;
let mut row = [Fp::zero(); InstructionBaseColumn::NUM_TRACE_COLUMNS];
row[Ip as usize] = Fp::from(register.ip as u64);
row[CurrInstr as usize] = Fp::from(register.curr_instr as u64);
row[NextInstr as usize] = Fp::from(register.next_instr as u64);
row
});
// Update pointer registers according to instruction
if register.curr_instr == OpCode::LoopBegin as usize {
register.ip = if register.mem_val == 0 {
program[register.ip + 1]
} else {
register.ip + 2
};
} else if register.curr_instr == OpCode::LoopEnd as usize {
register.ip = if register.mem_val != 0 {
program[register.ip + 1]
} else {
register.ip + 2
}
} else if register.curr_instr == OpCode::DecrementPointer as usize {
register.ip += 1;
register.mp -= 1;
} else if register.curr_instr == OpCode::IncrementPointer as usize {
register.ip += 1;
register.mp += 1;
} else if register.curr_instr == OpCode::Increment as usize {
register.ip += 1;
tape[register.mp] += 1;
} else if register.curr_instr == OpCode::Decrement as usize {
register.ip += 1;
tape[register.mp] -= 1;
} else if register.curr_instr == OpCode::Write as usize {
register.ip += 1;
let x = &tape[register.mp..register.mp + 1];
output.write_all(x).expect("failed to write output");
output_rows.push([x[0].into()]);
output_symbols.push(x[0]);
} else if register.curr_instr == OpCode::Read as usize {
register.ip += 1;
let mut x = [0u8; 1];
input.read_exact(&mut x).expect("failed to read input");
tape[register.mp] = x[0];
input_rows.push([x[0].into()]);
input_symbols.push(x[0]);
} else {
panic!("unrecognized instruction at ip:{}", register.ip);
}
register.cycle += 1;
register.curr_instr = program.get(register.ip).map_or(0, |&x| x);
register.next_instr = program.get(register.ip + 1).map_or(0, |&x| x);
register.mem_val = tape[register.mp].into();
}
// Collect final state into execution tables
let mem_val = Fp::from(register.mem_val as u64);
processor_rows.push({
use ProcessorBaseColumn::*;
let mut row = [Fp::zero(); ProcessorBaseColumn::NUM_TRACE_COLUMNS];
row[Cycle as usize] = Fp::from(register.cycle as u64);
row[Ip as usize] = Fp::from(register.ip as u64);
row[CurrInstr as usize] = Fp::from(register.curr_instr as u64);
row[NextInstr as usize] = Fp::from(register.next_instr as u64);
row[Mp as usize] = Fp::from(register.mp as u64);
row[MemVal as usize] = mem_val;
row[MemValInv as usize] = mem_val.inverse().unwrap_or_default();
row[Dummy as usize] = Fp::from(register.curr_instr == 0);
row
});
instruction_rows.push({
use InstructionBaseColumn::*;
let mut row = [Fp::zero(); InstructionBaseColumn::NUM_TRACE_COLUMNS];
row[Ip as usize] = Fp::from(register.ip as u64);
row[CurrInstr as usize] = Fp::from(register.curr_instr as u64);
row[NextInstr as usize] = Fp::from(register.next_instr as u64);
row
});
// sort instructions by address
instruction_rows.sort_by_key(|row| row[0]);
let mut memory_rows = derive_memory_rows(&processor_rows);
let padding_len = {
let max_length = [
processor_rows.len(),
memory_rows.len(),
instruction_rows.len(),
input_rows.len(),
output_rows.len(),
]
.into_iter()
.max()
.unwrap();
ceil_power_of_two(max_length)
};
pad_processor_rows(&mut processor_rows, padding_len);
pad_memory_rows(&mut memory_rows, padding_len);
pad_instruction_rows(&mut instruction_rows, padding_len);
pad_input_rows(&mut input_rows, padding_len);
pad_output_rows(&mut output_rows, padding_len);
let processor_base_trace = Matrix::new(into_columns(processor_rows));
let memory_base_trace = Matrix::new(into_columns(memory_rows));
let instruction_base_trace = Matrix::new(into_columns(instruction_rows));
let input_base_trace = Matrix::new(into_columns(input_rows));
let output_base_trace = Matrix::new(into_columns(output_rows));
BrainfuckTrace::new(
processor_base_trace,
memory_base_trace,
instruction_base_trace,
input_base_trace,
output_base_trace,
)
}
fn pad_processor_rows(rows: &mut Vec<[Fp; ProcessorBaseColumn::NUM_TRACE_COLUMNS]>, n: usize) {
use ProcessorBaseColumn::*;
while rows.len() < n {
let last_row = rows.last().unwrap();
let mut new_row = [Fp::zero(); ProcessorBaseColumn::NUM_TRACE_COLUMNS];
new_row[Cycle as usize] = last_row[Cycle as usize] + Fp::one();
new_row[Ip as usize] = last_row[Ip as usize];
new_row[CurrInstr as usize] = Fp::zero();
new_row[NextInstr as usize] = Fp::zero();
new_row[Mp as usize] = last_row[Mp as usize];
new_row[MemVal as usize] = last_row[MemVal as usize];
new_row[MemValInv as usize] = last_row[MemValInv as usize];
new_row[Dummy as usize] = Fp::one();
rows.push(new_row);
}
}
fn pad_memory_rows(rows: &mut Vec<[Fp; MemoryBaseColumn::NUM_TRACE_COLUMNS]>, n: usize) {
use MemoryBaseColumn::*;
while rows.len() < n {
let last_row = rows.last().unwrap();
let mut new_row = [Fp::zero(); MemoryBaseColumn::NUM_TRACE_COLUMNS];
new_row[Cycle as usize] = last_row[Cycle as usize] + Fp::one();
new_row[Mp as usize] = last_row[Mp as usize];
new_row[MemVal as usize] = last_row[MemVal as usize];
new_row[Dummy as usize] = Fp::one();
rows.push(new_row);
}
}
fn pad_instruction_rows(rows: &mut Vec<[Fp; InstructionBaseColumn::NUM_TRACE_COLUMNS]>, n: usize) {
use InstructionBaseColumn::*;
let last_ip = rows.last().unwrap()[Ip as usize];
while rows.len() < n {
let mut new_row = [Fp::zero(); InstructionBaseColumn::NUM_TRACE_COLUMNS];
new_row[Ip as usize] = last_ip;
new_row[CurrInstr as usize] = Fp::zero();
new_row[NextInstr as usize] = Fp::zero();
rows.push(new_row);
}
}
fn pad_input_rows(rows: &mut Vec<[Fp; InputBaseColumn::NUM_TRACE_COLUMNS]>, n: usize) {
while rows.len() < n {
let new_row = [Fp::zero(); InputBaseColumn::NUM_TRACE_COLUMNS];
rows.push(new_row);
}
}
fn pad_output_rows(rows: &mut Vec<[Fp; OutputBaseColumn::NUM_TRACE_COLUMNS]>, n: usize) {
while rows.len() < n {
let new_row = [Fp::zero(); OutputBaseColumn::NUM_TRACE_COLUMNS];
rows.push(new_row);
}
}
fn derive_memory_rows(
processor_rows: &[[Fp; ProcessorBaseColumn::NUM_TRACE_COLUMNS]],
) -> Vec<[Fp; MemoryBaseColumn::NUM_TRACE_COLUMNS]> {
use MemoryBaseColumn::*;
let mut memory_rows = processor_rows
.iter()
.filter_map(|row| {
if row[ProcessorBaseColumn::CurrInstr as usize].is_zero() {
None
} else {
let mut mem_row = [Fp::zero(); MemoryBaseColumn::NUM_TRACE_COLUMNS];
mem_row[Cycle as usize] = row[ProcessorBaseColumn::Cycle as usize];
mem_row[Mp as usize] = row[ProcessorBaseColumn::Mp as usize];
mem_row[MemVal as usize] = row[ProcessorBaseColumn::MemVal as usize];
mem_row[Dummy as usize] = Fp::zero();
Some(mem_row)
}
})
.collect::<Vec<_>>();
memory_rows.sort_by_key(|row| (row[Mp as usize], row[Cycle as usize]));
// insert dummy rows for smooth clk jumps
let mut i = 0;
while i < memory_rows.len() - 1 {
let curr = &memory_rows[i];
let next = &memory_rows[i + 1];
if curr[Mp as usize] == next[Mp as usize]
&& curr[Cycle as usize] + Fp::one() != next[Cycle as usize]
{
let mut dummy_row = [Fp::zero(); MemoryBaseColumn::NUM_TRACE_COLUMNS];
dummy_row[Cycle as usize] = curr[Cycle as usize] + Fp::one();
dummy_row[Mp as usize] = curr[Mp as usize];
dummy_row[MemVal as usize] = curr[MemVal as usize];
dummy_row[Dummy as usize] = Fp::one();
memory_rows.insert(i + 1, dummy_row)
}
i += 1;
}
memory_rows
}
/// Rounds the input value up the the nearest power of two
fn ceil_power_of_two(value: usize) -> usize {
if value.is_power_of_two() {
value
} else {
value.next_power_of_two()
}
}