Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
pure_sha256.cpp
Go to the documentation of this file.
1
#include "
barretenberg/vm2/simulation/standalone/pure_sha256.hpp
"
2
3
#include "
barretenberg/crypto/sha256/sha256.hpp
"
4
#include "
barretenberg/vm2/common/aztec_constants.hpp
"
5
#include "
barretenberg/vm2/common/tagged_value.hpp
"
6
#include "
barretenberg/vm2/simulation/events/sha256_event.hpp
"
7
#include "
barretenberg/vm2/simulation/interfaces/memory.hpp
"
8
9
namespace
bb::avm2::simulation
{
10
11
void
PureSha256::compression
(
MemoryInterface
&
memory
,
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
32
std::array<uint32_t, 16>
input;
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
aztec_constants.hpp
AVM_HIGHEST_MEM_ADDRESS
#define AVM_HIGHEST_MEM_ADDRESS
Definition
aztec_constants.hpp:45
bb::avm2::TaggedValue
Definition
tagged_value.hpp:114
bb::avm2::TaggedValue::as
T as() const
Definition
tagged_value.hpp:160
bb::avm2::TaggedValue::get_tag
ValueTag get_tag() const
Definition
tagged_value.cpp:380
bb::avm2::memory
Definition
memory.hpp:36
bb::avm2::simulation::MemoryInterface
Definition
memory.hpp:10
bb::avm2::simulation::PureSha256::compression
void compression(MemoryInterface &memory, MemoryAddress state_addr, MemoryAddress input_addr, MemoryAddress output_addr) override
Definition
pure_sha256.cpp:11
sha256.hpp
bb::avm2::simulation
Definition
address_derivation_event.hpp:6
bb::avm2::ValueTag::U32
@ U32
bb::avm2::MemoryAddress
uint32_t MemoryAddress
Definition
memory_types.hpp:11
bb::crypto::sha256_block
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
std::get
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition
tuple.hpp:13
pure_sha256.hpp
sha256_event.hpp
memory.hpp
bb::avm2::simulation::Sha256CompressionException
Definition
sha256_event.hpp:9
tagged_value.hpp
src
barretenberg
vm2
simulation
standalone
pure_sha256.cpp
Generated by
1.9.8