Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle issue 429 #430

Merged
merged 4 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
68 changes: 66 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,71 @@
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

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

#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

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 { };

// Import the data into the wide integer.
import_bits
(
p0_local,
input.cbegin(),
input.cend(),
static_cast<unsigned>(UINT8_C(8))
);

std::stringstream strm_local { };

strm_local << std::hex << p0_local;

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

const bool result_import_is_ok { str_local == "ffffffffffffff2162ffffffff21" };

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

export_bits(p0_local, export_local.begin(), static_cast<unsigned>(UINT8_C(8)));

const bool result_export_is_ok
{
export_local == std::vector<std::uint8_t>( { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x21, 0x62, 0xff, 0xff, 0xff, 0xff, 0x21, 0x00, 0x00, 0x00 } )
};

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 +709,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