From a81ba56f1b39d2cc0ae72f7e034fa8fd47f2f4b2 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 23 Apr 2024 04:41:35 +0300 Subject: [PATCH] Use chacha20_12 in random_generator; update definitions of random_generator_mt19937 and random_generator_pure --- include/boost/uuid/detail/chacha20.hpp | 11 +++ include/boost/uuid/random_generator.hpp | 72 ++++--------------- test/Jamfile.v2 | 3 - .../random_generator_no_copy_assign.cpp | 17 ----- .../random_generator_no_copy_ctor.cpp | 17 ----- test/test_comparison.cpp | 2 +- 6 files changed, 27 insertions(+), 95 deletions(-) delete mode 100644 test/compile-fail/random_generator_no_copy_assign.cpp delete mode 100644 test/compile-fail/random_generator_no_copy_ctor.cpp diff --git a/include/boost/uuid/detail/chacha20.hpp b/include/boost/uuid/detail/chacha20.hpp index d1588e10..a7486289 100644 --- a/include/boost/uuid/detail/chacha20.hpp +++ b/include/boost/uuid/detail/chacha20.hpp @@ -5,6 +5,7 @@ // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt +#include #include #include @@ -120,6 +121,16 @@ class chacha20_12 state_[ 13 ] = 0; } + static constexpr result_type min() + { + return std::numeric_limits::min(); + } + + static constexpr result_type max() + { + return std::numeric_limits::max(); + } + result_type operator()() { if( index_ == 16 ) diff --git a/include/boost/uuid/random_generator.hpp b/include/boost/uuid/random_generator.hpp index 405f4291..f3942b11 100644 --- a/include/boost/uuid/random_generator.hpp +++ b/include/boost/uuid/random_generator.hpp @@ -1,75 +1,33 @@ #ifndef BOOST_UUID_RANDOM_GENERATOR_HPP_INCLUDED #define BOOST_UUID_RANDOM_GENERATOR_HPP_INCLUDED -// Boost random_generator.hpp header file ----------------------------------------------// - // Copyright 2010 Andy Tompkins. // Copyright 2017 James E. King III -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) +// Copyright 2024 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt #include -#include -#include -#include -#include -#include +#include +#include namespace boost { namespace uuids { -//! \brief a far less complex random generator that uses -//! operating system provided entropy which will -//! satisfy the majority of use cases -class random_generator_pure +// only provided for compatibility with 1.85 +class random_generator_mt19937: public basic_random_generator { -private: - - std::unique_ptr prov_; - -public: - - typedef uuid result_type; - - random_generator_pure(): prov_( new detail::random_provider ) - { - } - - random_generator_pure(random_generator_pure&& that) = default; - random_generator_pure& operator= (random_generator_pure&& that) = default; - - //! \returns a random, valid uuid - //! \throws entropy_error - result_type operator()() - { - result_type result; - - std::uint32_t tmp[ 4 ]; - prov_->generate( tmp + 0, tmp + 4 ); - - std::memcpy( result.data, tmp, 16 ); - - // set variant - // must be 0b10xxxxxx - *(result.begin() + 8) &= 0xBF; - *(result.begin() + 8) |= 0x80; - - // set version - // must be 0b0100xxxx - *(result.begin() + 6) &= 0x4F; //0b01001111 - *(result.begin() + 6) |= 0x40; //0b01000000 +}; - return result; - } +// only provided for compatibility with 1.85 +class random_generator_pure: public basic_random_generator +{ }; -#if defined(BOOST_UUID_RANDOM_GENERATOR_COMPAT) -typedef basic_random_generator random_generator; -#else -typedef random_generator_pure random_generator; -typedef basic_random_generator random_generator_mt19937; -#endif +// the default random generator +class random_generator: public basic_random_generator +{ +}; }} // namespace boost::uuids diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 71b35f02..2ff19a53 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -116,9 +116,6 @@ run test_detail_chacha20.cpp ; # compile-fail tests -compile-fail compile-fail/random_generator_no_copy_assign.cpp ; -compile-fail compile-fail/random_generator_no_copy_ctor.cpp ; - compile-fail compile-fail/random_provder_no_copy_ctor.cpp ; compile-fail compile-fail/random_provider_no_copy_assign.cpp ; diff --git a/test/compile-fail/random_generator_no_copy_assign.cpp b/test/compile-fail/random_generator_no_copy_assign.cpp deleted file mode 100644 index 0ff28d9e..00000000 --- a/test/compile-fail/random_generator_no_copy_assign.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// (c) Copyright Andrey Semashev 2018 - -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -// The test verifies that random_generator is not copy assignable - -#include - -int main() -{ - boost::uuids::random_generator uuid_gen1, uuid_gen2; - uuid_gen2 = uuid_gen1; - - return 1; -} diff --git a/test/compile-fail/random_generator_no_copy_ctor.cpp b/test/compile-fail/random_generator_no_copy_ctor.cpp deleted file mode 100644 index 9a5da835..00000000 --- a/test/compile-fail/random_generator_no_copy_ctor.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// (c) Copyright Andrey Semashev 2018 - -// Distributed under the Boost Software License, Version 1.0. (See -// accompanying file LICENSE_1_0.txt or copy at -// https://www.boost.org/LICENSE_1_0.txt) - -// The test verifies that random_generator is not copy constructible - -#include - -int main() -{ - boost::uuids::random_generator uuid_gen1; - boost::uuids::random_generator uuid_gen2(uuid_gen1); - - return 1; -} diff --git a/test/test_comparison.cpp b/test/test_comparison.cpp index 18b3f395..6bb06ed6 100644 --- a/test/test_comparison.cpp +++ b/test/test_comparison.cpp @@ -74,7 +74,7 @@ int main() { std::mt19937 rng; - random_generator_mt19937 gen( &rng ); + basic_random_generator gen( &rng ); for( int i = 0; i < 16; ++i ) {