Skip to content

Commit

Permalink
Handle issue 429
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Sep 27, 2024
1 parent 3c48e81 commit e0d4968
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/wide_integer_fuzzing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
clang++ -std=c++20 -g -O2 -fsanitize=fuzzer -I. -I../boost-root test/fuzzing/test_fuzzing_${{ matrix.tcase }}.cpp -o test_fuzzing_${{ matrix.tcase }}
echo "ls test_fuzzing_${{ matrix.tcase }}"
ls -la test_fuzzing_${{ matrix.tcase }}
./test_fuzzing_${{ matrix.tcase }} -max_total_time=240 -jobs=8
./test_fuzzing_${{ matrix.tcase }} -max_total_time=300 -jobs=8
29 changes: 12 additions & 17 deletions test/fuzzing/test_fuzzing_prime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/miller_rabin.hpp>

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <random>
#include <sstream>
#include <vector>

namespace fuzzing
{
Expand Down Expand Up @@ -65,32 +62,30 @@ auto fuzzing::eval_prime(const std::uint8_t* data, std::size_t size) -> bool
distribution_type distribution2;

local_uint_type p0 { 0U };
boost_uint_type pb { 0U };

// Import the random data into the prime candidate.
import_bits
(
p0,
data,
data + size
data + size,
8U
);

const bool miller_rabin_result_local { miller_rabin(p0, 25U, distribution2, generator2) };

auto from_string =
[](const local_uint_type& ui)
{
std::stringstream strm { };

strm << ui;

return boost_uint_type { strm.str() };
};

const boost_uint_type pb { std::move(from_string(p0)) };
// Import the random data into the boost control prime candidate.
import_bits
(
pb,
data,
data + size,
8U
);

// Ensure that both uintwide_t as well as boost obtain
// the same prime (or non-prime) result.

const bool miller_rabin_result_local { miller_rabin(p0, 25U, distribution2, generator2) };
const bool miller_rabin_result_boost { boost::multiprecision::miller_rabin_test(pb, 25U, generator2) };

const bool
Expand Down
91 changes: 89 additions & 2 deletions test/test_uintwide_t_spot_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,94 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <test/test_uintwide_t.h>
#include <math/wide_integer/uintwide_t.h>

#include <boost/multiprecision/cpp_int.hpp>

#include <algorithm>
#include <array>
#include <cassert>
#include <sstream>
#include <string>
#include <vector>

#include <math/wide_integer/uintwide_t.h>
#include <test/test_uintwide_t.h>
namespace from_issue_429
{
auto test_uintwide_t_spot_values_from_issue_429() -> bool
{
#if defined(WIDE_INTEGER_NAMESPACE)
using local_uint_type = WIDE_INTEGER_NAMESPACE::math::wide_integer::uint256_t;
#else
using local_uint_type = ::math::wide_integer::uint256_t;
#endif

using boost_uint_backend_type =
boost::multiprecision::cpp_int_backend<static_cast<unsigned>(UINT32_C(256)),
static_cast<unsigned>(UINT32_C(256)),
boost::multiprecision::unsigned_magnitude>;

using boost_uint_type = boost::multiprecision::number<boost_uint_backend_type,
boost::multiprecision::et_off>;


const std::vector<std::uint8_t>
input
(
{
0x00, 0x00, 0x00, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x21, 0x62,
0xff, 0xff, 0xff, 0xff,
0x21
}
);

local_uint_type p0_local { };
boost_uint_type p0_boost { };

// Import the data into the wide integer.
import_bits
(
p0_local,
input.cbegin(),
input.cend(),
8U
);

// Import the data into boost's cpp_int.
import_bits
(
p0_boost,
input.cbegin(),
input.cend(),
8U
);

std::stringstream strm_local { };
std::stringstream strm_boost { };

strm_local << std::hex << p0_local;
strm_boost << std::hex << p0_boost;

const std::string str_local { strm_local.str() };
const std::string str_boost { strm_boost.str() };

const bool result_import_is_ok { str_local == str_boost };

std::vector<std::uint8_t> export_local(input.size());
std::vector<std::uint8_t> export_boost(input.size());

export_bits(p0_local, export_local.begin(), 8U);
export_bits(p0_boost, export_boost.begin(), 8U);

const bool result_export_is_ok { export_local == export_boost };

const bool result_import_export_is_ok { result_import_is_ok && result_export_is_ok };

return result_import_export_is_ok;
}
} // namespace from_issue_429

namespace from_issue_362
{
Expand Down Expand Up @@ -651,6 +732,12 @@ auto local_test_spot_values::test() -> bool // NOLINT(readability-function-cogni
{
auto result_is_ok = true;

{
// See also: https://github.com/ckormanyos/wide-integer/issues/429

result_is_ok = (from_issue_429::test_uintwide_t_spot_values_from_issue_429() && result_is_ok);
}

{
// See also: https://github.com/ckormanyos/wide-integer/issues/362

Expand Down

0 comments on commit e0d4968

Please sign in to comment.