Skip to content

Commit

Permalink
microcanonical entropy
Browse files Browse the repository at this point in the history
  • Loading branch information
Aizen committed Oct 7, 2024
1 parent 624aecb commit ee39245
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cryptipass.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (g *Generator) GenNextToken(seed string) (string, float64) {
N := g.Rng.IntN(tr.total)
for i, v := range tr.counts {
if N < v {
return tr.tokens[i], tr.entropy
return tr.tokens[i], tr.entropies[i]
}
}
panic("unexpected")
Expand Down Expand Up @@ -280,7 +280,7 @@ func (g *Generator) GenWordLength() (int, float64) {
N := g.Rng.IntN(tr.total)
for i, v := range tr.counts {
if N < v {
return int(tr.tokens[i][0]), tr.entropy
return int(tr.tokens[i][0]), tr.entropies[i]
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func (g *Generator) assert_ready() {
// The distribution struct is used internally to facilitate the generation
// of passwords by modeling the relationships between characters in the generated output.
type distribution struct {
tokens []string
counts []int
total int
entropy float64
tokens []string
counts []int
entropies []float64
total int
}

// distill processes a list of words and builds a probabilistic transition matrix
Expand Down Expand Up @@ -105,21 +105,19 @@ func distill(tokens []string, depth int) map[string]distribution {
for _, freq := range rfreq {
C += freq
}
H := 0.0
tr := distribution{}
tr.counts = make([]int, 0)

tr.tokens = make([]string, 0)
cum := 0
for ru, freq := range rfreq {
p := float64(freq) / float64(C)
H -= math.Log2(p) * p
cum += freq
tr.counts = append(tr.counts, cum)
tr.tokens = append(tr.tokens, string(ru))
tr.entropies = append(tr.entropies, math.Log2(1.0/p))
}
tr.total = C
tr.entropy = H
dist_trans_matrix[k] = tr
}
return dist_trans_matrix
Expand Down

0 comments on commit ee39245

Please sign in to comment.