diff --git a/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.cpp b/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.cpp index 1ef6fdcba0..4f675eb7c7 100644 --- a/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.cpp +++ b/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.cpp @@ -4,6 +4,7 @@ #include "barretenberg/plonk/proof_system/types/prover_settings.hpp" // #include "barretenberg/plonk/proof_system/verifier/verifier.hpp" #include "barretenberg/proof_system/circuit_constructors/ultra_circuit_constructor.hpp" +#include "barretenberg/proof_system/composer/composer_helper_lib.hpp" #include "barretenberg/proof_system/composer/permutation_helper.hpp" #include "barretenberg/plonk/proof_system/commitment_scheme/kate_commitment_scheme.hpp" @@ -20,8 +21,7 @@ namespace proof_system::honk { * TODO(luke): The wire polynomials are returned directly whereas the sorted list polys are added to the proving * key. This should be made consistent once Cody's Flavor work is settled. */ -template -void UltraHonkComposerHelper::compute_witness(CircuitConstructor& circuit_constructor) +void UltraHonkComposerHelper::compute_witness(CircuitConstructor& circuit_constructor) { if (computed_witness) { return; @@ -40,8 +40,8 @@ void UltraHonkComposerHelper::compute_witness(CircuitConstru const size_t subgroup_size = circuit_constructor.get_circuit_subgroup_size(total_num_gates + NUM_RESERVED_GATES); // Pad the wires (pointers to `witness_indices` of the `variables` vector). - // Note: the remaining NUM_RESERVED_GATES indices are padded with zeros within `compute_witness_base` (called - // next). + // Note: the remaining NUM_RESERVED_GATES indices are padded with zeros within `construct_wire_polynomials_base` + // (called next). for (size_t i = filled_gates; i < total_num_gates; ++i) { circuit_constructor.w_l.emplace_back(circuit_constructor.zero_idx); circuit_constructor.w_r.emplace_back(circuit_constructor.zero_idx); @@ -49,12 +49,13 @@ void UltraHonkComposerHelper::compute_witness(CircuitConstru circuit_constructor.w_4.emplace_back(circuit_constructor.zero_idx); } - // TODO(#340)(luke): within compute_witness_base, the 3rd argument is used in the calculation of the dyadic circuit - // size (subgroup_size). Here (and in other split composers) we're passing in NUM_RANDOMIZED_GATES, but elsewhere, - // e.g. directly above, we use NUM_RESERVED_GATES in a similar role. Therefore, these two constants must be equal - // for everything to be consistent. What we should do is compute the dyadic circuit size once and for all then pass - // that around rather than computing in multiple places. - wire_polynomials = compute_witness_base(circuit_constructor, total_num_gates, NUM_RANDOMIZED_GATES); + // TODO(#340)(luke): within construct_wire_polynomials_base, the 3rd argument is used in the calculation of the + // dyadic circuit size (subgroup_size). Here (and in other split composers) we're passing in NUM_RANDOMIZED_GATES, + // but elsewhere, e.g. directly above, we use NUM_RESERVED_GATES in a similar role. Therefore, these two constants + // must be equal for everything to be consistent. What we should do is compute the dyadic circuit size once and for + // all then pass that around rather than computing in multiple places. + wire_polynomials = + construct_wire_polynomials_base(circuit_constructor, total_num_gates, NUM_RANDOMIZED_GATES); polynomial s_1(subgroup_size); polynomial s_2(subgroup_size); @@ -130,18 +131,18 @@ void UltraHonkComposerHelper::compute_witness(CircuitConstru ++count; } - // TODO(luke): Adding these to the key for now but this is inconsistent since these are 'witness' polys. Need - // to see what becomes of the proving key before making a decision here. - circuit_proving_key->polynomial_store.put("s_1_lagrange", std::move(s_1)); - circuit_proving_key->polynomial_store.put("s_2_lagrange", std::move(s_2)); - circuit_proving_key->polynomial_store.put("s_3_lagrange", std::move(s_3)); - circuit_proving_key->polynomial_store.put("s_4_lagrange", std::move(s_4)); + // WORK TODO + // // TODO(luke): Adding these to the key for now but this is inconsistent since these are 'witness' polys. Need + // // to see what becomes of the proving key before making a decision here. + // circuit_proving_key->polynomial_store.put("s_1_lagrange", std::move(s_1)); + // circuit_proving_key->polynomial_store.put("s_2_lagrange", std::move(s_2)); + // circuit_proving_key->polynomial_store.put("s_3_lagrange", std::move(s_3)); + // circuit_proving_key->polynomial_store.put("s_4_lagrange", std::move(s_4)); computed_witness = true; } -template -UltraProver UltraHonkComposerHelper::create_prover(CircuitConstructor& circuit_constructor) +UltraProver UltraHonkComposerHelper::create_prover(CircuitConstructor& circuit_constructor) { finalize_circuit(circuit_constructor); @@ -160,8 +161,7 @@ UltraProver UltraHonkComposerHelper::create_prover(CircuitCo // * @return The verifier. // * */ // // TODO(Cody): This should go away altogether. -// template -// plonk::UltraVerifier UltraHonkComposerHelper::create_verifier( +// plonk::UltraVerifier UltraHonkComposerHelper::create_verifier( // const CircuitConstructor& circuit_constructor) // { // auto verification_key = compute_verification_key(circuit_constructor); @@ -177,8 +177,7 @@ UltraProver UltraHonkComposerHelper::create_prover(CircuitCo // return output_state; // } -template -std::shared_ptr UltraHonkComposerHelper::compute_proving_key( +std::shared_ptr UltraHonkComposerHelper::compute_proving_key( const CircuitConstructor& circuit_constructor) { if (circuit_proving_key) { @@ -196,17 +195,16 @@ std::shared_ptr UltraHonkComposerHelper: const size_t num_randomized_gates = NUM_RANDOMIZED_GATES; // Initialize circuit_proving_key // TODO(#229)(Kesha): replace composer types. - circuit_proving_key = initialize_proving_key( + circuit_proving_key = initialize_proving_key( circuit_constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::PLOOKUP); - construct_lagrange_selector_forms(circuit_constructor, circuit_proving_key.get()); + construct_selector_polynomials(circuit_constructor, circuit_proving_key.get()); // TODO(#217)(luke): Naively enforcing non-zero selectors for Honk will result in some relations not being // satisfied. // enforce_nonzero_polynomial_selectors(circuit_constructor, circuit_proving_key.get()); - compute_honk_generalized_sigma_permutations(circuit_constructor, - circuit_proving_key.get()); + compute_honk_generalized_sigma_permutations(circuit_constructor, circuit_proving_key.get()); compute_first_and_last_lagrange_polynomials(circuit_proving_key.get()); @@ -280,26 +278,30 @@ std::shared_ptr UltraHonkComposerHelper: poly_q_table_column_3[subgroup_size - 1] = ++unique_last_value; poly_q_table_column_4[subgroup_size - 1] = ++unique_last_value; - circuit_proving_key->polynomial_store.put("table_value_1_lagrange", std::move(poly_q_table_column_1)); - circuit_proving_key->polynomial_store.put("table_value_2_lagrange", std::move(poly_q_table_column_2)); - circuit_proving_key->polynomial_store.put("table_value_3_lagrange", std::move(poly_q_table_column_3)); - circuit_proving_key->polynomial_store.put("table_value_4_lagrange", std::move(poly_q_table_column_4)); - - // Copy memory read/write record data into proving key. Prover needs to know which gates contain a read/write - // 'record' witness on the 4th wire. This wire value can only be fully computed once the first 3 wire polynomials - // have been committed to. The 4th wire on these gates will be a random linear combination of the first 3 wires, - // using the plookup challenge `eta` - std::copy(circuit_constructor.memory_read_records.begin(), - circuit_constructor.memory_read_records.end(), - std::back_inserter(circuit_proving_key->memory_read_records)); - std::copy(circuit_constructor.memory_write_records.begin(), - circuit_constructor.memory_write_records.end(), - std::back_inserter(circuit_proving_key->memory_write_records)); - - circuit_proving_key->recursive_proof_public_input_indices = - std::vector(recursive_proof_public_input_indices.begin(), recursive_proof_public_input_indices.end()); - - circuit_proving_key->contains_recursive_proof = contains_recursive_proof; + // WORKTODO + // circuit_proving_key->polynomial_store.put("table_value_1_lagrange", std::move(poly_q_table_column_1)); + // circuit_proving_key->polynomial_store.put("table_value_2_lagrange", std::move(poly_q_table_column_2)); + // circuit_proving_key->polynomial_store.put("table_value_3_lagrange", std::move(poly_q_table_column_3)); + // circuit_proving_key->polynomial_store.put("table_value_4_lagrange", std::move(poly_q_table_column_4)); + + // WORKTODO + // // Copy memory read/write record data into proving key. Prover needs to know which gates contain a read/write + // // 'record' witness on the 4th wire. This wire value can only be fully computed once the first 3 wire polynomials + // // have been committed to. The 4th wire on these gates will be a random linear combination of the first 3 wires, + // // using the plookup challenge `eta` + // std::copy(circuit_constructor.memory_read_records.begin(), + // circuit_constructor.memory_read_records.end(), + // std::back_inserter(circuit_proving_key->memory_read_records)); + // std::copy(circuit_constructor.memory_write_records.begin(), + // circuit_constructor.memory_write_records.end(), + // std::back_inserter(circuit_proving_key->memory_write_records)); + + // WORKTODO + // circuit_proving_key->recursive_proof_public_input_indices = + // std::vector(recursive_proof_public_input_indices.begin(), + // recursive_proof_public_input_indices.end()); + + // circuit_proving_key->contains_recursive_proof = contains_recursive_proof; return circuit_proving_key; } @@ -309,8 +311,7 @@ std::shared_ptr UltraHonkComposerHelper: // * // * @return Pointer to created circuit verification key. // * */ -// template -// std::shared_ptr UltraHonkComposerHelper::compute_verification_key( +// std::shared_ptr UltraHonkComposerHelper::compute_verification_key( // const CircuitConstructor& circuit_constructor) // { // if (circuit_verification_key) { @@ -335,8 +336,7 @@ std::shared_ptr UltraHonkComposerHelper: // return circuit_verification_key; // } -// template -// void UltraHonkComposerHelper::add_table_column_selector_poly_to_proving_key( +// void UltraHonkComposerHelper::add_table_column_selector_poly_to_proving_key( // polynomial& selector_poly_lagrange_form, const std::string& tag) // { // polynomial selector_poly_lagrange_form_copy(selector_poly_lagrange_form, circuit_proving_key->small_domain.size); @@ -352,5 +352,4 @@ std::shared_ptr UltraHonkComposerHelper: // circuit_proving_key->polynomial_store.put(tag + "_fft", std::move(selector_poly_coset_form)); // } -template class UltraHonkComposerHelper; } // namespace proof_system::honk diff --git a/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.hpp b/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.hpp index de309735e6..14bcc7f904 100644 --- a/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.hpp +++ b/cpp/src/barretenberg/honk/composer/composer_helper/ultra_honk_composer_helper.hpp @@ -15,17 +15,21 @@ namespace proof_system::honk { // TODO(Kesha): change initializations to specify this parameter // Cody: What does this mean? -template class UltraHonkComposerHelper { +class UltraHonkComposerHelper { public: + using Flavor = flavor::Ultra; + using CircuitConstructor = Flavor::CircuitConstructor; + using ProvingKey = Flavor::ProvingKey; // WORKTODO: undo this changed; not needed + // TODO(#340)(luke): In the split composers, NUM_RANDOMIZED_GATES has replaced NUM_RESERVED_GATES (in some places) // to determine the next-power-of-2 circuit size. (There are some places in this composer that still use // NUM_RESERVED_GATES). Therefore for consistency within this composer itself, and consistency with the original // Ultra Composer, this value must match that of NUM_RESERVED_GATES. This issue needs to be reconciled // simultaneously here and in the other split composers. static constexpr size_t NUM_RANDOMIZED_GATES = 4; // equal to the number of multilinear evaluations leaked - static constexpr size_t program_width = CircuitConstructor::program_width; + static constexpr size_t num_wires = CircuitConstructor::num_wires; std::vector wire_polynomials; - std::shared_ptr circuit_proving_key; + std::shared_ptr circuit_proving_key; std::shared_ptr circuit_verification_key; // TODO(#218)(kesha): we need to put this into the commitment key, so that the composer doesn't have to handle srs // at all @@ -45,7 +49,7 @@ template class UltraHonkComposerHelper { : crs_factory_(std::move(crs_factory)) {} - UltraHonkComposerHelper(std::shared_ptr p_key, std::shared_ptr v_key) + UltraHonkComposerHelper(std::shared_ptr p_key, std::shared_ptr v_key) : circuit_proving_key(std::move(p_key)) , circuit_verification_key(std::move(v_key)) {} @@ -58,7 +62,7 @@ template class UltraHonkComposerHelper { void finalize_circuit(CircuitConstructor& circuit_constructor) { circuit_constructor.finalize_circuit(); }; - std::shared_ptr compute_proving_key(const CircuitConstructor& circuit_constructor); + std::shared_ptr compute_proving_key(const CircuitConstructor& circuit_constructor); // std::shared_ptr compute_verification_key(const CircuitConstructor& circuit_constructor); void compute_witness(CircuitConstructor& circuit_constructor); diff --git a/cpp/src/barretenberg/honk/composer/ultra_honk_composer.hpp b/cpp/src/barretenberg/honk/composer/ultra_honk_composer.hpp index 5fa40b4626..6efd77899c 100644 --- a/cpp/src/barretenberg/honk/composer/ultra_honk_composer.hpp +++ b/cpp/src/barretenberg/honk/composer/ultra_honk_composer.hpp @@ -16,7 +16,7 @@ class UltraHonkComposer { // 1) Proving and verification keys // 2) CRS // 3) Converting variables to witness vectors/polynomials - UltraHonkComposerHelper composer_helper; + UltraHonkComposerHelper composer_helper; size_t& num_gates; UltraHonkComposer() diff --git a/cpp/src/barretenberg/honk/composer/ultra_honk_composer.test.cpp b/cpp/src/barretenberg/honk/composer/ultra_honk_composer.test.cpp index 0c8f1baefc..b7a33947d2 100644 --- a/cpp/src/barretenberg/honk/composer/ultra_honk_composer.test.cpp +++ b/cpp/src/barretenberg/honk/composer/ultra_honk_composer.test.cpp @@ -1,1036 +1,1039 @@ -#include "ultra_honk_composer.hpp" -#include "barretenberg/common/log.hpp" -#include "barretenberg/honk/proof_system/ultra_prover.hpp" -#include "barretenberg/honk/sumcheck/relations/relation.hpp" -#include "barretenberg/numeric/uint256/uint256.hpp" -#include "barretenberg/honk/flavor/flavor.hpp" -#include -#include -#include "barretenberg/honk/proof_system/prover.hpp" -#include "barretenberg/honk/sumcheck/sumcheck_round.hpp" -#include "barretenberg/honk/sumcheck/relations/grand_product_computation_relation.hpp" -#include "barretenberg/honk/sumcheck/relations/grand_product_initialization_relation.hpp" -#include "barretenberg/honk/utils/public_inputs.hpp" - -// TODO(luke): TEMPORARY; for testing only (comparison with Ultra Plonk composers) -#include "barretenberg/plonk/composer/ultra_composer.hpp" -#include "barretenberg/plonk/composer/splitting_tmp/ultra_plonk_composer.hpp" -#include "barretenberg/plonk/proof_system/prover/prover.hpp" - -#include -#include - -using namespace proof_system::honk; - -namespace test_ultra_honk_composer { - -std::vector add_variables(auto& composer, std::vector variables) -{ - std::vector res; - for (size_t i = 0; i < variables.size(); i++) { - res.emplace_back(composer.add_variable(variables[i])); - } - return res; -} - -/** - * @brief TEMPORARY method for checking consistency of polynomials computed by Ultra Plonk/Honk composers - * - * @param honk_prover - * @param plonk_prover - */ -// NOTE: Currently checking exact consistency for witness polynomials (wires, sorted lists) and table polys. -// The permutation polys are computed differently between plonk and honk so we do not expect consistency. -// Equality is checked on all selectors but we ignore the final entry since we do not enforce non-zero selectors in -// Honk. -void verify_consistency(honk::UltraProver& honk_prover, plonk::UltraProver& plonk_prover) -{ - auto& honk_store = honk_prover.key->polynomial_store; - auto& plonk_store = plonk_prover.key->polynomial_store; - - // Check that all selectors agree (aside from the final element which will differ due to not enforcing non-zero - // selectors in Honk). - for (auto& entry : honk_store) { - std::string key = entry.first; - bool is_selector = (key.find("q_") != std::string::npos) || (key.find("table_type") != std::string::npos); - if (plonk_store.contains(key) && is_selector) { - // check equality for all but final entry - for (size_t i = 0; i < honk_store.get(key).size() - 1; ++i) { - ASSERT_EQ(honk_store.get(key)[i], plonk_store.get(key)[i]); - } - } - } - - // Check that sorted witness-table and table polys agree - for (auto& entry : honk_store) { - std::string key = entry.first; - bool is_sorted_table = (key.find("s_") != std::string::npos); - bool is_table = (key.find("table_value_") != std::string::npos); - if (plonk_store.contains(key) && (is_sorted_table || is_table)) { - ASSERT_EQ(honk_store.get(key), plonk_store.get(key)); - } - } - - // Check that all wires agree - // Note: for Honk, wires are owned directly by the prover. For Plonk they are stored in the key. - for (size_t i = 0; i < 4; ++i) { - std::string label = "w_" + std::to_string(i + 1) + "_lagrange"; - ASSERT_EQ(honk_prover.wire_polynomials[i], plonk_prover.key->polynomial_store.get(label)); - } -} - -/** - * @brief TEMPORARY (verbose) method for checking consistency of polynomials computed by Ultra Plonk/Honk composers - * - * @param honk_prover - * @param plonk_prover - */ -void check_consistency(honk::UltraProver& honk_prover, plonk::UltraProver& plonk_prover) -{ - auto& honk_store = honk_prover.key->polynomial_store; - auto& plonk_store = plonk_prover.key->polynomial_store; - for (auto& entry : honk_store) { - std::string key = entry.first; - if (plonk_store.contains(key)) { - - bool polys_equal = (honk_store.get(key) == plonk_store.get(key)); - if (polys_equal) { - info("Equal: ", key); - } - if (!polys_equal) { - info("UNEQUAL: ", key); - } - } - } - - for (size_t i = 0; i < 4; ++i) { - std::string label = "w_" + std::to_string(i + 1) + "_lagrange"; - bool wire_equal = (honk_prover.wire_polynomials[i] == plonk_prover.key->polynomial_store.get(label)); - if (wire_equal) { - info("Wire Equal: ", i); - } - if (!wire_equal) { - info("Wire UNEQUAL: ", i); - } - } - - // std::string label = "w_1_lagrange"; - // for (size_t i = 0; i < plonk_store.get(label).size(); ++i) { - // auto val_honk = honk_prover.wire_polynomials[0][i]; - // // auto val_honk = honk_store.get(label)[i]; - // auto val_plonk = plonk_store.get(label)[i]; - // if (val_honk != val_plonk) { - // info("UNEQUAL index = ", i); - // info("honk: ",val_honk); - // info("plonk: ", val_plonk); - // } - // } -} - -TEST(UltraHonkComposer, create_gates_from_plookup_accumulators) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - barretenberg::fr input_value = fr::random_element(); - { - - const fr input_hi = uint256_t(input_value).slice(126, 256); - const fr input_lo = uint256_t(input_value).slice(0, 126); - const auto input_hi_index = honk_composer.add_variable(input_hi); - const auto input_lo_index = honk_composer.add_variable(input_lo); - - const auto sequence_data_hi = - plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_HI, input_hi); - const auto sequence_data_lo = - plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_LO, input_lo); - - const auto lookup_witnesses_hi = honk_composer.create_gates_from_plookup_accumulators( - plookup::MultiTableId::PEDERSEN_LEFT_HI, sequence_data_hi, input_hi_index); - const auto lookup_witnesses_lo = honk_composer.create_gates_from_plookup_accumulators( - plookup::MultiTableId::PEDERSEN_LEFT_LO, sequence_data_lo, input_lo_index); - } - { - const fr input_hi = uint256_t(input_value).slice(126, 256); - const fr input_lo = uint256_t(input_value).slice(0, 126); - const auto input_hi_index = plonk_composer.add_variable(input_hi); - const auto input_lo_index = plonk_composer.add_variable(input_lo); - - const auto sequence_data_hi = - plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_HI, input_hi); - const auto sequence_data_lo = - plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_LO, input_lo); - - const auto lookup_witnesses_hi = plonk_composer.create_gates_from_plookup_accumulators( - plookup::MultiTableId::PEDERSEN_LEFT_HI, sequence_data_hi, input_hi_index); - const auto lookup_witnesses_lo = plonk_composer.create_gates_from_plookup_accumulators( - plookup::MultiTableId::PEDERSEN_LEFT_LO, sequence_data_lo, input_lo_index); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -/** - * @brief Build UltraHonkComposer - * - */ -TEST(UltraHonkComposer, test_no_lookup_proof) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - size_t MM = 4; - for (size_t i = 0; i < MM; ++i) { - for (size_t j = 0; j < MM; ++j) { - uint64_t left = static_cast(j); - uint64_t right = static_cast(i); - uint32_t left_idx = honk_composer.add_variable(fr(left)); - uint32_t right_idx = honk_composer.add_variable(fr(right)); - uint32_t result_idx = honk_composer.add_variable(fr(left ^ right)); - - uint32_t add_idx = - honk_composer.add_variable(fr(left) + fr(right) + honk_composer.get_variable(result_idx)); - honk_composer.create_big_add_gate( - { left_idx, right_idx, result_idx, add_idx, fr(1), fr(1), fr(1), fr(-1), fr(0) }); - } - } - - for (size_t i = 0; i < MM; ++i) { - for (size_t j = 0; j < MM; ++j) { - uint64_t left = static_cast(j); - uint64_t right = static_cast(i); - uint32_t left_idx = plonk_composer.add_variable(fr(left)); - uint32_t right_idx = plonk_composer.add_variable(fr(right)); - uint32_t result_idx = plonk_composer.add_variable(fr(left ^ right)); - - uint32_t add_idx = - plonk_composer.add_variable(fr(left) + fr(right) + plonk_composer.get_variable(result_idx)); - plonk_composer.create_big_add_gate( - { left_idx, right_idx, result_idx, add_idx, fr(1), fr(1), fr(1), fr(-1), fr(0) }); - } - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, test_elliptic_gate) -{ - typedef grumpkin::g1::affine_element affine_element; - typedef grumpkin::g1::element element; - - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - affine_element p1 = crypto::generators::get_generator_data({ 0, 0 }).generator; - affine_element p2 = crypto::generators::get_generator_data({ 0, 1 }).generator; - affine_element p3(element(p1) + element(p2)); - - uint32_t x1 = honk_composer.add_variable(p1.x); - uint32_t y1 = honk_composer.add_variable(p1.y); - uint32_t x2 = honk_composer.add_variable(p2.x); - uint32_t y2 = honk_composer.add_variable(p2.y); - uint32_t x3 = honk_composer.add_variable(p3.x); - uint32_t y3 = honk_composer.add_variable(p3.y); - - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; - honk_composer.create_ecc_add_gate(gate); - - grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); - affine_element p2_endo = p2; - p2_endo.x *= beta; - p3 = affine_element(element(p1) + element(p2_endo)); - x3 = honk_composer.add_variable(p3.x); - y3 = honk_composer.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; - honk_composer.create_ecc_add_gate(gate); - - p2_endo.x *= beta; - p3 = affine_element(element(p1) - element(p2_endo)); - x3 = honk_composer.add_variable(p3.x); - y3 = honk_composer.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - honk_composer.create_ecc_add_gate(gate); - } - { - affine_element p1 = crypto::generators::get_generator_data({ 0, 0 }).generator; - affine_element p2 = crypto::generators::get_generator_data({ 0, 1 }).generator; - affine_element p3(element(p1) + element(p2)); - - uint32_t x1 = plonk_composer.add_variable(p1.x); - uint32_t y1 = plonk_composer.add_variable(p1.y); - uint32_t x2 = plonk_composer.add_variable(p2.x); - uint32_t y2 = plonk_composer.add_variable(p2.y); - uint32_t x3 = plonk_composer.add_variable(p3.x); - uint32_t y3 = plonk_composer.add_variable(p3.y); - - ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; - plonk_composer.create_ecc_add_gate(gate); - - grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); - affine_element p2_endo = p2; - p2_endo.x *= beta; - p3 = affine_element(element(p1) + element(p2_endo)); - x3 = plonk_composer.add_variable(p3.x); - y3 = plonk_composer.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; - plonk_composer.create_ecc_add_gate(gate); - - p2_endo.x *= beta; - p3 = affine_element(element(p1) - element(p2_endo)); - x3 = plonk_composer.add_variable(p3.x); - y3 = plonk_composer.add_variable(p3.y); - gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; - plonk_composer.create_ecc_add_gate(gate); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, non_trivial_tag_permutation) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - fr a = fr::random_element(); - { - fr b = -a; - - auto a_idx = honk_composer.add_variable(a); - auto b_idx = honk_composer.add_variable(b); - auto c_idx = honk_composer.add_variable(b); - auto d_idx = honk_composer.add_variable(a); - - honk_composer.create_add_gate( - { a_idx, b_idx, honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); - honk_composer.create_add_gate( - { c_idx, d_idx, honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); - - honk_composer.create_tag(1, 2); - honk_composer.create_tag(2, 1); - - honk_composer.assign_tag(a_idx, 1); - honk_composer.assign_tag(b_idx, 1); - honk_composer.assign_tag(c_idx, 2); - honk_composer.assign_tag(d_idx, 2); - } - { - fr b = -a; - - auto a_idx = plonk_composer.add_variable(a); - auto b_idx = plonk_composer.add_variable(b); - auto c_idx = plonk_composer.add_variable(b); - auto d_idx = plonk_composer.add_variable(a); - - plonk_composer.create_add_gate( - { a_idx, b_idx, plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); - plonk_composer.create_add_gate( - { c_idx, d_idx, plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); - - plonk_composer.create_tag(1, 2); - plonk_composer.create_tag(2, 1); - - plonk_composer.assign_tag(a_idx, 1); - plonk_composer.assign_tag(b_idx, 1); - plonk_composer.assign_tag(c_idx, 2); - plonk_composer.assign_tag(d_idx, 2); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, non_trivial_tag_permutation_and_cycles) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - fr a = fr::random_element(); - { - fr c = -a; - - auto a_idx = honk_composer.add_variable(a); - auto b_idx = honk_composer.add_variable(a); - honk_composer.assert_equal(a_idx, b_idx); - auto c_idx = honk_composer.add_variable(c); - auto d_idx = honk_composer.add_variable(c); - honk_composer.assert_equal(c_idx, d_idx); - auto e_idx = honk_composer.add_variable(a); - auto f_idx = honk_composer.add_variable(a); - honk_composer.assert_equal(e_idx, f_idx); - auto g_idx = honk_composer.add_variable(c); - auto h_idx = honk_composer.add_variable(c); - honk_composer.assert_equal(g_idx, h_idx); - - honk_composer.create_tag(1, 2); - honk_composer.create_tag(2, 1); - - honk_composer.assign_tag(a_idx, 1); - honk_composer.assign_tag(c_idx, 1); - honk_composer.assign_tag(e_idx, 2); - honk_composer.assign_tag(g_idx, 2); - - honk_composer.create_add_gate( - { b_idx, a_idx, honk_composer.get_zero_idx(), fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); - honk_composer.create_add_gate( - { c_idx, g_idx, honk_composer.get_zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); - honk_composer.create_add_gate( - { e_idx, f_idx, honk_composer.get_zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); - } - { - fr c = -a; - - auto a_idx = plonk_composer.add_variable(a); - auto b_idx = plonk_composer.add_variable(a); - plonk_composer.assert_equal(a_idx, b_idx); - auto c_idx = plonk_composer.add_variable(c); - auto d_idx = plonk_composer.add_variable(c); - plonk_composer.assert_equal(c_idx, d_idx); - auto e_idx = plonk_composer.add_variable(a); - auto f_idx = plonk_composer.add_variable(a); - plonk_composer.assert_equal(e_idx, f_idx); - auto g_idx = plonk_composer.add_variable(c); - auto h_idx = plonk_composer.add_variable(c); - plonk_composer.assert_equal(g_idx, h_idx); - - plonk_composer.create_tag(1, 2); - plonk_composer.create_tag(2, 1); - - plonk_composer.assign_tag(a_idx, 1); - plonk_composer.assign_tag(c_idx, 1); - plonk_composer.assign_tag(e_idx, 2); - plonk_composer.assign_tag(g_idx, 2); - - plonk_composer.create_add_gate( - { b_idx, a_idx, plonk_composer.zero_idx, fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); - plonk_composer.create_add_gate( - { c_idx, g_idx, plonk_composer.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); - plonk_composer.create_add_gate( - { e_idx, f_idx, plonk_composer.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, bad_tag_permutation) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - fr a = fr::random_element(); - { - fr b = -a; - - auto a_idx = honk_composer.add_variable(a); - auto b_idx = honk_composer.add_variable(b); - auto c_idx = honk_composer.add_variable(b); - auto d_idx = honk_composer.add_variable(a + 1); - - honk_composer.create_add_gate({ a_idx, b_idx, honk_composer.get_zero_idx(), 1, 1, 0, 0 }); - honk_composer.create_add_gate({ c_idx, d_idx, honk_composer.get_zero_idx(), 1, 1, 0, -1 }); - - honk_composer.create_tag(1, 2); - honk_composer.create_tag(2, 1); - - honk_composer.assign_tag(a_idx, 1); - honk_composer.assign_tag(b_idx, 1); - honk_composer.assign_tag(c_idx, 2); - honk_composer.assign_tag(d_idx, 2); - } - { - fr b = -a; - - auto a_idx = plonk_composer.add_variable(a); - auto b_idx = plonk_composer.add_variable(b); - auto c_idx = plonk_composer.add_variable(b); - auto d_idx = plonk_composer.add_variable(a + 1); - - plonk_composer.create_add_gate({ a_idx, b_idx, plonk_composer.zero_idx, 1, 1, 0, 0 }); - plonk_composer.create_add_gate({ c_idx, d_idx, plonk_composer.zero_idx, 1, 1, 0, -1 }); - - plonk_composer.create_tag(1, 2); - plonk_composer.create_tag(2, 1); - - plonk_composer.assign_tag(a_idx, 1); - plonk_composer.assign_tag(b_idx, 1); - plonk_composer.assign_tag(c_idx, 2); - plonk_composer.assign_tag(d_idx, 2); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, sort_widget) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - fr a = fr::one(); - fr b = fr(2); - fr c = fr(3); - fr d = fr(4); - - auto a_idx = honk_composer.add_variable(a); - auto b_idx = honk_composer.add_variable(b); - auto c_idx = honk_composer.add_variable(c); - auto d_idx = honk_composer.add_variable(d); - honk_composer.create_sort_constraint({ a_idx, b_idx, c_idx, d_idx }); - } - { - fr a = fr::one(); - fr b = fr(2); - fr c = fr(3); - fr d = fr(4); - - auto a_idx = plonk_composer.add_variable(a); - auto b_idx = plonk_composer.add_variable(b); - auto c_idx = plonk_composer.add_variable(c); - auto d_idx = plonk_composer.add_variable(d); - plonk_composer.create_sort_constraint({ a_idx, b_idx, c_idx, d_idx }); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, sort_with_edges_gate) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - auto idx = add_variables(honk_composer, { 1, 2, 5, 6, 7, 10, 11, 13, 16, 17, 20, 22, 22, 25, - 26, 29, 29, 32, 32, 33, 35, 38, 39, 39, 42, 42, 43, 45 }); - - honk_composer.create_sort_constraint_with_edges(idx, 1, 29); - } - { - auto idx = add_variables(plonk_composer, { 1, 2, 5, 6, 7, 10, 11, 13, 16, 17, 20, 22, 22, 25, - 26, 29, 29, 32, 32, 33, 35, 38, 39, 39, 42, 42, 43, 45 }); - - plonk_composer.create_sort_constraint_with_edges(idx, 1, 29); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, range_constraint) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - auto indices = - add_variables(honk_composer, { 1, 0, 3, 80, 5, 6, 29, 8, 15, 11, 32, 21, 42, 79, 16, 10, 3, 26, 13, 14 }); - for (size_t i = 0; i < indices.size(); i++) { - honk_composer.create_new_range_constraint(indices[i], 79); - } - honk_composer.create_dummy_constraints(indices); - } - { - auto indices = - add_variables(plonk_composer, { 1, 0, 3, 80, 5, 6, 29, 8, 15, 11, 32, 21, 42, 79, 16, 10, 3, 26, 13, 14 }); - for (size_t i = 0; i < indices.size(); i++) { - plonk_composer.create_new_range_constraint(indices[i], 79); - } - plonk_composer.create_dummy_constraints(indices); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, range_with_gates) -{ - - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - auto idx = add_variables(honk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); - for (size_t i = 0; i < idx.size(); i++) { - honk_composer.create_new_range_constraint(idx[i], 8); - } - - honk_composer.create_add_gate( - { idx[0], idx[1], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); - honk_composer.create_add_gate( - { idx[2], idx[3], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); - honk_composer.create_add_gate( - { idx[4], idx[5], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); - honk_composer.create_add_gate( - { idx[6], idx[7], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); - } - { - auto idx = add_variables(plonk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); - for (size_t i = 0; i < idx.size(); i++) { - plonk_composer.create_new_range_constraint(idx[i], 8); - } - - plonk_composer.create_add_gate( - { idx[0], idx[1], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - plonk_composer.create_add_gate( - { idx[2], idx[3], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); - plonk_composer.create_add_gate( - { idx[4], idx[5], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); - plonk_composer.create_add_gate( - { idx[6], idx[7], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, range_with_gates_where_range_is_not_a_power_of_two) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - auto idx = add_variables(honk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); - for (size_t i = 0; i < idx.size(); i++) { - honk_composer.create_new_range_constraint(idx[i], 12); - } - - honk_composer.create_add_gate( - { idx[0], idx[1], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); - honk_composer.create_add_gate( - { idx[2], idx[3], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); - honk_composer.create_add_gate( - { idx[4], idx[5], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); - honk_composer.create_add_gate( - { idx[6], idx[7], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); - } - { - auto idx = add_variables(plonk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); - for (size_t i = 0; i < idx.size(); i++) { - plonk_composer.create_new_range_constraint(idx[i], 12); - } - - plonk_composer.create_add_gate( - { idx[0], idx[1], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); - plonk_composer.create_add_gate( - { idx[2], idx[3], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); - plonk_composer.create_add_gate( - { idx[4], idx[5], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); - plonk_composer.create_add_gate( - { idx[6], idx[7], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, sort_widget_complex) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - { - std::vector a = { 1, 3, 4, 7, 7, 8, 16, 14, 15, 15, 18, 19, 21, 21, 24, 25, 26, 27, 30, 32 }; - std::vector ind; - for (size_t i = 0; i < a.size(); i++) - ind.emplace_back(honk_composer.add_variable(a[i])); - honk_composer.create_sort_constraint(ind); - } - { - std::vector a = { 1, 3, 4, 7, 7, 8, 16, 14, 15, 15, 18, 19, 21, 21, 24, 25, 26, 27, 30, 32 }; - std::vector ind; - for (size_t i = 0; i < a.size(); i++) - ind.emplace_back(plonk_composer.add_variable(a[i])); - plonk_composer.create_sort_constraint(ind); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, composed_range_constraint) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - auto c = fr::random_element(); - { - auto d = uint256_t(c).slice(0, 133); - auto e = fr(d); - auto a_idx = honk_composer.add_variable(fr(e)); - honk_composer.create_add_gate( - { a_idx, honk_composer.get_zero_idx(), honk_composer.get_zero_idx(), 1, 0, 0, -fr(e) }); - honk_composer.decompose_into_default_range(a_idx, 134); - } - { - auto d = uint256_t(c).slice(0, 133); - auto e = fr(d); - auto a_idx = plonk_composer.add_variable(fr(e)); - plonk_composer.create_add_gate({ a_idx, plonk_composer.zero_idx, plonk_composer.zero_idx, 1, 0, 0, -fr(e) }); - plonk_composer.decompose_into_default_range(a_idx, 134); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, non_native_field_multiplication) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - fq a = fq::random_element(); - fq b = fq::random_element(); - { - uint256_t modulus = fq::modulus; - - uint1024_t a_big = uint512_t(uint256_t(a)); - uint1024_t b_big = uint512_t(uint256_t(b)); - uint1024_t p_big = uint512_t(uint256_t(modulus)); - - uint1024_t q_big = (a_big * b_big) / p_big; - uint1024_t r_big = (a_big * b_big) % p_big; - - uint256_t q(q_big.lo.lo); - uint256_t r(r_big.lo.lo); - - const auto split_into_limbs = [&](const uint512_t& input) { - constexpr size_t NUM_BITS = 68; - std::array limbs; - limbs[0] = input.slice(0, NUM_BITS).lo; - limbs[1] = input.slice(NUM_BITS * 1, NUM_BITS * 2).lo; - limbs[2] = input.slice(NUM_BITS * 2, NUM_BITS * 3).lo; - limbs[3] = input.slice(NUM_BITS * 3, NUM_BITS * 4).lo; - limbs[4] = fr(input.lo); - return limbs; - }; - - const auto get_limb_witness_indices = [&](const std::array& limbs) { - std::array limb_indices; - limb_indices[0] = honk_composer.add_variable(limbs[0]); - limb_indices[1] = honk_composer.add_variable(limbs[1]); - limb_indices[2] = honk_composer.add_variable(limbs[2]); - limb_indices[3] = honk_composer.add_variable(limbs[3]); - limb_indices[4] = honk_composer.add_variable(limbs[4]); - return limb_indices; - }; - const uint512_t BINARY_BASIS_MODULUS = uint512_t(1) << (68 * 4); - auto modulus_limbs = split_into_limbs(BINARY_BASIS_MODULUS - uint512_t(modulus)); - - const auto a_indices = get_limb_witness_indices(split_into_limbs(uint256_t(a))); - const auto b_indices = get_limb_witness_indices(split_into_limbs(uint256_t(b))); - const auto q_indices = get_limb_witness_indices(split_into_limbs(uint256_t(q))); - const auto r_indices = get_limb_witness_indices(split_into_limbs(uint256_t(r))); - - proof_system::non_native_field_witnesses inputs{ - a_indices, b_indices, q_indices, r_indices, modulus_limbs, fr(uint256_t(modulus)), - }; - const auto [lo_1_idx, hi_1_idx] = honk_composer.evaluate_non_native_field_multiplication(inputs); - honk_composer.range_constrain_two_limbs(lo_1_idx, hi_1_idx, 70, 70); - } - { - uint256_t modulus = fq::modulus; - - uint1024_t a_big = uint512_t(uint256_t(a)); - uint1024_t b_big = uint512_t(uint256_t(b)); - uint1024_t p_big = uint512_t(uint256_t(modulus)); - - uint1024_t q_big = (a_big * b_big) / p_big; - uint1024_t r_big = (a_big * b_big) % p_big; - - uint256_t q(q_big.lo.lo); - uint256_t r(r_big.lo.lo); - - const auto split_into_limbs = [&](const uint512_t& input) { - constexpr size_t NUM_BITS = 68; - std::array limbs; - limbs[0] = input.slice(0, NUM_BITS).lo; - limbs[1] = input.slice(NUM_BITS * 1, NUM_BITS * 2).lo; - limbs[2] = input.slice(NUM_BITS * 2, NUM_BITS * 3).lo; - limbs[3] = input.slice(NUM_BITS * 3, NUM_BITS * 4).lo; - limbs[4] = fr(input.lo); - return limbs; - }; - - const auto get_limb_witness_indices = [&](const std::array& limbs) { - std::array limb_indices; - limb_indices[0] = plonk_composer.add_variable(limbs[0]); - limb_indices[1] = plonk_composer.add_variable(limbs[1]); - limb_indices[2] = plonk_composer.add_variable(limbs[2]); - limb_indices[3] = plonk_composer.add_variable(limbs[3]); - limb_indices[4] = plonk_composer.add_variable(limbs[4]); - return limb_indices; - }; - const uint512_t BINARY_BASIS_MODULUS = uint512_t(1) << (68 * 4); - auto modulus_limbs = split_into_limbs(BINARY_BASIS_MODULUS - uint512_t(modulus)); - - const auto a_indices = get_limb_witness_indices(split_into_limbs(uint256_t(a))); - const auto b_indices = get_limb_witness_indices(split_into_limbs(uint256_t(b))); - const auto q_indices = get_limb_witness_indices(split_into_limbs(uint256_t(q))); - const auto r_indices = get_limb_witness_indices(split_into_limbs(uint256_t(r))); - - proof_system::plonk::UltraComposer::non_native_field_witnesses inputs{ - a_indices, b_indices, q_indices, r_indices, modulus_limbs, fr(uint256_t(modulus)), - }; - const auto [lo_1_idx, hi_1_idx] = plonk_composer.evaluate_non_native_field_multiplication(inputs); - plonk_composer.range_constrain_two_limbs(lo_1_idx, hi_1_idx, 70, 70); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, rom) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - auto a = fr::random_element(); - auto b = fr::random_element(); - auto c = fr::random_element(); - auto d = fr::random_element(); - auto e = fr::random_element(); - auto f = fr::random_element(); - auto g = fr::random_element(); - auto h = fr::random_element(); - { - uint32_t rom_values[8]{ - honk_composer.add_variable(a), honk_composer.add_variable(b), honk_composer.add_variable(c), - honk_composer.add_variable(d), honk_composer.add_variable(e), honk_composer.add_variable(f), - honk_composer.add_variable(g), honk_composer.add_variable(h), - }; - - size_t rom_id = honk_composer.create_ROM_array(8); - - for (size_t i = 0; i < 8; ++i) { - honk_composer.set_ROM_element(rom_id, i, rom_values[i]); - } - - uint32_t a_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(5)); - EXPECT_EQ(a_idx != rom_values[5], true); - uint32_t b_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(4)); - uint32_t c_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(1)); - - const auto d_value = - honk_composer.get_variable(a_idx) + honk_composer.get_variable(b_idx) + honk_composer.get_variable(c_idx); - uint32_t d_idx = honk_composer.add_variable(d_value); - - honk_composer.create_big_add_gate({ - a_idx, - b_idx, - c_idx, - d_idx, - 1, - 1, - 1, - -1, - 0, - }); - } - { - uint32_t rom_values[8]{ - plonk_composer.add_variable(a), plonk_composer.add_variable(b), plonk_composer.add_variable(c), - plonk_composer.add_variable(d), plonk_composer.add_variable(e), plonk_composer.add_variable(f), - plonk_composer.add_variable(g), plonk_composer.add_variable(h), - }; - - size_t rom_id = plonk_composer.create_ROM_array(8); - - for (size_t i = 0; i < 8; ++i) { - plonk_composer.set_ROM_element(rom_id, i, rom_values[i]); - } - - uint32_t a_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(5)); - EXPECT_EQ(a_idx != rom_values[5], true); - uint32_t b_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(4)); - uint32_t c_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(1)); - - const auto d_value = plonk_composer.get_variable(a_idx) + plonk_composer.get_variable(b_idx) + - plonk_composer.get_variable(c_idx); - uint32_t d_idx = plonk_composer.add_variable(d_value); - - plonk_composer.create_big_add_gate({ - a_idx, - b_idx, - c_idx, - d_idx, - 1, - 1, - 1, - -1, - 0, - }); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - check_consistency(honk_prover, plonk_prover); - verify_consistency(honk_prover, plonk_prover); -} - -TEST(UltraHonkComposer, ram) -{ - auto honk_composer = UltraHonkComposer(); - auto plonk_composer = proof_system::plonk::UltraComposer(); - - auto a = fr::random_element(); - auto b = fr::random_element(); - auto c = fr::random_element(); - auto d = fr::random_element(); - auto e = fr::random_element(); - auto f = fr::random_element(); - auto g = fr::random_element(); - auto h = fr::random_element(); - { - uint32_t ram_values[8]{ - honk_composer.add_variable(a), honk_composer.add_variable(b), honk_composer.add_variable(c), - honk_composer.add_variable(d), honk_composer.add_variable(e), honk_composer.add_variable(f), - honk_composer.add_variable(g), honk_composer.add_variable(h), - }; - - size_t ram_id = honk_composer.create_RAM_array(8); - - for (size_t i = 0; i < 8; ++i) { - honk_composer.init_RAM_element(ram_id, i, ram_values[i]); - } - - uint32_t a_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(5)); - EXPECT_EQ(a_idx != ram_values[5], true); - - uint32_t b_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(4)); - uint32_t c_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(1)); - - honk_composer.write_RAM_array(ram_id, honk_composer.add_variable(4), honk_composer.add_variable(500)); - uint32_t d_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(4)); - - EXPECT_EQ(honk_composer.get_variable(d_idx), 500); - - // ensure these vars get used in another arithmetic gate - const auto e_value = honk_composer.get_variable(a_idx) + honk_composer.get_variable(b_idx) + - honk_composer.get_variable(c_idx) + honk_composer.get_variable(d_idx); - uint32_t e_idx = honk_composer.add_variable(e_value); - - honk_composer.create_big_add_gate( - { - a_idx, - b_idx, - c_idx, - d_idx, - -1, - -1, - -1, - -1, - 0, - }, - true); - honk_composer.create_big_add_gate( - { - honk_composer.get_zero_idx(), - honk_composer.get_zero_idx(), - honk_composer.get_zero_idx(), - e_idx, - 0, - 0, - 0, - 0, - 0, - }, - false); - } - { - uint32_t ram_values[8]{ - plonk_composer.add_variable(a), plonk_composer.add_variable(b), plonk_composer.add_variable(c), - plonk_composer.add_variable(d), plonk_composer.add_variable(e), plonk_composer.add_variable(f), - plonk_composer.add_variable(g), plonk_composer.add_variable(h), - }; - - size_t ram_id = plonk_composer.create_RAM_array(8); - - for (size_t i = 0; i < 8; ++i) { - plonk_composer.init_RAM_element(ram_id, i, ram_values[i]); - } - - uint32_t a_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(5)); - EXPECT_EQ(a_idx != ram_values[5], true); - - uint32_t b_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(4)); - uint32_t c_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(1)); - - plonk_composer.write_RAM_array(ram_id, plonk_composer.add_variable(4), plonk_composer.add_variable(500)); - uint32_t d_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(4)); - - EXPECT_EQ(plonk_composer.get_variable(d_idx), 500); - - // ensure these vars get used in another arithmetic gate - const auto e_value = plonk_composer.get_variable(a_idx) + plonk_composer.get_variable(b_idx) + - plonk_composer.get_variable(c_idx) + plonk_composer.get_variable(d_idx); - uint32_t e_idx = plonk_composer.add_variable(e_value); - - plonk_composer.create_big_add_gate( - { - a_idx, - b_idx, - c_idx, - d_idx, - -1, - -1, - -1, - -1, - 0, - }, - true); - plonk_composer.create_big_add_gate( - { - plonk_composer.zero_idx, - plonk_composer.zero_idx, - plonk_composer.zero_idx, - e_idx, - 0, - 0, - 0, - 0, - 0, - }, - false); - } - - auto honk_prover = honk_composer.create_prover(); - auto plonk_prover = plonk_composer.create_prover(); - - verify_consistency(honk_prover, plonk_prover); -} - -} // namespace test_ultra_honk_composer +// #include "ultra_honk_composer.hpp" +// #include "barretenberg/common/log.hpp" +// #include "barretenberg/honk/proof_system/ultra_prover.hpp" +// #include "barretenberg/honk/sumcheck/relations/relation.hpp" +// #include "barretenberg/numeric/uint256/uint256.hpp" +// #include "barretenberg/honk/flavor/flavor.hpp" +// #include +// #include +// #include "barretenberg/honk/proof_system/prover.hpp" +// #include "barretenberg/honk/sumcheck/sumcheck_round.hpp" +// #include "barretenberg/honk/sumcheck/relations/grand_product_computation_relation.hpp" +// #include "barretenberg/honk/sumcheck/relations/grand_product_initialization_relation.hpp" +// #include "barretenberg/honk/utils/public_inputs.hpp" + +// // TODO(luke): TEMPORARY; for testing only (comparison with Ultra Plonk composers) +// #include "barretenberg/plonk/composer/ultra_composer.hpp" +// #include "barretenberg/plonk/composer/splitting_tmp/ultra_plonk_composer.hpp" +// #include "barretenberg/plonk/proof_system/prover/prover.hpp" + +// #include +// #include + +// using namespace proof_system::honk; + +// namespace test_ultra_honk_composer { + +// std::vector add_variables(auto& composer, std::vector variables) +// { +// std::vector res; +// for (size_t i = 0; i < variables.size(); i++) { +// res.emplace_back(composer.add_variable(variables[i])); +// } +// return res; +// } + +// /** +// * @brief TEMPORARY method for checking consistency of polynomials computed by Ultra Plonk/Honk composers +// * +// * @param honk_prover +// * @param plonk_prover +// */ +// // NOTE: Currently checking exact consistency for witness polynomials (wires, sorted lists) and table polys. +// // The permutation polys are computed differently between plonk and honk so we do not expect consistency. +// // Equality is checked on all selectors but we ignore the final entry since we do not enforce non-zero selectors in +// // Honk. +// void verify_consistency(honk::UltraProver& honk_prover, plonk::UltraProver& plonk_prover) +// { +// auto& honk_store = honk_prover.proving_key->polynomial_store; +// auto& plonk_store = plonk_prover.key->polynomial_store; + +// // Check that all selectors agree (aside from the final element which will differ due to not enforcing non-zero +// // selectors in Honk). +// for (auto& entry : honk_store) { +// std::string key = entry.first; +// bool is_selector = (key.find("q_") != std::string::npos) || (key.find("table_type") != std::string::npos); +// if (plonk_store.contains(key) && is_selector) { +// // check equality for all but final entry +// for (size_t i = 0; i < honk_store.get(key).size() - 1; ++i) { +// ASSERT_EQ(honk_store.get(key)[i], plonk_store.get(key)[i]); +// } +// } +// } + +// // Check that sorted witness-table and table polys agree +// for (auto& entry : honk_store) { +// std::string key = entry.first; +// bool is_sorted_table = (key.find("s_") != std::string::npos); +// bool is_table = (key.find("table_value_") != std::string::npos); +// if (plonk_store.contains(key) && (is_sorted_table || is_table)) { +// ASSERT_EQ(honk_store.get(key), plonk_store.get(key)); +// } +// } + +// // Check that all wires agree +// // Note: for Honk, wires are owned directly by the prover. For Plonk they are stored in the key. +// for (size_t i = 0; i < 4; ++i) { +// std::string label = "w_" + std::to_string(i + 1) + "_lagrange"; +// ASSERT_EQ(honk_prover.wire_polynomials[i], plonk_prover.key->polynomial_store.get(label)); +// } +// } + +// /** +// * @brief TEMPORARY (verbose) method for checking consistency of polynomials computed by Ultra Plonk/Honk composers +// * +// * @param honk_prover +// * @param plonk_prover +// */ +// void check_consistency(honk::UltraProver& honk_prover, plonk::UltraProver& plonk_prover) +// { +// auto& honk_store = honk_prover.proving_key->polynomial_store; +// auto& plonk_store = plonk_prover.key->polynomial_store; +// for (auto& entry : honk_store) { +// std::string key = entry.first; +// if (plonk_store.contains(key)) { + +// bool polys_equal = (honk_store.get(key) == plonk_store.get(key)); +// if (polys_equal) { +// info("Equal: ", key); +// } +// if (!polys_equal) { +// info("UNEQUAL: ", key); +// } +// } +// } + +// for (size_t i = 0; i < 4; ++i) { +// std::string label = "w_" + std::to_string(i + 1) + "_lagrange"; +// bool wire_equal = (honk_prover.wire_polynomials[i] == plonk_prover.key->polynomial_store.get(label)); +// if (wire_equal) { +// info("Wire Equal: ", i); +// } +// if (!wire_equal) { +// info("Wire UNEQUAL: ", i); +// } +// } + +// // std::string label = "w_1_lagrange"; +// // for (size_t i = 0; i < plonk_store.get(label).size(); ++i) { +// // auto val_honk = honk_prover.wire_polynomials[0][i]; +// // // auto val_honk = honk_store.get(label)[i]; +// // auto val_plonk = plonk_store.get(label)[i]; +// // if (val_honk != val_plonk) { +// // info("UNEQUAL index = ", i); +// // info("honk: ",val_honk); +// // info("plonk: ", val_plonk); +// // } +// // } +// } + +// TEST(UltraHonkComposer, create_gates_from_plookup_accumulators) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// barretenberg::fr input_value = fr::random_element(); +// { + +// const fr input_hi = uint256_t(input_value).slice(126, 256); +// const fr input_lo = uint256_t(input_value).slice(0, 126); +// const auto input_hi_index = honk_composer.add_variable(input_hi); +// const auto input_lo_index = honk_composer.add_variable(input_lo); + +// const auto sequence_data_hi = +// plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_HI, input_hi); +// const auto sequence_data_lo = +// plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_LO, input_lo); + +// const auto lookup_witnesses_hi = honk_composer.create_gates_from_plookup_accumulators( +// plookup::MultiTableId::PEDERSEN_LEFT_HI, sequence_data_hi, input_hi_index); +// const auto lookup_witnesses_lo = honk_composer.create_gates_from_plookup_accumulators( +// plookup::MultiTableId::PEDERSEN_LEFT_LO, sequence_data_lo, input_lo_index); +// } +// { +// const fr input_hi = uint256_t(input_value).slice(126, 256); +// const fr input_lo = uint256_t(input_value).slice(0, 126); +// const auto input_hi_index = plonk_composer.add_variable(input_hi); +// const auto input_lo_index = plonk_composer.add_variable(input_lo); + +// const auto sequence_data_hi = +// plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_HI, input_hi); +// const auto sequence_data_lo = +// plookup::get_lookup_accumulators(plookup::MultiTableId::PEDERSEN_LEFT_LO, input_lo); + +// const auto lookup_witnesses_hi = plonk_composer.create_gates_from_plookup_accumulators( +// plookup::MultiTableId::PEDERSEN_LEFT_HI, sequence_data_hi, input_hi_index); +// const auto lookup_witnesses_lo = plonk_composer.create_gates_from_plookup_accumulators( +// plookup::MultiTableId::PEDERSEN_LEFT_LO, sequence_data_lo, input_lo_index); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// /** +// * @brief Build UltraHonkComposer +// * +// */ +// TEST(UltraHonkComposer, test_no_lookup_proof) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// size_t MM = 4; +// for (size_t i = 0; i < MM; ++i) { +// for (size_t j = 0; j < MM; ++j) { +// uint64_t left = static_cast(j); +// uint64_t right = static_cast(i); +// uint32_t left_idx = honk_composer.add_variable(fr(left)); +// uint32_t right_idx = honk_composer.add_variable(fr(right)); +// uint32_t result_idx = honk_composer.add_variable(fr(left ^ right)); + +// uint32_t add_idx = +// honk_composer.add_variable(fr(left) + fr(right) + honk_composer.get_variable(result_idx)); +// honk_composer.create_big_add_gate( +// { left_idx, right_idx, result_idx, add_idx, fr(1), fr(1), fr(1), fr(-1), fr(0) }); +// } +// } + +// for (size_t i = 0; i < MM; ++i) { +// for (size_t j = 0; j < MM; ++j) { +// uint64_t left = static_cast(j); +// uint64_t right = static_cast(i); +// uint32_t left_idx = plonk_composer.add_variable(fr(left)); +// uint32_t right_idx = plonk_composer.add_variable(fr(right)); +// uint32_t result_idx = plonk_composer.add_variable(fr(left ^ right)); + +// uint32_t add_idx = +// plonk_composer.add_variable(fr(left) + fr(right) + plonk_composer.get_variable(result_idx)); +// plonk_composer.create_big_add_gate( +// { left_idx, right_idx, result_idx, add_idx, fr(1), fr(1), fr(1), fr(-1), fr(0) }); +// } +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, test_elliptic_gate) +// { +// typedef grumpkin::g1::affine_element affine_element; +// typedef grumpkin::g1::element element; + +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// affine_element p1 = crypto::generators::get_generator_data({ 0, 0 }).generator; +// affine_element p2 = crypto::generators::get_generator_data({ 0, 1 }).generator; +// affine_element p3(element(p1) + element(p2)); + +// uint32_t x1 = honk_composer.add_variable(p1.x); +// uint32_t y1 = honk_composer.add_variable(p1.y); +// uint32_t x2 = honk_composer.add_variable(p2.x); +// uint32_t y2 = honk_composer.add_variable(p2.y); +// uint32_t x3 = honk_composer.add_variable(p3.x); +// uint32_t y3 = honk_composer.add_variable(p3.y); + +// ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; +// honk_composer.create_ecc_add_gate(gate); + +// grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); +// affine_element p2_endo = p2; +// p2_endo.x *= beta; +// p3 = affine_element(element(p1) + element(p2_endo)); +// x3 = honk_composer.add_variable(p3.x); +// y3 = honk_composer.add_variable(p3.y); +// gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; +// honk_composer.create_ecc_add_gate(gate); + +// p2_endo.x *= beta; +// p3 = affine_element(element(p1) - element(p2_endo)); +// x3 = honk_composer.add_variable(p3.x); +// y3 = honk_composer.add_variable(p3.y); +// gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; +// honk_composer.create_ecc_add_gate(gate); +// } +// { +// affine_element p1 = crypto::generators::get_generator_data({ 0, 0 }).generator; +// affine_element p2 = crypto::generators::get_generator_data({ 0, 1 }).generator; +// affine_element p3(element(p1) + element(p2)); + +// uint32_t x1 = plonk_composer.add_variable(p1.x); +// uint32_t y1 = plonk_composer.add_variable(p1.y); +// uint32_t x2 = plonk_composer.add_variable(p2.x); +// uint32_t y2 = plonk_composer.add_variable(p2.y); +// uint32_t x3 = plonk_composer.add_variable(p3.x); +// uint32_t y3 = plonk_composer.add_variable(p3.y); + +// ecc_add_gate gate{ x1, y1, x2, y2, x3, y3, 1, 1 }; +// plonk_composer.create_ecc_add_gate(gate); + +// grumpkin::fq beta = grumpkin::fq::cube_root_of_unity(); +// affine_element p2_endo = p2; +// p2_endo.x *= beta; +// p3 = affine_element(element(p1) + element(p2_endo)); +// x3 = plonk_composer.add_variable(p3.x); +// y3 = plonk_composer.add_variable(p3.y); +// gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta, 1 }; +// plonk_composer.create_ecc_add_gate(gate); + +// p2_endo.x *= beta; +// p3 = affine_element(element(p1) - element(p2_endo)); +// x3 = plonk_composer.add_variable(p3.x); +// y3 = plonk_composer.add_variable(p3.y); +// gate = ecc_add_gate{ x1, y1, x2, y2, x3, y3, beta.sqr(), -1 }; +// plonk_composer.create_ecc_add_gate(gate); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, non_trivial_tag_permutation) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// fr a = fr::random_element(); +// { +// fr b = -a; + +// auto a_idx = honk_composer.add_variable(a); +// auto b_idx = honk_composer.add_variable(b); +// auto c_idx = honk_composer.add_variable(b); +// auto d_idx = honk_composer.add_variable(a); + +// honk_composer.create_add_gate( +// { a_idx, b_idx, honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); +// honk_composer.create_add_gate( +// { c_idx, d_idx, honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), fr::zero() }); + +// honk_composer.create_tag(1, 2); +// honk_composer.create_tag(2, 1); + +// honk_composer.assign_tag(a_idx, 1); +// honk_composer.assign_tag(b_idx, 1); +// honk_composer.assign_tag(c_idx, 2); +// honk_composer.assign_tag(d_idx, 2); +// } +// { +// fr b = -a; + +// auto a_idx = plonk_composer.add_variable(a); +// auto b_idx = plonk_composer.add_variable(b); +// auto c_idx = plonk_composer.add_variable(b); +// auto d_idx = plonk_composer.add_variable(a); + +// plonk_composer.create_add_gate( +// { a_idx, b_idx, plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); +// plonk_composer.create_add_gate( +// { c_idx, d_idx, plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), fr::zero() }); + +// plonk_composer.create_tag(1, 2); +// plonk_composer.create_tag(2, 1); + +// plonk_composer.assign_tag(a_idx, 1); +// plonk_composer.assign_tag(b_idx, 1); +// plonk_composer.assign_tag(c_idx, 2); +// plonk_composer.assign_tag(d_idx, 2); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, non_trivial_tag_permutation_and_cycles) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// fr a = fr::random_element(); +// { +// fr c = -a; + +// auto a_idx = honk_composer.add_variable(a); +// auto b_idx = honk_composer.add_variable(a); +// honk_composer.assert_equal(a_idx, b_idx); +// auto c_idx = honk_composer.add_variable(c); +// auto d_idx = honk_composer.add_variable(c); +// honk_composer.assert_equal(c_idx, d_idx); +// auto e_idx = honk_composer.add_variable(a); +// auto f_idx = honk_composer.add_variable(a); +// honk_composer.assert_equal(e_idx, f_idx); +// auto g_idx = honk_composer.add_variable(c); +// auto h_idx = honk_composer.add_variable(c); +// honk_composer.assert_equal(g_idx, h_idx); + +// honk_composer.create_tag(1, 2); +// honk_composer.create_tag(2, 1); + +// honk_composer.assign_tag(a_idx, 1); +// honk_composer.assign_tag(c_idx, 1); +// honk_composer.assign_tag(e_idx, 2); +// honk_composer.assign_tag(g_idx, 2); + +// honk_composer.create_add_gate( +// { b_idx, a_idx, honk_composer.get_zero_idx(), fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); +// honk_composer.create_add_gate( +// { c_idx, g_idx, honk_composer.get_zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); +// honk_composer.create_add_gate( +// { e_idx, f_idx, honk_composer.get_zero_idx(), fr::one(), -fr::one(), fr::zero(), fr::zero() }); +// } +// { +// fr c = -a; + +// auto a_idx = plonk_composer.add_variable(a); +// auto b_idx = plonk_composer.add_variable(a); +// plonk_composer.assert_equal(a_idx, b_idx); +// auto c_idx = plonk_composer.add_variable(c); +// auto d_idx = plonk_composer.add_variable(c); +// plonk_composer.assert_equal(c_idx, d_idx); +// auto e_idx = plonk_composer.add_variable(a); +// auto f_idx = plonk_composer.add_variable(a); +// plonk_composer.assert_equal(e_idx, f_idx); +// auto g_idx = plonk_composer.add_variable(c); +// auto h_idx = plonk_composer.add_variable(c); +// plonk_composer.assert_equal(g_idx, h_idx); + +// plonk_composer.create_tag(1, 2); +// plonk_composer.create_tag(2, 1); + +// plonk_composer.assign_tag(a_idx, 1); +// plonk_composer.assign_tag(c_idx, 1); +// plonk_composer.assign_tag(e_idx, 2); +// plonk_composer.assign_tag(g_idx, 2); + +// plonk_composer.create_add_gate( +// { b_idx, a_idx, plonk_composer.zero_idx, fr::one(), fr::neg_one(), fr::zero(), fr::zero() }); +// plonk_composer.create_add_gate( +// { c_idx, g_idx, plonk_composer.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); +// plonk_composer.create_add_gate( +// { e_idx, f_idx, plonk_composer.zero_idx, fr::one(), -fr::one(), fr::zero(), fr::zero() }); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, bad_tag_permutation) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// fr a = fr::random_element(); +// { +// fr b = -a; + +// auto a_idx = honk_composer.add_variable(a); +// auto b_idx = honk_composer.add_variable(b); +// auto c_idx = honk_composer.add_variable(b); +// auto d_idx = honk_composer.add_variable(a + 1); + +// honk_composer.create_add_gate({ a_idx, b_idx, honk_composer.get_zero_idx(), 1, 1, 0, 0 }); +// honk_composer.create_add_gate({ c_idx, d_idx, honk_composer.get_zero_idx(), 1, 1, 0, -1 }); + +// honk_composer.create_tag(1, 2); +// honk_composer.create_tag(2, 1); + +// honk_composer.assign_tag(a_idx, 1); +// honk_composer.assign_tag(b_idx, 1); +// honk_composer.assign_tag(c_idx, 2); +// honk_composer.assign_tag(d_idx, 2); +// } +// { +// fr b = -a; + +// auto a_idx = plonk_composer.add_variable(a); +// auto b_idx = plonk_composer.add_variable(b); +// auto c_idx = plonk_composer.add_variable(b); +// auto d_idx = plonk_composer.add_variable(a + 1); + +// plonk_composer.create_add_gate({ a_idx, b_idx, plonk_composer.zero_idx, 1, 1, 0, 0 }); +// plonk_composer.create_add_gate({ c_idx, d_idx, plonk_composer.zero_idx, 1, 1, 0, -1 }); + +// plonk_composer.create_tag(1, 2); +// plonk_composer.create_tag(2, 1); + +// plonk_composer.assign_tag(a_idx, 1); +// plonk_composer.assign_tag(b_idx, 1); +// plonk_composer.assign_tag(c_idx, 2); +// plonk_composer.assign_tag(d_idx, 2); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, sort_widget) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// fr a = fr::one(); +// fr b = fr(2); +// fr c = fr(3); +// fr d = fr(4); + +// auto a_idx = honk_composer.add_variable(a); +// auto b_idx = honk_composer.add_variable(b); +// auto c_idx = honk_composer.add_variable(c); +// auto d_idx = honk_composer.add_variable(d); +// honk_composer.create_sort_constraint({ a_idx, b_idx, c_idx, d_idx }); +// } +// { +// fr a = fr::one(); +// fr b = fr(2); +// fr c = fr(3); +// fr d = fr(4); + +// auto a_idx = plonk_composer.add_variable(a); +// auto b_idx = plonk_composer.add_variable(b); +// auto c_idx = plonk_composer.add_variable(c); +// auto d_idx = plonk_composer.add_variable(d); +// plonk_composer.create_sort_constraint({ a_idx, b_idx, c_idx, d_idx }); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, sort_with_edges_gate) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// auto idx = add_variables(honk_composer, { 1, 2, 5, 6, 7, 10, 11, 13, 16, 17, 20, 22, 22, 25, +// 26, 29, 29, 32, 32, 33, 35, 38, 39, 39, 42, 42, 43, 45 }); + +// honk_composer.create_sort_constraint_with_edges(idx, 1, 29); +// } +// { +// auto idx = add_variables(plonk_composer, { 1, 2, 5, 6, 7, 10, 11, 13, 16, 17, 20, 22, 22, 25, +// 26, 29, 29, 32, 32, 33, 35, 38, 39, 39, 42, 42, 43, 45 }); + +// plonk_composer.create_sort_constraint_with_edges(idx, 1, 29); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, range_constraint) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// auto indices = +// add_variables(honk_composer, { 1, 0, 3, 80, 5, 6, 29, 8, 15, 11, 32, 21, 42, 79, 16, 10, 3, 26, 13, 14 +// }); +// for (size_t i = 0; i < indices.size(); i++) { +// honk_composer.create_new_range_constraint(indices[i], 79); +// } +// honk_composer.create_dummy_constraints(indices); +// } +// { +// auto indices = +// add_variables(plonk_composer, { 1, 0, 3, 80, 5, 6, 29, 8, 15, 11, 32, 21, 42, 79, 16, 10, 3, 26, 13, 14 +// }); +// for (size_t i = 0; i < indices.size(); i++) { +// plonk_composer.create_new_range_constraint(indices[i], 79); +// } +// plonk_composer.create_dummy_constraints(indices); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, range_with_gates) +// { + +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// auto idx = add_variables(honk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); +// for (size_t i = 0; i < idx.size(); i++) { +// honk_composer.create_new_range_constraint(idx[i], 8); +// } + +// honk_composer.create_add_gate( +// { idx[0], idx[1], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); +// honk_composer.create_add_gate( +// { idx[2], idx[3], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); +// honk_composer.create_add_gate( +// { idx[4], idx[5], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); +// honk_composer.create_add_gate( +// { idx[6], idx[7], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); +// } +// { +// auto idx = add_variables(plonk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); +// for (size_t i = 0; i < idx.size(); i++) { +// plonk_composer.create_new_range_constraint(idx[i], 8); +// } + +// plonk_composer.create_add_gate( +// { idx[0], idx[1], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); +// plonk_composer.create_add_gate( +// { idx[2], idx[3], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); +// plonk_composer.create_add_gate( +// { idx[4], idx[5], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); +// plonk_composer.create_add_gate( +// { idx[6], idx[7], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, range_with_gates_where_range_is_not_a_power_of_two) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// auto idx = add_variables(honk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); +// for (size_t i = 0; i < idx.size(); i++) { +// honk_composer.create_new_range_constraint(idx[i], 12); +// } + +// honk_composer.create_add_gate( +// { idx[0], idx[1], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -3 }); +// honk_composer.create_add_gate( +// { idx[2], idx[3], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -7 }); +// honk_composer.create_add_gate( +// { idx[4], idx[5], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -11 }); +// honk_composer.create_add_gate( +// { idx[6], idx[7], honk_composer.get_zero_idx(), fr::one(), fr::one(), fr::zero(), -15 }); +// } +// { +// auto idx = add_variables(plonk_composer, { 1, 2, 3, 4, 5, 6, 7, 8 }); +// for (size_t i = 0; i < idx.size(); i++) { +// plonk_composer.create_new_range_constraint(idx[i], 12); +// } + +// plonk_composer.create_add_gate( +// { idx[0], idx[1], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -3 }); +// plonk_composer.create_add_gate( +// { idx[2], idx[3], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -7 }); +// plonk_composer.create_add_gate( +// { idx[4], idx[5], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -11 }); +// plonk_composer.create_add_gate( +// { idx[6], idx[7], plonk_composer.zero_idx, fr::one(), fr::one(), fr::zero(), -15 }); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, sort_widget_complex) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// { +// std::vector a = { 1, 3, 4, 7, 7, 8, 16, 14, 15, 15, 18, 19, 21, 21, 24, 25, 26, 27, 30, 32 }; +// std::vector ind; +// for (size_t i = 0; i < a.size(); i++) +// ind.emplace_back(honk_composer.add_variable(a[i])); +// honk_composer.create_sort_constraint(ind); +// } +// { +// std::vector a = { 1, 3, 4, 7, 7, 8, 16, 14, 15, 15, 18, 19, 21, 21, 24, 25, 26, 27, 30, 32 }; +// std::vector ind; +// for (size_t i = 0; i < a.size(); i++) +// ind.emplace_back(plonk_composer.add_variable(a[i])); +// plonk_composer.create_sort_constraint(ind); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, composed_range_constraint) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// auto c = fr::random_element(); +// { +// auto d = uint256_t(c).slice(0, 133); +// auto e = fr(d); +// auto a_idx = honk_composer.add_variable(fr(e)); +// honk_composer.create_add_gate( +// { a_idx, honk_composer.get_zero_idx(), honk_composer.get_zero_idx(), 1, 0, 0, -fr(e) }); +// honk_composer.decompose_into_default_range(a_idx, 134); +// } +// { +// auto d = uint256_t(c).slice(0, 133); +// auto e = fr(d); +// auto a_idx = plonk_composer.add_variable(fr(e)); +// plonk_composer.create_add_gate({ a_idx, plonk_composer.zero_idx, plonk_composer.zero_idx, 1, 0, 0, -fr(e) }); +// plonk_composer.decompose_into_default_range(a_idx, 134); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, non_native_field_multiplication) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// fq a = fq::random_element(); +// fq b = fq::random_element(); +// { +// uint256_t modulus = fq::modulus; + +// uint1024_t a_big = uint512_t(uint256_t(a)); +// uint1024_t b_big = uint512_t(uint256_t(b)); +// uint1024_t p_big = uint512_t(uint256_t(modulus)); + +// uint1024_t q_big = (a_big * b_big) / p_big; +// uint1024_t r_big = (a_big * b_big) % p_big; + +// uint256_t q(q_big.lo.lo); +// uint256_t r(r_big.lo.lo); + +// const auto split_into_limbs = [&](const uint512_t& input) { +// constexpr size_t NUM_BITS = 68; +// std::array limbs; +// limbs[0] = input.slice(0, NUM_BITS).lo; +// limbs[1] = input.slice(NUM_BITS * 1, NUM_BITS * 2).lo; +// limbs[2] = input.slice(NUM_BITS * 2, NUM_BITS * 3).lo; +// limbs[3] = input.slice(NUM_BITS * 3, NUM_BITS * 4).lo; +// limbs[4] = fr(input.lo); +// return limbs; +// }; + +// const auto get_limb_witness_indices = [&](const std::array& limbs) { +// std::array limb_indices; +// limb_indices[0] = honk_composer.add_variable(limbs[0]); +// limb_indices[1] = honk_composer.add_variable(limbs[1]); +// limb_indices[2] = honk_composer.add_variable(limbs[2]); +// limb_indices[3] = honk_composer.add_variable(limbs[3]); +// limb_indices[4] = honk_composer.add_variable(limbs[4]); +// return limb_indices; +// }; +// const uint512_t BINARY_BASIS_MODULUS = uint512_t(1) << (68 * 4); +// auto modulus_limbs = split_into_limbs(BINARY_BASIS_MODULUS - uint512_t(modulus)); + +// const auto a_indices = get_limb_witness_indices(split_into_limbs(uint256_t(a))); +// const auto b_indices = get_limb_witness_indices(split_into_limbs(uint256_t(b))); +// const auto q_indices = get_limb_witness_indices(split_into_limbs(uint256_t(q))); +// const auto r_indices = get_limb_witness_indices(split_into_limbs(uint256_t(r))); + +// proof_system::non_native_field_witnesses inputs{ +// a_indices, b_indices, q_indices, r_indices, modulus_limbs, fr(uint256_t(modulus)), +// }; +// const auto [lo_1_idx, hi_1_idx] = honk_composer.evaluate_non_native_field_multiplication(inputs); +// honk_composer.range_constrain_two_limbs(lo_1_idx, hi_1_idx, 70, 70); +// } +// { +// uint256_t modulus = fq::modulus; + +// uint1024_t a_big = uint512_t(uint256_t(a)); +// uint1024_t b_big = uint512_t(uint256_t(b)); +// uint1024_t p_big = uint512_t(uint256_t(modulus)); + +// uint1024_t q_big = (a_big * b_big) / p_big; +// uint1024_t r_big = (a_big * b_big) % p_big; + +// uint256_t q(q_big.lo.lo); +// uint256_t r(r_big.lo.lo); + +// const auto split_into_limbs = [&](const uint512_t& input) { +// constexpr size_t NUM_BITS = 68; +// std::array limbs; +// limbs[0] = input.slice(0, NUM_BITS).lo; +// limbs[1] = input.slice(NUM_BITS * 1, NUM_BITS * 2).lo; +// limbs[2] = input.slice(NUM_BITS * 2, NUM_BITS * 3).lo; +// limbs[3] = input.slice(NUM_BITS * 3, NUM_BITS * 4).lo; +// limbs[4] = fr(input.lo); +// return limbs; +// }; + +// const auto get_limb_witness_indices = [&](const std::array& limbs) { +// std::array limb_indices; +// limb_indices[0] = plonk_composer.add_variable(limbs[0]); +// limb_indices[1] = plonk_composer.add_variable(limbs[1]); +// limb_indices[2] = plonk_composer.add_variable(limbs[2]); +// limb_indices[3] = plonk_composer.add_variable(limbs[3]); +// limb_indices[4] = plonk_composer.add_variable(limbs[4]); +// return limb_indices; +// }; +// const uint512_t BINARY_BASIS_MODULUS = uint512_t(1) << (68 * 4); +// auto modulus_limbs = split_into_limbs(BINARY_BASIS_MODULUS - uint512_t(modulus)); + +// const auto a_indices = get_limb_witness_indices(split_into_limbs(uint256_t(a))); +// const auto b_indices = get_limb_witness_indices(split_into_limbs(uint256_t(b))); +// const auto q_indices = get_limb_witness_indices(split_into_limbs(uint256_t(q))); +// const auto r_indices = get_limb_witness_indices(split_into_limbs(uint256_t(r))); + +// proof_system::plonk::UltraComposer::non_native_field_witnesses inputs{ +// a_indices, b_indices, q_indices, r_indices, modulus_limbs, fr(uint256_t(modulus)), +// }; +// const auto [lo_1_idx, hi_1_idx] = plonk_composer.evaluate_non_native_field_multiplication(inputs); +// plonk_composer.range_constrain_two_limbs(lo_1_idx, hi_1_idx, 70, 70); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, rom) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// auto a = fr::random_element(); +// auto b = fr::random_element(); +// auto c = fr::random_element(); +// auto d = fr::random_element(); +// auto e = fr::random_element(); +// auto f = fr::random_element(); +// auto g = fr::random_element(); +// auto h = fr::random_element(); +// { +// uint32_t rom_values[8]{ +// honk_composer.add_variable(a), honk_composer.add_variable(b), honk_composer.add_variable(c), +// honk_composer.add_variable(d), honk_composer.add_variable(e), honk_composer.add_variable(f), +// honk_composer.add_variable(g), honk_composer.add_variable(h), +// }; + +// size_t rom_id = honk_composer.create_ROM_array(8); + +// for (size_t i = 0; i < 8; ++i) { +// honk_composer.set_ROM_element(rom_id, i, rom_values[i]); +// } + +// uint32_t a_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(5)); +// EXPECT_EQ(a_idx != rom_values[5], true); +// uint32_t b_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(4)); +// uint32_t c_idx = honk_composer.read_ROM_array(rom_id, honk_composer.add_variable(1)); + +// const auto d_value = +// honk_composer.get_variable(a_idx) + honk_composer.get_variable(b_idx) + +// honk_composer.get_variable(c_idx); +// uint32_t d_idx = honk_composer.add_variable(d_value); + +// honk_composer.create_big_add_gate({ +// a_idx, +// b_idx, +// c_idx, +// d_idx, +// 1, +// 1, +// 1, +// -1, +// 0, +// }); +// } +// { +// uint32_t rom_values[8]{ +// plonk_composer.add_variable(a), plonk_composer.add_variable(b), plonk_composer.add_variable(c), +// plonk_composer.add_variable(d), plonk_composer.add_variable(e), plonk_composer.add_variable(f), +// plonk_composer.add_variable(g), plonk_composer.add_variable(h), +// }; + +// size_t rom_id = plonk_composer.create_ROM_array(8); + +// for (size_t i = 0; i < 8; ++i) { +// plonk_composer.set_ROM_element(rom_id, i, rom_values[i]); +// } + +// uint32_t a_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(5)); +// EXPECT_EQ(a_idx != rom_values[5], true); +// uint32_t b_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(4)); +// uint32_t c_idx = plonk_composer.read_ROM_array(rom_id, plonk_composer.add_variable(1)); + +// const auto d_value = plonk_composer.get_variable(a_idx) + plonk_composer.get_variable(b_idx) + +// plonk_composer.get_variable(c_idx); +// uint32_t d_idx = plonk_composer.add_variable(d_value); + +// plonk_composer.create_big_add_gate({ +// a_idx, +// b_idx, +// c_idx, +// d_idx, +// 1, +// 1, +// 1, +// -1, +// 0, +// }); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// check_consistency(honk_prover, plonk_prover); +// verify_consistency(honk_prover, plonk_prover); +// } + +// TEST(UltraHonkComposer, ram) +// { +// auto honk_composer = UltraHonkComposer(); +// auto plonk_composer = proof_system::plonk::UltraComposer(); + +// auto a = fr::random_element(); +// auto b = fr::random_element(); +// auto c = fr::random_element(); +// auto d = fr::random_element(); +// auto e = fr::random_element(); +// auto f = fr::random_element(); +// auto g = fr::random_element(); +// auto h = fr::random_element(); +// { +// uint32_t ram_values[8]{ +// honk_composer.add_variable(a), honk_composer.add_variable(b), honk_composer.add_variable(c), +// honk_composer.add_variable(d), honk_composer.add_variable(e), honk_composer.add_variable(f), +// honk_composer.add_variable(g), honk_composer.add_variable(h), +// }; + +// size_t ram_id = honk_composer.create_RAM_array(8); + +// for (size_t i = 0; i < 8; ++i) { +// honk_composer.init_RAM_element(ram_id, i, ram_values[i]); +// } + +// uint32_t a_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(5)); +// EXPECT_EQ(a_idx != ram_values[5], true); + +// uint32_t b_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(4)); +// uint32_t c_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(1)); + +// honk_composer.write_RAM_array(ram_id, honk_composer.add_variable(4), honk_composer.add_variable(500)); +// uint32_t d_idx = honk_composer.read_RAM_array(ram_id, honk_composer.add_variable(4)); + +// EXPECT_EQ(honk_composer.get_variable(d_idx), 500); + +// // ensure these vars get used in another arithmetic gate +// const auto e_value = honk_composer.get_variable(a_idx) + honk_composer.get_variable(b_idx) + +// honk_composer.get_variable(c_idx) + honk_composer.get_variable(d_idx); +// uint32_t e_idx = honk_composer.add_variable(e_value); + +// honk_composer.create_big_add_gate( +// { +// a_idx, +// b_idx, +// c_idx, +// d_idx, +// -1, +// -1, +// -1, +// -1, +// 0, +// }, +// true); +// honk_composer.create_big_add_gate( +// { +// honk_composer.get_zero_idx(), +// honk_composer.get_zero_idx(), +// honk_composer.get_zero_idx(), +// e_idx, +// 0, +// 0, +// 0, +// 0, +// 0, +// }, +// false); +// } +// { +// uint32_t ram_values[8]{ +// plonk_composer.add_variable(a), plonk_composer.add_variable(b), plonk_composer.add_variable(c), +// plonk_composer.add_variable(d), plonk_composer.add_variable(e), plonk_composer.add_variable(f), +// plonk_composer.add_variable(g), plonk_composer.add_variable(h), +// }; + +// size_t ram_id = plonk_composer.create_RAM_array(8); + +// for (size_t i = 0; i < 8; ++i) { +// plonk_composer.init_RAM_element(ram_id, i, ram_values[i]); +// } + +// uint32_t a_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(5)); +// EXPECT_EQ(a_idx != ram_values[5], true); + +// uint32_t b_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(4)); +// uint32_t c_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(1)); + +// plonk_composer.write_RAM_array(ram_id, plonk_composer.add_variable(4), plonk_composer.add_variable(500)); +// uint32_t d_idx = plonk_composer.read_RAM_array(ram_id, plonk_composer.add_variable(4)); + +// EXPECT_EQ(plonk_composer.get_variable(d_idx), 500); + +// // ensure these vars get used in another arithmetic gate +// const auto e_value = plonk_composer.get_variable(a_idx) + plonk_composer.get_variable(b_idx) + +// plonk_composer.get_variable(c_idx) + plonk_composer.get_variable(d_idx); +// uint32_t e_idx = plonk_composer.add_variable(e_value); + +// plonk_composer.create_big_add_gate( +// { +// a_idx, +// b_idx, +// c_idx, +// d_idx, +// -1, +// -1, +// -1, +// -1, +// 0, +// }, +// true); +// plonk_composer.create_big_add_gate( +// { +// plonk_composer.zero_idx, +// plonk_composer.zero_idx, +// plonk_composer.zero_idx, +// e_idx, +// 0, +// 0, +// 0, +// 0, +// 0, +// }, +// false); +// } + +// auto honk_prover = honk_composer.create_prover(); +// auto plonk_prover = plonk_composer.create_prover(); + +// verify_consistency(honk_prover, plonk_prover); +// } + +// } // namespace test_ultra_honk_composer diff --git a/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp b/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp index 769e744a6d..573d237355 100644 --- a/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp +++ b/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp @@ -32,12 +32,12 @@ namespace proof_system::honk { * * @tparam settings Settings class. * */ -template -UltraHonkProver::UltraHonkProver(std::vector&& wire_polys, - std::shared_ptr input_key) +template +UltraHonkProver::UltraHonkProver(std::vector&& wire_polys, + std::shared_ptr input_key) : wire_polynomials(wire_polys) , key(input_key) - , queue(key, transcript) + , queue(input_key->circuit_size, transcript) {} template plonk::proof& UltraHonkProver::export_proof() @@ -51,6 +51,6 @@ template plonk::proof& UltraHonkProver::construct_ return export_proof(); } -template class UltraHonkProver; +template class UltraHonkProver; } // namespace proof_system::honk diff --git a/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp b/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp index b645e5863f..e8743e9c3b 100644 --- a/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp +++ b/cpp/src/barretenberg/honk/proof_system/ultra_prover.hpp @@ -24,31 +24,32 @@ #include #include "barretenberg/honk/pcs/claim.hpp" #include "barretenberg/honk/proof_system/prover_library.hpp" +#include "barretenberg/proof_system/flavor/flavor.hpp" namespace proof_system::honk { // TODO(luke): The naming here is awkward. The Standard Honk prover is called "Prover" and aliased as StandardProver. To // be consistent with that convention outside of the prover class itself, I've called this class UltraHonkProver and use // the alias UltraProver externally. Resolve. -template class UltraHonkProver { +template class UltraHonkProver { - using Fr = barretenberg::fr; - using Polynomial = barretenberg::Polynomial; - using Commitment = barretenberg::g1::affine_element; - using POLYNOMIAL = proof_system::honk::StandardArithmetization::POLYNOMIAL; + using FF = typename Flavor::FF; + using PCSParams = typename Flavor::PCSParams; + using ProvingKey = typename Flavor::ProvingKey; + using Polynomial = typename Flavor::Polynomial; public: UltraHonkProver(std::vector&& wire_polys, - std::shared_ptr input_key = nullptr); + std::shared_ptr input_key = nullptr); plonk::proof& export_proof(); plonk::proof& construct_proof(); - ProverTranscript transcript; + ProverTranscript transcript; std::vector wire_polynomials; - std::shared_ptr key; + std::shared_ptr key; work_queue queue; @@ -56,8 +57,8 @@ template class UltraHonkProver { plonk::proof proof; }; -extern template class UltraHonkProver; +extern template class UltraHonkProver; -using UltraProver = UltraHonkProver; +using UltraProver = UltraHonkProver; } // namespace proof_system::honk diff --git a/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp b/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp index f5556a31da..932e953557 100644 --- a/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp +++ b/cpp/src/barretenberg/honk/sumcheck/relations/relation_correctness.test.cpp @@ -11,6 +11,7 @@ #include "barretenberg/honk/sumcheck/relations/grand_product_computation_relation.hpp" #include "barretenberg/honk/sumcheck/relations/grand_product_initialization_relation.hpp" #include "barretenberg/honk/utils/public_inputs.hpp" +#include "barretenberg/proof_system/flavor/flavor.hpp" #include @@ -32,7 +33,7 @@ TEST(RelationCorrectness, StandardRelationCorrectness) { // Create a composer and a dummy circuit with a few gates auto composer = StandardHonkComposer(); - static const size_t num_wires = StandardHonkComposer::num_wires; + // static const size_t num_wires = StandardHonkComposer::num_wires; fr a = fr::one(); // Using the public variable to check that public_input_delta is computed and added to the relation correctly uint32_t a_idx = composer.add_public_variable(a); @@ -66,8 +67,8 @@ TEST(RelationCorrectness, StandardRelationCorrectness) constexpr size_t num_polynomials = proof_system::honk::StandardArithmetization::NUM_POLYNOMIALS; // Compute grand product polynomial - polynomial z_perm_poly = - prover_library::compute_permutation_grand_product(prover.key, prover.wire_polynomials, beta, gamma); + polynomial z_perm_poly = prover_library::compute_permutation_grand_product( + prover.key, prover.wire_polynomials, beta, gamma); // Create an array of spans to the underlying polynomials to more easily // get the transposition. @@ -139,7 +140,7 @@ TEST(RelationCorrectness, UltraRelationCorrectness) { // Create a composer and a dummy circuit with a few gates auto composer = UltraHonkComposer(); - static const size_t num_wires = 4; + // static const size_t num_wires = 4; fr a = fr::one(); // Using the public variable to check that public_input_delta is computed and added to the relation correctly // TODO(luke): add method "add_public_variable" to UH composer @@ -177,8 +178,8 @@ TEST(RelationCorrectness, UltraRelationCorrectness) constexpr size_t num_polynomials = proof_system::honk::UltraArithmetization::COUNT; // Compute grand product polynomial - auto z_perm_poly = - prover_library::compute_permutation_grand_product(prover.key, prover.wire_polynomials, beta, gamma); + auto z_perm_poly = prover_library::compute_permutation_grand_product( + prover.key, prover.wire_polynomials, beta, gamma); // Create an array of spans to the underlying polynomials to more easily // get the transposition. diff --git a/cpp/src/barretenberg/proof_system/composer/permutation_helper.hpp b/cpp/src/barretenberg/proof_system/composer/permutation_helper.hpp index 20ab2d4b66..d49ec2cc04 100644 --- a/cpp/src/barretenberg/proof_system/composer/permutation_helper.hpp +++ b/cpp/src/barretenberg/proof_system/composer/permutation_helper.hpp @@ -229,15 +229,10 @@ PermutationMapping compute_permutation_mapping( * @param permutation_mappings A table with information about permuting each element * @param key Pointer to the proving key */ -// MERGECONFLICT -// template -// void compute_honk_style_permutation_lagrange_polynomials_from_mapping( -// std::string label, -// std::array, program_width>& permutation_mappings, -// plonk::proving_key* key) template -void compute_honk_style_sigma_lagrange_polynomials_from_mapping( - std::array, Flavor::num_wires>& sigma_mappings, +void compute_honk_style_permutation_lagrange_polynomials_from_mapping( + std::string label, + std::array, Flavor::num_wires>& permutation_mappings, typename Flavor::ProvingKey* proving_key) { const size_t num_gates = proving_key->circuit_size; @@ -245,11 +240,14 @@ void compute_honk_style_sigma_lagrange_polynomials_from_mapping( std::array permutation_poly; // sigma or ID poly size_t wire_index = 0; - for (auto& sigma_polynomial : proving_key->get_sigma_polynomials()) { + for (auto& current_permutation_poly : proving_key->get_sigma_polynomials()) { + // permutation_poly[wire_index] = barretenberg::polynomial(num_gates); + // auto& current_permutation_poly = permutation_poly[wire_index]; + auto new_poly = barretenberg::polynomial(num_gates); // WORKTODO - current_sigma_polynomial = new_poly; + current_permutation_poly = new_poly; ITERATE_OVER_DOMAIN_START(proving_key->evaluation_domain); - const auto& current_mapping = sigma_mappings[wire_index][i]; + const auto& current_mapping = permutation_mappings[wire_index][i]; if (current_mapping.is_public_input) { // We intentionally want to break the cycles of the public input variables. // During the witness generation, the left and right wire polynomials at index i contain the i-th public @@ -263,7 +261,7 @@ void compute_honk_style_sigma_lagrange_polynomials_from_mapping( -barretenberg::fr(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 * program_width + current_mapping.row_index; + 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 @@ -272,14 +270,15 @@ void compute_honk_style_sigma_lagrange_polynomials_from_mapping( } ITERATE_OVER_DOMAIN_END; } + static_cast(label); // MERGECONFLICT // // Save to polynomial cache - // for (size_t j = 0; j < program_width; j++) { + // for (size_t j = 0; j < Flavor::num_wires; j++) { // std::string index = std::to_string(j + 1); // key->polynomial_store.put(label + "_" + index + "_lagrange", std::move(permutation_poly[j])); - wire_index++; - } + wire_index++; } +} // namespace /** * Compute sigma permutation polynomial in lagrange base @@ -455,7 +454,7 @@ void compute_standard_honk_sigma_permutations(const typename Flavor::CircuitCons // Compute the permutation table specifying which element becomes which auto mapping = compute_permutation_mapping(circuit_constructor, proving_key); // Compute Honk-style sigma polynomial from the permutation table - compute_honk_style_permutation_lagrange_polynomials_from_mapping(mapping.sigmas, proving_key); + compute_honk_style_permutation_lagrange_polynomials_from_mapping("sigma", mapping.sigmas, proving_key); } /** @@ -519,24 +518,24 @@ void compute_plonk_generalized_sigma_permutations(const typename Flavor::Circuit compute_monomial_and_coset_fft_polynomials_from_lagrange("id", key); } -// MERGECONFLICT what is this? /** * @brief Compute generalized permutation sigmas and ids for ultra plonk * * @tparam program_width * @tparam CircuitConstructor * @param circuit_constructor - * @param key + * @param proving_key * @return std::array, program_width> */ -template -void compute_honk_generalized_sigma_permutations(const CircuitConstructor& circuit_constructor, plonk::proving_key* key) +template +void compute_honk_generalized_sigma_permutations(const typename Flavor::CircuitConstructor& circuit_constructor, + typename Flavor::ProvingKey* proving_key) { - auto mapping = compute_permutation_mapping(circuit_constructor, key); + auto mapping = compute_permutation_mapping(circuit_constructor, proving_key); // Compute Honk-style sigma and ID polynomials from the corresponding mappings - compute_honk_style_permutation_lagrange_polynomials_from_mapping("sigma", mapping.sigmas, key); - compute_honk_style_permutation_lagrange_polynomials_from_mapping("id", mapping.ids, key); + compute_honk_style_permutation_lagrange_polynomials_from_mapping("sigma", mapping.sigmas, proving_key); + compute_honk_style_permutation_lagrange_polynomials_from_mapping("id", mapping.ids, proving_key); } } // namespace proof_system diff --git a/cpp/src/barretenberg/proof_system/composer/permutation_helper.test.cpp b/cpp/src/barretenberg/proof_system/composer/permutation_helper.test.cpp index ea3d845d17..68391ca760 100644 --- a/cpp/src/barretenberg/proof_system/composer/permutation_helper.test.cpp +++ b/cpp/src/barretenberg/proof_system/composer/permutation_helper.test.cpp @@ -70,7 +70,8 @@ TEST_F(PermutationHelperTests, ComputePermutationMapping) TEST_F(PermutationHelperTests, ComputeHonkStyleSigmaLagrangePolynomialsFromMapping) { auto mapping = compute_permutation_mapping(circuit_constructor, proving_key.get()); - compute_honk_style_sigma_lagrange_polynomials_from_mapping(mapping.sigmas, proving_key.get()); + compute_honk_style_permutation_lagrange_polynomials_from_mapping( + "label", mapping.sigmas, proving_key.get()); } TEST_F(PermutationHelperTests, ComputeStandardAuxPolynomials) diff --git a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp index 9d9d7b33d1..75cf80d42e 100644 --- a/cpp/src/barretenberg/proof_system/flavor/flavor.hpp +++ b/cpp/src/barretenberg/proof_system/flavor/flavor.hpp @@ -14,6 +14,7 @@ #include "barretenberg/proof_system/circuit_constructors/turbo_circuit_constructor.hpp" #include "barretenberg/proof_system/circuit_constructors/ultra_circuit_constructor.hpp" #include "barretenberg/srs/reference_string/reference_string.hpp" +#include "barretenberg/proof_system/polynomial_store/polynomial_store.hpp" // could be shared, but will it? namespace proof_system::honk::flavor { @@ -36,8 +37,8 @@ class Ultra { public: using CircuitConstructor = proof_system::UltraCircuitConstructor; static constexpr size_t num_wires = CircuitConstructor::num_wires; - // static constexpr size_t NUM_ALL_ENTITIES = 18; - // static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 13; + static constexpr size_t NUM_ALL_ENTITIES = 18; + static constexpr size_t NUM_PRECOMPUTED_ENTITIES = 13; using FF = barretenberg::fr; using Polynomial = barretenberg::Polynomial; @@ -47,14 +48,36 @@ class Ultra { using CommitmentView = G1; // TODO(Cody): make a pointer? using PCSParams = pcs::kzg::Params; - class ProvingKey { + template class PrecomputedData : public Data { + public: + T& q_m = std::get<0>(this->_data); + T& q_l = std::get<1>(this->_data); + T& q_r = std::get<2>(this->_data); + T& q_o = std::get<3>(this->_data); + T& q_c = std::get<4>(this->_data); + T& sigma_1 = std::get<5>(this->_data); + T& sigma_2 = std::get<6>(this->_data); + T& sigma_3 = std::get<7>(this->_data); + T& id_1 = std::get<8>(this->_data); + T& id_2 = std::get<9>(this->_data); + T& id_3 = std::get<10>(this->_data); + T& lagrange_first = std::get<11>(this->_data); + T& lagrange_last = std::get<12>(this->_data); // = LAGRANGE_N-1 whithout ZK, but can be less + + std::array, 5> get_selectors() { return { q_m, q_l, q_r, q_o, q_c }; }; + std::array, 3> get_sigma_polynomials() { return { sigma_1, sigma_2, sigma_3 }; }; + std::array, 3> get_id_polynomials() { return { id_1, id_2, id_3 }; }; + }; + + class ProvingKey : public PrecomputedData { public: const size_t circuit_size; - const size_t log_circuit_size; // TODO(Cody) + const size_t log_circuit_size; const size_t num_public_inputs; std::shared_ptr crs; EvaluationDomain evaluation_domain; - const ComposerType composer_type; // TODO(Cody): Get rid of this + const ComposerType composer_type; // TODO(Cody): Get rid of this + PolynomialStore polynomial_store; // TODO(Cody): Get rid of this ProvingKey(const size_t circuit_size, const size_t num_public_inputs, @@ -67,6 +90,73 @@ class Ultra { , evaluation_domain(circuit_size, circuit_size) , composer_type(composer_type){}; }; + + using VerificationKey = PrecomputedData; + + template struct AllData : Data { + public: + T& w_l = std::get<0>(this->_data); + T& w_r = std::get<1>(this->_data); + T& w_o = std::get<2>(this->_data); + T& z_perm = std::get<3>(this->_data); + T& z_perm_shift = std::get<4>(this->_data); + T& q_m = std::get<5>(this->_data); + T& q_l = std::get<6>(this->_data); + T& q_r = std::get<7>(this->_data); + T& q_o = std::get<8>(this->_data); + T& q_c = std::get<9>(this->_data); + T& sigma_1 = std::get<10>(this->_data); + T& sigma_2 = std::get<11>(this->_data); + T& sigma_3 = std::get<12>(this->_data); + T& id_1 = std::get<13>(this->_data); + T& id_2 = std::get<14>(this->_data); + T& id_3 = std::get<15>(this->_data); + T& lagrange_first = std::get<16>(this->_data); + T& lagrange_last = std::get<17>(this->_data); + + std::vector get_not_to_be_shifted() + { // ...z_perm_shift is in here? + return { w_l, w_r, w_o, z_perm_shift, q_m, q_l, q_r, q_o, q_c, sigma_1, sigma_2, sigma_3, + id_1, id_2, id_3, lagrange_first, lagrange_last }; + }; + + std::vector get_to_be_shifted() { return { z_perm }; }; + + // TODO(Cody): Look for a better solution? + std::vector get_in_order() + { + std::vector result{ get_not_to_be_shifted() }; + std::vector to_be_shifted{ get_to_be_shifted() }; + result.insert(result.end(), to_be_shifted.begin(), to_be_shifted.end()); + return result; + }; + }; + // These are classes are views of data living in different entities. They + // provide the utility of grouping these and ranged `for` loops over subsets. + using ProverPolynomials = AllData; + using VerifierCommitments = AllData; + + // TODO: Handle univariates right + using ExtendedEdges = AllData>; + + using PurportedEvaluations = AllData; + + class CommitmentLabels : public AllData { + public: + // this does away with the ENUM_TO_COMM array while preserving the + // transcript interface, which takes a string + // + // note: we could consider "enriching" the transcript interface to not use + // strings in the future, but I leave it this way for simplicity + std::string w_l = "w_l"; + std::string w_r = "w_r"; + std::string w_o = "w_o"; + std::string p_0 = "p_0"; + std::string p_1 = "p_1"; + std::string q_0 = "q_0"; + std::string q_1 = "q_1"; + std::string s_0 = "s_0"; + }; }; class Standard { @@ -111,11 +201,12 @@ class Standard { class ProvingKey : public PrecomputedData { public: const size_t circuit_size; - const size_t log_circuit_size; // TODO(Cody) + const size_t log_circuit_size; const size_t num_public_inputs; std::shared_ptr crs; EvaluationDomain evaluation_domain; - const ComposerType composer_type; // TODO(Cody): Get rid of this + const ComposerType composer_type; // TODO(Cody): Get rid of this + PolynomialStore polynomial_store; // TODO(Cody): Get rid of this ProvingKey(const size_t circuit_size, const size_t num_public_inputs,