// Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2014 The Bitcoin Core developers // Copyright (c) 2016-2024 The Hush developers // Distributed under the GPLv3 software license, see the accompanying // file COPYING or https://www.gnu.org/licenses/gpl-3.0.en.html /****************************************************************************** * Copyright © 2014-2019 The SuperNET Developers. * * * * See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at * * the top-level directory of this distribution for the individual copyright * * holder information and the developer policies on copyright and licensing. * * * * Unless otherwise agreed in a custom licensing agreement, no part of the * * SuperNET software, including this file may be copied, modified, propagated * * or distributed except according to the terms contained in the LICENSE file * * * * Removal or modification of this copyright notice is prohibited. * * * ******************************************************************************/ #ifndef HUSH_RANDOM_H #define HUSH_RANDOM_H #include "uint256.h" #include #include /** * Functions to gather random data via the libsodium CSPRNG */ void GetRandBytes(unsigned char* buf, size_t num); uint64_t GetRand(uint64_t nMax); int GetRandInt(int nMax); uint256 GetRandHash(); /** * Identity function for MappedShuffle, so that elements retain their original order. */ int GenIdentity(int n); /** * Rearranges the elements in the range [first,first+len) randomly, assuming * that gen is a uniform random number generator. Follows the same algorithm as * std::shuffle in C++11 (a Durstenfeld shuffle). * * The elements in the range [mapFirst,mapFirst+len) are rearranged according to * the same permutation, enabling the permutation to be tracked by the caller. * * gen takes an integer n and produces a uniform random output in [0,n). */ template void MappedShuffle(RandomAccessIterator first, MapRandomAccessIterator mapFirst, size_t len, std::function gen) { for (size_t i = len-1; i > 0; --i) { auto r = gen(i+1); assert(r >= 0); assert(r <= i); std::swap(first[i], first[r]); std::swap(mapFirst[i], mapFirst[r]); } } /** * Seed insecure_rand using the random pool. * @param Deterministic Use a deterministic seed */ void seed_insecure_rand(bool fDeterministic = false); /** * MWC RNG of George Marsaglia * This is intended to be fast. It has a period of 2^59.3, though the * least significant 16 bits only have a period of about 2^30.1. * * @return random value */ extern uint32_t insecure_rand_Rz; extern uint32_t insecure_rand_Rw; static inline uint32_t insecure_rand(void) { insecure_rand_Rz = 36969 * (insecure_rand_Rz & 65535) + (insecure_rand_Rz >> 16); insecure_rand_Rw = 18000 * (insecure_rand_Rw & 65535) + (insecure_rand_Rw >> 16); return (insecure_rand_Rw << 16) + insecure_rand_Rz; } #endif // HUSH_RANDOM_H