Skip to content

Commit

Permalink
safe range max counters
Browse files Browse the repository at this point in the history
  • Loading branch information
krlosMata committed Jan 13, 2023
1 parent 38fe011 commit 7bb5384
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 162 deletions.
23 changes: 17 additions & 6 deletions main/constants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,28 @@ CONST %SSTORE_SET_GAS_REDUCED = 19900 ; sstore gas cost reduced base_dynamic_gas
CONST %SSTORE_RESET_GAS = 2900
CONST %SSTORE_RESET_GAS_REDUCED = 2800 ; sstore update gas cost reduced base_dynamic_gas(original_value == 0) - base_dynamic_gas(100)
CONST %SSTORE_CLEARS_SCHEDULE = 15000

; COUNTERS
CONST %MIN_STEPS_FINISH_BATCH = 200 ; min steps to finish tx
CONST %TOTAL_STEPS = 2**23
CONST %MAX_CNT_STEPS = %TOTAL_STEPS - %MIN_STEPS_FINISH_BATCH

CONST %MAX_CNT_ARITH = %TOTAL_STEPS / 32
CONST %MAX_CNT_BINARY = %TOTAL_STEPS / 16
CONST %MAX_CNT_MEM_ALIGN = %TOTAL_STEPS / 32
CONST %MAX_CNT_KECCAK_F = (%TOTAL_STEPS / 155286) * 44
CONST %MAX_CNT_PADDING_PG = (%TOTAL_STEPS / 56)
CONST %MAX_CNT_POSEIDON_G = (%TOTAL_STEPS / 30)
CONST %MAX_CNT_ARITH_LIMIT = %TOTAL_STEPS / 32
CONST %MAX_CNT_BINARY_LIMIT = %TOTAL_STEPS / 16
CONST %MAX_CNT_MEM_ALIGN_LIMIT = %TOTAL_STEPS / 32
CONST %MAX_CNT_KECCAK_F_LIMIT = (%TOTAL_STEPS / 155286) * 44
CONST %MAX_CNT_PADDING_PG_LIMIT = (%TOTAL_STEPS / 56)
CONST %MAX_CNT_POSEIDON_G_LIMIT = (%TOTAL_STEPS / 30)

CONST %SAFE_RANGE = 20 ; safe guard counters to not take into account (%RANGE = 1 / SAFE_RANGE)

CONST %MAX_CNT_ARITH = %MAX_CNT_ARITH_LIMIT - (%MAX_CNT_ARITH_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_BINARY = %MAX_CNT_BINARY_LIMIT - (%MAX_CNT_BINARY_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_MEM_ALIGN = %MAX_CNT_MEM_ALIGN_LIMIT - (%MAX_CNT_MEM_ALIGN_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_KECCAK_F = %MAX_CNT_KECCAK_F_LIMIT - (%MAX_CNT_KECCAK_F_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_PADDING_PG = %MAX_CNT_PADDING_PG_LIMIT - (%MAX_CNT_PADDING_PG_LIMIT / %SAFE_RANGE)
CONST %MAX_CNT_POSEIDON_G = %MAX_CNT_POSEIDON_G_LIMIT - (%MAX_CNT_POSEIDON_G_LIMIT / %SAFE_RANGE)

CONST %MIN_CNT_KECCAK_BATCH = 1 ; minimum necessary keccaks to compute global hash

; ETHEREUM CONSTANTS
Expand Down
40 changes: 15 additions & 25 deletions main/opcodes/arithmetic.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* @link [Link EVM behaviour --> evm.codes?]
* @zk-counters
* - 20 steps
* - 1 binary
* - 100 steps
* @process-opcode
* - stack input: [a, b]
* - stack output: [a + b]
Expand All @@ -30,8 +30,7 @@ opADD:
/**
* @link [https://www.evm.codes/#02?fork=berlin]
* @zk-counters
* - 1 arith
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: [a, b]
* - stack output: [a * b]
Expand All @@ -58,8 +57,8 @@ opMUL:
/**
* @link [https://www.evm.codes/#03?fork=berlin]
* @zk-counters
* - 1 arith
* - 100 steps
* - 20 steps
* - 1 binary
* @process-opcode
* - stack input: [a, b]
* - stack output: [a - b]
Expand All @@ -81,9 +80,7 @@ opSUB:
/**
* @link [https://www.evm.codes/#04?fork=berlin]
* @zk-counters
* - 1 arith
* - 2 binary
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: [a, b]
* - stack output: [a / b]
Expand All @@ -109,9 +106,8 @@ opDIV:
/**
* @link [https://www.evm.codes/#05?fork=berlin]
* @zk-counters
* - 1 arith
* - 2 binary
* - 100 steps
* - 50 steps
* - 1 binary
* @process-opcode
* - stack input: [a, b]
* - stack output: [a / b]
Expand Down Expand Up @@ -148,9 +144,7 @@ SDIVNeg:
/**
* @link [https://www.evm.codes/#06?fork=berlin]
* @zk-counters
* - 1 arith
* - 2 binary
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: [a, b]
* - stack output: [a % b]
Expand All @@ -174,9 +168,8 @@ opMOD:
/**
* @link [https://www.evm.codes/#07?fork=berlin]
* @zk-counters
* - 1 arith
* - 8 binary
* - 100 steps
* - 20 steps
* - 1 binary
* @process-opcode
* - stack input: [a, b]
* - stack output: [a % b]
Expand Down Expand Up @@ -210,9 +203,9 @@ opSMODNeg:
/**
* @link [https://www.evm.codes/#08?fork=berlin]
* @zk-counters
* - 1 arith
* - 4 binary
* - 80 steps
* - 3 binary
* - 1 arith
* @process-opcode
* - stack input: [a, b, N]
* - stack output: [(a + b) % N]
Expand Down Expand Up @@ -262,9 +255,7 @@ zeroOneAddMod:
/**
* @link [https://www.evm.codes/#09?fork=berlin]
* @zk-counters
* - 3 arith
* - 3 binary
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: [a, b, N]
* - stack output: [(a * b) % N]
Expand All @@ -287,8 +278,7 @@ opMULMOD:
/**
* @link [https://www.evm.codes/#0A?fork=berlin]
* @zk-counters
* - 5 binary
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: [a, exp]
* - stack output: [a ** exp]
Expand Down Expand Up @@ -319,8 +309,8 @@ opEXP:
/**
* @link [https://www.evm.codes/#0B?fork=berlin]
* @zk-counters
* - 6 arith
* - 100 steps
* - 6 binary
* @process-opcode
* - stack input: [b,x]
* - stack output: [y]
Expand Down
16 changes: 8 additions & 8 deletions main/opcodes/block.zkasm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @link [https://www.evm.codes/#40?fork=berlin]
* @zk-counters
* - 1 keccak
* - 255 poseidon
* - 1 keccak
* - 100 steps
* @process-opcode
* - stack input: [blockNumber]
Expand Down Expand Up @@ -54,7 +54,7 @@ opBLOCKHASHzero:
/**
* @link [https://www.evm.codes/#41?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [address]
Expand All @@ -74,7 +74,7 @@ opCOINBASE:
/**
* @link [https://www.evm.codes/#42?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [timestamp]
Expand All @@ -93,7 +93,7 @@ opTIMESTAMP:
/**
* @link [https://www.evm.codes/#43?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [blockNumber]
Expand All @@ -113,7 +113,7 @@ opNUMBER:
/**
* @link [https://www.evm.codes/#44?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [difficulty]
Expand All @@ -133,7 +133,7 @@ opDIFFICULTY:
/**
* @link [https://www.evm.codes/#45?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [gasLimit]
Expand All @@ -153,7 +153,7 @@ opGASLIMIT:
/**
* @link [https://www.evm.codes/#46?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [chainId]
Expand All @@ -172,7 +172,7 @@ opCHAINID:
/**
* @link [https://www.evm.codes/#48?fork=berlin]
* @zk-counters
* - 100 steps
* - 10 steps
* @process-opcode
* - stack input: []
* - stack output: [baseFee]
Expand Down
31 changes: 11 additions & 20 deletions main/opcodes/calldata-returndata-code.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
/**
* @link [https://www.evm.codes/#35?fork=berlin]
* @zk-counters
* - 64 arith
* - 66 binary
* - 1 binary
* - 100 steps
* @process-opcode
* - stack input: [offset]
Expand Down Expand Up @@ -82,10 +81,8 @@ opCALLDATASIZEdep:
/**
* @link [https://www.evm.codes/#37?fork=berlin]
* @zk-counters
* - 192 arith
* - 192 binary
* - 1 mem align
* - 100 steps
* - 2 binary
* @process-opcode
* - stack input: [destOffset, offset, size]
* - stack output: []
Expand Down Expand Up @@ -224,8 +221,8 @@ opCALLDATACOPYend:
/**
* @link [https://www.evm.codes/#38?fork=berlin]
* @zk-counters
* - 252 poseidon
* - 100 steps
* - 255 poseidon
* - 30 steps
* @process-opcode
* - stack input: []
* - stack output: [size]
Expand Down Expand Up @@ -263,7 +260,7 @@ VAR GLOBAL memInteger
/**
* @link [https://www.evm.codes/#39?fork=berlin]
* @zk-counters
* - dynamic binary: 100 * size
* - dynamic binary: 2 * size
* - dynamic mem align: 2 * size
* - dynamic poseidon: 510 * size
* - dynamic steps: 100 * size
Expand Down Expand Up @@ -366,8 +363,8 @@ readValueBytecode:
/**
* @link [https://www.evm.codes/#3B?fork=berlin]
* @zk-counters
* - 255 poseidon
* - 100 steps
* - 255 poseidon
* @process-opcode
* - stack input: [address]
* - stack output: [size]
Expand Down Expand Up @@ -399,11 +396,8 @@ VAR GLOBAL tmpContractLength
/**
* @link [https://www.evm.codes/#51?fork=berlin]
* @zk-counters
* - 11 padding
* - dynamic binary: 100 * size
* - dynamic mem align: 2 * size
* - dynamic poseidon: 510 * size
* - dynamic steps: 100 * size
* - dynamic: 100
* - dynamic poseidon: 255 + f(size)
* @process-opcode
* - stack input: [address, destOffset, offset, size]
* - stack output: []
Expand Down Expand Up @@ -507,7 +501,7 @@ opEXTCODECOPYCheckHashEnd:
* @link [https://www.evm.codes/#3D?fork=berlin]
* @zk-counters
* - 1 binary
* - 100 steps
* - 20 steps
* @process-opcode
* - stack input: []
* - stack output: [size]
Expand Down Expand Up @@ -537,10 +531,7 @@ endOpRETURNDATASIZE:
/**
* @link [https://www.evm.codes/#3E?fork=berlin]
* @zk-counters
* - 32 arith
* - 32 binary
* - 2 mem align
* - 510 poseidon
* - 2 binary
* - dynamic steps: 400 + 100 * size
* @process-opcode
* - stack input: [destOffset, offset, size]
Expand Down Expand Up @@ -631,8 +622,8 @@ opRETURNDATACOPYEmpty:
/**
* @link [https://www.evm.codes/#3F?fork=berlin]
* @zk-counters
* - 255 poseidon
* - 100 steps
* - 255 poseidon
* @process-opcode
* - stack input: [address]
* - stack output: [hash]
Expand Down
Loading

0 comments on commit 7bb5384

Please sign in to comment.