Introduce new PRNG with drastically better performance

Signed-off-by: kayos@tcp.direct <kayos@tcp.direct>
This commit is contained in:
kayos@tcp.direct 2022-03-15 20:39:22 -07:00
parent 28c6a6193d
commit c36496d75d
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
5 changed files with 15 additions and 5 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
*.swp
*.save
test.*

View File

@ -4,6 +4,7 @@ import (
crip "crypto/rand"
"encoding/binary"
"math/rand"
"nullprogram.com/x/rng"
"time"
)
@ -18,8 +19,10 @@ func RandomStrChoice(choice []string) string {
func RNG(n int) int {
var seed int64
r := new(rng.SplitMix64)
binary.Read(crip.Reader, binary.BigEndian, &seed)
rng := rand.New(rand.NewSource(seed))
r.Seed(seed)
rng := rand.New(r)
return rng.Intn(n)
}

View File

@ -18,7 +18,7 @@ func Test_RNG(t *testing.T) {
for n := 0; n != 500; n++ {
zero := RNG(55555)
one := RNG(55555)
t.Logf("Random0: %d Random1: %d", zero, one)
// t.Logf("Random0: %d Random1: %d", zero, one)
if zero == one {
t.Errorf("RNG hit a duplicate! %d == %d", zero, one)
}
@ -44,7 +44,7 @@ func Test_RandStr(t *testing.T) {
for n := 0; n != 500; n++ {
zero := RandStr(55)
one := RandStr(55)
t.Logf("Random0: %s Random1: %s", zero, one)
// t.Logf("Random0: %s Random1: %s", zero, one)
randStrChecks(zero, one, t)
zero = ""
one = ""
@ -53,6 +53,7 @@ func Test_RandStr(t *testing.T) {
}
func Test_RandStr_Entropy(t *testing.T) {
var totalScore = 0
for n := 0; n != 500; n++ {
zero := RandStr(55)
one := RandStr(55)
@ -65,14 +66,16 @@ func Test_RandStr_Entropy(t *testing.T) {
continue
}
similarity++
t.Logf("[-] zeroSplit[%d] is the same as oneSplit[%d] (%s)", i, i, char)
// t.Logf("[-] zeroSplit[%d] is the same as oneSplit[%d] (%s)", i, i, char)
}
if similarity*4 > 55 {
t.Errorf("[ENTROPY FAILURE] more than a quarter of the string is the same!\n zero: %s \n one: %s \nTotal similar: %d", zero, one, similarity)
}
t.Logf("[ENTROPY] Similarity score (lower is better): %d", similarity)
// t.Logf("[ENTROPY] Similarity score (lower is better): %d", similarity)
totalScore = totalScore + similarity
zero = ""
one = ""
}
t.Logf("[ENTROPY] final score (lower is better): %d", totalScore)
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/rs/zerolog v1.26.1
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6
nullprogram.com/x/rng v1.1.0
)
require (

2
go.sum
View File

@ -47,3 +47,5 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6 h1:acCzuUSQ79tGsM/O50VRFySfMm19IoMKL+sZztZkCxw=
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6/go.mod h1:y3MGhcFMlh0KZPMuXXow8mpjxxAk3yoDNsp4cQz54i8=
nullprogram.com/x/rng v1.1.0 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
nullprogram.com/x/rng v1.1.0/go.mod h1:glGw6V87vyfawxCzqOABL3WfL95G65az9Z2JZCylCkg=