Skip to content

Commit

Permalink
chore: remove pedersen getHashTree (#3069)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kevaundray authored Oct 26, 2023
1 parent 36ffe57 commit 5e78f63
Show file tree
Hide file tree
Showing 12 changed files with 5 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> to_compress(data, data + length);
auto output = crypto::pedersen_hash::hash_buffer(to_compress);
write(r, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
25 changes: 0 additions & 25 deletions barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<grumpkin::fq>>(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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<std::vector<grumpkin::fq>>(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);
}
}
16 changes: 0 additions & 16 deletions barretenberg/exports.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
5 changes: 0 additions & 5 deletions barretenberg/ts/src/barretenberg_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ export class BarretenbergApi {
return result[0];
}

async pedersenHashToTree(data: Fr[]): Promise<Fr[]> {
const result = await this.binder.callWasmExport('pedersen_hash_to_tree', [data], [VectorDeserializer(Fr)]);
return result[0];
}

async blake2s(data: Uint8Array): Promise<Buffer32> {
const result = await this.binder.callWasmExport('blake2s', [data], [Buffer32]);
return result[0];
Expand Down
13 changes: 0 additions & 13 deletions barretenberg/ts/src/barretenberg_api/pedersen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]);
});
});

This file was deleted.

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

Expand All @@ -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<Buffer[]> {
return Promise.resolve(pedersenGetHashTree(this.wasm, leaves));
}
}
13 changes: 0 additions & 13 deletions yarn-project/types/src/interfaces/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer[]>;
}

0 comments on commit 5e78f63

Please sign in to comment.