# Counters at zk-rom
### Objective
Detect posible out of counters at prover from the rom code before it happens
### Current approach
Currently we are checking if we have enough counters to run a function at the begginning of the function, always checking the worst case scenario. We are doing it at the lowest function level.
Example:
**opJUMPI**: check counters 100 steps + 2 binaries
- EQ -> -1 binary
- **checkJumpDest**
- **checkJumpDestDeployment**
- **checkJumpDestDeploymentCreate**
- MLOADX: check 100 steps + 2 binary + 1 mem align
- SHRarith: 50 steps + 2 binary + 1 arith
- EQ -> -1 binary
- ReadCode
As we can see, we can fall into the situation where we check at the beginning for the 2 binaries and we have 5 binaries left.
- 1 spent at EQ(4)
- 2 spent at MLOADX(2)
- 2 spent at SHRarith(0)
- EQ -> not detected OOC
### Posible solutions
#### 1- Check counters before every register
Check counters just before every EQ, LT, MEMALIGN, HASKDIGEST etc...
pros:
- We have total control of the counters
- No ooc posible if we control every situation
cons:
- Spend lots of steps
#### 2- Check counters at every function + special cases
Check counters before starting every function, this is how it is being done now but additionaly, add checks in special situations like the one expained above
pros:
- Less steps than option 1
cons:
- A bit more tricky to calculate the spent counters than option 1. Not easy to be aware of ALL the cases like explained in example
#### 3- Check counters at high level functions
Cehck counters at high level functions like opcode, dont check counters at utils function. Calculate the works case scenario in every opcode, also counting how much counters will be spent in the worst case scenario of the function calls inside the opcode logic.
pros:
- Less steps
cons:
- Is very difficult to calculate works case scenarios in some cases
- Difficult to recalculate the counters if the code changes/evolves
#### 4- Add some logic to executor
Add some instruction at executor where the counters are checked before executing the state machine and jump to a label when posible out of counters. Add some logic to executor.
Such as:
```
$ :LT, CHECK_BIN, JMPC(outOfCountersKeccak)
$ :SSTORE, CHECK_BIN, CHECK_POSEIDON
```
pros:
- Depends on the logic
cons:
- Maybe it will have a polynomial cost?
#### Extra
- Being able to set counters register in the ROM such as: `1 => CNT_KECCAK_F`
- Save and easy to manage the necessary counters to finalize the batch