Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix pedersen_compress_with_hash_index c_bind function #3054

Merged
merged 19 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down