Skip to content

Commit

Permalink
multiprecision: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ioxid committed Dec 14, 2024
1 parent 1ebae56 commit 8d11094
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once

#include <boost/functional/hash.hpp>
#include <climits>
#include <cmath>
#include <cstring>
#include <ostream>
#include <string>
#include <type_traits>

#include <boost/functional/hash.hpp>

#include "nil/crypto3/multiprecision/detail/assert.hpp"
#include "nil/crypto3/multiprecision/detail/big_uint/big_uint_impl.hpp"
#include "nil/crypto3/multiprecision/detail/config.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <algorithm>
#include <array>
#include <bit>
#include <boost/functional/hash.hpp>
#include <cctype>
#include <charconv>
#include <climits>
Expand All @@ -19,9 +18,11 @@
#include <system_error>
#include <type_traits>

#include <boost/functional/hash.hpp>

#include "nil/crypto3/multiprecision/detail/assert.hpp"
#include "nil/crypto3/multiprecision/detail/big_uint/arithmetic.hpp"
#include "nil/crypto3/multiprecision/detail/big_uint/parsing.hpp"
#include "nil/crypto3/multiprecision/detail/big_uint/parsing.hpp" // IWYU pragma: export
#include "nil/crypto3/multiprecision/detail/big_uint/storage.hpp"
#include "nil/crypto3/multiprecision/detail/big_uint/type_traits.hpp" // IWYU pragma: export
#include "nil/crypto3/multiprecision/detail/config.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace nil::crypto3::multiprecision {
bit_location = limb_bits - bit_location;

NIL_CO3_MP_ASSERT(
!((bit_location < sizeof(bits) * CHAR_BIT) && (bits >>= bit_location)));
!((bit_location < sizeof(bits) * CHAR_BIT) && (bits >> bit_location)));
}

template<std::size_t Bits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

#pragma once

#include <boost/random.hpp>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <limits>
#include <random>
#include <type_traits>

#include <boost/random.hpp>

#include "nil/crypto3/multiprecision/big_uint.hpp"
#include "nil/crypto3/multiprecision/detail/assert.hpp"
#include "nil/crypto3/multiprecision/integer.hpp"
Expand All @@ -33,7 +35,9 @@ namespace nil::crypto3::multiprecision {

for (std::size_t i = 0; i < sizeof(small_factors1) / sizeof(small_factors1[0]); ++i) {
NIL_CO3_MP_ASSERT(pp1 % small_factors1[i] == 0);
if (m1 % small_factors1[i] == 0) return false;
if (m1 % small_factors1[i] == 0) {
return false;
}
}

constexpr std::uint32_t small_factors2[] = {29u, 31u, 37u, 41u, 43u, 47u};
Expand All @@ -43,7 +47,9 @@ namespace nil::crypto3::multiprecision {

for (std::size_t i = 0; i < sizeof(small_factors2) / sizeof(small_factors2[0]); ++i) {
NIL_CO3_MP_ASSERT(pp2 % small_factors2[i] == 0);
if (m1 % small_factors2[i] == 0) return false;
if (m1 % small_factors2[i] == 0) {
return false;
}
}

constexpr std::uint32_t small_factors3[] = {53u, 59u, 61u, 67u, 71u};
Expand All @@ -53,7 +59,9 @@ namespace nil::crypto3::multiprecision {

for (std::size_t i = 0; i < sizeof(small_factors3) / sizeof(small_factors3[0]); ++i) {
NIL_CO3_MP_ASSERT(pp3 % small_factors3[i] == 0);
if (m1 % small_factors3[i] == 0) return false;
if (m1 % small_factors3[i] == 0) {
return false;
}
}

constexpr std::uint32_t small_factors4[] = {73u, 79u, 83u, 89u, 97u};
Expand All @@ -63,7 +71,9 @@ namespace nil::crypto3::multiprecision {

for (std::size_t i = 0; i < sizeof(small_factors4) / sizeof(small_factors4[0]); ++i) {
NIL_CO3_MP_ASSERT(pp4 % small_factors4[i] == 0);
if (m1 % small_factors4[i] == 0) return false;
if (m1 % small_factors4[i] == 0) {
return false;
}
}

constexpr std::uint32_t small_factors5[6][4] = {
Expand All @@ -76,12 +86,14 @@ namespace nil::crypto3::multiprecision {
181u * 191u * 193u * 197u,
199u * 211u * 223u * 227u};

for (std::size_t k = 0; k < sizeof(pp5) / sizeof(*pp5); ++k) {
for (std::size_t k = 0; k < std::size(pp5); ++k) {
m1 = integer_modulus(n, pp5[k]);

for (std::size_t i = 0; i < 4; ++i) {
NIL_CO3_MP_ASSERT(pp5[k] % small_factors5[k][i] == 0);
if (m1 % small_factors5[k][i] == 0) return false;
if (m1 % small_factors5[k][i] == 0) {
return false;
}
}
}
return true;
Expand All @@ -93,8 +105,10 @@ namespace nil::crypto3::multiprecision {
43u, 47u, 53u, 59u, 61u, 67u, 71u, 73u, 79u, 83u, 89u, 97u,
101u, 103u, 107u, 109u, 113u, 127u, 131u, 137u, 139u, 149u, 151u, 157u,
163u, 167u, 173u, 179u, 181u, 191u, 193u, 197u, 199u, 211u, 223u, 227u};
for (std::size_t i = 0; i < sizeof(p) / sizeof(*p); ++i) {
if (n == p[i]) return true;
for (std::size_t i = 0; i < std::size(p); ++i) {
if (n == p[i]) {
return true;
}
}
return false;
}
Expand All @@ -105,11 +119,19 @@ namespace nil::crypto3::multiprecision {
const I& n, std::size_t trials, Engine& gen) {
using number_type = I;

if (n == 2) return true; // Trivial special case.
if (bit_test(n, 0) == 0) return false; // n is even
if (n <= 227) return detail::is_small_prime(static_cast<unsigned>(n));
if (n == 2) {
return true; // Trivial special case.
}
if (bit_test(n, 0) == 0) {
return false; // n is even
}
if (n <= 227) {
return detail::is_small_prime(static_cast<unsigned>(n));
}

if (!detail::check_small_factors(n)) return false;
if (!detail::check_small_factors(n)) {
return false;
}

number_type nm1 = n - 1u;
//
Expand All @@ -118,7 +140,9 @@ namespace nil::crypto3::multiprecision {
number_type q(228), x,
y; // We know n is greater than this, as we've excluded small factors
x = powm(q, nm1, n);
if (x != 1u) return false;
if (x != 1u) {
return false;
}

q = n - 1u;
std::size_t k = lsb(q);
Expand All @@ -135,12 +159,18 @@ namespace nil::crypto3::multiprecision {
y = powm(x, q, n);
std::size_t j = 0;
while (true) {
if (y == nm1) break;
if (y == nm1) {
break;
}
if (y == 1) {
if (j == 0) break;
if (j == 0) {
break;
}
return false; // test failed
}
if (++j == k) return false; // failed
if (++j == k) {
return false; // failed
}
y = powm(y, 2, n);
}
}
Expand Down

0 comments on commit 8d11094

Please sign in to comment.