Skip to content

Commit

Permalink
Use chacha20_12 in random_generator; update definitions of random_gen…
Browse files Browse the repository at this point in the history
…erator_mt19937 and random_generator_pure
  • Loading branch information
pdimov committed Apr 23, 2024
1 parent 059bf06 commit a81ba56
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 95 deletions.
11 changes: 11 additions & 0 deletions include/boost/uuid/detail/chacha20.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <limits>
#include <cstdint>
#include <cstddef>

Expand Down Expand Up @@ -120,6 +121,16 @@ class chacha20_12
state_[ 13 ] = 0;
}

static constexpr result_type min()
{
return std::numeric_limits<result_type>::min();
}

static constexpr result_type max()
{
return std::numeric_limits<result_type>::max();
}

result_type operator()()
{
if( index_ == 16 )
Expand Down
72 changes: 15 additions & 57 deletions include/boost/uuid/random_generator.hpp
Original file line number Diff line number Diff line change
@@ -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 <boost/uuid/basic_random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/detail/random_provider.hpp>
#include <memory>
#include <cstring>
#include <cstdint>
#include <boost/uuid/detail/random_device.hpp>
#include <boost/uuid/detail/chacha20.hpp>

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<std::mt19937>
{
private:

std::unique_ptr<detail::random_provider> 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<detail::random_device>
{
};

#if defined(BOOST_UUID_RANDOM_GENERATOR_COMPAT)
typedef basic_random_generator<std::mt19937> random_generator;
#else
typedef random_generator_pure random_generator;
typedef basic_random_generator<std::mt19937> random_generator_mt19937;
#endif
// the default random generator
class random_generator: public basic_random_generator<detail::chacha20_12>
{
};

}} // namespace boost::uuids

Expand Down
3 changes: 0 additions & 3 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;

Expand Down
17 changes: 0 additions & 17 deletions test/compile-fail/random_generator_no_copy_assign.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions test/compile-fail/random_generator_no_copy_ctor.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/test_comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ int main()

{
std::mt19937 rng;
random_generator_mt19937 gen( &rng );
basic_random_generator<std::mt19937> gen( &rng );

for( int i = 0; i < 16; ++i )
{
Expand Down

0 comments on commit a81ba56

Please sign in to comment.