|
Barretenberg
The ZK-SNARK library at the core of Aztec
|
Virtual machine for field arithmetic operations. More...
#include <field.fuzzer.hpp>
Classes | |
| struct | ParsedInstruction |
| Structure to hold parsed instruction data. More... | |
Public Member Functions | |
| FieldVM (bool with_debug=false, size_t max_steps=SIZE_MAX) | |
| Constructor for FieldVM. | |
| size_t | execute_instruction (const unsigned char *data_ptr) |
| Execute a single VM instruction. | |
| std::pair< std::vector< ParsedInstruction >, size_t > | parse_instructions (const unsigned char *Data, size_t Size, size_t max_steps) |
| Parse instructions from data buffer into a vector. | |
| bool | execute_parsed_instruction (const ParsedInstruction &parsed) |
| Execute a parsed instruction. | |
| size_t | run (const unsigned char *Data, size_t Size, bool reset_steps=true) |
| Run the VM on input data. | |
| bool | check_internal_state () const |
| Check internal state consistency between field and uint256_t representations. | |
| std::vector< numeric::uint256_t > | export_uint_state () const |
| Export the final uint state as a vector of uint256_t values. | |
| size_t | get_step_count () const |
| Get the number of steps executed. | |
| bool | was_stopped_by_max_steps () const |
| Check if the VM was stopped due to reaching max steps. | |
| void | set_max_steps (size_t new_max_steps) |
| Set a new step limit for the VM. | |
| void | reset_step_count () |
| Reset the step counter to 0. | |
| size_t | get_max_steps () const |
| Get the current step limit. | |
| bool | has_remaining_steps () const |
| Check if there are remaining steps available. | |
| bool | verify_initial_state (const std::vector< numeric::uint256_t > &state) const |
| Verify that the initial state is correctly loaded. | |
Static Public Member Functions | |
| static numeric::uint256_t | reduce_to_modulus (const numeric::uint256_t &value) |
| Reduce a uint256_t value to the field's modulus. | |
Public Attributes | |
| std::array< Field, INTERNAL_STATE_SIZE > | field_internal_state |
| Internal state array of field elements. | |
| std::array< numeric::uint256_t, INTERNAL_STATE_SIZE > | uint_internal_state |
| Internal state array of uint256_t values. | |
| bool | with_debug |
| Flag to enable debug output. | |
| VMSettings | settings |
| VM settings controlling which operations are enabled. | |
| size_t | max_steps |
| Maximum number of steps the VM can execute. | |
| size_t | step_count {} |
| Number of steps executed so far. | |
Static Public Attributes | |
| static constexpr bool | LARGE_MODULUS = (Field::modulus.data[3] >= MODULUS_TOP_LIMB_LARGE_THRESHOLD) |
| Flag indicating if the field has a large modulus requiring uint512_t for arithmetic. | |
| static constexpr bool | SUPPORTS_SQRT |
| Flag indicating if the field supports square root operations. | |
Virtual machine for field arithmetic operations.
| Field | The field type to operate on |
This template class implements a virtual machine that can execute field arithmetic operations. It maintains both field elements and uint256_t representations for verification purposes. The VM supports various field operations and includes comprehensive error checking and debugging capabilities.
Definition at line 147 of file field.fuzzer.hpp.
|
inline |
Constructor for FieldVM.
| with_debug | Whether to enable debug output |
| max_steps | Maximum number of steps to execute |
Initializes the VM with default settings and zero-initialized state
Definition at line 212 of file field.fuzzer.hpp.
|
inline |
Check internal state consistency between field and uint256_t representations.
This method verifies that the field_internal_state and uint_internal_state arrays are consistent with each other. Any discrepancy indicates a potential bug in the field arithmetic implementation.
Definition at line 1241 of file field.fuzzer.hpp.
|
inline |
Execute a single VM instruction.
| data_ptr | Pointer to the instruction data |
This method parses and executes a single VM instruction. It handles all supported field operations and maintains consistency between field and uint256_t representations. The method returns the number of bytes consumed.
Helper function to get an index from data with wraparound
| data_ptr_index | Pointer to the data containing the index |
| offset | Offset into the data |
< Total number of instructions
Helper function to get a uint256_t value from data
| data_ptr_value | Pointer to the data containing the value |
| offset | Offset into the data |
Helper function to get a uint64_t value from data
| data_ptr_value | Pointer to the data containing the value |
| offset | Offset into the data |
Definition at line 259 of file field.fuzzer.hpp.
|
inline |
Execute a parsed instruction.
| parsed | The parsed instruction to execute |
This method executes a previously parsed instruction. It provides debug output if enabled and returns whether execution should continue.
Definition at line 1160 of file field.fuzzer.hpp.
|
inline |
Export the final uint state as a vector of uint256_t values.
This method creates a copy of the uint_internal_state array for external use, typically for state transfer between VM phases.
Definition at line 1263 of file field.fuzzer.hpp.
|
inline |
Get the current step limit.
Definition at line 1304 of file field.fuzzer.hpp.
|
inline |
Get the number of steps executed.
Definition at line 1278 of file field.fuzzer.hpp.
|
inline |
Check if there are remaining steps available.
Definition at line 1311 of file field.fuzzer.hpp.
|
inline |
Parse instructions from data buffer into a vector.
| Data | Pointer to the data buffer |
| Size | Size of the data buffer |
| max_steps | Maximum number of instructions to parse |
This method parses the input data into a sequence of instructions. It handles settings parsing and instruction size calculation. The method returns both the parsed instructions and the number of bytes consumed during parsing.
< Total number of instructions
Definition at line 1017 of file field.fuzzer.hpp.
|
inlinestatic |
Reduce a uint256_t value to the field's modulus.
| value | The value to reduce |
This method handles modulus reduction for both large and small modulus fields. For large modulus fields, it uses uint512_t to prevent overflow.
Definition at line 1322 of file field.fuzzer.hpp.
|
inline |
Reset the step counter to 0.
Definition at line 1297 of file field.fuzzer.hpp.
|
inline |
Run the VM on input data.
| Data | The data to run the VM on |
| Size | The size of the data |
| reset_steps | Whether to reset the step counter (default: true) |
This method is the main entry point for VM execution. It parses all instructions from the input data and executes them sequentially. The method handles step counting, debug output, and returns the number of bytes consumed.
Definition at line 1198 of file field.fuzzer.hpp.
|
inline |
Set a new step limit for the VM.
| new_max_steps | The new maximum number of steps |
Definition at line 1292 of file field.fuzzer.hpp.
|
inline |
Verify that the initial state is correctly loaded.
| state | The state vector to verify against |
This method verifies that the imported state matches the current internal state after proper modulus reduction and field conversion.
Definition at line 1340 of file field.fuzzer.hpp.
|
inline |
Check if the VM was stopped due to reaching max steps.
Definition at line 1285 of file field.fuzzer.hpp.
| std::array<Field, INTERNAL_STATE_SIZE> bb::FieldVM< Field >::field_internal_state |
Internal state array of field elements.
Used for actual field arithmetic operations
Definition at line 175 of file field.fuzzer.hpp.
|
staticconstexpr |
Flag indicating if the field has a large modulus requiring uint512_t for arithmetic.
Fields with moduli >= 2^254 require uint512_t for safe addition/subtraction
Definition at line 153 of file field.fuzzer.hpp.
| size_t bb::FieldVM< Field >::max_steps |
Maximum number of steps the VM can execute.
Definition at line 197 of file field.fuzzer.hpp.
| VMSettings bb::FieldVM< Field >::settings |
VM settings controlling which operations are enabled.
Definition at line 192 of file field.fuzzer.hpp.
| size_t bb::FieldVM< Field >::step_count {} |
Number of steps executed so far.
Definition at line 202 of file field.fuzzer.hpp.
|
staticconstexpr |
Flag indicating if the field supports square root operations.
Fields with very small 2-adicity (like secp256r1) have issues with Tonelli-Shanks
Definition at line 160 of file field.fuzzer.hpp.
| std::array<numeric::uint256_t, INTERNAL_STATE_SIZE> bb::FieldVM< Field >::uint_internal_state |
Internal state array of uint256_t values.
Used as oracles for checking the correctness of field operations
Definition at line 182 of file field.fuzzer.hpp.
| bool bb::FieldVM< Field >::with_debug |
Flag to enable debug output.
Definition at line 187 of file field.fuzzer.hpp.