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
15
16namespace bb::avm2::constraining {
17namespace {
18
19using tracegen::KeccakF1600TraceBuilder;
20using tracegen::TestTraceContainer;
21using C = Column;
22
23using keccakf1600_relation = bb::avm2::keccakf1600<AvmFlavorSettings::FF>;
24using keccak_memory_relation = bb::avm2::keccak_memory<AvmFlavorSettings::FF>;
25
26TEST(KeccakF1600ConstrainingTest, EmptyRow)
27{
28 check_relation<keccakf1600_relation>(testing::empty_trace());
29}
30
31// Positive test of a single permutation with simulation and trace generation and checking interactions.
32TEST(KeccakF1600ConstrainingTest, SinglewithSimulationAndTraceGenInteractions)
33{
34 TestTraceContainer trace;
35
36 const MemoryAddress src_addr = 0;
37 const MemoryAddress dst_addr = 200;
38
39 testing::generate_keccak_trace(trace, { dst_addr }, { src_addr }, /*space_id=*/23);
40
41 check_all_interactions<tracegen::KeccakF1600TraceBuilder>(trace);
42 check_relation<keccakf1600_relation>(trace);
43 check_relation<keccak_memory_relation>(trace);
44}
45
46// Positive test for multiple permutation calls with simulation and trace generation.
47// We also check all interactions.
48TEST(KeccakF1600ConstrainingTest, MultipleWithSimulationAndTraceGenInteractions)
49{
50 TestTraceContainer trace;
51
52 constexpr size_t NUM_PERMUTATIONS = 3;
53
54 std::vector<MemoryAddress> src_addresses(NUM_PERMUTATIONS);
55 std::vector<MemoryAddress> dst_addresses(NUM_PERMUTATIONS);
56
57 for (size_t k = 0; k < NUM_PERMUTATIONS; ++k) {
58 src_addresses.at(k) = static_cast<MemoryAddress>(k * 200);
59 dst_addresses.at(k) = static_cast<MemoryAddress>((k * 200) + 1000);
60 }
61
62 testing::generate_keccak_trace(trace, dst_addresses, src_addresses, /*space_id=*/79);
63
64 check_all_interactions<tracegen::KeccakF1600TraceBuilder>(trace);
65 check_relation<keccakf1600_relation>(trace);
66 check_relation<keccak_memory_relation>(trace);
67}
68
69// Test tag error handling when a memory value has an incorrect tag.
70// We test when the memory tag is not U64 for a read value at A[x=2][y=1].
71// We check that the tag_error is 1 for this index (flattened index 7) and that
72// we correctly propagate the error to the top.
73TEST(KeccakF1600ConstrainingTest, TagErrorHandling)
74{
75 TestTraceContainer trace;
76
77 const MemoryAddress src_addr = 0;
78 const MemoryAddress dst_addr = 200;
79 const uint16_t space_id = 79;
80
81 // Standard Keccak layout: memory[(y * 5) + x] = A[x][y]
82 // Position A[x=2][y=1] in the 5x5 matrix corresponds to index (1 * 5) + 2 = 7 in the flattened array
83 const size_t error_offset = 7;
84 const MemoryTag error_tag = MemoryTag::U32; // Using U32 instead of U64 to trigger error
85
86 testing::generate_keccak_trace_with_tag_error(trace, dst_addr, src_addr, error_offset, error_tag, space_id);
87
88 check_all_interactions<tracegen::KeccakF1600TraceBuilder>(trace);
89 check_relation<keccakf1600_relation>(trace);
90 check_relation<keccak_memory_relation>(trace);
91}
92
93// Test slice error handling when the src address is out of bounds.
94TEST(KeccakF1600ConstrainingTest, SrcAddressOutOfBounds)
95{
96 TestTraceContainer trace;
97
99 const MemoryAddress dst_addr = 456;
100 const uint16_t space_id = 23;
101
103
104 check_all_interactions<tracegen::KeccakF1600TraceBuilder>(trace);
105 check_relation<keccakf1600_relation>(trace);
106 check_relation<keccak_memory_relation>(trace);
107}
108
109// Test slice error handling when the dst address is out of bounds.
110TEST(KeccakF1600ConstrainingTest, DstAddressOutOfBounds)
111{
112 TestTraceContainer trace;
113
114 const MemoryAddress src_addr = 123;
116 const uint16_t space_id = 23;
117
119
120 check_all_interactions<tracegen::KeccakF1600TraceBuilder>(trace);
121 check_relation<keccakf1600_relation>(trace);
122 check_relation<keccak_memory_relation>(trace);
123}
124
125// Negative test: when sel_slice_write is active, round must equal AVM_KECCAKF1600_NUM_ROUNDS (24).
126TEST(KeccakF1600ConstrainingTest, NegativeRoundCountAtWrite)
127{
128 TestTraceContainer trace = TestTraceContainer({ {
129 { C::precomputed_first_row, 1 },
130 },
131 {
132 { C::keccakf1600_sel, 1 },
133 { C::keccakf1600_sel_slice_write, 1 },
134 { C::keccakf1600_round, 23 },
135 } });
136
138 check_relation<keccakf1600_relation>(trace, keccakf1600_relation::SR_ROUND_COUNT_AT_WRITE),
139 "ROUND_COUNT_AT_WRITE");
140
141 // Setting round to 24 should satisfy the constraint.
142 trace.set(C::keccakf1600_round, 1, 24);
143 check_relation<keccakf1600_relation>(trace, keccakf1600_relation::SR_ROUND_COUNT_AT_WRITE);
144}
145
146// Negative test: sel cannot drop from 1 to 0 mid-block (i.e., without end == 1).
147// Row 0: first_row=1, sel=1, start=1 (LATCH_CONDITION=1 via first_row)
148// Row 1: sel=1, end=0 (mid-block row, sel must not drop to 0 here)
149// Row 2: sel=0 (violates TRACE_CONTINUITY at row 1)
150TEST(KeccakF1600ConstrainingTest, NegativeTraceContinuity)
151{
152 TestTraceContainer trace = TestTraceContainer({ {
153 { C::precomputed_first_row, 1 },
154 { C::keccakf1600_sel, 1 },
155 { C::keccakf1600_start, 1 },
156 },
157 {
158 { C::keccakf1600_sel, 1 },
159 },
160 {
161 { C::keccakf1600_sel, 0 },
162 } });
163
164 // sel drops from 1 to 0 at row 1 without end == 1, so this should fail.
165 EXPECT_THROW_WITH_MESSAGE(check_relation<keccakf1600_relation>(trace, keccakf1600_relation::SR_TRACE_CONTINUITY),
166 "TRACE_CONTINUITY");
167
168 // Setting end=1 on row 1 should satisfy the constraint.
169 trace.set(C::keccakf1600_end, 1, 1);
170
171 check_relation<keccakf1600_relation>(trace, keccakf1600_relation::SR_TRACE_CONTINUITY);
172}
173
174} // namespace
175} // namespace bb::avm2::constraining
#define EXPECT_THROW_WITH_MESSAGE(code, expectedMessageRegex)
Definition assert.hpp:193
#define AVM_KECCAKF1600_STATE_SIZE
#define AVM_HIGHEST_MEM_ADDRESS
void set(Column col, uint32_t row, const FF &value)
uint32_t src_addr
uint32_t dst_addr
TestTraceContainer trace
TEST(AvmFixedVKTests, FixedVKCommitments)
Test that the fixed VK commitments agree with the ones computed from precomputed columns.
void generate_keccak_trace(TestTraceContainer &trace, const std::vector< MemoryAddress > &dst_addresses, const std::vector< MemoryAddress > &src_addresses, uint16_t space_id)
void generate_keccak_trace_with_slice_error(TestTraceContainer &trace, MemoryAddress dst_address, MemoryAddress src_address, uint16_t space_id)
TestTraceContainer empty_trace()
Definition fixtures.cpp:153
void generate_keccak_trace_with_tag_error(TestTraceContainer &trace, MemoryAddress dst_address, MemoryAddress src_address, size_t error_offset, MemoryTag error_tag, uint16_t space_id)
uint32_t MemoryAddress
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13