Skip to content

Commit

Permalink
Repair and upgrade example chapter16_08
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Dec 12, 2024
1 parent 6771f68 commit f9f2008
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@

virtual ~miller_rabin_base() = default;

virtual bool search() = 0;
virtual auto search() -> bool = 0;

bool get_n_is_probably_prime() const
auto get_n_is_probably_prime() const -> bool
{
return my_n_is_probably_prime;
}

const wide_integer_type& get_n() const
auto get_n() const -> const wide_integer_type&
{
return my_n;
}

void reseed1(const typename generator1_type::result_type seed1) { my_generator1.seed(seed1); }
void reseed2(const typename generator2_type::result_type seed2) { my_generator2.seed(seed2); }
auto reseed1(const typename generator1_type::result_type seed1) -> void { my_generator1.seed(seed1); }
auto reseed2(const typename generator2_type::result_type seed2) -> void { my_generator2.seed(seed2); }

static std::uint64_t get_n_total_mul_10() { return my_n_total_mul_10; }
static auto get_n_total_mul_10() -> std::uint64_t { return my_n_total_mul_10; }

protected:
using distribution_param_type = typename distribution_type::param_type;

static constexpr std::uint_fast32_t my_number_of_trials = UINT32_C(25);
static constexpr std::uint_fast32_t my_number_of_trials { UINT8_C(25) };

wide_integer_type my_n_trial;
bool my_n_trial_is_probably_prime;
Expand All @@ -59,9 +59,9 @@
wide_integer_type my_n_m1;
generator1_type my_generator1;
generator2_type my_generator2;
distribution_type my_distribution;
std::uint_fast32_t my_k;
wide_integer_type my_q;
distribution_type my_distribution { };
std::uint_fast32_t my_k { };
wide_integer_type my_q { };

constexpr miller_rabin_base(const typename generator1_type::result_type seed1 = typename generator1_type::result_type(),
const typename generator2_type::result_type seed2 = typename generator2_type::result_type())
Expand All @@ -71,12 +71,9 @@
my_n_is_probably_prime (false),
my_n_m1 (),
my_generator1 (seed1),
my_generator2 (seed2),
my_distribution (),
my_k (0U),
my_q () { }
my_generator2 (seed2) { }

bool set_n()
auto set_n() -> bool
{
my_distribution.param(distribution_param_type());

Expand Down Expand Up @@ -132,18 +129,18 @@
return set_n_is_ok;
}

bool exclude_small_factors_1_0() { return do_exclude_small_factors_from_ppn({ 3U, 5U, 7U, 11U, 13U, 17U, 19U, 23U }, UINT32_C( 223092870)); }
bool exclude_small_factors_2_0() { return do_exclude_small_factors_from_ppn({ 29U, 31U, 37U, 41U, 43U, 47U }, UINT32_C(2756205443)); }
bool exclude_small_factors_3_0() { return do_exclude_small_factors_from_ppn({ 53U, 59U, 61U, 67U, 71U }, UINT32_C( 907383479)); }
bool exclude_small_factors_4_0() { return do_exclude_small_factors_from_ppn({ 73U, 79U, 83U, 89U, 97U }, UINT32_C(4132280413)); }
bool exclude_small_factors_5_0() { return do_exclude_small_factors_from_ppn({ 101U, 103U, 107U, 109U }, UINT32_C( 121330189)); }
bool exclude_small_factors_5_1() { return do_exclude_small_factors_from_ppn({ 113U, 127U, 131U, 137U }, std::uint32_t(113ULL * 127ULL * 131ULL * 137ULL)); }
bool exclude_small_factors_5_2() { return do_exclude_small_factors_from_ppn({ 139U, 149U, 151U, 157U }, std::uint32_t(139ULL * 149ULL * 151ULL * 157ULL)); }
bool exclude_small_factors_5_3() { return do_exclude_small_factors_from_ppn({ 163U, 167U, 173U, 179U }, std::uint32_t(163ULL * 167ULL * 173ULL * 179ULL)); }
bool exclude_small_factors_5_4() { return do_exclude_small_factors_from_ppn({ 181U, 191U, 193U, 197U }, std::uint32_t(181ULL * 191ULL * 193ULL * 197ULL)); }
bool exclude_small_factors_5_5() { return do_exclude_small_factors_from_ppn({ 199U, 211U, 223U, 227U }, std::uint32_t(199ULL * 211ULL * 223ULL * 227ULL)); }

void prepare_random_trials()
auto exclude_small_factors_1_0() -> bool { return do_exclude_small_factors_from_ppn({ 3U, 5U, 7U, 11U, 13U, 17U, 19U, 23U }, UINT32_C( 223092870)); }
auto exclude_small_factors_2_0() -> bool { return do_exclude_small_factors_from_ppn({ 29U, 31U, 37U, 41U, 43U, 47U }, UINT32_C(2756205443)); }
auto exclude_small_factors_3_0() -> bool { return do_exclude_small_factors_from_ppn({ 53U, 59U, 61U, 67U, 71U }, UINT32_C( 907383479)); }
auto exclude_small_factors_4_0() -> bool { return do_exclude_small_factors_from_ppn({ 73U, 79U, 83U, 89U, 97U }, UINT32_C(4132280413)); }
auto exclude_small_factors_5_0() -> bool { return do_exclude_small_factors_from_ppn({ 101U, 103U, 107U, 109U }, UINT32_C( 121330189)); }
auto exclude_small_factors_5_1() -> bool { return do_exclude_small_factors_from_ppn({ 113U, 127U, 131U, 137U }, std::uint32_t(113ULL * 127ULL * 131ULL * 137ULL)); }
auto exclude_small_factors_5_2() -> bool { return do_exclude_small_factors_from_ppn({ 139U, 149U, 151U, 157U }, std::uint32_t(139ULL * 149ULL * 151ULL * 157ULL)); }
auto exclude_small_factors_5_3() -> bool { return do_exclude_small_factors_from_ppn({ 163U, 167U, 173U, 179U }, std::uint32_t(163ULL * 167ULL * 173ULL * 179ULL)); }
auto exclude_small_factors_5_4() -> bool { return do_exclude_small_factors_from_ppn({ 181U, 191U, 193U, 197U }, std::uint32_t(181ULL * 191ULL * 193ULL * 197ULL)); }
auto exclude_small_factors_5_5() -> bool { return do_exclude_small_factors_from_ppn({ 199U, 211U, 223U, 227U }, std::uint32_t(199ULL * 211ULL * 223ULL * 227ULL)); }

auto prepare_random_trials() -> void
{
my_k = lsb(my_n_m1);

Expand All @@ -155,7 +152,7 @@
my_n_trial_is_probably_prime = true;
}

virtual bool execute_one_trial()
virtual auto execute_one_trial() -> bool
{
wide_integer_type y = powm(my_distribution(my_generator2), my_q, my_n_trial);

Expand Down Expand Up @@ -195,7 +192,7 @@
private:
static std::uint64_t my_n_total_mul_10;

bool do_exclude_small_factors_from_ppn(std::initializer_list<std::uint8_t> small_factors, const std::uint32_t& ppn)
auto do_exclude_small_factors_from_ppn(std::initializer_list<std::uint8_t> small_factors, const std::uint32_t& ppn) -> bool
{
bool exclude_does_terminate_prime_candidate = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,58 +24,62 @@
using local_limb_type = typename local_normal_width_type::limb_type;

public:
miller_rabin_powm_state() : my_done(false),
my_y (),
my_p (),
my_m () { }
miller_rabin_powm_state() = default;

~miller_rabin_powm_state() = default;

void init(const local_normal_width_type& b,
auto init(const local_normal_width_type& b,
const local_normal_width_type& p,
const local_normal_width_type& m)
const local_normal_width_type& m) -> void
{
my_done = false;
my_y = local_double_width_type(b);
my_x = local_double_width_type(1U);
my_y = local_double_width_type { b };
my_x = local_double_width_type { unsigned { UINT8_C(1) } };
my_p = p;
my_m = m;

if((local_limb_type(my_p) == 0U) && (my_p == 0U))
if( (static_cast<local_limb_type>(my_p) == local_limb_type { UINT8_C(0) })
&& (my_p == unsigned { UINT8_C(0) }))
{
my_x = ((my_m != 1U) ? typename local_double_width_type::limb_type(1U)
: typename local_double_width_type::limb_type(0U));
my_x =
(
(my_m != unsigned { UINT8_C(1) })
? typename local_double_width_type::limb_type(1U)
: typename local_double_width_type::limb_type(0U)
);

my_done = true;
}
else if((local_limb_type(my_p) == 1U) && (my_p == 1U))
else if( (static_cast<local_limb_type>(my_p) == local_limb_type { UINT8_C(1) })
&& (my_p == unsigned { UINT8_C(1) }))
{
my_x = local_double_width_type(b) % local_double_width_type(m);

my_done = true;
}
else if((local_limb_type(my_p) == 2U) && (my_p == 2U))
else if( (static_cast<local_limb_type>(my_p) == local_limb_type { UINT8_C(2) })
&& (my_p == unsigned { UINT8_C(2) }))
{
my_y *= my_y;
my_y %= local_double_width_type(my_m);
my_y %= local_double_width_type { my_m };

my_x = my_y;

my_done = true;
}
}

bool get_done() const
auto get_done() const -> bool
{
return my_done;
}

void get_result(local_normal_width_type& result)
auto get_result(local_normal_width_type& result) -> void
{
result = local_normal_width_type(my_x);
}

void calculate()
auto calculate() -> void
{
// Calculate (b ^ p) % m.

Expand Down Expand Up @@ -104,11 +108,11 @@
}

private:
bool my_done;
local_double_width_type my_y;
local_double_width_type my_x;
local_normal_width_type my_p;
local_normal_width_type my_m;
bool my_done { };
local_double_width_type my_y { };
local_double_width_type my_x { };
local_normal_width_type my_p { };
local_normal_width_type my_m { };
};

template<typename Generator1Type,
Expand Down Expand Up @@ -155,21 +159,16 @@
calculate_state_done,
};


public:
constexpr miller_rabin_state(const typename base_class_type::generator1_type::result_type seed1 = typename base_class_type::generator1_type::result_type(),
const typename base_class_type::generator2_type::result_type seed2 = typename base_class_type::generator2_type::result_type())
: base_class_type(seed1, seed2),
my_search_state (search_state_type::search_state_set_n),
my_calculate_state(calculate_state_type::calculate_state_done),
my_i (0U),
my_j (0U),
my_y (),
my_powm () { }
my_calculate_state(calculate_state_type::calculate_state_done) { }

virtual ~miller_rabin_state() = default;
~miller_rabin_state() override = default;

virtual bool search()
auto search() -> bool override
{
bool search_is_done = false;

Expand Down Expand Up @@ -215,12 +214,12 @@
private:
search_state_type my_search_state;
calculate_state_type my_calculate_state;
std::uint_fast32_t my_i;
std::uint_fast32_t my_j;
typename base_class_type::wide_integer_type my_y;
miller_rabin_powm_type my_powm;
std::uint_fast32_t my_i { };
std::uint_fast32_t my_j { };
typename base_class_type::wide_integer_type my_y { };
miller_rabin_powm_type my_powm { };

void calculate()
auto calculate() -> void
{
switch(my_calculate_state)
{
Expand Down Expand Up @@ -287,6 +286,7 @@
else
{
base_class_type::my_n_m1 = base_class_type::my_n_trial;

--base_class_type::my_n_m1;

my_powm.init(typename base_class_type::wide_integer_type(typename base_class_type::limb_type(228U)),
Expand All @@ -307,13 +307,16 @@
}
else
{
typename base_class_type::wide_integer_type fn;
using local_wide_integer_type = typename base_class_type::wide_integer_type;

local_wide_integer_type fn;

my_powm.get_result(fn);

const typename base_class_type::limb_type fn0 = static_cast<typename base_class_type::limb_type>(fn);
using local_limb_type = typename local_wide_integer_type::limb_type;

if((fn0 != 1U) && (fn != 1U))
if( (static_cast<local_limb_type>(fn) != local_limb_type { UINT8_C(1) })
&& (fn != unsigned { UINT8_C(1) }))
{
base_class_type::my_n_trial_is_probably_prime = false;

Expand All @@ -332,7 +335,7 @@
// Prepare the random trials.
base_class_type::prepare_random_trials();

my_i = 0U;
my_i = unsigned { UINT8_C(0) };

my_calculate_state = calculate_state_type::calculate_state_next_powm_for_random_trials;

Expand All @@ -352,7 +355,7 @@

case calculate_state_type::calculate_state_next_y_for_random_trials:

if(my_powm.get_done() == false)
if(!my_powm.get_done())
{
my_powm.calculate();
}
Expand All @@ -374,7 +377,7 @@

if(trials_are_finished)
{
if(base_class_type::my_n_trial_is_probably_prime == false)
if(!base_class_type::my_n_trial_is_probably_prime)
{
my_calculate_state = calculate_state_type::calculate_state_done;
}
Expand All @@ -399,48 +402,54 @@
}
}

virtual bool execute_one_trial()
auto execute_one_trial() -> bool override
{
// TBD: Reduce the number of return points in this subroutine.
bool result = false;

if(my_y == base_class_type::my_n_m1)
{
return true;
result = true;
}

const typename base_class_type::limb_type y0(my_y);

if((y0 == 1U) && (my_y == 1U))
else
{
if(my_j != 0U)
{
base_class_type::my_n_trial_is_probably_prime = false;
}
using local_limb_type = typename base_class_type::limb_type;

return true;
}
if( (static_cast<local_limb_type>(my_y) == local_limb_type { UINT8_C(1) })
&& (my_y == unsigned { UINT8_C(1) }))
{
if(my_j != unsigned { UINT8_C(0) })
{
base_class_type::my_n_trial_is_probably_prime = false;
}

++my_j;
result = true;
}
else
{
++my_j;

if(my_j == base_class_type::my_k)
{
base_class_type::my_n_trial_is_probably_prime = false;
if(my_j == base_class_type::my_k)
{
base_class_type::my_n_trial_is_probably_prime = false;

return true;
}
else
{
using local_double_width_type = typename base_class_type::wide_integer_type::double_width_type;
result = true;
}
else
{
using local_wide_integer_type = typename base_class_type::wide_integer_type;
using local_double_width_type = typename local_wide_integer_type::double_width_type;

local_double_width_type yd(my_y);
local_double_width_type yd { my_y };

yd *= yd;
yd %= local_double_width_type(base_class_type::my_n_trial);
yd *= yd;
yd %= local_double_width_type { base_class_type::my_n_trial };

my_y = typename base_class_type::wide_integer_type(yd);
my_y = local_wide_integer_type { yd };
}
}
}

return false;
return result;
}
};

Expand Down

0 comments on commit f9f2008

Please sign in to comment.