|
7133 | 7133 |
|
7134 | 7134 | const auto nm1 = static_cast<local_wide_integer_type>(np - static_cast<unsigned>(UINT8_C(1))); |
7135 | 7135 |
|
| 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 | + |
7136 | 7149 | // Since we have already excluded all small factors |
7137 | 7150 | // up to and including 227, n is greater than 227. |
7138 | 7151 |
|
7139 | 7152 | { |
7140 | 7153 | // Perform a single Fermat test which will |
7141 | 7154 | // exclude many non-prime candidates. |
7142 | 7155 |
|
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) }; |
7146 | 7157 |
|
7147 | | - if((fn0 != static_cast<local_limb_type>(UINT8_C(1))) && (fn != 1U)) |
| 7158 | + if(!isone(fn)) |
7148 | 7159 | { |
7149 | 7160 | return false; |
7150 | 7161 | } |
7151 | 7162 | } |
7152 | 7163 |
|
7153 | | - const unsigned_fast_type k { lsb(nm1) }; |
| 7164 | + const unsigned k { static_cast<unsigned>(lsb(nm1)) }; |
7154 | 7165 |
|
7155 | 7166 | const local_wide_integer_type q { nm1 >> k }; |
7156 | 7167 |
|
7157 | 7168 | using local_param_type = typename DistributionType::param_type; |
7158 | 7169 |
|
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 | + }; |
7160 | 7176 |
|
7161 | | - local_wide_integer_type x { }; |
7162 | | - local_wide_integer_type y { }; |
| 7177 | + local_wide_integer_type x; |
| 7178 | + local_wide_integer_type y; |
7163 | 7179 |
|
7164 | 7180 | // Assume the test will pass, even though it usually does not pass. |
7165 | 7181 | bool result { true }; |
7166 | 7182 |
|
7167 | 7183 | // Loop over the trials to perform the primality testing. |
7168 | 7184 |
|
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 |
7170 | 7188 | { |
7171 | 7189 | x = distribution(generator, params); |
7172 | 7190 | y = powm(x, q, np); |
7173 | 7191 |
|
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) }; |
7175 | 7197 |
|
7176 | 7198 | // Continue while y is not nm1, and while y is not 1, |
7177 | 7199 | // and while the result is true. |
7178 | 7200 |
|
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) |
7180 | 7202 | { |
7181 | 7203 | ++jdx; |
7182 | 7204 |
|
7183 | | - if (std::size_t { jdx } == k) |
| 7205 | + if(jdx == static_cast<std::size_t>(k)) |
7184 | 7206 | { |
7185 | 7207 | // Mark failure if max iterations reached. |
7186 | 7208 | result = false; |
7187 | 7209 | } |
7188 | 7210 | else |
7189 | 7211 | { |
7190 | 7212 | // 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 }; |
7192 | 7223 | } |
7193 | 7224 | } |
7194 | 7225 |
|
7195 | 7226 | // 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) })) |
7197 | 7228 | { |
7198 | 7229 | // Mark failure if (y == 1) and (jdx != 0). |
7199 | 7230 | result = false; |
7200 | 7231 | } |
| 7232 | + |
| 7233 | + ++idx; |
7201 | 7234 | } |
| 7235 | + while((idx < number_of_trials) && result); |
7202 | 7236 |
|
7203 | 7237 | return result; |
7204 | 7238 | } |
|
0 commit comments