From 45b61e6f54c98e73879740d5d8f6fb26f56a181d Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 6 Oct 2023 13:04:32 +0000 Subject: [PATCH] WIP --- circuits/cpp/src/aztec3/circuits/hash.hpp | 4 +-- .../acir-simulator/src/public/execution.ts | 4 +-- .../aztec-node/src/aztec-node/server.ts | 6 ++-- yarn-project/circuits.js/src/abis/abis.ts | 2 +- .../circuits.js/src/cbind/circuits.gen.ts | 2 +- .../src/block_builder/solo_block_builder.ts | 11 ++++-- .../src/simulator/public_executor.ts | 9 ++--- yarn-project/world-state/src/index.ts | 1 - yarn-project/world-state/src/utils.ts | 35 ------------------- .../src/world-state-db/merkle_trees.ts | 9 +++-- 10 files changed, 26 insertions(+), 57 deletions(-) delete mode 100644 yarn-project/world-state/src/utils.ts diff --git a/circuits/cpp/src/aztec3/circuits/hash.hpp b/circuits/cpp/src/aztec3/circuits/hash.hpp index e29810bce0f..bf23853d1d7 100644 --- a/circuits/cpp/src/aztec3/circuits/hash.hpp +++ b/circuits/cpp/src/aztec3/circuits/hash.hpp @@ -381,10 +381,10 @@ template typename NCT::fr compute_public_data_tree_value(typename * @param storage_slot The storage slot to which the inserted element belongs * @return The index for insertion into the public data tree */ -template typename NCT::fr compute_public_data_tree_index(typename NCT::fr const& contract_address, +template typename NCT::fr compute_public_data_tree_index(typename NCT::address const& contract_address, typename NCT::fr const& storage_slot) { - return NCT::compress({ contract_address, storage_slot }, GeneratorIndex::PUBLIC_LEAF_INDEX); + return NCT::compress({ contract_address.to_field(), storage_slot }, GeneratorIndex::PUBLIC_LEAF_INDEX); } template typename NCT::fr compute_l2_to_l1_hash(typename NCT::address const& contract_address, diff --git a/yarn-project/acir-simulator/src/public/execution.ts b/yarn-project/acir-simulator/src/public/execution.ts index e26dba8831d..e5a8bf43aea 100644 --- a/yarn-project/acir-simulator/src/public/execution.ts +++ b/yarn-project/acir-simulator/src/public/execution.ts @@ -122,7 +122,7 @@ function contractStorageReadToPublicDataRead( contractAddress: AztecAddress, ): PublicDataRead { return new PublicDataRead( - computePublicDataTreeIndex(wasm, contractAddress.toField(), read.storageSlot), + computePublicDataTreeIndex(wasm, contractAddress, read.storageSlot), computePublicDataTreeValue(wasm, read.currentValue), read.sideEffectCounter!, ); @@ -141,7 +141,7 @@ function contractStorageUpdateRequestToPublicDataUpdateRequest( contractAddress: AztecAddress, ): PublicDataUpdateRequest { return new PublicDataUpdateRequest( - computePublicDataTreeIndex(wasm, contractAddress.toField(), update.storageSlot), + computePublicDataTreeIndex(wasm, contractAddress, update.storageSlot), computePublicDataTreeValue(wasm, update.oldValue), computePublicDataTreeValue(wasm, update.newValue), update.sideEffectCounter!, diff --git a/yarn-project/aztec-node/src/aztec-node/server.ts b/yarn-project/aztec-node/src/aztec-node/server.ts index 9b7c610fda7..94600c453b8 100644 --- a/yarn-project/aztec-node/src/aztec-node/server.ts +++ b/yarn-project/aztec-node/src/aztec-node/server.ts @@ -8,6 +8,7 @@ import { L1_TO_L2_MSG_TREE_HEIGHT, PRIVATE_DATA_TREE_HEIGHT, } from '@aztec/circuits.js'; +import { computePublicDataTreeIndex } from '@aztec/circuits.js/abis'; import { L1ContractAddresses } from '@aztec/ethereum'; import { AztecAddress } from '@aztec/foundation/aztec-address'; import { createDebugLogger } from '@aztec/foundation/log'; @@ -41,7 +42,6 @@ import { ServerWorldStateSynchronizer, WorldStateConfig, WorldStateSynchronizer, - computePublicDataTreeLeafIndex, getConfigEnvVars as getWorldStateConfig, } from '@aztec/world-state'; @@ -321,8 +321,8 @@ export class AztecNodeService implements AztecNode { */ public async getPublicStorageAt(contract: AztecAddress, slot: bigint): Promise { const committedDb = await this.#getWorldState(); - const leafIndex = computePublicDataTreeLeafIndex(contract, new Fr(slot), await CircuitsWasm.get()); - return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex); + const leafIndex = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, new Fr(slot)); + return committedDb.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, leafIndex.value); } /** diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index 7f4a94ef46b..7226848c5b5 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -371,7 +371,7 @@ export function computePublicDataTreeValue(wasm: IWasmModule, value: Fr): Fr { * @returns Public data tree index computed from contract address and storage slot. */ -export function computePublicDataTreeIndex(wasm: IWasmModule, contractAddress: Fr, storageSlot: Fr): Fr { +export function computePublicDataTreeIndex(wasm: IWasmModule, contractAddress: AztecAddress, storageSlot: Fr): Fr { wasm.call('pedersen__init'); return abisComputePublicDataTreeIndex(wasm, contractAddress, storageSlot); } diff --git a/yarn-project/circuits.js/src/cbind/circuits.gen.ts b/yarn-project/circuits.js/src/cbind/circuits.gen.ts index d89e191b586..abeec9a7c91 100644 --- a/yarn-project/circuits.js/src/cbind/circuits.gen.ts +++ b/yarn-project/circuits.js/src/cbind/circuits.gen.ts @@ -3208,7 +3208,7 @@ export function abisComputeGlobalsHash(wasm: IWasmModule, arg0: GlobalVariables) export function abisComputePublicDataTreeValue(wasm: IWasmModule, arg0: Fr): Fr { return Fr.fromBuffer(callCbind(wasm, 'abis__compute_public_data_tree_value', [toBuffer(arg0)])); } -export function abisComputePublicDataTreeIndex(wasm: IWasmModule, arg0: Fr, arg1: Fr): Fr { +export function abisComputePublicDataTreeIndex(wasm: IWasmModule, arg0: Address, arg1: Fr): Fr { return Fr.fromBuffer(callCbind(wasm, 'abis__compute_public_data_tree_index', [toBuffer(arg0), toBuffer(arg1)])); } export function privateKernelDummyPreviousKernel(wasm: IWasmModule): PreviousKernelData { diff --git a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts index 0435ef2eea4..0841fcb917e 100644 --- a/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts +++ b/yarn-project/sequencer-client/src/block_builder/solo_block_builder.ts @@ -36,7 +36,12 @@ import { VerificationKey, makeTuple, } from '@aztec/circuits.js'; -import { computeBlockHash, computeBlockHashWithGlobals, computeContractLeaf } from '@aztec/circuits.js/abis'; +import { + computeBlockHash, + computeBlockHashWithGlobals, + computeContractLeaf, + computeGlobalsHash, +} from '@aztec/circuits.js/abis'; import { toFriendlyJSON } from '@aztec/circuits.js/utils'; import { toBigIntBE } from '@aztec/foundation/bigint-buffer'; import { padArrayEnd } from '@aztec/foundation/collection'; @@ -44,7 +49,7 @@ import { Fr } from '@aztec/foundation/fields'; import { createDebugLogger } from '@aztec/foundation/log'; import { Tuple, assertLength } from '@aztec/foundation/serialize'; import { ContractData, L2Block, L2BlockL2Logs, MerkleTreeId, PublicDataWrite, TxL2Logs } from '@aztec/types'; -import { MerkleTreeOperations, computeGlobalVariablesHash } from '@aztec/world-state'; +import { MerkleTreeOperations } from '@aztec/world-state'; import chunk from 'lodash.chunk'; import flatMap from 'lodash.flatmap'; @@ -308,7 +313,7 @@ export class SoloBlockBuilder implements BlockBuilder { // Update the root trees with the latest data and contract tree roots, // and validate them against the output of the root circuit simulation this.debug(`Updating and validating root trees`); - const globalVariablesHash = await computeGlobalVariablesHash(left[0].constants.globalVariables); + const globalVariablesHash = computeGlobalsHash(await CircuitsWasm.get(), left[0].constants.globalVariables); await this.db.updateLatestGlobalVariablesHash(globalVariablesHash); await this.db.updateHistoricBlocksTree(globalVariablesHash); diff --git a/yarn-project/sequencer-client/src/simulator/public_executor.ts b/yarn-project/sequencer-client/src/simulator/public_executor.ts index 6928cff3ffc..3d6c2d5284b 100644 --- a/yarn-project/sequencer-client/src/simulator/public_executor.ts +++ b/yarn-project/sequencer-client/src/simulator/public_executor.ts @@ -6,8 +6,9 @@ import { PublicStateDB, } from '@aztec/acir-simulator'; import { AztecAddress, CircuitsWasm, EthAddress, Fr, FunctionSelector, HistoricBlockData } from '@aztec/circuits.js'; +import { computePublicDataTreeIndex } from '@aztec/circuits.js/abis'; import { ContractDataSource, ExtendedContractData, L1ToL2MessageSource, MerkleTreeId, Tx } from '@aztec/types'; -import { MerkleTreeOperations, computePublicDataTreeLeafIndex } from '@aztec/world-state'; +import { MerkleTreeOperations } from '@aztec/world-state'; /** * Returns a new PublicExecutor simulator backed by the supplied merkle tree db and contract data source. @@ -31,7 +32,7 @@ export function getPublicExecutor( /** * Implements the PublicContractsDB using a ContractDataSource. - * Progresively records contracts in transaction as they are processed in a block. + * Progressively records contracts in transaction as they are processed in a block. */ export class ContractsDataSourcePublicDB implements PublicContractsDB { cache = new Map(); @@ -106,7 +107,7 @@ class WorldStatePublicDB implements PublicStateDB { * @returns The current value in the storage slot. */ public async storageRead(contract: AztecAddress, slot: Fr): Promise { - const index = computePublicDataTreeLeafIndex(contract, slot, await CircuitsWasm.get()); + const index = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, slot).value; const cached = this.writeCache.get(index); if (cached !== undefined) return cached; const value = await this.db.getLeafValue(MerkleTreeId.PUBLIC_DATA_TREE, index); @@ -120,7 +121,7 @@ class WorldStatePublicDB implements PublicStateDB { * @param newValue - The new value to store. */ public async storageWrite(contract: AztecAddress, slot: Fr, newValue: Fr): Promise { - const index = computePublicDataTreeLeafIndex(contract, slot, await CircuitsWasm.get()); + const index = computePublicDataTreeIndex(await CircuitsWasm.get(), contract, slot).value; this.writeCache.set(index, newValue); } } diff --git a/yarn-project/world-state/src/index.ts b/yarn-project/world-state/src/index.ts index b0b168e54e5..08c8c225b66 100644 --- a/yarn-project/world-state/src/index.ts +++ b/yarn-project/world-state/src/index.ts @@ -1,4 +1,3 @@ export * from './synchronizer/index.js'; export * from './world-state-db/index.js'; -export * from './utils.js'; export * from './synchronizer/config.js'; diff --git a/yarn-project/world-state/src/utils.ts b/yarn-project/world-state/src/utils.ts deleted file mode 100644 index 46f199a979d..00000000000 --- a/yarn-project/world-state/src/utils.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { CircuitsWasm, GeneratorIndex, GlobalVariables } from '@aztec/circuits.js'; -import { computeGlobalsHash } from '@aztec/circuits.js/abis'; -import { pedersenCompressWithHashIndex } from '@aztec/circuits.js/barretenberg'; -import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr } from '@aztec/foundation/fields'; -import { toBigInt } from '@aztec/foundation/serialize'; -import { IWasmModule } from '@aztec/foundation/wasm'; - -/** - * Computes the index in the public data tree for a given contract and storage slot. - * @param contract - Address of the contract who owns the storage. - * @param slot - Slot within the contract storage. - * @param bbWasm - Wasm module for computing the hash. - * @returns The leaf index of the public data tree that maps to this storage slot. - */ -export function computePublicDataTreeLeafIndex(contract: AztecAddress, slot: Fr, wasm: IWasmModule): bigint { - return toBigInt( - pedersenCompressWithHashIndex( - wasm, - [contract, slot].map(f => f.toBuffer()), - GeneratorIndex.PUBLIC_LEAF_INDEX, - ), - ); -} - -/** - * Computes the hash of the global variables. - * @returns The hash of the global variables. - */ -export async function computeGlobalVariablesHash( - globalVariables: GlobalVariables = GlobalVariables.empty(), -): Promise { - const wasm = await CircuitsWasm.get(); - return computeGlobalsHash(wasm, globalVariables); -} diff --git a/yarn-project/world-state/src/world-state-db/merkle_trees.ts b/yarn-project/world-state/src/world-state-db/merkle_trees.ts index 27069d581cf..30271dd1eb7 100644 --- a/yarn-project/world-state/src/world-state-db/merkle_trees.ts +++ b/yarn-project/world-state/src/world-state-db/merkle_trees.ts @@ -10,7 +10,7 @@ import { PRIVATE_DATA_TREE_HEIGHT, PUBLIC_DATA_TREE_HEIGHT, } from '@aztec/circuits.js'; -import { computeBlockHash } from '@aztec/circuits.js/abis'; +import { computeBlockHash, computeGlobalsHash } from '@aztec/circuits.js/abis'; import { Committable } from '@aztec/foundation/committable'; import { SerialQueue } from '@aztec/foundation/fifo'; import { createDebugLogger } from '@aztec/foundation/log'; @@ -33,7 +33,6 @@ import { L2Block, MerkleTreeId, SiblingPath } from '@aztec/types'; import { default as levelup } from 'levelup'; import { MerkleTreeOperationsFacade } from '../merkle-tree/merkle_tree_operations_facade.js'; -import { computeGlobalVariablesHash } from '../utils.js'; import { CurrentTreeRoots, HandleL2BlockResult, @@ -127,12 +126,12 @@ export class MerkleTrees implements MerkleTreeDb { // The first leaf in the blocks tree contains the empty roots of the other trees and empty global variables. if (!fromDb) { - const initialGlobalVariablesHash = await computeGlobalVariablesHash(GlobalVariables.empty()); + const initialGlobalVariablesHash = computeGlobalsHash(wasm, GlobalVariables.empty()); await this._updateLatestGlobalVariablesHash(initialGlobalVariablesHash); await this._updateHistoricBlocksTree(initialGlobalVariablesHash, true); await this._commit(); } else { - await this._updateLatestGlobalVariablesHash(await computeGlobalVariablesHash(fromDbOptions.globalVariables)); + await this._updateLatestGlobalVariablesHash(computeGlobalsHash(wasm, fromDbOptions.globalVariables)); } } @@ -575,7 +574,7 @@ export class MerkleTrees implements MerkleTreeDb { } // Sync and add the block to the historic blocks tree - const globalVariablesHash = await computeGlobalVariablesHash(l2Block.globalVariables); + const globalVariablesHash = computeGlobalsHash(await CircuitsWasm.get(), l2Block.globalVariables); await this._updateLatestGlobalVariablesHash(globalVariablesHash); this.log(`Synced global variables with hash ${globalVariablesHash}`);