From 477fe460148f889fb6748ec59dacaa8c57feafac Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 04:43:10 +0200 Subject: [PATCH 1/8] Integrate random time-point seed utility --- examples/example008_miller_rabin_prime.cpp | 24 +-- examples/example008a_miller_rabin_prime.cpp | 9 +- examples/example009_timed_mul.cpp | 7 +- examples/example009a_timed_mul_4_by_4.cpp | 7 +- examples/example009b_timed_mul_8_by_8.cpp | 7 +- examples/example012_rsa_crypto.cpp | 8 +- test/test_uintwide_t_edge_cases.cpp | 37 ++--- test/test_uintwide_t_float_convert.cpp | 20 ++- test/test_uintwide_t_int_convert.cpp | 14 +- test/test_uintwide_t_n_base.h | 6 +- ...e_t_n_binary_ops_mul_div_4_by_4_template.h | 8 +- test/test_uintwide_t_n_binary_ops_template.h | 8 +- ..._uintwide_t_n_binary_ops_template_signed.h | 8 +- .../util_pseudorandom_time_point_seed.h | 147 ++++++++++++++++++ wide_integer.vcxproj | 1 + wide_integer.vcxproj.filters | 9 ++ 16 files changed, 234 insertions(+), 86 deletions(-) create mode 100644 util/utility/util_pseudorandom_time_point_seed.h diff --git a/examples/example008_miller_rabin_prime.cpp b/examples/example008_miller_rabin_prime.cpp index 022136f2..6d99877a 100644 --- a/examples/example008_miller_rabin_prime.cpp +++ b/examples/example008_miller_rabin_prime.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2018 - 2022. // +// Copyright Christopher Kormanyos 2018 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -10,11 +10,11 @@ // The Boost.Multiprecision code can be found here: // https://www.boost.org/doc/libs/1_73_0/libs/multiprecision/doc/html/boost_multiprecision/tut/primetest.html -#include #include #include #include +#include #if defined(__clang__) #if defined __has_feature && __has_feature(thread_sanitizer) @@ -43,20 +43,12 @@ namespace local_example008_miller_rabin_prime using random_engine1_type = std::linear_congruential_engine; using random_engine2_type = std::mt19937; - template - auto time_point() -> IntegralTimePointType - { - using local_integral_time_point_type = IntegralTimePointType; - - return static_cast(std::clock()); - } - auto example008_miller_rabin_prime_run() -> bool { // Use a pseudo-random seed for this test. - random_engine1_type generator1(time_point()); - random_engine2_type generator2(time_point()); + random_engine1_type generator1(util::util_pseudorandom_time_point_seed::value()); + random_engine2_type generator2(util::util_pseudorandom_time_point_seed::value()); distribution_type distribution1; distribution_type distribution2; @@ -96,10 +88,10 @@ namespace local_example008_miller_rabin_prime const auto gd = gcd(p0, p1); - const auto result_is_ok = ( (p0 != 0U) - && (p1 != 0U) + const auto result_is_ok = ( (p0 != static_cast(UINT8_C(0))) + && (p1 != static_cast(UINT8_C(0))) && (p0 != p1) - && (gd == 1U)); + && (gd == static_cast(UINT8_C(1)))); return result_is_ok; } @@ -149,7 +141,7 @@ namespace local_example008_miller_rabin_prime #endif }; - random_engine1_type generator(time_point()); + random_engine1_type generator(util::util_pseudorandom_time_point_seed::value()); distribution_type distribution; diff --git a/examples/example008a_miller_rabin_prime.cpp b/examples/example008a_miller_rabin_prime.cpp index 66073554..692e6843 100644 --- a/examples/example008a_miller_rabin_prime.cpp +++ b/examples/example008a_miller_rabin_prime.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2018 - 2022. // +// Copyright Christopher Kormanyos 2018 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -10,13 +10,14 @@ // The Boost.Multiprecision code can be found here: // https://www.boost.org/doc/libs/1_78_0/libs/multiprecision/doc/html/boost_multiprecision/tut/primetest.html -#include #include #include #include #include +#include + #if !defined(BOOST_VERSION) #error BOOST_VERSION is not defined. Ensure that is properly included. #endif @@ -104,7 +105,7 @@ auto ::math::wide_integer::example008a_miller_rabin_prime() -> bool using random_engine1_type = std::mt19937; using random_engine2_type = std::linear_congruential_engine; // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - const auto seed_start = std::clock(); + const auto seed_start = util::util_pseudorandom_time_point_seed::value(); random_engine1_type gen1(static_cast(seed_start)); random_engine2_type gen2(static_cast(seed_start)); @@ -156,7 +157,7 @@ auto ::math::wide_integer::example008a_miller_rabin_prime() -> bool } } - const auto seed_next = std::clock(); + const auto seed_next = util::util_pseudorandom_time_point_seed::value(); gen1.seed(static_cast(seed_next)); diff --git a/examples/example009_timed_mul.cpp b/examples/example009_timed_mul.cpp index a50d6126..6627b10b 100644 --- a/examples/example009_timed_mul.cpp +++ b/examples/example009_timed_mul.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2018 - 2022. // +// Copyright Christopher Kormanyos 2018 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -19,6 +18,8 @@ #include #include +#include + namespace local_timed_mul { constexpr std::uint32_t wide_integer_test9_digits2 = static_cast(1ULL << 15U); @@ -78,7 +79,7 @@ auto ::math::wide_integer::example009_timed_mul() -> bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(static_cast(std::clock())); + rng.seed(util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul::local_a().size(); ++i) { diff --git a/examples/example009a_timed_mul_4_by_4.cpp b/examples/example009a_timed_mul_4_by_4.cpp index 26e9d32b..31aa5e68 100644 --- a/examples/example009a_timed_mul_4_by_4.cpp +++ b/examples/example009a_timed_mul_4_by_4.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2018 - 2022. // +// Copyright Christopher Kormanyos 2018 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -19,6 +18,8 @@ #include #include +#include + namespace local_timed_mul_4_by_4 { template bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(static_cast(std::clock())); + rng.seed(util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul_4_by_4::local_a().size(); ++i) { diff --git a/examples/example009b_timed_mul_8_by_8.cpp b/examples/example009b_timed_mul_8_by_8.cpp index ac214e51..a4f18a02 100644 --- a/examples/example009b_timed_mul_8_by_8.cpp +++ b/examples/example009b_timed_mul_8_by_8.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2018 - 2022. // +// Copyright Christopher Kormanyos 2018 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -19,6 +18,8 @@ #include #include +#include + namespace local_timed_mul_8_by_8 { template bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(static_cast(std::clock())); + rng.seed(util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul_8_by_8::local_a().size(); ++i) { diff --git a/examples/example012_rsa_crypto.cpp b/examples/example012_rsa_crypto.cpp index 562f6bad..7a87e53c 100644 --- a/examples/example012_rsa_crypto.cpp +++ b/examples/example012_rsa_crypto.cpp @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2021 - 2022. // +// Copyright Christopher Kormanyos 2021 - 2023. // // Distributed under the Boost Software License, // // Version 1.0. (See accompanying file LICENSE_1_0.txt // // or copy at http://www.boost.org/LICENSE_1_0.txt) // @@ -11,6 +11,8 @@ #include #include +#include + namespace local_rsa { template static auto is_prime(const my_uintwide_t& p, - const RandomEngineType& generator = RandomEngineType(static_cast(std::clock()))) -> bool + const RandomEngineType& generator = RandomEngineType(util::util_pseudorandom_time_point_seed::value())) -> bool { #if defined(WIDE_INTEGER_NAMESPACE) using local_distribution_type = @@ -396,7 +398,7 @@ auto ::math::wide_integer::example012_rsa_crypto() -> bool { using local_random_engine_type = std::mt19937; - local_random_engine_type generator(static_cast(std::clock())); + local_random_engine_type generator(util::util_pseudorandom_time_point_seed::value()); const bool p_is_prime = rsa_type::is_prime(p, generator); diff --git a/test/test_uintwide_t_edge_cases.cpp b/test/test_uintwide_t_edge_cases.cpp index 131cf90c..0fc4999b 100644 --- a/test/test_uintwide_t_edge_cases.cpp +++ b/test/test_uintwide_t_edge_cases.cpp @@ -5,7 +5,6 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include #include #include #include @@ -69,6 +68,8 @@ #include #include +#include + #if defined(__clang__) #if defined __has_feature && __has_feature(thread_sanitizer) #define UINTWIDE_T_REDUCE_TEST_DEPTH @@ -102,10 +103,6 @@ constexpr auto loop_count_lo = static_cast(UINT16_C(4)); constexpr auto loop_count_hi = static_cast(UINT16_C(8)); #endif -// Forward declaration -template -auto time_point() -> IntegralTimePointType; - #if defined(WIDE_INTEGER_NAMESPACE) using local_uintwide_t_small_unsigned_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uintwide_t; @@ -169,14 +166,6 @@ auto zero_as_small_unsigned_type() -> const local_uintwide_t_small_unsigned_type auto one_as_small_unsigned_type () -> const local_uintwide_t_small_unsigned_type&; auto m_one_as_small_signed_type () -> const local_uintwide_t_small_signed_type&; -template -auto time_point() -> IntegralTimePointType -{ - using local_integral_time_point_type = IntegralTimePointType; - - return static_cast(std::clock()); -} - template auto generate_wide_integer_value(bool is_positive = true, local_base base_to_get = local_base::dec, @@ -371,8 +360,8 @@ auto test_various_ostream_ops() -> bool { auto result_is_ok = true; - eng_sgn.seed(time_point()); - eng_dig.seed(time_point()); + eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); + eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); { const auto u = local_uintwide_t_small_unsigned_type(static_cast(UINT32_C(29363))); @@ -843,7 +832,7 @@ auto test_small_prime_and_non_prime() -> bool using local_random_engine_result_type = typename random_engine_type::result_type; - auto generator = random_engine_type(time_point()); + auto generator = random_engine_type(util::util_pseudorandom_time_point_seed::value()); random_engine_type local_generator(generator); @@ -1478,8 +1467,8 @@ auto test_various_isolated_edge_cases() -> bool // NOLINT(readability-function-c auto test_to_chars_and_to_string() -> bool // NOLINT(readability-function-cognitive-complexity) { - eng_sgn.seed(time_point()); - eng_dig.seed(time_point()); + eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); + eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); auto result_is_ok = true; @@ -1673,8 +1662,8 @@ auto test_to_chars_and_to_string() -> bool // NOLINT(readability-function-cognit auto test_import_bits() -> bool // NOLINT(readability-function-cognitive-complexity) { - eng_sgn.seed(time_point()); - eng_dig.seed(time_point()); + eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); + eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); using local_boost_small_uint_backend_type = boost::multiprecision::cpp_int_backend bool // NOLINT(readability-function-cognitive-complex auto test_export_bits() -> bool // NOLINT(readability-function-cognitive-complexity) { - eng_sgn.seed(time_point()); - eng_dig.seed(time_point()); + eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); + eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); using local_boost_small_uint_backend_type = boost::multiprecision::cpp_int_backend auto ::math::wide_integer::test_uintwide_t_edge_cases() -> bool #endif { - test_uintwide_t_edge::eng_sgn.seed(test_uintwide_t_edge::time_point()); - test_uintwide_t_edge::eng_dig.seed(test_uintwide_t_edge::time_point()); + test_uintwide_t_edge::eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); + test_uintwide_t_edge::eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); auto result_is_ok = true; diff --git a/test/test_uintwide_t_float_convert.cpp b/test/test_uintwide_t_float_convert.cpp index f369d93e..572ff815 100644 --- a/test/test_uintwide_t_float_convert.cpp +++ b/test/test_uintwide_t_float_convert.cpp @@ -1,18 +1,16 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2021 - 2022. +// Copyright Christopher Kormanyos 2021 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include -#include #include -#include -#include #include +#include + #if !defined(BOOST_VERSION) #error BOOST_VERSION is not defined. Ensure that is properly included. #endif @@ -229,9 +227,9 @@ auto ::math::wide_integer::test_uintwide_t_float_convert() -> bool using local_sint_type = ::math::wide_integer::uintwide_t; #endif - local_float_convert::engine_man().seed(static_cast (std::clock())); - local_float_convert::engine_sgn().seed(static_cast (std::clock())); - local_float_convert::engine_e10().seed(static_cast::result_type>(std::clock())); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_float_convert::engine_man().seed(util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_e10().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) bool result_is_ok = true; @@ -269,9 +267,9 @@ auto ::math::wide_integer::test_uintwide_t_float_convert() -> bool result_is_ok = ((str_boost_signed == str_local_signed) && result_is_ok); } - local_float_convert::engine_man().seed(static_cast (std::clock())); - local_float_convert::engine_sgn().seed(static_cast (std::clock())); - local_float_convert::engine_e10().seed(static_cast::result_type>(std::clock())); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_float_convert::engine_man().seed(util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_e10().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) #if !defined(UINTWIDE_T_REDUCE_TEST_DEPTH) for(auto i = static_cast(0U); i < static_cast(UINT32_C(0x100000)); ++i) diff --git a/test/test_uintwide_t_int_convert.cpp b/test/test_uintwide_t_int_convert.cpp index 0bbacc8a..3b3a9bed 100644 --- a/test/test_uintwide_t_int_convert.cpp +++ b/test/test_uintwide_t_int_convert.cpp @@ -1,18 +1,16 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2021 - 2022. +// Copyright Christopher Kormanyos 2021 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) // -#include -#include #include -#include -#include #include +#include + #if !defined(BOOST_VERSION) #error BOOST_VERSION is not defined. Ensure that is properly included. #endif @@ -183,9 +181,9 @@ auto ::math::wide_integer::test_uintwide_t_int_convert() -> bool using local_sint_type = ::math::wide_integer::uintwide_t; #endif - local_int_convert::engine_val().seed(static_cast (std::clock())); - local_int_convert::engine_sgn().seed(static_cast (std::clock())); - local_int_convert::engine_len().seed(static_cast::result_type>(std::clock())); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_int_convert::engine_val().seed(util::util_pseudorandom_time_point_seed::value()); + local_int_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); + local_int_convert::engine_len().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) bool result_is_ok = true; diff --git a/test/test_uintwide_t_n_base.h b/test/test_uintwide_t_n_base.h index a83b381c..f92b4b25 100644 --- a/test/test_uintwide_t_n_base.h +++ b/test/test_uintwide_t_n_base.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2019 - 2022. +// Copyright Christopher Kormanyos 2019 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -14,6 +14,8 @@ #include + #include + #if !defined(BOOST_VERSION) #error BOOST_VERSION is not defined. Ensure that is properly included. #endif @@ -119,7 +121,7 @@ using other_local_uint_type = OtherLocalUintType; using other_boost_uint_type = OtherBoostUintType; - my_random_generator().seed(static_cast(std::clock())); + my_random_generator().seed(util::util_pseudorandom_time_point_seed::value()); #if defined(WIDE_INTEGER_NAMESPACE) using distribution_type = diff --git a/test/test_uintwide_t_n_binary_ops_mul_div_4_by_4_template.h b/test/test_uintwide_t_n_binary_ops_mul_div_4_by_4_template.h index b525d231..02d8fc4d 100644 --- a/test/test_uintwide_t_n_binary_ops_mul_div_4_by_4_template.h +++ b/test/test_uintwide_t_n_binary_ops_mul_div_4_by_4_template.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2021 - 2022. +// Copyright Christopher Kormanyos 2021 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -14,6 +14,8 @@ #include #include + #include + #if defined(WIDE_INTEGER_NAMESPACE) template(std::clock())); + test_uintwide_t_n_binary_ops_base::my_gen().seed(util::util_pseudorandom_time_point_seed::value()); std::uniform_int_distribution<> dis(1, static_cast(digits2 - 1U)); bool result_is_ok = true; @@ -196,7 +198,7 @@ test_uintwide_t_n_base::my_random_generator().seed ( - static_cast::result_type>(std::clock()) // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + util::util_pseudorandom_time_point_seed::value::result_type>() // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) ); #if defined(WIDE_INTEGER_NAMESPACE) diff --git a/test/test_uintwide_t_n_binary_ops_template.h b/test/test_uintwide_t_n_binary_ops_template.h index e651825b..4fd79626 100644 --- a/test/test_uintwide_t_n_binary_ops_template.h +++ b/test/test_uintwide_t_n_binary_ops_template.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2019 - 2022. +// Copyright Christopher Kormanyos 2019 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -16,6 +16,8 @@ #include + #include + #if defined(WIDE_INTEGER_NAMESPACE) template(std::clock())); + my_gen().seed(util::util_pseudorandom_time_point_seed::value()); std::uniform_int_distribution<> dis(1, static_cast(digits2 - 1U)); bool result_is_ok = true; @@ -233,7 +235,7 @@ { std::atomic_flag test_lock = ATOMIC_FLAG_INIT; - my_gen().seed(static_cast(std::clock())); + my_gen().seed(util::util_pseudorandom_time_point_seed::value()); std::uniform_int_distribution<> dis(1, static_cast(digits2 - 1U)); bool result_is_ok = true; diff --git a/test/test_uintwide_t_n_binary_ops_template_signed.h b/test/test_uintwide_t_n_binary_ops_template_signed.h index b985e3c4..e73106a2 100644 --- a/test/test_uintwide_t_n_binary_ops_template_signed.h +++ b/test/test_uintwide_t_n_binary_ops_template_signed.h @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright Christopher Kormanyos 2021 - 2022. +// Copyright Christopher Kormanyos 2021 - 2023. // Distributed under the Boost Software License, // Version 1.0. (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) @@ -16,6 +16,8 @@ #include + #include + #if defined(WIDE_INTEGER_NAMESPACE) template(std::clock())); + std::mt19937_64 eng64(util::util_pseudorandom_time_point_seed::value()); std::uniform_int_distribution dst_u64(UINT64_C(1), UINT64_C(0xFFFFFFFFFFFFFFFF)); @@ -535,7 +537,7 @@ WIDE_INTEGER_NODISCARD virtual auto test_binary_shr() const -> bool { - my_gen().seed(static_cast(std::clock())); + my_gen().seed(util::util_pseudorandom_time_point_seed::value()); bool result_is_ok = true; diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h new file mode 100644 index 00000000..6b181642 --- /dev/null +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -0,0 +1,147 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright Christopher Kormanyos 2023. +// Distributed under the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt +// or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H + #define UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H + + #include + #include + #include + #include + #include + #include + #include + #include + #include + + namespace util { + + struct util_pseudorandom_time_point_seed + { + public: + template + static auto value() -> IntegralType + { + using strftime_uint8_array_type = std::array(UINT8_C(64))>; + + strftime_uint8_array_type buf_u8 { }; buf_u8.fill(static_cast(UINT8_C(0))); + + std::size_t str_tm_len { }; + + { + // Get the UCT (time) expressed as a character string and also + // note its string-length. + + struct timespec ts; timespec_get(&ts, TIME_UTC); + + using strftime_char_array_type = std::array>; + + strftime_char_array_type buf { }; + + strftime(buf.data(), buf.size(), "%D %T", gmtime(&ts.tv_sec)); + + std::stringstream strm; + + strm << buf.data(); + strm << '.' << std::setfill('0') << std::setw(9) << ts.tv_nsec; + + const auto str_tm = strm.str(); + + str_tm_len = str_tm.length(); + + std::copy(str_tm.cbegin(), str_tm.cend(), buf_u8.begin()); + } + + using local_integral_type = IntegralType; + + return static_cast(crc_crc64(buf_u8.data(), str_tm_len)); + } + + private: + template + static auto crc_bitwise_template(const std::uint8_t* message, + const std::size_t count, + const UnsignedIntegralType& polynomial, + const UnsignedIntegralType& initial_value, + const UnsignedIntegralType& final_xor_value) -> UnsignedIntegralType + { + using value_type = UnsignedIntegralType; + + // The data_type is fixed to exactly 8-bits in width at the moment. + using data_type = std::uint8_t; + + value_type crc = initial_value; + + // Perform the polynomial division, one element at a time. + for(auto data_index = static_cast(UINT8_C(0)); data_index < count; ++data_index) + { + // Obtain the next data element (and reflect it if necessary). + const data_type next_data_element = message[data_index]; + + { + constexpr auto left_shift_amount = + static_cast + ( + std::numeric_limits::digits - std::numeric_limits::digits + ); + + crc ^= static_cast(static_cast(next_data_element) << left_shift_amount); + } + + // Process the next data byte, one bit at a time. + for(std::uint_fast8_t index = 0U; + index < static_cast(std::numeric_limits::digits); + ++index) + { + const auto high_bit_value = + static_cast + ( + crc + & static_cast + ( + static_cast(UINT8_C(1)) << static_cast(std::numeric_limits::digits - 1) + ) + ); + + const bool high_bit_of_crc_is_set = (high_bit_value != static_cast(UINT8_C(0))); + + crc = crc << static_cast(UINT8_C(1)); + + if(high_bit_of_crc_is_set) + { + // Shift through the polynomial. Also left-justify the + // polynomial within the width of value_type, if necessary. + + crc ^= static_cast(polynomial); + } + } + } + + // Perform the final XOR on the result. + crc ^= final_xor_value; + + return crc; + } + + static auto crc_crc64(const std::uint8_t* message, const std::size_t count) -> std::uint64_t + { + // check: 0x6C40DF5F0B497347 + return crc_bitwise_template<64U, std::uint64_t> + ( + message, + count, + static_cast(UINT64_C(0x42F0E1EBA9EA3693)), + static_cast(UINT64_C(0x0000000000000000)), + static_cast(UINT64_C(0x0000000000000000)) + ); + } + }; + + } // namespace util + +#endif // UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H diff --git a/wide_integer.vcxproj b/wide_integer.vcxproj index 42c604bc..788e6034 100644 --- a/wide_integer.vcxproj +++ b/wide_integer.vcxproj @@ -195,6 +195,7 @@ + diff --git a/wide_integer.vcxproj.filters b/wide_integer.vcxproj.filters index 4ebb00d5..0d8dfbc0 100644 --- a/wide_integer.vcxproj.filters +++ b/wide_integer.vcxproj.filters @@ -68,6 +68,12 @@ {24de4171-df89-40f3-87b7-3dfdee251467} + + {81934cc8-18b6-4dc9-8558-f5adcec3bf99} + + + {5995e678-0e77-4d1b-b881-ef1ada7cb408} + @@ -112,6 +118,9 @@ Source Files\test + + Source Files\util\utility + From a38fa3332a261aab827ca1760122f9e307edeee1 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 04:53:17 +0200 Subject: [PATCH 2/8] Handle a warning on MSVC --- util/utility/util_pseudorandom_time_point_seed.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h index 6b181642..b8abaf13 100644 --- a/util/utility/util_pseudorandom_time_point_seed.h +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -38,11 +38,18 @@ struct timespec ts; timespec_get(&ts, TIME_UTC); - using strftime_char_array_type = std::array>; + using strftime_char_array_type = std::array::value>; strftime_char_array_type buf { }; + #if defined(_MSC_VER) + #pragma warning(push) + #pragma warning(disable : 4996) + #endif strftime(buf.data(), buf.size(), "%D %T", gmtime(&ts.tv_sec)); + #if defined(_MSC_VER) + #pragma warning( pop ) + #endif std::stringstream strm; From e1875510799b39fadac1792a622a899098a6f3e6 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 05:21:20 +0200 Subject: [PATCH 3/8] Handle some namespace issues and clang-tidy --- examples/example008a_miller_rabin_prime.cpp | 4 ++-- examples/example009_timed_mul.cpp | 2 +- examples/example009a_timed_mul_4_by_4.cpp | 2 +- examples/example009b_timed_mul_8_by_8.cpp | 2 +- examples/example012_rsa_crypto.cpp | 2 +- test/test_uintwide_t_edge_cases.cpp | 4 ++-- test/test_uintwide_t_float_convert.cpp | 12 +++++------ test/test_uintwide_t_int_convert.cpp | 6 +++--- .../util_pseudorandom_time_point_seed.h | 20 +++++++++---------- 9 files changed, 27 insertions(+), 27 deletions(-) diff --git a/examples/example008a_miller_rabin_prime.cpp b/examples/example008a_miller_rabin_prime.cpp index 692e6843..6d0e5cbe 100644 --- a/examples/example008a_miller_rabin_prime.cpp +++ b/examples/example008a_miller_rabin_prime.cpp @@ -105,7 +105,7 @@ auto ::math::wide_integer::example008a_miller_rabin_prime() -> bool using random_engine1_type = std::mt19937; using random_engine2_type = std::linear_congruential_engine; // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) - const auto seed_start = util::util_pseudorandom_time_point_seed::value(); + const auto seed_start = ::util::util_pseudorandom_time_point_seed::value(); random_engine1_type gen1(static_cast(seed_start)); random_engine2_type gen2(static_cast(seed_start)); @@ -157,7 +157,7 @@ auto ::math::wide_integer::example008a_miller_rabin_prime() -> bool } } - const auto seed_next = util::util_pseudorandom_time_point_seed::value(); + const auto seed_next = ::util::util_pseudorandom_time_point_seed::value(); gen1.seed(static_cast(seed_next)); diff --git a/examples/example009_timed_mul.cpp b/examples/example009_timed_mul.cpp index 6627b10b..e8fbcc9f 100644 --- a/examples/example009_timed_mul.cpp +++ b/examples/example009_timed_mul.cpp @@ -79,7 +79,7 @@ auto ::math::wide_integer::example009_timed_mul() -> bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(util::util_pseudorandom_time_point_seed::value()); + rng.seed(::util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul::local_a().size(); ++i) { diff --git a/examples/example009a_timed_mul_4_by_4.cpp b/examples/example009a_timed_mul_4_by_4.cpp index 31aa5e68..d771268d 100644 --- a/examples/example009a_timed_mul_4_by_4.cpp +++ b/examples/example009a_timed_mul_4_by_4.cpp @@ -77,7 +77,7 @@ auto ::math::wide_integer::example009a_timed_mul_4_by_4() -> bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(util::util_pseudorandom_time_point_seed::value()); + rng.seed(::util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul_4_by_4::local_a().size(); ++i) { diff --git a/examples/example009b_timed_mul_8_by_8.cpp b/examples/example009b_timed_mul_8_by_8.cpp index a4f18a02..52289249 100644 --- a/examples/example009b_timed_mul_8_by_8.cpp +++ b/examples/example009b_timed_mul_8_by_8.cpp @@ -77,7 +77,7 @@ auto ::math::wide_integer::example009b_timed_mul_8_by_8() -> bool random_engine_type rng; // NOLINT(cert-msc32-c,cert-msc51-cpp) - rng.seed(util::util_pseudorandom_time_point_seed::value()); + rng.seed(::util::util_pseudorandom_time_point_seed::value()); for(auto i = static_cast::size_type>(0U); i < local_timed_mul_8_by_8::local_a().size(); ++i) { diff --git a/examples/example012_rsa_crypto.cpp b/examples/example012_rsa_crypto.cpp index 7a87e53c..39ff8755 100644 --- a/examples/example012_rsa_crypto.cpp +++ b/examples/example012_rsa_crypto.cpp @@ -398,7 +398,7 @@ auto ::math::wide_integer::example012_rsa_crypto() -> bool { using local_random_engine_type = std::mt19937; - local_random_engine_type generator(util::util_pseudorandom_time_point_seed::value()); + local_random_engine_type generator(::util::util_pseudorandom_time_point_seed::value()); const bool p_is_prime = rsa_type::is_prime(p, generator); diff --git a/test/test_uintwide_t_edge_cases.cpp b/test/test_uintwide_t_edge_cases.cpp index 0fc4999b..56ee4bb3 100644 --- a/test/test_uintwide_t_edge_cases.cpp +++ b/test/test_uintwide_t_edge_cases.cpp @@ -2178,8 +2178,8 @@ auto WIDE_INTEGER_NAMESPACE::math::wide_integer::test_uintwide_t_edge_cases() -> auto ::math::wide_integer::test_uintwide_t_edge_cases() -> bool #endif { - test_uintwide_t_edge::eng_sgn.seed(util::util_pseudorandom_time_point_seed::value()); - test_uintwide_t_edge::eng_dig.seed(util::util_pseudorandom_time_point_seed::value()); + test_uintwide_t_edge::eng_sgn.seed(::util::util_pseudorandom_time_point_seed::value()); + test_uintwide_t_edge::eng_dig.seed(::util::util_pseudorandom_time_point_seed::value()); auto result_is_ok = true; diff --git a/test/test_uintwide_t_float_convert.cpp b/test/test_uintwide_t_float_convert.cpp index 572ff815..51ac8192 100644 --- a/test/test_uintwide_t_float_convert.cpp +++ b/test/test_uintwide_t_float_convert.cpp @@ -227,9 +227,9 @@ auto ::math::wide_integer::test_uintwide_t_float_convert() -> bool using local_sint_type = ::math::wide_integer::uintwide_t; #endif - local_float_convert::engine_man().seed(util::util_pseudorandom_time_point_seed::value()); - local_float_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); - local_float_convert::engine_e10().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_float_convert::engine_man().seed(::util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_sgn().seed(::util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_e10().seed(::util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) bool result_is_ok = true; @@ -267,9 +267,9 @@ auto ::math::wide_integer::test_uintwide_t_float_convert() -> bool result_is_ok = ((str_boost_signed == str_local_signed) && result_is_ok); } - local_float_convert::engine_man().seed(util::util_pseudorandom_time_point_seed::value()); - local_float_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); - local_float_convert::engine_e10().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_float_convert::engine_man().seed(::util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_sgn().seed(::util::util_pseudorandom_time_point_seed::value()); + local_float_convert::engine_e10().seed(::util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) #if !defined(UINTWIDE_T_REDUCE_TEST_DEPTH) for(auto i = static_cast(0U); i < static_cast(UINT32_C(0x100000)); ++i) diff --git a/test/test_uintwide_t_int_convert.cpp b/test/test_uintwide_t_int_convert.cpp index 3b3a9bed..834aa888 100644 --- a/test/test_uintwide_t_int_convert.cpp +++ b/test/test_uintwide_t_int_convert.cpp @@ -181,9 +181,9 @@ auto ::math::wide_integer::test_uintwide_t_int_convert() -> bool using local_sint_type = ::math::wide_integer::uintwide_t; #endif - local_int_convert::engine_val().seed(util::util_pseudorandom_time_point_seed::value()); - local_int_convert::engine_sgn().seed(util::util_pseudorandom_time_point_seed::value()); - local_int_convert::engine_len().seed(util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + local_int_convert::engine_val().seed(::util::util_pseudorandom_time_point_seed::value()); + local_int_convert::engine_sgn().seed(::util::util_pseudorandom_time_point_seed::value()); + local_int_convert::engine_len().seed(::util::util_pseudorandom_time_point_seed::value::result_type>()); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) bool result_is_ok = true; diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h index b8abaf13..b580dd35 100644 --- a/util/utility/util_pseudorandom_time_point_seed.h +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -5,7 +5,7 @@ // or copy at http://www.boost.org/LICENSE_1_0.txt) // -#ifndef UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H +#ifndef UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H // NOLINT(llvm-header-guard) #define UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H #include @@ -36,7 +36,7 @@ // Get the UCT (time) expressed as a character string and also // note its string-length. - struct timespec ts; timespec_get(&ts, TIME_UTC); + struct timespec ts { }; timespec_get(&ts, TIME_UTC); using strftime_char_array_type = std::array::value>; @@ -54,7 +54,7 @@ std::stringstream strm; strm << buf.data(); - strm << '.' << std::setfill('0') << std::setw(9) << ts.tv_nsec; + strm << '.' << std::setfill('0') << std::setw(static_cast(INT8_C(9))) << ts.tv_nsec; const auto str_tm = strm.str(); @@ -71,11 +71,11 @@ private: template - static auto crc_bitwise_template(const std::uint8_t* message, - const std::size_t count, - const UnsignedIntegralType& polynomial, - const UnsignedIntegralType& initial_value, - const UnsignedIntegralType& final_xor_value) -> UnsignedIntegralType + static constexpr auto crc_bitwise_template(const std::uint8_t* message, + const std::size_t count, + const UnsignedIntegralType& polynomial, // NOLINT(bugprone-easily-swappable-parameters) + const UnsignedIntegralType& initial_value, + const UnsignedIntegralType& final_xor_value) -> UnsignedIntegralType { using value_type = UnsignedIntegralType; @@ -135,10 +135,10 @@ return crc; } - static auto crc_crc64(const std::uint8_t* message, const std::size_t count) -> std::uint64_t + static constexpr auto crc_crc64(const std::uint8_t* message, const std::size_t count) -> std::uint64_t { // check: 0x6C40DF5F0B497347 - return crc_bitwise_template<64U, std::uint64_t> + return crc_bitwise_template(UINT8_C(64)), std::uint64_t> ( message, count, From 88e3938d97f58479eb91b42e376bc8ba3dd39f49 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 05:36:31 +0200 Subject: [PATCH 4/8] Enable UTC on MinGW and cygwin WinHost --- .github/workflows/wide_integer.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/wide_integer.yml b/.github/workflows/wide_integer.yml index 9f6dce06..e03fdd58 100644 --- a/.github/workflows/wide_integer.yml +++ b/.github/workflows/wide_integer.yml @@ -653,7 +653,7 @@ jobs: run: | echo compile ./wide_integer.exe ${{ matrix.compiler }} -v - ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -I. -I../boost-root -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe + ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -D_UCRT -I. -I../boost-root -pthread -lpthread -lucrt test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe dir %cd%\wide_integer.exe %cd%\wide_integer.exe cygwin-winhost-x64: @@ -686,7 +686,7 @@ jobs: - name: boost-generate-headers run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE")/../boost-root && ./b2 headers' - name: wide-integer-compile - run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -I. -I../boost-root -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe' + run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -D_UCRT -I. -I../boost-root -pthread -lpthread -lucrt test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe' - name: wide-integer-run run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ./wide_integer.exe' gcc-arm-none-eabi: From 7fb9155fbef1eb2f7354c31b60b8b312c6d0c729 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 07:42:31 +0200 Subject: [PATCH 5/8] Adaptions for all of CI --- .github/workflows/wide_integer.yml | 4 ++-- .../util_pseudorandom_time_point_seed.h | 24 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/wide_integer.yml b/.github/workflows/wide_integer.yml index e03fdd58..9cc99a32 100644 --- a/.github/workflows/wide_integer.yml +++ b/.github/workflows/wide_integer.yml @@ -653,7 +653,7 @@ jobs: run: | echo compile ./wide_integer.exe ${{ matrix.compiler }} -v - ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -D_UCRT -I. -I../boost-root -pthread -lpthread -lucrt test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe + ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -I. -I../boost-root -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe dir %cd%\wide_integer.exe %cd%\wide_integer.exe cygwin-winhost-x64: @@ -686,7 +686,7 @@ jobs: - name: boost-generate-headers run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE")/../boost-root && ./b2 headers' - name: wide-integer-compile - run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -D_UCRT -I. -I../boost-root -pthread -lpthread -lucrt test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe' + run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ${{ matrix.compiler }} -finline-functions -m64 -O3 -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow -Wundef -Wunused-parameter -Wuninitialized -Wunreachable-code -Winit-self -Wzero-as-null-pointer-constant -std=${{ matrix.standard }} -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -I. -I../boost-root -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example000a_builtin_convert.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp examples/example013_ecdsa_sign_verify.cpp examples/example014_pi_spigot_wide.cpp -o wide_integer.exe' - name: wide-integer-run run: C:\\tools\\cygwin\\bin\\bash -l -c 'cd $(cygpath -u "$GITHUB_WORKSPACE") && ./wide_integer.exe' gcc-arm-none-eabi: diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h index b580dd35..74d48ecc 100644 --- a/util/utility/util_pseudorandom_time_point_seed.h +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -33,10 +33,8 @@ std::size_t str_tm_len { }; { - // Get the UCT (time) expressed as a character string and also - // note its string-length. - - struct timespec ts { }; timespec_get(&ts, TIME_UTC); + // Get the time. + const std::time_t now = std::time(nullptr); using strftime_char_array_type = std::array::value>; @@ -46,15 +44,17 @@ #pragma warning(push) #pragma warning(disable : 4996) #endif - strftime(buf.data(), buf.size(), "%D %T", gmtime(&ts.tv_sec)); + // Format the time in a calendar-style. + strftime(buf.data(), buf.size(), "%c", std::localtime(&now)); #if defined(_MSC_VER) #pragma warning( pop ) #endif std::stringstream strm; + // Append the clock()-time in arbitrary units. strm << buf.data(); - strm << '.' << std::setfill('0') << std::setw(static_cast(INT8_C(9))) << ts.tv_nsec; + strm << '+' << std::setfill('0') << std::setw(9) << std::clock(); const auto str_tm = strm.str(); @@ -71,11 +71,11 @@ private: template - static constexpr auto crc_bitwise_template(const std::uint8_t* message, - const std::size_t count, - const UnsignedIntegralType& polynomial, // NOLINT(bugprone-easily-swappable-parameters) - const UnsignedIntegralType& initial_value, - const UnsignedIntegralType& final_xor_value) -> UnsignedIntegralType + static constexpr auto crc_bitwise_template(const std::uint8_t* message, + const std::size_t count, + const UnsignedIntegralType polynomial, // NOLINT(bugprone-easily-swappable-parameters) + const UnsignedIntegralType initial_value, + const UnsignedIntegralType final_xor_value) -> UnsignedIntegralType { using value_type = UnsignedIntegralType; @@ -88,7 +88,7 @@ for(auto data_index = static_cast(UINT8_C(0)); data_index < count; ++data_index) { // Obtain the next data element (and reflect it if necessary). - const data_type next_data_element = message[data_index]; + const data_type next_data_element = message[data_index]; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) { constexpr auto left_shift_amount = From e8a26a2b2a93959a0eccadfd429a759eeed34c70 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 09:21:31 +0200 Subject: [PATCH 6/8] Tests and clang-tidy --- .../util_pseudorandom_time_point_seed.h | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h index 74d48ecc..b0cdeb9f 100644 --- a/util/utility/util_pseudorandom_time_point_seed.h +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -45,7 +45,7 @@ #pragma warning(disable : 4996) #endif // Format the time in a calendar-style. - strftime(buf.data(), buf.size(), "%c", std::localtime(&now)); + strftime(buf.data(), buf.size(), "%c", std::localtime(&now)); // NOLINT(concurrency-mt-unsafe) #if defined(_MSC_VER) #pragma warning( pop ) #endif @@ -54,7 +54,7 @@ // Append the clock()-time in arbitrary units. strm << buf.data(); - strm << '+' << std::setfill('0') << std::setw(9) << std::clock(); + strm << '+' << std::setfill('0') << std::setw(static_cast(INT8_C(9))) << std::clock(); const auto str_tm = strm.str(); @@ -68,6 +68,8 @@ return static_cast(crc_crc64(buf_u8.data(), str_tm_len)); } + static constexpr auto test() noexcept -> bool; + private: template @@ -149,6 +151,28 @@ } }; + constexpr auto util_pseudorandom_time_point_seed::test() noexcept -> bool + { + constexpr std::uint8_t crc64_test_data[static_cast(UINT8_C(9))] = + { + 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, 0x38U, 0x39U + }; + + constexpr auto crc64_test_result = crc_bitwise_template(UINT8_C(64)), std::uint64_t> + ( + crc64_test_data, + sizeof(crc64_test_data), + static_cast(UINT64_C(0x42F0E1EBA9EA3693)), + static_cast(UINT64_C(0x0000000000000000)), + static_cast(UINT64_C(0x0000000000000000)) + ); + + // check: 0x6C40DF5F0B497347 + return (crc64_test_result == static_cast(UINT64_C(0x6C40DF5F0B497347))); + } + + static_assert(util::util_pseudorandom_time_point_seed::test(), "Error: crc64 implementation is not working properly"); + } // namespace util #endif // UTIL_PSEUDORANDOM_TIME_POINT_SEED_2023_10_27_H From ac4657eae449a9b98b398c6896452c908dfce580 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 09:32:17 +0200 Subject: [PATCH 7/8] Handle clang tidy in static test --- util/utility/util_pseudorandom_time_point_seed.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/utility/util_pseudorandom_time_point_seed.h b/util/utility/util_pseudorandom_time_point_seed.h index b0cdeb9f..27e32363 100644 --- a/util/utility/util_pseudorandom_time_point_seed.h +++ b/util/utility/util_pseudorandom_time_point_seed.h @@ -153,14 +153,14 @@ constexpr auto util_pseudorandom_time_point_seed::test() noexcept -> bool { - constexpr std::uint8_t crc64_test_data[static_cast(UINT8_C(9))] = + constexpr std::uint8_t crc64_test_data[static_cast(UINT8_C(9))] = // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) { 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, 0x38U, 0x39U }; constexpr auto crc64_test_result = crc_bitwise_template(UINT8_C(64)), std::uint64_t> ( - crc64_test_data, + crc64_test_data, // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) sizeof(crc64_test_data), static_cast(UINT64_C(0x42F0E1EBA9EA3693)), static_cast(UINT64_C(0x0000000000000000)), From e8de654fa263911353005e0223c960700d9556b2 Mon Sep 17 00:00:00 2001 From: Christopher Kormanyos Date: Sat, 28 Oct 2023 12:43:29 +0200 Subject: [PATCH 8/8] Update sonar scanner version --- .github/workflows/wide_integer_sonar.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/wide_integer_sonar.yml b/.github/workflows/wide_integer_sonar.yml index eacadc14..3c44f2cb 100644 --- a/.github/workflows/wide_integer_sonar.yml +++ b/.github/workflows/wide_integer_sonar.yml @@ -17,7 +17,7 @@ jobs: name: sonar-gcc-native runs-on: ubuntu-latest env: - SONAR_SCANNER_VERSION: 4.8.0.2856 + SONAR_SCANNER_VERSION: 5.0.1.3006 SONAR_SERVER_URL: "https://sonarcloud.io" BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory # Directory where build-wrapper output will be placed steps: