Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::FieldVM< Field > Struct Template Reference

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_texport_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_SIZEfield_internal_state
 Internal state array of field elements.
 
std::array< numeric::uint256_t, INTERNAL_STATE_SIZEuint_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] >= 0x4000000000000000ULL)
 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.
 

Detailed Description

template<typename Field>
struct bb::FieldVM< Field >

Virtual machine for field arithmetic operations.

Template Parameters
FieldThe 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 146 of file field.fuzzer.hpp.

Constructor & Destructor Documentation

◆ FieldVM()

template<typename Field >
bb::FieldVM< Field >::FieldVM ( bool  with_debug = false,
size_t  max_steps = SIZE_MAX 
)
inline

Constructor for FieldVM.

Parameters
with_debugWhether to enable debug output
max_stepsMaximum number of steps to execute

Initializes the VM with default settings and zero-initialized state

Definition at line 211 of file field.fuzzer.hpp.

Member Function Documentation

◆ check_internal_state()

template<typename Field >
bool bb::FieldVM< Field >::check_internal_state ( ) const
inline

Check internal state consistency between field and uint256_t representations.

Returns
bool True if all state elements are consistent, false otherwise

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 1240 of file field.fuzzer.hpp.

◆ execute_instruction()

template<typename Field >
size_t bb::FieldVM< Field >::execute_instruction ( const unsigned char *  data_ptr)
inline

Execute a single VM instruction.

Parameters
data_ptrPointer to the instruction data
Returns
size_t Number of bytes consumed by the instruction

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

Parameters
data_ptr_indexPointer to the data containing the index
offsetOffset into the data
Returns
size_t The index value wrapped around INTERNAL_STATE_SIZE

< Total number of instructions

Helper function to get a uint256_t value from data

Parameters
data_ptr_valuePointer to the data containing the value
offsetOffset into the data
Returns
numeric::uint256_t The uint256_t value read from data

Helper function to get a uint64_t value from data

Parameters
data_ptr_valuePointer to the data containing the value
offsetOffset into the data
Returns
uint64_t The uint64_t value read from data

Definition at line 258 of file field.fuzzer.hpp.

◆ execute_parsed_instruction()

template<typename Field >
bool bb::FieldVM< Field >::execute_parsed_instruction ( const ParsedInstruction parsed)
inline

Execute a parsed instruction.

Parameters
parsedThe parsed instruction to execute
Returns
bool True if execution should continue, false if should stop

This method executes a previously parsed instruction. It provides debug output if enabled and returns whether execution should continue.

Definition at line 1159 of file field.fuzzer.hpp.

◆ export_uint_state()

template<typename Field >
std::vector< numeric::uint256_t > bb::FieldVM< Field >::export_uint_state ( ) const
inline

Export the final uint state as a vector of uint256_t values.

Returns
std::vector<numeric::uint256_t> The final uint state

This method creates a copy of the uint_internal_state array for external use, typically for state transfer between VM phases.

Definition at line 1262 of file field.fuzzer.hpp.

◆ get_max_steps()

template<typename Field >
size_t bb::FieldVM< Field >::get_max_steps ( ) const
inline

Get the current step limit.

Returns
size_t The current maximum number of steps

Definition at line 1303 of file field.fuzzer.hpp.

◆ get_step_count()

template<typename Field >
size_t bb::FieldVM< Field >::get_step_count ( ) const
inline

Get the number of steps executed.

Returns
size_t The number of steps executed

Definition at line 1277 of file field.fuzzer.hpp.

◆ has_remaining_steps()

template<typename Field >
bool bb::FieldVM< Field >::has_remaining_steps ( ) const
inline

Check if there are remaining steps available.

Returns
bool True if more steps can be executed

Definition at line 1310 of file field.fuzzer.hpp.

◆ parse_instructions()

template<typename Field >
std::pair< std::vector< ParsedInstruction >, size_t > bb::FieldVM< Field >::parse_instructions ( const unsigned char *  Data,
size_t  Size,
size_t  max_steps 
)
inline

Parse instructions from data buffer into a vector.

Parameters
DataPointer to the data buffer
SizeSize of the data buffer
max_stepsMaximum number of instructions to parse
Returns
std::pair<std::vector<ParsedInstruction>, size_t> Vector of parsed instructions and bytes consumed

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 1016 of file field.fuzzer.hpp.

◆ reduce_to_modulus()

template<typename Field >
static numeric::uint256_t bb::FieldVM< Field >::reduce_to_modulus ( const numeric::uint256_t value)
inlinestatic

Reduce a uint256_t value to the field's modulus.

Parameters
valueThe value to reduce
Returns
numeric::uint256_t The reduced value

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 1321 of file field.fuzzer.hpp.

◆ reset_step_count()

template<typename Field >
void bb::FieldVM< Field >::reset_step_count ( )
inline

Reset the step counter to 0.

Definition at line 1296 of file field.fuzzer.hpp.

◆ run()

template<typename Field >
size_t bb::FieldVM< Field >::run ( const unsigned char *  Data,
size_t  Size,
bool  reset_steps = true 
)
inline

Run the VM on input data.

Parameters
DataThe data to run the VM on
SizeThe size of the data
reset_stepsWhether to reset the step counter (default: true)
Returns
size_t The number of bytes consumed, or 0 if not enough data for settings

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 1197 of file field.fuzzer.hpp.

◆ set_max_steps()

template<typename Field >
void bb::FieldVM< Field >::set_max_steps ( size_t  new_max_steps)
inline

Set a new step limit for the VM.

Parameters
new_max_stepsThe new maximum number of steps

Definition at line 1291 of file field.fuzzer.hpp.

◆ verify_initial_state()

template<typename Field >
bool bb::FieldVM< Field >::verify_initial_state ( const std::vector< numeric::uint256_t > &  state) const
inline

Verify that the initial state is correctly loaded.

Parameters
stateThe state vector to verify against
Returns
bool True if the state is correctly loaded

This method verifies that the imported state matches the current internal state after proper modulus reduction and field conversion.

Definition at line 1339 of file field.fuzzer.hpp.

◆ was_stopped_by_max_steps()

template<typename Field >
bool bb::FieldVM< Field >::was_stopped_by_max_steps ( ) const
inline

Check if the VM was stopped due to reaching max steps.

Returns
bool True if the VM was stopped due to max steps

Definition at line 1284 of file field.fuzzer.hpp.

Member Data Documentation

◆ field_internal_state

template<typename Field >
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 174 of file field.fuzzer.hpp.

◆ LARGE_MODULUS

template<typename Field >
constexpr bool bb::FieldVM< Field >::LARGE_MODULUS = (Field::modulus.data[3] >= 0x4000000000000000ULL)
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 152 of file field.fuzzer.hpp.

◆ max_steps

template<typename Field >
size_t bb::FieldVM< Field >::max_steps

Maximum number of steps the VM can execute.

Definition at line 196 of file field.fuzzer.hpp.

◆ settings

template<typename Field >
VMSettings bb::FieldVM< Field >::settings

VM settings controlling which operations are enabled.

Definition at line 191 of file field.fuzzer.hpp.

◆ step_count

template<typename Field >
size_t bb::FieldVM< Field >::step_count {}

Number of steps executed so far.

Definition at line 201 of file field.fuzzer.hpp.

◆ SUPPORTS_SQRT

template<typename Field >
constexpr bool bb::FieldVM< Field >::SUPPORTS_SQRT
staticconstexpr
Initial value:
= []() {
if constexpr (requires { Field::primitive_root_log_size(); }) {
return Field::primitive_root_log_size() >= 6;
} else {
return true;
}
}()

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 159 of file field.fuzzer.hpp.

◆ uint_internal_state

template<typename Field >
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 181 of file field.fuzzer.hpp.

◆ with_debug

template<typename Field >
bool bb::FieldVM< Field >::with_debug

Flag to enable debug output.

Definition at line 186 of file field.fuzzer.hpp.


The documentation for this struct was generated from the following file: