Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pure_sha256.cpp
Go to the documentation of this file.
2
8
9namespace bb::avm2::simulation {
10
12 MemoryAddress state_addr,
13 MemoryAddress input_addr,
14 MemoryAddress output_addr)
15{
16 // Check address ranges
17 if (static_cast<uint64_t>(state_addr) + 7 > AVM_HIGHEST_MEM_ADDRESS ||
18 static_cast<uint64_t>(input_addr) + 15 > AVM_HIGHEST_MEM_ADDRESS ||
19 static_cast<uint64_t>(output_addr) + 7 > AVM_HIGHEST_MEM_ADDRESS) {
20 throw Sha256CompressionException("Memory address out of range for sha256 compression.");
21 }
22
23 std::array<uint32_t, 8> state;
24 for (size_t i = 0; i < 8; ++i) {
25 MemoryValue val = memory.get(static_cast<MemoryAddress>(state_addr + i));
26 if (val.get_tag() != MemoryTag::U32) {
27 throw Sha256CompressionException("Invalid tag for sha256 state values.");
28 }
29 state[i] = val.as<uint32_t>();
30 }
31
33 for (size_t i = 0; i < 16; ++i) {
34 MemoryValue val = memory.get(static_cast<MemoryAddress>(input_addr + i));
35 if (val.get_tag() != MemoryTag::U32) {
36 throw Sha256CompressionException("Invalid tag for sha256 input values.");
37 }
38 input[i] = val.as<uint32_t>();
39 }
40
41 std::array<uint32_t, 8> output = crypto::sha256_block(state, input);
42
43 for (size_t i = 0; i < 8; ++i) {
44 memory.set(static_cast<MemoryAddress>(output_addr + i), MemoryValue::from<uint32_t>(output[i]));
45 }
46}
47
48} // namespace bb::avm2::simulation
#define AVM_HIGHEST_MEM_ADDRESS
ValueTag get_tag() const
void compression(MemoryInterface &memory, MemoryAddress state_addr, MemoryAddress input_addr, MemoryAddress output_addr) override
uint32_t MemoryAddress
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
SHA-256 compression function (FIPS 180-4 Section 6.2.2)
Definition sha256.cpp:73
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13