From 879eb6f785c5fef696d4dbdd73c2f5c83569f32c Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Tue, 23 Jul 2024 16:00:01 -0700 Subject: [PATCH] root: src: Random: support multiple types Allows signed/alternative-unsigned types (impl-specific). --- .../src/root/src/internal/impl/random.hh | 34 +++++++++++++++---- container/src/root/src/random.hh | 15 ++++++-- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/container/src/root/src/internal/impl/random.hh b/container/src/root/src/internal/impl/random.hh index 8416868..560cd37 100644 --- a/container/src/root/src/internal/impl/random.hh +++ b/container/src/root/src/internal/impl/random.hh @@ -54,6 +54,9 @@ namespace impl //! \since docker-finance 1.0.0 namespace common { + +namespace type = docker_finance::internal::type; + //! \brief Generic Random implementation common among all library-specific implementations //! \ingroup cpp_API_impl //! \since docker-finance 1.0.0 @@ -76,15 +79,14 @@ class RandomImpl : public ::docker_finance::internal::Random t_random generate_impl() { static_assert( - std::is_same_v, "Random interface has changed"); + type::is_real_integral::value, + "Random interface has changed"); return t_impl::that()->template generate(); } }; } // namespace common -namespace type = docker_finance::internal::type; - //! \namespace docker_finance::crypto::impl::botan //! \since docker-finance 1.0.0 namespace botan @@ -118,7 +120,13 @@ class Random : public common::RandomImpl t_random generate_impl() { static_assert( - std::is_same_v, "Random interface has changed"); + type::is_real_integral::value, + "Random interface has changed"); + + static_assert( + std::is_same_v + || std::is_same_v, + "Invalid type (only uint16_t or uint32_t supported)"); THROW_IF(!m_csprng.is_seeded(), type::RuntimeError, "Botan is not seeded") @@ -170,8 +178,15 @@ class Random : public common::RandomImpl template t_random generate_impl() { + // NOTE: signed/unsigned types are supported (up to uint32_t) static_assert( - std::is_same_v, "Random interface has changed"); + type::is_real_integral::value, + "Random interface has changed"); + + static_assert( + !std::is_same_v + && !std::is_same_v, + "Invalid type (no greater than uint32_t allowed)"); return m_csprng.GenerateWord32(0, std::numeric_limits::max()); } @@ -226,7 +241,14 @@ class Random : public common::RandomImpl t_random generate_impl() { static_assert( - std::is_same_v, "Random interface has changed"); + type::is_real_integral::value, + "Random interface has changed"); + + // signed or uint64_t types are not supported + static_assert( + std::is_same_v + || std::is_same_v, + "Invalid type (only uint16_t or uint32_t supported)"); return ::randombytes_random(); } diff --git a/container/src/root/src/random.hh b/container/src/root/src/random.hh index 5e8686e..4048538 100644 --- a/container/src/root/src/random.hh +++ b/container/src/root/src/random.hh @@ -25,6 +25,7 @@ #define CONTAINER_SRC_ROOT_SRC_RANDOM_HH_ #include +#include #include "./internal/impl/random.hh" @@ -64,9 +65,17 @@ class Random final : public t_impl public: //! \brief Random number generator - //! \details Returns a random value between 0 and 0xFFFFFFFF - //! \todo May as well enable_if uint32_t for consistency - uint32_t generate() { return t_impl::template generate(); } + //! \details Returns a random value between 0 and 0xFFFFFFFF (depending on type) + //! \warning Signed types will return positive value (per implementation) + //! \tparam t_random Integral type of random number + //! \return t_random Random number of type t_random + template < + typename t_random = uint32_t, + std::enable_if_t::value, bool> = true> + t_random generate() + { + return t_impl::template generate(); + } }; } // namespace common