Skip to content

Commit c725651

Browse files
authored
Merge pull request #440 from ckormanyos/simplify_miller_rabin
2 parents fbf0b37 + 0cabd6d commit c725651

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

math/wide_integer/uintwide_t.h

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7133,72 +7133,106 @@
71337133

71347134
const auto nm1 = static_cast<local_wide_integer_type>(np - static_cast<unsigned>(UINT8_C(1)));
71357135

7136+
auto
7137+
isone
7138+
{
7139+
[](const local_wide_integer_type& t1)
7140+
{
7141+
return
7142+
(
7143+
(static_cast<local_limb_type>(t1) == local_limb_type { UINT8_C(1) })
7144+
&& (t1 == unsigned { UINT8_C(1) })
7145+
);
7146+
}
7147+
};
7148+
71367149
// Since we have already excluded all small factors
71377150
// up to and including 227, n is greater than 227.
71387151

71397152
{
71407153
// Perform a single Fermat test which will
71417154
// exclude many non-prime candidates.
71427155

7143-
const local_wide_integer_type fn = powm(local_wide_integer_type(static_cast<local_limb_type>(228U)), nm1, np);
7144-
7145-
const auto fn0 = static_cast<local_limb_type>(fn);
7156+
const local_wide_integer_type fn { powm(local_wide_integer_type(static_cast<local_limb_type>(228U)), nm1, np) };
71467157

7147-
if((fn0 != static_cast<local_limb_type>(UINT8_C(1))) && (fn != 1U))
7158+
if(!isone(fn))
71487159
{
71497160
return false;
71507161
}
71517162
}
71527163

7153-
const unsigned_fast_type k { lsb(nm1) };
7164+
const unsigned k { static_cast<unsigned>(lsb(nm1)) };
71547165

71557166
const local_wide_integer_type q { nm1 >> k };
71567167

71577168
using local_param_type = typename DistributionType::param_type;
71587169

7159-
const local_param_type params(local_wide_integer_type(2U), np - 2U);
7170+
const local_param_type
7171+
params
7172+
{
7173+
local_wide_integer_type { unsigned { UINT8_C(2) } },
7174+
np - unsigned { UINT8_C(2) }
7175+
};
71607176

7161-
local_wide_integer_type x { };
7162-
local_wide_integer_type y { };
7177+
local_wide_integer_type x;
7178+
local_wide_integer_type y;
71637179

71647180
// Assume the test will pass, even though it usually does not pass.
71657181
bool result { true };
71667182

71677183
// Loop over the trials to perform the primality testing.
71687184

7169-
for(std::size_t idx { 0U }; ((idx < number_of_trials) && result); ++idx) // NOLINT(altera-id-dependent-backward-branch)
7185+
std::size_t idx { UINT8_C(0) };
7186+
7187+
do
71707188
{
71717189
x = distribution(generator, params);
71727190
y = powm(x, q, np);
71737191

7174-
std::size_t jdx { 0U };
7192+
using local_double_width_type = typename local_wide_integer_type::double_width_type;
7193+
7194+
const local_double_width_type np_dbl { np };
7195+
7196+
std::size_t jdx { UINT8_C(0) };
71757197

71767198
// Continue while y is not nm1, and while y is not 1,
71777199
// and while the result is true.
71787200

7179-
while((y != nm1) && (y != 1U) && result) // NOLINT(altera-id-dependent-backward-branch)
7201+
while((y != nm1) && (!isone(y)) && result) // NOLINT(altera-id-dependent-backward-branch)
71807202
{
71817203
++jdx;
71827204

7183-
if (std::size_t { jdx } == k)
7205+
if(jdx == static_cast<std::size_t>(k))
71847206
{
71857207
// Mark failure if max iterations reached.
71867208
result = false;
71877209
}
71887210
else
71897211
{
71907212
// Continue with the next value of y.
7191-
y = powm(y, 2, np);
7213+
7214+
// Manually calculate:
7215+
// y = powm(y, 2, np);
7216+
7217+
local_double_width_type yd { y };
7218+
7219+
yd *= yd;
7220+
yd %= np_dbl;
7221+
7222+
y = local_wide_integer_type { yd };
71927223
}
71937224
}
71947225

71957226
// Check for (y == 1) after the loop.
7196-
if((y == 1U) && (jdx != std::size_t { 0U }))
7227+
if(isone(y) && (jdx != std::size_t { UINT8_C(0) }))
71977228
{
71987229
// Mark failure if (y == 1) and (jdx != 0).
71997230
result = false;
72007231
}
7232+
7233+
++idx;
72017234
}
7235+
while((idx < number_of_trials) && result);
72027236

72037237
return result;
72047238
}

wide_integer_vs2022.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@
7777
</PropertyGroup>
7878
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
7979
<LinkIncremental>true</LinkIncremental>
80-
<IncludePath>$(ProjectDir);C:\boost\boost_1_85_0;$(IncludePath)</IncludePath>
80+
<IncludePath>$(ProjectDir);C:\boost\boost_1_86_0;$(IncludePath)</IncludePath>
8181
</PropertyGroup>
8282
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
8383
<LinkIncremental>false</LinkIncremental>
8484
<IncludePath>$(ProjectDir);C:\boost\boost_1_85_0;$(IncludePath)</IncludePath>
8585
</PropertyGroup>
8686
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
8787
<LinkIncremental>false</LinkIncremental>
88-
<IncludePath>$(ProjectDir);C:\boost\boost_1_85_0;$(IncludePath)</IncludePath>
88+
<IncludePath>$(ProjectDir);C:\boost\boost_1_86_0;$(IncludePath)</IncludePath>
8989
</PropertyGroup>
9090
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
9191
<ClCompile>

0 commit comments

Comments
 (0)