Skip to content

Commit

Permalink
simplify word lists
Browse files Browse the repository at this point in the history
each diceware word list consists of lines of words. each word is
prefixed (exception of the rule: diceware8k) with a "dicechain", a
string which represents the result of a dice throw: '1' when the dice
shows '1', a '5' when the dice shows a '5' and so on.

the used word lists are compiled into words_xyz.go. because each list is
roughly 80/90kbyte in size, my initial plan was to gzip the raw lists,
base64 it again and use this internally. the result was something in the
ballpark of 50kbyte, but the lists could not be checked visually anymore.

by removing the prefix the lists shrink to ~50kbyte and can be checked
visually. the prefix for each position in the list can be calculated by
transforming the index from base-10 to base-6 ('0'-'5') and adjusting the
shown value by '1' ('0' becomes '1', '5' becomes '6' etc). to convert
a "dicechain" of the user to an index in the word list, the values are
first decreased by 1 ('6' becomes '5' etc) and then converting a base-6
number to base-10:

  dicechain     base10
      11111 <-> 0000
      11112 <-> 0001
      11116 <-> 0005
      12345 <-> 0310
      54321 <-> 5910
      66666 <-> 7775
  • Loading branch information
mgumz committed Oct 2, 2016
1 parent 3eb9e97 commit 1d2990a
Show file tree
Hide file tree
Showing 13 changed files with 55,033 additions and 11,866 deletions.
95 changes: 95 additions & 0 deletions dicechain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"fmt"
"strconv"
"strings"
)

// a (chain of) dice rolls (lets say "dicechain") result in a string like
// "12345" (the first throw was a '1', the second one a '2' etc.).
//
// a diceware list essentially is a list of words, prefixed with a dice roll
// chain:
//
// 11111 a
// 11112 a&p
// 11113 a's
// 11114 aa
// 11115 aaa
// 11116 aaaa
//
// so, '11111' maps to index '0', '11116' maps to index '6' in the list.
//
// by using (real world) dices, a user creates a dicechain, look up the
// word in the list, repeat. the -electronic mode switches to the weaker
// (from a quality of randomness point of view) (pseudo-) random number
// generator of the operating system, picks the word of the used list
// based upon the random number, repeat.
//
// i removed the dicechain prefix of each list to slim down the size of
// each list.

// parse a dicechain into an "index".
func parseDiceChain(chain string) (int, error) {

if len(chain) != 5 {
return -1, fmt.Errorf("invalid dicechain %q: expected 6 chars", chain)
}

r := strings.Map(diceDown, chain)
n, err := strconv.ParseInt(r, 6, 32)

return int(n), err
}

func indexToDiceChain(index int) string {

r := strconv.FormatInt(int64(index), 6)
r = fmt.Sprintf("%05s", r)
return strings.Map(diceUp, r)
}

func diceDown(r rune) rune {

switch r {
case '1':
return '0'
case '2':
return '1'
case '3':
return '2'
case '4':
return '3'
case '5':
return '4'
case '6':
return '5'
}

panic("unhandled rune " + string(r))

return 0
}

func diceUp(r rune) rune {

switch r {
case '0':
return '1'
case '1':
return '2'
case '2':
return '3'
case '3':
return '4'
case '4':
return '5'
case '5':
return '6'
}

panic("unhandled rune " + string(r))

return 0
}
9 changes: 5 additions & 4 deletions electronic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func doRollOnList(rolls int, lines []string, extra bool, printer *wordPrinter) {
max := len(lines)

for i := 0; i < rolls; i += 1 {
line := lines[randN(max)]
words[i].Set(line)
index := randN(max)
line := lines[index]
words[i].Set(line, index)
}

if extra {
Expand All @@ -22,8 +23,8 @@ func doRollOnList(rolls int, lines []string, extra bool, printer *wordPrinter) {
printer.Print(words)
}

//
// to cite diceware:
// doExtra replace one char of one randomly selected word of words according
// to the diceware-extra rules. to cite diceware:
//
// For extra security without adding another word, insert one special
// character or digit chosen at random into your passphrase. Here is how to
Expand Down
Loading

0 comments on commit 1d2990a

Please sign in to comment.