-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Pedersen Hash & Commitment Gadgets (#95)
* [SQUASHED] Pedersen refactor into hash and commitment. Rename `crypto_pedersen` to `crypto_pedersen_hash` Rename. Pull generator data stuff out of pedersen crypto. Large refactor of pedersen native and stdlib. FIX everything. Get rid of unnecessary pedersen hash test. Its tricky to get this working: you need hash interface for byte array which is what we would like to avoid for pedersen_hash Fix cci. Enable ultra with different interfaces for: 1. pedersen hash 2. pedersen commitment Use lookup pedersen for merkle tree, fixed-base pedersen for commitments. Merkle tree test fixes. Circuit vk updates. * post rebase fixes. * js eg circuit size fix. --------- Co-authored-by: Suyash Bagad <[email protected]>
- Loading branch information
Showing
100 changed files
with
1,111 additions
and
647 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
barretenberg_module(crypto_generators ecc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
barretenberg_module(crypto_pedersen_commitment ecc crypto_generators crypto_pedersen_hash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include "pedersen.hpp" | ||
#include <common/serialize.hpp> | ||
#include <common/timer.hpp> | ||
#include <common/mem.hpp> | ||
#include <common/streams.hpp> | ||
#define WASM_EXPORT __attribute__((visibility("default"))) | ||
|
||
extern "C" { | ||
|
||
WASM_EXPORT void pedersen__init() | ||
{ | ||
crypto::generators::init_generator_data(); | ||
} | ||
|
||
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_commitment::compress_native(to_compress); | ||
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) | ||
{ | ||
std::vector<grumpkin::fq> to_compress; | ||
read(inputs_buffer, to_compress); | ||
auto r = crypto::pedersen_commitment::compress_native(to_compress, hash_index); | ||
barretenberg::fr::serialize_to_buffer(r, output); | ||
} | ||
|
||
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_commitment::compress_native(to_compress); | ||
write(r, output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
cpp/src/aztec/crypto/pedersen_commitment/pedersen_lookup.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "./pedersen_lookup.hpp" | ||
#include "../pedersen_hash/pedersen_lookup.hpp" | ||
#include "./convert_buffer_to_field.hpp" | ||
|
||
#include <ecc/curves/grumpkin/grumpkin.hpp> | ||
|
||
using namespace crypto::pedersen_hash::lookup; | ||
|
||
namespace crypto::pedersen_hash::lookup { | ||
extern std::array<std::vector<grumpkin::g1::affine_element>, NUM_PEDERSEN_TABLES> pedersen_tables; | ||
extern std::vector<grumpkin::g1::affine_element> pedersen_iv_table; | ||
extern std::array<grumpkin::g1::affine_element, NUM_PEDERSEN_TABLES> generators; | ||
} // namespace crypto::pedersen_hash::lookup | ||
|
||
namespace crypto { | ||
namespace pedersen_commitment { | ||
namespace lookup { | ||
|
||
grumpkin::g1::element merkle_damgard_compress(const std::vector<grumpkin::fq>& inputs, const size_t iv) | ||
{ | ||
if (inputs.size() == 0) { | ||
auto result = grumpkin::g1::affine_one; | ||
result.self_set_infinity(); | ||
return result; | ||
} | ||
init(); | ||
const size_t num_inputs = inputs.size(); | ||
|
||
grumpkin::fq result = (pedersen_iv_table[iv]).x; | ||
for (size_t i = 0; i < num_inputs; i++) { | ||
result = hash_pair(result, inputs[i]); | ||
} | ||
|
||
return (hash_single(result, false) + hash_single(grumpkin::fq(num_inputs), true)); | ||
} | ||
|
||
grumpkin::g1::affine_element commit_native(const std::vector<grumpkin::fq>& inputs, const size_t hash_index) | ||
{ | ||
return grumpkin::g1::affine_element(merkle_damgard_compress(inputs, hash_index)); | ||
} | ||
|
||
grumpkin::fq compress_native(const std::vector<grumpkin::fq>& inputs, const size_t hash_index) | ||
{ | ||
return commit_native(inputs, hash_index).x; | ||
} | ||
|
||
grumpkin::fq compress_native_buffer_to_field(const std::vector<uint8_t>& input) | ||
{ | ||
const auto elements = convert_buffer_to_field(input); | ||
grumpkin::fq result_fq = compress_native(elements); | ||
return result_fq; | ||
} | ||
|
||
std::vector<uint8_t> compress_native(const std::vector<uint8_t>& input) | ||
{ | ||
const auto result_fq = compress_native_buffer_to_field(input); | ||
uint256_t result_u256(result_fq); | ||
const size_t num_bytes = input.size(); | ||
|
||
bool is_zero = true; | ||
for (const auto byte : input) { | ||
is_zero = is_zero && (byte == static_cast<uint8_t>(0)); | ||
} | ||
if (is_zero) { | ||
result_u256 = num_bytes; | ||
} | ||
std::vector<uint8_t> result_buffer; | ||
result_buffer.reserve(32); | ||
for (size_t i = 0; i < 32; ++i) { | ||
const uint64_t shift = (31 - i) * 8; | ||
uint256_t shifted = result_u256 >> uint256_t(shift); | ||
result_buffer.push_back(static_cast<uint8_t>(shifted.data[0])); | ||
} | ||
return result_buffer; | ||
} | ||
|
||
} // namespace lookup | ||
} // namespace pedersen_commitment | ||
} // namespace crypto |
Oops, something went wrong.