From 2268ae80830e2d553a8b97f7c675e268c9b4ad0e Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Fri, 14 Nov 2025 12:32:12 -0800 Subject: [PATCH] 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`) --- .../src/root/src/internal/impl/random.hh | 64 +++++++++++++++++++ container/src/root/src/random.hh | 14 ++++ 2 files changed, 78 insertions(+) diff --git a/container/src/root/src/internal/impl/random.hh b/container/src/root/src/internal/impl/random.hh index bb158ab..877fb25 100644 --- a/container/src/root/src/internal/impl/random.hh +++ b/container/src/root/src/internal/impl/random.hh @@ -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 }; } // 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 +concept RType = + ::dfi::internal::type::is_real_integral::value + && (std::same_as || std::same_as); + +//! \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 +{ + 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 + t_random generate_impl() + { + if constexpr (std::same_as) + { + return m_ctx.rand32(); + } + else if constexpr (std::same_as) + { + return m_ctx.rand64(); + } + } + + public: + //! \brief Generate random number + template + t_random generate() + { + return this->generate_impl(); + } + + 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 diff --git a/container/src/root/src/random.hh b/container/src/root/src/random.hh index 2eeb56a..ec20839 100644 --- a/container/src/root/src/random.hh +++ b/container/src/root/src/random.hh @@ -118,6 +118,20 @@ namespace libsodium using Random = ::dfi::crypto::common::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; +} // namespace bitcoin +#endif + } // namespace crypto } // namespace dfi