Skip to content

Commit

Permalink
chore: Remove unused pedersen c_binds (#3058)
Browse files Browse the repository at this point in the history
Related to #3029 

This removes all of the unused c_binds. The typescript code has been
modified to not use any of these, so at this point its not a breaking
change for yarn-packages. The bb.js codegen were using these inside of
tests that I added to make sure that they were computing the same thing,
these tests have been removed.

# 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).

---------

Co-authored-by: Leila Wang <leizciw@gmail.com>
  • Loading branch information
kevaundray and LeilaWang authored Oct 26, 2023
1 parent ccd4611 commit e71e5f9
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 556 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@
#include "pedersen.hpp"

WASM_EXPORT void pedersen__init() {}
WASM_EXPORT void pedersen__compress_fields(uint8_t const* left, uint8_t const* right, uint8_t* result)
{
auto lhs = barretenberg::fr::serialize_from_buffer(left);
auto rhs = barretenberg::fr::serialize_from_buffer(right);
auto r = crypto::pedersen_hash::hash({ lhs, rhs });
barretenberg::fr::serialize_to_buffer(r, result);
}

WASM_EXPORT void pedersen__compress(uint8_t const* inputs_buffer, uint8_t* output)
{
std::vector<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
auto r = crypto::pedersen_hash::hash(to_compress);
barretenberg::fr::serialize_to_buffer(r, output);
}

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);
crypto::GeneratorContext<curve::Grumpkin> ctx; // todo fix
ctx.offset = static_cast<size_t>(hash_index);
auto r = crypto::pedersen_hash::hash(to_compress, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen__commit(uint8_t const* inputs_buffer, uint8_t* output)
{
Expand All @@ -37,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 @@ -5,12 +5,4 @@
#include "barretenberg/common/timer.hpp"

WASM_EXPORT void pedersen__init();

WASM_EXPORT void pedersen__compress_fields(uint8_t const* left, uint8_t const* right, uint8_t* result);

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, 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 @@ -9,35 +9,6 @@ using namespace barretenberg;

WASM_EXPORT void pedersen___init() {}

WASM_EXPORT void pedersen___compress_fields(fr::in_buf left, fr::in_buf right, fr::out_buf result)
{
auto lhs = barretenberg::fr::serialize_from_buffer(left);
auto rhs = barretenberg::fr::serialize_from_buffer(right);
auto r = crypto::pedersen_hash::hash({ lhs, rhs });
barretenberg::fr::serialize_to_buffer(r, result);
}

WASM_EXPORT void pedersen___compress(fr::vec_in_buf inputs_buffer, fr::out_buf output)
{
std::vector<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
auto r = crypto::pedersen_hash::hash(to_compress);
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen___compress_with_hash_index(fr::vec_in_buf inputs_buffer,
uint32_t const* hash_index,
fr::out_buf output)
{
std::vector<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
const size_t generator_offset = ntohl(*hash_index);
crypto::GeneratorContext<curve::Grumpkin> ctx; // todo fix
ctx.offset = generator_offset;
auto r = crypto::pedersen_hash::hash(to_compress, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen___commit(fr::vec_in_buf inputs_buffer, fr::out_buf output)
{
std::vector<grumpkin::fq> to_compress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,5 @@ using namespace barretenberg;

WASM_EXPORT void pedersen___init();

WASM_EXPORT void pedersen___compress_fields(fr::in_buf left, fr::in_buf right, fr::out_buf result);

WASM_EXPORT void pedersen___compress(fr::vec_in_buf inputs_buffer, fr::out_buf output);

WASM_EXPORT void pedersen___compress_with_hash_index(fr::vec_in_buf inputs_buffer,
uint32_t const* hash_index,
fr::out_buf output);

WASM_EXPORT void pedersen___commit(fr::vec_in_buf inputs_buffer, fr::out_buf output);
}
61 changes: 4 additions & 57 deletions barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,13 @@ extern "C" {

WASM_EXPORT void pedersen_hash__init() {}

WASM_EXPORT void pedersen__hash(uint8_t const* inputs_buffer, uint8_t* output)
WASM_EXPORT void pedersen__hash_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);
auto r = crypto::pedersen_hash::hash(to_compress);
crypto::GeneratorContext<curve::Grumpkin> ctx; // todo fix
ctx.offset = static_cast<size_t>(hash_index);
auto r = crypto::pedersen_hash::hash(to_compress, ctx);
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen__hash_with_hash_index(uint8_t const* inputs_buffer,
uint32_t const* hash_index,
uint8_t* output)
{
std::vector<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
auto r = crypto::pedersen_hash::hash(to_compress, ntohl(*hash_index));
barretenberg::fr::serialize_to_buffer(r, output);
}

WASM_EXPORT void pedersen__hash_pair(uint8_t const* left, uint8_t const* right, uint8_t* result)
{
auto lhs = barretenberg::fr::serialize_from_buffer(left);
auto rhs = barretenberg::fr::serialize_from_buffer(right);
auto r = crypto::pedersen_hash::hash({ lhs, rhs });
barretenberg::fr::serialize_to_buffer(r, result);
}

WASM_EXPORT void pedersen__hash_multiple(uint8_t const* inputs_buffer, uint8_t* output)
{
pedersen__hash(inputs_buffer, output);
}

WASM_EXPORT void pedersen__hash_multiple_with_hash_index(uint8_t const* inputs_buffer,
uint32_t const* hash_index,
uint8_t* output)
{
pedersen__hash_with_hash_index(inputs_buffer, hash_index, 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;
}
}
18 changes: 0 additions & 18 deletions barretenberg/cpp/src/barretenberg/crypto/pedersen_hash/c_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,7 @@ extern "C" {
using namespace barretenberg;

WASM_EXPORT void pedersen_hash_init();
WASM_EXPORT void pedersen_hash(fr::vec_in_buf inputs_buffer, fr::out_buf output);
WASM_EXPORT void pedersen_hash_with_hash_index(fr::vec_in_buf inputs_buffer,
uint32_t const* hash_index,
fr::out_buf output);

WASM_EXPORT void pedersen_hash_pair(fr::in_buf left, fr::in_buf right, fr::out_buf result);

WASM_EXPORT void pedersen_hash_multiple(fr::vec_in_buf inputs_buffer, fr::out_buf output);

WASM_EXPORT void pedersen_hash_multiple_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,13 +5,8 @@

extern "C" {

WASM_EXPORT void pedersen_hash(uint8_t const* inputs_buffer, uint8_t* output)
{
std::vector<grumpkin::fq> to_compress;
read(inputs_buffer, to_compress);
auto r = crypto::pedersen_hash::hash(to_compress);
barretenberg::fr::serialize_to_buffer(r, output);
}
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 @@ -21,47 +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() {}

WASM_EXPORT void pedersen_hash_pair(uint8_t const* left, uint8_t const* right, uint8_t* result)
{
auto lhs = barretenberg::fr::serialize_from_buffer(left);
auto rhs = barretenberg::fr::serialize_from_buffer(right);
auto r = crypto::pedersen_hash::hash({ lhs, rhs });
barretenberg::fr::serialize_to_buffer(r, result);
}

WASM_EXPORT void pedersen_hash_multiple(uint8_t const* inputs_buffer, uint8_t* output)
{
pedersen_hash(inputs_buffer, output);
}

WASM_EXPORT void pedersen_hash_multiple_with_hash_index(uint8_t const* inputs_buffer,
uint32_t const* hash_index,
uint8_t* output)
{
pedersen_hash_with_hash_index(inputs_buffer, hash_index, 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)
{
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);
}
}
Loading

0 comments on commit e71e5f9

Please sign in to comment.