From 4d149be20e73321fece072a1b7e410225b5dc8c9 Mon Sep 17 00:00:00 2001 From: Facundo Date: Tue, 21 Jan 2025 10:37:49 +0000 Subject: [PATCH] feat(avm): include initial tree roots in DB (#11360) We'll need the roots for the context and other stuff. I expect that `get_tree_roots()` will not lay constraints. I expect that the roots will be advanced via hints in, e.g, `emit_nullifier` (root before, root after). --- .../barretenberg/vm2/common/avm_inputs.hpp | 14 +++++++++++++- .../vm2/common/avm_inputs.test.cpp | 7 +++++++ .../vm2/common/avm_inputs.testdata.bin | Bin 2219 -> 2435 bytes .../vm2/simulation/bytecode_manager.cpp | 3 ++- .../vm2/simulation/events/bytecode_events.hpp | 1 + .../vm2/simulation/lib/raw_data_db.cpp | 1 + .../vm2/simulation/lib/raw_data_db.hpp | 3 +++ .../circuits.js/src/structs/avm/avm.test.ts | 8 +++++++- .../circuits.js/src/structs/avm/avm.ts | 6 ++++++ .../foundation/src/testing/files/index.ts | 4 ++-- 10 files changed, 42 insertions(+), 5 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp index a19ddd57d83..b806ccb82b1 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.hpp @@ -50,13 +50,25 @@ struct ContractClassHint { MSGPACK_FIELDS(artifactHash, privateFunctionsRoot, publicBytecodeCommitment, packedBytecode); }; +struct TreeRoots { + FF publicDataTree; + FF nullifierTree; + FF noteHashTree; + FF l1ToL2MessageTree; + + bool operator==(const TreeRoots& other) const = default; + + MSGPACK_FIELDS(publicDataTree, nullifierTree, noteHashTree, l1ToL2MessageTree); +}; + struct ExecutionHints { std::vector contractInstances; std::vector contractClasses; + TreeRoots initialTreeRoots; bool operator==(const ExecutionHints& other) const = default; - MSGPACK_FIELDS(contractInstances, contractClasses); + MSGPACK_FIELDS(contractInstances, contractClasses, initialTreeRoots); }; struct PublicExecutionRequest { diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.test.cpp b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.test.cpp index 46911c0d683..c76cdece1f8 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.test.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.test.cpp @@ -106,6 +106,12 @@ TEST(AvmInputsTest, Deserialization) .packedBytecode = string_to_buffer("secondbuffer"), }, }, + .initialTreeRoots = { + .publicDataTree = FF(0x1), + .nullifierTree = FF(0x2), + .noteHashTree = FF(0x3), + .l1ToL2MessageTree = FF(0x4), + }, }, }; @@ -115,6 +121,7 @@ TEST(AvmInputsTest, Deserialization) EXPECT_EQ(inputs.publicInputs, expected.publicInputs); EXPECT_EQ(inputs.hints.contractClasses, expected.hints.contractClasses); EXPECT_EQ(inputs.hints.contractInstances, expected.hints.contractInstances); + EXPECT_EQ(inputs.hints.initialTreeRoots, expected.hints.initialTreeRoots); EXPECT_EQ(inputs, expected); // Catch all. } diff --git a/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin b/barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin index 18ed5069e324fed4cd80a06788c0a9c59d870c11..703290cccd6b24ffcc6b98be4e9024a75e6a174f 100644 GIT binary patch delta 139 zcmZ22*euL*kAZn delta 26 icmZn`UM>E diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.cpp index 6a17a809fe6..2126b7e8b90 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.cpp @@ -35,7 +35,8 @@ BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address) .address = address, .siloed_address = address, // FIXME: compute, check. .contract_instance = instance, - .contract_class = klass // WARNING: this class has the whole bytecode. + .contract_class = klass, // WARNING: this class has the whole bytecode. + .nullifier_root = db.get_tree_roots().nullifierTree, }); return bytecode_id; diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/events/bytecode_events.hpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/events/bytecode_events.hpp index b6be18268f6..c54441ccee4 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/events/bytecode_events.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/events/bytecode_events.hpp @@ -27,6 +27,7 @@ struct BytecodeRetrievalEvent { AztecAddress siloed_address; ContractInstance contract_instance; ContractClass contract_class; + FF nullifier_root; bool error = false; }; diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.cpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.cpp index bc33faa8367..7a739d8e185 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.cpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.cpp @@ -8,6 +8,7 @@ namespace bb::avm2::simulation { HintedRawDataDB::HintedRawDataDB(const ExecutionHints& hints) : contract_instances(hints.contractInstances) , contract_classes(hints.contractClasses) + , tree_roots(hints.initialTreeRoots) {} ContractInstance HintedRawDataDB::get_contract_instance(const AztecAddress& address) const diff --git a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.hpp b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.hpp index 98d810bc3ca..78835520458 100644 --- a/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.hpp +++ b/barretenberg/cpp/src/barretenberg/vm2/simulation/lib/raw_data_db.hpp @@ -12,6 +12,7 @@ class RawDataDBInterface { virtual ContractInstance get_contract_instance(const AztecAddress& address) const = 0; virtual ContractClass get_contract_class(const ContractClassId& class_id) const = 0; + virtual const TreeRoots& get_tree_roots() const = 0; }; class HintedRawDataDB : public RawDataDBInterface { @@ -20,10 +21,12 @@ class HintedRawDataDB : public RawDataDBInterface { ContractInstance get_contract_instance(const AztecAddress& address) const override; ContractClass get_contract_class(const ContractClassId& class_id) const override; + const TreeRoots& get_tree_roots() const override { return tree_roots; } private: std::vector contract_instances; std::vector contract_classes; + TreeRoots tree_roots; mutable size_t contract_instances_idx = 0; mutable size_t contract_classes_idx = 0; }; diff --git a/yarn-project/circuits.js/src/structs/avm/avm.test.ts b/yarn-project/circuits.js/src/structs/avm/avm.test.ts index e9fbe35e0d6..176aefb4c11 100644 --- a/yarn-project/circuits.js/src/structs/avm/avm.test.ts +++ b/yarn-project/circuits.js/src/structs/avm/avm.test.ts @@ -93,6 +93,12 @@ describe('Avm circuit inputs', () => { packedBytecode: Buffer.from('secondbuffer'), }, ], + initialTreeRoots: { + publicDataTree: new Fr(1), + nullifierTree: new Fr(2), + noteHashTree: new Fr(3), + l1ToL2MessageTree: new Fr(4), + }, }; const enqueuedCalls = [ @@ -125,7 +131,7 @@ describe('Avm circuit inputs', () => { // Run with AZTEC_GENERATE_TEST_DATA=1 to update test data const path = 'barretenberg/cpp/src/barretenberg/vm2/common/avm_inputs.testdata.bin'; - writeTestData(path, buffer); + writeTestData(path, buffer, /*raw=*/ true); const expected = readTestData(path); expect(buffer).toEqual(expected); diff --git a/yarn-project/circuits.js/src/structs/avm/avm.ts b/yarn-project/circuits.js/src/structs/avm/avm.ts index dda6d42cafa..868f587e78b 100644 --- a/yarn-project/circuits.js/src/structs/avm/avm.ts +++ b/yarn-project/circuits.js/src/structs/avm/avm.ts @@ -955,6 +955,12 @@ export class AvmCircuitInputs { const hints = { contractInstances: [] as any[], contractClasses: [] as any[], + initialTreeRoots: { + publicDataTree: this.output.startTreeSnapshots.publicDataTree.root, + nullifierTree: this.output.startTreeSnapshots.nullifierTree.root, + noteHashTree: this.output.startTreeSnapshots.noteHashTree.root, + l1ToL2MessageTree: this.output.startTreeSnapshots.l1ToL2MessageTree.root, + }, }; const inputs = { hints: hints, diff --git a/yarn-project/foundation/src/testing/files/index.ts b/yarn-project/foundation/src/testing/files/index.ts index 2267a5702ea..02e0377440e 100644 --- a/yarn-project/foundation/src/testing/files/index.ts +++ b/yarn-project/foundation/src/testing/files/index.ts @@ -6,12 +6,12 @@ import { fileURLToPath } from '../../url/index.js'; import { isGenerateTestDataEnabled } from '../test_data.js'; /** Writes the contents specified to the target file if test data generation is enabled. */ -export function writeTestData(targetFileFromRepoRoot: string, contents: string | Buffer) { +export function writeTestData(targetFileFromRepoRoot: string, contents: string | Buffer, raw: boolean = false) { if (!isGenerateTestDataEnabled()) { return; } const targetFile = getPathToFile(targetFileFromRepoRoot); - const toWrite = typeof contents === 'string' ? contents : contents.toString('hex'); + const toWrite = raw ? contents : typeof contents === 'string' ? contents : contents.toString('hex'); writeFileSync(targetFile, toWrite); const logger = createConsoleLogger('aztec:testing:test_data'); logger(`Wrote test data to ${targetFile}`);