Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
poseidon2.test.cpp
Go to the documentation of this file.
2
3#include <gmock/gmock.h>
4#include <gtest/gtest.h>
5
10
11namespace bb::avm2::simulation {
12namespace {
13
14using ::testing::_;
15using ::testing::ElementsAre;
16using ::testing::ElementsAreArray;
17using ::testing::Field;
18using ::testing::StrictMock;
19
20class Poseidon2SimulationTest : public ::testing::Test {
21 protected:
22 Poseidon2SimulationTest()
23 {
24 EXPECT_CALL(execution_id_manager, get_execution_id())
25 .WillRepeatedly(testing::Return(0)); // Default execution ID for testing
26 }
27
28 EventEmitter<Poseidon2HashEvent> hash_event_emitter;
29 EventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
30 EventEmitter<Poseidon2PermutationMemoryEvent> perm_mem_event_emitter;
31
32 StrictMock<MockExecutionIdManager> execution_id_manager;
33
34 FakeGreaterThan gt;
36 Poseidon2(execution_id_manager, gt, hash_event_emitter, perm_event_emitter, perm_mem_event_emitter);
37};
38
39TEST_F(Poseidon2SimulationTest, Hash)
40{
41 // Taken From barretenberg/crypto/poseidon2/poseidon2.test.cpp
42 FF a("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
43 FF b("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
44 FF c("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
45 FF d("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
46
47 std::vector<FF> input = { a, b, c, d };
48
49 FF result = poseidon2.hash(input);
50 FF expected("0x2f43a0f83b51a6f5fc839dea0ecec74947637802a579fa9841930a25a0bcec11");
51 EXPECT_EQ(result, expected);
52
53 std::vector<Poseidon2HashEvent> event_result = hash_event_emitter.dump_events();
54
55 EXPECT_THAT(event_result,
56 ElementsAre(testing::AllOf(Field(&Poseidon2HashEvent::inputs, ElementsAreArray(input)),
58 Field(&Poseidon2HashEvent::output, expected))));
59}
60
61TEST_F(Poseidon2SimulationTest, Permutation)
62{
63 // Taken From barretenberg/crypto/poseidon2/poseidon2.test.cpp
64 FF a("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
65 FF b("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
66 FF c("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
67 FF d("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789");
68
69 std::array<FF, 4> input = { a, b, c, d };
70 auto result = poseidon2.permutation(input);
71
72 std::array<FF, 4> expected = {
73 FF("0x2bf1eaf87f7d27e8dc4056e9af975985bccc89077a21891d6c7b6ccce0631f95"),
74 FF("0x0c01fa1b8d0748becafbe452c0cb0231c38224ea824554c9362518eebdd5701f"),
75 FF("0x018555a8eb50cf07f64b019ebaf3af3c925c93e631f3ecd455db07bbb52bbdd3"),
76 FF("0x0cbea457c91c22c6c31fd89afd2541efc2edf31736b9f721e823b2165c90fd41"),
77 };
78 EXPECT_THAT(result, ElementsAreArray(expected));
79
81
82 EXPECT_THAT(event_results,
83 ElementsAre(AllOf(Field(&Poseidon2PermutationEvent::input, ElementsAreArray(input)),
84 Field(&Poseidon2PermutationEvent::output, ElementsAreArray(expected)))));
85}
86
87// This test may seem redundant, but since we kind of re-implement the Poseidon2 sponge function, it's good to have it.
88TEST_F(Poseidon2SimulationTest, RandomHash)
89{
90 std::vector<FF> input;
91 input.reserve(14);
92
93 for (int i = 0; i < 14; i++) {
94 input.push_back(FF::random_element());
95 }
96
97 FF result = poseidon2.hash(input);
98 FF bb_result = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash(input);
99
100 EXPECT_EQ(result, bb_result);
101}
102
103TEST_F(Poseidon2SimulationTest, PermutationWithMemory)
104{
105 MemoryStore mem;
106 MemoryAddress src_address = 0;
107
108 // Inputs taken From barretenberg/crypto/poseidon2/poseidon2.test.cpp
109 std::array<FF, 4> inputs = { FF("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789"),
110 FF("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789"),
111 FF("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789"),
112 FF("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789") };
113 // Fill the memory with 4 FF Elements
114 for (uint32_t i = 0; i < 4; ++i) {
115 mem.set(src_address + i, MemoryValue::from<FF>(inputs[i]));
116 }
117
118 MemoryAddress dst_address = 4;
119
120 poseidon2.permutation(mem, src_address, dst_address);
121
122 std::array<FF, 4> expected = {
123 FF("0x2bf1eaf87f7d27e8dc4056e9af975985bccc89077a21891d6c7b6ccce0631f95"),
124 FF("0x0c01fa1b8d0748becafbe452c0cb0231c38224ea824554c9362518eebdd5701f"),
125 FF("0x018555a8eb50cf07f64b019ebaf3af3c925c93e631f3ecd455db07bbb52bbdd3"),
126 FF("0x0cbea457c91c22c6c31fd89afd2541efc2edf31736b9f721e823b2165c90fd41"),
127 };
128
129 // Read the memory at the destination address and check if it matches the expected output
130 for (uint32_t i = 0; i < expected.size(); ++i) {
131 EXPECT_EQ(mem.get(dst_address + i).as_ff(), expected[i]);
132 }
133}
134
135TEST_F(Poseidon2SimulationTest, PermutationWithMemoryAddressOutOfRange)
136{
137 MemoryStore mem;
138 MemoryAddress src_address = AVM_HIGHEST_MEM_ADDRESS; // This will cause an out of range error
139 MemoryAddress dst_address = 0;
140
141 EXPECT_THROW(poseidon2.permutation(mem, src_address, dst_address), std::runtime_error);
142}
143
144TEST_F(Poseidon2SimulationTest, PermutationWithMemoryInvalidMemTag)
145{
146 MemoryStore mem;
147 MemoryAddress src_address = 0;
148 MemoryAddress dst_address = 4;
149
150 // Inputs taken From barretenberg/crypto/poseidon2/poseidon2.test.cpp
152 MemoryValue::from<FF>(FF("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789")),
153 MemoryValue::from<FF>(FF("9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789")),
154 MemoryValue::from<uint64_t>(123456789), // Invalid tag
155 MemoryValue::from<FF>(FF("0x9a807b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789"))
156 };
157
158 // Fill the memory with 4 elements
159 for (uint32_t i = 0; i < inputs.size(); ++i) {
160 mem.set(src_address + i, inputs[i]);
161 }
162
163 EXPECT_THROW(poseidon2.permutation(mem, src_address, dst_address), Poseidon2Exception);
164}
165
166} // namespace
167} // namespace bb::avm2::simulation
#define AVM_HIGHEST_MEM_ADDRESS
void set(MemoryAddress index, MemoryValue value) override
Definition memory.hpp:98
const MemoryValue & get(MemoryAddress index) const override
Definition memory.hpp:92
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
Implements a parallelized batch insertion indexed tree Accepts template argument of the type of store...
ExecutionIdManager execution_id_manager
MemoryStore mem
GreaterThan gt
FF a
FF b
NoopEventEmitter< Poseidon2PermutationEvent > perm_event_emitter
NoopEventEmitter< Poseidon2PermutationMemoryEvent > perm_mem_event_emitter
EventEmitter< Poseidon2HashEvent > hash_event_emitter
uint32_t MemoryAddress
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:123
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::vector< std::array< FF, 4 > > intermediate_states
static field random_element(numeric::RNG *engine=nullptr) noexcept