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("0x09348974e76c3602893d7a4b4bb52c2ec746f1ade5004ac471d0fbb4587a81a6");
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(), compute_public_bytecode_first_field(bytecode.size()));
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 = { compute_public_bytecode_first_field(bytecode.size()) };
81 prepended_bytecode_fields.reserve(1 + bytecode_fields.size());
82 prepended_bytecode_fields.insert(prepended_bytecode_fields.end(), bytecode_fields.begin(), bytecode_fields.end());
83
84 auto hash = RawPoseidon2::hash(prepended_bytecode_fields);
85 EXPECT_CALL(poseidon2, hash(prepended_bytecode_fields)).WillOnce(Return(hash));
86
88
89 EXPECT_THAT(hashing_events.dump_events(),
90 AllOf(SizeIs(1),
91 ElementsAre(AllOf(Field(&BytecodeHashingEvent::bytecode_id, 0xc0ffee),
93 Field(&BytecodeHashingEvent::bytecode_fields, bytecode_fields)))));
94}
95
96} // namespace
97} // namespace bb::avm2::simulation
std::shared_ptr< Napi::ThreadSafeFunction > 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.
AVM range check gadget for witness generation.
std::vector< FF > encode_bytecode(std::span< const uint8_t > bytecode)
FF compute_public_bytecode_first_field(size_t bytecode_size)
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:155
std::vector< uint8_t > to_buffer(T const &value)