diff --git a/build-system b/build-system index 819a987a489d..1a8c6c121173 160000 --- a/build-system +++ b/build-system @@ -1 +1 @@ -Subproject commit 819a987a489d1313d7fd5906dfa490ab36a95d43 +Subproject commit 1a8c6c121173172691a08e2ecd5dfb210418400a diff --git a/circuits/cpp/barretenberg/cpp/bootstrap.sh b/circuits/cpp/barretenberg/cpp/bootstrap.sh index e72bd661e18f..22419e1aecae 100755 --- a/circuits/cpp/barretenberg/cpp/bootstrap.sh +++ b/circuits/cpp/barretenberg/cpp/bootstrap.sh @@ -48,6 +48,12 @@ echo "#################################" cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert cmake --build --preset $PRESET ${@/#/--target } +cd ./build +# The Grumpkin SRS is generated manually at the moment only up to a large enough size for tests +cmake --build . --parallel --target grumpkin_srs_gen +./bin/grumpkin_srs_gen 4096 +echo "Generated Grumpkin SRS successfully" + # Install wasi-sdk. ./scripts/install-wasi-sdk.sh diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp index ad4b89132a4e..0889bd2b4e26 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_format/acir_format.hpp @@ -40,7 +40,9 @@ struct acir_format { // A standard plonk arithmetic constraint, as defined in the poly_triple struct, consists of selector values // for q_M,q_L,q_R,q_O,q_C and indices of three variables taking the role of left, right and output wire // This could be a large vector so use slab allocator, we don't expect the blackbox implementations to be so large. - std::vector> constraints; + std::vector, + ContainerSlabAllocator>> + constraints; std::vector block_constraints; // For serialization, update with any new fields diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp index 159b8f3b4528..29463a06db7f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.cpp @@ -31,8 +31,9 @@ void AcirComposer::create_circuit(acir_format::acir_format& constraint_system) size_hint_ = circuit_subgroup_size_; } -void AcirComposer::init_proving_key(std::shared_ptr const& crs_factory, - acir_format::acir_format& constraint_system) +void AcirComposer::init_proving_key( + std::shared_ptr> const& crs_factory, + acir_format::acir_format& constraint_system) { vinfo("building circuit... ", size_hint_); builder_ = acir_format::Builder(size_hint_); @@ -52,7 +53,7 @@ void AcirComposer::init_proving_key(std::shared_ptr AcirComposer::create_proof( - std::shared_ptr const& crs_factory, + std::shared_ptr> const& crs_factory, acir_format::acir_format& constraint_system, acir_format::WitnessVector& witness, bool is_recursive) @@ -107,8 +108,9 @@ std::shared_ptr AcirComposer::init_verifi return verification_key_; } -void AcirComposer::load_verification_key(std::shared_ptr const& crs_factory, - proof_system::plonk::verification_key_data&& data) +void AcirComposer::load_verification_key( + std::shared_ptr> const& crs_factory, + proof_system::plonk::verification_key_data&& data) { verification_key_ = std::make_shared(std::move(data), crs_factory->get_verifier_crs()); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp index 3b25c26f2dc9..71f91093d5e6 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/dsl/acir_proofs/acir_composer.hpp @@ -13,16 +13,18 @@ class AcirComposer { void create_circuit(acir_format::acir_format& constraint_system); - void init_proving_key(std::shared_ptr const& crs_factory, + void init_proving_key(std::shared_ptr> const& crs_factory, acir_format::acir_format& constraint_system); - std::vector create_proof(std::shared_ptr const& crs_factory, - acir_format::acir_format& constraint_system, - acir_format::WitnessVector& witness, - bool is_recursive); + std::vector create_proof( + std::shared_ptr> const& crs_factory, + acir_format::acir_format& constraint_system, + acir_format::WitnessVector& witness, + bool is_recursive); - void load_verification_key(std::shared_ptr const& crs_factory, - proof_system::plonk::verification_key_data&& data); + void load_verification_key( + std::shared_ptr> const& crs_factory, + proof_system::plonk::verification_key_data&& data); std::shared_ptr init_verification_key(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp index cd575a16d608..fc799acce556 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp @@ -22,7 +22,7 @@ struct GrumpkinG1Params { }; static constexpr barretenberg::fr a{ 0UL, 0UL, 0UL, 0UL }; - // generator point = (x, y) = (1, sqrt(-15)) + // generator point = (x, y) = (1, sqrt(-16)), sqrt(-16) = 4i static constexpr barretenberg::fr one_x = barretenberg::fr::one(); static constexpr barretenberg::fr one_y{ 0x11b2dff1448c41d8UL, 0x23d3446f21c77dc3UL, 0xaa7b8cf435dfafbbUL, 0x14b34cf69dc25d68UL diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp index a477ccb00a6a..19af6aa0d691 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.cpp @@ -20,7 +20,7 @@ void build_circuit(Builder& builder) } BuilderComposerPtrs create_builder_and_composer( - std::shared_ptr const& crs_factory) + std::shared_ptr> const& crs_factory) { // WARNING: Size hint is essential to perform 512k circuits! auto builder = std::make_unique(CIRCUIT_SIZE); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp index 8915a3f8283a..0932708bf216 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.hpp @@ -13,7 +13,7 @@ struct BuilderComposerPtrs { }; BuilderComposerPtrs create_builder_and_composer( - std::shared_ptr const& crs_factory); + std::shared_ptr> const& crs_factory); proof create_proof(BuilderComposerPtrs pair); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp index 16399f08d4ee..a43c3de45033 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/examples/simple/simple.test.cpp @@ -8,7 +8,7 @@ namespace examples::simple { TEST(examples_simple, create_proof) { auto srs_path = std::filesystem::absolute("../srs_db/ignition"); - auto crs_factory = std::make_shared(srs_path); + auto crs_factory = std::make_shared>(srs_path); auto ptrs = create_builder_and_composer(crs_factory); auto proof = create_proof(ptrs); bool valid = verify_proof(ptrs, proof); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/grumpkin_srs_gen/grumpkin_srs_gen.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/grumpkin_srs_gen/grumpkin_srs_gen.cpp index 4f5ac394431d..a04606b383ae 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/grumpkin_srs_gen/grumpkin_srs_gen.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/grumpkin_srs_gen/grumpkin_srs_gen.cpp @@ -11,7 +11,9 @@ const std::string protocol_name = "BARRETENBERG_GRUMPKIN_IPA_CRS"; /** * @brief Generates a monomial basis Grumpkin SRS. * - * @details We only provide functionality create a single transcript file. + * @details We only provide functionality to create a single transcript file. + * ! Note that, unlike the bn254 SRS, the first element of the Grumpkin SRS will not be equal to point one defined in + * grumpkin.hpp as this function finds Grumpkin points in an arbitrary order. * */ int main(int argc, char** argv) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.hpp index 7119fb86b695..8bf37eae502f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.hpp @@ -26,7 +26,7 @@ template class StandardComposer_ { std::shared_ptr verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr crs_factory_; + std::shared_ptr> crs_factory_; // The commitment key is passed to the prover but also used herein to compute the verfication key commitments std::shared_ptr commitment_key; @@ -37,15 +37,22 @@ template class StandardComposer_ { bool computed_witness = false; // TODO(Luke): use make_shared + // TODO(#637): design the crs factory better StandardComposer_() - : StandardComposer_( - std::shared_ptr(new srs::factories::FileCrsFactory("../srs_db/ignition"))) - {} - StandardComposer_(std::shared_ptr crs_factory) + { + if constexpr (IsGrumpkinFlavor) { + crs_factory_ = barretenberg::srs::get_grumpkin_crs_factory(); + + } else { + crs_factory_ = barretenberg::srs::get_crs_factory(); + } + } + + StandardComposer_(std::shared_ptr> crs_factory) : crs_factory_(std::move(crs_factory)) {} - StandardComposer_(std::unique_ptr&& crs_factory) + StandardComposer_(std::unique_ptr>&& crs_factory) : crs_factory_(std::move(crs_factory)) {} StandardComposer_(std::shared_ptr p_key, std::shared_ptr v_key) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.test.cpp index 3f3043d09dd8..21872ce863fd 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/standard_composer.test.cpp @@ -15,13 +15,30 @@ using namespace proof_system::honk; +#define TYPE_ALIASES \ + using Flavor = TypeParam; \ + using FF = typename Flavor::FF; \ + using CircuitBuilder = StandardCircuitBuilder_; \ + using Composer = StandardComposer_; + namespace test_standard_honk_composer { -class StandardHonkComposerTests : public ::testing::Test { +template class StandardHonkComposerTests : public ::testing::Test { protected: - static void SetUpTestSuite() { barretenberg::srs::init_crs_factory("../srs_db/ignition"); } + // TODO(640): The Standard Honk on Grumpkin test suite fails unless the SRS is initialised for every test. + virtual void SetUp() + { + if constexpr (IsGrumpkinFlavor) { + barretenberg::srs::init_grumpkin_crs_factory("../srs_db/grumpkin"); + } else { + barretenberg::srs::init_crs_factory("../srs_db/ignition"); + } + }; }; +using FlavorTypes = ::testing::Types; +TYPED_TEST_SUITE(StandardHonkComposerTests, FlavorTypes); + /** * @brief The goal of this test is to check that the sigma permutation vectors for honk are generated correctly. * @@ -30,9 +47,10 @@ class StandardHonkComposerTests : public ::testing::Test { * 2) That if the permutation argument is computed with witness values, the values from the identity permutation and * sigma permutation are equal */ -TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) +TYPED_TEST(StandardHonkComposerTests, SigmaIDCorrectness) { - auto test_permutation = [](StandardCircuitBuilder& circuit_constructor, StandardComposer& composer) { + TYPE_ALIASES + auto test_permutation = [](CircuitBuilder& circuit_constructor, Composer& composer) { auto prover = composer.create_prover(circuit_constructor); auto proving_key = prover.key; @@ -43,11 +61,11 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) auto num_gates = circuit_constructor.get_num_gates(); // Using the same random beta and gamma as in the permutation argument - barretenberg::fr beta = barretenberg::fr::random_element(); - barretenberg::fr gamma = barretenberg::fr::random_element(); + FF beta = FF::random_element(); + FF gamma = FF::random_element(); - barretenberg::fr left = barretenberg::fr::one(); - barretenberg::fr right = barretenberg::fr::one(); + FF left = FF::one(); + FF right = FF::one(); // Let's check that indices are the same and nothing is lost, first size_t wire_idx = 0; @@ -60,7 +78,7 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) // and fix the cycle by adding the extra terms if (wire_idx == 0) { for (size_t i = 0; i < num_public_inputs; ++i) { - EXPECT_EQ(sigma_polynomial[i], -fr(i + 1)); + EXPECT_EQ(sigma_polynomial[i], -FF(i + 1)); left *= (gamma - (i + 1)); right *= (gamma + (n + i)); } @@ -70,13 +88,13 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) EXPECT_EQ(left, right); - left = barretenberg::fr::one(); - right = barretenberg::fr::one(); + left = FF::one(); + right = FF::one(); auto permutation_polynomials = proving_key->get_sigma_polynomials(); auto id_polynomials = proving_key->get_id_polynomials(); auto wire_polynomials = proving_key->get_wires(); - for (size_t j = 0; j < StandardComposer::NUM_WIRES; ++j) { + for (size_t j = 0; j < Composer::NUM_WIRES; ++j) { std::string index = std::to_string(j + 1); const auto& permutation_polynomial = permutation_polynomials[j]; const auto& witness_polynomial = wire_polynomials[j]; @@ -103,7 +121,7 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) } // test correctness of the public input delta - auto delta = proof_system::honk::compute_public_input_delta(public_inputs, beta, gamma, n); + auto delta = proof_system::honk::compute_public_input_delta(public_inputs, beta, gamma, n); EXPECT_EQ(left / right, delta); for (size_t i = 0; i < num_public_inputs; ++i) { @@ -113,29 +131,29 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) EXPECT_EQ(left, right); }; - auto circuit_constructor = StandardCircuitBuilder(); - fr a = fr::one(); + auto circuit_constructor = CircuitBuilder(); + FF a = FF::one(); uint32_t a_idx = circuit_constructor.add_variable(a); - fr b = fr::one(); - fr c = a + b; + FF b = FF::one(); + FF c = a + b; uint32_t b_idx = circuit_constructor.add_variable(b); uint32_t c_idx = circuit_constructor.add_variable(c); - fr d = a + c; + FF d = a + c; uint32_t d_idx = circuit_constructor.add_public_variable(d); uint32_t e_idx = circuit_constructor.put_constant_variable(d); circuit_constructor.assert_equal(e_idx, d_idx, ""); - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); - circuit_constructor.create_add_gate({ d_idx, c_idx, a_idx, fr::one(), fr::neg_one(), fr::neg_one(), fr::zero() }); - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); - circuit_constructor.create_add_gate({ b_idx, a_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); + circuit_constructor.create_add_gate({ d_idx, c_idx, a_idx, FF::one(), FF::neg_one(), FF::neg_one(), FF::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); + circuit_constructor.create_add_gate({ b_idx, a_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); for (size_t i = 0; i < 30; ++i) { - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); } - auto composer = StandardComposer(); + auto composer = Composer(); test_permutation(circuit_constructor, composer); } @@ -143,38 +161,40 @@ TEST_F(StandardHonkComposerTests, SigmaIDCorrectness) * @brief Check the correctness of lagrange polynomials generated during proving key computation * */ -TEST_F(StandardHonkComposerTests, LagrangeCorrectness) +TYPED_TEST(StandardHonkComposerTests, LagrangeCorrectness) { + TYPE_ALIASES + using Polynomial = typename Flavor::Polynomial; // Create a dummy circuit with a few gates - auto circuit_constructor = StandardCircuitBuilder(); - fr a = fr::one(); + auto circuit_constructor = CircuitBuilder(); + FF a = FF::one(); uint32_t a_idx = circuit_constructor.add_variable(a); - fr b = fr::one(); - fr c = a + b; - fr d = a + c; + FF b = FF::one(); + FF c = a + b; + FF d = a + c; uint32_t b_idx = circuit_constructor.add_variable(b); uint32_t c_idx = circuit_constructor.add_variable(c); uint32_t d_idx = circuit_constructor.add_variable(d); for (size_t i = 0; i < 16; i++) { - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); circuit_constructor.create_add_gate( - { d_idx, c_idx, a_idx, fr::one(), fr::neg_one(), fr::neg_one(), fr::zero() }); + { d_idx, c_idx, a_idx, FF::one(), FF::neg_one(), FF::neg_one(), FF::zero() }); } // Generate proving key - auto composer = StandardComposer(); + auto composer = Composer(); auto prover = composer.create_prover(circuit_constructor); auto proving_key = prover.key; // Generate a random polynomial - barretenberg::polynomial random_polynomial = barretenberg::polynomial(proving_key->circuit_size); + Polynomial random_polynomial = Polynomial(proving_key->circuit_size); for (size_t i = 0; i < proving_key->circuit_size; i++) { - random_polynomial[i] = barretenberg::fr::random_element(); + random_polynomial[i] = FF::random_element(); } // Compute inner product of random polynomial and the first lagrange polynomial - barretenberg::polynomial first_lagrange_polynomial = proving_key->lagrange_first; - barretenberg::fr first_product(0); + Polynomial first_lagrange_polynomial = proving_key->lagrange_first; + FF first_product(0); for (size_t i = 0; i < proving_key->circuit_size; i++) { first_product += random_polynomial[i] * first_lagrange_polynomial[i]; } @@ -182,7 +202,7 @@ TEST_F(StandardHonkComposerTests, LagrangeCorrectness) // Compute inner product of random polynomial and the last lagrange polynomial auto last_lagrange_polynomial = proving_key->lagrange_last; - barretenberg::fr last_product(0); + FF last_product(0); for (size_t i = 0; i < proving_key->circuit_size; i++) { last_product += random_polynomial[i] * last_lagrange_polynomial[i]; } @@ -197,26 +217,27 @@ TEST_F(StandardHonkComposerTests, LagrangeCorrectness) * merged. * In this test we create two almost identical circuits. They differ because one */ -TEST_F(StandardHonkComposerTests, AssertEquals) +TYPED_TEST(StandardHonkComposerTests, AssertEquals) { + TYPE_ALIASES /** * @brief A function that creates a simple circuit with repeated gates, leading to large permutation cycles * */ auto create_simple_circuit = [](auto& circuit_constructor) { - fr a = fr::one(); + FF a = FF::one(); uint32_t a_idx = circuit_constructor.add_variable(a); - fr b = fr::one(); - fr c = a + b; + FF b = FF::one(); + FF c = a + b; uint32_t b_idx = circuit_constructor.add_variable(b); uint32_t c_idx = circuit_constructor.add_variable(c); for (size_t i = 0; i < 10; i++) { circuit_constructor.create_add_gate( - { a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + { a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); circuit_constructor.create_add_gate( - { b_idx, a_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + { b_idx, a_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); } return std::make_tuple(a_idx, b_idx); }; @@ -256,7 +277,8 @@ TEST_F(StandardHonkComposerTests, AssertEquals) size_t cycle_length = 1; visited_indices[i] = true; - // Jump through the cycle untill we reach the start or the permutation length exceeds the possible maximum + // Jump through the cycle untill we reach the start or the permutation length exceeds the possible + // maximum while ((next_element != starting_element) && cycle_length < (permutation_length + 1)) { // Update cycle length and visited index infromation cycle_length++; @@ -280,8 +302,8 @@ TEST_F(StandardHonkComposerTests, AssertEquals) }; // Get 2 circuits - auto circuit_constructor_no_assert_equal = StandardCircuitBuilder(); - auto circuit_constructor_with_assert_equal = StandardCircuitBuilder(); + auto circuit_constructor_no_assert_equal = CircuitBuilder(); + auto circuit_constructor_with_assert_equal = CircuitBuilder(); // Construct circuits create_simple_circuit(circuit_constructor_no_assert_equal); @@ -293,47 +315,49 @@ TEST_F(StandardHonkComposerTests, AssertEquals) "Equality asssertion in standard honk composer test"); // Check that the maximum cycle in the one, where we used assert_equal, is twice as long - auto composer_no_assert_equal = StandardComposer(); - auto composer_with_assert_equal = StandardComposer(); + auto composer_no_assert_equal = Composer(); + auto composer_with_assert_equal = Composer(); EXPECT_EQ(get_maximum_cycle(circuit_constructor_with_assert_equal, composer_with_assert_equal), get_maximum_cycle(circuit_constructor_no_assert_equal, composer_no_assert_equal) * 2); } -TEST_F(StandardHonkComposerTests, VerificationKeyCreation) +TYPED_TEST(StandardHonkComposerTests, VerificationKeyCreation) { + TYPE_ALIASES // Create a composer and a dummy circuit with a few gates - auto circuit_constructor = StandardCircuitBuilder(); - fr a = fr::one(); + auto circuit_constructor = CircuitBuilder(); + FF a = FF::one(); uint32_t a_idx = circuit_constructor.add_variable(a); - fr b = fr::one(); - fr c = a + b; - fr d = a + c; + FF b = FF::one(); + FF c = a + b; + FF d = a + c; uint32_t b_idx = circuit_constructor.add_variable(b); uint32_t c_idx = circuit_constructor.add_variable(c); uint32_t d_idx = circuit_constructor.add_variable(d); for (size_t i = 0; i < 16; i++) { - circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + circuit_constructor.create_add_gate({ a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); circuit_constructor.create_add_gate( - { d_idx, c_idx, a_idx, fr::one(), fr::neg_one(), fr::neg_one(), fr::zero() }); + { d_idx, c_idx, a_idx, FF::one(), FF::neg_one(), FF::neg_one(), FF::zero() }); } - auto composer = StandardComposer(); + auto composer = Composer(); composer.create_prover(circuit_constructor); auto verification_key = composer.compute_verification_key(circuit_constructor); - // There is nothing we can really check apart from the fact that constraint selectors and permutation selectors were - // committed to, we simply check that the verification key now contains the appropriate number of constraint and - // permutation selector commitments. This method should work with any future arithemtization. + // There is nothing we can really check apart from the fact that constraint selectors and permutation selectors + // were committed to, we simply check that the verification key now contains the appropriate number of + // constraint and permutation selector commitments. This method should work with any future arithemtization. EXPECT_EQ(verification_key->size(), circuit_constructor.selectors.size() + composer.NUM_WIRES * 2 + 2); } -TEST_F(StandardHonkComposerTests, BaseCase) +TYPED_TEST(StandardHonkComposerTests, BaseCase) { - auto circuit_constructor = StandardCircuitBuilder(); - fr a = 1; + TYPE_ALIASES + auto circuit_constructor = CircuitBuilder(); + FF a = 1; circuit_constructor.add_variable(a); - auto composer = StandardComposer(); + auto composer = Composer(); auto prover = composer.create_prover(circuit_constructor); auto proof = prover.construct_proof(); auto verifier = composer.create_verifier(circuit_constructor); @@ -341,10 +365,11 @@ TEST_F(StandardHonkComposerTests, BaseCase) ASSERT_TRUE(verified); } -TEST_F(StandardHonkComposerTests, TwoGates) +TYPED_TEST(StandardHonkComposerTests, TwoGates) { + TYPE_ALIASES auto run_test = [](bool expect_verified) { - auto circuit_constructor = StandardCircuitBuilder(); + auto circuit_constructor = CircuitBuilder(); // 1 + 1 - 2 = 0 uint32_t w_l_1_idx; if (expect_verified) { @@ -362,7 +387,7 @@ TEST_F(StandardHonkComposerTests, TwoGates) uint32_t w_o_2_idx = circuit_constructor.add_variable(4); circuit_constructor.create_mul_gate({ w_l_2_idx, w_r_2_idx, w_o_2_idx, 1, -1, 0 }); - auto composer = StandardComposer(); + auto composer = Composer(); auto prover = composer.create_prover(circuit_constructor); auto proof = prover.construct_proof(); @@ -376,16 +401,17 @@ TEST_F(StandardHonkComposerTests, TwoGates) run_test(/* expect_verified=*/false); } -TEST_F(StandardHonkComposerTests, SumcheckEvaluations) +TYPED_TEST(StandardHonkComposerTests, SumcheckEvaluations) { + TYPE_ALIASES auto run_test = [](bool expected_result) { - auto circuit_constructor = StandardCircuitBuilder(); - fr a = fr::one(); + auto circuit_constructor = CircuitBuilder(); + FF a = FF::one(); // Construct a small but non-trivial circuit uint32_t a_idx = circuit_constructor.add_public_variable(a); - fr b = fr::one(); - fr c = a + b; - fr d = a + c; + FF b = FF::one(); + FF c = a + b; + FF d = a + c; if (expected_result == false) { d += 1; @@ -396,12 +422,12 @@ TEST_F(StandardHonkComposerTests, SumcheckEvaluations) uint32_t d_idx = circuit_constructor.add_variable(d); for (size_t i = 0; i < 16; i++) { circuit_constructor.create_add_gate( - { a_idx, b_idx, c_idx, fr::one(), fr::one(), fr::neg_one(), fr::zero() }); + { a_idx, b_idx, c_idx, FF::one(), FF::one(), FF::neg_one(), FF::zero() }); circuit_constructor.create_add_gate( - { d_idx, c_idx, a_idx, fr::one(), fr::neg_one(), fr::neg_one(), fr::zero() }); + { d_idx, c_idx, a_idx, FF::one(), FF::neg_one(), FF::neg_one(), FF::zero() }); } - auto composer = StandardComposer(); + auto composer = Composer(); auto prover = composer.create_prover(circuit_constructor); auto proof = prover.construct_proof(); auto verifier = composer.create_verifier(circuit_constructor); @@ -411,17 +437,4 @@ TEST_F(StandardHonkComposerTests, SumcheckEvaluations) run_test(/*expected_result=*/true); run_test(/*expected_result=*/false); } -TEST(StandardGrumpkinHonkComposer, BaseCase) -{ - auto circuit_constructor = StandardCircuitBuilder(); - fr a = 1; - circuit_constructor.add_variable(a); - - auto composer = StandardGrumpkinComposer(); - auto prover = composer.create_prover(circuit_constructor); - auto proof = prover.construct_proof(); - auto verifier = composer.create_verifier(circuit_constructor); - bool verified = verifier.verify_proof(proof); - ASSERT_TRUE(verified); -} } // namespace test_standard_honk_composer diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.hpp index c58cffec6ff7..dd1c9542828c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.hpp @@ -31,7 +31,7 @@ template class UltraComposer_ { std::shared_ptr verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr crs_factory_; + std::shared_ptr> crs_factory_; // The commitment key is passed to the prover but also used herein to compute the verfication key commitments std::shared_ptr commitment_key; @@ -46,10 +46,9 @@ template class UltraComposer_ { size_t num_public_inputs = 0; size_t num_ecc_op_gates = 0; - UltraComposer_() - : crs_factory_(barretenberg::srs::get_crs_factory()){}; + UltraComposer_() { crs_factory_ = barretenberg::srs::get_crs_factory(); } - explicit UltraComposer_(std::shared_ptr crs_factory) + explicit UltraComposer_(std::shared_ptr> crs_factory) : crs_factory_(std::move(crs_factory)) {} @@ -84,6 +83,8 @@ template class UltraComposer_ { }; }; extern template class UltraComposer_; +// TODO: the UltraGrumpkin flavor still works on BN254 because plookup needs to be templated to be able to construct +// Grumpkin circuits. extern template class UltraComposer_; extern template class UltraComposer_; // TODO(#532): this pattern is weird; is this not instantiating the templates? diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.test.cpp index 776711387227..9cc1b3b088fe 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/composer/ultra_composer.test.cpp @@ -274,8 +274,7 @@ TEST_F(UltraHonkComposerTests, test_elliptic_gate) uint32_t x3 = circuit_constructor.add_variable(p3.x); uint32_t y3 = circuit_constructor.add_variable(p3.y); - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, 1, 1 }); grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); affine_element p2_endo = p2; @@ -283,15 +282,13 @@ TEST_F(UltraHonkComposerTests, test_elliptic_gate) p3 = affine_element(element(p1) + element(p2_endo)); x3 = circuit_constructor.add_variable(p3.x); y3 = circuit_constructor.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta, 1 }); p2_endo.x *= beta; p3 = affine_element(element(p1) - element(p2_endo)); x3 = circuit_constructor.add_variable(p3.x); y3 = circuit_constructor.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }); auto composer = UltraComposer(); prove_and_verify(circuit_constructor, composer, /*expected_result=*/true); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp index b46613675d6b..8036f0541589 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp @@ -29,15 +29,15 @@ namespace proof_system::honk::flavor { class GoblinUltra { public: using CircuitBuilder = UltraCircuitBuilder; - using FF = barretenberg::fr; - using Polynomial = barretenberg::Polynomial; - using PolynomialHandle = std::span; - using G1 = barretenberg::g1; - using GroupElement = G1::element; - using Commitment = G1::affine_element; - using CommitmentHandle = G1::affine_element; using PCSParams = pcs::kzg::Params; using PCS = pcs::kzg::KZG; + using Curve = PCSParams::Curve; + using GroupElement = Curve::Element; + using Commitment = Curve::AffineElement; + using CommitmentHandle = Curve::AffineElement; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; + using PolynomialHandle = std::span; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard.hpp index 9fc599cf051a..d7e5411603f4 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard.hpp @@ -32,15 +32,15 @@ namespace proof_system::honk::flavor { class Standard { public: using CircuitBuilder = StandardCircuitBuilder; - using FF = barretenberg::fr; - using Polynomial = barretenberg::Polynomial; - using PolynomialHandle = std::span; - using G1 = barretenberg::g1; - using GroupElement = G1::element; - using Commitment = G1::affine_element; - using CommitmentHandle = G1::affine_element; using PCSParams = pcs::kzg::Params; using PCS = pcs::kzg::KZG; + using Curve = PCSParams::Curve; + using GroupElement = Curve::Element; + using Commitment = Curve::AffineElement; + using CommitmentHandle = Curve::AffineElement; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; + using PolynomialHandle = std::span; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard_grumpkin.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard_grumpkin.hpp index 9a7ecc19df0a..400e49bb55c6 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard_grumpkin.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/standard_grumpkin.hpp @@ -23,16 +23,16 @@ class StandardGrumpkin { // TODO(Mara): At the moment this class is a duplicate of the Standard flavor with a different PCS for testing // purposes. This will be changed to Grumpkin once generating Honk proofs over Grumpkin has been enabled. public: - using CircuitBuilder = StandardCircuitBuilder; - using FF = barretenberg::fr; - using Polynomial = barretenberg::Polynomial; - using PolynomialHandle = std::span; - using G1 = barretenberg::g1; - using GroupElement = G1::element; - using Commitment = G1::affine_element; - using CommitmentHandle = G1::affine_element; + using CircuitBuilder = StandardGrumpkinCircuitBuilder; using PCSParams = pcs::ipa::Params; using PCS = pcs::ipa::IPA; + using Curve = PCSParams::Curve; + using GroupElement = Curve::Element; + using Commitment = Curve::AffineElement; + using CommitmentHandle = Curve::AffineElement; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; + using PolynomialHandle = std::span; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often // need containers of this size to hold related data, so we choose a name more agnostic than `NUM_POLYNOMIALS` diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp index 7691c8a666b9..a87550f8146f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp @@ -28,18 +28,15 @@ namespace proof_system::honk::flavor { class Ultra { public: using CircuitBuilder = UltraCircuitBuilder; - using FF = barretenberg::fr; - using Polynomial = barretenberg::Polynomial; - using PolynomialHandle = std::span; - using G1 = barretenberg::g1; - using GroupElement = G1::element; - using Commitment = G1::affine_element; - using CommitmentHandle = G1::affine_element; - // UltraHonk will be run with KZG by default but temporarily we set the commitment to IPA to - // be able to do e2e tests with this pcs as well - // TODO: instantiate this with both IPA and KZG when the templating work is finished using PCSParams = pcs::kzg::Params; using PCS = pcs::kzg::KZG; + using Curve = PCSParams::Curve; + using GroupElement = Curve::Element; + using Commitment = Curve::AffineElement; + using CommitmentHandle = Curve::AffineElement; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; + using PolynomialHandle = std::span; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_grumpkin.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_grumpkin.hpp index 3ac884b4a5b1..c949a4cb3439 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_grumpkin.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_grumpkin.hpp @@ -2,6 +2,7 @@ #include "barretenberg/ecc/curves/bn254/g1.hpp" #include "barretenberg/honk/pcs/commitment_key.hpp" #include "barretenberg/honk/pcs/ipa/ipa.hpp" +#include "barretenberg/honk/pcs/kzg/kzg.hpp" #include "barretenberg/honk/sumcheck/polynomials/barycentric_data.hpp" #include "barretenberg/honk/sumcheck/polynomials/univariate.hpp" #include "barretenberg/honk/sumcheck/relations/auxiliary_relation.hpp" @@ -25,19 +26,19 @@ namespace proof_system::honk::flavor { class UltraGrumpkin { - // TODO(Mara): At the moment this class is a duplicate of the Standard flavor with a different PCS for testing - // purposes. This will be changed to Grumpkin once generating Honk proofs over Grumpkin has been enabled. + // TODO(#636): At the moment this class is a duplicate of the Ultra flavor with a different PCS for testing + // purposes. This can be changed to Grumpkin and IPA once UltraCircuitBuilder also works on Grumpkin. public: using CircuitBuilder = UltraCircuitBuilder; - using FF = barretenberg::fr; + using PCSParams = pcs::kzg::Params; + using PCS = pcs::kzg::KZG; + using Curve = PCSParams::Curve; + using GroupElement = Curve::Element; + using Commitment = Curve::AffineElement; + using CommitmentHandle = Curve::AffineElement; + using FF = Curve::ScalarField; using Polynomial = barretenberg::Polynomial; using PolynomialHandle = std::span; - using G1 = barretenberg::g1; - using GroupElement = G1::element; - using Commitment = G1::affine_element; - using CommitmentHandle = G1::affine_element; - using PCSParams = pcs::ipa::Params; - using PCS = pcs::ipa::IPA; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // The number of multivariate polynomials on which a sumcheck prover sumcheck operates (including shifts). We often diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.hpp index e1856554bdcd..02046f1c98d4 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.hpp @@ -9,6 +9,7 @@ #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/bn254/pairing.hpp" +#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" #include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp" #include "barretenberg/numeric/bitop/pow.hpp" #include "barretenberg/polynomials/polynomial.hpp" @@ -25,9 +26,10 @@ namespace proof_system::honk::pcs { namespace kzg { struct Params { - using Fr = typename barretenberg::g1::Fr; - using Commitment = typename barretenberg::g1::affine_element; - using GroupElement = barretenberg::g1::element; + using Curve = curve::BN254; + using Fr = typename Curve::ScalarField; + using Commitment = typename Curve::AffineElement; + using GroupElement = typename Curve::Element; using Polynomial = barretenberg::Polynomial; @@ -52,7 +54,8 @@ struct Params { * @param path * */ - CommitmentKey(const size_t num_points, std::shared_ptr crs_factory) + CommitmentKey(const size_t num_points, + std::shared_ptr> crs_factory) : pippenger_runtime_state(num_points) , srs(crs_factory->get_prover_crs(num_points)) {} @@ -60,9 +63,9 @@ struct Params { // Note: This constructor is used only by Plonk; For Honk the CommitmentKey is solely responsible for extracting // the srs. CommitmentKey(const size_t num_points, - std::shared_ptr> prover_srs) + std::shared_ptr> prover_crs) : pippenger_runtime_state(num_points) - , srs(prover_srs) + , srs(prover_crs) {} /** @@ -75,12 +78,12 @@ struct Params { { const size_t degree = polynomial.size(); ASSERT(degree <= srs->get_monomial_size()); - return barretenberg::scalar_multiplication::pippenger_unsafe( + return barretenberg::scalar_multiplication::pippenger_unsafe( const_cast(polynomial.data()), srs->get_monomial_points(), degree, pippenger_runtime_state); }; - barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; - std::shared_ptr> srs; + barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; + std::shared_ptr> srs; }; class VerificationKey { @@ -92,11 +95,11 @@ struct Params { * @brief Construct a new Kate Verification Key object from existing SRS * * @param num_points - * @param verifier_srs verifier G2 point + * @paramsrs verifier G2 point */ VerificationKey([[maybe_unused]] size_t num_points, - std::shared_ptr crs_factory) - : verifier_srs(crs_factory->get_verifier_crs()) + std::shared_ptr> crs_factory) + : srs(crs_factory->get_verifier_crs()) {} /** @@ -110,14 +113,13 @@ struct Params { { Commitment pairing_points[2]{ p0, p1 }; // The final pairing check of step 12. - // TODO(Adrian): try to template parametrise the pairing + fq12 output :/ - barretenberg::fq12 result = barretenberg::pairing::reduced_ate_pairing_batch_precomputed( - pairing_points, verifier_srs->get_precomputed_g2_lines(), 2); + Curve::TargetField result = barretenberg::pairing::reduced_ate_pairing_batch_precomputed( + pairing_points, srs->get_precomputed_g2_lines(), 2); - return (result == barretenberg::fq12::one()); + return (result == Curve::TargetField::one()); } - std::shared_ptr verifier_srs; + std::shared_ptr> srs; }; }; @@ -186,9 +188,10 @@ template struct Params { namespace ipa { struct Params { - using Fr = typename barretenberg::g1::Fr; - using Commitment = typename barretenberg::g1::affine_element; - using GroupElement = barretenberg::g1::element; + using Curve = curve::Grumpkin; + using Fr = typename Curve::ScalarField; + using Commitment = typename Curve::AffineElement; + using GroupElement = typename Curve::Element; using Polynomial = barretenberg::Polynomial; @@ -207,7 +210,8 @@ struct Params { * @param path * */ - CommitmentKey(const size_t num_points, std::shared_ptr crs_factory) + CommitmentKey(const size_t num_points, + std::shared_ptr> crs_factory) : pippenger_runtime_state(num_points) , srs(crs_factory->get_prover_crs(num_points)) {} @@ -222,12 +226,12 @@ struct Params { { const size_t degree = polynomial.size(); ASSERT(degree <= srs->get_monomial_size()); - return barretenberg::scalar_multiplication::pippenger_unsafe( + return barretenberg::scalar_multiplication::pippenger_unsafe( const_cast(polynomial.data()), srs->get_monomial_points(), degree, pippenger_runtime_state); }; - barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; - std::shared_ptr> srs; + barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; + std::shared_ptr> srs; }; class VerificationKey { @@ -241,13 +245,14 @@ struct Params { * @param num_points specifies the length of the SRS * @param path is the location to the SRS file */ - VerificationKey(size_t num_points, std::shared_ptr crs_factory) + VerificationKey(size_t num_points, std::shared_ptr> crs_factory) : pippenger_runtime_state(num_points) - , srs(crs_factory->get_prover_crs(num_points)) + , srs(crs_factory->get_verifier_crs(num_points)) + {} - barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; - std::shared_ptr> srs; + barretenberg::scalar_multiplication::pippenger_runtime_state pippenger_runtime_state; + std::shared_ptr> srs; }; }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.test.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.test.hpp index a833187b588d..4f27faff9d6d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.test.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/commitment_key.test.hpp @@ -23,17 +23,17 @@ template inline std::shared_ptr CreateCommitmentKey(); template <> inline std::shared_ptr CreateCommitmentKey() { - constexpr size_t n = 128; - std::shared_ptr crs_factory( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition")); + constexpr size_t n = 4096; + std::shared_ptr> crs_factory( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition", 4096)); return std::make_shared(n, crs_factory); } // For IPA template <> inline std::shared_ptr CreateCommitmentKey() { - constexpr size_t n = 128; - std::shared_ptr crs_factory( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition")); + constexpr size_t n = 4096; + std::shared_ptr> crs_factory( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/grumpkin", 4096)); return std::make_shared(n, crs_factory); } @@ -47,17 +47,17 @@ template inline std::shared_ptr CreateVerificationKey(); template <> inline std::shared_ptr CreateVerificationKey() { - constexpr size_t n = 128; - std::shared_ptr crs_factory( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition")); + constexpr size_t n = 4096; + std::shared_ptr> crs_factory( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition", 4096)); return std::make_shared(n, crs_factory); } // For IPA template <> inline std::shared_ptr CreateVerificationKey() { - constexpr size_t n = 128; - std::shared_ptr crs_factory( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition")); + constexpr size_t n = 4096; + std::shared_ptr> crs_factory( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/grumpkin", 4096)); return std::make_shared(n, crs_factory); } template inline std::shared_ptr CreateVerificationKey() @@ -72,7 +72,6 @@ template class CommitmentTest : public ::testing::Test { using Fr = typename Params::Fr; using Commitment = typename Params::Commitment; using Polynomial = typename Params::Polynomial; - using Transcript = transcript::StandardTranscript; public: CommitmentTest() @@ -127,6 +126,7 @@ template class CommitmentTest : public ::testing::Test { Fr y_expected = witness.evaluate(x); EXPECT_EQ(y, y_expected) << "OpeningClaim: evaluations mismatch"; Commitment commitment_expected = commit(witness); + // found it EXPECT_EQ(commitment, commitment_expected) << "OpeningClaim: commitment mismatch"; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/gemini/gemini.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/gemini/gemini.test.cpp index 69a28eb0324f..e5791a080490 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/gemini/gemini.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/gemini/gemini.test.cpp @@ -100,7 +100,8 @@ template class GeminiTest : public CommitmentTest { } }; -TYPED_TEST_SUITE(GeminiTest, CommitmentSchemeParams); +using ParamsTypes = ::testing::Types; +TYPED_TEST_SUITE(GeminiTest, ParamsTypes); TYPED_TEST(GeminiTest, Single) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.hpp index 25cc01431e3d..4bc02e0f4a2a 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.hpp @@ -15,8 +15,8 @@ * */ namespace proof_system::honk::pcs::ipa { - template class IPA { + using Curve = typename Params::Curve; using Fr = typename Params::Fr; using GroupElement = typename Params::GroupElement; using Commitment = typename Params::Commitment; @@ -86,15 +86,14 @@ template class IPA { // L_i = < a_vec_lo, G_vec_hi > + inner_prod_L * aux_generator L_elements[i] = // TODO(#473) - barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( + barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( &a_vec[0], &G_vec_local[round_size], round_size, ck->pippenger_runtime_state); L_elements[i] += aux_generator * inner_prod_L; // R_i = < a_vec_hi, G_vec_lo > + inner_prod_R * aux_generator // TODO(#473) - R_elements[i] = - barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( - &a_vec[round_size], &G_vec_local[0], round_size, ck->pippenger_runtime_state); + R_elements[i] = barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( + &a_vec[round_size], &G_vec_local[0], round_size, ck->pippenger_runtime_state); R_elements[i] += aux_generator * inner_prod_R; std::string index = std::to_string(i); @@ -168,9 +167,8 @@ template class IPA { msm_scalars[2 * i + 1] = round_challenges_inv[i].sqr(); } // TODO(#473) - GroupElement LR_sums = - barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( - &msm_scalars[0], &msm_elements[0], pippenger_size, vk->pippenger_runtime_state); + GroupElement LR_sums = barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( + &msm_scalars[0], &msm_elements[0], pippenger_size, vk->pippenger_runtime_state); GroupElement C_zero = C_prime + LR_sums; /** @@ -213,7 +211,7 @@ template class IPA { G_vec_local[i >> 1] = srs_elements[i]; } // TODO(#473) - auto G_zero = barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( + auto G_zero = barretenberg::scalar_multiplication::pippenger_without_endomorphism_basis_points( &s_vec[0], &G_vec_local[0], poly_degree, vk->pippenger_runtime_state); auto a_zero = transcript.template receive_from_prover("IPA:a_0"); @@ -224,4 +222,4 @@ template class IPA { } }; -} // namespace proof_system::honk::pcs::ipa +} // namespace proof_system::honk::pcs::ipa \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.test.cpp index f5fb23df682d..2b5873743892 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/ipa/ipa.test.cpp @@ -1,3 +1,5 @@ +#include "../gemini/gemini.hpp" +#include "../shplonk/shplonk_single.hpp" #include "barretenberg/common/mem.hpp" #include "barretenberg/ecc/curves/bn254/fq12.hpp" #include "barretenberg/ecc/curves/types.hpp" @@ -78,4 +80,95 @@ TEST_F(IPATest, Open) EXPECT_EQ(prover_transcript.get_manifest(), verifier_transcript.get_manifest()); } + +TEST_F(IPATest, GeminiShplonkIPAWithShift) +{ + using IPA = IPA; + using Shplonk = shplonk::SingleBatchOpeningScheme; + using Gemini = gemini::MultilinearReductionScheme; + + const size_t n = 8; + const size_t log_n = 3; + + Fr rho = Fr::random_element(); + + // Generate multilinear polynomials, their commitments (genuine and mocked) and evaluations (genuine) at a random + // point. + const auto mle_opening_point = this->random_evaluation_point(log_n); // sometimes denoted 'u' + auto poly1 = this->random_polynomial(n); + auto poly2 = this->random_polynomial(n); + poly2[0] = Fr::zero(); // this property is required of polynomials whose shift is used + + GroupElement commitment1 = this->commit(poly1); + GroupElement commitment2 = this->commit(poly2); + + auto eval1 = poly1.evaluate_mle(mle_opening_point); + auto eval2 = poly2.evaluate_mle(mle_opening_point); + auto eval2_shift = poly2.evaluate_mle(mle_opening_point, true); + + std::vector multilinear_evaluations = { eval1, eval2, eval2_shift }; + + std::vector rhos = Gemini::powers_of_rho(rho, multilinear_evaluations.size()); + + Fr batched_evaluation = Fr::zero(); + for (size_t i = 0; i < rhos.size(); ++i) { + batched_evaluation += multilinear_evaluations[i] * rhos[i]; + } + + Polynomial batched_unshifted(n); + Polynomial batched_to_be_shifted(n); + batched_unshifted.add_scaled(poly1, rhos[0]); + batched_unshifted.add_scaled(poly2, rhos[1]); + batched_to_be_shifted.add_scaled(poly2, rhos[2]); + + GroupElement batched_commitment_unshifted = GroupElement::zero(); + GroupElement batched_commitment_to_be_shifted = GroupElement::zero(); + batched_commitment_unshifted = commitment1 * rhos[0] + commitment2 * rhos[1]; + batched_commitment_to_be_shifted = commitment2 * rhos[2]; + + auto prover_transcript = ProverTranscript::init_empty(); + + auto fold_polynomials = Gemini::compute_fold_polynomials( + mle_opening_point, std::move(batched_unshifted), std::move(batched_to_be_shifted)); + + for (size_t l = 0; l < log_n - 1; ++l) { + std::string label = "FOLD_" + std::to_string(l + 1); + auto commitment = this->ck()->commit(fold_polynomials[l + 2]); + prover_transcript.send_to_verifier(label, commitment); + } + + const Fr r_challenge = prover_transcript.get_challenge("Gemini:r"); + + const auto [gemini_opening_pairs, gemini_witnesses] = + Gemini::compute_fold_polynomial_evaluations(mle_opening_point, std::move(fold_polynomials), r_challenge); + + for (size_t l = 0; l < log_n; ++l) { + std::string label = "Gemini:a_" + std::to_string(l); + const auto& evaluation = gemini_opening_pairs[l + 1].evaluation; + prover_transcript.send_to_verifier(label, evaluation); + } + + const Fr nu_challenge = prover_transcript.get_challenge("Shplonk:nu"); + auto batched_quotient_Q = Shplonk::compute_batched_quotient(gemini_opening_pairs, gemini_witnesses, nu_challenge); + prover_transcript.send_to_verifier("Shplonk:Q", this->ck()->commit(batched_quotient_Q)); + + const Fr z_challenge = prover_transcript.get_challenge("Shplonk:z"); + const auto [shplonk_opening_pair, shplonk_witness] = Shplonk::compute_partially_evaluated_batched_quotient( + gemini_opening_pairs, gemini_witnesses, std::move(batched_quotient_Q), nu_challenge, z_challenge); + + IPA::compute_opening_proof(this->ck(), shplonk_opening_pair, shplonk_witness, prover_transcript); + + auto verifier_transcript = VerifierTranscript::init_empty(prover_transcript); + + auto gemini_verifier_claim = Gemini::reduce_verify(mle_opening_point, + batched_evaluation, + batched_commitment_unshifted, + batched_commitment_to_be_shifted, + verifier_transcript); + + const auto shplonk_verifier_claim = Shplonk::reduce_verify(this->vk(), gemini_verifier_claim, verifier_transcript); + bool verified = IPA::verify(this->vk(), shplonk_verifier_claim, verifier_transcript); + + EXPECT_EQ(verified, true); +} } // namespace proof_system::honk::pcs::ipa diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.test.cpp index 8c5dfe57a917..0659402ec774 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.test.cpp @@ -163,7 +163,7 @@ TYPED_TEST(KZGTest, GeminiShplonkKzgWithShift) verifier_transcript); // Shplonk verifier claim: commitment [Q] - [Q_z], opening point (z_challenge, 0) - const auto shplonk_verifier_claim = Shplonk::reduce_verify(gemini_verifier_claim, verifier_transcript); + const auto shplonk_verifier_claim = Shplonk::reduce_verify(this->vk(), gemini_verifier_claim, verifier_transcript); // KZG verifier: // aggregates inputs [Q] - [Q_z] and [W] into an 'accumulator' (can perform pairing check on result) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk.test.cpp index 9763018e6a2b..6e171403a636 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk.test.cpp @@ -13,7 +13,8 @@ namespace proof_system::honk::pcs::shplonk { template class ShplonkTest : public CommitmentTest {}; -TYPED_TEST_SUITE(ShplonkTest, CommitmentSchemeParams); +using ParamsTypes = ::testing::Types; +TYPED_TEST_SUITE(ShplonkTest, ParamsTypes); // Test of Shplonk prover/verifier for two polynomials of different size, each opened at a single (different) point TYPED_TEST(ShplonkTest, ShplonkSimple) @@ -64,7 +65,7 @@ TYPED_TEST(ShplonkTest, ShplonkSimple) auto verifier_transcript = VerifierTranscript::init_empty(prover_transcript); // Execute the shplonk verifier functionality - const auto verifier_claim = Shplonk::reduce_verify(opening_claims, verifier_transcript); + const auto verifier_claim = Shplonk::reduce_verify(this->vk(), opening_claims, verifier_transcript); this->verify_opening_claim(verifier_claim, shplonk_prover_witness); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk_single.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk_single.hpp index 262362eef9ee..524c5d3bc2c0 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk_single.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/shplonk/shplonk_single.hpp @@ -20,6 +20,7 @@ template class SingleBatchOpeningScheme { using GroupElement = typename Params::GroupElement; using Commitment = typename Params::Commitment; using Polynomial = barretenberg::Polynomial; + using VK = typename Params::VerificationKey; public: /** @@ -123,7 +124,8 @@ template class SingleBatchOpeningScheme { * @param transcript * @return OpeningClaim */ - static OpeningClaim reduce_verify(std::span> claims, + static OpeningClaim reduce_verify(std::shared_ptr vk, + std::span> claims, VerifierTranscript& transcript) { const size_t num_claims = claims.size(); @@ -169,7 +171,9 @@ template class SingleBatchOpeningScheme { current_nu *= nu; } // [G] += G₀⋅[1] = [G] + (∑ⱼ ρʲ ⋅ vⱼ / ( r − xⱼ ))⋅[1] - G_commitment += GroupElement::one() * G_commitment_constant; + + // GroupElement sort_of_one{ x, y }; + G_commitment += vk->srs->get_first_g1() * G_commitment_constant; // Return opening pair (z, 0) and commitment [G] return { { z_challenge, Fr::zero() }, G_commitment }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/composer_lib.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/composer_lib.hpp index d62b223ccf5c..dca4f4851397 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/composer_lib.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/composer_lib.hpp @@ -19,7 +19,7 @@ namespace proof_system::honk { template std::shared_ptr compute_verification_key_common( std::shared_ptr const& proving_key, - std::shared_ptr const& vrs) + std::shared_ptr> const& vrs) { auto verification_key = std::make_shared( proving_key->circuit_size, proving_key->num_public_inputs, vrs); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_verifier.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_verifier.cpp index 7fbdcd932dd9..aea17570f63c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_verifier.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_verifier.cpp @@ -165,7 +165,7 @@ template bool UltraVerifier_::verify_proof(const plonk transcript); // Produce a Shplonk claim: commitment [Q] - [Q_z], evaluation zero (at random challenge z) - auto shplonk_claim = Shplonk::reduce_verify(gemini_claim, transcript); + auto shplonk_claim = Shplonk::reduce_verify(pcs_verification_key, gemini_claim, transcript); // // Verify the Shplonk claim with KZG or IPA return PCS::verify(pcs_verification_key, shplonk_claim, transcript); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/verifier.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/verifier.cpp index a109f02ca1e2..0e559c2b4691 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/verifier.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/proof_system/verifier.cpp @@ -160,7 +160,7 @@ template bool StandardVerifier_::verify_proof(const pl transcript); // Produce a Shplonk claim: commitment [Q] - [Q_z], evaluation zero (at random challenge z) - auto shplonk_claim = Shplonk::reduce_verify(gemini_claim, transcript); + auto shplonk_claim = Shplonk::reduce_verify(pcs_verification_key, gemini_claim, transcript); // Verify the Shplonk claim with KZG or IPA return PCS::verify(pcs_verification_key, shplonk_claim, transcript); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp index 09243e548166..f88bb12de1ee 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp @@ -185,8 +185,7 @@ template void create_some_elliptic_curve_addition_gates(auto& uint32_t x3 = circuit_builder.add_variable(p3.x); uint32_t y3 = circuit_builder.add_variable(p3.y); - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, beta_scalar, -1 }; - circuit_builder.create_ecc_add_gate(gate); + circuit_builder.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta_scalar, -1 }); } template void create_some_ecc_op_queue_gates(auto& circuit_builder) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.test.cpp index cb1670b710c0..0f1e95454ca6 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.test.cpp @@ -570,8 +570,7 @@ TEST_F(SumcheckTests, RealCircuitUltra) uint32_t x3 = circuit_constructor.add_variable(p3.x); uint32_t y3 = circuit_constructor.add_variable(p3.y); - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, beta_scalar, -1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta_scalar, -1 }); // Add some RAM gates uint32_t ram_values[8]{ diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp index f3c46576f528..3821d5d6505e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp @@ -124,8 +124,8 @@ TYPED_TEST(TranscriptTests, ProverManifestConsistency) { using Flavor = TypeParam; // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size) - fr a = 1; - auto circuit_constructor = proof_system::StandardCircuitBuilder(); + typename Flavor::FF a = 1; + auto circuit_constructor = typename Flavor::CircuitBuilder(); circuit_constructor.add_variable(a); circuit_constructor.add_public_variable(a); @@ -152,8 +152,8 @@ TYPED_TEST(TranscriptTests, VerifierManifestConsistency) { using Flavor = TypeParam; // Construct a simple circuit of size n = 8 (i.e. the minimum circuit size) - auto circuit_constructor = proof_system::StandardCircuitBuilder(); - fr a = 1; + typename Flavor::FF a = 1; + auto circuit_constructor = typename Flavor::CircuitBuilder(); circuit_constructor.add_variable(a); circuit_constructor.add_public_variable(a); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp index e73a1be11eb4..b4fa332c9a71 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/compute_circuit_data.hpp @@ -21,7 +21,7 @@ struct circuit_data { : num_gates(0) {} - std::shared_ptr srs; + std::shared_ptr> srs; std::shared_ptr proving_key; std::shared_ptr verification_key; size_t num_gates; @@ -40,7 +40,7 @@ inline bool exists(std::string const& path) template circuit_data get_circuit_data(std::string const& name, std::string const& path_name, - std::shared_ptr const& srs, + std::shared_ptr> const& srs, std::string const& key_path, bool compute, bool save, diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.cpp index c81913eaefa6..6f0d1f952c8d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.cpp @@ -59,7 +59,8 @@ join_split_tx noop_tx() return tx; } -circuit_data get_circuit_data(std::shared_ptr const& srs, bool mock) +circuit_data get_circuit_data(std::shared_ptr> const& srs, + bool mock) { std::cerr << "Getting join-split circuit data..." << std::endl; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.hpp index 4f467f2f1657..459fc36a9dd5 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/compute_circuit_data.hpp @@ -10,7 +10,8 @@ join_split_tx noop_tx(); using circuit_data = proofs::circuit_data; -circuit_data get_circuit_data(std::shared_ptr const& srs, bool mock = false); +circuit_data get_circuit_data(std::shared_ptr> const& srs, + bool mock = false); } // namespace join_split } // namespace proofs diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp index 3bb2bc4a1799..1da1caee45fe 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.cpp @@ -14,7 +14,8 @@ using namespace proof_system::plonk::stdlib::merkle_tree; static std::shared_ptr proving_key; static std::shared_ptr verification_key; -void init_proving_key(std::shared_ptr const& crs_factory, bool mock) +void init_proving_key(std::shared_ptr> const& crs_factory, + bool mock) { if (proving_key) { return; @@ -42,7 +43,7 @@ void release_proving_key() proving_key.reset(); } -void init_verification_key(std::shared_ptr const& crs_factory) +void init_verification_key(std::shared_ptr> const& crs_factory) { if (!proving_key) { std::abort(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp index 91e83de2e9c3..1d4a1dd5fa7a 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.hpp @@ -7,11 +7,12 @@ namespace join_split_example { namespace proofs { namespace join_split { -void init_proving_key(std::shared_ptr const& crs_factory, bool mock); +void init_proving_key(std::shared_ptr> const& crs_factory, + bool mock); void release_proving_key(); -void init_verification_key(std::shared_ptr const& crs_factory); +void init_verification_key(std::shared_ptr> const& crs_factory); Prover new_join_split_prover(join_split_tx const& tx, bool mock); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp index c24012a91bde..1377ebf10555 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split.test.cpp @@ -43,9 +43,10 @@ class join_split_tests : public ::testing::Test { static constexpr size_t ACCOUNT_INDEX = 14; static void SetUpTestCase() { - auto null_crs_factory = std::make_shared(); + auto null_crs_factory = std::make_shared>(); init_proving_key(null_crs_factory, false); - auto crs_factory = std::make_unique("../srs_db/ignition"); + auto crs_factory = + std::make_unique>("../srs_db/ignition"); init_verification_key(std::move(crs_factory)); info("vk hash: ", get_verification_key()->sha256_hash()); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp index 3407d319496b..f23bceef85f2 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/join_split_example/proofs/join_split/join_split_js_parity.test.cpp @@ -25,9 +25,10 @@ class join_split_js_parity_tests : public ::testing::Test { protected: static void SetUpTestCase() { - auto null_crs_factory = std::make_shared(); + auto null_crs_factory = std::make_shared>(); init_proving_key(null_crs_factory, false); - auto crs_factory = std::make_unique("../srs_db/ignition"); + auto crs_factory = + std::make_unique>("../srs_db/ignition"); init_verification_key(std::move(crs_factory)); info("vk hash: ", get_verification_key()->sha256_hash()); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.cpp index 73bc15311c2e..caeb3dcd796d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.cpp @@ -44,7 +44,8 @@ void compute_monomial_and_coset_selector_forms(plonk::proving_key* circuit_provi */ std::shared_ptr compute_verification_key_common( std::shared_ptr const& proving_key, - std::shared_ptr const& vrs) + // Here too + std::shared_ptr> const& vrs) { auto circuit_verification_key = std::make_shared( proving_key->circuit_size, proving_key->num_public_inputs, vrs, proving_key->circuit_type); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.hpp index d24ca45279e6..9b079013dfdd 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/composer_lib.hpp @@ -1,4 +1,5 @@ #pragma once +#include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp" #include "barretenberg/plonk/proof_system/verification_key/verification_key.hpp" @@ -20,11 +21,12 @@ struct SelectorProperties { * in that case. * @return std::shared_ptr */ -std::shared_ptr initialize_proving_key(const auto& circuit_constructor, - barretenberg::srs::factories::CrsFactory* crs_factory, - const size_t minimum_circuit_size, - const size_t num_randomized_gates, - CircuitType circuit_type) +std::shared_ptr initialize_proving_key( + const auto& circuit_constructor, + barretenberg::srs::factories::CrsFactory* crs_factory, + const size_t minimum_circuit_size, + const size_t num_randomized_gates, + CircuitType circuit_type) { const size_t num_gates = circuit_constructor.num_gates; @@ -77,6 +79,7 @@ void compute_monomial_and_coset_selector_forms(plonk::proving_key* key, */ std::shared_ptr compute_verification_key_common( std::shared_ptr const& proving_key, - std::shared_ptr const& vrs); + // silencing for now but need to figure out where to extract type of VerifierCrs from :-/ + std::shared_ptr> const& vrs); } // namespace proof_system::plonk diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.cpp index 97439bf7983b..66a3f6790aae 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.cpp @@ -37,7 +37,8 @@ void StandardComposer::compute_witness(const CircuitBuilder& circuit_constructor const size_t subgroup_size = circuit_constructor.get_circuit_subgroup_size(num_constraints + NUM_RESERVED_GATES); - auto wire_polynomial_evaluations = construct_wire_polynomials_base(circuit_constructor, subgroup_size); + auto wire_polynomial_evaluations = + construct_wire_polynomials_base(circuit_constructor, subgroup_size); for (size_t j = 0; j < program_width; ++j) { std::string index = std::to_string(j + 1); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.hpp index b96679a7ab5e..b954a49d67df 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.hpp @@ -15,6 +15,7 @@ namespace proof_system::plonk { class StandardComposer { public: using Flavor = plonk::flavor::Standard; + using CircuitBuilder = StandardCircuitBuilder; static constexpr std::string_view NAME_STRING = "StandardPlonk"; @@ -24,19 +25,19 @@ class StandardComposer { std::shared_ptr circuit_verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr crs_factory_; + std::shared_ptr> crs_factory_; bool computed_witness = false; StandardComposer() - : StandardComposer(std::shared_ptr( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition"))) + : StandardComposer(std::shared_ptr>( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition"))) {} - StandardComposer(std::shared_ptr crs_factory) + StandardComposer(std::shared_ptr> crs_factory) : crs_factory_(std::move(crs_factory)) {} - StandardComposer(std::unique_ptr&& crs_factory) + StandardComposer(std::unique_ptr>&& crs_factory) : crs_factory_(std::move(crs_factory)) {} StandardComposer(std::shared_ptr p_key, std::shared_ptr v_key) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.test.cpp index 54f95ffa78a6..26c8ffc7c961 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/standard_composer.test.cpp @@ -40,7 +40,7 @@ TEST(standard_plonk_composer, composer_from_serialized_keys) auto pk_data = from_buffer(pk_buf); auto vk_data = from_buffer(vk_buf); - auto crs = std::make_unique("../srs_db/ignition"); + auto crs = std::make_unique>("../srs_db/ignition"); auto proving_key = std::make_shared(std::move(pk_data), crs->get_prover_crs(pk_data.circuit_size + 1)); auto verification_key = std::make_shared(std::move(vk_data), crs->get_verifier_crs()); @@ -282,8 +282,7 @@ TEST(standard_plonk_composer, and_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = - builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); + auto accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); // builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { @@ -354,8 +353,7 @@ TEST(standard_plonk_composer, xor_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = - builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); + auto accumulators = builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { uint32_t left_expected = (left_value >> (30U - (2 * j))); @@ -422,17 +420,15 @@ TEST(standard_plonk_composer, big_add_gate_with_bit_extract) uint32_t input = engine.get_random_uint32(); uint32_t output = input + (quad_value > 1 ? 1 : 0); - add_quad gate{ builder.add_variable(uint256_t(input)), - builder.add_variable(uint256_t(output)), - right_idx, - left_idx, - fr(6), - -fr(6), - fr::zero(), - fr::zero(), - fr::zero() }; - - builder.create_big_add_gate_with_bit_extraction(gate); + builder.create_big_add_gate_with_bit_extraction({ builder.add_variable(uint256_t(input)), + builder.add_variable(uint256_t(output)), + right_idx, + left_idx, + fr(6), + -fr(6), + fr::zero(), + fr::zero(), + fr::zero() }); }; generate_constraints(0); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.hpp index b9b64ea0b2e6..8f124c3787d7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.hpp @@ -22,18 +22,18 @@ class TurboComposer { std::shared_ptr circuit_verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr crs_factory_; + std::shared_ptr> crs_factory_; bool computed_witness = false; TurboComposer() - : TurboComposer(std::shared_ptr( - new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition"))) + : TurboComposer(std::shared_ptr>( + new barretenberg::srs::factories::FileCrsFactory("../srs_db/ignition"))) {} - TurboComposer(std::shared_ptr crs_factory) + TurboComposer(std::shared_ptr> crs_factory) : crs_factory_(std::move(crs_factory)) {} - TurboComposer(std::unique_ptr&& crs_factory) + TurboComposer(std::unique_ptr>&& crs_factory) : crs_factory_(std::move(crs_factory)) {} TurboComposer(std::shared_ptr p_key, std::shared_ptr v_key) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.test.cpp index 3c280bafc0b6..f57a7a4a1c83 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/turbo_composer.test.cpp @@ -44,7 +44,7 @@ TEST(turbo_plonk_composer, composer_from_serialized_keys) auto pk_data = from_buffer(pk_buf); auto vk_data = from_buffer(vk_buf); - auto crs = std::make_unique("../srs_db/ignition"); + auto crs = std::make_unique>("../srs_db/ignition"); auto proving_key = std::make_shared(std::move(pk_data), crs->get_prover_crs(pk_data.circuit_size + 1)); auto verification_key = std::make_shared(std::move(vk_data), crs->get_verifier_crs()); @@ -278,17 +278,12 @@ TEST(turbo_plonk_composer, small_scalar_multipliers) } grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; - auto builder = TurboCircuitBuilder(); auto composer = TurboComposer(); fr x_alpha = accumulator_offset; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = builder.add_variable(accumulator_transcript[i]); round_quad.a = builder.add_variable(multiplication_transcript[i].x); round_quad.b = builder.add_variable(multiplication_transcript[i].y); @@ -306,20 +301,23 @@ TEST(turbo_plonk_composer, small_scalar_multipliers) if (i > 0) { builder.create_fixed_group_add_gate(round_quad); } else { - builder.create_fixed_group_add_gate_with_init(round_quad, init_quad); + builder.create_fixed_group_add_gate_with_init(round_quad, + { origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }); } } - add_quad add_quad{ builder.add_variable(multiplication_transcript[num_quads].x), - builder.add_variable(multiplication_transcript[num_quads].y), - builder.add_variable(x_alpha), - builder.add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; - builder.create_big_add_gate(add_quad); + builder.create_big_add_gate({ builder.add_variable(multiplication_transcript[num_quads].x), + builder.add_variable(multiplication_transcript[num_quads].y), + builder.add_variable(x_alpha), + builder.add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }); grumpkin::g1::element expected_point = grumpkin::g1::element(generator * scalar_multiplier.to_montgomery_form()).normalize(); @@ -409,17 +407,12 @@ TEST(turbo_plonk_composer, large_scalar_multipliers) } grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; - auto builder = TurboCircuitBuilder(); auto composer = TurboComposer(); fr x_alpha = accumulator_offset; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = builder.add_variable(accumulator_transcript[i]); round_quad.a = builder.add_variable(multiplication_transcript[i].x); round_quad.b = builder.add_variable(multiplication_transcript[i].y); @@ -437,20 +430,23 @@ TEST(turbo_plonk_composer, large_scalar_multipliers) if (i > 0) { builder.create_fixed_group_add_gate(round_quad); } else { - builder.create_fixed_group_add_gate_with_init(round_quad, init_quad); + builder.create_fixed_group_add_gate_with_init(round_quad, + { origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }); } } - add_quad add_quad{ builder.add_variable(multiplication_transcript[num_quads].x), - builder.add_variable(multiplication_transcript[num_quads].y), - builder.add_variable(x_alpha), - builder.add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; - builder.create_big_add_gate(add_quad); + builder.create_big_add_gate({ builder.add_variable(multiplication_transcript[num_quads].x), + builder.add_variable(multiplication_transcript[num_quads].y), + builder.add_variable(x_alpha), + builder.add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }); grumpkin::g1::element expected_point = grumpkin::g1::element(generator * scalar_multiplier.to_montgomery_form()).normalize(); @@ -561,7 +557,7 @@ TEST(turbo_plonk_composer, and_constraint_failure) uint32_t right_witness_index = builder.add_variable(right_witness_value); // 4 && 5 is 4, so 3 bits are needed, but we only constrain 2 - accumulator_triple accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 2); + auto accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 2); auto prover = composer.create_prover(builder); @@ -597,8 +593,7 @@ TEST(turbo_plonk_composer, and_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = - builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); + auto accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); // builder.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { @@ -668,7 +663,7 @@ TEST(turbo_plonk_composer, xor_constraint_failure) uint32_t right_witness_index = builder.add_variable(right_witness_value); // 4 && 1 is 5, so 3 bits are needed, but we only constrain 2 - accumulator_triple accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 2); + auto accumulators = builder.create_and_constraint(left_witness_index, right_witness_index, 2); auto prover = composer.create_prover(builder); @@ -704,8 +699,7 @@ TEST(turbo_plonk_composer, xor_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = - builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); + auto accumulators = builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { uint32_t left_expected = (left_value >> (30U - (2 * j))); @@ -772,17 +766,15 @@ TEST(turbo_plonk_composer, big_add_gate_with_bit_extract) uint32_t input = engine.get_random_uint32(); uint32_t output = input + (quad_value > 1 ? 1 : 0); - add_quad gate{ builder.add_variable(uint256_t(input)), - builder.add_variable(uint256_t(output)), - right_idx, - left_idx, - fr(6), - -fr(6), - fr::zero(), - fr::zero(), - fr::zero() }; - - builder.create_big_add_gate_with_bit_extraction(gate); + builder.create_big_add_gate_with_bit_extraction({ builder.add_variable(uint256_t(input)), + builder.add_variable(uint256_t(output)), + right_idx, + left_idx, + fr(6), + -fr(6), + fr::zero(), + fr::zero(), + fr::zero() }); }; generate_constraints(0); @@ -1059,17 +1051,12 @@ TEST(turbo_plonk_composer, test_check_circuit_fixed_group) } grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; - auto builder = TurboCircuitBuilder(); auto composer = TurboComposer(); fr x_alpha = accumulator_offset; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = builder.add_variable(accumulator_transcript[i]); round_quad.a = builder.add_variable(multiplication_transcript[i].x); round_quad.b = builder.add_variable(multiplication_transcript[i].y); @@ -1087,20 +1074,23 @@ TEST(turbo_plonk_composer, test_check_circuit_fixed_group) if (i > 0) { builder.create_fixed_group_add_gate(round_quad); } else { - builder.create_fixed_group_add_gate_with_init(round_quad, init_quad); + builder.create_fixed_group_add_gate_with_init(round_quad, + { origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }); } } - add_quad add_quad{ builder.add_variable(multiplication_transcript[num_quads].x), - builder.add_variable(multiplication_transcript[num_quads].y), - builder.add_variable(x_alpha), - builder.add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; - builder.create_big_add_gate(add_quad); + builder.create_big_add_gate({ builder.add_variable(multiplication_transcript[num_quads].x), + builder.add_variable(multiplication_transcript[num_quads].y), + builder.add_variable(x_alpha), + builder.add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }); grumpkin::g1::element expected_point = grumpkin::g1::element(generator * scalar_multiplier.to_montgomery_form()).normalize(); @@ -1166,8 +1156,7 @@ TEST(turbo_plonk_composer, test_check_circuit_xor) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = - builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); + auto accumulators = builder.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); } uint32_t zero_idx = builder.add_variable(fr::zero()); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp index eace805e7152..1f71e1e2e9b9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.hpp @@ -17,6 +17,7 @@ class UltraComposer { public: using Flavor = flavor::Ultra; using CircuitBuilder = UltraCircuitBuilder; + using Curve = Flavor::Curve; static constexpr std::string_view NAME_STRING = "UltraPlonk"; static constexpr CircuitType type = CircuitType::ULTRA; @@ -26,7 +27,7 @@ class UltraComposer { std::shared_ptr circuit_verification_key; // The crs_factory holds the path to the srs and exposes methods to extract the srs elements - std::shared_ptr crs_factory_; + std::shared_ptr> crs_factory_; bool computed_witness = false; @@ -40,9 +41,9 @@ class UltraComposer { : UltraComposer("../srs_db/ignition"){}; UltraComposer(std::string const& crs_path) - : UltraComposer(std::make_unique(crs_path)){}; + : UltraComposer(std::make_unique>(crs_path)){}; - explicit UltraComposer(std::shared_ptr crs_factory) + explicit UltraComposer(std::shared_ptr> crs_factory) : crs_factory_(std::move(crs_factory)) {} diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.test.cpp index 21132e39d543..99b3b8232d3c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/composer/ultra_composer.test.cpp @@ -186,8 +186,7 @@ TYPED_TEST(ultra_plonk_composer, test_elliptic_gate) uint32_t x3 = builder.add_variable(p3.x); uint32_t y3 = builder.add_variable(p3.y); - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; - builder.create_ecc_add_gate(gate); + builder.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, 1, 1 }); grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); affine_element p2_endo = p2; @@ -195,15 +194,13 @@ TYPED_TEST(ultra_plonk_composer, test_elliptic_gate) p3 = affine_element(element(p1) + element(p2_endo)); x3 = builder.add_variable(p3.x); y3 = builder.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; - builder.create_ecc_add_gate(gate); + builder.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta, 1 }); p2_endo.x *= beta; p3 = affine_element(element(p1) - element(p2_endo)); x3 = builder.add_variable(p3.x); y3 = builder.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - builder.create_ecc_add_gate(gate); + builder.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }); TestFixture::prove_and_verify(builder, composer, /*expected_result=*/true); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/flavor/flavor.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/flavor/flavor.hpp index 95fdc135281f..87e22689d45f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/flavor/flavor.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/flavor/flavor.hpp @@ -10,6 +10,9 @@ class Standard { public: using CircuitBuilder = proof_system::StandardCircuitBuilder; using ProvingKey = plonk::proving_key; + using Curve = curve::BN254; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // Whether or not the first row of the execution trace is reserved for 0s to enable shifts static constexpr bool has_zero_row = false; @@ -19,6 +22,9 @@ class Turbo { public: using CircuitBuilder = proof_system::TurboCircuitBuilder; using ProvingKey = plonk::proving_key; + using Curve = curve::BN254; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // Whether or not the first row of the execution trace is reserved for 0s to enable shifts static constexpr bool has_zero_row = false; @@ -28,6 +34,9 @@ class Ultra { public: using CircuitBuilder = proof_system::UltraCircuitBuilder; using ProvingKey = plonk::proving_key; + using Curve = curve::BN254; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; static constexpr size_t NUM_WIRES = CircuitBuilder::NUM_WIRES; // Whether or not the first row of the execution trace is reserved for 0s to enable shifts static constexpr bool has_zero_row = false; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/commitment_scheme/commitment_scheme.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/commitment_scheme/commitment_scheme.test.cpp index 95606da3804c..e874cb7b5e94 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/commitment_scheme/commitment_scheme.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/commitment_scheme/commitment_scheme.test.cpp @@ -35,9 +35,9 @@ TEST(commitment_scheme, kate_open) transcript::StandardTranscript inp_tx = transcript::StandardTranscript(transcript::Manifest()); plonk::KateCommitmentScheme newKate; - // std::shared_ptr crs_factory = (new + // std::shared_ptr> crs_factory = (new // FileReferenceStringFactory("../srs_db/ignition")); - auto file_crs = std::make_shared("../srs_db/ignition"); + auto file_crs = std::make_shared>("../srs_db/ignition"); auto crs = file_crs->get_prover_crs(n); auto circuit_proving_key = std::make_shared(n, 0, crs, CircuitType::STANDARD); work_queue queue(circuit_proving_key.get(), &inp_tx); @@ -94,7 +94,7 @@ TEST(commitment_scheme, kate_batch_open) transcript::StandardTranscript inp_tx = transcript::StandardTranscript(transcript::Manifest()); plonk::KateCommitmentScheme newKate; - auto file_crs = std::make_shared("../srs_db/ignition"); + auto file_crs = std::make_shared>("../srs_db/ignition"); auto crs = file_crs->get_prover_crs(n); auto circuit_proving_key = std::make_shared(n, 0, crs, CircuitType::STANDARD); work_queue queue(circuit_proving_key.get(), &inp_tx); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/proving_key/proving_key.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/proving_key/proving_key.test.cpp index 53a1166b36bb..a44e2b532995 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/proving_key/proving_key.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/proving_key/proving_key.test.cpp @@ -26,7 +26,7 @@ TEST(proving_key, proving_key_from_serialized_key) plonk::proving_key& p_key = *composer.compute_proving_key(builder); auto pk_buf = to_buffer(p_key); auto pk_data = from_buffer(pk_buf); - auto crs = std::make_unique("../srs_db/ignition"); + auto crs = std::make_unique>("../srs_db/ignition"); auto proving_key = std::make_shared(std::move(pk_data), crs->get_prover_crs(pk_data.circuit_size + 1)); @@ -63,7 +63,7 @@ TEST(proving_key, proving_key_from_serialized_key_ultra) plonk::proving_key& p_key = *composer.compute_proving_key(builder); auto pk_buf = to_buffer(p_key); auto pk_data = from_buffer(pk_buf); - auto crs = std::make_unique("../srs_db/ignition"); + auto crs = std::make_unique>("../srs_db/ignition"); auto proving_key = std::make_shared(std::move(pk_data), crs->get_prover_crs(pk_data.circuit_size + 1)); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.cpp index 0de60f52fbb0..42914ddd11c1 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.cpp @@ -87,7 +87,7 @@ barretenberg::fr verification_key_data::compress_native(const size_t hash_index) verification_key::verification_key(const size_t num_gates, const size_t num_inputs, - std::shared_ptr const& crs, + std::shared_ptr> const& crs, CircuitType circuit_type_) : circuit_type(circuit_type_) , circuit_size(num_gates) @@ -99,7 +99,7 @@ verification_key::verification_key(const size_t num_gates, {} verification_key::verification_key(verification_key_data&& data, - std::shared_ptr const& crs) + std::shared_ptr> const& crs) : circuit_type(static_cast(data.circuit_type)) , circuit_size(data.circuit_size) , log_circuit_size(numeric::get_msb(data.circuit_size)) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.hpp index a9ca0331dd03..96ed0f68cafb 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verification_key/verification_key.hpp @@ -1,6 +1,7 @@ #pragma once #include "barretenberg/common/streams.hpp" #include "barretenberg/crypto/sha256/sha256.hpp" +#include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/bn254/fr.hpp" #include "barretenberg/plonk/proof_system/types/polynomial_manifest.hpp" #include "barretenberg/polynomials/evaluation_domain.hpp" @@ -70,10 +71,10 @@ struct verification_key { // default constructor needed for msgpack unpack verification_key() = default; verification_key(verification_key_data&& data, - std::shared_ptr const& crs); + std::shared_ptr> const& crs); verification_key(const size_t num_gates, const size_t num_inputs, - std::shared_ptr const& crs, + std::shared_ptr> const& crs, CircuitType circuit_type); verification_key(const verification_key& other); @@ -103,7 +104,7 @@ struct verification_key { barretenberg::evaluation_domain domain; - std::shared_ptr reference_string; + std::shared_ptr> reference_string; std::map commitments; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verifier/verifier.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verifier/verifier.test.cpp index fcf4cfc93a02..dd923130f833 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verifier/verifier.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/plonk/proof_system/verifier/verifier.test.cpp @@ -41,7 +41,7 @@ plonk::Verifier generate_verifier(std::shared_ptr circuit_proving_k state)); } - auto crs = std::make_shared("../srs_db/ignition"); + auto crs = std::make_shared>("../srs_db/ignition"); std::shared_ptr circuit_verification_key = std::make_shared(circuit_proving_key->circuit_size, circuit_proving_key->num_public_inputs, diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.cpp index 8a6ed0e55473..2bd2f040d5b7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.cpp @@ -5,6 +5,7 @@ #include "barretenberg/common/thread.hpp" #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" #include "barretenberg/numeric/bitop/get_msb.hpp" +#include "barretenberg/proof_system/types/circuit_type.hpp" #include #include @@ -57,8 +58,6 @@ EvaluationDomain::EvaluationDomain(const size_t domain_size, const size_t ta , log2_thread_size(static_cast(numeric::get_msb(thread_size))) , log2_num_threads(static_cast(numeric::get_msb(num_threads))) , generator_size(target_generator_size ? target_generator_size : domain_size) - , root(Fr::get_root_of_unity(log2_size)) - , root_inverse(root.invert()) , domain(Fr{ size, 0, 0, 0 }.to_montgomery_form()) , domain_inverse(domain.invert()) , generator(Fr::coset_generator(0)) @@ -66,6 +65,15 @@ EvaluationDomain::EvaluationDomain(const size_t domain_size, const size_t ta , four_inverse(Fr(4).invert()) , roots(nullptr) { + // Grumpkin does not have many roots of unity and, given these are not used for Honk, we set it to one. + if (proof_system::IsAnyOf) { + root = Fr::one(); + } else { + root = Fr::get_root_of_unity(log2_size); + } + + root_inverse = root.invert(); + ASSERT((1UL << log2_size) == size || (size == 0)); ASSERT((1UL << log2_thread_size) == thread_size || (size == 0)); ASSERT((1UL << log2_num_threads) == num_threads || (size == 0)); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.hpp index 7e69a80ee2fb..3726455677d1 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/evaluation_domain.hpp @@ -5,7 +5,7 @@ namespace barretenberg { -template class EvaluationDomain { +template class EvaluationDomain { public: EvaluationDomain() : size(0) @@ -15,13 +15,13 @@ template class EvaluationDomain { , log2_thread_size(0) , log2_num_threads(0) , generator_size(0) - , root(fr::zero()) - , root_inverse(fr::zero()) - , domain(fr::zero()) - , domain_inverse(fr::zero()) - , generator(fr::zero()) - , generator_inverse(fr::zero()) - , four_inverse(fr::zero()) + , root(FF::zero()) + , root_inverse(FF::zero()) + , domain(FF::zero()) + , domain_inverse(FF::zero()) + , generator(FF::zero()) + , generator_inverse(FF::zero()) + , four_inverse(FF::zero()) , roots(nullptr){}; EvaluationDomain(const size_t domain_size, const size_t target_generator_size = 0); @@ -36,8 +36,8 @@ template class EvaluationDomain { void compute_lookup_table(); void compute_generator_table(const size_t target_generator_size); - const std::vector& get_round_roots() const { return round_roots; }; - const std::vector& get_inverse_round_roots() const { return inverse_round_roots; } + const std::vector& get_round_roots() const { return round_roots; }; + const std::vector& get_inverse_round_roots() const { return inverse_round_roots; } size_t size; // n, always a power of 2 size_t num_threads; // num_threads * thread_size = size @@ -47,28 +47,28 @@ template class EvaluationDomain { size_t log2_num_threads; size_t generator_size; - Fr root; // omega; the nth root of unity - Fr root_inverse; // omega^{-1} - Fr domain; // n; same as size - Fr domain_inverse; // n^{-1} - Fr generator; - Fr generator_inverse; - Fr four_inverse; + FF root; // omega; the nth root of unity + FF root_inverse; // omega^{-1} + FF domain; // n; same as size + FF domain_inverse; // n^{-1} + FF generator; + FF generator_inverse; + FF four_inverse; private: - std::vector round_roots; // An entry for each of the log(n) rounds: each entry is a pointer to + std::vector round_roots; // An entry for each of the log(n) rounds: each entry is a pointer to // the subset of the roots of unity required for that fft round. // E.g. round_roots[0] = [1, ω^(n/2 - 1)], // round_roots[1] = [1, ω^(n/4 - 1), ω^(n/2 - 1), ω^(3n/4 - 1)] // ... - std::vector inverse_round_roots; + std::vector inverse_round_roots; - std::shared_ptr roots; + std::shared_ptr roots; }; // tell the compiler we will take care of instantiating these in the .cpp file extern template class EvaluationDomain; -extern template class EvaluationDomain; +// extern template class EvaluationDomain; // add alias for compatibility using evaluation_domain = EvaluationDomain; } // namespace barretenberg diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp index 7193a38937df..644144fc9f75 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/polynomials/polynomial_arithmetic.test.cpp @@ -9,6 +9,7 @@ #include using namespace barretenberg; +// TODO(#635): This tests should be typed to run on both barretenberg::fr and grumpkin::fr. TEST(polynomials, evaluation_domain) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp index ba60a0dd0ea7..dbdd25061d58 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/arithmetization/gate_data.hpp @@ -16,7 +16,6 @@ template struct add_triple_ { FF c_scaling; FF const_scaling; }; -using add_triple = add_triple_; template struct add_quad_ { uint32_t a; @@ -29,8 +28,6 @@ template struct add_quad_ { FF d_scaling; FF const_scaling; }; -using add_quad = add_quad_; - template struct mul_quad_ { uint32_t a; uint32_t b; @@ -43,8 +40,6 @@ template struct mul_quad_ { FF d_scaling; FF const_scaling; }; -using mul_quad = mul_quad_; - template struct mul_triple_ { uint32_t a; uint32_t b; @@ -53,8 +48,6 @@ template struct mul_triple_ { FF c_scaling; FF const_scaling; }; -using mul_triple = mul_triple_; - template struct poly_triple_ { uint32_t a; uint32_t b; @@ -67,9 +60,7 @@ template struct poly_triple_ { friend bool operator==(poly_triple_ const& lhs, poly_triple_ const& rhs) = default; }; - using poly_triple = poly_triple_; - struct ecc_op_tuple { uint32_t op; uint32_t x_lo; @@ -80,7 +71,7 @@ struct ecc_op_tuple { uint32_t z_hi; }; -template inline void read(B& buf, poly_triple& constraint) +template inline void read(B& buf, poly_triple_& constraint) { using serialize::read; read(buf, constraint.a); @@ -92,7 +83,7 @@ template inline void read(B& buf, poly_triple& constraint) read(buf, constraint.q_o); read(buf, constraint.q_c); } -template inline void write(B& buf, poly_triple const& constraint) +template inline void write(B& buf, poly_triple_ const& constraint) { using serialize::write; write(buf, constraint.a); @@ -115,23 +106,17 @@ template struct fixed_group_add_quad_ { FF q_y_1; FF q_y_2; }; -using fixed_group_add_quad = fixed_group_add_quad_; - template struct fixed_group_init_quad_ { FF q_x_1; FF q_x_2; FF q_y_1; FF q_y_2; }; -using fixed_group_init_quad = fixed_group_init_quad_; - template struct accumulator_triple_ { std::vector left; std::vector right; std::vector out; }; -using accumulator_triple = accumulator_triple_; - template struct ecc_add_gate_ { uint32_t x1; uint32_t y1; @@ -142,5 +127,4 @@ template struct ecc_add_gate_ { FF endomorphism_coefficient; FF sign_coefficient; }; -using ecc_add_gate = ecc_add_gate_; } // namespace proof_system diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_translator_circuit_builder.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_translator_circuit_builder.hpp index cf6b8e0899e5..49edf02b275e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_translator_circuit_builder.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/goblin_translator_circuit_builder.hpp @@ -24,10 +24,10 @@ class GoblinTranslatorCircuitBuilder : CircuitBuilderBase&) override{}; + void create_mul_gate(const mul_triple_&) override{}; void create_bool_gate(const uint32_t) override{}; - void create_poly_gate(const poly_triple&) override{}; + void create_poly_gate(const poly_triple_&) override{}; [[nodiscard]] size_t get_num_constant_gates() const override { return 0; }; /** diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.cpp index 025699f52193..100e4045706a 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.cpp @@ -301,14 +301,14 @@ std::vector StandardCircuitBuilder_::decompose_into_base4_accumula } template -accumulator_triple StandardCircuitBuilder_::create_logic_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits, - const bool is_xor_gate) +accumulator_triple_ StandardCircuitBuilder_::create_logic_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits, + const bool is_xor_gate) { this->assert_valid_variables({ a, b }); - accumulator_triple accumulators; + accumulator_triple_ accumulators; const uint256_t left_witness_value(this->get_variable(a)); const uint256_t right_witness_value(this->get_variable(b)); @@ -457,17 +457,17 @@ template uint32_t StandardCircuitBuilder_::put_constant_variab } template -accumulator_triple StandardCircuitBuilder_::create_and_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits) +accumulator_triple_ StandardCircuitBuilder_::create_and_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits) { return create_logic_constraint(a, b, num_bits, false); } template -accumulator_triple StandardCircuitBuilder_::create_xor_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits) +accumulator_triple_ StandardCircuitBuilder_::create_xor_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits) { return create_logic_constraint(a, b, num_bits, true); } @@ -494,13 +494,16 @@ template bool StandardCircuitBuilder_::check_circuit() FF gate_sum; FF left, right, output; for (size_t i = 0; i < this->num_gates; i++) { + gate_sum = FF::zero(); left = this->get_variable(w_l[i]); right = this->get_variable(w_r[i]); output = this->get_variable(w_o[i]); gate_sum = q_m[i] * left * right + q_1[i] * left + q_2[i] * right + q_3[i] * output + q_c[i]; - if (!gate_sum.is_zero()) + if (!gate_sum.is_zero()) { + info("gate number", i); return false; + } } return true; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.hpp index a30bf488e19c..2c0230016090 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.hpp @@ -84,7 +84,7 @@ template class StandardCircuitBuilder_ : public CircuitBuilderBase const fixed_group_init_quad_& init); void create_fixed_group_add_gate_final(const add_quad_& in); - fixed_group_add_quad previous_add_quad; + fixed_group_add_quad_ previous_add_quad; // TODO(#216)(Adrian): This should be a virtual overridable method in the base class. void fix_witness(const uint32_t witness_index, const FF& witness_value); @@ -100,12 +100,12 @@ template class StandardCircuitBuilder_ : public CircuitBuilderBase decompose_into_base4_accumulators(variable_index, num_bits, msg); } - accumulator_triple create_logic_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits, - bool is_xor_gate); - accumulator_triple create_and_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); - accumulator_triple create_xor_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); + accumulator_triple_ create_logic_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits, + bool is_xor_gate); + accumulator_triple_ create_and_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); + accumulator_triple_ create_xor_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); // TODO(#216)(Adrian): The 2 following methods should be virtual in the base class uint32_t put_constant_variable(const FF& variable); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.test.cpp index c263b13f8bee..2eb80bb438ad 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/standard_circuit_builder.test.cpp @@ -24,11 +24,47 @@ TEST(standard_circuit_constructor, base_case) TEST(standard_circuit_constructor, grumpkin_base_case) { - StandardGrumpkinCircuitBuilder composer = StandardGrumpkinCircuitBuilder(); + StandardGrumpkinCircuitBuilder circuit_constructor = StandardGrumpkinCircuitBuilder(); grumpkin::fr a = grumpkin::fr::one(); - composer.add_public_variable(a); + circuit_constructor.add_public_variable(a); + grumpkin::fr b = grumpkin::fr::one(); + grumpkin::fr c = a + b; + grumpkin::fr d = a + c; + uint32_t a_idx = circuit_constructor.add_variable(a); + uint32_t b_idx = circuit_constructor.add_variable(b); + uint32_t c_idx = circuit_constructor.add_variable(c); + uint32_t d_idx = circuit_constructor.add_variable(d); + + uint32_t w_l_2_idx = circuit_constructor.add_variable(2); + uint32_t w_r_2_idx = circuit_constructor.add_variable(2); + uint32_t w_o_2_idx = circuit_constructor.add_variable(4); + circuit_constructor.create_mul_gate({ w_l_2_idx, w_r_2_idx, w_o_2_idx, 1, -1, 0 }); + + circuit_constructor.create_add_gate({ a_idx, + b_idx, + c_idx, + grumpkin::fr::one(), + grumpkin::fr::one(), + grumpkin::fr::neg_one(), + grumpkin::fr::zero() }); + + circuit_constructor.create_add_gate({ d_idx, + c_idx, + a_idx, + grumpkin::fr::one(), + grumpkin::fr::neg_one(), + grumpkin::fr::neg_one(), + grumpkin::fr::zero() }); + + circuit_constructor.create_add_gate({ d_idx, + c_idx, + b_idx, + grumpkin::fr::one(), + grumpkin::fr::neg_one(), + grumpkin::fr::neg_one(), + grumpkin::fr::zero() }); - bool result = composer.check_circuit(); + bool result = circuit_constructor.check_circuit(); EXPECT_EQ(result, true); } @@ -229,7 +265,7 @@ TEST(standard_circuit_constructor, and_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = + auto accumulators = circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); // circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); @@ -297,7 +333,7 @@ TEST(standard_circuit_constructor, xor_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = + auto accumulators = circuit_constructor.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { @@ -361,17 +397,16 @@ TEST(standard_circuit_constructor, big_add_gate_with_bit_extract) uint32_t input = engine.get_random_uint32(); uint32_t output = input + (quad_value > 1 ? 1 : 0); - add_quad gate{ circuit_constructor.add_variable(uint256_t(input)), - circuit_constructor.add_variable(uint256_t(output)), - right_idx, - left_idx, - fr(6), - -fr(6), - fr::zero(), - fr::zero(), - fr::zero() }; - - circuit_constructor.create_big_add_gate_with_bit_extraction(gate); + circuit_constructor.create_big_add_gate_with_bit_extraction( + { circuit_constructor.add_variable(uint256_t(input)), + circuit_constructor.add_variable(uint256_t(output)), + right_idx, + left_idx, + fr(6), + -fr(6), + fr::zero(), + fr::zero(), + fr::zero() }); }; generate_constraints(0); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.cpp index 3bfb16c48cf0..ba7a5d213ce9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.cpp @@ -33,7 +33,7 @@ TurboCircuitBuilder_::TurboCircuitBuilder_(const size_t size_hint) * @param in Specifies addition gate parameters: * w_l, w_r, w_o, q_1, q_2, q_3, q_c. * */ -template void TurboCircuitBuilder_::create_add_gate(const add_triple& in) +template void TurboCircuitBuilder_::create_add_gate(const add_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -64,7 +64,7 @@ template void TurboCircuitBuilder_::create_add_gate(const add_ * @param in Specifies addition gate parameters: * w_l, w_r, w_o, w_4, q_1, q_2, q_3, q_4, q_c. * */ -template void TurboCircuitBuilder_::create_big_add_gate(const add_quad& in) +template void TurboCircuitBuilder_::create_big_add_gate(const add_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -102,7 +102,7 @@ template void TurboCircuitBuilder_::create_big_add_gate(const * ensure this assumption is backed by a constraint (e.g., c and d could be accumulators produced using the TurboPLONK * function `decompose_into_base4_accumulators`). * */ -template void TurboCircuitBuilder_::create_big_add_gate_with_bit_extraction(const add_quad& in) +template void TurboCircuitBuilder_::create_big_add_gate_with_bit_extraction(const add_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -124,7 +124,7 @@ template void TurboCircuitBuilder_::create_big_add_gate_with_b ++this->num_gates; } -template void TurboCircuitBuilder_::create_big_mul_gate(const mul_quad& in) +template void TurboCircuitBuilder_::create_big_mul_gate(const mul_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -163,7 +163,7 @@ template void TurboCircuitBuilder_::create_big_mul_gate(const * @warning Even with the constraint on w_3, it is typically necessary to range constrain the wire value that will be * returned. */ -template void TurboCircuitBuilder_::create_balanced_add_gate(const add_quad& in) +template void TurboCircuitBuilder_::create_balanced_add_gate(const add_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -194,7 +194,7 @@ template void TurboCircuitBuilder_::create_balanced_add_gate(c * @param in Contains the values for w_l, w_r, w_o, * q_m, q_3, q_c. * */ -template void TurboCircuitBuilder_::create_mul_gate(const mul_triple& in) +template void TurboCircuitBuilder_::create_mul_gate(const mul_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -255,7 +255,7 @@ template void TurboCircuitBuilder_::create_bool_gate(const uin * @param in Contains the values for * w_l, w_r, w_o, q_m, q_1, q_2, q_3, q_c. * */ -template void TurboCircuitBuilder_::create_poly_gate(const poly_triple& in) +template void TurboCircuitBuilder_::create_poly_gate(const poly_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -283,7 +283,7 @@ template void TurboCircuitBuilder_::create_poly_gate(const pol * * @param in Witnesses and values of two points. * */ -template void TurboCircuitBuilder_::create_fixed_group_add_gate(const fixed_group_add_quad& in) +template void TurboCircuitBuilder_::create_fixed_group_add_gate(const fixed_group_add_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -314,8 +314,8 @@ template void TurboCircuitBuilder_::create_fixed_group_add_gat * @param init Initialization parameters (points). * */ template -void TurboCircuitBuilder_::create_fixed_group_add_gate_with_init(const fixed_group_add_quad& in, - const fixed_group_init_quad& init) +void TurboCircuitBuilder_::create_fixed_group_add_gate_with_init(const fixed_group_add_quad_& in, + const fixed_group_init_quad_& init) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -339,7 +339,7 @@ void TurboCircuitBuilder_::create_fixed_group_add_gate_with_init(const fixed ++this->num_gates; } -template void TurboCircuitBuilder_::create_fixed_group_add_gate_final(const add_quad& in) +template void TurboCircuitBuilder_::create_fixed_group_add_gate_final(const add_quad_& in) { create_big_add_gate(in); } @@ -563,10 +563,10 @@ std::vector TurboCircuitBuilder_::decompose_into_base4_accumulator * The same holds, mutatis mutandis, for T.right. */ template -accumulator_triple TurboCircuitBuilder_::create_logic_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits, - const bool is_xor_gate) +accumulator_triple_ TurboCircuitBuilder_::create_logic_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits, + const bool is_xor_gate) { this->assert_valid_variables({ a, b }); @@ -647,7 +647,7 @@ accumulator_triple TurboCircuitBuilder_::create_logic_constraint(const uint3 const uint256_t left_witness_value(this->get_variable(a)); const uint256_t right_witness_value(this->get_variable(b)); - accumulator_triple accumulators; + accumulator_triple_ accumulators; FF left_accumulator = FF::zero(); FF right_accumulator = FF::zero(); FF out_accumulator = FF::zero(); @@ -751,17 +751,17 @@ accumulator_triple TurboCircuitBuilder_::create_logic_constraint(const uint3 } template -accumulator_triple TurboCircuitBuilder_::create_and_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits) +accumulator_triple_ TurboCircuitBuilder_::create_and_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits) { return create_logic_constraint(a, b, num_bits, false); } template -accumulator_triple TurboCircuitBuilder_::create_xor_constraint(const uint32_t a, - const uint32_t b, - const size_t num_bits) +accumulator_triple_ TurboCircuitBuilder_::create_xor_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits) { return create_logic_constraint(a, b, num_bits, true); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.hpp index 9ec56e68df7e..7ab81feef2d3 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.hpp @@ -54,19 +54,20 @@ template class TurboCircuitBuilder_ : public CircuitBuilderBase& in); - void create_big_add_gate(const add_quad& in); - void create_big_add_gate_with_bit_extraction(const add_quad& in); - void create_big_mul_gate(const mul_quad& in); - void create_balanced_add_gate(const add_quad& in); + void create_big_add_gate(const add_quad_& in); + void create_big_add_gate_with_bit_extraction(const add_quad_& in); + void create_big_mul_gate(const mul_quad_& in); + void create_balanced_add_gate(const add_quad_& in); - void create_mul_gate(const mul_triple& in); + void create_mul_gate(const mul_triple_& in); void create_bool_gate(const uint32_t a); - void create_poly_gate(const poly_triple& in); - void create_fixed_group_add_gate(const fixed_group_add_quad& in); - void create_fixed_group_add_gate_with_init(const fixed_group_add_quad& in, const fixed_group_init_quad& init); - void create_fixed_group_add_gate_final(const add_quad& in); + void create_poly_gate(const poly_triple_& in); + void create_fixed_group_add_gate(const fixed_group_add_quad_& in); + void create_fixed_group_add_gate_with_init(const fixed_group_add_quad_& in, + const fixed_group_init_quad_& init); + void create_fixed_group_add_gate_final(const add_quad_& in); void fix_witness(const uint32_t witness_index, const FF& witness_value); FF arithmetic_gate_evaluation(const size_t index, const FF alpha_base); @@ -90,12 +91,12 @@ template class TurboCircuitBuilder_ : public CircuitBuilderBase create_logic_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits, + bool is_xor_gate); + accumulator_triple_ create_and_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); + accumulator_triple_ create_xor_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); uint32_t put_constant_variable(const FF& variable); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.test.cpp index 6b84323572aa..27b3398651b7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/turbo_circuit_builder.test.cpp @@ -221,16 +221,11 @@ TEST(turbo_circuit_constructor, small_scalar_multipliers) } grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; - TurboCircuitBuilder circuit_constructor = TurboCircuitBuilder(); fr x_alpha = accumulator_offset; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = circuit_constructor.add_variable(accumulator_transcript[i]); round_quad.a = circuit_constructor.add_variable(multiplication_transcript[i].x); round_quad.b = circuit_constructor.add_variable(multiplication_transcript[i].y); @@ -248,20 +243,23 @@ TEST(turbo_circuit_constructor, small_scalar_multipliers) if (i > 0) { circuit_constructor.create_fixed_group_add_gate(round_quad); } else { - circuit_constructor.create_fixed_group_add_gate_with_init(round_quad, init_quad); + circuit_constructor.create_fixed_group_add_gate_with_init(round_quad, + { origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }); } } - add_quad add_quad{ circuit_constructor.add_variable(multiplication_transcript[num_quads].x), - circuit_constructor.add_variable(multiplication_transcript[num_quads].y), - circuit_constructor.add_variable(x_alpha), - circuit_constructor.add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; - circuit_constructor.create_big_add_gate(add_quad); + circuit_constructor.create_big_add_gate({ circuit_constructor.add_variable(multiplication_transcript[num_quads].x), + circuit_constructor.add_variable(multiplication_transcript[num_quads].y), + circuit_constructor.add_variable(x_alpha), + circuit_constructor.add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }); grumpkin::g1::element expected_point = grumpkin::g1::element(generator * scalar_multiplier.to_montgomery_form()).normalize(); @@ -345,16 +343,11 @@ TEST(turbo_circuit_constructor, large_scalar_multipliers) } grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; - TurboCircuitBuilder circuit_constructor = TurboCircuitBuilder(); fr x_alpha = accumulator_offset; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = circuit_constructor.add_variable(accumulator_transcript[i]); round_quad.a = circuit_constructor.add_variable(multiplication_transcript[i].x); round_quad.b = circuit_constructor.add_variable(multiplication_transcript[i].y); @@ -372,20 +365,23 @@ TEST(turbo_circuit_constructor, large_scalar_multipliers) if (i > 0) { circuit_constructor.create_fixed_group_add_gate(round_quad); } else { - circuit_constructor.create_fixed_group_add_gate_with_init(round_quad, init_quad); + circuit_constructor.create_fixed_group_add_gate_with_init(round_quad, + { origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }); } } - add_quad add_quad{ circuit_constructor.add_variable(multiplication_transcript[num_quads].x), - circuit_constructor.add_variable(multiplication_transcript[num_quads].y), - circuit_constructor.add_variable(x_alpha), - circuit_constructor.add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; - circuit_constructor.create_big_add_gate(add_quad); + circuit_constructor.create_big_add_gate({ circuit_constructor.add_variable(multiplication_transcript[num_quads].x), + circuit_constructor.add_variable(multiplication_transcript[num_quads].y), + circuit_constructor.add_variable(x_alpha), + circuit_constructor.add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }); grumpkin::g1::element expected_point = grumpkin::g1::element(generator * scalar_multiplier.to_montgomery_form()).normalize(); @@ -474,8 +470,7 @@ TEST(turbo_circuit_constructor, and_constraint_failure) uint32_t right_witness_index = circuit_constructor.add_variable(right_witness_value); // 4 && 5 is 4, so 3 bits are needed, but we only constrain 2 - accumulator_triple accumulators = - circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 2); + auto accumulators = circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 2); bool result = circuit_constructor.check_circuit(); @@ -504,7 +499,7 @@ TEST(turbo_circuit_constructor, and_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = + auto accumulators = circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); // circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 32 + extra_bits); @@ -571,8 +566,7 @@ TEST(turbo_circuit_constructor, xor_constraint_failure) uint32_t right_witness_index = circuit_constructor.add_variable(right_witness_value); // 4 && 1 is 5, so 3 bits are needed, but we only constrain 2 - accumulator_triple accumulators = - circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 2); + auto accumulators = circuit_constructor.create_and_constraint(left_witness_index, right_witness_index, 2); bool result = circuit_constructor.check_circuit(); @@ -601,7 +595,7 @@ TEST(turbo_circuit_constructor, xor_constraint) // include non-nice numbers of bits, that will bleed over gate boundaries size_t extra_bits = 2 * (i % 4); - accumulator_triple accumulators = + auto accumulators = circuit_constructor.create_xor_constraint(left_witness_index, right_witness_index, 32 + extra_bits); for (uint32_t j = 0; j < 16; ++j) { @@ -665,17 +659,16 @@ TEST(turbo_circuit_constructor, big_add_gate_with_bit_extract) uint32_t input = engine.get_random_uint32(); uint32_t output = input + (quad_value > 1 ? 1 : 0); - add_quad gate{ circuit_constructor.add_variable(uint256_t(input)), - circuit_constructor.add_variable(uint256_t(output)), - right_idx, - left_idx, - fr(6), - -fr(6), - fr::zero(), - fr::zero(), - fr::zero() }; - - circuit_constructor.create_big_add_gate_with_bit_extraction(gate); + circuit_constructor.create_big_add_gate_with_bit_extraction( + { circuit_constructor.add_variable(uint256_t(input)), + circuit_constructor.add_variable(uint256_t(output)), + right_idx, + left_idx, + fr(6), + -fr(6), + fr::zero(), + fr::zero(), + fr::zero() }); }; generate_constraints(0); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp index fad06b949fea..d96dfce13c91 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp @@ -118,7 +118,7 @@ template void UltraCircuitBuilder_::add_gates_to_ensure_all_po * * @param in A structure with variable indexes and selector values for the gate. */ -template void UltraCircuitBuilder_::create_add_gate(const add_triple& in) +template void UltraCircuitBuilder_::create_add_gate(const add_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -149,7 +149,7 @@ template void UltraCircuitBuilder_::create_add_gate(const add_ * @param include_next_gate_w_4 Switches on/off the addition of w_4 at the next index */ template -void UltraCircuitBuilder_::create_big_add_gate(const add_quad& in, const bool include_next_gate_w_4) +void UltraCircuitBuilder_::create_big_add_gate(const add_quad_& in, const bool include_next_gate_w_4) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); w_l.emplace_back(in.a); @@ -176,7 +176,7 @@ void UltraCircuitBuilder_::create_big_add_gate(const add_quad& in, const boo * * @param in Structure with variables and witness selector values */ -template void UltraCircuitBuilder_::create_big_add_gate_with_bit_extraction(const add_quad& in) +template void UltraCircuitBuilder_::create_big_add_gate_with_bit_extraction(const add_quad_& in) { // This method is an artifact of a turbo plonk feature that implicitly extracts // a high or low bit from a base-4 quad and adds it into the arithmetic gate relationship. @@ -239,7 +239,7 @@ template void UltraCircuitBuilder_::create_big_add_gate_with_b * * @param in Structure containing variables and witness selectors */ -template void UltraCircuitBuilder_::create_big_mul_gate(const mul_quad& in) +template void UltraCircuitBuilder_::create_big_mul_gate(const mul_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -263,7 +263,7 @@ template void UltraCircuitBuilder_::create_big_mul_gate(const // Creates a width-4 addition gate, where the fourth witness must be a boolean. // Can be used to normalize a 32-bit addition -template void UltraCircuitBuilder_::create_balanced_add_gate(const add_quad& in) +template void UltraCircuitBuilder_::create_balanced_add_gate(const add_quad_& in) { this->assert_valid_variables({ in.a, in.b, in.c, in.d }); @@ -303,7 +303,7 @@ template void UltraCircuitBuilder_::create_balanced_add_gate(c * * @param in Structure containing variables and witness selectors */ -template void UltraCircuitBuilder_::create_mul_gate(const mul_triple& in) +template void UltraCircuitBuilder_::create_mul_gate(const mul_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -358,7 +358,7 @@ template void UltraCircuitBuilder_::create_bool_gate(const uin * * @param in Structure containing variables and witness selectors */ -template void UltraCircuitBuilder_::create_poly_gate(const poly_triple& in) +template void UltraCircuitBuilder_::create_poly_gate(const poly_triple_& in) { this->assert_valid_variables({ in.a, in.b, in.c }); @@ -391,7 +391,7 @@ template void UltraCircuitBuilder_::create_poly_gate(const pol * added, the resulting point coordinates and the selector values that describe whether the endomorphism is used on the * second point and whether it is negated. */ -template void UltraCircuitBuilder_::create_ecc_add_gate(const ecc_add_gate& in) +template void UltraCircuitBuilder_::create_ecc_add_gate(const ecc_add_gate_& in) { /** * | 1 | 2 | 3 | 4 | @@ -3535,4 +3535,7 @@ template bool UltraCircuitBuilder_::check_circuit() return result; } template class UltraCircuitBuilder_; +// To enable this we need to template plookup +// template class UltraCircuitBuilder_; + } // namespace proof_system \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp index 29220028f6ad..2ff9f0eb70ac 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp @@ -644,17 +644,17 @@ template class UltraCircuitBuilder_ : public CircuitBuilderBase& in) override; - void create_big_add_gate(const add_quad& in, const bool use_next_gate_w_4 = false); - void create_big_add_gate_with_bit_extraction(const add_quad& in); - void create_big_mul_gate(const mul_quad& in); - void create_balanced_add_gate(const add_quad& in); + void create_big_add_gate(const add_quad_& in, const bool use_next_gate_w_4 = false); + void create_big_add_gate_with_bit_extraction(const add_quad_& in); + void create_big_mul_gate(const mul_quad_& in); + void create_balanced_add_gate(const add_quad_& in); - void create_mul_gate(const mul_triple& in) override; + void create_mul_gate(const mul_triple_& in) override; void create_bool_gate(const uint32_t a) override; - void create_poly_gate(const poly_triple& in) override; - void create_ecc_add_gate(const ecc_add_gate& in); + void create_poly_gate(const poly_triple_& in) override; + void create_ecc_add_gate(const ecc_add_gate_& in); void fix_witness(const uint32_t witness_index, const FF& witness_value); @@ -678,7 +678,7 @@ template class UltraCircuitBuilder_ : public CircuitBuilderBase{ .a = variable_index, .b = variable_index, .c = variable_index, @@ -694,12 +694,12 @@ template class UltraCircuitBuilder_ : public CircuitBuilderBase create_logic_constraint(const uint32_t a, + const uint32_t b, + const size_t num_bits, + bool is_xor_gate); + accumulator_triple_ create_and_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); + accumulator_triple_ create_xor_constraint(const uint32_t a, const uint32_t b, const size_t num_bits); uint32_t put_constant_variable(const FF& variable); @@ -1201,5 +1201,8 @@ template class UltraCircuitBuilder_ : public CircuitBuilderBase; +// TODO: template plookup to be able to be able to have UltraCircuitBuilder on Grumpkin +// extern template class UltraCircuitBuilder_; using UltraCircuitBuilder = UltraCircuitBuilder_; +// using UltraGrumpkinCircuitBuilder = UltraCircuitBuilder_; } // namespace proof_system diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.test.cpp index d994c0b9235b..5ed67a0e028d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.test.cpp @@ -147,8 +147,7 @@ TEST(ultra_circuit_constructor, test_elliptic_gate) uint32_t x3 = circuit_constructor.add_variable(p3.x); uint32_t y3 = circuit_constructor.add_variable(p3.y); - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, 1, 1 }); grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); affine_element p2_endo = p2; @@ -156,15 +155,13 @@ TEST(ultra_circuit_constructor, test_elliptic_gate) p3 = affine_element(element(p1) + element(p2_endo)); x3 = circuit_constructor.add_variable(p3.x); y3 = circuit_constructor.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta, 1 }); p2_endo.x *= beta; p3 = affine_element(element(p1) - element(p2_endo)); x3 = circuit_constructor.add_variable(p3.x); y3 = circuit_constructor.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }); auto saved_state = UltraCircuitBuilder::CircuitDataBackup::store_full_state(circuit_constructor); bool result = circuit_constructor.check_circuit(); @@ -172,8 +169,7 @@ TEST(ultra_circuit_constructor, test_elliptic_gate) EXPECT_EQ(result, true); EXPECT_TRUE(saved_state.is_same_state(circuit_constructor)); - gate = ecc_add_gate{ x1 + 1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - circuit_constructor.create_ecc_add_gate(gate); + circuit_constructor.create_ecc_add_gate({ x1 + 1, y1, x2, y2, x3, y3, beta.sqr(), -1 }); EXPECT_EQ(circuit_constructor.check_circuit(), false); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp index 5b8bce15f81c..b4934e7186f0 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.hpp @@ -45,7 +45,7 @@ void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circu // Copy the selector values for all gates, keeping the rows at which we store public inputs as 0. // Initializing the polynomials in this way automatically applies 0-padding to the selectors. - barretenberg::polynomial selector_poly_lagrange(proving_key->circuit_size); + typename Flavor::Polynomial selector_poly_lagrange(proving_key->circuit_size); for (size_t i = 0; i < selector_values.size(); ++i) { selector_poly_lagrange[i + gate_offset] = selector_values[i]; } @@ -75,10 +75,10 @@ void construct_selector_polynomials(const typename Flavor::CircuitBuilder& circu * @param circuit_constructor * @param dyadic_circuit_size Power of 2 circuit size * - * @return std::vector + * @return std::vector * */ template -std::vector construct_wire_polynomials_base( +std::vector construct_wire_polynomials_base( const typename Flavor::CircuitBuilder& circuit_constructor, const size_t dyadic_circuit_size) { // Determine size of each block of data in the wire polynomials @@ -96,13 +96,13 @@ std::vector construct_wire_polynomials_base( size_t pub_input_offset = num_zero_rows + num_ecc_op_gates; size_t gate_offset = num_zero_rows + num_ecc_op_gates + num_public_inputs; - std::vector wire_polynomials; + std::vector wire_polynomials; // Populate the wire polynomials with values from ecc op gates, public inputs and conventional wires for (size_t wire_idx = 0; wire_idx < Flavor::NUM_WIRES; ++wire_idx) { // Expect all values to be set to 0 initially - barretenberg::polynomial w_lagrange(dyadic_circuit_size); + typename Flavor::Polynomial w_lagrange(dyadic_circuit_size); // Insert leading zero row into wire poly (for clarity; not stricly necessary due to zero-initialization) for (size_t i = 0; i < num_zero_rows; ++i) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.test.cpp index 0df8d5066613..2d9a85a410d7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/composer_lib.test.cpp @@ -14,7 +14,7 @@ class ComposerLibTests : public ::testing::Test { using FF = typename Flavor::FF; Flavor::CircuitBuilder circuit_constructor; Flavor::ProvingKey proving_key = []() { - auto crs_factory = barretenberg::srs::factories::CrsFactory(); + auto crs_factory = barretenberg::srs::factories::CrsFactory(); auto crs = crs_factory.get_prover_crs(4); return Flavor::ProvingKey(/*circuit_size=*/4, /*num_public_inputs=*/0); }(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp index fe7994ce6fc4..65c5e318e998 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.hpp @@ -62,7 +62,9 @@ namespace { /** * @brief Compute all CyclicPermutations of the circuit. Each CyclicPermutation represents the indices of the values in - * the witness wires that must have the same value. + * the witness wires that must have the same value. using Curve = curve::BN254; + using FF = Curve::ScalarField; + using Polynomial = barretenberg::Polynomial; * * @tparam program_width Program width * @@ -284,6 +286,7 @@ void compute_honk_style_permutation_lagrange_polynomials_from_mapping( std::array, Flavor::NUM_WIRES>& permutation_mappings, typename Flavor::ProvingKey* proving_key) { + using FF = typename Flavor::FF; const size_t num_gates = proving_key->circuit_size; size_t wire_index = 0; @@ -299,16 +302,14 @@ void compute_honk_style_permutation_lagrange_polynomials_from_mapping( // -(i+1) -> (n+i) // These indices are chosen so they can easily be computed by the verifier. They can expect the running // product to be equal to the "public input delta" that is computed in - current_permutation_poly[i] = - -barretenberg::fr(current_mapping.row_index + 1 + num_gates * current_mapping.column_index); + current_permutation_poly[i] = -FF(current_mapping.row_index + 1 + num_gates * current_mapping.column_index); } else if (current_mapping.is_tag) { // Set evaluations to (arbitrary) values disjoint from non-tag values current_permutation_poly[i] = num_gates * Flavor::NUM_WIRES + current_mapping.row_index; } else { // For the regular permutation we simply point to the next location by setting the evaluation to its // index - current_permutation_poly[i] = - barretenberg::fr(current_mapping.row_index + num_gates * current_mapping.column_index); + current_permutation_poly[i] = FF(current_mapping.row_index + num_gates * current_mapping.column_index); } ITERATE_OVER_DOMAIN_END; wire_index++; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.test.cpp index ee9125606a41..4d9bb3592908 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/composer/permutation_lib.test.cpp @@ -14,7 +14,8 @@ class PermutationHelperTests : public ::testing::Test { using FF = typename Flavor::FF; using ProvingKey = Flavor::ProvingKey; Flavor::CircuitBuilder circuit_constructor; - barretenberg::srs::factories::CrsFactory crs_factory = barretenberg::srs::factories::CrsFactory(); + barretenberg::srs::factories::CrsFactory crs_factory = + barretenberg::srs::factories::CrsFactory(); std::shared_ptr proving_key; virtual void SetUp() diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/crs_factory.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/crs_factory.hpp index 1cffa463bdce..8c5f659bdfb1 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/crs_factory.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/crs_factory.hpp @@ -3,6 +3,7 @@ #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/bn254/g1.hpp" #include "barretenberg/ecc/curves/bn254/g2.hpp" +#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" #include namespace barretenberg::pairing { @@ -26,30 +27,43 @@ template class ProverCrs { virtual size_t get_monomial_size() const = 0; }; -class VerifierCrs { +template class VerifierCrs { public: virtual ~VerifierCrs() = default; - ; - - virtual barretenberg::g2::affine_element get_g2x() const = 0; +}; +template <> class VerifierCrs { + using Curve = curve::BN254; + public: + virtual Curve::G2AffineElement get_g2x() const = 0; virtual barretenberg::pairing::miller_lines const* get_precomputed_g2_lines() const = 0; + virtual Curve::AffineElement get_first_g1() const = 0; +}; + +template <> class VerifierCrs { + using Curve = curve::Grumpkin; + + public: + virtual Curve::AffineElement* get_monomial_points() const = 0; + virtual size_t get_monomial_size() const = 0; + virtual Curve::AffineElement get_first_g1() const = 0; }; /** * A factory class to return the prover crs and verifier crs on request. * You can construct an empty placeholder factory, because composers need to be given a factory at construction time. */ -class CrsFactory { +template class CrsFactory { public: CrsFactory() = default; CrsFactory(CrsFactory&& other) = default; virtual ~CrsFactory() = default; - virtual std::shared_ptr> get_prover_crs(size_t) + virtual std::shared_ptr> get_prover_crs(size_t) { return nullptr; } + virtual std::shared_ptr> get_verifier_crs( + [[maybe_unused]] size_t degree = 0) { return nullptr; } - virtual std::shared_ptr get_verifier_crs() { return nullptr; } }; -} // namespace barretenberg::srs::factories +} // namespace barretenberg::srs::factories \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.cpp index fbc15949cd4a..602300657dc1 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.cpp @@ -3,67 +3,79 @@ #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/bn254/g1.hpp" #include "barretenberg/ecc/curves/bn254/pairing.hpp" +#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" #include "barretenberg/ecc/scalar_multiplication/point_table.hpp" #include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp" namespace barretenberg::srs::factories { -template -FileProverCrs::FileProverCrs(const size_t num_points, std::string const& path) - : num_points(num_points) -{ - monomials_ = scalar_multiplication::point_table_alloc(num_points); - - srs::IO::read_transcript_g1(monomials_.get(), num_points, path); - scalar_multiplication::generate_pippenger_point_table(monomials_.get(), monomials_.get(), num_points); -} - -FileVerifierCrs::FileVerifierCrs(std::string const& path) +FileVerifierCrs::FileVerifierCrs(std::string const& path, const size_t) : precomputed_g2_lines( (barretenberg::pairing::miller_lines*)(aligned_alloc(64, sizeof(barretenberg::pairing::miller_lines) * 2))) { - + using Curve = curve::BN254; + auto point_buf = scalar_multiplication::point_table_alloc(1); + srs::IO::read_transcript_g1(point_buf.get(), 1, path); srs::IO::read_transcript_g2(g2_x, path); barretenberg::pairing::precompute_miller_lines(barretenberg::g2::one, precomputed_g2_lines[0]); barretenberg::pairing::precompute_miller_lines(g2_x, precomputed_g2_lines[1]); + first_g1 = point_buf[0]; } -FileVerifierCrs::~FileVerifierCrs() +FileVerifierCrs::~FileVerifierCrs() { aligned_free(precomputed_g2_lines); } -g2::affine_element FileVerifierCrs::get_g2x() const +FileVerifierCrs::FileVerifierCrs(std::string const& path, const size_t num_points) + : num_points(num_points) { - return g2_x; + using Curve = curve::Grumpkin; + monomials_ = scalar_multiplication::point_table_alloc(num_points); + srs::IO::read_transcript_g1(monomials_.get(), num_points, path); + scalar_multiplication::generate_pippenger_point_table(monomials_.get(), monomials_.get(), num_points); + first_g1 = monomials_[0]; +}; + +curve::Grumpkin::AffineElement* FileVerifierCrs::get_monomial_points() const +{ + return monomials_.get(); } -pairing::miller_lines const* FileVerifierCrs::get_precomputed_g2_lines() const +size_t FileVerifierCrs::get_monomial_size() const { - return precomputed_g2_lines; + return num_points; } -FileCrsFactory::FileCrsFactory(std::string path, size_t initial_degree) +template +FileCrsFactory::FileCrsFactory(std::string path, size_t initial_degree) : path_(std::move(path)) , degree_(initial_degree) - , verifier_crs_(std::make_shared(path_)) {} -std::shared_ptr> FileCrsFactory::get_prover_crs(size_t degree) +template +std::shared_ptr> FileCrsFactory::get_prover_crs(size_t degree) { if (degree != degree_ || !prover_crs_) { - prover_crs_ = std::make_shared>(degree, path_); + prover_crs_ = std::make_shared>(degree, path_); degree_ = degree; } return prover_crs_; } -std::shared_ptr FileCrsFactory::get_verifier_crs() +template +std::shared_ptr> FileCrsFactory::get_verifier_crs(size_t degree) { + if (degree != degree_ || !verifier_crs_) { + verifier_crs_ = std::make_shared>(path_, degree); + degree_ = degree; + } return verifier_crs_; } template class FileProverCrs; template class FileProverCrs; +template class FileCrsFactory; +template class FileCrsFactory; -} // namespace barretenberg::srs::factories \ No newline at end of file +} // namespace barretenberg::srs::factories diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.hpp index 83ea35d84e38..3a925e2ccf94 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/file_crs_factory.hpp @@ -1,15 +1,44 @@ #pragma once +#include "../io.hpp" #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" +#include "barretenberg/ecc/scalar_multiplication/point_table.hpp" +#include "barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp" #include "crs_factory.hpp" #include #include namespace barretenberg::srs::factories { +/** + * Create reference strings given a path to a directory of transcript files. + */ +template class FileCrsFactory : public CrsFactory { + public: + FileCrsFactory(std::string path, size_t initial_degree = 0); + FileCrsFactory(FileCrsFactory&& other) = default; + + std::shared_ptr> get_prover_crs(size_t degree) override; + + std::shared_ptr> get_verifier_crs(size_t degree = 0) override; + + private: + std::string path_; + size_t degree_; + std::shared_ptr> prover_crs_; + std::shared_ptr> verifier_crs_; +}; + template class FileProverCrs : public ProverCrs { public: - FileProverCrs(const size_t num_points, std::string const& path); + FileProverCrs(const size_t num_points, std::string const& path) + : num_points(num_points) + { + monomials_ = scalar_multiplication::point_table_alloc(num_points); + + srs::IO::read_transcript_g1(monomials_.get(), num_points, path); + scalar_multiplication::generate_pippenger_point_table(monomials_.get(), monomials_.get(), num_points); + }; typename Curve::AffineElement* get_monomial_points() { return monomials_.get(); } @@ -20,38 +49,41 @@ template class FileProverCrs : public ProverCrs { std::shared_ptr monomials_; }; -class FileVerifierCrs : public VerifierCrs { +template class FileVerifierCrs : public VerifierCrs { public: - FileVerifierCrs(std::string const& path); - - ~FileVerifierCrs(); + FileVerifierCrs(std::string const& path, const size_t num_points); +}; - g2::affine_element get_g2x() const override; +template <> class FileVerifierCrs : public VerifierCrs { + using Curve = curve::BN254; - pairing::miller_lines const* get_precomputed_g2_lines() const override; + public: + FileVerifierCrs(std::string const& path, const size_t num_points = 0); + virtual ~FileVerifierCrs(); + Curve::G2AffineElement get_g2x() const override { return g2_x; }; + pairing::miller_lines const* get_precomputed_g2_lines() const override { return precomputed_g2_lines; }; + Curve::AffineElement get_first_g1() const override { return first_g1; }; private: - g2::affine_element g2_x; + Curve::AffineElement first_g1; + Curve::G2AffineElement g2_x; pairing::miller_lines* precomputed_g2_lines; }; -/** - * Create reference strings given a path to a directory of transcript files. - */ -class FileCrsFactory : public CrsFactory { - public: - FileCrsFactory(std::string path, size_t initial_degree = 0); - FileCrsFactory(FileCrsFactory&& other) = default; - - std::shared_ptr> get_prover_crs(size_t degree) override; +template <> class FileVerifierCrs : public VerifierCrs { + using Curve = curve::Grumpkin; - std::shared_ptr get_verifier_crs() override; + public: + FileVerifierCrs(std::string const& path, const size_t num_points); + virtual ~FileVerifierCrs() = default; + Curve::AffineElement* get_monomial_points() const override; + size_t get_monomial_size() const override; + Curve::AffineElement get_first_g1() const override { return first_g1; }; private: - std::string path_; - size_t degree_; - std::shared_ptr> prover_crs_; - std::shared_ptr verifier_crs_; + Curve::AffineElement first_g1; + size_t num_points; + std::shared_ptr monomials_; }; extern template class FileProverCrs; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.cpp index 94ae89a76aa1..44315a8b7ca4 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.cpp @@ -30,24 +30,27 @@ class MemProverCrs : public ProverCrs { std::shared_ptr monomials_; }; -class MemVerifierCrs : public VerifierCrs { +class MemVerifierCrs : public VerifierCrs { public: MemVerifierCrs(g2::affine_element const& g2_point) : g2_x(g2_point) + , precomputed_g2_lines( + static_cast(aligned_alloc(64, sizeof(barretenberg::pairing::miller_lines) * 2))) { - precomputed_g2_lines = - (pairing::miller_lines*)(aligned_alloc(64, sizeof(barretenberg::pairing::miller_lines) * 2)); + barretenberg::pairing::precompute_miller_lines(barretenberg::g2::one, precomputed_g2_lines[0]); barretenberg::pairing::precompute_miller_lines(g2_x, precomputed_g2_lines[1]); } - ~MemVerifierCrs() { aligned_free(precomputed_g2_lines); } + virtual ~MemVerifierCrs() { aligned_free(precomputed_g2_lines); } - g2::affine_element get_g2x() const override { return g2_x; } + g2::affine_element get_g2x() const { return g2_x; } - pairing::miller_lines const* get_precomputed_g2_lines() const override { return precomputed_g2_lines; } + pairing::miller_lines const* get_precomputed_g2_lines() const { return precomputed_g2_lines; } + g1::affine_element get_first_g1() const { return first_g1x; }; private: + g1::affine_element first_g1x; g2::affine_element g2_x; pairing::miller_lines* precomputed_g2_lines; }; @@ -66,7 +69,7 @@ std::shared_ptr> MemCrsFac return prover_crs_; } -std::shared_ptr MemCrsFactory::get_verifier_crs() +std::shared_ptr> MemCrsFactory::get_verifier_crs(size_t) { return verifier_crs_; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.hpp index c4c9fba8d520..a4e6b90db6d7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.hpp @@ -10,19 +10,22 @@ namespace barretenberg::srs::factories { /** * Create reference strings given pointers to in memory buffers. + * + * This class is currently only used with wasm and works exclusively with the BN254 CRS. */ -class MemCrsFactory : public CrsFactory { +class MemCrsFactory : public CrsFactory { public: MemCrsFactory(std::vector const& points, g2::affine_element const g2_point); MemCrsFactory(MemCrsFactory&& other) = default; std::shared_ptr> get_prover_crs(size_t degree) override; - std::shared_ptr get_verifier_crs() override; + std::shared_ptr> get_verifier_crs( + size_t degree = 0) override; private: std::shared_ptr> prover_crs_; - std::shared_ptr verifier_crs_; + std::shared_ptr> verifier_crs_; }; } // namespace barretenberg::srs::factories diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.test.cpp index 57b725e9e2ac..2b50a60209de 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/factories/mem_crs_factory.test.cpp @@ -12,7 +12,7 @@ using namespace barretenberg::srs::factories; TEST(reference_string, mem_file_consistency) { // Load 1024 from file. - auto file_crs = FileCrsFactory("../srs_db/ignition", 1024); + auto file_crs = FileCrsFactory("../srs_db/ignition", 1024); // Use low level io lib to read 1024 from file. std::vector points(1024); @@ -43,3 +43,8 @@ TEST(reference_string, mem_file_consistency) sizeof(barretenberg::pairing::miller_lines) * 2), 0); } + +TEST(reference_string, grumpkin) +{ + auto file_crs = FileCrsFactory("../srs_db/grumpkin"); +} diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.cpp index 912a5758d7a1..232702dfa96c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.cpp @@ -4,26 +4,43 @@ #include "barretenberg/common/throw_or_abort.hpp" namespace { -std::shared_ptr crs_factory; -} +// TODO(#637): As a PoC we have two global variables for the two CRS but this could be improved to avoid duplication. +std::shared_ptr> crs_factory; +std::shared_ptr> grumpkin_crs_factory; +} // namespace namespace barretenberg::srs { +// Initialises the crs using the memory buffers void init_crs_factory(std::vector const& points, g2::affine_element const g2_point) { crs_factory = std::make_shared(points, g2_point); } +// Initialises crs from a file path this we use in the entire codebase void init_crs_factory(std::string crs_path) { - crs_factory = std::make_shared(crs_path); + crs_factory = std::make_shared>(crs_path); +} + +void init_grumpkin_crs_factory(std::string crs_path) +{ + grumpkin_crs_factory = std::make_shared>(crs_path); } -std::shared_ptr get_crs_factory() +std::shared_ptr> get_crs_factory() { if (!crs_factory) { - throw_or_abort("You need to initalize the global CRS with a call to init_crs_factory(...)!"); + throw_or_abort("You need vto initalize the global CRS with a call to init_crs_factory(...)!"); } return crs_factory; } + +std::shared_ptr> get_grumpkin_crs_factory() +{ + if (!grumpkin_crs_factory) { + throw_or_abort("You need vto initalize the global CRS with a call to init_grumpkin_crs_factory(...)!"); + } + return grumpkin_crs_factory; +} } // namespace barretenberg::srs \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.hpp index 08ba671a75b2..06ecd066d913 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/srs/global_crs.hpp @@ -1,11 +1,15 @@ #include "./factories/crs_factory.hpp" +#include "barretenberg/ecc/curves/bn254/bn254.hpp" +#include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" namespace barretenberg::srs { - void init_crs_factory(std::vector const& points, barretenberg::g2::affine_element const g2_point); void init_crs_factory(std::string crs_path); +void init_grumpkin_crs_factory(std::string crs_path); + +std::shared_ptr> get_crs_factory(); +std::shared_ptr> get_grumpkin_crs_factory(); -std::shared_ptr get_crs_factory(); } // namespace barretenberg::srs \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen.cpp index e642faecb0e2..28f2b8c5a0b4 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen.cpp @@ -143,10 +143,10 @@ point pedersen_hash::hash_single_internal(const field_t& in, grumpkin::g1::element::batch_normalize(&multiplication_transcript[0], num_quads + 1); - fixed_group_init_quad init_quad{ origin_points[0].x, - (origin_points[0].x - origin_points[1].x), - origin_points[0].y, - (origin_points[0].y - origin_points[1].y) }; + fixed_group_init_quad_ init_quad{ origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }; /** * Fill the gates as following: @@ -180,7 +180,7 @@ point pedersen_hash::hash_single_internal(const field_t& in, fr x_alpha = accumulator_offset; std::vector accumulator_witnesses; for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = ctx->add_variable(accumulator_transcript[i]); round_quad.a = ctx->add_variable(multiplication_transcript[i].x); round_quad.b = ctx->add_variable(multiplication_transcript[i].y); @@ -225,17 +225,16 @@ point pedersen_hash::hash_single_internal(const field_t& in, * - init_quad.q_x_2 * round_quad.d * + init_quad.q_x_2 * */ - mul_quad x_init_quad{ .a = round_quad.a, - .b = round_quad.c, - .c = 0, - .d = round_quad.d, - .mul_scaling = -1, - .a_scaling = 0, - .b_scaling = init_quad.q_x_1, - .c_scaling = 0, - .d_scaling = -init_quad.q_x_2, - .const_scaling = init_quad.q_x_2 }; - ctx->create_big_mul_gate(x_init_quad); + ctx->create_big_mul_gate({ .a = round_quad.a, + .b = round_quad.c, + .c = 0, + .d = round_quad.d, + .mul_scaling = -1, + .a_scaling = 0, + .b_scaling = init_quad.q_x_1, + .c_scaling = 0, + .d_scaling = -init_quad.q_x_2, + .const_scaling = init_quad.q_x_2 }); } gates.create_fixed_group_add_gate_with_init(round_quad, init_quad); }; @@ -245,15 +244,15 @@ point pedersen_hash::hash_single_internal(const field_t& in, // In Turbo PLONK, this effectively just adds the last row of the table as witnesses. // In Standard PLONK, this also creates the constraint involving the final two rows. - add_quad add_quad{ ctx->add_variable(multiplication_transcript[num_quads].x), - ctx->add_variable(multiplication_transcript[num_quads].y), - ctx->add_variable(x_alpha), - ctx->add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; + add_quad_ add_quad{ ctx->add_variable(multiplication_transcript[num_quads].x), + ctx->add_variable(multiplication_transcript[num_quads].y), + ctx->add_variable(x_alpha), + ctx->add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }; gates.create_fixed_group_add_gate_final(add_quad); accumulator_witnesses.push_back(add_quad.d); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_gates.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_gates.hpp index b91574306ec7..1a5cb6ac44ef 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_gates.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_gates.hpp @@ -22,9 +22,10 @@ namespace stdlib { */ template class pedersen_gates { public: - using fixed_group_add_quad = proof_system::fixed_group_add_quad; - using fixed_group_init_quad = proof_system::fixed_group_init_quad; - using add_quad = proof_system::add_quad; + using FF = typename Composer::FF; + using fixed_group_add_quad = proof_system::fixed_group_add_quad_; + using fixed_group_init_quad = proof_system::fixed_group_init_quad_; + using add_quad = proof_system::add_quad_; Composer* context; fixed_group_add_quad previous_add_quad; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_plookup.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_plookup.cpp index 67e95f1735c7..164906d7b818 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_plookup.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/hash/pedersen/pedersen_plookup.cpp @@ -67,10 +67,14 @@ point pedersen_plookup_hash::add_points(const point& p1, const point& p2, point p3{ witness_t(ctx, x_3_raw), witness_t(ctx, y_3_raw) }; - ecc_add_gate add_gate = - ecc_add_gate{ p1.x.witness_index, p1.y.witness_index, p2.x.witness_index, p2.y.witness_index, - p3.x.witness_index, p3.y.witness_index, endomorphism_coefficient, sign_coefficient }; - ctx->create_ecc_add_gate(add_gate); + ctx->create_ecc_add_gate({ p1.x.witness_index, + p1.y.witness_index, + p2.x.witness_index, + p2.y.witness_index, + p3.x.witness_index, + p3.y.witness_index, + endomorphism_coefficient, + sign_coefficient }); return p3; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp index a106f811c550..0ee6c3ef8ad9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/bool/bool.cpp @@ -150,11 +150,16 @@ bool_t bool_t::operator&(const bool_t& other) fr q2(i_a * (1 - 2 * i_b)); fr q3(-1); fr qc(i_a * i_b); - - const poly_triple gate_coefficients{ - witness_index, other.witness_index, result.witness_index, qm, q1, q2, q3, qc, - }; - context->create_poly_gate(gate_coefficients); + context->create_poly_gate({ + witness_index, + other.witness_index, + result.witness_index, + qm, + q1, + q2, + q3, + qc, + }); } else if (witness_index != IS_CONSTANT && other.witness_index == IS_CONSTANT) { if (other.witness_bool ^ other.witness_inverted) { result = bool_t(*this); @@ -229,11 +234,14 @@ bool_t bool_t::operator|(const bool_t& other) right_coefficient = barretenberg::fr::one(); constant_coefficient = barretenberg::fr::zero(); } - const poly_triple gate_coefficients{ - witness_index, other.witness_index, result.witness_index, multiplicative_coefficient, - left_coefficient, right_coefficient, barretenberg::fr::neg_one(), constant_coefficient - }; - context->create_poly_gate(gate_coefficients); + context->create_poly_gate({ witness_index, + other.witness_index, + result.witness_index, + multiplicative_coefficient, + left_coefficient, + right_coefficient, + barretenberg::fr::neg_one(), + constant_coefficient }); } else if (witness_index != IS_CONSTANT && other.witness_index == IS_CONSTANT) { if (other.witness_bool ^ other.witness_inverted) { result.witness_index = IS_CONSTANT; @@ -289,11 +297,14 @@ bool_t bool_t::operator^(const bool_t& other) right_coefficient = barretenberg::fr::neg_one(); constant_coefficient = barretenberg::fr::one(); } - const poly_triple gate_coefficients{ - witness_index, other.witness_index, result.witness_index, multiplicative_coefficient, - left_coefficient, right_coefficient, barretenberg::fr::neg_one(), constant_coefficient - }; - context->create_poly_gate(gate_coefficients); + context->create_poly_gate({ witness_index, + other.witness_index, + result.witness_index, + multiplicative_coefficient, + left_coefficient, + right_coefficient, + barretenberg::fr::neg_one(), + constant_coefficient }); } else if (witness_index != IS_CONSTANT && other.witness_index == IS_CONSTANT) { // witness ^ 1 = !witness if (other.witness_bool ^ other.witness_inverted) { @@ -364,11 +375,14 @@ bool_t bool_t::operator==(const bool_t& other) right_coefficient = barretenberg::fr::one(); constant_coefficient = barretenberg::fr::zero(); } - const poly_triple gate_coefficients{ - witness_index, other.witness_index, result.witness_index, multiplicative_coefficient, - left_coefficient, right_coefficient, barretenberg::fr::neg_one(), constant_coefficient - }; - context->create_poly_gate(gate_coefficients); + context->create_poly_gate({ witness_index, + other.witness_index, + result.witness_index, + multiplicative_coefficient, + left_coefficient, + right_coefficient, + barretenberg::fr::neg_one(), + constant_coefficient }); return result; } } @@ -538,10 +552,7 @@ template bool_t bool_tcreate_poly_gate(gate_coefficients); + context->create_poly_gate({ witness_index, witness_index, new_witness, q_m, q_l, q_r, q_o, q_c }); witness_index = new_witness; witness_bool = new_value; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp index 7db8a2ab0b09..52a83dda9a4a 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/circuit_builders/circuit_builders_fwd.hpp @@ -18,11 +18,13 @@ class Ultra; namespace barretenberg { class Bn254FrParams; +class Bn254FqParams; template struct alignas(32) field; } // namespace barretenberg namespace proof_system { template class StandardCircuitBuilder_; using StandardCircuitBuilder = StandardCircuitBuilder_>; +using StandardGrumpkinCircuitBuilder = StandardCircuitBuilder_>; template class TurboCircuitBuilder_; using TurboCircuitBuilder = TurboCircuitBuilder_>; template class UltraCircuitBuilder_; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp index 46a6487e6a38..a9b756f61806 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.cpp @@ -121,14 +121,13 @@ field_t field_t::operator+(const field_t& othe out += other.additive_constant; result.witness_index = ctx->add_variable(out); - const add_triple gate_coefficients{ witness_index, - other.witness_index, - result.witness_index, - multiplicative_constant, - other.multiplicative_constant, - barretenberg::fr::neg_one(), - (additive_constant + other.additive_constant) }; - ctx->create_add_gate(gate_coefficients); + ctx->create_add_gate({ witness_index, + other.witness_index, + result.witness_index, + multiplicative_constant, + other.multiplicative_constant, + barretenberg::fr::neg_one(), + (additive_constant + other.additive_constant) }); } return result; } @@ -233,15 +232,14 @@ field_t field_t::operator*(const field_t& othe out += T0; out += q_c; result.witness_index = ctx->add_variable(out); - const poly_triple gate_coefficients{ .a = witness_index, - .b = other.witness_index, - .c = result.witness_index, - .q_m = q_m, - .q_l = q_l, - .q_r = q_r, - .q_o = barretenberg::fr::neg_one(), - .q_c = q_c }; - ctx->create_poly_gate(gate_coefficients); + ctx->create_poly_gate({ .a = witness_index, + .b = other.witness_index, + .c = result.witness_index, + .q_m = q_m, + .q_l = q_l, + .q_r = q_r, + .q_o = barretenberg::fr::neg_one(), + .q_c = q_c }); } return result; } @@ -289,15 +287,14 @@ field_t field_t::divide_no_zero_check(const fi barretenberg::fr q_c = -get_value(); barretenberg::fr out_value = get_value() / other.get_value(); result.witness_index = ctx->add_variable(out_value); - const poly_triple gate_coefficients{ .a = result.witness_index, - .b = other.witness_index, - .c = result.witness_index, - .q_m = q_m, - .q_l = q_l, - .q_r = 0, - .q_o = 0, - .q_c = q_c }; - ctx->create_poly_gate(gate_coefficients); + ctx->create_poly_gate({ .a = result.witness_index, + .b = other.witness_index, + .c = result.witness_index, + .q_m = q_m, + .q_l = q_l, + .q_r = 0, + .q_o = 0, + .q_c = q_c }); } } else { // TODO SHOULD WE CARE ABOUT IF THE DIVISOR IS ZERO? @@ -335,15 +332,14 @@ field_t field_t::divide_no_zero_check(const fi barretenberg::fr q_o = -multiplicative_constant; barretenberg::fr q_c = -additive_constant; - const poly_triple gate_coefficients{ .a = result.witness_index, - .b = other.witness_index, - .c = witness_index, - .q_m = q_m, - .q_l = q_l, - .q_r = q_r, - .q_o = q_o, - .q_c = q_c }; - ctx->create_poly_gate(gate_coefficients); + ctx->create_poly_gate({ .a = result.witness_index, + .b = other.witness_index, + .c = witness_index, + .q_m = q_m, + .q_l = q_l, + .q_r = q_r, + .q_o = q_o, + .q_c = q_c }); } return result; } @@ -439,8 +435,7 @@ field_t field_t::madd(const field_t& to_mul, c field_t result(ctx); result.witness_index = ctx->add_variable(out); - - const mul_quad gate_coefficients{ + ctx->create_big_mul_gate({ .a = witness_index == IS_CONSTANT ? ctx->zero_idx : witness_index, .b = to_mul.witness_index == IS_CONSTANT ? ctx->zero_idx : to_mul.witness_index, .c = to_add.witness_index == IS_CONSTANT ? ctx->zero_idx : to_add.witness_index, @@ -451,8 +446,7 @@ field_t field_t::madd(const field_t& to_mul, c .c_scaling = q_3, .d_scaling = -barretenberg::fr(1), .const_scaling = q_c, - }; - ctx->create_big_mul_gate(gate_coefficients); + }); return result; } @@ -481,7 +475,7 @@ field_t field_t::add_two(const field_t& add_a, field_t result(ctx); result.witness_index = ctx->add_variable(out); - const mul_quad gate_coefficients{ + ctx->create_big_mul_gate({ .a = witness_index == IS_CONSTANT ? ctx->zero_idx : witness_index, .b = add_a.witness_index == IS_CONSTANT ? ctx->zero_idx : add_a.witness_index, .c = add_b.witness_index == IS_CONSTANT ? ctx->zero_idx : add_b.witness_index, @@ -492,8 +486,7 @@ field_t field_t::add_two(const field_t& add_a, .c_scaling = q_3, .d_scaling = -barretenberg::fr(1), .const_scaling = q_c, - }; - ctx->create_big_mul_gate(gate_coefficients); + }); return result; } @@ -523,15 +516,13 @@ template field_t field_t this.v * this.v * [ 0 ] + this.v * [this.mul] + this.v * [ 0 ] + result.v * [ -1] + [this.add] == 0 // <=> this.v * this.v * [q_m] + this.v * [ q_l ] + this.v * [q_r] + result.v * [q_o] + [ q_c ] == 0 - const add_triple gate_coefficients{ .a = witness_index, - .b = witness_index, - .c = result.witness_index, - .a_scaling = multiplicative_constant, - .b_scaling = 0, - .c_scaling = barretenberg::fr::neg_one(), - .const_scaling = additive_constant }; - - context->create_add_gate(gate_coefficients); + context->create_add_gate({ .a = witness_index, + .b = witness_index, + .c = result.witness_index, + .a_scaling = multiplicative_constant, + .b_scaling = 0, + .c_scaling = barretenberg::fr::neg_one(), + .const_scaling = additive_constant }); return result; } @@ -552,7 +543,8 @@ template void field_t::assert_is_zer // this.v * 0 * [q_m] + this.v * [ q_l ] + 0 * [q_r] + 0 * [q_o] + [ q_c ] == 0 ComposerContext* ctx = context; - const poly_triple gate_coefficients{ + + context->create_poly_gate({ .a = witness_index, .b = ctx->zero_idx, .c = ctx->zero_idx, @@ -561,8 +553,7 @@ template void field_t::assert_is_zer .q_r = barretenberg::fr(0), .q_o = barretenberg::fr(0), .q_c = additive_constant, - }; - context->create_poly_gate(gate_coefficients); + }); } template void field_t::assert_is_not_zero(std::string const& msg) const @@ -593,7 +584,7 @@ template void field_t::assert_is_not // <=> this.v * inverse.v * [ q_m ] + this.v * [q_l] + inverse.v * [ q_r ] + 0 * [q_o] + [q_c] == 0 // (a * mul_const + add_const) * b - 1 = 0 - const poly_triple gate_coefficients{ + context->create_poly_gate({ .a = witness_index, // input value .b = inverse.witness_index, // inverse .c = ctx->zero_idx, // no output @@ -602,8 +593,7 @@ template void field_t::assert_is_not .q_r = additive_constant, // b * mul_const .q_o = barretenberg::fr(0), // c * 0 .q_c = barretenberg::fr(-1), // -1 - }; - context->create_poly_gate(gate_coefficients); + }); } template bool_t field_t::is_zero() const @@ -641,28 +631,28 @@ template bool_t field_tcreate_poly_gate(gate_coefficients_a); + + context->create_poly_gate({ .a = k.witness_index, + .b = k_inverse.witness_index, + .c = is_zero.witness_index, + .q_m = q_m, + .q_l = q_l, + .q_r = q_r, + .q_o = q_o, + .q_c = q_c }); // is_zero * k_inverse - is_zero = 0 q_o = barretenberg::fr::neg_one(); q_c = barretenberg::fr::zero(); - const poly_triple gate_coefficients_b{ .a = is_zero.witness_index, - .b = k_inverse.witness_index, - .c = is_zero.witness_index, - .q_m = q_m, - .q_l = q_l, - .q_r = q_r, - .q_o = q_o, - .q_c = q_c }; - context->create_poly_gate(gate_coefficients_b); + + context->create_poly_gate({ .a = is_zero.witness_index, + .b = k_inverse.witness_index, + .c = is_zero.witness_index, + .q_m = q_m, + .q_l = q_l, + .q_r = q_r, + .q_o = q_o, + .q_c = q_c }); return is_zero; } @@ -897,7 +887,7 @@ void field_t::evaluate_linear_identity(const field_t& a, barretenberg::fr q_4 = d.multiplicative_constant; barretenberg::fr q_c = a.additive_constant + b.additive_constant + c.additive_constant + d.additive_constant; - const add_quad gate_coefficients{ + ctx->create_big_add_gate({ a.witness_index == IS_CONSTANT ? ctx->zero_idx : a.witness_index, b.witness_index == IS_CONSTANT ? ctx->zero_idx : b.witness_index, c.witness_index == IS_CONSTANT ? ctx->zero_idx : c.witness_index, @@ -907,8 +897,7 @@ void field_t::evaluate_linear_identity(const field_t& a, q_3, q_4, q_c, - }; - ctx->create_big_add_gate(gate_coefficients); + }); } template @@ -934,7 +923,7 @@ void field_t::evaluate_polynomial_identity(const field_t& a, barretenberg::fr q_4 = d.multiplicative_constant; barretenberg::fr q_c = a.additive_constant * b.additive_constant + c.additive_constant + d.additive_constant; - const mul_quad gate_coefficients{ + ctx->create_big_mul_gate({ a.witness_index == IS_CONSTANT ? ctx->zero_idx : a.witness_index, b.witness_index == IS_CONSTANT ? ctx->zero_idx : b.witness_index, c.witness_index == IS_CONSTANT ? ctx->zero_idx : c.witness_index, @@ -945,8 +934,7 @@ void field_t::evaluate_polynomial_identity(const field_t& a, q_3, q_4, q_c, - }; - ctx->create_big_mul_gate(gate_coefficients); + }); } /** diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp index 43181df58827..5677bee9e774 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/field/field.test.cpp @@ -138,27 +138,25 @@ template class stdlib_field : public testing::Test { */ if (true_when_y_val_zero) { // constraint: 0*x + 1*y + 0*0 + 0 == 0 - add_triple t{ .a = x.witness_index, - .b = y.witness_index, - .c = composer.zero_idx, - .a_scaling = 0, - .b_scaling = 1, - .c_scaling = 0, - .const_scaling = 0 }; - - composer.create_add_gate(t); + + composer.create_add_gate({ .a = x.witness_index, + .b = y.witness_index, + .c = composer.zero_idx, + .a_scaling = 0, + .b_scaling = 1, + .c_scaling = 0, + .const_scaling = 0 }); expected_result = false; } else { // constraint: 0*x + 1*y + 0*0 - 1 == 0 - add_triple t{ .a = x.witness_index, - .b = y.witness_index, - .c = composer.zero_idx, - .a_scaling = 0, - .b_scaling = 1, - .c_scaling = 0, - .const_scaling = -1 }; - - composer.create_add_gate(t); + + composer.create_add_gate({ .a = x.witness_index, + .b = y.witness_index, + .c = composer.zero_idx, + .a_scaling = 0, + .b_scaling = 1, + .c_scaling = 0, + .const_scaling = -1 }); expected_result = true; } } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/group.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/group.hpp index 8023dad30dfe..90b666f95727 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/group.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/group/group.hpp @@ -153,16 +153,16 @@ auto group::fixed_base_scalar_mul_internal(const field_t init_quad{ origin_points[0].x, + (origin_points[0].x - origin_points[1].x), + origin_points[0].y, + (origin_points[0].y - origin_points[1].y) }; fr x_alpha = accumulator_offset; std::vector accumulator_witnesses; pedersen_gates pedersen_gates(ctx); for (size_t i = 0; i < num_quads; ++i) { - fixed_group_add_quad round_quad; + fixed_group_add_quad_ round_quad; round_quad.d = ctx->add_variable(accumulator_transcript[i]); round_quad.a = ctx->add_variable(multiplication_transcript[i].x); round_quad.b = ctx->add_variable(multiplication_transcript[i].y); @@ -193,15 +193,15 @@ auto group::fixed_base_scalar_mul_internal(const field_tadd_variable(multiplication_transcript[num_quads].x), - ctx->add_variable(multiplication_transcript[num_quads].y), - ctx->add_variable(x_alpha), - ctx->add_variable(accumulator_transcript[num_quads]), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero(), - fr::zero() }; + add_quad_ add_quad{ ctx->add_variable(multiplication_transcript[num_quads].x), + ctx->add_variable(multiplication_transcript[num_quads].y), + ctx->add_variable(x_alpha), + ctx->add_variable(accumulator_transcript[num_quads]), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero(), + fr::zero() }; ctx->create_big_add_gate(add_quad); accumulator_witnesses.push_back(add_quad.d); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/arithmetic.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/arithmetic.cpp index 26b10b520ea4..b1e5d26e6d90 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/arithmetic.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/arithmetic.cpp @@ -80,7 +80,7 @@ uint uint::operator+(const uint& other) cons * is {0, 1, 2}. **/ - const add_quad gate{ + const add_quad_ gate{ .a = witness_index, .b = other.witness_index, .c = ctx->add_variable(remainder), @@ -161,7 +161,7 @@ uint uint::operator-(const uint& other) cons // witness - other_witness - remainder - 2**width . overflow + (2**width + constant_term) == 0 // and // overflow in {0, 1, 2} - const add_quad gate{ + const add_quad_ gate{ .a = lhs_idx, .b = rhs_idx, .c = ctx->add_variable(remainder), @@ -232,7 +232,7 @@ uint uint::operator*(const uint& other) cons * ab + a const_b + b const_a - r - (2**width) overflow + (const_a const_b % 2**width) == 0 */ - const mul_quad gate{ + const mul_quad_ gate{ .a = witness_index, // a .b = rhs_idx, // b .c = ctx->add_variable(remainder), @@ -344,30 +344,29 @@ std::pair, uint> uint // constraint: qb + const_b q + 0 b - a + r - const_a == 0 // i.e., a + const_a = q(b + const_b) + r - const mul_quad division_gate{ .a = quotient_idx, // q - .b = divisor_idx, // b - .c = dividend_idx, // a - .d = remainder_idx, // r - .mul_scaling = fr::one(), - .a_scaling = other.additive_constant, - .b_scaling = fr::zero(), - .c_scaling = fr::neg_one(), - .d_scaling = fr::one(), - .const_scaling = -fr(additive_constant) }; - ctx->create_big_mul_gate(division_gate); + ctx->create_big_mul_gate({ .a = quotient_idx, // q + .b = divisor_idx, // b + .c = dividend_idx, // a + .d = remainder_idx, // r + .mul_scaling = fr::one(), + .a_scaling = other.additive_constant, + .b_scaling = fr::zero(), + .c_scaling = fr::neg_one(), + .d_scaling = fr::one(), + .const_scaling = -fr(additive_constant) }); // set delta = (b + const_b - r - 1) const uint256_t delta = divisor - r - 1; const uint32_t delta_idx = ctx->add_variable(delta); // constraint: b - r - delta + const_b - 1 == 0 - const add_triple delta_gate{ .a = divisor_idx, - .b = remainder_idx, - .c = delta_idx, - .a_scaling = fr::one(), - .b_scaling = fr::neg_one(), - .c_scaling = fr::neg_one(), - .const_scaling = other.additive_constant + fr::neg_one() }; + const add_triple_ delta_gate{ .a = divisor_idx, + .b = remainder_idx, + .c = delta_idx, + .a_scaling = fr::one(), + .b_scaling = fr::neg_one(), + .c_scaling = fr::neg_one(), + .const_scaling = other.additive_constant + fr::neg_one() }; ctx->create_add_gate(delta_gate); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/logic.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/logic.cpp index e9f3d8e6be0f..e22efc1e226a 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/logic.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/logic.cpp @@ -134,7 +134,7 @@ uint uint::operator>>(const size_t shift) co const uint32_t left_index = shift == width - 1 ? context->zero_idx : accumulators[static_cast(idx - 1)]; // Constraint: -6.(self >> shift) + 12.a[idx-1] + 6.high bit of (a[idx] - 4.a[idx-1]) = 0. - const add_quad gate{ + const add_quad_ gate{ .a = context->zero_idx, .b = context->add_variable(output), .c = right_index, @@ -199,13 +199,13 @@ uint uint::operator<<(const size_t shift) co const uint256_t output = (get_value() << shift) & MASK; // (value * 2**shift) % 2**32 // constraint: A.2**shift - A_{x-1}.2**width + (value * 2**shift) % 2**32 == 0 - const add_triple gate{ .a = base_idx, - .b = right_idx, - .c = context->add_variable(output), - .a_scaling = base_shift_factor, - .b_scaling = -fr(right_shift_factor), - .c_scaling = fr::neg_one(), - .const_scaling = fr::zero() }; + const add_triple_ gate{ .a = base_idx, + .b = right_idx, + .c = context->add_variable(output), + .a_scaling = base_shift_factor, + .b_scaling = -fr(right_shift_factor), + .c_scaling = fr::neg_one(), + .const_scaling = fr::zero() }; context->create_add_gate(gate); @@ -272,15 +272,15 @@ uint uint::operator<<(const size_t shift) co * + 6 * high bit of A_x - 4 A_{x-1} , * where A = witness and shift = s = 2x + 1 and A_{-1} = 0. */ - const add_quad gate{ .a = context->add_variable(output), - .b = base_index, - .c = left_index, - .d = right_index, - .a_scaling = -q_1, - .b_scaling = q_2, - .c_scaling = fr::zero(), - .d_scaling = -q_3, - .const_scaling = fr::zero() }; + const add_quad_ gate{ .a = context->add_variable(output), + .b = base_index, + .c = left_index, + .d = right_index, + .a_scaling = -q_1, + .b_scaling = q_2, + .c_scaling = fr::zero(), + .d_scaling = -q_3, + .const_scaling = fr::zero() }; context->create_big_add_gate_with_bit_extraction(gate); @@ -344,13 +344,13 @@ uint uint::ror(const size_t target_rotation) const fr base_shift_factor = t1; // constraint: 4^{w-x} A + (1 - 4^w) A_pivot - out == 0 - const add_triple gate{ .a = base_idx, - .b = left_idx, - .c = context->add_variable(output), - .a_scaling = base_shift_factor, - .b_scaling = left_shift_factor, - .c_scaling = fr::neg_one(), - .const_scaling = fr::zero() }; + const add_triple_ gate{ .a = base_idx, + .b = left_idx, + .c = context->add_variable(output), + .a_scaling = base_shift_factor, + .b_scaling = left_shift_factor, + .c_scaling = fr::neg_one(), + .const_scaling = fr::zero() }; context->create_add_gate(gate); @@ -413,15 +413,15 @@ uint uint::ror(const size_t target_rotation) * -out + (2 * 4^{w-x}) A + (2 - 2 * 4^{w}) A_{w-x-1} + (1 - 4^w) a_{x-1, 1} == 0. * */ - const add_quad gate{ .a = context->add_variable(output), - .b = base_idx, - .c = next_pivot_idx, - .d = pivot_idx, - .a_scaling = q_1, - .b_scaling = q_2, - .c_scaling = fr::zero(), - .d_scaling = q_3, - .const_scaling = fr::zero() }; + const add_quad_ gate{ .a = context->add_variable(output), + .b = base_idx, + .c = next_pivot_idx, + .d = pivot_idx, + .a_scaling = q_1, + .b_scaling = q_2, + .c_scaling = fr::zero(), + .d_scaling = q_3, + .const_scaling = fr::zero() }; context->create_big_add_gate_with_bit_extraction(gate); @@ -483,7 +483,7 @@ uint uint::logic_operator(const uint& other, const uint32_t lhs_idx = is_constant() ? ctx->add_variable(lhs) : witness_index; const uint32_t rhs_idx = other.is_constant() ? ctx->add_variable(rhs) : other.witness_index; - accumulator_triple logic_accumulators; + accumulator_triple_ logic_accumulators; switch (op_type) { case AND: { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp index befd21c39277..59c147a58a5c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/plookup/arithmetic.cpp @@ -26,7 +26,7 @@ uint_plookup uint_plookup::operator+(const u const uint256_t overflow = sum >> width; const uint256_t remainder = sum & MASK; - const add_quad gate{ + const add_quad_ gate{ is_constant() ? ctx->zero_idx : witness_index, other.is_constant() ? ctx->zero_idx : other.witness_index, ctx->add_variable(remainder), @@ -71,7 +71,7 @@ uint_plookup uint_plookup::operator-(const u const uint256_t overflow = difference >> width; const uint256_t remainder = difference & MASK; - const add_quad gate{ + const add_quad_ gate{ lhs_idx, rhs_idx, ctx->add_variable(remainder), @@ -113,7 +113,7 @@ uint_plookup uint_plookup::operator*(const u const uint256_t overflow = product >> width; const uint256_t remainder = product & MASK; - const mul_quad gate{ + const mul_quad_ gate{ witness_index, rhs_idx, ctx->add_variable(remainder), @@ -210,7 +210,7 @@ std::pair, uint_plookup> uint_p const uint32_t quotient_idx = ctx->add_variable(q); const uint32_t remainder_idx = ctx->add_variable(r); - const mul_quad division_gate{ + const mul_quad_ division_gate{ quotient_idx, // q divisor_idx, // b dividend_idx, // a @@ -228,7 +228,7 @@ std::pair, uint_plookup> uint_p const uint256_t delta = divisor - r; const uint32_t delta_idx = ctx->add_variable(delta); - const add_triple delta_gate{ + const add_triple_ delta_gate{ divisor_idx, // b remainder_idx, // r delta_idx, // d diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp index e6cc06dfbbb1..2e14c6c0bf9b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.cpp @@ -215,15 +215,15 @@ template uint uint> width; const uint256_t remainder = value & MASK; - const add_quad gate{ .a = witness_index, - .b = context->zero_idx, - .c = context->add_variable(remainder), - .d = context->add_variable(overflow), - .a_scaling = fr::one(), - .b_scaling = fr::zero(), - .c_scaling = fr::neg_one(), - .d_scaling = -fr(CIRCUIT_UINT_MAX_PLUS_ONE), - .const_scaling = (additive_constant & MASK) }; + const add_quad_ gate{ .a = witness_index, + .b = context->zero_idx, + .c = context->add_variable(remainder), + .d = context->add_variable(overflow), + .a_scaling = fr::one(), + .b_scaling = fr::zero(), + .c_scaling = fr::neg_one(), + .d_scaling = -fr(CIRCUIT_UINT_MAX_PLUS_ONE), + .const_scaling = (additive_constant & MASK) }; context->create_balanced_add_gate(gate); @@ -323,7 +323,7 @@ template bool_t uint gate{ context->add_variable(lo_bit), context->add_variable(hi_bit), right_idx, left_idx, 1, 2, -1, 4, 0, }; context->create_new_range_constraint(gate.a, 1); @@ -352,15 +352,15 @@ template bool_t uintadd_variable(lo_bit), - .b = context->zero_idx, - .c = right_idx, - .d = left_idx, - .a_scaling = fr(3), - .b_scaling = fr::zero(), - .c_scaling = -fr(3), - .d_scaling = fr(12), - .const_scaling = fr::zero() }; + add_quad_ gate{ .a = context->add_variable(lo_bit), + .b = context->zero_idx, + .c = right_idx, + .d = left_idx, + .a_scaling = fr(3), + .b_scaling = fr::zero(), + .c_scaling = -fr(3), + .d_scaling = fr(12), + .const_scaling = fr::zero() }; /** constraint: * 3 lo_bit + 0 * 0 - 3 a_pivot + 12 a_{pivot - 1} + 0 + 6 high bit of (A_pivot - 4 A_{pivot - 1}) == 0 * i.e., @@ -376,15 +376,15 @@ template bool_t uint> 1; - add_quad gate{ .a = context->zero_idx, - .b = context->add_variable(hi_bit), - .c = right_idx, - .d = left_idx, - .a_scaling = fr::zero(), - .b_scaling = -fr(6), - .c_scaling = fr::zero(), - .d_scaling = fr::zero(), - .const_scaling = fr::zero() }; + add_quad_ gate{ .a = context->zero_idx, + .b = context->add_variable(hi_bit), + .c = right_idx, + .d = left_idx, + .a_scaling = fr::zero(), + .b_scaling = -fr(6), + .c_scaling = fr::zero(), + .d_scaling = fr::zero(), + .const_scaling = fr::zero() }; /** * constraint: diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp index 5b0e1a6287a7..24778dc6efca 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/primitives/uint/uint.test.cpp @@ -1129,17 +1129,16 @@ template class stdlib_uint : public testing::Test { // constraint: qb + const_b q + 0 b - a + r - const_a == 0 // i.e., a + const_a = q(b + const_b) + r - const mul_quad division_gate{ .a = quotient_idx, // q - .b = divisor_idx, // b - .c = dividend_idx, // a - .d = remainder_idx, // r - .mul_scaling = fr::one(), - .a_scaling = b.get_additive_constant(), - .b_scaling = fr::zero(), - .c_scaling = fr::neg_one(), - .d_scaling = fr::one(), - .const_scaling = -a.get_additive_constant() }; - composer.create_big_mul_gate(division_gate); + composer.create_big_mul_gate({ .a = quotient_idx, // q + .b = divisor_idx, // b + .c = dividend_idx, // a + .d = remainder_idx, // r + .mul_scaling = fr::one(), + .a_scaling = b.get_additive_constant(), + .b_scaling = fr::zero(), + .c_scaling = fr::neg_one(), + .d_scaling = fr::one(), + .const_scaling = -a.get_additive_constant() }); // set delta = (b + const_b - r) @@ -1147,7 +1146,7 @@ template class stdlib_uint : public testing::Test { const uint256_t delta = divisor - r - 1; const uint32_t delta_idx = composer.add_variable(delta); - const add_triple delta_gate{ + composer.create_add_gate({ .a = divisor_idx, // b .b = remainder_idx, // r .c = delta_idx, // d @@ -1155,9 +1154,7 @@ template class stdlib_uint : public testing::Test { .b_scaling = fr::neg_one(), .c_scaling = fr::neg_one(), .const_scaling = b.get_additive_constant(), - }; - - composer.create_add_gate(delta_gate); + }); // validate delta is in the correct range stdlib::field_t::from_witness_index(&composer, delta_idx) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.hpp index b5ac65cb7fd5..aa990dce7dde 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.hpp @@ -444,7 +444,7 @@ template struct verification_key { // Native data: - std::shared_ptr reference_string; + std::shared_ptr> reference_string; PolynomialManifest polynomial_manifest; // Used to check in the circuit if a proof contains any aggregated state. diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.test.cpp index 26765efd3725..440049eac32f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/stdlib/recursion/verification_key/verification_key.test.cpp @@ -57,7 +57,7 @@ TYPED_TEST(VerificationKeyFixture, vk_data_vs_recursion_compress_native) verification_key_data vk_data = TestFixture::rand_vk_data(); verification_key_data vk_data_copy = vk_data; - auto file_crs = std::make_unique("../srs_db/ignition"); + auto file_crs = std::make_unique>("../srs_db/ignition"); auto file_verifier = file_crs->get_verifier_crs(); auto native_vk = std::make_shared(std::move(vk_data_copy), file_verifier); @@ -77,7 +77,7 @@ TYPED_TEST(VerificationKeyFixture, compress_vs_compress_native) verification_key_data vk_data = TestFixture::rand_vk_data(); - auto file_crs = std::make_unique("../srs_db/ignition"); + auto file_crs = std::make_unique>("../srs_db/ignition"); auto file_verifier = file_crs->get_verifier_crs(); auto native_vk = std::make_shared(std::move(vk_data), file_verifier);