Skip to content

Commit

Permalink
chore: proxy redundant hash methods (#3046)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kevaundray authored Oct 25, 2023
1 parent de7e63b commit df389b5
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand Down
15 changes: 1 addition & 14 deletions yarn-project/merkle-tree/src/pedersen.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 0 additions & 7 deletions yarn-project/types/src/interfaces/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit df389b5

Please sign in to comment.