From ee392452e57a0edd7bc010e251c58fad6c8e11c3 Mon Sep 17 00:00:00 2001 From: Aizen Date: Mon, 7 Oct 2024 14:59:18 +0200 Subject: [PATCH] microcanonical entropy --- cryptipass.go | 4 ++-- internal.go | 12 +++++------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/cryptipass.go b/cryptipass.go index 2583ac4..f159e13 100644 --- a/cryptipass.go +++ b/cryptipass.go @@ -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") @@ -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] } } } diff --git a/internal.go b/internal.go index 741f549..c6b26a0 100644 --- a/internal.go +++ b/internal.go @@ -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 @@ -105,7 +105,6 @@ 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) @@ -113,13 +112,12 @@ func distill(tokens []string, depth int) map[string]distribution { 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