From df389b5f593a202bc644479a6c3dff884b7d3652 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Wed, 25 Oct 2023 20:18:01 +0100 Subject: [PATCH] chore: proxy redundant `hash` methods (#3046) Partly extracted from #3038 # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --- .../barretenberg/crypto/pedersen/pedersen.ts | 42 ++----------------- yarn-project/merkle-tree/src/pedersen.ts | 15 +------ yarn-project/types/src/interfaces/hasher.ts | 7 ---- 3 files changed, 4 insertions(+), 60 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 69362c4f878..786976580f5 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -14,15 +14,7 @@ import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVec * purposes. */ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8Array): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - if (lhs.length !== 32 || rhs.length !== 32) { - throw new Error(`Pedersen lhs and rhs inputs must be 32 bytes (got ${lhs.length} and ${rhs.length} respectively)`); - } - wasm.writeMemory(0, lhs); - wasm.writeMemory(32, rhs); - wasm.call('pedersen__hash_pair', 0, 32, 64); - return Buffer.from(wasm.getMemorySlice(64, 96)); + return pedersenCompressWithHashIndex(wasm, [Buffer.from(lhs), Buffer.from(rhs)], 0); } /** @@ -35,12 +27,7 @@ export function pedersenCompress(wasm: IWasmModule, lhs: Uint8Array, rhs: Uint8A * purposes. */ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const inputVectors = serializeBufferArrayToVector(inputs); - wasm.writeMemory(0, inputVectors); - wasm.call('pedersen__hash_multiple', 0, 0); - return Buffer.from(wasm.getMemorySlice(0, 32)); + return pedersenCompressWithHashIndex(wasm, inputs, 0); } /** @@ -52,12 +39,7 @@ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer * purposes. */ export function pedersenCompressInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const inputVectors = serializeBufferArrayToVector(inputs); - wasm.writeMemory(0, inputVectors); - wasm.call('pedersen__compress', 0, 0); - return Buffer.from(wasm.getMemorySlice(0, 32)); + return pedersenCompressWithHashIndex(wasm, inputs, 0); } /** @@ -78,24 +60,6 @@ export function pedersenCompressWithHashIndex(wasm: IWasmModule, inputs: Buffer[ return Buffer.from(wasm.getMemorySlice(0, 32)); } -/** - * Get a 32-byte pedersen hash from a buffer. - * @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 suitably-named functions for specific - * purposes. - */ -export function pedersenGetHash(wasm: IWasmModule, data: Buffer): Buffer { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const mem = wasm.call('bbmalloc', data.length); - wasm.writeMemory(mem, data); - wasm.call('pedersen__buffer_to_field', mem, data.length, 0); - wasm.call('bbfree', mem); - return Buffer.from(wasm.getMemorySlice(0, 32)); -} - /** * Given a buffer containing 32 byte pedersen leaves, return a new buffer containing the leaves and all pairs of nodes * that define a merkle tree. diff --git a/yarn-project/merkle-tree/src/pedersen.ts b/yarn-project/merkle-tree/src/pedersen.ts index 7c2c1caecd2..a4c0b57bb9a 100644 --- a/yarn-project/merkle-tree/src/pedersen.ts +++ b/yarn-project/merkle-tree/src/pedersen.ts @@ -1,9 +1,4 @@ -import { - pedersenCompress, - pedersenGetHash, - pedersenGetHashTree, - pedersenHashInputs, -} from '@aztec/circuits.js/barretenberg'; +import { pedersenCompress, pedersenGetHashTree, pedersenHashInputs } from '@aztec/circuits.js/barretenberg'; import { IWasmModule } from '@aztec/foundation/wasm'; import { Hasher } from '@aztec/types'; @@ -31,14 +26,6 @@ export class Pedersen implements Hasher { return pedersenHashInputs(this.wasm, inputs); } - /* - * @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 suitably-named functions for specific * purposes. diff --git a/yarn-project/types/src/interfaces/hasher.ts b/yarn-project/types/src/interfaces/hasher.ts index aea50dc0ae1..e5e7178972d 100644 --- a/yarn-project/types/src/interfaces/hasher.ts +++ b/yarn-project/types/src/interfaces/hasher.ts @@ -17,13 +17,6 @@ export interface Hasher { */ compressInputs(inputs: Buffer[]): Buffer; - /** - * Get a 32-byte hash from a buffer. - * @param data - The data buffer. - * @returns The resulting hash buffer. - */ - hashToField(data: Uint8Array): Buffer; - /** * Given a buffer containing 32 byte leaves, return a new buffer containing the leaves and all pairs of * nodes that define a merkle tree.