From 96753bbc948e7df30fa09cc6536c35143e0b8558 Mon Sep 17 00:00:00 2001 From: sowle Date: Fri, 17 Mar 2023 23:59:21 +0100 Subject: [PATCH] proofs generation moved from construct_miner_tx to wallet2::prepare_and_sign_pos_block + improvements over generate_zc_outs_range_proof --- src/currency_core/currency_format_utils.cpp | 27 +++------------------ src/currency_core/currency_format_utils.h | 2 ++ src/wallet/wallet2.cpp | 23 ++++++++++++++++-- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 350d6755..6fbbaee0 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -77,9 +77,10 @@ namespace currency return true; } //-------------------------------------------------------------------------------- - bool generate_zc_outs_range_proof(const crypto::hash& context_hash, size_t out_index_start, size_t outs_count, const outputs_generation_context& outs_gen_context, + bool generate_zc_outs_range_proof(const crypto::hash& context_hash, size_t out_index_start, const outputs_generation_context& outs_gen_context, const std::vector& vouts, zc_outs_range_proof& result) { + size_t outs_count = outs_gen_context.amounts.size(); CHECK_AND_ASSERT_MES(outs_gen_context.check_sizes(outs_count), false, ""); CHECK_AND_ASSERT_MES(out_index_start + outs_count == vouts.size(), false, ""); @@ -372,28 +373,6 @@ namespace currency set_tx_unlock_time(tx, height + CURRENCY_MINED_MONEY_UNLOCK_WINDOW); } - // - // The tx prefix should be sealed by now, and the tx hash should be defined. - // Any changes made below should only affect the signatures/proofs and should not impact the prefix hash calculation. - // - - // TODO: @#@# move to prepare_and_sign_pos_block() - if (tx.version > TRANSACTION_VERSION_PRE_HF4) - { - crypto::hash tx_id = get_transaction_hash(tx); - - //add range proofs - currency::zc_outs_range_proof range_proofs = AUTO_VAL_INIT(range_proofs); - bool r = generate_zc_outs_range_proof(tx_id, 0, destinations.size(), outs_gen_context, tx.vout, range_proofs); - CHECK_AND_ASSERT_MES(r, false, "Failed to generate zc_outs_range_proof()"); - tx.proofs.emplace_back(std::move(range_proofs)); - - currency::zc_balance_proof balance_proof{}; - r = generate_tx_balance_proof(tx, tx_id, outs_gen_context, block_reward, balance_proof); - CHECK_AND_ASSERT_MES(r, false, "generate_tx_balance_proof failed"); - tx.proofs.emplace_back(std::move(balance_proof)); - } - if (ogc_ptr) *ogc_ptr = outs_gen_context; // TODO @#@# consider refactoring (a lot of copying) -- sowle @@ -2249,7 +2228,7 @@ namespace currency // add range proofs currency::zc_outs_range_proof range_proofs = AUTO_VAL_INIT(range_proofs); - r = generate_zc_outs_range_proof(tx_prefix_hash, range_proof_start_index, outputs_to_be_constructed, outs_gen_context, tx.vout, range_proofs); + r = generate_zc_outs_range_proof(tx_prefix_hash, range_proof_start_index, outs_gen_context, tx.vout, range_proofs); CHECK_AND_ASSERT_MES(r, false, "Failed to generate zc_outs_range_proof()"); tx.proofs.emplace_back(std::move(range_proofs)); diff --git a/src/currency_core/currency_format_utils.h b/src/currency_core/currency_format_utils.h index bef31190..492962c2 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -230,6 +230,8 @@ namespace currency bool verify_multiple_zc_outs_range_proofs(const std::vector& range_proofs); bool generate_tx_balance_proof(const transaction &tx, const crypto::hash& tx_id, const outputs_generation_context& ogc, uint64_t block_reward_for_miner_tx, currency::zc_balance_proof& proof); + bool generate_zc_outs_range_proof(const crypto::hash& context_hash, size_t out_index_start, const outputs_generation_context& outs_gen_context, + const std::vector& vouts, zc_outs_range_proof& result); bool check_tx_bare_balance(const transaction& tx, uint64_t additional_inputs_amount_and_fees_for_mining_tx = 0); bool check_tx_balance(const transaction& tx, const crypto::hash& tx_id, uint64_t additional_inputs_amount_and_fees_for_mining_tx = 0); bool validate_asset_operation(const transaction& tx, const crypto::hash& tx_id, const asset_descriptor_operation& ado, crypto::public_key& asset_id); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 1859aaba..7efaf452 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -3974,14 +3974,33 @@ bool wallet2::prepare_and_sign_pos_block(const mining_context& cxt, currency::bl } #endif - crypto::hash tx_hash_for_sig = get_block_hash(b); + crypto::hash hash_for_zarcanum_sig = get_block_hash(b); uint8_t err = 0; - r = crypto::zarcanum_generate_proof(tx_hash_for_sig, cxt.kernel_hash, ring, cxt.last_pow_block_id_hashed, cxt.sk.kimage, + r = crypto::zarcanum_generate_proof(hash_for_zarcanum_sig, cxt.kernel_hash, ring, cxt.last_pow_block_id_hashed, cxt.sk.kimage, secret_x, cxt.secret_q, secret_index, -miner_tx_ogc.amount_blinding_masks_sum, cxt.stake_amount, cxt.stake_out_blinding_mask, static_cast(sig), &err); WLT_CHECK_AND_ASSERT_MES(r, false, "zarcanum_generate_proof failed, err: " << (int)err); + // + // The miner tx prefix should be sealed by now, and the tx hash should be defined. + // Any changes made below should only affect the signatures/proofs and should not impact the prefix hash calculation. + // + crypto::hash miner_tx_id = get_transaction_hash(b.miner_tx); + + // proofs for miner_tx + currency::zc_outs_range_proof range_proofs = AUTO_VAL_INIT(range_proofs); + r = generate_zc_outs_range_proof(miner_tx_id, 0, miner_tx_ogc, b.miner_tx.vout, range_proofs); + CHECK_AND_ASSERT_MES(r, false, "Failed to generate zc_outs_range_proof()"); + b.miner_tx.proofs.emplace_back(std::move(range_proofs)); + + uint64_t block_reward = COIN; + + currency::zc_balance_proof balance_proof{}; + r = generate_tx_balance_proof(b.miner_tx, miner_tx_id, miner_tx_ogc, block_reward, balance_proof); + CHECK_AND_ASSERT_MES(r, false, "generate_tx_balance_proof failed"); + b.miner_tx.proofs.emplace_back(std::move(balance_proof)); + return true; } //------------------------------------------------------------------