Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
keccakf1600.test.cpp
Go to the documentation of this file.
1#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3
4#include <algorithm>
5#include <cstddef>
6#include <cstdint>
7
20
21namespace bb::avm2::simulation {
22namespace {
23
24class KeccakSimulationTest : public ::testing::Test {
25 protected:
26 KeccakSimulationTest()
31 {}
32
33 MemoryStore memory;
34 ExecutionIdManager execution_id_manager;
35 NoopEventEmitter<KeccakF1600Event> keccak_event_emitter;
36 NoopEventEmitter<BitwiseEvent> bitwise_event_emitter;
37 NoopEventEmitter<RangeCheckEvent> range_check_event_emitter;
38 Bitwise bitwise;
39 RangeCheck range_check;
40 KeccakF1600 keccak;
41};
42
43void flatten_state(KeccakF1600State state, uint64_t* flattened)
44{
45 for (size_t i = 0; i < 5; i++) {
46 for (size_t j = 0; j < 5; j++) {
47 flattened[(j * 5) + i] = state[i][j];
48 }
49 }
50}
51
52TEST_F(KeccakSimulationTest, matchesReferenceImplementation)
53{
54 KeccakF1600State input = {
55 { { 0, 1, 2, 3, 4 }, { 5, 6, 7, 8, 9 }, { 10, 11, 12, 13, 14 }, { 15, 16, 17, 18, 19 }, { 20, 21, 22, 23, 24 } }
56 };
57
58 const MemoryAddress src_addr = 1979;
59 const MemoryAddress dst_addr = 3030;
60
61 uint64_t reference_input[25];
62 uint64_t flat_output[25];
63 flatten_state(input, reference_input);
64
65 for (size_t i = 0; i < 5; i++) {
66 for (size_t j = 0; j < 5; j++) {
67 memory.set(src_addr + static_cast<MemoryAddress>((i * 5) + j), MemoryValue::from<uint64_t>(input[i][j]));
68 }
69 }
70
71 keccak.permutation(memory, dst_addr, src_addr);
72 KeccakF1600State output;
73
74 for (size_t i = 0; i < 5; i++) {
75 for (size_t j = 0; j < 5; j++) {
76 MemoryValue val = memory.get(dst_addr + static_cast<MemoryAddress>((i * 5) + j));
77 EXPECT_EQ(val.get_tag(), MemoryTag::U64);
78 output[i][j] = val.as<uint64_t>();
79 }
80 }
81
82 flatten_state(output, flat_output);
83
84 ethash_keccakf1600(reference_input); // Mutate input
85 EXPECT_THAT(reference_input, testing::ElementsAreArray(flat_output));
86}
87
88// We simulate a tag error in the memory read.
89// We test that an exception of type KeccakF1600Exception is thrown with the string
90// "Read slice tag invalid - addr: 1979 tag: 6 (MemoryTag::U128)"
91TEST_F(KeccakSimulationTest, tagError)
92{
93 const MemoryAddress src_addr = 1970;
94 const MemoryAddress src_addr_wrong_tag = 1979;
95 const MemoryAddress dst_addr = 3030;
96 const MemoryTag wrong_tag = MemoryTag::U128;
97
98 // Initialize the full slice with U64 values
99 for (size_t i = 0; i < 5; i++) {
100 for (size_t j = 0; j < 5; j++) {
101 memory.set(src_addr + static_cast<MemoryAddress>((i * 5) + j),
103 }
104 }
105
106 // Override just the first value with U128 to trigger the tag error
107 memory.set(src_addr_wrong_tag, MemoryValue::from_tag_truncating(wrong_tag, 0));
108
110 keccak.permutation(memory, dst_addr, src_addr),
111 format("Read slice tag invalid - addr: ", src_addr_wrong_tag, " tag: ", static_cast<uint32_t>(wrong_tag)));
112}
113
114TEST_F(KeccakSimulationTest, srcSliceOutOfBounds)
115{
117 const MemoryAddress dst_addr = 3030;
118
119 EXPECT_THROW_WITH_MESSAGE(keccak.permutation(memory, dst_addr, src_addr), "Read slice out of range");
120}
121
122TEST_F(KeccakSimulationTest, dstSliceOutOfBounds)
123{
124 const MemoryAddress src_addr = 1970;
126
127 EXPECT_THROW_WITH_MESSAGE(keccak.permutation(memory, dst_addr, src_addr), "Write slice out of range");
128}
129
130} // namespace
131} // namespace bb::avm2::simulation
#define AVM_KECCAKF1600_STATE_SIZE
#define AVM_HIGHEST_MEM_ADDRESS
static TaggedValue from_tag_truncating(ValueTag tag, FF value)
void permutation(MemoryInterface &memory, MemoryAddress dst_addr, MemoryAddress src_addr) override
Permutation Keccak-f[1600] consisting in AVM_KECCAKF1600_NUM_ROUNDS (24) rounds and a state of 25 64-...
void set(MemoryAddress index, MemoryValue value) override
Definition memory.hpp:98
const MemoryValue & get(MemoryAddress index) const override
Definition memory.hpp:92
std::string format(Args... args)
Definition log.hpp:20
ExecutionIdManager execution_id_manager
EventEmitter< RangeCheckEvent > range_check_event_emitter
uint32_t dst_addr
RangeCheck range_check
void ethash_keccakf1600(uint64_t state[25]) NOEXCEPT
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessage)
Definition macros.hpp:7
std::array< std::array< uint64_t, 5 >, 5 > KeccakF1600State
TaggedValue MemoryValue
uint32_t MemoryAddress
ValueTag MemoryTag
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:123
Bitwise bitwise
NoopEventEmitter< BitwiseEvent > bitwise_event_emitter
KeccakF1600 keccak
NoopEventEmitter< KeccakF1600Event > keccak_event_emitter
MemoryStore memory