Skip to content

Commit 057dfec

Browse files
authored
Revamp lean vm (#56)
* utils: faster add_multilinears * cleanup * utils: add add_multilinears_inplace * chore: revamp lean vm crate * fmt * add memory tests * add update_call_counters * make some functions constants * fmt * more constant values
1 parent 53afb35 commit 057dfec

31 files changed

+2174
-1288
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! VM constants and configuration
2+
3+
/// Vector dimension for field operations
4+
pub const DIMENSION: usize = 5;
5+
6+
/// Logarithm of vector length
7+
pub const LOG_VECTOR_LEN: usize = 3;
8+
9+
/// Vector length (2^LOG_VECTOR_LEN)
10+
pub const VECTOR_LEN: usize = 1 << LOG_VECTOR_LEN;
11+
12+
/// Convention: vectorized pointer of size 2, pointing to 16 zeros
13+
pub const ZERO_VEC_PTR: usize = 0;
14+
15+
/// Convention: vectorized pointer of size 1, pointing to 10000000
16+
pub const ONE_VEC_PTR: usize = 2;
17+
18+
/// Convention: vectorized pointer of size 2, = the 16 elements of poseidon_16(0)
19+
pub const POSEIDON_16_NULL_HASH_PTR: usize = 3;
20+
21+
/// Convention: vectorized pointer of size 1, = the last 8 elements of poseidon_24(0)
22+
pub const POSEIDON_24_NULL_HASH_PTR: usize = 5;
23+
24+
/// Normal pointer to start of public input
25+
pub const PUBLIC_INPUT_START: usize = 6 * 8;
26+
27+
/// Maximum memory size for VM runner
28+
pub const MAX_RUNNER_MEMORY_SIZE: usize = 1 << 20;

crates/lean_vm/src/core/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//! Core VM abstractions and type definitions
2+
3+
pub mod constants;
4+
pub mod types;
5+
6+
pub use constants::*;
7+
pub use types::*;

crates/lean_vm/src/core/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Core type definitions for the VM
2+
3+
use p3_koala_bear::{KoalaBear, QuinticExtensionFieldKB};
4+
5+
/// Base field type for VM operations
6+
pub type F = KoalaBear;
7+
8+
/// Extension field type for VM operations
9+
pub type EF = QuinticExtensionFieldKB;
10+
11+
/// Location in source code for debugging
12+
pub type LocationInSourceCode = usize;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//! VM error types and execution results
2+
3+
use crate::core::F;
4+
use crate::execution::Memory;
5+
use crate::witness::{
6+
WitnessDotProduct, WitnessMultilinearEval, WitnessPoseidon16, WitnessPoseidon24,
7+
};
8+
use thiserror::Error;
9+
10+
/// Errors that can occur during VM execution
11+
#[derive(Debug, Clone, Error)]
12+
pub enum RunnerError {
13+
#[error("Out of memory")]
14+
OutOfMemory,
15+
16+
#[error("Memory already set")]
17+
MemoryAlreadySet,
18+
19+
#[error("Not a pointer")]
20+
NotAPointer,
21+
22+
#[error("Division by zero")]
23+
DivByZero,
24+
25+
#[error("Computation invalid: {0} != {1}")]
26+
NotEqual(F, F),
27+
28+
#[error("Undefined memory access")]
29+
UndefinedMemory,
30+
31+
#[error("Program counter out of bounds")]
32+
PCOutOfBounds,
33+
34+
#[error("Point for multilinear eval not padded with zeros")]
35+
MultilinearEvalPointNotPadded,
36+
}
37+
38+
/// Result type for VM operations
39+
pub type VMResult<T> = Result<T, RunnerError>;
40+
41+
/// Execution result containing outputs and execution data
42+
#[derive(Debug)]
43+
pub struct ExecutionResult {
44+
pub no_vec_runtime_memory: usize,
45+
pub public_memory_size: usize,
46+
pub memory: Memory,
47+
pub pcs: Vec<usize>,
48+
pub fps: Vec<usize>,
49+
pub poseidons_16: Vec<WitnessPoseidon16>,
50+
pub poseidons_24: Vec<WitnessPoseidon24>,
51+
pub dot_products: Vec<WitnessDotProduct>,
52+
pub multilinear_evals: Vec<WitnessMultilinearEval>,
53+
}
54+
55+
impl ExecutionResult {
56+
/// Check if execution was successful
57+
///
58+
/// TODO: placeholder for now.
59+
pub fn is_success(&self) -> bool {
60+
true
61+
}
62+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Diagnostics, error handling, and debugging utilities
2+
#![allow(unused_imports)]
3+
4+
pub mod error;
5+
pub mod profiler;
6+
pub mod stack_trace;
7+
8+
pub use error::*;
9+
pub use profiler::*;
10+
pub use stack_trace::*;
File renamed without changes.
File renamed without changes.

crates/lean_vm/src/error.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Execution context and state management
2+
3+
use crate::core::LocationInSourceCode;
4+
use std::collections::BTreeMap;
5+
6+
/// Execution history for profiling and debugging
7+
#[derive(Debug, Clone, Default)]
8+
pub struct ExecutionHistory {
9+
pub lines: Vec<LocationInSourceCode>,
10+
pub cycles: Vec<usize>, // for each line, how many cycles it took
11+
}
12+
13+
impl ExecutionHistory {
14+
pub fn new() -> Self {
15+
Self::default()
16+
}
17+
18+
pub fn add_line(&mut self, location: LocationInSourceCode, cycles: usize) {
19+
self.lines.push(location);
20+
self.cycles.push(cycles);
21+
}
22+
23+
pub fn total_cycles(&self) -> usize {
24+
self.cycles.iter().sum()
25+
}
26+
27+
pub const fn len(&self) -> usize {
28+
self.lines.len()
29+
}
30+
31+
pub const fn is_empty(&self) -> bool {
32+
self.lines.is_empty()
33+
}
34+
}
35+
36+
/// VM execution context
37+
#[derive(Debug)]
38+
pub struct ExecutionContext<'a> {
39+
pub source_code: &'a str,
40+
pub function_locations: &'a BTreeMap<usize, String>,
41+
pub profiler_enabled: bool,
42+
pub std_out: String,
43+
pub instruction_history: ExecutionHistory,
44+
}
45+
46+
impl<'a> ExecutionContext<'a> {
47+
pub fn new(
48+
source_code: &'a str,
49+
function_locations: &'a BTreeMap<usize, String>,
50+
profiler_enabled: bool,
51+
) -> Self {
52+
Self {
53+
source_code,
54+
function_locations,
55+
profiler_enabled,
56+
std_out: String::new(),
57+
instruction_history: ExecutionHistory::new(),
58+
}
59+
}
60+
61+
pub fn print(&mut self, message: &str) {
62+
self.std_out.push_str(message);
63+
}
64+
65+
pub fn println(&mut self, message: &str) {
66+
self.std_out.push_str(message);
67+
self.std_out.push('\n');
68+
}
69+
}

0 commit comments

Comments
 (0)