nimbus-eth1/tests/test_op_arith.nim

468 lines
13 KiB
Nim
Raw Permalink Normal View History

2023-11-01 03:32:09 +00:00
# Nimbus
# Copyright (c) 2019-2024 Status Research & Development GmbH
2023-11-01 03:32:09 +00:00
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
import macro_assembler, unittest2
2019-09-21 05:45:23 +00:00
proc opArithMain*() =
suite "Arithmetic Opcodes":
assembler:
title: "Add_1"
2019-09-21 05:45:23 +00:00
code:
Push1 "0x02"
Push1 "0x02"
Add
2019-09-21 05:45:23 +00:00
stack: "0x04"
assembler:
title: "Add_2"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1002"
Push1 "0x02"
Add
2019-09-21 05:45:23 +00:00
stack: "0x1004"
assembler:
title: "Add_3"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1002"
Push6 "0x123456789009"
Add
2019-09-21 05:45:23 +00:00
stack: "0x12345678A00B"
assembler:
title: "Add_4"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1234"
Add
2019-09-21 05:45:23 +00:00
success: false
stack: "0x1234"
assembler:
title: "Addmod_1"
2019-09-21 05:45:23 +00:00
code:
Push1 "0x02"
Push1 "0x02"
Push1 "0x03"
Addmod
2019-09-21 05:45:23 +00:00
stack: "0x01"
assembler:
title: "Addmod_2"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1000"
Push1 "0x02"
Push2 "0x1002"
Addmod
Push1 "0x00"
2019-09-21 05:45:23 +00:00
stack:
"0x04"
"0x00"
assembler:
title: "Addmod_3"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1002"
Push6 "0x123456789009"
Push1 "0x02"
Addmod
2019-09-21 05:45:23 +00:00
stack: "0x093B"
assembler:
title: "Addmod_4"
2019-09-21 05:45:23 +00:00
code:
Push2 "0x1234"
Addmod
2019-09-21 05:45:23 +00:00
stack: "0x1234"
success: false
assembler:
title: "MUL_1"
code:
Push1 "0x03"
Push1 "0x02"
Mul
2019-09-21 05:45:23 +00:00
stack: "0x06"
assembler:
title: "MUL_2"
code:
Push3 "0x222222"
Push1 "0x03"
Mul
2019-09-21 05:45:23 +00:00
stack: "0x666666"
assembler:
title: "MUL_3"
code:
Push3 "0x222222"
Push3 "0x333333"
Mul
2019-09-21 05:45:23 +00:00
stack: "0x6D3A05F92C6"
assembler:
title: "MUL_4"
code:
Push1 "0x01"
Mul
2019-09-21 05:45:23 +00:00
stack: "0x01"
success: false
assembler: # Mulmod OP
2019-09-21 05:45:23 +00:00
title: "MULMOD_2"
code:
Push3 "0x222222"
Push1 "0x03"
Push1 "0x04"
Mulmod
2019-09-21 05:45:23 +00:00
stack: "0x000000000000000000000000000000000000000000000000000000000000000C"
assembler: # Mulmod OP
2019-09-21 05:45:23 +00:00
title: "MULMOD_3"
code:
Push3 "0x222222"
Push3 "0x333333"
Push3 "0x444444"
Mulmod
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler: # Mulmod OP mal
2019-09-21 05:45:23 +00:00
title: "MULMOD_4"
code:
Push1 "0x01"
Mulmod
2019-09-21 05:45:23 +00:00
success: false
stack: "0x01"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_1"
code:
Push1 "0x02"
Push1 "0x04"
Div
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_2"
code:
Push1 "0x33"
Push1 "0x99"
Div
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000003"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_3"
code:
Push1 "0x22"
Push1 "0x99"
Div
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000004"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_4"
code:
Push1 "0x15"
Push1 "0x99"
Div
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000007"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_5"
code:
Push1 "0x04"
Push1 "0x07"
Div
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Div OP
2019-09-21 05:45:23 +00:00
title: "DIV_6"
code:
Push1 "0x07"
Div
2019-09-21 05:45:23 +00:00
success: false
stack: "0x07"
assembler: # Sdiv OP
2019-09-21 05:45:23 +00:00
title: "SDIV_1"
code:
Push2 "0x03E8"
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC18"
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
assembler: # Sdiv OP
2019-09-21 05:45:23 +00:00
title: "SDIV_2"
code:
Push1 "0xFF"
Push1 "0xFF"
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Sdiv OP, Sdiv by zero should be zero
2019-09-21 05:45:23 +00:00
title: "SDIV_3"
code:
Push1 "0x00"
Push1 "0xFF"
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler: # Sdiv OP mal
2019-09-21 05:45:23 +00:00
title: "SDIV_4"
code:
Push1 "0xFF"
Sdiv
2019-09-21 05:45:23 +00:00
success: false
stack: "0xFF"
assembler: # -2^255 Sdiv -1 = -2^255 (special case in yellow paper)
2019-09-21 05:45:23 +00:00
title: "SDIV_5"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
assembler: # -2^255 Sdiv 1 = -2^255
2019-09-21 05:45:23 +00:00
title: "SDIV_6"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000001" # 1
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x8000000000000000000000000000000000000000000000000000000000000000" # -2^255 == low(int256)
assembler: # -2 Sdiv 2 = -1
2019-09-21 05:45:23 +00:00
title: "SDIV_7"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
assembler: # 4 Sdiv -2 = -2
2019-09-21 05:45:23 +00:00
title: "SDIV_8"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Push32 "0x0000000000000000000000000000000000000000000000000000000000000004" # 4
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
assembler: # -4 Sdiv 2 = -2
2019-09-21 05:45:23 +00:00
title: "SDIV_9"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # -4
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC" # 2
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
assembler: # -1 Sdiv 2 = 0
2019-09-21 05:45:23 +00:00
title: "SDIV_10"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" # -1
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x00" # 0
assembler: # low(int256) Sdiv low(int256) = 1
2019-09-21 05:45:23 +00:00
title: "SDIV_11"
code:
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x01" # 1
assembler: # low(int256) Sdiv 2
2019-09-21 05:45:23 +00:00
title: "SDIV_12"
code:
Push32 "0x0000000000000000000000000000000000000000000000000000000000000002" # 2
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xC000000000000000000000000000000000000000000000000000000000000000" # negative half low(int256)
assembler: # low(int256) Sdiv -2
2019-09-21 05:45:23 +00:00
title: "SDIV_13"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE" # -2
Push32 "0x8000000000000000000000000000000000000000000000000000000000000000" # low(int256)
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0x4000000000000000000000000000000000000000000000000000000000000000" # positive version of SDIV_12
assembler:
title: "SDIV_14"
code:
Push1 "0x01" # 1
Push1 "0x7F" # 127
Shl # 1 shl 127 (move the bit to the center or 128th position)
2019-09-21 05:45:23 +00:00
Push1 "0x01" # 1
Push1 "0xFF" # 255
Shl # 1 shl 255 (create low(int256))
2019-09-21 05:45:23 +00:00
Sdiv
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000000000000000000000" # half of the hi bits are set
fork: Constantinople
2019-09-21 05:45:23 +00:00
assembler: # Sub OP
2019-09-21 05:45:23 +00:00
title: "SUB_1"
code:
Push1 "0x04"
Push1 "0x06"
Sub
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Sub OP
2019-09-21 05:45:23 +00:00
title: "SUB_2"
code:
Push2 "0x4444"
Push2 "0x6666"
Sub
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000002222"
assembler: # Sub OP
2019-09-21 05:45:23 +00:00
title: "SUB_3"
code:
Push2 "0x4444"
Push4 "0x99996666"
Sub
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000099992222"
assembler: # Sub OP mal
2019-09-21 05:45:23 +00:00
title: "SUB_4"
code:
Push4 "0x99996666"
Sub
2019-09-21 05:45:23 +00:00
success: false
stack: "0x99996666"
assembler: # Exp OP
2019-09-21 05:45:23 +00:00
title: "EXP_1"
code:
Push1 "0x03"
Push1 "0x02"
Exp
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000008"
#assertEquals(4, gas);
assembler: # Exp OP
2019-09-21 05:45:23 +00:00
title: "EXP_2"
code:
Push1 "0x00"
Push3 "0x123456"
Exp
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
#assertEquals(3, gas);
assembler: # Exp OP
2019-09-21 05:45:23 +00:00
title: "EXP_3"
code:
Push2 "0x1122"
Push1 "0x01"
Exp
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
#assertEquals(5, gas);
assembler: # Exp OP mal
2019-09-21 05:45:23 +00:00
title: "EXP_4"
code:
Push3 "0x123456"
Exp
2019-09-21 05:45:23 +00:00
success: false
stack: "0x123456"
assembler: # Mod OP
2019-09-21 05:45:23 +00:00
title: "MOD_1"
code:
Push1 "0x03"
Push1 "0x04"
Mod
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Mod OP
2019-09-21 05:45:23 +00:00
title: "MOD_2"
code:
Push2 "0x012C"
Push2 "0x01F4"
Mod
2019-09-21 05:45:23 +00:00
stack: "0x00000000000000000000000000000000000000000000000000000000000000C8"
assembler: # Mod OP
2019-09-21 05:45:23 +00:00
title: "MOD_3"
code:
Push1 "0x04"
Push1 "0x02"
Mod
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000002"
assembler: # Mod OP mal
2019-09-21 05:45:23 +00:00
title: "MOD_4"
code:
Push1 "0x04"
Mod
2019-09-21 05:45:23 +00:00
success: false
stack: "0x04"
assembler: # Smod OP
2019-09-21 05:45:23 +00:00
title: "SMOD_1"
code:
Push1 "0x03"
Push1 "0x04"
Smod
2019-09-21 05:45:23 +00:00
stack: "0x0000000000000000000000000000000000000000000000000000000000000001"
assembler: # Smod OP
2019-09-21 05:45:23 +00:00
title: "SMOD_2"
code:
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE2" # -30
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF56" # -170
Smod
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC"
assembler: # Smod OP
2019-09-21 05:45:23 +00:00
title: "SMOD_3"
code:
Push32 "0x000000000000000000000000000000000000000000000000000000000000001E" # 30
Push32 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF56" # -170
Smod
2019-09-21 05:45:23 +00:00
stack: "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC"
assembler: # Smod OP mal
2019-09-21 05:45:23 +00:00
title: "SMOD_4"
code:
Push32 "0x000000000000000000000000000000000000000000000000000000000000001E" # 30
Smod
2019-09-21 05:45:23 +00:00
success: false
stack: "0x000000000000000000000000000000000000000000000000000000000000001E"
# real case, EVM bug, integer over flow
assembler: # SignExtend OP
2019-09-21 05:45:23 +00:00
title: "SIGNEXTEND_1"
code:
Push32 "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307"
Push1 "0x0F"
SignExtend
2019-09-21 05:45:23 +00:00
stack: "0x000000000000000000000000000000003f9b347132d29b62d161117bca8c7307"
assembler:
title: "Byte with overflow pos 1"
code:
Push32 "0x77676767676760000000000000001002e000000000000040000000e000000000"
Push32 "0x0000000000000000000000000000000000000000000000010000000000000000"
Byte
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
assembler:
title: "Byte with overflow pos 2"
code:
Push32 "0x001f000000000000000000000000000000200000000100000000000000000000"
Push32 "0x0000000000000000000000000000000080000000000000000000000000000001"
Byte
stack: "0x0000000000000000000000000000000000000000000000000000000000000000"
when isMainModule:
opArithMain()