From b9e969bfdcdb15b5df20010da1642b66a7fea72d Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 6 Oct 2023 11:52:59 +0000 Subject: [PATCH 1/5] marking pedersen bindings deprecated --- yarn-project/circuits.js/src/abis/abis.ts | 2 +- .../src/barretenberg/crypto/pedersen/pedersen.ts | 9 +++++++++ yarn-project/merkle-tree/src/pedersen.ts | 13 +++++++++++++ .../src/test/utils/pedersen_with_counter.ts | 2 ++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index f24e6c54471..459f8468119 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -252,7 +252,7 @@ export function siloCommitment(wasm: IWasmModule, contract: AztecAddress, innerC } /** - * Computes a unique commitment. It includes a nonce which contains data that guarantees the commiment will be unique. + * Computes a unique commitment. It includes a nonce which contains data that guarantees the commitment will be unique. * @param wasm - A module providing low-level wasm access. * @param nonce - The contract address. * @param siloedCommitment - An siloed commitment. diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts index 485fb6ac7ba..7013aabc78c 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -10,6 +10,7 @@ import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVec * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8Array): Buffer { // If not done already, precompute constants. @@ -29,6 +30,7 @@ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8A * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -44,6 +46,7 @@ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -59,6 +62,7 @@ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buf * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -75,6 +79,7 @@ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]) * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -91,6 +96,7 @@ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Bu * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -107,6 +113,7 @@ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[ * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -122,6 +129,7 @@ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: * @param wasm - The barretenberg module. * @param data - The data buffer. * @returns The hash buffer. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { // If not done already, precompute constants. @@ -144,6 +152,7 @@ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { * @param wasm - The barretenberg module. * @param values - The 32 byte pedersen leaves. * @returns A tree represented by an array. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export function pedersenGetHashTree(wasm: IWasmModule, values: Buffer[]) { // If not done already, precompute constants. diff --git a/yarn-project/merkle-tree/src/pedersen.ts b/yarn-project/merkle-tree/src/pedersen.ts index 376f2708d3a..1f26a812706 100644 --- a/yarn-project/merkle-tree/src/pedersen.ts +++ b/yarn-project/merkle-tree/src/pedersen.ts @@ -9,22 +9,35 @@ import { Hasher } from '@aztec/types'; /** * A helper class encapsulating Pedersen hash functionality. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export class Pedersen implements Hasher { constructor(private wasm: IWasmModule) {} + /* + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { return pedersenCompress(this.wasm, lhs, rhs); } + /* + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + */ public compressInputs(inputs: Buffer[]): Buffer { return pedersenHashInputs(this.wasm, inputs); } + /* + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + */ public hashToField(data: Uint8Array): Buffer { return pedersenGetHash(this.wasm, Buffer.from(data)); } + /* + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + */ public hashToTree(leaves: Buffer[]): Promise { return Promise.resolve(pedersenGetHashTree(this.wasm, leaves)); } diff --git a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts index b5e26783954..79eaab79214 100644 --- a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts +++ b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts @@ -2,6 +2,7 @@ import { Pedersen } from '../../index.js'; /** * A test utility allowing us to count the number of times the compress function has been called. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ export class PedersenWithCounter extends Pedersen { /** @@ -14,6 +15,7 @@ export class PedersenWithCounter extends Pedersen { * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. + * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { this.compressCounter++; From 95e124656dbd352e1734ac7385d9e67ca8eed0d9 Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 6 Oct 2023 12:41:47 +0000 Subject: [PATCH 2/5] typo --- yarn-project/circuits.js/src/abis/abis.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn-project/circuits.js/src/abis/abis.ts b/yarn-project/circuits.js/src/abis/abis.ts index 459f8468119..7f4a94ef46b 100644 --- a/yarn-project/circuits.js/src/abis/abis.ts +++ b/yarn-project/circuits.js/src/abis/abis.ts @@ -367,7 +367,7 @@ export function computePublicDataTreeValue(wasm: IWasmModule, value: Fr): Fr { * Computes a public data tree index from contract address and storage slot. * @param wasm - A module providing low-level wasm access. * @param contractAddress - Contract where insertion is occurring. - * @param storageSlot - Storage slot where insertion is occuring. + * @param storageSlot - Storage slot where insertion is occurring. * @returns Public data tree index computed from contract address and storage slot. */ From 45b61e6f54c98e73879740d5d8f6fb26f56a181d Mon Sep 17 00:00:00 2001 From: benesjan Date: Fri, 6 Oct 2023 13:04:32 +0000 Subject: [PATCH 3/5] 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}`); From f446df19714c524136784d7d1c4e43708b3dac23 Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 10 Oct 2023 09:05:38 +0000 Subject: [PATCH 4/5] last touches --- yarn-project/acir-simulator/src/index.ts | 1 - .../barretenberg/crypto/pedersen/pedersen.ts | 18 +++++++++--------- yarn-project/merkle-tree/src/pedersen.ts | 10 +++++----- .../src/test/utils/pedersen_with_counter.ts | 4 ++-- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/yarn-project/acir-simulator/src/index.ts b/yarn-project/acir-simulator/src/index.ts index d51150943c6..e166c532bdd 100644 --- a/yarn-project/acir-simulator/src/index.ts +++ b/yarn-project/acir-simulator/src/index.ts @@ -1,4 +1,3 @@ export * from './client/index.js'; export * from './acvm/index.js'; export * from './public/index.js'; -export { computeSlotForMapping } from './utils.js'; diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts index 7013aabc78c..0131c49c629 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -10,7 +10,7 @@ import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVec * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8Array): Buffer { // If not done already, precompute constants. @@ -30,7 +30,7 @@ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8A * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -46,7 +46,7 @@ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -62,7 +62,7 @@ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buf * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -79,7 +79,7 @@ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]) * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -96,7 +96,7 @@ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Bu * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -113,7 +113,7 @@ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[ * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -129,7 +129,7 @@ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: * @param wasm - The barretenberg module. * @param data - The data buffer. * @returns The hash buffer. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { // If not done already, precompute constants. @@ -152,7 +152,7 @@ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { * @param wasm - The barretenberg module. * @param values - The 32 byte pedersen leaves. * @returns A tree represented by an array. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export function pedersenGetHashTree(wasm: IWasmModule, values: Buffer[]) { // If not done already, precompute constants. diff --git a/yarn-project/merkle-tree/src/pedersen.ts b/yarn-project/merkle-tree/src/pedersen.ts index 1f26a812706..6132306178c 100644 --- a/yarn-project/merkle-tree/src/pedersen.ts +++ b/yarn-project/merkle-tree/src/pedersen.ts @@ -9,34 +9,34 @@ import { Hasher } from '@aztec/types'; /** * A helper class encapsulating Pedersen hash functionality. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export class Pedersen implements Hasher { constructor(private wasm: IWasmModule) {} /* - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { return pedersenCompress(this.wasm, lhs, rhs); } /* - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ public compressInputs(inputs: Buffer[]): Buffer { return pedersenHashInputs(this.wasm, inputs); } /* - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ public hashToField(data: Uint8Array): Buffer { return pedersenGetHash(this.wasm, Buffer.from(data)); } /* - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ public hashToTree(leaves: Buffer[]): Promise { return Promise.resolve(pedersenGetHashTree(this.wasm, leaves)); diff --git a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts index 79eaab79214..401cddbe474 100644 --- a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts +++ b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts @@ -2,7 +2,7 @@ import { Pedersen } from '../../index.js'; /** * A test utility allowing us to count the number of times the compress function has been called. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ export class PedersenWithCounter extends Pedersen { /** @@ -15,7 +15,7 @@ export class PedersenWithCounter extends Pedersen { * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly. Create specific nicely-called WASM functions for your use-case. + * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { this.compressCounter++; From 4b722a5d79143fa4138c7c7fded119945b23d5f7 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 11 Oct 2023 10:25:37 +0000 Subject: [PATCH 5/5] Phil's suggestions --- .../barretenberg/crypto/pedersen/pedersen.ts | 27 ++++++++++++------- yarn-project/merkle-tree/src/pedersen.ts | 15 +++++++---- .../src/test/utils/pedersen_with_counter.ts | 6 +++-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts index 0131c49c629..32ddc30f377 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -10,7 +10,8 @@ import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVec * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8Array): Buffer { // If not done already, precompute constants. @@ -30,7 +31,8 @@ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8A * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -46,7 +48,8 @@ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -62,7 +65,8 @@ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buf * @param wasm - The barretenberg module. * @param inputs - The array of buffers to compress. * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { // If not done already, precompute constants. @@ -79,7 +83,8 @@ export function pedersenPlookupCommitInputs(wasm: IWasmModule, inputs: Buffer[]) * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -96,7 +101,8 @@ export function pedersenPlookupCommitWithHashIndex(wasm: IWasmModule, inputs: Bu * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -113,7 +119,8 @@ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[ * @param inputs - The array of buffers to compress. * @param hashIndex - Hash index of the generator to use (See GeneratorIndex enum). * @returns The resulting 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. @@ -129,7 +136,8 @@ export function pedersenPlookupCompressWithHashIndex(wasm: IWasmModule, inputs: * @param wasm - The barretenberg module. * @param data - The data buffer. * @returns The hash buffer. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { // If not done already, precompute constants. @@ -152,7 +160,8 @@ export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { * @param wasm - The barretenberg module. * @param values - The 32 byte pedersen leaves. * @returns A tree represented by an array. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export function pedersenGetHashTree(wasm: IWasmModule, values: Buffer[]) { // If not done already, precompute constants. diff --git a/yarn-project/merkle-tree/src/pedersen.ts b/yarn-project/merkle-tree/src/pedersen.ts index 6132306178c..7c2c1caecd2 100644 --- a/yarn-project/merkle-tree/src/pedersen.ts +++ b/yarn-project/merkle-tree/src/pedersen.ts @@ -9,34 +9,39 @@ import { Hasher } from '@aztec/types'; /** * A helper class encapsulating Pedersen hash functionality. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export class Pedersen implements Hasher { constructor(private wasm: IWasmModule) {} /* - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { return pedersenCompress(this.wasm, lhs, rhs); } /* - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ public compressInputs(inputs: Buffer[]): Buffer { return pedersenHashInputs(this.wasm, inputs); } /* - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ public hashToField(data: Uint8Array): Buffer { return pedersenGetHash(this.wasm, Buffer.from(data)); } /* - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ public hashToTree(leaves: Buffer[]): Promise { return Promise.resolve(pedersenGetHashTree(this.wasm, leaves)); diff --git a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts index 401cddbe474..4555ffa8105 100644 --- a/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts +++ b/yarn-project/merkle-tree/src/test/utils/pedersen_with_counter.ts @@ -2,7 +2,8 @@ import { Pedersen } from '../../index.js'; /** * A test utility allowing us to count the number of times the compress function has been called. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ export class PedersenWithCounter extends Pedersen { /** @@ -15,7 +16,8 @@ export class PedersenWithCounter extends Pedersen { * @param lhs - The first hash. * @param rhs - The second hash. * @returns The new 32-byte hash. - * @deprecated Don't call pedersen directly in production code. Instead, create specific nicely-called WASM functions. + * @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific + * purposes. */ public compress(lhs: Uint8Array, rhs: Uint8Array): Buffer { this.compressCounter++;