Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bytecode_hashing.test.cpp
Go to the documentation of this file.
2
3#include "gmock/gmock.h"
4#include <cstdint>
5#include <gtest/gtest.h>
6#include <memory>
7#include <optional>
8#include <vector>
9
21
22namespace bb::avm2::simulation {
23
24using ::testing::AllOf;
25using ::testing::ElementsAre;
26using ::testing::Field;
27using ::testing::Return;
28using ::testing::SizeIs;
29using ::testing::StrictMock;
30
32
33namespace {
34
35class BytecodeHashingTest : public ::testing::Test {
36 protected:
37 BytecodeHashingTest()
39 {}
40
41 StrictMock<MockPoseidon2> poseidon2;
42 EventEmitter<BytecodeHashingEvent> hashing_events;
43 BytecodeHasher bytecode_hasher;
44};
45
46TEST_F(BytecodeHashingTest, SimpleHash)
47{
48 // The hardcoded value is taken from noir-projects/aztec-nr/aztec/src/hash.nr:
49 FF hash = FF("0x16d621c3387156ef53754679e7b2c9be8f0bceeb44aa59a74991df3b0b42a0bf");
50
51 std::vector<FF> bytecode_fields = {};
52 for (uint32_t i = 1; i < 100; i++) {
53 bytecode_fields.push_back(FF(i));
54 }
55
56 std::vector<uint8_t> bytecode = {};
57 for (auto bytecode_field : bytecode_fields) {
58 auto bytes = to_buffer(bytecode_field);
59 // Each field elt of encoded bytecode represents 31 bytes, but to_buffer returns 32, hence start at +1:
60 bytecode.insert(bytecode.end(), bytes.begin() + 1, bytes.end());
61 }
62
63 bytecode_fields.insert(bytecode_fields.begin(), GENERATOR_INDEX__PUBLIC_BYTECODE);
64
65 EXPECT_CALL(poseidon2, hash(bytecode_fields)).WillOnce(Return(hash));
66
68
69 EXPECT_THAT(hashing_events.dump_events(),
70 AllOf(SizeIs(1),
71 ElementsAre(AllOf(Field(&BytecodeHashingEvent::bytecode_id, 0xc0ffee),
73 Field(&BytecodeHashingEvent::bytecode_fields, SizeIs(99))))));
74}
75
76TEST_F(BytecodeHashingTest, Hash)
77{
78 std::vector<uint8_t> bytecode = testing::random_bytes(500);
79 std::vector<FF> bytecode_fields = encode_bytecode(bytecode);
80 std::vector<FF> prepended_bytecode_fields = { GENERATOR_INDEX__PUBLIC_BYTECODE };
81 prepended_bytecode_fields.insert(prepended_bytecode_fields.end(), bytecode_fields.begin(), bytecode_fields.end());
82
83 auto hash = RawPoseidon2::hash(prepended_bytecode_fields);
84 EXPECT_CALL(poseidon2, hash(prepended_bytecode_fields)).WillOnce(Return(hash));
85
87
88 EXPECT_THAT(hashing_events.dump_events(),
89 AllOf(SizeIs(1),
90 ElementsAre(AllOf(Field(&BytecodeHashingEvent::bytecode_id, 0xc0ffee),
92 Field(&BytecodeHashingEvent::bytecode_fields, bytecode_fields)))));
93}
94
95} // namespace
96} // namespace bb::avm2::simulation
#define GENERATOR_INDEX__PUBLIC_BYTECODE
BytecodeHasher bytecode_hasher
EventEmitter< BytecodeHashingEvent > hashing_events
void assert_public_bytecode_commitment(const BytecodeId &bytecode_id, const std::vector< uint8_t > &bytecode, const FF &public_bytecode_commitment) override
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
void hash(State &state) noexcept
std::vector< FF > encode_bytecode(std::span< const uint8_t > bytecode)
std::vector< uint8_t > random_bytes(size_t n)
Definition fixtures.cpp:33
AvmFlavorSettings::FF FF
Definition field.hpp:10
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:188
typename Flavor::FF FF
std::vector< uint8_t > to_buffer(T const &value)