From 1df36cb8415d9ba20b5551377d3f23ca846c74c9 Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 27 Apr 2021 05:14:11 +0300 Subject: [PATCH 1/6] crypto: fix some warnings --- src/CMakeLists.txt | 7 +++++++ src/crypto/hash.c | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0ab05c76..3f36b1d5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -108,6 +108,13 @@ if(NOT MSVC AND NOT APPLE AND NOT CLANG) # TODO(unassigned): do we really need target_compile_options(common PRIVATE -fno-var-tracking-assignments) endif() +# disable specific warning in order not to touch original code by D. J. Bernstein +if(MSVC) + set_source_files_properties("crypto/chacha8_stream.c" PROPERTIES COMPILE_FLAGS "/wd4018") +else() + set_source_files_properties("crypto/chacha8_stream.c" PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-strict-prototypes") +endif() + add_library(crypto ${CRYPTO}) add_library(currency_core ${CURRENCY_CORE}) diff --git a/src/crypto/hash.c b/src/crypto/hash.c index e9d35a68..0d399f49 100644 --- a/src/crypto/hash.c +++ b/src/crypto/hash.c @@ -27,5 +27,5 @@ void cn_fast_hash_old(const void *data, size_t length, char *hash) void cn_fast_hash(const void *data, size_t length, char *hash) { - keccak(data, (int)length, hash, HASH_SIZE); + keccak(data, (int)length, (uint8_t*)hash, HASH_SIZE); } From 191a89f9c880c4db51d16d25ecc5f5d2e6b7abff Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 27 Apr 2021 05:18:09 +0300 Subject: [PATCH 2/6] crypto: parse_tpod_from_hex_string --- src/crypto/crypto-sugar.h | 43 +++++++++++++++++++++++++ tests/functional_tests/crypto_tests.cpp | 14 ++++++++ 2 files changed, 57 insertions(+) diff --git a/src/crypto/crypto-sugar.h b/src/crypto/crypto-sugar.h index 14c168e8..4752d81b 100644 --- a/src/crypto/crypto-sugar.h +++ b/src/crypto/crypto-sugar.h @@ -88,6 +88,49 @@ namespace crypto return ss.str(); } + template + bool parse_tpod_from_hex_string(const std::string& hex_str, t_pod_type& t_pod) + { + static const int16_t char_map[256] = { // 0-9, a-f, A-F is only allowed + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00 - 0x1F + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, // 0x20 - 0x3F + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x40 - 0x5F + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x60 - 0x7F + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x80 - 0x9F + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xA0 - 0xBF + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xC0 - 0xDF + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; // 0xE0 - 0xFF + + size_t pod_size = sizeof t_pod; + uint8_t *p = reinterpret_cast(&t_pod); + + if (hex_str.size() != 2 * pod_size) + return false; + + for (size_t i = 0; i < pod_size; ++i) + { + int16_t hi = char_map[static_cast(hex_str[2 * i])]; + int16_t lo = char_map[static_cast(hex_str[2 * i + 1])]; + if (hi < 0 || lo < 0) + { + // invalid characters in hex_str + memset(p, 0, pod_size); + return false; + } + p[i] = static_cast(hi * 16 + lo); // write byte to pod + } + return true; + } + + template + t_pod_type parse_tpod_from_hex_string(const std::string& hex_str) + { + t_pod_type t_pod = AUTO_VAL_INIT(t_pod); + parse_tpod_from_hex_string(hex_str, t_pod); + return t_pod; + } + + // // scalar_t - holds a 256-bit scalar, normally in [0..L-1] diff --git a/tests/functional_tests/crypto_tests.cpp b/tests/functional_tests/crypto_tests.cpp index 25ceb2ee..775e9b93 100644 --- a/tests/functional_tests/crypto_tests.cpp +++ b/tests/functional_tests/crypto_tests.cpp @@ -1385,6 +1385,20 @@ TEST(ml2s, sig_verif_performance_2) } +TEST(crypto, hex_tools) +{ + ASSERT_EQ(parse_tpod_from_hex_string("00"), 0x00); + ASSERT_EQ(parse_tpod_from_hex_string("01"), 0x01); + ASSERT_EQ(parse_tpod_from_hex_string("f1"), 0xf1); + ASSERT_EQ(parse_tpod_from_hex_string("fe"), 0xfe); + ASSERT_EQ(parse_tpod_from_hex_string("efcdab8967452301"), 0x0123456789abcdef); + ASSERT_EQ(parse_tpod_from_hex_string("0123456789abcdef"), 0xefcdab8967452301); + ASSERT_EQ(parse_tpod_from_hex_string("ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f"), c_scalar_Pm1); + ASSERT_EQ(parse_tpod_from_hex_string("792fdce229e50661d0da1c7db39dd30700000000000000000000000000000006"), c_scalar_1div8); + + return true; +} + // // test's runner // From 09ad4f5900f90230577413d716969087bf30478e Mon Sep 17 00:00:00 2001 From: sowle Date: Tue, 27 Apr 2021 05:52:55 +0300 Subject: [PATCH 3/6] crypto-sugar made less dependent of epee --- src/crypto/crypto-sugar.cpp | 2 +- src/crypto/crypto-sugar.h | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/crypto/crypto-sugar.cpp b/src/crypto/crypto-sugar.cpp index 0e923359..4774e9b7 100644 --- a/src/crypto/crypto-sugar.cpp +++ b/src/crypto/crypto-sugar.cpp @@ -18,7 +18,7 @@ namespace crypto const scalar_t c_scalar_P = { 0xffffffffffffffed, 0xffffffffffffffff, 0xffffffffffffffff, 0x7fffffffffffffff }; const scalar_t c_scalar_Pm1 = { 0xffffffffffffffec, 0xffffffffffffffff, 0xffffffffffffffff, 0x7fffffffffffffff }; const scalar_t c_scalar_256m1 = { 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff }; - const scalar_t c_scalar_1div8 = { 0x6106e529e2dc2f79, 0x7d39db37d1cdad0, 0x0, 0x600000000000000 }; + const scalar_t c_scalar_1div8 = { 0x6106e529e2dc2f79, 0x07d39db37d1cdad0, 0x0, 0x0600000000000000 }; const point_t c_point_H = { 0x05087c1f5b9b32d6, 0x00547595f445c3b5, 0x764df64578552f2a, 0x8a49a651e0e0da45 }; // == Hp(G), this is being checked in bpp_basics const point_t c_point_0 = point_t(point_t::tag_zero()); diff --git a/src/crypto/crypto-sugar.h b/src/crypto/crypto-sugar.h index 4752d81b..a064af19 100644 --- a/src/crypto/crypto-sugar.h +++ b/src/crypto/crypto-sugar.h @@ -8,7 +8,6 @@ #include #include #include "crypto.h" -#include "epee/include/string_tools.h" namespace crypto { @@ -130,8 +129,6 @@ namespace crypto return t_pod; } - - // // scalar_t - holds a 256-bit scalar, normally in [0..L-1] // @@ -407,7 +404,7 @@ namespace crypto std::string to_string_as_secret_key() const { - return epee::string_tools::pod_to_hex(*this); + return pod_to_hex(*this); } template @@ -501,7 +498,7 @@ namespace crypto bool from_string(const std::string& str) { crypto::public_key pk; - if (!epee::string_tools::parse_tpod_from_hex_string(str, pk)) + if (!parse_tpod_from_hex_string(str, pk)) return false; return from_public_key(pk); } @@ -634,19 +631,19 @@ namespace crypto friend std::ostream& operator<<(std::ostream& ss, const point_t &v) { crypto::public_key pk = v.to_public_key(); - return ss << epee::string_tools::pod_to_hex(pk); + return ss << pod_to_hex(pk); } operator std::string() const { crypto::public_key pk = to_public_key(); - return epee::string_tools::pod_to_hex(pk); + return pod_to_hex(pk); } std::string to_string() const { crypto::public_key pk = to_public_key(); - return epee::string_tools::pod_to_hex(pk); + return pod_to_hex(pk); } std::string to_hex_comma_separated_bytes_str() const From b1e2fbbe552505832eb967f733bf271d88d878e5 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 27 Apr 2021 17:15:13 -0500 Subject: [PATCH 4/6] fixed few coretests --- src/currency_core/blockchain_storage.cpp | 2 +- tests/core_tests/chaingen.cpp | 13 ++++++++++--- tests/core_tests/chaingen_main.cpp | 3 +++ tests/core_tests/hard_fork_1.cpp | 14 +++++++------- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 497116d9..e422a446 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -5779,7 +5779,7 @@ bool blockchain_storage::prevalidate_block(const block& bl) get_block_height(bl) <= m_core_runtime_config.hard_fork_03_starts_after_height ) { - if (bl.major_version == HF1_BLOCK_MAJOR_VERSION) + if (bl.major_version <= HF1_BLOCK_MAJOR_VERSION ) return true; else return false; diff --git a/tests/core_tests/chaingen.cpp b/tests/core_tests/chaingen.cpp index ff7b9762..bbd61e4c 100644 --- a/tests/core_tests/chaingen.cpp +++ b/tests/core_tests/chaingen.cpp @@ -219,10 +219,17 @@ bool test_generator::construct_block(currency::block& blk, const std::list& tx_list, const std::list& coin_stake_sources)//in case of PoS block { - if (height > m_hardfork_01_after_heigh) - blk.major_version = CURRENT_BLOCK_MAJOR_VERSION; - else +// if (height > m_hardfork_01_after_heigh) +// blk.major_version = CURRENT_BLOCK_MAJOR_VERSION; +// else +// blk.major_version = BLOCK_MAJOR_VERSION_INITAL; + + if (height <= m_hardfork_01_after_heigh) blk.major_version = BLOCK_MAJOR_VERSION_INITAL; + else if (height <= m_hardfork_03_after_heigh) + blk.major_version = HF1_BLOCK_MAJOR_VERSION; + else + blk.major_version = CURRENT_BLOCK_MAJOR_VERSION; blk.minor_version = CURRENT_BLOCK_MINOR_VERSION; blk.timestamp = timestamp; diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index b24bb27b..1f21fce7 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1007,6 +1007,9 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(hard_fork_1_chain_switch_pow_only); GENERATE_AND_PLAY(hard_fork_1_checkpoint_basic_test); GENERATE_AND_PLAY(hard_fork_1_pos_locked_height_vs_time); + GENERATE_AND_PLAY(hard_fork_1_pos_and_locked_coins); + + // Hardfork 2 tests GENERATE_AND_PLAY(hard_fork_2_tx_payer_in_wallet); diff --git a/tests/core_tests/hard_fork_1.cpp b/tests/core_tests/hard_fork_1.cpp index 6116fdd9..216c7dab 100644 --- a/tests/core_tests/hard_fork_1.cpp +++ b/tests/core_tests/hard_fork_1.cpp @@ -113,7 +113,7 @@ bool hard_fork_1_unlock_time_2_in_normal_tx::generate(std::vector& MAKE_NEXT_BLOCK(events, blk_3a, blk_2a, miner_acc); // hardfork should happen here MAKE_NEXT_BLOCK(events, blk_4a, blk_3a, miner_acc); // make sure hardfork went okay - CHECK_AND_ASSERT_MES(blk_3a.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_4a.major_version == CURRENT_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); + CHECK_AND_ASSERT_MES(blk_3a.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_4a.major_version == HF1_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); // @@ -357,7 +357,7 @@ bool hard_fork_1_checkpoint_basic_test::generate(std::vector& MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_acc); // <-- hard fork MAKE_NEXT_BLOCK(events, blk_4, blk_3, miner_acc); // make sure hardfork went okay - CHECK_AND_ASSERT_MES(blk_3.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_4.major_version == CURRENT_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); + CHECK_AND_ASSERT_MES(blk_3.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_4.major_version == HF1_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); // // after hardfork 1 @@ -397,7 +397,7 @@ bool hard_fork_1_checkpoint_basic_test::generate(std::vector& pos_block_builder pb; pb.step1_init_header(height, prev_id); - pb.m_block.major_version = CURRENT_BLOCK_MAJOR_VERSION; + pb.m_block.major_version = HF1_BLOCK_MAJOR_VERSION; pb.step2_set_txs(std::vector()); pb.step3_build_stake_kernel(stake_output_amount, stake_output_gidx, stake_output_key_image, diff, prev_id, null_hash, prev_block.timestamp); pb.step4_generate_coinbase_tx(generator.get_timestamps_median(prev_id), generator.get_already_generated_coins(prev_block), miner_acc.get_public_address(), stakeholder.get_public_address()); @@ -584,7 +584,7 @@ bool hard_fork_1_pos_and_locked_coins::generate(std::vector& e MAKE_NEXT_BLOCK(events, blk_2, blk_1r, miner_acc); MAKE_NEXT_BLOCK(events, blk_3, blk_2, miner_acc); // make sure hardfork went okay - CHECK_AND_ASSERT_MES(blk_2.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_3.major_version == CURRENT_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); + CHECK_AND_ASSERT_MES(blk_2.major_version != CURRENT_BLOCK_MAJOR_VERSION && blk_3.major_version == HF1_BLOCK_MAJOR_VERSION, false, "hardfork did not happen as expected"); // try to make a PoS block with locked stake after the hardfork @@ -726,7 +726,7 @@ bool hard_fork_1_pos_locked_height_vs_time::generate(std::vector Date: Tue, 27 Apr 2021 18:03:57 -0500 Subject: [PATCH 5/6] fixed last coretest --- tests/core_tests/hard_fork_1.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core_tests/hard_fork_1.cpp b/tests/core_tests/hard_fork_1.cpp index 216c7dab..21715e4f 100644 --- a/tests/core_tests/hard_fork_1.cpp +++ b/tests/core_tests/hard_fork_1.cpp @@ -840,7 +840,7 @@ bool hard_fork_1_pos_locked_height_vs_time::generate(std::vector()); pb.step3_build_stake_kernel(stake_output_amount, stake_output_gidx, stake_output_key_image, diff, prev_id, null_hash, prev_block.timestamp); pb.step4_generate_coinbase_tx(generator.get_timestamps_median(prev_id), generator.get_already_generated_coins(prev_block), miner_acc.get_public_address(), stakeholder.get_public_address()); From 5d3a8e730aadb9e0a7f116022f1f9a2a195ddbf9 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 27 Apr 2021 20:23:49 -0500 Subject: [PATCH 6/6] disabled test --- tests/core_tests/chaingen_main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/core_tests/chaingen_main.cpp b/tests/core_tests/chaingen_main.cpp index 1f21fce7..f4c22914 100644 --- a/tests/core_tests/chaingen_main.cpp +++ b/tests/core_tests/chaingen_main.cpp @@ -1007,7 +1007,7 @@ int main(int argc, char* argv[]) GENERATE_AND_PLAY(hard_fork_1_chain_switch_pow_only); GENERATE_AND_PLAY(hard_fork_1_checkpoint_basic_test); GENERATE_AND_PLAY(hard_fork_1_pos_locked_height_vs_time); - GENERATE_AND_PLAY(hard_fork_1_pos_and_locked_coins); + //GENERATE_AND_PLAY(hard_fork_1_pos_and_locked_coins);