[+] Improve RNG apis. TODO: more work is needed

This commit is contained in:
Reece Wilson 2022-02-20 20:43:37 +00:00
parent 3651c22159
commit 188e478033
4 changed files with 33 additions and 15 deletions

View File

@ -28,25 +28,37 @@ namespace Aurora::RNG
template<typename T, int N> template<typename T, int N>
inline void NextArray(T(&array)[N]) inline void NextFillArray(T(&array)[N])
{ {
Read(array, N * sizeof(T)); Read(array, N * sizeof(T));
} }
template<typename T> template<typename T>
inline void NextArray(T *array, AuUInt32 length) inline void NextFillArray(T *array, AuUInt32 length)
{ {
Raed(array, length * sizeof(T)); Read(array, length * sizeof(T));
} }
template<typename T> template<typename T>
inline T NextTmpl() static inline T &NextFillRange(const T &items)
{
NextFillArray(items.begin(), items.end() - items.begin());
}
template<typename T>
inline T NextFillTmpl()
{ {
T ret {}; T ret {};
Read(&ret, sizeof(T)); Read(&ret, sizeof(T));
return ret; return ret;
} }
template<typename T>
inline T &NextIterator(T &begin, T &end)
{
return begin + NextIndex(AuUInt(end - begin));
}
template<typename T> template<typename T>
inline T &NextArray(T *items, AuUInt32 count) inline T &NextArray(T *items, AuUInt32 count)
{ {
@ -60,9 +72,9 @@ namespace Aurora::RNG
} }
template<typename T> template<typename T>
static inline T &RngRange(const T &items) static inline T &NextRange(const T &items)
{ {
return RngArray(items.begin(), items.end() - items.begin()); return NextIterator(items.begin(), items.end());
} }
}; };

View File

@ -33,7 +33,7 @@ namespace Aurora::RNG
// Note: it is conceivable that someone may want the following templates for some cryptographic purpose // Note: it is conceivable that someone may want the following templates for some cryptographic purpose
template<bool fast = true, typename T, int N> template<bool fast = true, typename T, int N>
static inline void RngArray(T(&array)[N]) static inline void RngFillArray(T(&array)[N])
{ {
if constexpr (fast) if constexpr (fast)
{ {
@ -46,7 +46,7 @@ namespace Aurora::RNG
} }
template<bool fast = true, typename T> template<bool fast = true, typename T>
static inline void RngArray(T *array, AuUInt32 length) static inline void RngFillArray(T *array, AuUInt32 length)
{ {
if constexpr (fast) if constexpr (fast)
{ {
@ -58,6 +58,12 @@ namespace Aurora::RNG
} }
} }
template<bool fast = true, typename T>
static inline T &RngFillRange(const T &items)
{
return RngFillArray<fast>(items.begin(), items.end() - items.begin());
}
template<bool fast = true, typename T> template<bool fast = true, typename T>
static inline T RngTmpl() static inline T RngTmpl()
{ {

View File

@ -261,7 +261,7 @@ namespace Aurora::RNG
static void InitFastRng() static void InitFastRng()
{ {
AuArray<AuUInt8, 64> maxEntropy; AuArray<AuUInt8, 64> maxEntropy;
RngArray<false, AuUInt8>(maxEntropy.data(), 64); RngFillArray<false, AuUInt8>(maxEntropy.data(), 64);
gWellRand = WELL_SeedRandBig64(maxEntropy); gWellRand = WELL_SeedRandBig64(maxEntropy);
gFastDevice = RandomUnique(RandomDef {false}); gFastDevice = RandomUnique(RandomDef {false});
} }

View File

@ -55,7 +55,7 @@ namespace Aurora::RNG
else else
{ {
this->fast_ = {}; this->fast_ = {};
RNG::RngArray<false>(this->fast_.state); RNG::RngFillArray<false>(this->fast_.state);
} }
} }
// secure rng requires no init -> we just passthrough to the global ReadSecureRNG function // secure rng requires no init -> we just passthrough to the global ReadSecureRNG function
@ -110,7 +110,7 @@ namespace Aurora::RNG
AuUInt8 RandomDevice::NextByte() AuUInt8 RandomDevice::NextByte()
{ {
return NextTmpl<AuUInt8>(); return NextFillTmpl<AuUInt8>();
} }
bool RandomDevice::NextBoolean() bool RandomDevice::NextBoolean()
@ -120,12 +120,12 @@ namespace Aurora::RNG
AuUInt32 RandomDevice::NextU32() AuUInt32 RandomDevice::NextU32()
{ {
return NextTmpl<AuUInt32>(); return NextFillTmpl<AuUInt32>();
} }
AuUInt64 RandomDevice::NextU64() AuUInt64 RandomDevice::NextU64()
{ {
return NextTmpl<AuUInt64>(); return NextFillTmpl<AuUInt64>();
} }
AuInt32 RandomDevice::NextInt(AuInt32 min, AuInt32 max) AuInt32 RandomDevice::NextInt(AuInt32 min, AuInt32 max)