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 f3b636a8e08..ee6beb6f3e5 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.cpp @@ -20,7 +20,7 @@ WASM_EXPORT void pedersen__compress(uint8_t const* inputs_buffer, uint8_t* outpu barretenberg::fr::serialize_to_buffer(r, output); } -WASM_EXPORT void pedersen__compress_with_hash_index(uint8_t const* inputs_buffer, uint8_t* output, uint32_t hash_index) +WASM_EXPORT void pedersen__compress_with_hash_index(uint8_t const* inputs_buffer, uint32_t hash_index, uint8_t* output) { std::vector to_compress; read(inputs_buffer, to_compress); 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 0ed52cc3fd8..af8ec1c6543 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/pedersen_commitment/c_bind.hpp @@ -10,7 +10,7 @@ WASM_EXPORT void pedersen__compress_fields(uint8_t const* left, uint8_t const* r WASM_EXPORT void pedersen__compress(uint8_t const* inputs_buffer, uint8_t* output); -WASM_EXPORT void pedersen__compress_with_hash_index(uint8_t const* inputs_buffer, uint8_t* output, uint32_t hash_index); +WASM_EXPORT void pedersen__compress_with_hash_index(uint8_t const* inputs_buffer, uint32_t hash_index, uint8_t* output); 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/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts index b0147b5faa7..5b6993bff8d 100644 --- a/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts +++ b/yarn-project/circuits.js/src/barretenberg/crypto/pedersen/pedersen.ts @@ -41,10 +41,24 @@ export function pedersenHashInputs(wasm: IWasmModule, inputs: Buffer[]): Buffer export function pedersenHashWithHashIndex(wasm: IWasmModule, inputs: Buffer[], hashIndex: number): Buffer { // If not done already, precompute constants. wasm.call('pedersen__init'); - const inputVectors = serializeBufferArrayToVector(inputs); - wasm.writeMemory(0, inputVectors); - wasm.call('pedersen__compress_with_hash_index', 0, 0, hashIndex); - return Buffer.from(wasm.getMemorySlice(0, 32)); + + // Allocate memory for the inputs. We can optimize this + // by checking the length and copying the data to the + // wasm scratch space if it is small enough. + const data = serializeBufferArrayToVector(inputs); + const inputPtr = wasm.call('bbmalloc', data.length); + wasm.writeMemory(inputPtr, data); + + // Since the output is 32 bytes, instead of allocating memory + // we can simply use the scratch space. + const outputPtr = 0; + + wasm.call('pedersen__compress_with_hash_index', inputPtr, hashIndex, outputPtr); + const hashOutput = wasm.getMemorySlice(0, 32); + + wasm.call('bbfree', inputPtr); + + return Buffer.from(hashOutput); } /**