Skip to content

Commit d25acde

Browse files
committed
constants: add F as a constant
1 parent c9744ba commit d25acde

File tree

17 files changed

+185
-312
lines changed

17 files changed

+185
-312
lines changed

crates/leanVm/src/bytecode/hint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::operand::MemOrConstant;
44
///
55
/// They are not part of the verified computation trace.
66
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7-
pub enum Hint<F> {
7+
pub enum Hint {
88
/// A hint for the prover to allocate a new memory segment for a function's stack frame.
99
///
1010
/// This is the core mechanism for memory management in a VM without an `ap` (allocation pointer)
@@ -13,7 +13,7 @@ pub enum Hint<F> {
1313
/// The offset from `fp` where the pointer to the newly allocated segment will be stored.
1414
offset: usize,
1515
/// The requested size of the memory segment in scalar field elements.
16-
size: MemOrConstant<F>,
16+
size: MemOrConstant,
1717
/// If true, the start of the allocated memory is aligned to an 8-element boundary
1818
/// to facilitate vectorized memory access for extension field operations.
1919
/// The value stored at `m[fp + offset]` will be the aligned address divided by 8.
@@ -27,13 +27,13 @@ pub enum Hint<F> {
2727
/// The starting offset from `fp` where the resulting bits will be stored.
2828
res_offset: usize,
2929
/// The field element that needs to be decomposed into its bits.
30-
to_decompose: MemOrConstant<F>,
30+
to_decompose: MemOrConstant,
3131
},
3232
/// A hint used for debugging to print values from memory during execution.
3333
Print {
3434
/// A string containing line information (e.g., file and line number) for context.
3535
line_info: String,
3636
/// A list of memory locations or constants whose values should be printed.
37-
content: Vec<MemOrConstant<F>>,
37+
content: Vec<MemOrConstant>,
3838
},
3939
}

crates/leanVm/src/bytecode/instruction.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ use super::{
88
/// The ISA is minimal and includes basic arithmetic, memory operations, control flow,
99
/// and powerful precompiles for hashing and extension field arithmetic.
1010
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11-
pub enum Instruction<F> {
11+
pub enum Instruction {
1212
/// Performs a basic arithmetic computation: `res = arg_a op arg_b`.
1313
///
1414
/// This corresponds to the `ADD` and `MUL` opcodes in the design document.
1515
Computation {
1616
/// The arithmetic operation to perform (`Add` or `Mul`).
1717
operation: Operation,
1818
/// The first operand of the computation.
19-
arg_a: MemOrConstant<F>,
19+
arg_a: MemOrConstant,
2020
/// The second operand of the computation.
2121
arg_b: MemOrFp,
2222
/// The memory location or constant that the result must be equal to.
23-
res: MemOrConstant<F>,
23+
res: MemOrConstant,
2424
},
2525
/// Performs a memory dereference: `res = m[m[fp + shift_0] + shift_1]`.
2626
///
@@ -31,16 +31,16 @@ pub enum Instruction<F> {
3131
/// The offset added to the pointer from the first access to get the final address.
3232
shift_1: usize,
3333
/// The value that the result of the double dereference must be equal to.
34-
res: MemOrFpOrConstant<F>,
34+
res: MemOrFpOrConstant,
3535
},
3636
/// A conditional jump, called `JUZ` (Jump Unless Zero).
3737
///
3838
/// Changes the `pc` if `condition` is non-zero.
3939
JumpIfNotZero {
4040
/// The value to check. The jump is taken if this value is not zero.
41-
condition: MemOrConstant<F>,
41+
condition: MemOrConstant,
4242
/// The destination `pc` for the jump.
43-
dest: MemOrConstant<F>,
43+
dest: MemOrConstant,
4444
/// The new value for the frame pointer (`fp`) after the instruction.
4545
updated_fp: MemOrFp,
4646
},

crates/leanVm/src/bytecode/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ pub mod operation;
1010

1111
/// Represents the compiled bytecode of a program for the zkVM.
1212
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13-
pub struct Bytecode<F> {
13+
pub struct Bytecode {
1414
/// A vector of instructions that form the executable part of the program.
1515
///
1616
/// The Program Counter (pc) iterates over this vector.
17-
pub instructions: Vec<Instruction<F>>,
17+
pub instructions: Vec<Instruction>,
1818

1919
/// A map from a program counter (pc) value to a list of `Hint`s.
2020
///
2121
/// Hints are auxiliary, non-deterministic instructions executed only by the prover.
2222
///
2323
/// In this zkVM, they are crucial for managing memory allocations in the absence
2424
/// of an `ap` register.
25-
pub hints: BTreeMap<usize, Vec<Hint<F>>>,
25+
pub hints: BTreeMap<usize, Vec<Hint>>,
2626

2727
/// The memory offset from the frame pointer (fp) where the public input for the program begins.
2828
pub public_input_start: usize,

crates/leanVm/src/bytecode/operand.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::constant::F;
2+
13
/// Represents a value that can either be a constant or a value from memory.
24
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3-
pub enum MemOrConstant<F> {
5+
pub enum MemOrConstant {
46
/// A constant value (a field element).
57
Constant(F),
68
/// A memory location specified by a positive offset from the frame pointer (`fp`).
@@ -14,7 +16,7 @@ pub enum MemOrConstant<F> {
1416

1517
/// Represents a value that can be a memory location, the `fp` register itself, or a constant.
1618
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17-
pub enum MemOrFpOrConstant<F> {
19+
pub enum MemOrFpOrConstant {
1820
/// A memory location specified by a positive offset from `fp`. Represents `m[fp + shift]`.
1921
MemoryAfterFp {
2022
/// The offset from `fp` where the memory location is located.

crates/leanVm/src/bytecode/operation.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use p3_field::PrimeField64;
1+
use p3_field::Field;
2+
3+
use crate::constant::F;
24

35
/// The basic arithmetic operations supported by the VM's `Computation` instruction.
46
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -23,10 +25,8 @@ impl Operation {
2325
/// For example:
2426
/// - If `self` is `Add`, returns `a + b`.
2527
/// - If `self` is `Mul`, returns `a * b`.
26-
pub fn compute<F>(&self, a: F, b: F) -> F
27-
where
28-
F: PrimeField64,
29-
{
28+
#[must_use]
29+
pub fn compute(&self, a: F, b: F) -> F {
3030
match self {
3131
Self::Add => a + b,
3232
Self::Mul => a * b,
@@ -47,10 +47,8 @@ impl Operation {
4747
///
4848
/// - `Some(a)` if the inverse exists.
4949
/// - `None` if the inverse does not exist (e.g., `b == 0` for `Mul`).
50-
pub fn inverse_compute<F>(&self, a: F, b: F) -> Option<F>
51-
where
52-
F: PrimeField64,
53-
{
50+
#[must_use]
51+
pub fn inverse_compute(&self, a: F, b: F) -> Option<F> {
5452
match self {
5553
Self::Add => Some(a - b),
5654
Self::Mul => (!b.is_zero()).then(|| a / b),
@@ -60,13 +58,10 @@ impl Operation {
6058

6159
#[cfg(test)]
6260
mod tests {
63-
use p3_baby_bear::BabyBear;
6461
use p3_field::PrimeCharacteristicRing;
6562

6663
use super::*;
6764

68-
type F = BabyBear;
69-
7065
#[test]
7166
fn test_compute_add() {
7267
let op = Operation::Add;

crates/leanVm/src/constant.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use p3_koala_bear::KoalaBear;
2+
3+
pub(crate) const DIMENSION: usize = 8;
4+
pub(crate) type F = KoalaBear;

crates/leanVm/src/context/run_context.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use p3_field::PrimeField64;
2-
31
use crate::{
42
bytecode::operand::{MemOrConstant, MemOrFp, MemOrFpOrConstant},
53
errors::{memory::MemoryError, vm::VirtualMachineError},
@@ -36,14 +34,11 @@ impl RunContext {
3634
///
3735
/// - If the operand is a constant, it returns the constant.
3836
/// - If it's a memory location, it computes the address relative to `fp` and fetches the value from memory.
39-
pub fn value_from_mem_or_constant<F>(
37+
pub fn value_from_mem_or_constant(
4038
&self,
41-
operand: &MemOrConstant<F>,
39+
operand: &MemOrConstant,
4240
memory: &MemoryManager,
43-
) -> Result<MemoryValue<F>, MemoryError<F>>
44-
where
45-
F: PrimeField64,
46-
{
41+
) -> Result<MemoryValue, MemoryError> {
4742
match operand {
4843
MemOrConstant::Constant(val) => Ok(MemoryValue::Int(*val)),
4944
MemOrConstant::MemoryAfterFp { shift } => {
@@ -59,14 +54,11 @@ impl RunContext {
5954
///
6055
/// - If the operand is the frame pointer `Fp`, it returns the `fp` address itself.
6156
/// - If it's a memory location, it computes the address relative to `fp` and fetches the value.
62-
pub fn value_from_mem_or_fp<F>(
57+
pub fn value_from_mem_or_fp(
6358
&self,
6459
operand: &MemOrFp,
6560
memory: &MemoryManager,
66-
) -> Result<MemoryValue<F>, MemoryError<F>>
67-
where
68-
F: PrimeField64,
69-
{
61+
) -> Result<MemoryValue, MemoryError> {
7062
match operand {
7163
MemOrFp::Fp => Ok(MemoryValue::Address(self.fp)),
7264
MemOrFp::MemoryAfterFp { shift } => {
@@ -84,14 +76,11 @@ impl RunContext {
8476
/// - a constant value,
8577
/// - a memory location relative to `fp`,
8678
/// - the `fp` register itself.
87-
pub fn value_from_mem_or_fp_or_constant<F>(
79+
pub fn value_from_mem_or_fp_or_constant(
8880
&self,
89-
operand: &MemOrFpOrConstant<F>,
81+
operand: &MemOrFpOrConstant,
9082
memory: &MemoryManager,
91-
) -> Result<MemoryValue<F>, VirtualMachineError<F>>
92-
where
93-
F: PrimeField64,
94-
{
83+
) -> Result<MemoryValue, VirtualMachineError> {
9584
match operand {
9685
MemOrFpOrConstant::Constant(val) => Ok(MemoryValue::Int(*val)),
9786
MemOrFpOrConstant::Fp => Ok(MemoryValue::Address(self.fp)),
@@ -107,12 +96,10 @@ impl RunContext {
10796

10897
#[cfg(test)]
10998
mod tests {
110-
use p3_baby_bear::BabyBear;
11199
use p3_field::PrimeCharacteristicRing;
112100

113101
use super::*;
114-
115-
type F = BabyBear;
102+
use crate::constant::F;
116103

117104
#[test]
118105
fn test_get_value_constant() {
@@ -183,7 +170,7 @@ mod tests {
183170
// We won't insert anything, so all memory is uninitialized.
184171

185172
// Shift = 1 → fp + 1 points to offset 1 (which is uninitialized).
186-
let operand: MemOrConstant<F> = MemOrConstant::MemoryAfterFp { shift: 1 };
173+
let operand: MemOrConstant = MemOrConstant::MemoryAfterFp { shift: 1 };
187174

188175
// Set up context.
189176
let ctx = RunContext::new(
@@ -223,7 +210,7 @@ mod tests {
223210
fn test_get_value_from_mem_or_fp_or_constant_is_fp() {
224211
let fp_addr = MemoryAddress::new(1, 10);
225212
let ctx = RunContext::new(MemoryAddress::new(0, 0), fp_addr);
226-
let operand = MemOrFpOrConstant::<F>::Fp;
213+
let operand = MemOrFpOrConstant::Fp;
227214
let memory = MemoryManager::default();
228215
let result = ctx
229216
.value_from_mem_or_fp_or_constant(&operand, &memory)
@@ -235,8 +222,8 @@ mod tests {
235222
fn test_get_value_from_mem_or_fp_or_constant_is_mem_success() {
236223
let mut memory = MemoryManager::default();
237224
let fp = memory.add();
238-
let addr_to_read = fp.add_usize::<F>(7).unwrap();
239-
let expected_val = MemoryValue::<F>::Address(MemoryAddress::new(5, 5));
225+
let addr_to_read = fp.add_usize(7).unwrap();
226+
let expected_val = MemoryValue::Address(MemoryAddress::new(5, 5));
240227
memory.memory.insert(addr_to_read, expected_val).unwrap();
241228

242229
let ctx = RunContext::new(MemoryAddress::new(0, 0), fp);

0 commit comments

Comments
 (0)