Skip to content

Commit e325e39

Browse files
committed
some explainations about memory layout
1 parent f7e8110 commit e325e39

File tree

7 files changed

+42
-22
lines changed

7 files changed

+42
-22
lines changed

crates/lean_compiler/src/c_compile_final.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{F, PUBLIC_INPUT_START, ZERO_VEC_PTR, ir::*, lang::*};
1+
use crate::{F, NONRESERVED_PROGRAM_INPUT_START, ZERO_VEC_PTR, ir::*, lang::*};
22
use lean_vm::*;
33
use p3_field::{PrimeCharacteristicRing, PrimeField32};
44
use std::collections::BTreeMap;
@@ -395,7 +395,7 @@ fn count_real_instructions(instrs: &[IntermediateInstruction]) -> usize {
395395
fn eval_constant_value(constant: &ConstantValue, compiler: &Compiler) -> usize {
396396
match constant {
397397
ConstantValue::Scalar(scalar) => *scalar,
398-
ConstantValue::PublicInputStart => PUBLIC_INPUT_START,
398+
ConstantValue::PublicInputStart => NONRESERVED_PROGRAM_INPUT_START,
399399
ConstantValue::PointerToZeroVector => ZERO_VEC_PTR,
400400
ConstantValue::PointerToOneVector => ONE_VEC_PTR,
401401
ConstantValue::FunctionSize { function_name } => {

crates/lean_prover/tests/test_zkvm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@ fn test_zk_vm_helper(program_str: &str) {
8383
const SECOND_POINT: usize = 2;
8484
const SECOND_N_VARS: usize = 7;
8585

86-
let mut public_input = (0..(1 << 13) - PUBLIC_INPUT_START)
86+
let mut public_input = (0..(1 << 13) - NONRESERVED_PROGRAM_INPUT_START)
8787
.map(F::from_usize)
8888
.collect::<Vec<_>>();
8989

9090
public_input[SECOND_POINT * (SECOND_N_VARS * DIMENSION).next_power_of_two()
9191
+ SECOND_N_VARS * DIMENSION
92-
- PUBLIC_INPUT_START
92+
- NONRESERVED_PROGRAM_INPUT_START
9393
..(SECOND_POINT + 1) * (SECOND_N_VARS * DIMENSION).next_power_of_two()
94-
- PUBLIC_INPUT_START]
94+
- NONRESERVED_PROGRAM_INPUT_START]
9595
.iter_mut()
9696
.for_each(|x| *x = F::ZERO);
9797

crates/lean_vm/src/core/constants.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ pub const LOG_VECTOR_LEN: usize = 3;
77
/// Vector length (2^LOG_VECTOR_LEN)
88
pub const VECTOR_LEN: usize = 1 << LOG_VECTOR_LEN;
99

10+
/// Maximum memory size for VM runner
11+
pub const MAX_RUNNER_MEMORY_SIZE: usize = 1 << 24;
12+
13+
/// Memory layout:
14+
///
15+
/// [memory] = [public_data] [private_data]
16+
///
17+
/// public_data: shared between prover and verifier
18+
/// private_data: witness, committed by the prover
19+
///
20+
/// [public_data] = [reserved_area] [program_input]
21+
///
22+
/// reserved_area: reserved for special constants (size = 48 field elements)
23+
/// program_input: the input of the program we want to prove
24+
///
25+
/// [reserved_area] = [00000000] [00000000] [10000000] [poseidon_16(0) (16 field elements)] [poseidon_24(0) (8 last field elements)]
26+
1027
/// Convention: vectorized pointer of size 2, pointing to 16 zeros
1128
pub const ZERO_VEC_PTR: usize = 0;
1229

@@ -19,8 +36,5 @@ pub const POSEIDON_16_NULL_HASH_PTR: usize = 3;
1936
/// Convention: vectorized pointer of size 1, = the last 8 elements of poseidon_24(0)
2037
pub const POSEIDON_24_NULL_HASH_PTR: usize = 5;
2138

22-
/// Normal pointer to start of public input
23-
pub const PUBLIC_INPUT_START: usize = 6 * 8;
24-
25-
/// Maximum memory size for VM runner
26-
pub const MAX_RUNNER_MEMORY_SIZE: usize = 1 << 24;
39+
/// Normal pointer to start of program input
40+
pub const NONRESERVED_PROGRAM_INPUT_START: usize = 6 * 8;

crates/lean_vm/src/execution/runner.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! VM execution runner
22
33
use crate::core::{
4-
DIMENSION, F, ONE_VEC_PTR, POSEIDON_16_NULL_HASH_PTR, POSEIDON_24_NULL_HASH_PTR,
5-
PUBLIC_INPUT_START, VECTOR_LEN, ZERO_VEC_PTR,
4+
DIMENSION, F, NONRESERVED_PROGRAM_INPUT_START, ONE_VEC_PTR, POSEIDON_16_NULL_HASH_PTR,
5+
POSEIDON_24_NULL_HASH_PTR, VECTOR_LEN, ZERO_VEC_PTR,
66
};
77
use crate::diagnostics::{ExecutionResult, RunnerError};
88
use crate::execution::{ExecutionHistory, Memory};
@@ -23,9 +23,11 @@ const STACK_TRACE_INSTRUCTIONS: usize = 5000;
2323
/// Build public memory with standard initialization
2424
pub fn build_public_memory(public_input: &[F]) -> Vec<F> {
2525
// padded to a power of two
26-
let public_memory_len = (PUBLIC_INPUT_START + public_input.len()).next_power_of_two();
26+
let public_memory_len =
27+
(NONRESERVED_PROGRAM_INPUT_START + public_input.len()).next_power_of_two();
2728
let mut public_memory = F::zero_vec(public_memory_len);
28-
public_memory[PUBLIC_INPUT_START..][..public_input.len()].copy_from_slice(public_input);
29+
public_memory[NONRESERVED_PROGRAM_INPUT_START..][..public_input.len()]
30+
.copy_from_slice(public_input);
2931

3032
// "zero" vector
3133
let zero_start = ZERO_VEC_PTR * VECTOR_LEN;
@@ -157,7 +159,8 @@ fn execute_bytecode_helper(
157159
// set public memory
158160
let mut memory = Memory::new(build_public_memory(public_input));
159161

160-
let public_memory_size = (PUBLIC_INPUT_START + public_input.len()).next_power_of_two();
162+
let public_memory_size =
163+
(NONRESERVED_PROGRAM_INPUT_START + public_input.len()).next_power_of_two();
161164
let mut fp = public_memory_size;
162165

163166
for (i, value) in private_input.iter().enumerate() {
@@ -320,7 +323,8 @@ fn execute_bytecode_helper(
320323
summary.push_str(&format!("MEMORY: {}\n", pretty_integer(memory.0.len())));
321324
summary.push('\n');
322325

323-
let runtime_memory_size = memory.0.len() - (PUBLIC_INPUT_START + public_input.len());
326+
let runtime_memory_size =
327+
memory.0.len() - (NONRESERVED_PROGRAM_INPUT_START + public_input.len());
324328
summary.push_str(&format!(
325329
"Bytecode size: {}\n",
326330
pretty_integer(bytecode.instructions.len())
@@ -342,7 +346,7 @@ fn execute_bytecode_helper(
342346
let used_memory_cells = memory
343347
.0
344348
.iter()
345-
.skip(PUBLIC_INPUT_START + public_input.len())
349+
.skip(NONRESERVED_PROGRAM_INPUT_START + public_input.len())
346350
.filter(|&&x| x.is_some())
347351
.count();
348352
summary.push_str(&format!(

crates/lean_vm/tests/test_lean_vm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const MLE_N_VARS: usize = 1;
2727

2828
// Ensure public input covers the highest index used (dot product arg1 slice).
2929
const MAX_MEMORY_INDEX: usize = DOT_ARG1_PTR + DOT_PRODUCT_LEN * DIMENSION - 1;
30-
const PUBLIC_INPUT_LEN: usize = MAX_MEMORY_INDEX - PUBLIC_INPUT_START + 1;
30+
const PUBLIC_INPUT_LEN: usize = MAX_MEMORY_INDEX - NONRESERVED_PROGRAM_INPUT_START + 1;
3131

3232
const POSEIDON16_ARG_A_VALUES: [u64; VECTOR_LEN] = [1, 2, 3, 4, 5, 6, 7, 8];
3333
const POSEIDON16_ARG_B_VALUES: [u64; VECTOR_LEN] = [101, 102, 103, 104, 105, 106, 107, 108];
@@ -47,8 +47,8 @@ fn f(value: u64) -> F {
4747
}
4848

4949
fn set_public_input_cell(public_input: &mut [F], memory_index: usize, value: F) {
50-
assert!(memory_index >= PUBLIC_INPUT_START);
51-
let idx = memory_index - PUBLIC_INPUT_START;
50+
assert!(memory_index >= NONRESERVED_PROGRAM_INPUT_START);
51+
let idx = memory_index - NONRESERVED_PROGRAM_INPUT_START;
5252
assert!(idx < public_input.len());
5353
public_input[idx] = value;
5454
}

crates/rec_aggregation/src/recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn run_whir_recursion_benchmark() {
127127

128128
// to align the first merkle leaves (in base field) (required to appropriately call the precompile multilinear_eval)
129129
let mut proof_data_padding = (1 << first_folding_factor)
130-
- ((PUBLIC_INPUT_START
130+
- ((NONRESERVED_PROGRAM_INPUT_START
131131
+ public_input.len()
132132
+ {
133133
// sumcheck polys

crates/rec_aggregation/src/xmss_aggregate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,9 @@ pub fn run_xmss_benchmark(n_xmss: usize) {
224224
}
225225
public_input.insert(
226226
0,
227-
F::from_usize((public_input.len() + 8 + PUBLIC_INPUT_START).next_power_of_two()),
227+
F::from_usize(
228+
(public_input.len() + 8 + NONRESERVED_PROGRAM_INPUT_START).next_power_of_two(),
229+
),
228230
);
229231
public_input.splice(1..1, F::zero_vec(7));
230232

0 commit comments

Comments
 (0)