Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::avm2::simulation::Sha256 Class Reference

#include <sha256.hpp>

Inheritance diagram for bb::avm2::simulation::Sha256:
bb::avm2::simulation::Sha256Interface

Public Member Functions

 Sha256 (ExecutionIdGetterInterface &execution_id_manager, BitwiseInterface &bitwise, GreaterThanInterface &gt, EventEmitterInterface< Sha256CompressionEvent > &event_emitter)
 
void compression (MemoryInterface &memory, MemoryAddress state_addr, MemoryAddress input_addr, MemoryAddress output_addr) override
 Execute the SHA-256 compression function: read state and input from memory, compress, and write output.
 
- Public Member Functions inherited from bb::avm2::simulation::Sha256Interface
virtual ~Sha256Interface ()=default
 

Private Member Functions

MemoryValue shr (const MemoryValue &x, uint8_t shift)
 Perform a 32-bit right shift on a MemoryValue.
 
MemoryValue ror (const MemoryValue &x, uint8_t shift)
 Perform a 32-bit right rotation on a MemoryValue.
 
MemoryValue modulo_sum (std::span< const MemoryValue > values)
 Sum a span of U32 MemoryValues and return the result modulo 2^32.
 

Private Attributes

ExecutionIdGetterInterfaceexecution_id_manager
 
BitwiseInterfacebitwise
 
GreaterThanInterfacegt
 
EventEmitterInterface< Sha256CompressionEvent > & events
 

Detailed Description

Definition at line 17 of file sha256.hpp.

Constructor & Destructor Documentation

◆ Sha256()

bb::avm2::simulation::Sha256::Sha256 ( ExecutionIdGetterInterface execution_id_manager,
BitwiseInterface bitwise,
GreaterThanInterface gt,
EventEmitterInterface< Sha256CompressionEvent > &  event_emitter 
)
inline

Definition at line 19 of file sha256.hpp.

Member Function Documentation

◆ compression()

void bb::avm2::simulation::Sha256::compression ( MemoryInterface memory,
MemoryAddress  state_addr,
MemoryAddress  input_addr,
MemoryAddress  output_addr 
)
overridevirtual

Execute the SHA-256 compression function: read state and input from memory, compress, and write output.

Events are emitted in the following flavors:

  • Normal execution: all fields populated (state, input of 16 elements, computed output).
  • Error (address out of range, invalid state tag, or invalid input tag): state is partially populated (up to the point of failure), input contains elements read before the error, output is zeroed.

In all cases the event is emitted before re-throwing the exception.

Parameters
memoryThe memory interface to read state/input from and write output to.
state_addrThe starting address of the 8-element hash state in memory.
input_addrThe starting address of the 16-element hash input in memory.
output_addrThe starting address where the 8-element output will be written.
Exceptions
Sha256CompressionExceptionIf any of state/input/output address ranges exceed the maximum memory address (checked first).
Sha256CompressionExceptionIf any of the 8 state values do not have tag U32 (checked after address validation).
Sha256CompressionExceptionIf any of the 16 input values do not have tag U32 (checked after state tag validation, on first invalid element).

Implements bb::avm2::simulation::Sha256Interface.

Definition at line 135 of file sha256.cpp.

◆ modulo_sum()

MemoryValue bb::avm2::simulation::Sha256::modulo_sum ( std::span< const MemoryValue values)
private

Sum a span of U32 MemoryValues and return the result modulo 2^32.

Parameters
valuesA span of MemoryValue elements, each expected to hold a uint32_t.
Returns
The sum reduced modulo 2^32, wrapped in a MemoryValue.
Precondition
Every element in values must be tagged as U32. This is an internal helper; all callers (sha256_compress) guarantee the precondition by validating tags before invoking this function. The cast exception is therefore never thrown in practice.
Note
Asserts that both the low and high 32-bit halves of the 64-bit sum are in range.

Definition at line 93 of file sha256.cpp.

◆ ror()

MemoryValue bb::avm2::simulation::Sha256::ror ( const MemoryValue x,
uint8_t  shift 
)
private

Perform a 32-bit right rotation on a MemoryValue.

Parameters
xThe value to rotate (must hold a uint32_t).
shiftThe number of bits to rotate right (must be non-zero and < 32).
Returns
The rotated 32-bit result wrapped in a MemoryValue.
Precondition
x must be tagged as U32. This is an internal helper; all callers (sha256_compress) guarantee the precondition by validating tags before invoking this function. The cast exception is therefore never thrown in practice.
shift must satisfy 0 < shift < 32. A shift >= 32 causes undefined behavior per the C++ standard for 32-bit operands. A shift == 0 also causes undefined behavior because the reconstruction lo << (32 - shift) becomes a left shift by 32. All callers use fixed SHA-256 rotation amounts (2, 6, 7, 11, 13, 17, 18, 19, 22, 25), so this precondition is always satisfied.
Note
Asserts that the lower bits extracted during decomposition are in range (lo < 2^shift).

Definition at line 43 of file sha256.cpp.

◆ shr()

MemoryValue bb::avm2::simulation::Sha256::shr ( const MemoryValue x,
uint8_t  shift 
)
private

Perform a 32-bit right shift on a MemoryValue.

Parameters
xThe value to shift (must hold a uint32_t).
shiftThe number of bits to shift right (only 3 and 10 are used in SHA-256).
Returns
The shifted 32-bit result wrapped in a MemoryValue.
Precondition
x must be tagged as U32. This is an internal helper; all callers (sha256_compress) guarantee the precondition by validating tags before invoking this function. The cast exception is therefore never thrown in practice.
shift must satisfy shift < 32. A shift >= 32 would cause undefined behavior per the C++ standard for 32-bit operands. All callers use fixed SHA-256 shift amounts (3, 10), so this precondition is always satisfied.
Note
Asserts that the lower bits extracted during decomposition are in range (lo < 2^shift).

Definition at line 70 of file sha256.cpp.

Member Data Documentation

◆ bitwise

BitwiseInterface& bb::avm2::simulation::Sha256::bitwise
private

Definition at line 41 of file sha256.hpp.

◆ events

EventEmitterInterface<Sha256CompressionEvent>& bb::avm2::simulation::Sha256::events
private

Definition at line 43 of file sha256.hpp.

◆ execution_id_manager

ExecutionIdGetterInterface& bb::avm2::simulation::Sha256::execution_id_manager
private

Definition at line 40 of file sha256.hpp.

◆ gt

GreaterThanInterface& bb::avm2::simulation::Sha256::gt
private

Definition at line 42 of file sha256.hpp.


The documentation for this class was generated from the following files: