[+] Added 64bit rng seed support

[*] Cryptographically unsafe RNG is marginally less dangerous; use secure rng for the entire base seed instead of seeding using 32bit mt
This commit is contained in:
Reece Wilson 2021-10-01 17:54:55 +01:00
parent a6d1c04ba0
commit 4703264c57
4 changed files with 41 additions and 2 deletions

View File

@ -64,6 +64,7 @@ namespace Aurora::RNG
{
bool secure;
AuOptional<AuUInt32> seed;
AuOptional<AuUInt64> seed64;
inline void SetSeed(AuUInt32 seed)
{
@ -71,6 +72,12 @@ namespace Aurora::RNG
this->secure = false;
}
inline void SetSeed64(AuUInt64 seed)
{
this->seed64 = seed;
this->secure = false;
}
inline void SetCSRNG()
{
this->secure = true;

View File

@ -40,7 +40,19 @@ namespace Aurora::RNG
this->def_ = def;
if (!def.secure)
{
this->fast_ = WELL_SeedRand(def.seed.value_or(Aurora::RNG::RngTmpl<false, AuUInt32>()));
if (def.seed)
{
this->fast_ = WELL_SeedRand(def.seed.value());
}
else if (def.seed64)
{
this->fast_ = WELL_SeedRand64(def.seed64.value());
}
else
{
this->fast_ = {};
RNG::RngArray<false>(this->fast_.state);
}
}
}

View File

@ -26,6 +26,18 @@ inline static void WELL_SeedRand(WELLRand *rand, AuUInt32 seed)
}
}
inline static void WELL_SeedRand64(WELLRand *rand, AuUInt64 seed)
{
MTRand mtrand = MT_SeedRand(seed & 0xffffffff);
MTRand mtrand2 = MT_SeedRand(seed >> 32);
for (unsigned int i = 0; i < 16; i += 2)
{
rand->state[i] = MT_NextLong(&mtrand);
rand->state[i + 1] = MT_NextLong(&mtrand2);
}
}
/**
* Creates a new random number generator from a given seed.
*/
@ -36,6 +48,13 @@ WELLRand WELL_SeedRand(AuUInt32 seed)
return rand;
}
WELLRand WELL_SeedRand64(AuUInt64 seed)
{
WELLRand rand {};
WELL_SeedRand64(&rand, seed);
return rand;
}
/**
* Generates a pseudo-randomly generated long.
*/

View File

@ -8,5 +8,6 @@ struct WELLRand
};
WELLRand WELL_SeedRand(AuUInt32 seed);
WELLRand WELL_SeedRand64(AuUInt64 seed);
AuUInt32 WELL_NextLong(WELLRand* rand);
void WELL_NextBytes(WELLRand *rand, void *in, AuUInt32 length);