From 5e78f638352b970dac7307e6899b35ade23f2242 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 26 Oct 2023 17:03:50 +0100 Subject: [PATCH] chore: remove pedersen getHashTree (#3069) Related to #3029 We can merge this into `kw/pedersen-cleanup-remove-unused-cbinds` as part of the cleanup # 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). --- .../crypto/pedersen_commitment/c_bind.cpp | 9 +---- .../crypto/pedersen_commitment/c_bind.hpp | 2 -- .../crypto/pedersen_hash/c_bind.cpp | 25 ------------- .../crypto/pedersen_hash/c_bind.hpp | 8 ----- .../crypto/pedersen_hash/c_bind_new.cpp | 25 ++----------- barretenberg/exports.json | 16 --------- barretenberg/ts/src/barretenberg_api/index.ts | 5 --- .../ts/src/barretenberg_api/pedersen.test.ts | 13 ------- .../crypto/pedersen/pedersen.test.ts | 36 ------------------- .../barretenberg/crypto/pedersen/pedersen.ts | 35 +----------------- yarn-project/merkle-tree/src/pedersen.ts | 10 +----- yarn-project/types/src/interfaces/hasher.ts | 13 ------- 12 files changed, 5 insertions(+), 192 deletions(-) delete mode 100644 yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.test.ts diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp index fd8ada01d3c..71903bdcc6a 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp @@ -12,11 +12,4 @@ WASM_EXPORT void pedersen__commit(uint8_t const* inputs_buffer, uint8_t* output) grumpkin::g1::affine_element pedersen_hash = crypto::pedersen_commitment::commit_native(to_compress); serialize::write(output, pedersen_hash); -} - -WASM_EXPORT void pedersen__buffer_to_field(uint8_t const* data, size_t length, uint8_t* r) -{ - std::vector to_compress(data, data + length); - auto output = crypto::pedersen_hash::hash_buffer(to_compress); - write(r, output); -} +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp index 4f8aac2fd99..e26de08c14c 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp @@ -6,5 +6,3 @@ WASM_EXPORT void pedersen__init(); WASM_EXPORT void pedersen__commit(uint8_t const* inputs_buffer, uint8_t* output); - -WASM_EXPORT void pedersen__buffer_to_field(uint8_t const* data, size_t length, uint8_t* r); diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp index 6dc12a640df..3d35c62e4e4 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp @@ -16,29 +16,4 @@ WASM_EXPORT void pedersen__hash_with_hash_index(uint8_t const* inputs_buffer, ui auto r = crypto::pedersen_hash::hash(to_compress, ctx); barretenberg::fr::serialize_to_buffer(r, output); } - -/** - * 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. - * e.g. - * input: [1][2][3][4] - * output: [1][2][3][4][compress(1,2)][compress(3,4)][compress(5,6)] - */ -WASM_EXPORT uint8_t* pedersen__hash_to_tree(uint8_t const* data) -{ - auto fields = from_buffer>(data); - auto num_outputs = fields.size() * 2 - 1; - fields.reserve(num_outputs); - - for (size_t i = 0; fields.size() < num_outputs; i += 2) { - fields.push_back(crypto::pedersen_hash::hash({ fields[i], fields[i + 1] })); - } - - auto buf_size = 4 + num_outputs * sizeof(grumpkin::fq); - auto buf = (uint8_t*)aligned_alloc(64, buf_size); - auto dst = &buf[0]; - write(dst, fields); - - return buf; -} } \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp index 25f07519aec..218e38c0332 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp @@ -11,12 +11,4 @@ WASM_EXPORT void pedersen_hash_init(); WASM_EXPORT void pedersen_hash_with_hash_index(fr::vec_in_buf inputs_buffer, uint32_t const* hash_index, fr::out_buf output); -/** - * 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. - * e.g. - * input: [1][2][3][4] - * output: [1][2][3][4][compress(1,2)][compress(3,4)][compress(5,6)] - */ -WASM_EXPORT void pedersen_hash_to_tree(fr::vec_in_buf data, fr::vec_out_buf out); } \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind_new.cpp b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind_new.cpp index e4e05cddfd2..d7b860574fa 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind_new.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind_new.cpp @@ -5,6 +5,8 @@ extern "C" { +WASM_EXPORT void pedersen_hash_init() {} + WASM_EXPORT void pedersen_hash_with_hash_index(uint8_t const* inputs_buffer, uint32_t const* hash_index, uint8_t* output) @@ -14,27 +16,4 @@ WASM_EXPORT void pedersen_hash_with_hash_index(uint8_t const* inputs_buffer, auto r = crypto::pedersen_hash::hash(to_compress, ntohl(*hash_index)); barretenberg::fr::serialize_to_buffer(r, output); } - -WASM_EXPORT void pedersen_hash_init() {} - -/** - * 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. - * e.g. - * input: [1][2][3][4] - * output: [1][2][3][4][compress(1,2)][compress(3,4)][compress(5,6)] - * - */ -WASM_EXPORT void pedersen_hash_to_tree(fr::vec_in_buf data, fr::vec_out_buf out) -{ - auto fields = from_buffer>(data); - auto num_outputs = fields.size() * 2 - 1; - fields.reserve(num_outputs); - - for (size_t i = 0; fields.size() < num_outputs; i += 2) { - fields.push_back(crypto::pedersen_hash::hash({ fields[i], fields[i + 1] })); - } - - *out = to_heap_buffer(fields); -} } \ No newline at end of file diff --git a/barretenberg/exports.json b/barretenberg/exports.json index 775689b8713..9f628bdd31a 100644 --- a/barretenberg/exports.json +++ b/barretenberg/exports.json @@ -47,22 +47,6 @@ ], "isAsync": false }, - { - "functionName": "pedersen_hash_to_tree", - "inArgs": [ - { - "name": "data", - "type": "fr::vec_in_buf" - } - ], - "outArgs": [ - { - "name": "out", - "type": "fr::vec_out_buf" - } - ], - "isAsync": false - }, { "functionName": "blake2s", "inArgs": [ diff --git a/barretenberg/ts/src/barretenberg_api/index.ts b/barretenberg/ts/src/barretenberg_api/index.ts index 9f45459558c..bafb9840d80 100644 --- a/barretenberg/ts/src/barretenberg_api/index.ts +++ b/barretenberg/ts/src/barretenberg_api/index.ts @@ -37,11 +37,6 @@ export class BarretenbergApi { return result[0]; } - async pedersenHashToTree(data: Fr[]): Promise { - const result = await this.binder.callWasmExport('pedersen_hash_to_tree', [data], [VectorDeserializer(Fr)]); - return result[0]; - } - async blake2s(data: Uint8Array): Promise { const result = await this.binder.callWasmExport('blake2s', [data], [Buffer32]); return result[0]; diff --git a/barretenberg/ts/src/barretenberg_api/pedersen.test.ts b/barretenberg/ts/src/barretenberg_api/pedersen.test.ts index 40911474b3a..36e92f9caeb 100644 --- a/barretenberg/ts/src/barretenberg_api/pedersen.test.ts +++ b/barretenberg/ts/src/barretenberg_api/pedersen.test.ts @@ -22,17 +22,4 @@ describe('pedersen', () => { const result = await api.pedersenCommit([new Fr(4n), new Fr(8n), new Fr(12n)]); expect(result).toEqual(new Fr(18374309251862457296563484909553154519357910650678202211610516068880120638872n)); }); - - it('pedersenHashToTree', async () => { - const result = await api.pedersenHashToTree([new Fr(4n), new Fr(8n), new Fr(12n), new Fr(16n)]); - expect(result).toEqual([ - new Fr(4n), - new Fr(8n), - new Fr(12n), - new Fr(16n), - new Fr(1521373897829389584529155077412196627698249315427143054350987371861781120260n), - new Fr(18350527319045519333962768191016242826584323959670139897255818770108115223653n), - new Fr(5972535902427608430534212385621973704186819235181735133037695406667218179357n), - ]); - }); }); diff --git a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.test.ts b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.test.ts deleted file mode 100644 index 1ff650d30b1..00000000000 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { CircuitsWasm } from '@aztec/circuits.js'; - -import { Buffer } from 'buffer'; - -import { pedersenGetHashTree } from './pedersen.js'; - -describe('pedersen', () => { - let barretenbergWasm!: CircuitsWasm; - const values: Buffer[] = []; - - beforeAll(async () => { - barretenbergWasm = await CircuitsWasm.get(); - - // TODO was originally 2 ** 12 - for (let i = 0; i < 2 ** 2; ++i) { - const v = Buffer.alloc(32, 0); - v.writeUInt32LE(i, 0); - values[i] = v; - } - }); - - it('hasher_consistency_and_benchmark', () => { - // const start1 = new Date().getTime(); - const result = pedersenGetHashTree(barretenbergWasm, values); - // const end1 = new Date().getTime() - start1; - - // console.log(`Single hasher: ~${end1 / values.length}ms / value`); - // TODO more than smoke-test this - expect(result.length).toBe(values.length * 2 - 1); - // TODO - // console.log(`Pooled hasher: ~${end2 / values.length}ms / value`); - // console.log(`Pooled improvement: ${(end1 / end2).toFixed(2)}x`); - // expect(poolResults).toEqual(singleResults); - // await pool.destroy(); - }); -}); 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 318c778d4bb..a2028040490 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -2,7 +2,7 @@ import { IWasmModule } from '@aztec/foundation/wasm'; import { Buffer } from 'buffer'; -import { deserializeArrayFromVector, deserializeField, serializeBufferArrayToVector } from '../../serialize.js'; +import { serializeBufferArrayToVector } from '../../serialize.js'; /** * Hashes two arrays. @@ -73,36 +73,3 @@ export function pedersenHashWithHashIndex(wasm: IWasmModule, inputs: Buffer[], h return Buffer.from(hashOutput); } - -/** - * 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. - * - * E.g. - * Input: [1][2][3][4] - * Output: [1][2][3][4][hash(1,2)][hash(3,4)][hash(5,6)]. - * - * @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 suitably-named functions for specific - * purposes. - */ -export function pedersenGetHashTree(wasm: IWasmModule, values: Buffer[]) { - // If not done already, precompute constants. - wasm.call('pedersen__init'); - const data = serializeBufferArrayToVector(values); - const inputPtr = wasm.call('bbmalloc', data.length); - wasm.writeMemory(inputPtr, data); - - wasm.call('pedersen_hash_to_tree', inputPtr, 0); - const resultPtr = Buffer.from(wasm.getMemorySlice(0, 4)).readUInt32LE(0); - // First 4 bytes is full response length in byters. - // Second 4 bytes is vector length in fields. - const resultNumFields = Buffer.from(wasm.getMemorySlice(resultPtr + 4, resultPtr + 8)).readUInt32BE(0); - const resultData = Buffer.from(wasm.getMemorySlice(resultPtr + 4, resultPtr + 8 + resultNumFields * 32)); - wasm.call('bbfree', inputPtr); - wasm.call('bbfree', resultPtr); - - return deserializeArrayFromVector(deserializeField, resultData).elem; -} diff --git a/yarn-project/merkle-tree/src/pedersen.ts b/yarn-project/merkle-tree/src/pedersen.ts index 9b81c94b103..ae6c5e74e5b 100644 --- a/yarn-project/merkle-tree/src/pedersen.ts +++ b/yarn-project/merkle-tree/src/pedersen.ts @@ -1,4 +1,4 @@ -import { pedersenGetHashTree, pedersenHash, pedersenHashInputs } from '@aztec/circuits.js/barretenberg'; +import { pedersenHash, pedersenHashInputs } from '@aztec/circuits.js/barretenberg'; import { IWasmModule } from '@aztec/foundation/wasm'; import { Hasher } from '@aztec/types'; @@ -25,12 +25,4 @@ export class Pedersen implements Hasher { public hashInputs(inputs: Buffer[]): Buffer { return pedersenHashInputs(this.wasm, inputs); } - - /* - * @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/types/src/interfaces/hasher.ts b/yarn-project/types/src/interfaces/hasher.ts index 48e0d265e2b..5fb1da8b3f2 100644 --- a/yarn-project/types/src/interfaces/hasher.ts +++ b/yarn-project/types/src/interfaces/hasher.ts @@ -16,17 +16,4 @@ export interface Hasher { * @returns The resulting 32-byte hash. */ hashInputs(inputs: Buffer[]): 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. - * - * E.g. - * Input: [1][2][3][4] - * Output: [1][2][3][4][hash(1,2)][hash(3,4)][hash(5,6)]. - * - * @param leaves - The 32 byte leaves. - * @returns A tree represented by an array. - */ - hashToTree(leaves: Buffer[]): Promise; }