Skip to content

Commit

Permalink
Entropy: improve markov map randomness, decrease detectability
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 26, 2024
1 parent e0b4c0a commit 0bc411a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ require (
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/sys v0.21.0 // indirect
nullprogram.com/x/rng v1.1.0 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
nullprogram.com/x/rng v1.1.0 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
nullprogram.com/x/rng v1.1.0/go.mod h1:glGw6V87vyfawxCzqOABL3WfL95G65az9Z2JZCylCkg=
26 changes: 21 additions & 5 deletions heffalump/markov.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import (
"io"
"math/rand"
"strings"
"time"
"unicode"
"unicode/utf8"

"git.tcp.direct/kayos/common/entropy"
"git.tcp.direct/kayos/common/squish"
)

const (
sp = " "
nl = "\n"
)

// NewDefaultMarkovMap creates a new MarkovMap from the default source text.
func NewDefaultMarkovMap() MarkovMap {
src, err := squish.UnpackStr(srcGz)
Expand Down Expand Up @@ -70,11 +77,15 @@ func ScanHTML(data []byte, atEOF bool) (advance int, token []byte, err error) {
type tokenPair [2]string

// MarkovMap is a map that acts as a Markov chain generator.
type MarkovMap map[tokenPair][]string
type MarkovMap struct {
m map[tokenPair][]string
r *rand.Rand
}

// MakeMarkovMap makes an empty MakeMarkov and fills it with r.
func MakeMarkovMap(r io.Reader) MarkovMap {
m := MarkovMap{}
m := MarkovMap{m: make(map[tokenPair][]string)}
m.r = rand.New(rand.NewSource(entropy.GetOptimizedRand().Int63()))
m.Fill(r)
return m
}
Expand All @@ -97,13 +108,13 @@ func (mm MarkovMap) Fill(r io.Reader) {
// Add adds a three token sequence to the map.
func (mm MarkovMap) Add(w1, w2, w3 string) {
p := tokenPair{w1, w2}
mm[p] = append(mm[p], w3)
mm.m[p] = append(mm.m[p], w3)
}

// Get pseudo-randomly chooses a possible suffix to w1 and w2.
func (mm MarkovMap) Get(w1, w2 string) string {
p := tokenPair{w1, w2}
suffix, ok := mm[p]
suffix, ok := mm.m[p]
if !ok {
return ""
}
Expand All @@ -119,10 +130,15 @@ func (mm MarkovMap) Read(p []byte) (n int, err error) {
for {
w3 = mm.Get(w1, w2)
if n+len(w3)+1 >= len(p) {
n += copy(p[n:], nl)
break
}
n += copy(p[n:], w3)
n += copy(p[n:], "\n")
if time.Now().UnixNano()%10 == 0 {
n += copy(p[n:], nl)
} else {
n += copy(p[n:], sp)
}
w1, w2 = w2, w3
}
return
Expand Down

0 comments on commit 0bc411a

Please sign in to comment.