Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uma opcodes #35

Merged
merged 40 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bc5f8e4
Begin fatptr implementation
gianbelinche Jun 14, 2024
6923b27
Change U256 to TaggedValue
gianbelinche Jun 14, 2024
1ab052d
Add ptr.add instruction
gianbelinche Jun 14, 2024
031410c
Add instruction ptr.sub
gianbelinche Jun 14, 2024
57bce76
Format code
gianbelinche Jun 14, 2024
51dd3ff
Add more tests
gianbelinche Jun 18, 2024
3fdf796
Add ptr shrink
gianbelinche Jun 18, 2024
ae120bd
Add ptr pack
gianbelinche Jun 18, 2024
1180e50
Format code
gianbelinche Jun 18, 2024
aea47f0
Add stack tests
gianbelinche Jun 18, 2024
1a42221
Init heap write
gianbelinche Jun 18, 2024
78d8d42
Finish heap write
gianbelinche Jun 18, 2024
c7c2490
Fix corrections
gianbelinche Jun 18, 2024
d270717
Format code
gianbelinche Jun 18, 2024
77fa4d8
Implement heap read
gianbelinche Jun 18, 2024
88332c2
Add tests for heap opcodes
gianbelinche Jun 19, 2024
d9e187a
Format code
gianbelinche Jun 19, 2024
e4e6b84
Add heap inc
gianbelinche Jun 19, 2024
ec4aa08
Add fat pointer read
gianbelinche Jun 19, 2024
b96d57e
Add tests for pointer inc
gianbelinche Jun 19, 2024
6625770
Add more fat pointer tests
gianbelinche Jun 19, 2024
eb5e82d
Merge branch 'main' into pointer-instructions
gianbelinche Jun 19, 2024
7fa6c96
Merge main
gianbelinche Jun 19, 2024
70ef530
Merge branch 'main' into pointer-instructions
gianbelinche Jun 24, 2024
080537f
Merge main
gianbelinche Jun 24, 2024
d4c81a4
Add aux_heap
gianbelinche Jun 24, 2024
abb5f33
Merge branch 'pointer-instructions' into uma-opcodes
gianbelinche Jun 24, 2024
5a10490
Format code
gianbelinche Jun 24, 2024
bcfd89f
Merge branch 'main' into uma-opcodes
gianbelinche Jun 25, 2024
9d3ac2a
Merge main
gianbelinche Jun 25, 2024
e32599d
Add gas usage
gianbelinche Jun 25, 2024
125cb9f
Format code
gianbelinche Jun 25, 2024
730bf02
Merge branch 'main' into uma-opcodes
gianbelinche Jun 25, 2024
a02ed13
Merge main
gianbelinche Jun 25, 2024
7b1e8b0
Merge branch 'main' into uma-opcodes
gianbelinche Jun 26, 2024
aced18e
Format code
gianbelinche Jun 26, 2024
029e15f
Merge branch 'main' into uma-opcodes
gianbelinche Jun 26, 2024
0658a4f
Fix conflicts
gianbelinche Jun 26, 2024
8c347b3
Merge branch 'main' into uma-opcodes
gianbelinche Jun 27, 2024
1f11d66
Format code
gianbelinche Jun 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions programs/aux_heap_gas.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
sstore r0,r0
ld.2 r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/aux_heap_store_gas.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
sstore r0,r0
st.2 r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
14 changes: 14 additions & 0 deletions programs/fat_pointer_read.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=FatPointer
st.1 r1, r2
ld r3, r4
sstore r0, r4
ret
.func_end0:

.note.GNU-stack
.rodata
14 changes: 14 additions & 0 deletions programs/fat_pointer_read_inc.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=FatPointer
st.1 r1, r2
ld.inc r3, r4, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
14 changes: 14 additions & 0 deletions programs/heap.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.1 r1, r2
ld.1 r1, r3
sstore r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
14 changes: 14 additions & 0 deletions programs/heap_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.2 r1, r2
ld.2 r1, r3
sstore r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/heap_gas.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
sstore r0,r0
ld.1 r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_load_inc.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.1 r1, r2
ld.1.inc r1, r3, r4
ld.1 r4, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_load_inc_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.2 r1, r2
ld.2.inc r1, r3, r4
ld.2 r4, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/heap_only_read.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address
ld.1 r1, r3
sstore r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/heap_only_read_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address
ld.2 r1, r3
sstore r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/heap_store_gas.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
sstore r0,r0
st.1 r0, r3
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_store_inc.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.1.inc r1, r2, r3
st.1 r3, r2
ld.1 r3, r4
sstore r0, r4
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_store_inc_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value
st.2.inc r1, r2, r3
st.2 r3, r2
ld.2 r3, r4
sstore r0, r4
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_two_addresses.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=address2 , r4=value2
st.1 r1, r2
st.1 r3, r4
ld.1 r3, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_two_addresses_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=address2 , r4=value2
st.2 r1, r2
st.2 r3, r4
ld.2 r3, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_two_addresses_first.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=address2 , r4=value2
st.1 r1, r2
st.1 r3, r4
ld.1 r1, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
15 changes: 15 additions & 0 deletions programs/heap_two_addresses_first_aux.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.text
.file "add.zasm"
.globl __entry
__entry:
.func_begin0:
; sets r1=address, r2=value, r3=address2 , r4=value2
st.2 r1, r2
st.2 r3, r4
ld.2 r1, r5
sstore r0, r5
ret
.func_end0:

.note.GNU-stack
.rodata
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ pub mod value;
use std::path::PathBuf;

use op_handlers::add::_add;
use op_handlers::aux_heap_read::_aux_heap_read;
use op_handlers::aux_heap_write::_aux_heap_write;
use op_handlers::div::_div;
use op_handlers::fat_pointer_read::_fat_pointer_read;
use op_handlers::heap_read::_heap_read;
use op_handlers::heap_write::_heap_write;
use op_handlers::log::{
_storage_read, _storage_write, _transient_storage_read, _transient_storage_write,
};
Expand All @@ -27,6 +32,7 @@ use zkevm_opcode_defs::ISAVersion;
use zkevm_opcode_defs::LogOpcode;
use zkevm_opcode_defs::Opcode as Variant;
use zkevm_opcode_defs::PtrOpcode;
use zkevm_opcode_defs::UMAOpcode;

/// Run a vm program with a clean VM state and with in memory storage.
pub fn run_program_in_memory(bin_path: &str) -> (U256, VMState) {
Expand Down Expand Up @@ -106,7 +112,15 @@ pub fn run_program_with_custom_state(bin_path: &str, mut vm: VMState) -> (U256,
// hooked up.
break;
}
Variant::UMA(_) => todo!(),
Variant::UMA(uma_variant) => match uma_variant {
UMAOpcode::HeapRead => _heap_read(&mut vm, &opcode),
UMAOpcode::HeapWrite => _heap_write(&mut vm, &opcode),
UMAOpcode::AuxHeapRead => _aux_heap_read(&mut vm, &opcode),
UMAOpcode::AuxHeapWrite => _aux_heap_write(&mut vm, &opcode),
UMAOpcode::FatPointerRead => _fat_pointer_read(&mut vm, &opcode),
UMAOpcode::StaticMemoryRead => todo!(),
jorbush marked this conversation as resolved.
Show resolved Hide resolved
UMAOpcode::StaticMemoryWrite => todo!(),
},
}
}
vm.current_frame.pc += 1;
Expand Down
32 changes: 32 additions & 0 deletions src/op_handlers/aux_heap_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use u256::U256;
use zkevm_opcode_defs::MAX_OFFSET_TO_DEREF_LOW_U32;

use crate::address_operands::address_operands_read;
use crate::value::TaggedValue;
use crate::{opcode::Opcode, state::VMState};

pub fn _aux_heap_read(vm: &mut VMState, opcode: &Opcode) {
let (src0, _) = address_operands_read(vm, opcode);
if src0.is_pointer {
panic!("Invalid operands for heap_read");
}

if src0.value > U256::from(MAX_OFFSET_TO_DEREF_LOW_U32) {
panic!("Address too large for heap_read");
}
let addr = src0.value.low_u32();

let gas_cost = vm.current_frame.aux_heap.expand_memory(addr + 32);
vm.gas_left -= gas_cost;

let value = vm.current_frame.aux_heap.read(addr);
vm.set_register(opcode.dst0_index, TaggedValue::new_raw_integer(value));

if opcode.alters_vm_flags {
// This flag is set if .inc is present
vm.set_register(
opcode.dst1_index,
TaggedValue::new_raw_integer(U256::from(addr + 32)),
);
}
}
31 changes: 31 additions & 0 deletions src/op_handlers/aux_heap_write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use u256::U256;
use zkevm_opcode_defs::MAX_OFFSET_TO_DEREF_LOW_U32;

use crate::address_operands::address_operands_read;
use crate::value::TaggedValue;
use crate::{opcode::Opcode, state::VMState};

pub fn _aux_heap_write(vm: &mut VMState, opcode: &Opcode) {
let (src0, src1) = address_operands_read(vm, opcode);
if src0.is_pointer {
panic!("Invalid operands for heap_write");
}

if src0.value > U256::from(MAX_OFFSET_TO_DEREF_LOW_U32) {
panic!("Address too large for heap_write");
}
let addr = src0.value.low_u32();

let gas_cost = vm.current_frame.aux_heap.expand_memory(addr + 32);
vm.gas_left -= gas_cost;

vm.current_frame.aux_heap.store(addr, src1.value);

if opcode.alters_vm_flags {
// This flag is set if .inc is present
vm.set_register(
opcode.dst0_index,
TaggedValue::new_raw_integer(U256::from(addr + 32)),
);
}
}
Loading