Skip to content

Commit

Permalink
feat(avm): hinting merkle trees (#9658)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasRidhuan authored Nov 12, 2024
1 parent e305f48 commit 8d49e59
Show file tree
Hide file tree
Showing 27 changed files with 1,711 additions and 324 deletions.
150 changes: 148 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution_hints.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,104 @@ namespace bb::avm_trace {
using FF = AvmFlavorSettings::FF;
using AffinePoint = grumpkin::g1::affine_element;

struct PublicDataTreeLeafPreimage {
FF slot;
FF value;
FF next_index;
FF next_slot;
};

inline void read(uint8_t const*& it, PublicDataTreeLeafPreimage& hint)
{
using serialize::read;
read(it, hint.slot);
read(it, hint.value);
read(it, hint.next_index);
read(it, hint.next_slot);
}

struct PublicDataReadTreeHint {
PublicDataTreeLeafPreimage leaf_preimage;
FF leaf_index;
std::vector<FF> sibling_path;
};

inline void read(uint8_t const*& it, PublicDataReadTreeHint& hint)
{
using serialize::read;
read(it, hint.leaf_preimage);
read(it, hint.leaf_index);
read(it, hint.sibling_path);
}

struct PublicDataWriteTreeHint {
PublicDataReadTreeHint low_leaf_membership;
PublicDataTreeLeafPreimage new_leaf_preimage;
std::vector<FF> insertion_path;
};

inline void read(uint8_t const*& it, PublicDataWriteTreeHint& hint)
{
using serialize::read;
read(it, hint.low_leaf_membership);
read(it, hint.new_leaf_preimage);
read(it, hint.insertion_path);
}

struct NullifierLeafPreimage {
FF nullifier;
FF next_nullifier;
FF next_index;
};

inline void read(uint8_t const*& it, NullifierLeafPreimage& hint)
{
using serialize::read;
read(it, hint.nullifier);
read(it, hint.next_nullifier);
read(it, hint.next_index);
}

struct NullifierReadTreeHint {
NullifierLeafPreimage low_leaf_preimage;
FF low_leaf_index;
std::vector<FF> low_leaf_sibling_path;
};

inline void read(uint8_t const*& it, NullifierReadTreeHint& hint)
{
using serialize::read;
read(it, hint.low_leaf_preimage);
read(it, hint.low_leaf_index);
read(it, hint.low_leaf_sibling_path);
}

struct NullifierWriteTreeHint {
NullifierReadTreeHint low_leaf_membership;
std::vector<FF> insertion_path;
};

inline void read(uint8_t const*& it, NullifierWriteTreeHint& hint)
{
using serialize::read;
read(it, hint.low_leaf_membership);
read(it, hint.insertion_path);
}

struct AppendTreeHint {
FF leaf_index;
FF leaf_value;
std::vector<FF> sibling_path;
};

inline void read(uint8_t const*& it, AppendTreeHint& hint)
{
using serialize::read;
read(it, hint.leaf_index);
read(it, hint.leaf_value);
read(it, hint.sibling_path);
}

struct ExternalCallHint {
FF success{};
std::vector<FF> return_data;
Expand Down Expand Up @@ -128,6 +226,13 @@ struct ExecutionHints {
std::map<FF, ContractInstanceHint> contract_instance_hints;
// We could make this address-indexed
std::vector<AvmContractBytecode> all_contract_bytecode;
std::vector<PublicDataReadTreeHint> storage_read_hints;
std::vector<PublicDataWriteTreeHint> storage_write_hints;
std::vector<NullifierReadTreeHint> nullifier_read_hints;
std::vector<NullifierWriteTreeHint> nullifier_write_hints;
std::vector<AppendTreeHint> note_hash_read_hints;
std::vector<AppendTreeHint> note_hash_write_hints;
std::vector<AppendTreeHint> l1_to_l2_message_read_hints;

ExecutionHints() = default;

Expand Down Expand Up @@ -222,6 +327,27 @@ struct ExecutionHints {
std::vector<AvmContractBytecode> all_contract_bytecode;
read(it, all_contract_bytecode);

std::vector<PublicDataReadTreeHint> storage_read_hints;
read(it, storage_read_hints);

std::vector<PublicDataWriteTreeHint> storage_write_hints;
read(it, storage_write_hints);

std::vector<NullifierReadTreeHint> nullifier_read_hints;
read(it, nullifier_read_hints);

std::vector<NullifierWriteTreeHint> nullifier_write_hints;
read(it, nullifier_write_hints);

std::vector<AppendTreeHint> note_hash_read_hints;
read(it, note_hash_read_hints);

std::vector<AppendTreeHint> note_hash_write_hints;
read(it, note_hash_write_hints);

std::vector<AppendTreeHint> l1_to_l2_message_read_hints;
read(it, l1_to_l2_message_read_hints);

if (it != data.data() + data.size()) {
throw_or_abort("Failed to deserialize ExecutionHints: only read " + std::to_string(it - data.data()) +
" bytes out of " + std::to_string(data.size()) + " bytes");
Expand All @@ -230,7 +356,12 @@ struct ExecutionHints {
return { std::move(storage_value_hints), std::move(note_hash_exists_hints),
std::move(nullifier_exists_hints), std::move(l1_to_l2_message_exists_hints),
std::move(externalcall_hints), std::move(contract_instance_hints),
std::move(all_contract_bytecode) };
std::move(all_contract_bytecode), std::move(storage_read_hints),
std::move(storage_write_hints), std::move(nullifier_read_hints),
std::move(nullifier_write_hints), std::move(note_hash_read_hints),
std::move(note_hash_write_hints), std::move(l1_to_l2_message_read_hints)

};
}

private:
Expand All @@ -240,14 +371,29 @@ struct ExecutionHints {
std::vector<std::pair<FF, FF>> l1_to_l2_message_exists_hints,
std::vector<ExternalCallHint> externalcall_hints,
std::map<FF, ContractInstanceHint> contract_instance_hints,
std::vector<AvmContractBytecode> all_contract_bytecode)
std::vector<AvmContractBytecode> all_contract_bytecode,
std::vector<PublicDataReadTreeHint> storage_read_hints,
std::vector<PublicDataWriteTreeHint> storage_write_hints,
std::vector<NullifierReadTreeHint> nullifier_read_hints,
std::vector<NullifierWriteTreeHint> nullifier_write_hints,
std::vector<AppendTreeHint> note_hash_read_hints,
std::vector<AppendTreeHint> note_hash_write_hints,
std::vector<AppendTreeHint> l1_to_l2_message_read_hints)

: storage_value_hints(std::move(storage_value_hints))
, note_hash_exists_hints(std::move(note_hash_exists_hints))
, nullifier_exists_hints(std::move(nullifier_exists_hints))
, l1_to_l2_message_exists_hints(std::move(l1_to_l2_message_exists_hints))
, externalcall_hints(std::move(externalcall_hints))
, contract_instance_hints(std::move(contract_instance_hints))
, all_contract_bytecode(std::move(all_contract_bytecode))
, storage_read_hints(std::move(storage_read_hints))
, storage_write_hints(std::move(storage_write_hints))
, nullifier_read_hints(std::move(nullifier_read_hints))
, nullifier_write_hints(std::move(nullifier_write_hints))
, note_hash_read_hints(std::move(note_hash_read_hints))
, note_hash_write_hints(std::move(note_hash_write_hints))
, l1_to_l2_message_read_hints(std::move(l1_to_l2_message_read_hints))
{}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,9 @@ contract AvmTest {
let _ = l1_to_l2_msg_exists(1, 2);
dep::aztec::oracle::debug_log::debug_log("send_l2_to_l1_msg");
let _ = send_l2_to_l1_msg(EthAddress::from_field(0x2020), 1);
dep::aztec::oracle::debug_log::debug_log("nested_call_to_add");
let _ = nested_call_to_add(1, 2);
dep::aztec::oracle::debug_log::debug_log("nested_static_call_to_add");
let _ = nested_static_call_to_add(1, 2);
//dep::aztec::oracle::debug_log::debug_log("nested_call_to_add");
//let _ = nested_call_to_add(1, 2);
//dep::aztec::oracle::debug_log::debug_log("nested_static_call_to_add");
//let _ = nested_static_call_to_add(1, 2);
}
}
2 changes: 2 additions & 0 deletions yarn-project/bb-prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@aztec/noir-protocol-circuits-types": "workspace:^",
"@aztec/simulator": "workspace:^",
"@aztec/telemetry-client": "workspace:^",
"@aztec/world-state": "workspace:^",
"@msgpack/msgpack": "^3.0.0-beta2",
"@noir-lang/noirc_abi": "portal:../../noir/packages/noirc_abi",
"@noir-lang/types": "portal:../../noir/packages/types",
Expand All @@ -80,6 +81,7 @@
},
"devDependencies": {
"@aztec/ethereum": "workspace:^",
"@aztec/kv-store": "workspace:^",
"@aztec/types": "workspace:^",
"@jest/globals": "^29.5.0",
"@types/jest": "^29.5.0",
Expand Down
8 changes: 7 additions & 1 deletion yarn-project/bb-prover/src/avm_proving.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { makeContractClassPublic, makeContractInstanceFromClassId } from '@aztec
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr, Point } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { openTmpStore } from '@aztec/kv-store/utils';
import { AvmSimulator, PublicSideEffectTrace, type WorldStateDB } from '@aztec/simulator';
import {
getAvmTestContractBytecode,
Expand All @@ -21,6 +22,8 @@ import {
initPersistableStateManager,
resolveAvmTestContractAssertionMessage,
} from '@aztec/simulator/avm/fixtures';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
import { MerkleTrees } from '@aztec/world-state';

import { mock } from 'jest-mock-extended';
import fs from 'node:fs/promises';
Expand Down Expand Up @@ -103,7 +106,10 @@ const proveAndVerifyAvmTestContract = async (
worldStateDB.storageRead.mockResolvedValue(Promise.resolve(storageValue));

const trace = new PublicSideEffectTrace(startSideEffectCounter);
const persistableState = initPersistableStateManager({ worldStateDB, trace });
const telemetry = new NoopTelemetryClient();
const merkleTrees = await (await MerkleTrees.new(openTmpStore(), telemetry)).fork();
worldStateDB.getMerkleInterface.mockReturnValue(merkleTrees);
const persistableState = initPersistableStateManager({ worldStateDB, trace, merkleTrees, doMerkleOperations: true });
const environment = initExecutionEnvironment({
functionSelector,
calldata,
Expand Down
10 changes: 9 additions & 1 deletion yarn-project/bb-prover/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@
{
"path": "../telemetry-client"
},
{
"path": "../world-state"
},
{
"path": "../ethereum"
},
{
"path": "../kv-store"
},
{
"path": "../types"
}
],
"include": ["src"]
"include": [
"src"
]
}
Loading

0 comments on commit 8d49e59

Please sign in to comment.