Skip to content

Commit

Permalink
Merge pull request #168 from cppalliance/consteval_testing
Browse files Browse the repository at this point in the history
Add consteval testing
  • Loading branch information
mborland authored Jan 9, 2025
2 parents a0bf263 + 966182f commit a5e7692
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 35 deletions.
16 changes: 5 additions & 11 deletions fuzzing/fuzz_sha224.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha224.hpp>
#include <boost/crypt2/hash/sha224.hpp>
#include <iostream>
#include <exception>
#include <string>
Expand All @@ -15,26 +15,20 @@ extern "C" int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size
std::string c_data_str {c_data, size}; // Guarantee null termination since we can't pass the size argument

boost::crypt::sha224(c_data_str);
boost::crypt::sha224(c_data, size);
boost::crypt::sha224(data, size);

#ifdef BOOST_CRYPT_HAS_STRING_VIEW
std::string_view view {c_data_str};
boost::crypt::sha224(view);
#endif

#ifdef BOOST_CRYPT_HAS_SPAN
std::span data_span {c_data, size};
boost::crypt::sha224(data_span);
#endif

// Fuzz the hasher object
boost::crypt::sha224_hasher hasher;
hasher.process_bytes(data, size);
hasher.process_bytes(data, size);
hasher.process_bytes(data, size);
hasher.process_bytes(data_span);
hasher.process_bytes(data_span);
hasher.process_bytes(data_span);
hasher.get_digest();
hasher.process_bytes(data, size); // State is invalid but should not crash
hasher.process_bytes(data_span); // State is invalid but should not crash
}
catch(...)
{
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/fuzz_md5.cpp → fuzzing/old/fuzz_md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define BOOST_CRYPT_ENABLE_MD5

#include <boost/crypt/hash/md5.hpp>
#include "boost/crypt/hash/md5.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/fuzz_sha256.cpp → fuzzing/old/fuzz_sha256.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha256.hpp>
#include "boost/crypt/hash/sha256.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/fuzz_sha384.cpp → fuzzing/old/fuzz_sha384.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha384.hpp>
#include "boost/crypt/hash/sha384.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha3_224.hpp>
#include "boost/crypt/hash/sha3_224.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha3_256.hpp>
#include "boost/crypt/hash/sha3_256.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha3_384.hpp>
#include "boost/crypt/hash/sha3_384.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha3_512.hpp>
#include "boost/crypt/hash/sha3_512.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
2 changes: 1 addition & 1 deletion fuzzing/fuzz_sha512.cpp → fuzzing/old/fuzz_sha512.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha512.hpp>
#include "boost/crypt/hash/sha512.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha512_224.hpp>
#include "boost/crypt/hash/sha512_224.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/sha512_256.hpp>
#include "boost/crypt/hash/sha512_256.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/drbg/sha512_drbg.hpp>
#include "boost/crypt/drbg/sha512_drbg.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/drbg/sha512_drbg.hpp>
#include "boost/crypt/drbg/sha512_drbg.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/shake128.hpp>
#include "boost/crypt/hash/shake128.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/crypt/hash/shake256.hpp>
#include "boost/crypt/hash/shake256.hpp"
#include <iostream>
#include <exception>
#include <string>
Expand Down
2 changes: 2 additions & 0 deletions include/boost/crypt2/hash/detail/sha224_256_hasher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class sha_224_256_hasher final : public sha_1_2_hasher_base<digest_size, 8U>

BOOST_CRYPT_GPU_ENABLED_CONSTEXPR sha_224_256_hasher() noexcept { init(is_sha224()); }

BOOST_CRYPT_GPU_ENABLED_CONSTEXPR ~sha_224_256_hasher() noexcept override { base_class::base_destroy(); };

BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto init() noexcept { init(is_sha224()); }
};

Expand Down
4 changes: 2 additions & 2 deletions include/boost/crypt2/hash/detail/sha_1_2_hasher_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class sha_1_2_hasher_base
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto get_digest(compat::span<compat::byte, digest_size> data) noexcept -> state;

template <concepts::writable_output_range Range>
BOOST_CRYPT_GPU_ENABLED auto get_digest(Range&& data) noexcept -> void;
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto get_digest(Range&& data) noexcept -> void;

BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto base_init() noexcept -> void;
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto base_destroy() noexcept -> void;
};

template <compat::size_t digest_size, compat::size_t intermediate_hash_size>
template <concepts::writable_output_range Range>
auto sha_1_2_hasher_base<digest_size, intermediate_hash_size>::get_digest(Range&& data) noexcept -> void
BOOST_CRYPT_GPU_ENABLED_CONSTEXPR auto sha_1_2_hasher_base<digest_size, intermediate_hash_size>::get_digest(Range&& data) noexcept -> void
{
using value_type = compat::range_value_t<Range>;

Expand Down
2 changes: 1 addition & 1 deletion test/nvcc_jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ project : requirements
;

run test_sha1_nvcc.cu ;
#run test_sha224_nvcc.cu ;
run test_sha224_nvcc.cu ;
#run test_sha256_nvcc.cu ;
#run test_sha384_nvcc.cu ;
#run test_sha512_nvcc.cu ;
Expand Down
35 changes: 35 additions & 0 deletions test/test_sha1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,36 @@ void files_test()
BOOST_TEST_THROWS(boost::crypt::sha1_file(bad_path), std::runtime_error);
}

consteval bool immediate_test()
{
constexpr std::array<std::byte, 3> vals = {std::byte{0x61}, std::byte{0x62}, std::byte{0x63}};
constexpr std::array<std::byte, 20> expected_res = {
std::byte{0xA9}, std::byte{0x99}, std::byte{0x3E}, std::byte{0x36}, std::byte{0x47}, std::byte{0x06},
std::byte{0x81}, std::byte{0x6A}, std::byte{0xBA}, std::byte{0x3E}, std::byte{0x25}, std::byte{0x71},
std::byte{0x78}, std::byte{0x50}, std::byte{0xC2}, std::byte{0x6C}, std::byte{0x9C}, std::byte{0xD0},
std::byte{0xD8}, std::byte{0x9D}
};

const std::span<const std::byte> byte_span {vals};

boost::crypt::sha1_hasher hasher;
hasher.init();
hasher.process_bytes(byte_span);
const auto res = hasher.get_digest();

bool correct {true};
for (std::size_t i {}; i < res.size(); ++i)
{
if (res[i] != expected_res[i])
{
correct = false;
break;
}
}

return correct;
}

int main()
{
basic_tests();
Expand All @@ -262,5 +292,10 @@ int main()
files_test();
#endif

// GCC-14 has an internal compiler error here
#if defined(__GNUC__) && __GNUC__ != 14
static_assert(immediate_test());
#endif

return boost::report_errors();
}
35 changes: 35 additions & 0 deletions test/test_sha224.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,36 @@ void files_test()
BOOST_TEST_THROWS(boost::crypt::sha224_file(bad_path), std::runtime_error);
}

consteval bool immediate_test()
{
constexpr std::array<std::byte, 3> vals = {std::byte{0x61}, std::byte{0x62}, std::byte{0x63}};
constexpr std::array<std::byte, 28> expected_res = {
std::byte{0x23}, std::byte{0x09}, std::byte{0x7d}, std::byte{0x22}, std::byte{0x34}, std::byte{0x05},
std::byte{0xd8}, std::byte{0x22}, std::byte{0x86}, std::byte{0x42}, std::byte{0xa4}, std::byte{0x77},
std::byte{0xbd}, std::byte{0xa2}, std::byte{0x55}, std::byte{0xb3}, std::byte{0x2a}, std::byte{0xad},
std::byte{0xbc}, std::byte{0xe4}, std::byte{0xbd}, std::byte{0xa0}, std::byte{0xb3}, std::byte{0xf7},
std::byte{0xe3}, std::byte{0x6c}, std::byte{0x9d}, std::byte{0xa7}
};
std::span<const std::byte> byte_span {vals};

boost::crypt::sha224_hasher hasher;
hasher.init();
hasher.process_bytes(byte_span);
const auto res = hasher.get_digest();

bool correct {true};
for (std::size_t i {}; i < res.size(); ++i)
{
if (res[i] != expected_res[i])
{
correct = false;
break;
}
}

return correct;
}

int main()
{
basic_tests();
Expand All @@ -255,5 +285,10 @@ int main()
files_test();
#endif

// GCC-14 has an internal compiler error here
#if defined(__GNUC__) && __GNUC__ != 14
static_assert(immediate_test());
#endif

return boost::report_errors();
}
16 changes: 9 additions & 7 deletions test/test_sha224_nvcc.cu
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
// Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/crypt/hash/sha224.hpp>
#include <cuda_runtime.h>
#include <boost/crypt2/hash/sha224.hpp>
#include "cuda_managed_ptr.hpp"
#include "stopwatch.hpp"
#include "generate_random_strings.hpp"
#include <iostream>
#include <iomanip>
#include <exception>
#include <memory>
#include <span>

#include <cuda_runtime.h>

using digest_type = boost::crypt::sha224_hasher::return_type;
using digest_type = cuda::std::array<cuda::std::byte, 28>;

// The kernel function
__global__ void cuda_test(char** in, digest_type* out, int numElements)
Expand All @@ -23,7 +23,8 @@ __global__ void cuda_test(char** in, digest_type* out, int numElements)

if (i < numElements)
{
out[i] = boost::crypt::sha224(in[i]);
auto in_span {cuda::std::span(in[i], 64)};
out[i] = boost::crypt::sha224(in_span);
}
}

Expand Down Expand Up @@ -59,7 +60,7 @@ int main()

// Launch the Vector Add CUDA Kernel
int threadsPerBlock = 256;
int blocksPerGrid =(numElements + threadsPerBlock - 1) / threadsPerBlock;
int blocksPerGrid = (numElements + threadsPerBlock - 1) / threadsPerBlock;
std::cout << "CUDA kernel launch with " << blocksPerGrid << " blocks of " << threadsPerBlock << " threads" << std::endl;

watch w;
Expand All @@ -80,7 +81,8 @@ int main()
w.reset();
for(int i = 0; i < numElements; ++i)
{
results.emplace_back(boost::crypt::sha224(input_vector1[i]));
std::span<char> in(input_vector1[i], elementSize);
results.emplace_back(boost::crypt::sha224(in));
}
double t = w.elapsed();

Expand Down

0 comments on commit a5e7692

Please sign in to comment.