Introduce new PRNG with drastically better performance
Signed-off-by: kayos@tcp.direct <kayos@tcp.direct>
This commit is contained in:
parent
28c6a6193d
commit
c36496d75d
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
.idea
|
.idea
|
||||||
*.swp
|
*.swp
|
||||||
*.save
|
*.save
|
||||||
|
test.*
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
crip "crypto/rand"
|
crip "crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"nullprogram.com/x/rng"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,8 +19,10 @@ func RandomStrChoice(choice []string) string {
|
|||||||
|
|
||||||
func RNG(n int) int {
|
func RNG(n int) int {
|
||||||
var seed int64
|
var seed int64
|
||||||
|
r := new(rng.SplitMix64)
|
||||||
binary.Read(crip.Reader, binary.BigEndian, &seed)
|
binary.Read(crip.Reader, binary.BigEndian, &seed)
|
||||||
rng := rand.New(rand.NewSource(seed))
|
r.Seed(seed)
|
||||||
|
rng := rand.New(r)
|
||||||
return rng.Intn(n)
|
return rng.Intn(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ func Test_RNG(t *testing.T) {
|
|||||||
for n := 0; n != 500; n++ {
|
for n := 0; n != 500; n++ {
|
||||||
zero := RNG(55555)
|
zero := RNG(55555)
|
||||||
one := RNG(55555)
|
one := RNG(55555)
|
||||||
t.Logf("Random0: %d Random1: %d", zero, one)
|
// t.Logf("Random0: %d Random1: %d", zero, one)
|
||||||
if zero == one {
|
if zero == one {
|
||||||
t.Errorf("RNG hit a duplicate! %d == %d", 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++ {
|
for n := 0; n != 500; n++ {
|
||||||
zero := RandStr(55)
|
zero := RandStr(55)
|
||||||
one := 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)
|
randStrChecks(zero, one, t)
|
||||||
zero = ""
|
zero = ""
|
||||||
one = ""
|
one = ""
|
||||||
@ -53,6 +53,7 @@ func Test_RandStr(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_RandStr_Entropy(t *testing.T) {
|
func Test_RandStr_Entropy(t *testing.T) {
|
||||||
|
var totalScore = 0
|
||||||
for n := 0; n != 500; n++ {
|
for n := 0; n != 500; n++ {
|
||||||
zero := RandStr(55)
|
zero := RandStr(55)
|
||||||
one := RandStr(55)
|
one := RandStr(55)
|
||||||
@ -65,14 +66,16 @@ func Test_RandStr_Entropy(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
similarity++
|
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 {
|
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.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 = ""
|
zero = ""
|
||||||
one = ""
|
one = ""
|
||||||
}
|
}
|
||||||
|
t.Logf("[ENTROPY] final score (lower is better): %d", totalScore)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -6,6 +6,7 @@ require (
|
|||||||
github.com/rs/zerolog v1.26.1
|
github.com/rs/zerolog v1.26.1
|
||||||
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e
|
golang.org/x/crypto v0.0.0-20211215165025-cf75a172585e
|
||||||
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6
|
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6
|
||||||
|
nullprogram.com/x/rng v1.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
2
go.sum
2
go.sum
@ -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=
|
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 h1:acCzuUSQ79tGsM/O50VRFySfMm19IoMKL+sZztZkCxw=
|
||||||
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6/go.mod h1:y3MGhcFMlh0KZPMuXXow8mpjxxAk3yoDNsp4cQz54i8=
|
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=
|
||||||
|
Loading…
Reference in New Issue
Block a user