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
21
22namespace bb::avm2::simulation {
23namespace {
24
25class KeccakSimulationTest : public ::testing::Test {
26 protected:
27 KeccakSimulationTest()
34 {}
35
36 MemoryStore memory;
37 ExecutionIdManager execution_id_manager;
38 NoopEventEmitter<KeccakF1600Event> keccak_event_emitter;
39 NoopEventEmitter<BitwiseEvent> bitwise_event_emitter;
40 NoopEventEmitter<RangeCheckEvent> range_check_event_emitter;
41 NoopEventEmitter<GreaterThanEvent> greater_than_event_emitter;
42 NoopEventEmitter<FieldGreaterThanEvent> field_gt_event_emitter;
43 Bitwise bitwise;
44 RangeCheck range_check;
45 FieldGreaterThan field_gt;
46 GreaterThan greater_than;
47 KeccakF1600 keccak;
48};
49
50void flatten_state(KeccakF1600State state, uint64_t* flattened)
51{
52 for (size_t i = 0; i < 5; i++) {
53 for (size_t j = 0; j < 5; j++) {
54 flattened[(j * 5) + i] = state[i][j];
55 }
56 }
57}
58
59TEST_F(KeccakSimulationTest, matchesReferenceImplementation)
60{
61 KeccakF1600State input = {
62 { { 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 } }
63 };
64
65 const MemoryAddress src_addr = 1979;
66 const MemoryAddress dst_addr = 3030;
67
68 uint64_t reference_input[25];
69 uint64_t flat_output[25];
70 flatten_state(input, reference_input);
71
72 for (size_t i = 0; i < 5; i++) {
73 for (size_t j = 0; j < 5; j++) {
74 memory.set(src_addr + static_cast<MemoryAddress>((i * 5) + j), MemoryValue::from<uint64_t>(input[i][j]));
75 }
76 }
77
78 keccak.permutation(memory, dst_addr, src_addr);
79 KeccakF1600State output;
80
81 for (size_t i = 0; i < 5; i++) {
82 for (size_t j = 0; j < 5; j++) {
83 MemoryValue val = memory.get(dst_addr + static_cast<MemoryAddress>((i * 5) + j));
84 EXPECT_EQ(val.get_tag(), MemoryTag::U64);
85 output[i][j] = val.as<uint64_t>();
86 }
87 }
88
89 flatten_state(output, flat_output);
90
91 ethash_keccakf1600(reference_input); // Mutate input
92 EXPECT_THAT(reference_input, testing::ElementsAreArray(flat_output));
93}
94
95// We simulate a tag error in the memory read.
96// We test that an exception of type KeccakF1600Exception is thrown with the string
97// "Read slice tag invalid - addr: 1979 tag: 6 (MemoryTag::U128)"
98TEST_F(KeccakSimulationTest, tagError)
99{
100 const MemoryAddress src_addr = 1970;
101 const MemoryAddress src_addr_wrong_tag = 1979;
102 const MemoryAddress dst_addr = 3030;
103 const MemoryTag wrong_tag = MemoryTag::U128;
104
105 // Initialize the full slice with U64 values
106 for (size_t i = 0; i < 5; i++) {
107 for (size_t j = 0; j < 5; j++) {
108 memory.set(src_addr + static_cast<MemoryAddress>((i * 5) + j),
110 }
111 }
112
113 // Override just the first value with U128 to trigger the tag error
114 memory.set(src_addr_wrong_tag, MemoryValue::from_tag_truncating(wrong_tag, 0));
115
117 keccak.permutation(memory, dst_addr, src_addr),
118 format("Read slice tag invalid - addr: ", src_addr_wrong_tag, " tag: ", static_cast<uint32_t>(wrong_tag)));
119}
120
121TEST_F(KeccakSimulationTest, srcSliceOutOfBounds)
122{
124 const MemoryAddress dst_addr = 3030;
125
126 EXPECT_THROW_WITH_MESSAGE(keccak.permutation(memory, dst_addr, src_addr), "Read slice out of range");
127}
128
129TEST_F(KeccakSimulationTest, dstSliceOutOfBounds)
130{
131 const MemoryAddress src_addr = 1970;
133
134 EXPECT_THROW_WITH_MESSAGE(keccak.permutation(memory, dst_addr, src_addr), "Write slice out of range");
135}
136
137} // namespace
138} // 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
const MemoryValue & get(MemoryAddress index) const override
std::string format(Args... args)
Definition log.hpp:21
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
testing::StrictMock< MockGreaterThan > greater_than
#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:188
Bitwise bitwise
FieldGreaterThan field_gt
NoopEventEmitter< GreaterThanEvent > greater_than_event_emitter
NoopEventEmitter< FieldGreaterThanEvent > field_gt_event_emitter
NoopEventEmitter< BitwiseEvent > bitwise_event_emitter
KeccakF1600 keccak
NoopEventEmitter< KeccakF1600Event > keccak_event_emitter
MemoryStore memory