Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
gas.cpp
Go to the documentation of this file.
2
3#include <cstddef>
4
5#include "barretenberg/aztec/aztec_constants.hpp"
7
8namespace bb::avm2 {
9
16uint16_t compute_addressing_gas(uint16_t addressing_mode)
17{
18 // Check that the addressing gas cost fits in 16 bits.
19 static_assert(AVM_MAX_OPERANDS * (AVM_ADDRESSING_INDIRECT_L2_GAS + AVM_ADDRESSING_RELATIVE_L2_GAS) +
20 AVM_ADDRESSING_BASE_RESOLUTION_L2_GAS <=
21 UINT16_MAX,
22 "Addressing gas cost must fit in 16 bits");
23
24 uint16_t indirect_operand_count = 0;
25 uint16_t relative_operand_count = 0;
26
27 for (size_t i = 0; i < AVM_MAX_OPERANDS; i++) {
28 if (is_operand_indirect(addressing_mode, i)) {
29 indirect_operand_count++;
30 }
31 if (is_operand_relative(addressing_mode, i)) {
32 relative_operand_count++;
33 }
34 }
35
36 return static_cast<uint16_t>((relative_operand_count != 0 ? AVM_ADDRESSING_BASE_RESOLUTION_L2_GAS : 0) +
37 (indirect_operand_count * AVM_ADDRESSING_INDIRECT_L2_GAS) +
38 (relative_operand_count * AVM_ADDRESSING_RELATIVE_L2_GAS));
39}
40
41} // namespace bb::avm2
bool is_operand_relative(uint16_t indirect_flag, size_t operand_index)
Checks if the operand at the given index is relative.
bool is_operand_indirect(uint16_t indirect_flag, size_t operand_index)
Checks if the operand at the given index is indirect.
uint16_t compute_addressing_gas(uint16_t addressing_mode)
Computes the gas cost for addressing.
Definition gas.cpp:16