From 2fcb663b616a0d6c95dd7eaed22154ff7aecbc70 Mon Sep 17 00:00:00 2001 From: sowle Date: Mon, 18 Mar 2024 20:43:30 +0100 Subject: [PATCH] difficulty scaling fixed --- src/crypto/zarcanum.cpp | 42 ++++++++++++++++++++---- src/currency_core/blockchain_storage.cpp | 3 -- src/currency_core/pos_mining.cpp | 2 +- src/wallet/wallet2.cpp | 6 ++++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/crypto/zarcanum.cpp b/src/crypto/zarcanum.cpp index e0b81f44..01451d3f 100644 --- a/src/crypto/zarcanum.cpp +++ b/src/crypto/zarcanum.cpp @@ -7,6 +7,7 @@ #include "epee/include/misc_log_ex.h" #include "zarcanum.h" #include "range_proofs.h" +#include "../currency_core/currency_config.h" #include "../currency_core/crypto_config.h" // TODO: move it to the crypto #include "../common/crypto_stream_operators.h" // TODO: move it to the crypto @@ -34,13 +35,13 @@ namespace crypto mp::uint256_t zarcanum_precalculate_l_div_z_D(const mp::uint128_t& pos_difficulty) { //LOG_PRINT_GREEN_L0(ENDL << "floor( l / (z * D) ) = " << c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty)); - return c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty); // == floor( l / (z * D) ) + return c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty / STAGENET_POS_DIFF_DIVISOR); // == floor( l / (z * D) ) } mp::uint256_t zarcanum_precalculate_z_l_div_z_D(const mp::uint128_t& pos_difficulty) { //LOG_PRINT_GREEN_L0(ENDL << "z * floor( l / (z * D) ) = " << c_zarcanum_z_coeff_mp * (c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty))); - return c_zarcanum_z_coeff_mp * (c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty)); // == z * floor( l / (z * D) ) + return c_zarcanum_z_coeff_mp * (c_scalar_L.as_boost_mp_type() / (c_zarcanum_z_coeff_mp * pos_difficulty / STAGENET_POS_DIFF_DIVISOR)); // == z * floor( l / (z * D) ) } bool zarcanum_check_main_pos_inequality(const hash& kernel_hash, const scalar_t& blinding_mask, const scalar_t& secret_q, @@ -50,11 +51,31 @@ namespace crypto lhs = lhs_s.as_boost_mp_type(); rhs = static_cast(z_l_div_z_D) * stake_amount; // == floor( l / (z * D) ) * z * a - //LOG_PRINT_GREEN_L0(ENDL << - // "z_l_div_z_D = " << z_l_div_z_D << ENDL << - // "stake_amount = " << stake_amount << ENDL << - // "lhs = " << lhs << ENDL << - // "rhs = " << rhs); + // estimate the stake to satisfy the equation + mp::uint256_t estimated_amount = lhs / z_l_div_z_D; + mp::uint256_t ratio = estimated_amount / stake_amount; + static mp::uint256_t min_ratio = std::numeric_limits::max(); + static uint64_t stake_amount_for_min = 0; + static size_t count = 0; + if (min_ratio > ratio) + { + min_ratio = ratio; + stake_amount_for_min = stake_amount; + } + + if (++count > 100000) + { + LOG_PRINT_GREEN("min amount satisfying staking (in coins): " << min_ratio * stake_amount_for_min / ((uint64_t)1000000000000) << ", corresp. stake: " << + stake_amount_for_min / 1000000000000ull << ", ratio is: " << min_ratio, LOG_LEVEL_0); + count = 0; + min_ratio = std::numeric_limits::max(); + } + + //LOG_PRINT_GREEN_L0("Zarcanum check main ineq:" << ENDL << + // " z_l_div_z_D = " << z_l_div_z_D << ENDL << + // " stake_amount = " << stake_amount << ENDL << + // " lhs = " << lhs << ENDL << + // " rhs = " << rhs); return lhs < rhs; // h * (f + q + f') mod l < floor( l / (z * D) ) * z * a } @@ -226,6 +247,13 @@ namespace crypto // make sure 0 < d <= l / floor(z * D) const mp::uint256_t l_div_z_D_mp = zarcanum_precalculate_l_div_z_D(pos_difficulty); const scalar_t l_div_z_D(l_div_z_D_mp); + if (!(sig.d < l_div_z_D)) + { + LOG_PRINT_RED(ENDL << + "D = 0x" << std::hex << pos_difficulty << ENDL << + "sig.d = 0x" << std::hex << sig.d.as_boost_mp_type() << ENDL << + "l_div_z_D = 0x" << std::hex << l_div_z_D_mp, LOG_LEVEL_0); + } CHECK_AND_FAIL_WITH_ERROR_IF_FALSE(!sig.d.is_zero() && sig.d < l_div_z_D, 2); const scalar_t dz = sig.d * c_zarcanum_z_coeff_s; diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 8ec5b403..17ed1fbd 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -5957,9 +5957,6 @@ bool blockchain_storage::validate_pos_block(const block& b, uint64_t split_height )const { - - basic_diff = basic_diff / STAGENET_POS_DIFF_DIVISOR; - bool is_pos = is_pos_block(b); CHECK_AND_ASSERT_MES(is_pos, false, "is_pos_block() returned false validate_pos_block()"); diff --git a/src/currency_core/pos_mining.cpp b/src/currency_core/pos_mining.cpp index 6d42d8e1..5bc1d973 100644 --- a/src/currency_core/pos_mining.cpp +++ b/src/currency_core/pos_mining.cpp @@ -13,7 +13,7 @@ namespace currency { void pos_mining_context::init(const wide_difficulty_type& pos_diff, const stake_modifier_type& sm, bool is_zarcanum) { - this->basic_diff = pos_diff / STAGENET_POS_DIFF_DIVISOR; + this->basic_diff = pos_diff; this->sk.stake_modifier = sm; this->zarcanum = is_zarcanum; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index b2af9d58..df5de5d7 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -4450,6 +4450,12 @@ bool wallet2::prepare_and_sign_pos_block(const mining_context& cxt, uint64_t ful static_cast(sig), &err); WLT_CHECK_AND_ASSERT_MES(r, false, "zarcanum_generate_proof failed, err: " << (int)err); + LOG_PRINT_GREEN("WLT: Zarcanum proof generated: " << ENDL << + " D = 0x" << std::hex << cxt.basic_diff << ENDL << + " sig.d = 0x" << sig.d.to_string_as_hex_number() << ENDL << + " floor(l / (z * D)) = 0x" << std::hex << cxt.z_l_div_z_D / crypto::c_zarcanum_z_coeff_mp << ENDL + , LOG_LEVEL_0); + // // 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.