-
Notifications
You must be signed in to change notification settings - Fork 105
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
refactor!: Use circuit builders #501
Changes from 64 commits
2b681d2
4fe7ac2
ae2f094
56ebb56
a779895
bfd910f
7b4ed77
bc4bcbe
33f31ab
83412a7
83cf7d9
08e2741
dc05a2c
40e4d69
ecea160
4e7006d
5b10604
e3e1d45
063250b
624b4fa
c32e542
8a4c34c
4b9b7be
e95b048
0fdd8b5
ed6131f
ae5e01f
27f0b68
a084cb7
7d5d87c
9eb2f1e
c0503b8
665a54a
09baba7
44851bd
0d82d1f
9788410
180041f
89c9bde
a9aa020
d0a277f
30e714e
bd46581
e87617c
1fea680
77fbc30
61b2c6c
3773724
0a13946
0493ef0
efb4119
daec1bc
b053b79
02f9bd4
86ed54f
4a642c6
dc25ece
a33cb5b
a6ab841
22d2ff6
63a814d
36a8a24
e3954de
74124ba
77742d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
master | ||
64c8ba700b75df07a8452a6f2eae3d23cf7625a6 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,8 @@ | |
#include "ecc/curves/grumpkin/grumpkin.hpp" | ||
#include "numeric/random/engine.hpp" | ||
#include "numeric/uint256/uint256.hpp" | ||
#include "plonk/composer/turbo_plonk_composer.hpp" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The follow-up to this PR will include lots of renaming, eg "circuit constructor" becomes "circuit builder" in various places, "composer" becomes "circuit" or "builder" or "circuit_builder" or something in some places, and stays "composer" in other places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That said, there were certain places where I found it helpful/clarifying to do some renaming in order to get this refactor done, and I leave some of that work in place. |
||
#include "plonk/composer/ultra_plonk_composer.hpp" | ||
#include "proof_system/circuit_constructors/turbo_circuit_constructor.hpp" | ||
#include "proof_system/circuit_constructors/ultra_circuit_constructor.hpp" | ||
#include "plonk/proof_system/types/proof.hpp" | ||
#include "plonk/proof_system/verification_key/verification_key.hpp" | ||
#include "proof_system/types/composer_type.hpp" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
#include "barretenberg/crypto/ecdsa/ecdsa.hpp" | ||
#include "barretenberg/ecc/curves/bn254/fr.hpp" | ||
#include "barretenberg/honk/proof_system/ultra_prover.hpp" | ||
#include "barretenberg/honk/proof_system/ultra_verifier.hpp" | ||
#include <benchmark/benchmark.h> | ||
#include <cstddef> | ||
#include "barretenberg/honk/composer/standard_honk_composer.hpp" | ||
#include "barretenberg/plonk/composer/standard_plonk_composer.hpp" | ||
|
||
#include "barretenberg/stdlib/encryption/ecdsa/ecdsa.hpp" | ||
#include "barretenberg/stdlib/hash/keccak/keccak.hpp" | ||
#include "barretenberg/stdlib/primitives/curves/secp256k1.hpp" | ||
|
@@ -41,11 +35,13 @@ struct BenchParams { | |
* @param composer | ||
* @param num_iterations | ||
*/ | ||
template <typename Composer> void generate_basic_arithmetic_circuit(Composer& composer, size_t num_gates) | ||
template <typename Builder> void generate_basic_arithmetic_circuit(Builder& builder, size_t num_gates) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it better to be more verbose and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prefer |
||
{ | ||
plonk::stdlib::field_t a(plonk::stdlib::witness_t(&composer, barretenberg::fr::random_element())); | ||
plonk::stdlib::field_t b(plonk::stdlib::witness_t(&composer, barretenberg::fr::random_element())); | ||
plonk::stdlib::field_t c(&composer); | ||
proof_system::plonk::stdlib::field_t a( | ||
proof_system::plonk::stdlib::witness_t(&builder, barretenberg::fr::random_element())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was some |
||
proof_system::plonk::stdlib::field_t b( | ||
proof_system::plonk::stdlib::witness_t(&builder, barretenberg::fr::random_element())); | ||
proof_system::plonk::stdlib::field_t c(&builder); | ||
for (size_t i = 0; i < (num_gates / 4) - 4; ++i) { | ||
c = a + b; | ||
c = a * c; | ||
|
@@ -57,47 +53,47 @@ template <typename Composer> void generate_basic_arithmetic_circuit(Composer& co | |
/** | ||
* @brief Generate test circuit with specified number of sha256 hashes | ||
* | ||
* @param composer | ||
* @param builder | ||
* @param num_iterations | ||
*/ | ||
template <typename Composer> void generate_sha256_test_circuit(Composer& composer, size_t num_iterations) | ||
template <typename Builder> void generate_sha256_test_circuit(Builder& builder, size_t num_iterations) | ||
{ | ||
std::string in; | ||
in.resize(32); | ||
for (size_t i = 0; i < 32; ++i) { | ||
in[i] = 0; | ||
} | ||
proof_system::plonk::stdlib::packed_byte_array<Composer> input(&composer, in); | ||
proof_system::plonk::stdlib::packed_byte_array<Builder> input(&builder, in); | ||
for (size_t i = 0; i < num_iterations; i++) { | ||
input = proof_system::plonk::stdlib::sha256<Composer>(input); | ||
input = proof_system::plonk::stdlib::sha256<Builder>(input); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Generate test circuit with specified number of keccak hashes | ||
* | ||
* @param composer | ||
* @param builder | ||
* @param num_iterations | ||
*/ | ||
template <typename Composer> void generate_keccak_test_circuit(Composer& composer, size_t num_iterations) | ||
template <typename Builder> void generate_keccak_test_circuit(Builder& builder, size_t num_iterations) | ||
{ | ||
std::string in = "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz01"; | ||
|
||
proof_system::plonk::stdlib::byte_array<Composer> input(&composer, in); | ||
proof_system::plonk::stdlib::byte_array<Builder> input(&builder, in); | ||
for (size_t i = 0; i < num_iterations; i++) { | ||
input = proof_system::plonk::stdlib::keccak<Composer>::hash(input); | ||
input = proof_system::plonk::stdlib::keccak<Builder>::hash(input); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Generate test circuit with specified number of ecdsa verifications | ||
* | ||
* @param composer | ||
* @param builder | ||
* @param num_iterations | ||
*/ | ||
template <typename Composer> void generate_ecdsa_verification_test_circuit(Composer& composer, size_t num_iterations) | ||
template <typename Builder> void generate_ecdsa_verification_test_circuit(Builder& builder, size_t num_iterations) | ||
{ | ||
using curve = proof_system::plonk::stdlib::secp256k1<Composer>; | ||
using curve = proof_system::plonk::stdlib::secp256k1<Builder>; | ||
using fr = typename curve::fr; | ||
using fq = typename curve::fq; | ||
using g1 = typename curve::g1; | ||
|
@@ -115,22 +111,23 @@ template <typename Composer> void generate_ecdsa_verification_test_circuit(Compo | |
|
||
bool first_result = | ||
crypto::ecdsa::verify_signature<Sha256Hasher, fq, fr, g1>(message_string, account.public_key, signature); | ||
static_cast<void>(first_result); // TODO(Cody): This is not used anywhere. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've started using |
||
|
||
std::vector<uint8_t> rr(signature.r.begin(), signature.r.end()); | ||
std::vector<uint8_t> ss(signature.s.begin(), signature.s.end()); | ||
uint8_t vv = signature.v; | ||
|
||
typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&composer, account.public_key); | ||
typename curve::g1_bigfr_ct public_key = curve::g1_bigfr_ct::from_witness(&builder, account.public_key); | ||
|
||
proof_system::plonk::stdlib::ecdsa::signature<Composer> sig{ typename curve::byte_array_ct(&composer, rr), | ||
typename curve::byte_array_ct(&composer, ss), | ||
proof_system::plonk::stdlib::uint8<Composer>( | ||
&composer, vv) }; | ||
proof_system::plonk::stdlib::ecdsa::signature<Builder> sig{ typename curve::byte_array_ct(&builder, rr), | ||
typename curve::byte_array_ct(&builder, ss), | ||
proof_system::plonk::stdlib::uint8<Builder>( | ||
&builder, vv) }; | ||
|
||
typename curve::byte_array_ct message(&composer, message_string); | ||
typename curve::byte_array_ct message(&builder, message_string); | ||
|
||
// Verify ecdsa signature | ||
proof_system::plonk::stdlib::ecdsa::verify_signature<Composer, | ||
proof_system::plonk::stdlib::ecdsa::verify_signature<Builder, | ||
curve, | ||
typename curve::fq_ct, | ||
typename curve::bigfr_ct, | ||
|
@@ -141,15 +138,15 @@ template <typename Composer> void generate_ecdsa_verification_test_circuit(Compo | |
/** | ||
* @brief Generate test circuit with specified number of merkle membership checks | ||
* | ||
* @param composer | ||
* @param builder | ||
* @param num_iterations | ||
*/ | ||
template <typename Composer> void generate_merkle_membership_test_circuit(Composer& composer, size_t num_iterations) | ||
template <typename Builder> void generate_merkle_membership_test_circuit(Builder& builder, size_t num_iterations) | ||
{ | ||
using namespace proof_system::plonk::stdlib; | ||
using field_ct = field_t<Composer>; | ||
using witness_ct = witness_t<Composer>; | ||
using witness_ct = witness_t<Composer>; | ||
using field_ct = field_t<Builder>; | ||
using witness_ct = witness_t<Builder>; | ||
using witness_ct = witness_t<Builder>; | ||
using MemStore = merkle_tree::MemoryStore; | ||
using MerkleTree_ct = merkle_tree::MerkleTree<MemStore>; | ||
|
||
|
@@ -163,12 +160,12 @@ template <typename Composer> void generate_merkle_membership_test_circuit(Compos | |
size_t value = i * 2; | ||
merkle_tree.update_element(idx, value); | ||
|
||
field_ct root_ct = witness_ct(&composer, merkle_tree.root()); | ||
auto idx_ct = field_ct(witness_ct(&composer, fr(idx))).decompose_into_bits(); | ||
field_ct root_ct = witness_ct(&builder, merkle_tree.root()); | ||
auto idx_ct = field_ct(witness_ct(&builder, fr(idx))).decompose_into_bits(); | ||
auto value_ct = field_ct(value); | ||
|
||
merkle_tree::check_membership( | ||
root_ct, merkle_tree::create_witness_hash_path(composer, merkle_tree.get_hash_path(idx)), value_ct, idx_ct); | ||
root_ct, merkle_tree::create_witness_hash_path(builder, merkle_tree.get_hash_path(idx)), value_ct, idx_ct); | ||
} | ||
} | ||
|
||
|
@@ -177,21 +174,25 @@ template <typename Composer> void generate_merkle_membership_test_circuit(Compos | |
* | ||
* @details This function assumes state.range refers to num_gates which is the size of the underlying circuit | ||
* | ||
* @tparam Composer | ||
* @tparam Builder | ||
* @param state | ||
* @param test_circuit_function | ||
*/ | ||
template <typename Composer> | ||
void construct_proof_with_specified_num_gates(State& state, void (*test_circuit_function)(Composer&, size_t)) noexcept | ||
void construct_proof_with_specified_num_gates(State& state, | ||
void (*test_circuit_function)(typename Composer::CircuitConstructor&, | ||
size_t)) noexcept | ||
{ | ||
barretenberg::srs::init_crs_factory("../srs_db/ignition"); | ||
auto num_gates = static_cast<size_t>(1 << (size_t)state.range(0)); | ||
for (auto _ : state) { | ||
// Constuct circuit and prover; don't include this part in measurement | ||
state.PauseTiming(); | ||
auto builder = typename Composer::CircuitConstructor(); | ||
test_circuit_function(builder, num_gates); | ||
|
||
auto composer = Composer(); | ||
test_circuit_function(composer, num_gates); | ||
auto ext_prover = composer.create_prover(); | ||
auto ext_prover = composer.create_prover(builder); | ||
state.ResumeTiming(); | ||
|
||
// Construct proof | ||
|
@@ -205,22 +206,25 @@ void construct_proof_with_specified_num_gates(State& state, void (*test_circuit_ | |
* @details This function assumes state.range refers to num_iterations which is the number of times to perform a given | ||
* basic operation in the circuit, e.g. number of hashes | ||
* | ||
* @tparam Composer | ||
* @tparam Builder | ||
* @param state | ||
* @param test_circuit_function | ||
*/ | ||
template <typename Composer> | ||
void construct_proof_with_specified_num_iterations(State& state, | ||
void (*test_circuit_function)(Composer&, size_t)) noexcept | ||
void (*test_circuit_function)(typename Composer::CircuitConstructor&, | ||
size_t)) noexcept | ||
{ | ||
barretenberg::srs::init_crs_factory("../srs_db/ignition"); | ||
auto num_iterations = static_cast<size_t>(state.range(0)); | ||
for (auto _ : state) { | ||
// Constuct circuit and prover; don't include this part in measurement | ||
state.PauseTiming(); | ||
auto builder = typename Composer::CircuitConstructor(); | ||
test_circuit_function(builder, num_iterations); | ||
|
||
auto composer = Composer(); | ||
test_circuit_function(composer, num_iterations); | ||
auto ext_prover = composer.create_prover(); | ||
auto ext_prover = composer.create_prover(builder); | ||
state.ResumeTiming(); | ||
|
||
// Construct proof | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
#include "barretenberg/ecc/curves/bn254/fr.hpp" | ||
#include <benchmark/benchmark.h> | ||
#include <cstddef> | ||
#include "barretenberg/honk/composer/standard_honk_composer.hpp" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming and moving comes in the next PR. |
||
#include "barretenberg/honk/composer/composer_helper/standard_honk_composer_helper.hpp" | ||
#include "barretenberg/stdlib/primitives/field/field.hpp" | ||
#include "barretenberg/stdlib/primitives/witness/witness.hpp" | ||
|
||
|
@@ -10,18 +9,20 @@ using namespace proof_system::plonk::stdlib; | |
|
||
namespace standard_honk_bench { | ||
|
||
using Composer = proof_system::honk::StandardHonkComposer; | ||
using Builder = proof_system::StandardCircuitConstructor; | ||
using Composer = proof_system::honk::StandardHonkComposerHelper; | ||
|
||
constexpr size_t MIN_LOG_NUM_GATES = 16; | ||
constexpr size_t MAX_LOG_NUM_GATES = 16; | ||
// To get good statistics, number of Repetitions must be sufficient. ~30 Repetitions gives good results. | ||
constexpr size_t NUM_REPETITIONS = 5; | ||
|
||
void generate_test_circuit(auto& composer, size_t num_gates) | ||
void generate_test_circuit(auto& builder, size_t num_gates) | ||
{ | ||
field_t a(witness_t(&composer, barretenberg::fr::random_element())); | ||
field_t b(witness_t(&composer, barretenberg::fr::random_element())); | ||
field_t c(&composer); | ||
barretenberg::srs::init_crs_factory("../srs_db/ignition"); | ||
field_t a(witness_t(&builder, barretenberg::fr::random_element())); | ||
field_t b(witness_t(&builder, barretenberg::fr::random_element())); | ||
field_t c(&builder); | ||
for (size_t i = 0; i < (num_gates / 4) - 4; ++i) { | ||
c = a + b; | ||
c = a * c; | ||
|
@@ -38,11 +39,12 @@ void create_prover_standard(State& state) noexcept | |
for (auto _ : state) { | ||
state.PauseTiming(); | ||
auto num_gates = 1 << (size_t)state.range(0); | ||
auto composer = Composer(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(composer, static_cast<size_t>(num_gates)); | ||
auto builder = Builder(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(builder, static_cast<size_t>(num_gates)); | ||
state.ResumeTiming(); | ||
|
||
composer.create_prover(); | ||
auto composer = Composer(); | ||
composer.create_prover(builder); | ||
} | ||
} | ||
BENCHMARK(create_prover_standard)->DenseRange(MIN_LOG_NUM_GATES, MAX_LOG_NUM_GATES, 1)->Repetitions(NUM_REPETITIONS); | ||
|
@@ -55,9 +57,11 @@ void construct_proof_standard(State& state) noexcept | |
auto num_gates = 1 << (size_t)state.range(0); | ||
for (auto _ : state) { | ||
state.PauseTiming(); | ||
auto composer = Composer(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(composer, static_cast<size_t>(num_gates)); | ||
auto ext_prover = composer.create_prover(); | ||
auto builder = Builder(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(builder, static_cast<size_t>(num_gates)); | ||
|
||
auto composer = Composer(); | ||
auto ext_prover = composer.create_prover(builder); | ||
state.ResumeTiming(); | ||
|
||
auto proof = ext_prover.construct_proof(); | ||
|
@@ -77,11 +81,12 @@ void create_verifier_standard(State& state) noexcept | |
for (auto _ : state) { | ||
state.PauseTiming(); | ||
auto num_gates = 1 << (size_t)state.range(0); | ||
auto composer = Composer(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(composer, static_cast<size_t>(num_gates)); | ||
auto builder = Builder(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(builder, static_cast<size_t>(num_gates)); | ||
state.ResumeTiming(); | ||
|
||
composer.create_verifier(); | ||
auto composer = Composer(); | ||
composer.create_verifier(builder); | ||
} | ||
} | ||
// BENCHMARK(create_verifier_standard)->DenseRange(MIN_LOG_NUM_GATES, MAX_LOG_NUM_GATES, | ||
|
@@ -95,11 +100,13 @@ void verify_proof_standard(State& state) noexcept | |
for (auto _ : state) { | ||
state.PauseTiming(); | ||
auto num_gates = (size_t)state.range(0); | ||
auto composer = Composer(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(composer, static_cast<size_t>(num_gates)); | ||
auto prover = composer.create_prover(); | ||
auto builder = Builder(static_cast<size_t>(num_gates)); | ||
generate_test_circuit(builder, static_cast<size_t>(num_gates)); | ||
|
||
auto composer = Composer(); | ||
auto prover = composer.create_prover(builder); | ||
auto proof = prover.construct_proof(); | ||
auto verifier = composer.create_verifier(); | ||
auto verifier = composer.create_verifier(builder); | ||
state.ResumeTiming(); | ||
|
||
verifier.verify_proof(proof); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this option before to avoid a warning we had, but now we don't need to avoid that warning and I added the option incorrectly anyway.