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
9 changes: 9 additions & 0 deletions barretenberg/cpp/pil/avm/constants_gen.pil
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ namespace constants;
pol START_EMIT_NULLIFIER_WRITE_OFFSET = 207;
pol START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET = 223;
pol START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET = 225;
pol GENERATOR_INDEX__NOTE_HASH_NONCE = 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not used but part of constants gen right now

pol GENERATOR_INDEX__UNIQUE_NOTE_HASH = 3;
pol GENERATOR_INDEX__SILOED_NOTE_HASH = 4;
pol GENERATOR_INDEX__OUTER_NULLIFIER = 7;
pol GENERATOR_INDEX__CONTRACT_ADDRESS_V1 = 15;
pol GENERATOR_INDEX__CONTRACT_LEAF = 16;
pol GENERATOR_INDEX__PUBLIC_LEAF_INDEX = 23;
pol GENERATOR_INDEX__PARTIAL_ADDRESS = 27;
pol GENERATOR_INDEX__PUBLIC_KEYS_HASH = 52;

24 changes: 12 additions & 12 deletions barretenberg/cpp/pil/vm2/bc_retrieval.pil
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ pol commit public_bytecode_commitment;
// Observe the folowing also connects the class_id of the instance to the class members.
// TODO: Probably some latch is also needed.
// TODO: Selector needs to be guarded by error flag.
// #[LOOKUP_CLASS_ID_DERIVATION]
// sel {
// class_id,
// artifact_hash,
// private_function_root,
// public_bytecode_commitment
// } in class_id_derivation.sel {
// class_id_derivation.class_id,
// class_id_derivation.artifact_hash,
// class_id_derivation.private_function_root,
// class_id_derivation.public_bytecode_commitment
// }
#[CLASS_ID_DERIVATION]
sel {
class_id,
artifact_hash,
private_function_root,
public_bytecode_commitment
} in class_id_derivation.sel {
class_id_derivation.class_id,
class_id_derivation.artifact_hash,
class_id_derivation.private_function_root,
class_id_derivation.public_bytecode_commitment
};

// TODO: Gate by error flag.
#[BYTECODE_HASH_IS_CORRECT]
Expand Down
34 changes: 34 additions & 0 deletions barretenberg/cpp/pil/vm2/class_id_derivation.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include "constants_gen.pil";
include "poseidon2_hash.pil";
include "precomputed.pil";
include "bc_retrieval.pil";

namespace class_id_derivation;

pol commit sel;
sel * (1 - sel) = 0;

#[skippable_if]
sel = 0;

// Members of Contract Class Id, looked up with bc_retrieval
pol commit artifact_hash;
pol commit private_function_root;
pol commit public_bytecode_commitment; // This is constrained via bc_retrieval's lookup to bc_hashing
// The result of
// H(GENERATOR_INDEX__CONTRACT_LEAF, artifact_hash, private_function_root, public_bytecode_commitment)
pol commit class_id;

// TODO: We need this temporarily while we dont allow for aliases in the lookup tuple, there must be a better way
pol commit temp_constant_for_lookup;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a todo comment about the constants?

sel * (temp_constant_for_lookup - constants.GENERATOR_INDEX__CONTRACT_LEAF) = 0;
// Since the inputs to poseidon2 have to be chunks of 3, we need two lookups if we want to do this in a single row
#[CLASS_ID_POSEIDON2_0]
sel { temp_constant_for_lookup, artifact_hash, private_function_root, class_id }
in poseidon2_hash.start { poseidon2_hash.input_0, poseidon2_hash.input_1, poseidon2_hash.input_2, poseidon2_hash.output };

#[CLASS_ID_POSEIDON2_1]
sel { public_bytecode_commitment, precomputed.zero, precomputed.zero, class_id}
in poseidon2_hash.end { poseidon2_hash.input_0, poseidon2_hash.input_1, poseidon2_hash.input_2, poseidon2_hash.output };


9 changes: 9 additions & 0 deletions barretenberg/cpp/pil/vm2/constants_gen.pil
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ namespace constants;
pol START_EMIT_NULLIFIER_WRITE_OFFSET = 207;
pol START_EMIT_L2_TO_L1_MSG_WRITE_OFFSET = 223;
pol START_EMIT_UNENCRYPTED_LOG_WRITE_OFFSET = 225;
pol GENERATOR_INDEX__NOTE_HASH_NONCE = 2;
pol GENERATOR_INDEX__UNIQUE_NOTE_HASH = 3;
pol GENERATOR_INDEX__SILOED_NOTE_HASH = 4;
pol GENERATOR_INDEX__OUTER_NULLIFIER = 7;
pol GENERATOR_INDEX__CONTRACT_ADDRESS_V1 = 15;
pol GENERATOR_INDEX__CONTRACT_LEAF = 16;
pol GENERATOR_INDEX__PUBLIC_LEAF_INDEX = 23;
pol GENERATOR_INDEX__PARTIAL_ADDRESS = 27;
pol GENERATOR_INDEX__PUBLIC_KEYS_HASH = 52;

1 change: 1 addition & 0 deletions barretenberg/cpp/pil/vm2/execution.pil
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include "bc_decomposition.pil";
include "bc_hashing.pil";
include "bc_retrieval.pil";
include "instr_fetching.pil";
include "class_id_derivation.pil";
include "range_check.pil";
include "bitwise.pil";
include "precomputed.pil";
Expand Down
11 changes: 5 additions & 6 deletions barretenberg/cpp/pil/vm2/poseidon2_hash.pil
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,20 @@ namespace poseidon2_hash;
// The permutation output values are represented by b_0, b_1, b_2, b_3
// This most definitely could be simplified to a lower degree check
// The next perm input is constrained to be the previous perm output + the new values to be hashed if we
// are not at the start or the end of hashing
pol NEXT_INPUT_IS_PREV_OUTPUT_SEL = (1 - LATCH_CONDITION) * (1 - start);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was a bug 😨 . It was too relaxed, it's changed to only be the (1 - LATCH_CONDITION)

// are not at the end of hashing
pol commit a_0;
pol commit a_1;
pol commit a_2;
pol commit a_3;

sel * start * (a_0 - input_0) = 0;
sel * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_0' - b_0 - input_0') = 0;
sel * (1 - LATCH_CONDITION) * (a_0' - b_0 - input_0') = 0;
sel * start * (a_1 - input_1) = 0;
sel * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_1' - b_1 - input_1') = 0;
sel * (1 - LATCH_CONDITION) * (a_1' - b_1 - input_1') = 0;
sel * start * (a_2 - input_2) = 0;
sel * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_2' - b_2 - input_2') = 0;
sel * (1 - LATCH_CONDITION) * (a_2' - b_2 - input_2') = 0;
sel * start * (a_3 - IV) = 0; // IV is placed in the last slot if this is the start
sel * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_3' - b_3) = 0;
sel * (1 - LATCH_CONDITION) * (a_3' - b_3) = 0;

// The Hash output value represented by b_0
pol commit b_0;
Expand Down
5 changes: 4 additions & 1 deletion barretenberg/cpp/pil/vm2/precomputed.pil
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace precomputed;
// From 0 and incrementing up to the size of the circuit (2^21).
pol constant clk;

// A column of zeroes
pol constant zero;

// 1 only at row 0.
pol constant first_row;

Expand Down Expand Up @@ -76,4 +79,4 @@ pol constant exec_opcode;
// Toggle the rows which index (clk) is equal to a wire opcode
// Is used to lookup into the wire instruction spec table which contains the operand decomposition
// selectors as well as exec_opcode
pol constant sel_range_wire_opcode;
pol constant sel_range_wire_opcode;
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include "barretenberg/vm2/constraining/testing/check_relation.hpp"
#include "barretenberg/vm2/generated/columns.hpp"
#include "barretenberg/vm2/generated/flavor_settings.hpp"
#include "barretenberg/vm2/generated/relations/class_id_derivation.hpp"
#include "barretenberg/vm2/generated/relations/lookups_bc_retrieval.hpp"
#include "barretenberg/vm2/generated/relations/lookups_class_id_derivation.hpp"
#include "barretenberg/vm2/simulation/class_id_derivation.hpp"
#include "barretenberg/vm2/simulation/events/event_emitter.hpp"
#include "barretenberg/vm2/simulation/lib/contract_crypto.hpp"
#include "barretenberg/vm2/testing/fixtures.hpp"
#include "barretenberg/vm2/tracegen/bytecode_trace.hpp"
#include "barretenberg/vm2/tracegen/class_id_derivation_trace.hpp"
#include "barretenberg/vm2/tracegen/lib/lookup_builder.hpp"
#include "barretenberg/vm2/tracegen/poseidon2_trace.hpp"
#include "barretenberg/vm2/tracegen/test_trace_container.hpp"

namespace bb::avm2::constraining {
namespace {

using tracegen::BytecodeTraceBuilder;
using tracegen::ClassIdDerivationTraceBuilder;
using tracegen::LookupIntoDynamicTableSequential;
using tracegen::Poseidon2TraceBuilder;
using tracegen::TestTraceContainer;

using simulation::ClassIdDerivation;
using simulation::ClassIdDerivationEvent;
using simulation::compute_contract_class_id;
using simulation::EventEmitter;
using simulation::NoopEventEmitter;
using simulation::Poseidon2;
using simulation::Poseidon2HashEvent;
using simulation::Poseidon2PermutationEvent;

using FF = AvmFlavorSettings::FF;
using C = Column;
using class_id_derivation_relation = bb::avm2::class_id_derivation<FF>;
using poseidon2_relation = bb::avm2::poseidon2_hash<FF>;

using lookup_poseidon2_hash_0 = bb::avm2::lookup_class_id_derivation_class_id_poseidon2_0_relation<FF>;
using lookup_poseidon2_hash_1 = bb::avm2::lookup_class_id_derivation_class_id_poseidon2_1_relation<FF>;
using lookup_bc_retrieval = bb::avm2::lookup_bc_retrieval_class_id_derivation_relation<FF>;

ContractClass generate_contract_class()
{
return ContractClass{ .artifact_hash = FF::random_element(),
.private_function_root = FF::random_element(),
.public_bytecode_commitment = FF::random_element(),
.packed_bytecode = {} };
}

TEST(ClassIdDerivationConstrainingTest, EmptyRow)
{
TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
{ { C::precomputed_clk, 0 } },
});

check_relation<class_id_derivation_relation>(trace);
}

TEST(ClassIdDerivationConstrainingTest, Basic)
{
TestTraceContainer trace;
ClassIdDerivationTraceBuilder builder;

auto klass = generate_contract_class();

FF class_id =
compute_contract_class_id(klass.artifact_hash, klass.private_function_root, klass.public_bytecode_commitment);

builder.process({ { .class_id = class_id, .klass = klass } }, trace);

check_relation<class_id_derivation_relation>(trace);
}

TEST(ClassIdDerivationConstrainingTest, WithHashInteraction)
{
EventEmitter<Poseidon2HashEvent> hash_event_emitter;
EventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
Poseidon2 poseidon2(hash_event_emitter, perm_event_emitter);

EventEmitter<ClassIdDerivationEvent> event_emitter;
ClassIdDerivation class_id_derivation(poseidon2, event_emitter);

auto klass = generate_contract_class();
FF class_id =
compute_contract_class_id(klass.artifact_hash, klass.private_function_root, klass.public_bytecode_commitment);

TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
});

ClassIdDerivationTraceBuilder builder;
Poseidon2TraceBuilder poseidon2_builder;

class_id_derivation.assert_derivation(class_id, klass);

poseidon2_builder.process_hash(hash_event_emitter.dump_events(), trace);
builder.process({ { .class_id = class_id, .klass = klass } }, trace);

LookupIntoDynamicTableSequential<lookup_poseidon2_hash_0::Settings>().process(trace);
LookupIntoDynamicTableSequential<lookup_poseidon2_hash_1::Settings>().process(trace);

check_interaction<lookup_poseidon2_hash_0>(trace);
check_interaction<lookup_poseidon2_hash_1>(trace);
}

// TODO: This should probably be refined and moved to bc_retrieval test file once that exists
TEST(ClassIdDerivationConstrainingTest, WithRetrievalInteraction)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When bc_retrieval is being properly done, we should move the test to there

{
NoopEventEmitter<Poseidon2HashEvent> hash_event_emitter;
NoopEventEmitter<Poseidon2PermutationEvent> perm_event_emitter;
Poseidon2 poseidon2(hash_event_emitter, perm_event_emitter);

EventEmitter<ClassIdDerivationEvent> event_emitter;
ClassIdDerivation class_id_derivation(poseidon2, event_emitter);

auto klass = generate_contract_class();
FF class_id =
compute_contract_class_id(klass.artifact_hash, klass.private_function_root, klass.public_bytecode_commitment);

TestTraceContainer trace({
{ { C::precomputed_first_row, 1 } },
});

ClassIdDerivationTraceBuilder builder;
BytecodeTraceBuilder bc_trace_builder;

class_id_derivation.assert_derivation(class_id, klass);
builder.process({ { .class_id = class_id, .klass = klass } }, trace);

ContractInstance instance = {};
instance.contract_class_id = class_id;
bc_trace_builder.process_retrieval({ { .bytecode_id = 0,
.address = 1,
.siloed_address = 2,
.contract_instance = instance,
.contract_class = klass,
.nullifier_root = 3 } },
trace);

LookupIntoDynamicTableSequential<lookup_bc_retrieval::Settings>().process(trace);
check_interaction<lookup_bc_retrieval>(trace);
}

} // namespace
} // namespace bb::avm2::constraining
10 changes: 5 additions & 5 deletions barretenberg/cpp/src/barretenberg/vm2/generated/columns.hpp

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm2/generated/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "relations/bc_hashing.hpp"
#include "relations/bc_retrieval.hpp"
#include "relations/bitwise.hpp"
#include "relations/class_id_derivation.hpp"
#include "relations/ecc.hpp"
#include "relations/execution.hpp"
#include "relations/instr_fetching.hpp"
Expand All @@ -36,6 +37,7 @@
#include "relations/lookups_bc_hashing.hpp"
#include "relations/lookups_bc_retrieval.hpp"
#include "relations/lookups_bitwise.hpp"
#include "relations/lookups_class_id_derivation.hpp"
#include "relations/lookups_instr_fetching.hpp"
#include "relations/lookups_poseidon2_hash.hpp"
#include "relations/lookups_range_check.hpp"
Expand Down Expand Up @@ -81,13 +83,13 @@ class AvmFlavor {
// This flavor would not be used with ZK Sumcheck
static constexpr bool HasZK = false;

static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 36;
static constexpr size_t NUM_WITNESS_ENTITIES = 737;
static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 37;
static constexpr size_t NUM_WITNESS_ENTITIES = 749;
static constexpr size_t NUM_SHIFTED_ENTITIES = 89;
static constexpr size_t NUM_WIRES = NUM_WITNESS_ENTITIES + NUM_PRECOMPUTED_ENTITIES;
// We have two copies of the witness entities, so we subtract the number of fixed ones (they have no shift), one for
// the unshifted and one for the shifted
static constexpr size_t NUM_ALL_ENTITIES = 862;
static constexpr size_t NUM_ALL_ENTITIES = 875;

// Need to be templated for recursive verifier
template <typename FF_>
Expand All @@ -98,6 +100,7 @@ class AvmFlavor {
avm2::bc_hashing<FF_>,
avm2::bc_retrieval<FF_>,
avm2::bitwise<FF_>,
avm2::class_id_derivation<FF_>,
avm2::ecc<FF_>,
avm2::execution<FF_>,
avm2::instr_fetching<FF_>,
Expand All @@ -119,8 +122,11 @@ class AvmFlavor {
lookup_bc_hashing_iv_is_len_relation<FF_>,
lookup_bc_hashing_poseidon2_hash_relation<FF_>,
lookup_bc_retrieval_bytecode_hash_is_correct_relation<FF_>,
lookup_bc_retrieval_class_id_derivation_relation<FF_>,
lookup_bitwise_byte_operations_relation<FF_>,
lookup_bitwise_integral_tag_length_relation<FF_>,
lookup_class_id_derivation_class_id_poseidon2_0_relation<FF_>,
lookup_class_id_derivation_class_id_poseidon2_1_relation<FF_>,
lookup_instr_fetching_bytes_from_bc_dec_relation<FF_>,
lookup_instr_fetching_wire_instruction_info_relation<FF_>,
lookup_poseidon2_hash_poseidon2_perm_relation<FF_>,
Expand Down Expand Up @@ -417,6 +423,7 @@ class AvmFlavor {
this->precomputed_sel_unary = verification_key->precomputed_sel_unary;
this->precomputed_sha256_compression_round_constant =
verification_key->precomputed_sha256_compression_round_constant;
this->precomputed_zero = verification_key->precomputed_zero;
}
};

Expand Down
Loading
Loading