forked from EvergreenCrypto/docker-finance
container: root: src: add bitcoin plugin support
- Adds bitcoin plugin support to `Random` specialization/implementation * Currently only supports one of bitcoin's RNG's (`FastRandomContext`)
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
|
||||
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
|
||||
//! \since docker-finance 1.0.0
|
||||
//! \todo C++20 refactor
|
||||
|
||||
#ifndef CONTAINER_SRC_ROOT_SRC_INTERNAL_IMPL_RANDOM_HH_
|
||||
#define CONTAINER_SRC_ROOT_SRC_INTERNAL_IMPL_RANDOM_HH_
|
||||
@@ -259,6 +260,69 @@ class Random : public common::RandomImpl<libsodium::Random>
|
||||
};
|
||||
} // namespace libsodium
|
||||
|
||||
#ifdef __DFI_PLUGIN_BITCOIN__
|
||||
//! \namespace dfi::crypto::impl::bitcoin
|
||||
//! \since docker-finance 1.1.0
|
||||
namespace bitcoin
|
||||
{
|
||||
//! \concept dfi::crypto::impl::bitcoin::RType
|
||||
//! \brief Random number type
|
||||
//! \details Requirements include that "interface" specialization
|
||||
//! has not changed and that given type is supported by bitcoin.
|
||||
//! \since docker-finance 1.1.0
|
||||
template <typename t_random>
|
||||
concept RType =
|
||||
::dfi::internal::type::is_real_integral<t_random>::value
|
||||
&& (std::same_as<t_random, uint32_t> || std::same_as<t_random, uint64_t>);
|
||||
|
||||
//! \brief Implements Random API with bitcoin
|
||||
//! \ingroup cpp_API_impl
|
||||
//! \since docker-finance 1.1.0
|
||||
//! \todo span/bytes
|
||||
//! \todo reseed
|
||||
//! \todo insecure RNG option
|
||||
class Random : public common::RandomImpl<bitcoin::Random>
|
||||
{
|
||||
public:
|
||||
Random() = default;
|
||||
~Random() = default;
|
||||
|
||||
Random(const Random&) = delete;
|
||||
Random& operator=(const Random&) = delete;
|
||||
|
||||
Random(Random&&) = delete;
|
||||
Random& operator=(Random&&) = delete;
|
||||
|
||||
private:
|
||||
//! \brief Implements random number generator
|
||||
template <RType t_random>
|
||||
t_random generate_impl()
|
||||
{
|
||||
if constexpr (std::same_as<t_random, uint32_t>)
|
||||
{
|
||||
return m_ctx.rand32();
|
||||
}
|
||||
else if constexpr (std::same_as<t_random, uint64_t>)
|
||||
{
|
||||
return m_ctx.rand64();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
//! \brief Generate random number
|
||||
template <RType t_random>
|
||||
t_random generate()
|
||||
{
|
||||
return this->generate_impl<t_random>();
|
||||
}
|
||||
|
||||
private:
|
||||
//! \warning Bitcoin's RNG is not thread-safe (but dfi's CRTP provides external locks, by default)
|
||||
::FastRandomContext m_ctx;
|
||||
};
|
||||
} // namespace bitcoin
|
||||
#endif
|
||||
|
||||
} // namespace impl
|
||||
} // namespace crypto
|
||||
} // namespace dfi
|
||||
|
||||
@@ -118,6 +118,20 @@ namespace libsodium
|
||||
using Random = ::dfi::crypto::common::Random<impl::libsodium::Random>;
|
||||
} // namespace libsodium
|
||||
|
||||
#ifdef __DFI_PLUGIN_BITCOIN__
|
||||
//! \namespace dfi::crypto::bitcoin
|
||||
//! \brief Public-facing API namespace (bitcoin)
|
||||
//! \since docker-finance 1.1.0
|
||||
namespace bitcoin
|
||||
{
|
||||
//! \brief bitcoin Random API specialization ("interface" / implementation)
|
||||
//! \note For public consumption
|
||||
//! \ingroup cpp_API
|
||||
//! \since docker-finance 1.1.0
|
||||
using Random = ::dfi::crypto::common::Random<impl::bitcoin::Random>;
|
||||
} // namespace bitcoin
|
||||
#endif
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace dfi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user