From 72dab7bb1ea72ee96ff410e7bd86112aac7feb2f Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 17 Mar 2023 23:29:26 +0100 Subject: [PATCH] Zarcanum & assets balance proof refactoring: WIP --- src/crypto/crypto-sugar.h | 4 +- src/currency_core/currency_basic.h | 9 +++ src/currency_core/currency_format_utils.cpp | 3 + src/currency_core/currency_format_utils.h | 51 ++------------ .../currency_format_utils_transactions.h | 67 ++++++++++++++----- tests/core_tests/chaingen.cpp | 10 +-- tests/core_tests/chaingen.h | 2 +- tests/core_tests/pos_block_builder.cpp | 6 +- tests/core_tests/pos_block_builder.h | 2 +- .../core_concurrency_test.cpp | 10 ++- 10 files changed, 87 insertions(+), 77 deletions(-) diff --git a/src/crypto/crypto-sugar.h b/src/crypto/crypto-sugar.h index fd72537f..82b58777 100644 --- a/src/crypto/crypto-sugar.h +++ b/src/crypto/crypto-sugar.h @@ -491,9 +491,7 @@ namespace crypto // with x = X / Z, y = Y / Z, x * y = T / Z. ge_p3 m_p3; - point_t() - { - } + point_t() = default; explicit point_t(const crypto::public_key& pk) // can throw std::runtime_error { diff --git a/src/currency_core/currency_basic.h b/src/currency_core/currency_basic.h index 291beb85..089e381a 100644 --- a/src/currency_core/currency_basic.h +++ b/src/currency_core/currency_basic.h @@ -1144,6 +1144,15 @@ namespace currency END_KV_SERIALIZE_MAP() }; + bool operator ==(const currency::transaction& a, const currency::transaction& b); + bool operator ==(const currency::block& a, const currency::block& b); + bool operator ==(const currency::extra_attachment_info& a, const currency::extra_attachment_info& b); + bool operator ==(const currency::NLSAG_sig& a, const currency::NLSAG_sig& b); + bool operator ==(const currency::void_sig& a, const currency::void_sig& b); + bool operator ==(const currency::ZC_sig& a, const currency::ZC_sig& b); + bool operator ==(const currency::zarcanum_sig& a, const currency::zarcanum_sig& b); + bool operator ==(const currency::ref_by_id& a, const currency::ref_by_id& b); + } // namespace currency POD_MAKE_HASHABLE(currency, account_public_address); diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 916e3466..350d6755 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -4162,18 +4162,21 @@ namespace currency bool operator ==(const currency::void_sig& a, const currency::void_sig& b) { //@#@ + ASSERT_MES_AND_THROW("not implemented yet"); return false; } //-------------------------------------------------------------------------------- bool operator ==(const currency::ZC_sig& a, const currency::ZC_sig& b) { //@#@ TODO + ASSERT_MES_AND_THROW("not implemented yet"); return false; } //-------------------------------------------------------------------------------- bool operator ==(const currency::zarcanum_sig& a, const currency::zarcanum_sig& b) { //@#@ TODO + ASSERT_MES_AND_THROW("not implemented yet"); return false; } //-------------------------------------------------------------------------------- diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 26a3fa98..bef31190 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -53,15 +53,6 @@ namespace currency { - bool operator ==(const currency::transaction& a, const currency::transaction& b); - bool operator ==(const currency::block& a, const currency::block& b); - bool operator ==(const currency::extra_attachment_info& a, const currency::extra_attachment_info& b); - bool operator ==(const currency::NLSAG_sig& a, const currency::NLSAG_sig& b); - bool operator ==(const currency::void_sig& a, const currency::void_sig& b); - bool operator ==(const currency::ZC_sig& a, const currency::ZC_sig& b); - bool operator ==(const currency::zarcanum_sig& a, const currency::zarcanum_sig& b); - bool operator ==(const currency::ref_by_id& a, const currency::ref_by_id& b); - typedef boost::multiprecision::uint128_t uint128_tl; @@ -250,12 +241,12 @@ namespace currency const account_public_address &stakeholder_address, transaction& tx, uint64_t tx_version, - const blobdata& extra_nonce = blobdata(), - size_t max_outs = CURRENCY_MINER_TX_MAX_OUTS, - bool pos = false, - const pos_entry& pe = pos_entry(), - outputs_generation_context* ogc_ptr = nullptr, - const keypair* tx_one_time_key_to_use = nullptr); + const blobdata& extra_nonce = blobdata(), + size_t max_outs = CURRENCY_MINER_TX_MAX_OUTS, + bool pos = false, + const pos_entry& pe = pos_entry(), + outputs_generation_context* ogc_ptr = nullptr, + const keypair* tx_one_time_key_to_use = nullptr); //--------------------------------------------------------------- uint64_t get_string_uint64_hash(const std::string& str); bool construct_tx_out(const tx_destination_entry& de, const crypto::secret_key& tx_sec_key, size_t output_index, transaction& tx, std::set& deriv_cache, const account_keys& self, crypto::scalar_t& asset_blinding_mask, crypto::scalar_t& amount_blinding_mask, crypto::point_t& blinded_asset_id, crypto::point_t& amount_commitment, finalized_tx& result, uint8_t tx_outs_attr = CURRENCY_TO_KEY_OUT_RELAXED); @@ -613,36 +604,6 @@ namespace currency return get_or_add_field_to_variant_vector(extra); } //--------------------------------------------------------------- - template - void update_or_add_field_to_extra(std::vector& variant_container, const variant_type_t& v) - { - for (auto& ev : variant_container) - { - if (ev.type() == typeid(variant_type_t)) - { - boost::get(ev) = v; - return; - } - } - variant_container.push_back(v); - } - //--------------------------------------------------------------- - template - void remove_field_of_type_from_extra(std::vector& variant_container) - { - for (size_t i = 0; i != variant_container.size();) - { - if (variant_container[i].type() == typeid(variant_type_t)) - { - variant_container.erase(variant_container.begin()+i); - } - else - { - i++; - } - } - } - //--------------------------------------------------------------- template bool get_payment_id_from_tx(const t_container& att, std::string& payment_id) { diff --git a/src/currency_core/currency_format_utils_transactions.h b/src/currency_core/currency_format_utils_transactions.h index af4597ed..bcd2af18 100644 --- a/src/currency_core/currency_format_utils_transactions.h +++ b/src/currency_core/currency_format_utils_transactions.h @@ -126,7 +126,37 @@ namespace currency FIELD(explicit_native_asset_id) END_SERIALIZE() }; - + //--------------------------------------------------------------- + template + void update_or_add_field_to_extra(std::vector& variant_container, const variant_type_t& v) + { + for (auto& ev : variant_container) + { + if (ev.type() == typeid(variant_type_t)) + { + boost::get(ev) = v; + return; + } + } + variant_container.push_back(v); + } + //--------------------------------------------------------------- + template + void remove_field_of_type_from_extra(std::vector& variant_container) + { + for (size_t i = 0; i != variant_container.size();) + { + if (variant_container[i].type() == typeid(variant_type_t)) + { + variant_container.erase(variant_container.begin()+i); + } + else + { + i++; + } + } + } + //--------------------------------------------------------------- template uint64_t get_tx_x_detail(const transaction& tx) { @@ -134,6 +164,7 @@ namespace currency get_type_in_variant_container(tx.extra, e); return e.v; } + //--------------------------------------------------------------- template void set_tx_x_detail(transaction& tx, uint64_t v) { @@ -141,7 +172,7 @@ namespace currency e.v = v; update_or_add_field_to_extra(tx.extra, e); } - + //--------------------------------------------------------------- uint64_t get_tx_unlock_time(const transaction& tx, uint64_t o_i); uint64_t get_tx_max_unlock_time(const transaction& tx); bool get_tx_max_min_unlock_time(const transaction& tx, uint64_t& max_unlock_time, uint64_t& min_unlock_time); @@ -224,22 +255,22 @@ namespace currency // consider redesign BEGIN_KV_SERIALIZE_MAP() - KV_SERIALIZE(asset_ids); - KV_SERIALIZE(blinded_asset_ids); - KV_SERIALIZE(amount_commitments); - KV_SERIALIZE(asset_id_blinding_masks); - KV_SERIALIZE(amounts); - KV_SERIALIZE(amount_blinding_masks); - KV_SERIALIZE(pseudo_out_amount_commitments_sum); - KV_SERIALIZE(pseudo_out_amount_blinding_masks_sum); - KV_SERIALIZE(real_in_asset_id_blinding_mask_x_amount_sum); - KV_SERIALIZE(amount_commitments_sum); - KV_SERIALIZE(amount_blinding_masks_sum); - KV_SERIALIZE(asset_id_blinding_mask_x_amount_sum); - KV_SERIALIZE(ao_asset_id); - KV_SERIALIZE(ao_asset_id_pt); - KV_SERIALIZE(ao_amount_commitment); - KV_SERIALIZE(ao_amount_blinding_mask); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(asset_ids); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(blinded_asset_ids); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(amount_commitments); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(asset_id_blinding_masks); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(amounts); + KV_SERIALIZE_CONTAINER_POD_AS_BLOB(amount_blinding_masks); + KV_SERIALIZE_POD_AS_HEX_STRING(pseudo_out_amount_commitments_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(pseudo_out_amount_blinding_masks_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(real_in_asset_id_blinding_mask_x_amount_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(amount_commitments_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(amount_blinding_masks_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(asset_id_blinding_mask_x_amount_sum); + KV_SERIALIZE_POD_AS_HEX_STRING(ao_asset_id); + KV_SERIALIZE_POD_AS_HEX_STRING(ao_asset_id_pt); + KV_SERIALIZE_POD_AS_HEX_STRING(ao_amount_commitment); + KV_SERIALIZE_POD_AS_HEX_STRING(ao_amount_blinding_mask); END_KV_SERIALIZE_MAP() }; diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index 4412213f..1c19c459 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -286,7 +286,7 @@ bool test_generator::construct_block(currency::block& blk, blk.miner_tx = AUTO_VAL_INIT(blk.miner_tx); size_t target_block_size = txs_size + 0; // zero means no cost for ordinary coinbase - crypto::scalar_t blinding_masks_sum = 0; + outputs_generation_context miner_tx_ogc{}; while (true) { r = construct_miner_tx(height, misc_utils::median(block_sizes), @@ -301,7 +301,7 @@ bool test_generator::construct_block(currency::block& blk, test_generator::get_test_gentime_settings().miner_tx_max_outs, static_cast(coin_stake_sources.size()), pe, - &blinding_masks_sum); + &miner_tx_ogc); CHECK_AND_ASSERT_MES(r, false, "construct_miner_tx failed"); size_t coinbase_size = get_object_blobsize(blk.miner_tx); @@ -342,7 +342,7 @@ bool test_generator::construct_block(currency::block& blk, else { //need to build pos block - r = sign_block(wallets[won_walled_index].mining_context, pe, *wallets[won_walled_index].wallet, blinding_masks_sum, blk); + r = sign_block(wallets[won_walled_index].mining_context, pe, *wallets[won_walled_index].wallet, miner_tx_ogc, blk); CHECK_AND_ASSERT_MES(r, false, "Failed to find_kernel_and_sign()"); } @@ -362,10 +362,10 @@ bool test_generator::construct_block(currency::block& blk, bool test_generator::sign_block(const tools::wallet2::mining_context& mining_context, const pos_entry& pe, const tools::wallet2& w, - const crypto::scalar_t& blinding_masks_sum, + const outputs_generation_context& miner_tx_ogc, currency::block& b) { - bool r = w.prepare_and_sign_pos_block(mining_context, b, pe, blinding_masks_sum); + bool r = w.prepare_and_sign_pos_block(mining_context, b, pe, miner_tx_ogc); CHECK_AND_ASSERT_MES(r, false, "prepare_and_sign_pos_block failed"); return true; } diff --git a/tests/core_tests/chaingen.h b/tests/core_tests/chaingen.h index 00b5942d..d8905f6c 100644 --- a/tests/core_tests/chaingen.h +++ b/tests/core_tests/chaingen.h @@ -467,7 +467,7 @@ public: bool sign_block(const tools::wallet2::mining_context& mining_context, const currency::pos_entry& pe, const tools::wallet2& w, - const crypto::scalar_t& blinding_masks_sum, + const currency::outputs_generation_context& miner_tx_ogc, currency::block& b); /*bool get_output_details_by_global_index(const test_generator::blockchain_vector& blck_chain, diff --git a/tests/core_tests/pos_block_builder.cpp b/tests/core_tests/pos_block_builder.cpp index 3b6713c1..900e0d84 100644 --- a/tests/core_tests/pos_block_builder.cpp +++ b/tests/core_tests/pos_block_builder.cpp @@ -168,7 +168,7 @@ void pos_block_builder::step4_generate_coinbase_tx(size_t median_size, // generate miner tx using incorrect current_block_size only for size estimation size_t estimated_block_size = m_txs_total_size; bool r = construct_miner_tx(m_height, median_size, already_generated_coins, estimated_block_size, m_total_fee, - reward_receiver_address, stakeholder_address, m_block.miner_tx, tx_version, extra_nonce, max_outs, true, pe, &m_blinding_masks_sum, tx_one_time_key_to_use); + reward_receiver_address, stakeholder_address, m_block.miner_tx, tx_version, extra_nonce, max_outs, true, pe, &m_miner_tx_ogc, tx_one_time_key_to_use); CHECK_AND_ASSERT_THROW_MES(r, "construct_miner_tx failed"); estimated_block_size = m_txs_total_size + get_object_blobsize(m_block.miner_tx); @@ -176,7 +176,7 @@ void pos_block_builder::step4_generate_coinbase_tx(size_t median_size, for (size_t try_count = 0; try_count != 10; ++try_count) { r = construct_miner_tx(m_height, median_size, already_generated_coins, estimated_block_size, m_total_fee, - reward_receiver_address, stakeholder_address, m_block.miner_tx, tx_version, extra_nonce, max_outs, true, pe, &m_blinding_masks_sum, tx_one_time_key_to_use); + reward_receiver_address, stakeholder_address, m_block.miner_tx, tx_version, extra_nonce, max_outs, true, pe, &m_miner_tx_ogc, tx_one_time_key_to_use); CHECK_AND_ASSERT_THROW_MES(r, "construct_homemade_pos_miner_tx failed"); cumulative_size = m_txs_total_size + get_object_blobsize(m_block.miner_tx); @@ -232,7 +232,7 @@ void pos_block_builder::step5_sign(const currency::tx_source_entry& se, const cu uint8_t err = 0; r = crypto::zarcanum_generate_proof(tx_hash_for_sig, m_context.kernel_hash, ring, m_context.last_pow_block_id_hashed, m_context.sk.kimage, - secret_x, m_context.secret_q, prepared_real_out_index, m_blinding_masks_sum, m_context.stake_amount, m_context.stake_out_blinding_mask, + secret_x, m_context.secret_q, prepared_real_out_index, -m_miner_tx_ogc.amount_blinding_masks_sum, m_context.stake_amount, m_context.stake_out_blinding_mask, static_cast(sig), &err); CHECK_AND_ASSERT_THROW_MES(r, "zarcanum_generate_proof failed, err: " << (int)err); } diff --git a/tests/core_tests/pos_block_builder.h b/tests/core_tests/pos_block_builder.h index 581f277f..1365827c 100644 --- a/tests/core_tests/pos_block_builder.h +++ b/tests/core_tests/pos_block_builder.h @@ -78,7 +78,7 @@ struct pos_block_builder size_t m_height = 0; size_t m_pos_stake_output_gindex = 0; //uint64_t m_pos_stake_amount = 0; - crypto::scalar_t m_blinding_masks_sum {}; // bliding masks of zc outputs of miner tx + currency::outputs_generation_context m_miner_tx_ogc {}; currency::pos_mining_context m_context {}; }; diff --git a/tests/functional_tests/core_concurrency_test.cpp b/tests/functional_tests/core_concurrency_test.cpp index f1133910..06224d93 100644 --- a/tests/functional_tests/core_concurrency_test.cpp +++ b/tests/functional_tests/core_concurrency_test.cpp @@ -65,7 +65,15 @@ bool create_block_template_manually(const currency::block& prev_block, boost::mu // make things really simple by assuming block size is less than CURRENCY_BLOCK_GRANTED_FULL_REWARD_ZONE size_t median_size = 0; - bool r = construct_miner_tx(get_block_height(prev_block) + 1, median_size, already_generated_coins, txs_size, fee, miner_addr, miner_addr, result.miner_tx, TRANSACTION_VERSION_PRE_HF4); + bool r = construct_miner_tx(get_block_height(prev_block) + 1, + median_size, + already_generated_coins, + txs_size, + fee, + miner_addr, + miner_addr, + result.miner_tx, + TRANSACTION_VERSION_PRE_HF4); CHECK_AND_ASSERT_MES(r, false, "construct_miner_tx failed"); size_t coinbase_size = get_object_blobsize(result.miner_tx);