Skip to content

Commit

Permalink
Implementing Shift instructions (#41)
Browse files Browse the repository at this point in the history
* Init shift opcodes

* Test shl

* Test shr

* Rename file name declaration

* Test shl using stack

* Test shr using stack

* Test shl and shr with eq flag

* Format zasm

* Fix rol and test it

* Fix ror and test it

* Test rol and ror with stack

* Test rol and ror with conditional flag

* Add eq flag testing

* Format zasm

* Use TaggedValue

* Remove unnecessary register setup

* Fix stack usage

* Use unreacheable

* Remove redundant cast

* Relocate shift match

* Remove 4 bytes redundancy

* Test shift for number greater than 256

* Add more test for values greater than 256
  • Loading branch information
jorbush authored Jun 27, 2024
1 parent 9a95074 commit a151eec
Show file tree
Hide file tree
Showing 24 changed files with 552 additions and 1 deletion.
13 changes: 13 additions & 0 deletions programs/rol.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "rol.zasm"
.globl __entry
__entry:
.func_begin0:
add 1, r0, r1
add 4, r0, r2
rol r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/rol_conditional_eq.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "rol_conditional_eq.zasm"
.globl __entry
__entry:
.func_begin0:
add 1, r0, r1
add 4, r0, r2
rol.eq r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/rol_greater_than_256.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "rol_greater_than_256.zasm"
.globl __entry
__entry:
.func_begin0:
; test sets r1 = 1, r2 = 258
rol r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
16 changes: 16 additions & 0 deletions programs/rol_sets_eq_flag.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.text
.file "rol_sets_eq_flag.zasm"
.globl __entry
__entry:

.func_begin0:
; EQ is set if the result is zero
add 0, r0, r1
add 0, r0, r2
rol! r1, r2, r1
sstore r0, r1
ret

.func_end0:
.note.GNU-stack
.rodata
17 changes: 17 additions & 0 deletions programs/rol_stack.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.text
.file "rol_stack.zasm"
.globl __entry
__entry:
.func_begin0:
; grow stack
add 1, r0, stack+=[2]
; set stack value
add 1, r0, stack[0]
add 4, r0, r2
; left rotation
rol stack[0], r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/ror.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "ror.zasm"
.globl __entry
__entry:
.func_begin0:
add 16, r0, r1
add 4, r0, r2
ror r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/ror_conditional_eq.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "ror_conditional_eq.zasm"
.globl __entry
__entry:
.func_begin0:
add 16, r0, r1
add 4, r0, r2
ror.eq r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/ror_greater_than_256.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "ror_greater_than_256.zasm"
.globl __entry
__entry:
.func_begin0:
; test sets r1 = 16, r2 = 258
ror r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
16 changes: 16 additions & 0 deletions programs/ror_sets_eq_flag.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.text
.file "ror_sets_eq_flag.zasm"
.globl __entry
__entry:

.func_begin0:
; EQ is set if the result is zero
add 0, r0, r1
add 0, r0, r2
ror! r1, r2, r1
sstore r0, r1
ret

.func_end0:
.note.GNU-stack
.rodata
17 changes: 17 additions & 0 deletions programs/ror_stack.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.text
.file "ror_stack.zasm"
.globl __entry
__entry:
.func_begin0:
; grow stack
add 1, r0, stack+=[2]
; set stack value
add 16, r0, stack[0]
add 4, r0, r2
; right rotation
ror stack[0], r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/shl.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "shl.zasm"
.globl __entry
__entry:
.func_begin0:
add 1, r0, r1
add 2, r0, r2
shl r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/shl_conditional_eq.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "shl_conditional_eq.zasm"
.globl __entry
__entry:
.func_begin0:
add 4, r0, r1
add 1, r0, r2
shl.eq r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/shl_greater_than_256.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "shl_greater_than_256.zasm"
.globl __entry
__entry:
.func_begin0:
; test sets r1 = 1, r2 = 258
shl r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
16 changes: 16 additions & 0 deletions programs/shl_sets_eq_flag.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.text
.file "shl_sets_eq_flag.zasm"
.globl __entry
__entry:

.func_begin0:
; EQ is set if the result is zero
add 0, r0, r1
add 0, r0, r2
shl! r1, r2, r1
sstore r0, r1
ret

.func_end0:
.note.GNU-stack
.rodata
17 changes: 17 additions & 0 deletions programs/shl_stack.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.text
.file "shl_stack.zasm"
.globl __entry
__entry:
.func_begin0:
; grow stack
add 1, r0, stack+=[2]
; set stack value
add 4, r0, stack[0]
add 2, r0, r2
; shift left
shl stack[0], r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/shr.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "shr.zasm"
.globl __entry
__entry:
.func_begin0:
add 8, r0, r1
add 2, r0, r2
shr r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 13 additions & 0 deletions programs/shr_conditional_eq.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.text
.file "shr_conditional_eq.zasm"
.globl __entry
__entry:
.func_begin0:
add 8, r0, r1
add 2, r0, r2
shr.eq r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
12 changes: 12 additions & 0 deletions programs/shr_greater_than_256.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.text
.file "shr_greater_than_256.zasm"
.globl __entry
__entry:
.func_begin0:
; test sets r1 = 16, r2 = 258
shr r1, r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
16 changes: 16 additions & 0 deletions programs/shr_sets_eq_flag.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.text
.file "shr_sets_eq_flag.zasm"
.globl __entry
__entry:

.func_begin0:
; EQ is set if the result is zero
add 0, r0, r1
add 0, r0, r2
shr! r1, r2, r1
sstore r0, r1
ret

.func_end0:
.note.GNU-stack
.rodata
17 changes: 17 additions & 0 deletions programs/shr_stack.zasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.text
.file "shr_stack.zasm"
.globl __entry
__entry:
.func_begin0:
; grow stack
add 1, r0, stack+=[2]
; set stack value
add 4, r0, stack[0]
add 2, r0, r2
; shift right
shr stack[0], r2, r3
sstore r0, r3
ret
.func_end0:
.note.GNU-stack
.rodata
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ use op_handlers::ptr_add::_ptr_add;
use op_handlers::ptr_pack::_ptr_pack;
use op_handlers::ptr_shrink::_ptr_shrink;
use op_handlers::ptr_sub::_ptr_sub;
use op_handlers::shift::_rol;
use op_handlers::shift::_ror;
use op_handlers::shift::_shl;
use op_handlers::shift::_shr;
use op_handlers::sub::_sub;
use op_handlers::xor::_xor;
pub use opcode::Opcode;
Expand All @@ -39,6 +43,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::ShiftOpcode;

pub fn program_from_file(bin_path: &str) -> Vec<U256> {
let program = std::fs::read(bin_path).unwrap();
Expand Down Expand Up @@ -112,7 +117,13 @@ pub fn run(mut vm: VMState) -> (U256, VMState) {
Variant::Mul(_) => _mul(&mut vm, &opcode),
Variant::Div(_) => _div(&mut vm, &opcode),
Variant::Context(_) => todo!(),
Variant::Shift(_) => todo!(),
Variant::Shift(_) => match opcode.variant {
Variant::Shift(ShiftOpcode::Shl) => _shl(&mut vm, &opcode),
Variant::Shift(ShiftOpcode::Shr) => _shr(&mut vm, &opcode),
Variant::Shift(ShiftOpcode::Rol) => _rol(&mut vm, &opcode),
Variant::Shift(ShiftOpcode::Ror) => _ror(&mut vm, &opcode),
_ => unreachable!(),
},
Variant::Binop(binop) => match binop {
BinopOpcode::Xor => _xor(&mut vm, &opcode),
BinopOpcode::And => _and(&mut vm, &opcode),
Expand Down
1 change: 1 addition & 0 deletions src/op_handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pub mod ptr_add;
pub mod ptr_pack;
pub mod ptr_shrink;
pub mod ptr_sub;
pub mod shift;
pub mod sub;
pub mod xor;
Loading

0 comments on commit a151eec

Please sign in to comment.