Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time point pseudorandom seeds #387

Merged
merged 8 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/wide_integer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -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:
Expand Down Expand Up @@ -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 -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:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wide_integer_sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 8 additions & 16 deletions examples/example008_miller_rabin_prime.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -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 <ctime>
#include <random>

#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>
#include <util/utility/util_pseudorandom_time_point_seed.h>

#if defined(__clang__)
#if defined __has_feature && __has_feature(thread_sanitizer)
Expand Down Expand Up @@ -43,20 +43,12 @@ namespace local_example008_miller_rabin_prime
using random_engine1_type = std::linear_congruential_engine<std::uint32_t, UINT32_C(48271), UINT32_C(0), UINT32_C(2147483647)>;
using random_engine2_type = std::mt19937;

template<typename IntegralTimePointType>
auto time_point() -> IntegralTimePointType
{
using local_integral_time_point_type = IntegralTimePointType;

return static_cast<local_integral_time_point_type>(std::clock());
}

auto example008_miller_rabin_prime_run() -> bool
{
// Use a pseudo-random seed for this test.

random_engine1_type generator1(time_point<typename random_engine1_type::result_type>());
random_engine2_type generator2(time_point<typename random_engine2_type::result_type>());
random_engine1_type generator1(util::util_pseudorandom_time_point_seed::value<typename random_engine1_type::result_type>());
random_engine2_type generator2(util::util_pseudorandom_time_point_seed::value<typename random_engine2_type::result_type>());

distribution_type distribution1;
distribution_type distribution2;
Expand Down Expand Up @@ -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<unsigned>(UINT8_C(0)))
&& (p1 != static_cast<unsigned>(UINT8_C(0)))
&& (p0 != p1)
&& (gd == 1U));
&& (gd == static_cast<unsigned>(UINT8_C(1))));

return result_is_ok;
}
Expand Down Expand Up @@ -149,7 +141,7 @@ namespace local_example008_miller_rabin_prime
#endif
};

random_engine1_type generator(time_point<typename random_engine1_type::result_type>());
random_engine1_type generator(util::util_pseudorandom_time_point_seed::value<typename random_engine1_type::result_type>());

distribution_type distribution;

Expand Down
9 changes: 5 additions & 4 deletions examples/example008a_miller_rabin_prime.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -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 <ctime>
#include <random>
#include <sstream>
#include <string>

#include <boost/version.hpp>

#include <util/utility/util_pseudorandom_time_point_seed.h>

#if !defined(BOOST_VERSION)
#error BOOST_VERSION is not defined. Ensure that <boost/version.hpp> is properly included.
#endif
Expand Down Expand Up @@ -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<std::uint32_t, UINT32_C(48271), UINT32_C(0), UINT32_C(2147483647)>; // 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<std::uint64_t>();

random_engine1_type gen1(static_cast<typename random_engine1_type::result_type>(seed_start));
random_engine2_type gen2(static_cast<typename random_engine2_type::result_type>(seed_start));
Expand Down Expand Up @@ -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<std::uint64_t>();

gen1.seed(static_cast<typename random_engine1_type::result_type>(seed_next));

Expand Down
7 changes: 4 additions & 3 deletions examples/example009_timed_mul.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -8,7 +8,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
Expand All @@ -19,6 +18,8 @@
#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul
{
constexpr std::uint32_t wide_integer_test9_digits2 = static_cast<std::uint32_t>(1ULL << 15U);
Expand Down Expand Up @@ -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<typename random_engine_type::result_type>(std::clock()));
rng.seed(::util::util_pseudorandom_time_point_seed::value<typename random_engine_type::result_type>());

for(auto i = static_cast<typename std::vector<local_timed_mul::big_uint_type>::size_type>(0U); i < local_timed_mul::local_a().size(); ++i)
{
Expand Down
7 changes: 4 additions & 3 deletions examples/example009a_timed_mul_4_by_4.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -8,7 +8,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
Expand All @@ -19,6 +18,8 @@
#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul_4_by_4
{
template<typename UnsignedIntegralIteratorType,
Expand Down Expand Up @@ -76,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(static_cast<typename random_engine_type::result_type>(std::clock()));
rng.seed(::util::util_pseudorandom_time_point_seed::value<typename random_engine_type::result_type>());

for(auto i = static_cast<typename std::vector<local_timed_mul_4_by_4::big_uint_type>::size_type>(0U); i < local_timed_mul_4_by_4::local_a().size(); ++i)
{
Expand Down
7 changes: 4 additions & 3 deletions examples/example009b_timed_mul_8_by_8.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -8,7 +8,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <iterator>
Expand All @@ -19,6 +18,8 @@
#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_timed_mul_8_by_8
{
template<typename UnsignedIntegralIteratorType,
Expand Down Expand Up @@ -76,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(static_cast<typename random_engine_type::result_type>(std::clock()));
rng.seed(::util::util_pseudorandom_time_point_seed::value<typename random_engine_type::result_type>());

for(auto i = static_cast<typename std::vector<local_timed_mul_8_by_8::big_uint_type>::size_type>(0U); i < local_timed_mul_8_by_8::local_a().size(); ++i)
{
Expand Down
8 changes: 5 additions & 3 deletions examples/example012_rsa_crypto.cpp
Original file line number Diff line number Diff line change
@@ -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) //
Expand All @@ -11,6 +11,8 @@
#include <examples/example_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <util/utility/util_pseudorandom_time_point_seed.h>

namespace local_rsa
{
template<const std::size_t RsaBitCount,
Expand Down Expand Up @@ -206,7 +208,7 @@ namespace local_rsa

template<typename RandomEngineType = std::minstd_rand>
static auto is_prime(const my_uintwide_t& p,
const RandomEngineType& generator = RandomEngineType(static_cast<typename RandomEngineType::result_type>(std::clock()))) -> bool
const RandomEngineType& generator = RandomEngineType(util::util_pseudorandom_time_point_seed::value<typename RandomEngineType::result_type>())) -> bool
{
#if defined(WIDE_INTEGER_NAMESPACE)
using local_distribution_type =
Expand Down Expand Up @@ -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<typename std::mt19937::result_type>(std::clock()));
local_random_engine_type generator(::util::util_pseudorandom_time_point_seed::value<typename std::mt19937::result_type>());

const bool p_is_prime = rsa_type::is_prime(p, generator);

Expand Down
Loading