OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
random_source.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
11#ifndef OPENCBDC_TX_SRC_COMMON_RANDOM_SOURCE_H_
12#define OPENCBDC_TX_SRC_COMMON_RANDOM_SOURCE_H_
13
14#include "crypto/sha256.h"
15#include "hash.hpp"
16
17#include <limits>
18#include <mutex>
19#include <queue>
20
21namespace cbdc {
28 public:
29 using result_type = unsigned int;
30
33 explicit random_source(const std::string& source_file);
34 ~random_source() = default;
35
36 random_source(const random_source& other) = delete;
37 auto operator=(const random_source& other) = delete;
38
39 random_source(random_source&& other) = delete;
40 auto operator=(random_source&& other) = delete;
41
44 auto operator()() -> result_type;
45
48 static constexpr auto min() -> result_type {
49 return std::numeric_limits<result_type>::min();
50 }
51
54 static constexpr auto max() -> result_type {
55 return std::numeric_limits<result_type>::max();
56 }
57
63 auto random_hash() -> hash_t;
64
65 private:
66 auto hash_at_index(uint64_t idx) -> hash_t;
67
68 std::mutex m_mut;
69
70 std::queue<unsigned char> m_buf;
71
72 CSHA256 m_sha;
73 uint64_t m_counter{};
74 };
75}
76
77#endif // OPENCBDC_TX_SRC_COMMON_RANDOM_SOURCE_H_
Generates pseudo-random numbers from a given entropy source.
random_source(random_source &&other)=delete
random_source(const std::string &source_file)
Constructor.
auto operator()() -> result_type
Returns a new random integer.
unsigned int result_type
static constexpr auto min() -> result_type
Returns the minimum random value this source can produce.
static constexpr auto max() -> result_type
Returns the maximum random value this source can produce.
auto operator=(random_source &&other)=delete
auto operator=(const random_source &other)=delete
random_source(const random_source &other)=delete
auto random_hash() -> hash_t
Returns a random 32-byte hash value.
~random_source()=default
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.