Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm2/common/aztec_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ struct PublicKeys {
AffinePoint incoming_viewing_key;
AffinePoint outgoing_viewing_key;
AffinePoint tagging_key;

std::vector<FF> to_fields() const
{
return { nullifier_key.x, nullifier_key.y, incoming_viewing_key.x, incoming_viewing_key.y,
outgoing_viewing_key.x, outgoing_viewing_key.y, tagging_key.x, tagging_key.y };
}
};

struct ContractInstance {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "barretenberg/vm2/simulation/address_derivation.hpp"

#include <cassert>

#include "barretenberg/vm/aztec_constants.hpp"
#include "barretenberg/vm2/simulation/lib/contract_crypto.hpp"

namespace bb::avm2::simulation {

void AddressDerivation::assert_derivation(const AztecAddress& address, const ContractInstance& instance)
{
// TODO: Cache and deduplicate.
// TODO: Use gadget.
assert(compute_contract_address(instance) == address);
events.emit({ .address = address, .instance = instance });
}

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "barretenberg/vm2/common/aztec_types.hpp"
#include "barretenberg/vm2/simulation/events/address_derivation_event.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"

namespace bb::avm2::simulation {

class AddressDerivationInterface {
public:
virtual ~AddressDerivationInterface() = default;
virtual void assert_derivation(const AztecAddress& address, const ContractInstance& instance) = 0;
};

class AddressDerivation : public AddressDerivationInterface {
public:
AddressDerivation(EventEmitterInterface<AddressDerivationEvent>& events)
: events(events)
{}

void assert_derivation(const AztecAddress& address, const ContractInstance& instance) override;

private:
EventEmitterInterface<AddressDerivationEvent>& events;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address)
// TODO: catch errors etc.
// TODO: we should trigger the proper merkle checks etc. The raw DB doesn't.
ContractInstance instance = db.get_contract_instance(address);
address_derivation.assert_derivation(address, instance);
ContractClass klass = db.get_contract_class(instance.contract_class_id);
class_id_derivation.assert_derivation(instance.contract_class_id, klass);
Comment on lines +20 to +22
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might move these checks inside the DB.

auto bytecode_id = next_bytecode_id++;
info("Bytecode for ", address, " successfully retrieved!");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

#include "barretenberg/vm2/common/aztec_types.hpp"
#include "barretenberg/vm2/common/map.hpp"
#include "barretenberg/vm2/simulation/address_derivation.hpp"
#include "barretenberg/vm2/simulation/class_id_derivation.hpp"
#include "barretenberg/vm2/simulation/events/bytecode_events.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"
#include "barretenberg/vm2/simulation/lib/raw_data_db.hpp"
#include "barretenberg/vm2/simulation/lib/serialization.hpp"
#include "barretenberg/vm2/simulation/siloing.hpp"

namespace bb::avm2::simulation {

Expand All @@ -33,10 +36,14 @@ class TxBytecodeManagerInterface {
class TxBytecodeManager : public TxBytecodeManagerInterface {
public:
TxBytecodeManager(RawDataDBInterface& db,
AddressDerivationInterface& address_derivation,
ClassIdDerivationInterface& class_id_derivation,
EventEmitterInterface<BytecodeRetrievalEvent>& retrieval_events,
EventEmitterInterface<BytecodeHashingEvent>& hash_events,
EventEmitterInterface<BytecodeDecompositionEvent>& decomposition_events)
: db(db)
, address_derivation(address_derivation)
, class_id_derivation(class_id_derivation)
, retrieval_events(retrieval_events)
, hash_events(hash_events)
, decomposition_events(decomposition_events)
Expand All @@ -47,6 +54,8 @@ class TxBytecodeManager : public TxBytecodeManagerInterface {

private:
RawDataDBInterface& db;
AddressDerivationInterface& address_derivation;
ClassIdDerivationInterface& class_id_derivation;
EventEmitterInterface<BytecodeRetrievalEvent>& retrieval_events;
EventEmitterInterface<BytecodeHashingEvent>& hash_events;
EventEmitterInterface<BytecodeDecompositionEvent>& decomposition_events;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "barretenberg/vm2/simulation/class_id_derivation.hpp"

#include <cassert>

#include "barretenberg/vm/aztec_constants.hpp"
#include "barretenberg/vm2/simulation/lib/contract_crypto.hpp"

namespace bb::avm2::simulation {

void ClassIdDerivation::assert_derivation(const ContractClassId& class_id, const ContractClass& klass)
{
// TODO: Cache and deduplicate.
// TODO: Use gadget.
assert(compute_contract_class_id(
klass.artifact_hash, klass.private_function_root, klass.public_bytecode_commitment) == class_id);
events.emit({ .class_id = class_id, .klass = klass });
}

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include "barretenberg/vm2/common/aztec_types.hpp"
#include "barretenberg/vm2/simulation/events/class_id_derivation_event.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"

namespace bb::avm2::simulation {

class ClassIdDerivationInterface {
public:
virtual ~ClassIdDerivationInterface() = default;
virtual void assert_derivation(const ContractClassId& class_id, const ContractClass& klass) = 0;
};

class ClassIdDerivation : public ClassIdDerivationInterface {
public:
ClassIdDerivation(EventEmitterInterface<ClassIdDerivationEvent>& events)
: events(events)
{}

void assert_derivation(const ContractClassId& class_id, const ContractClass& klass) override;

private:
EventEmitterInterface<ClassIdDerivationEvent>& events;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "barretenberg/vm2/common/aztec_types.hpp"

namespace bb::avm2::simulation {

struct AddressDerivationEvent {
AztecAddress address;
ContractInstance instance;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "barretenberg/vm2/common/aztec_types.hpp"

namespace bb::avm2::simulation {

struct ClassIdDerivationEvent {
ContractClassId class_id;
// WARNING: this class has the whole bytecode. Create a new class.
ContractClass klass;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#pragma once

#include "barretenberg/vm2/simulation/events/address_derivation_event.hpp"
#include "barretenberg/vm2/simulation/events/addressing_event.hpp"
#include "barretenberg/vm2/simulation/events/alu_event.hpp"
#include "barretenberg/vm2/simulation/events/bytecode_events.hpp"
#include "barretenberg/vm2/simulation/events/class_id_derivation_event.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"
#include "barretenberg/vm2/simulation/events/execution_event.hpp"
#include "barretenberg/vm2/simulation/events/memory_event.hpp"
#include "barretenberg/vm2/simulation/events/siloing_event.hpp"

namespace bb::avm2::simulation {

Expand All @@ -17,6 +20,9 @@ struct EventsContainer {
EventEmitterInterface<BytecodeRetrievalEvent>::Container bytecode_retrieval;
EventEmitterInterface<BytecodeHashingEvent>::Container bytecode_hashing;
EventEmitterInterface<BytecodeDecompositionEvent>::Container bytecode_decomposition;
EventEmitterInterface<AddressDerivationEvent>::Container address_derivation;
EventEmitterInterface<ClassIdDerivationEvent>::Container class_id_derivation;
EventEmitterInterface<SiloingEvent>::Container siloing;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include "barretenberg/vm2/common/field.hpp"

namespace bb::avm2::simulation {

enum class SiloingType { NULLIFIER };

struct SiloingEvent {
SiloingType type;
FF elem;
FF siloed_by;
FF siloed_elem;
};

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,29 @@ FF compute_contract_class_id(const FF& artifact_hash, const FF& private_fn_root,
{ GENERATOR_INDEX__CONTRACT_LEAF, artifact_hash, private_fn_root, public_bytecode_commitment });
}

FF compute_contract_address(const ContractInstance& contract_instance)
{
FF salted_initialization_hash = poseidon2::hash({ GENERATOR_INDEX__PARTIAL_ADDRESS,
contract_instance.salt,
contract_instance.initialisation_hash,
contract_instance.deployer_addr });
FF partial_address = poseidon2::hash(
{ GENERATOR_INDEX__PARTIAL_ADDRESS, contract_instance.contract_class_id, salted_initialization_hash });

std::vector<FF> public_keys_hash_fields = contract_instance.public_keys.to_fields();
std::vector<FF> public_key_hash_vec{ GENERATOR_INDEX__PUBLIC_KEYS_HASH };
for (size_t i = 0; i < public_keys_hash_fields.size(); i += 2) {
public_key_hash_vec.push_back(public_keys_hash_fields[i]);
public_key_hash_vec.push_back(public_keys_hash_fields[i + 1]);
// Is it guaranteed we wont get a point at infinity here?
public_key_hash_vec.push_back(FF::zero());
}
FF public_keys_hash = poseidon2::hash({ public_key_hash_vec });

FF h = poseidon2::hash({ GENERATOR_INDEX__CONTRACT_ADDRESS_V1, public_keys_hash, partial_address });
// This is safe since BN254_Fr < GRUMPKIN_Fr so we know there is no modulo reduction
grumpkin::fr h_fq = grumpkin::fr(h);
return (grumpkin::g1::affine_one * h_fq + contract_instance.public_keys.incoming_viewing_key).x;
}

} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include <cstdint>
#include <span>

#include "barretenberg/vm2/common/aztec_types.hpp"
#include "barretenberg/vm2/common/field.hpp"

namespace bb::avm2::simulation {

FF compute_public_bytecode_commitment(std::span<const uint8_t> bytecode);
FF compute_contract_class_id(const FF& artifact_hash, const FF& private_fn_root, const FF& public_bytecode_commitment);
FF compute_contract_address(const ContractInstance& contract_instance);

} // namespace bb::avm2::simulation
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/memory.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "barretenberg/vm2/simulation/memory.hpp"
#include "barretenberg/common/log.hpp"
#include "barretenberg/vm2/common/memory_types.hpp"

#include <cstdint>
#include <memory>
#include <unordered_map>

#include "barretenberg/common/log.hpp"
#include "barretenberg/vm2/common/memory_types.hpp"

namespace bb::avm2::simulation {

Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/memory.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <memory>
#include <unordered_map>

#include "barretenberg/vm2/common/map.hpp"
#include "barretenberg/vm2/common/memory_types.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"
#include "barretenberg/vm2/simulation/events/memory_event.hpp"
Expand Down Expand Up @@ -50,7 +50,7 @@ class Memory : public MemoryInterface {
};

uint32_t space_id;
std::unordered_map<size_t, ValueAndTag> memory;
unordered_flat_map<size_t, ValueAndTag> memory;
EventEmitterInterface<MemoryEvent>& events;
};

Expand Down
20 changes: 20 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/siloing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "barretenberg/vm2/simulation/siloing.hpp"

#include "barretenberg/crypto/poseidon2/poseidon2.hpp"
#include "barretenberg/vm/aztec_constants.hpp"
#include "barretenberg/vm2/simulation/events/siloing_event.hpp"

namespace bb::avm2::simulation {

using Poseidon2 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>;

FF Siloing::silo(const FF& generator, const FF& elem, const FF& silo_by, SiloingType type)
{
// TODO: Cache and deduplicate.
// TODO: Use poseidon gadget.
auto siloed_elem = Poseidon2::hash({ generator, silo_by, elem });
events.emit({ .type = type, .elem = elem, .siloed_by = silo_by, .siloed_elem = siloed_elem });
return siloed_elem;
}

} // namespace bb::avm2::simulation
33 changes: 33 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/siloing.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "barretenberg/vm/aztec_constants.hpp"
#include "barretenberg/vm2/common/field.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"
#include "barretenberg/vm2/simulation/events/siloing_event.hpp"

namespace bb::avm2::simulation {

class SiloingInterface {
public:
virtual ~SiloingInterface() = default;
virtual FF silo_nullifier(const FF& nullifier, const FF& silo_by) = 0;
};

class Siloing : public SiloingInterface {
public:
Siloing(EventEmitterInterface<SiloingEvent>& events)
: events(events)
{}

FF silo_nullifier(const FF& nullifier, const FF& silo_by) override
{
return silo(GENERATOR_INDEX__OUTER_NULLIFIER, nullifier, silo_by, SiloingType::NULLIFIER);
}

private:
FF silo(const FF& generator, const FF& elem, const FF& silo_by, SiloingType type);

EventEmitterInterface<SiloingEvent>& events;
};

} // namespace bb::avm2::simulation
Loading
Loading