@@ -10,14 +10,29 @@ import (
1010)
1111
1212type dictionary struct {
13- bitsToWord map [string ]string
14- wordToBits map [string ]string
15- bitsToInt map [string ]int
16- bitsBatchSize int
13+ // bitsToWord ["00101"] => "banana"
14+ bitsToWord map [string ]string
15+
16+ // wordToBits ["banana"] => "00101"
17+ wordToBits map [string ]string
18+
19+ // bitsToInt ["00101"] => 5
20+ bitsToInt map [string ]int
21+
22+ // bitsStringLen how many bits could be encoded with provided words.
23+ //
24+ // example: 32 words could encode 5 bits [0 0 0 0 0]
25+ bitsStringLen int
26+
27+ // wordsChecksum is used to calc mnemonic checksum,
28+ // so mnemonic is binded to specific dictionary
1729 wordsChecksum []byte
18- checksumLen int
30+
31+ // checksumLen how many bits used for checksum
32+ checksumLen int
33+
1934 // how many bit in checksum are for tail len
20- // bitsBatchSize = checksumLen + tailChecksumLen
35+ // bitsStringLen = checksumLen + tailChecksumLen
2136 tailChecksumLen int
2237}
2338
@@ -36,8 +51,7 @@ func NewDictionary(words []string) (Recoder, error) {
3651 return nil , errors .New ("dictionary should be complete and len(words) == 2^N" )
3752 }
3853
39- bitsBatchSize := int (math .Log2 (float64 (len (words ))))
40-
54+ bitsStringLen := int (math .Log2 (float64 (len (words ))))
4155 bitsToWord := make (map [string ]string , len (words ))
4256 wordToBits := make (map [string ]string , len (words ))
4357 bitsToInt := make (map [string ]int , len (words ))
@@ -55,32 +69,33 @@ func NewDictionary(words []string) (Recoder, error) {
5569 }
5670 dups [word ] = true
5771
58- bitWord := idxToBitString (i , bitsBatchSize )
72+ bitWord := idxToBitString (i , bitsStringLen )
5973 bitsToWord [bitWord ] = word
6074 wordToBits [word ] = bitWord
6175 bitsToInt [bitWord ] = i
6276
6377 h .Write ([]byte (word ))
6478 }
6579
66- tailChecksumLen := tailBitsLenInChecksum (bitsBatchSize )
67- checksumLen := bitsBatchSize - tailChecksumLen
80+ tailChecksumLen := tailBitsLenInChecksum (bitsStringLen )
81+ checksumLen := bitsStringLen - tailChecksumLen
6882
6983 return & dictionary {
7084 bitsToWord : bitsToWord ,
7185 wordToBits : wordToBits ,
7286 bitsToInt : bitsToInt ,
73- bitsBatchSize : bitsBatchSize ,
87+ bitsStringLen : bitsStringLen ,
7488 wordsChecksum : h .Sum (nil ),
7589 checksumLen : checksumLen ,
7690 tailChecksumLen : tailChecksumLen ,
7791 }, nil
7892}
7993
80- func tailBitsLenInChecksum (bitsBatchSize int ) int {
94+ // tailBitsLenInChecksum calcs how many bits is required to encode
95+ func tailBitsLenInChecksum (bitsStringLen int ) int {
8196 tailChecksumLen := 0
82- if bitsBatchSize > 1 {
83- tailChecksumLen = int (math .Ceil (math .Log2 (float64 (bitsBatchSize ))))
97+ if bitsStringLen > 1 {
98+ tailChecksumLen = int (math .Ceil (math .Log2 (float64 (bitsStringLen ))))
8499 }
85100
86101 return tailChecksumLen
@@ -102,7 +117,7 @@ func idxToBitString(idx int, bitLen int) string {
102117 n := big .NewInt (int64 (idx ))
103118 b := padByteSlice (n .Bytes (), 2 )
104119
105- str := fmt .Sprintf ("%08b" , b [0 ]) + fmt . Sprintf ( "%08b" , b [1 ])
120+ str := fmt .Sprintf ("%08b%08b " , b [0 ], b [1 ])
106121
107122 return str [16 - bitLen :]
108123}
@@ -123,16 +138,17 @@ func (d *dictionary) Encode(data []byte) ([]string, error) {
123138 bits := bitsBuilder .String ()
124139
125140 // how many bits we should take from last word
126- tailLen := len (bits ) % d .bitsBatchSize
127- tailLenBits := idxToBitString (tailLen , d .bitsBatchSize )
141+ tailLen := len (bits ) % d .bitsStringLen
142+ tailLenBits := idxToBitString (tailLen , d .bitsStringLen )
128143 tailLenBits = tailLenBits [len (tailLenBits )- d .tailChecksumLen :]
129144
130145 // add checksum at the begining
131146 // so when decoding we dont care about its paddings
147+ // NOTE: len(cs + tailLenBits) == bitsStringLen, and encoded with one word
132148 bits = cs + tailLenBits + bits
133149
134- for i := 0 ; i < len (bits )- tailLen ; i += d .bitsBatchSize {
135- lb := bits [i : i + d .bitsBatchSize ]
150+ for i := 0 ; i < len (bits )- tailLen ; i += d .bitsStringLen {
151+ lb := bits [i : i + d .bitsStringLen ]
136152 word , ok := d .bitsToWord [lb ]
137153 if ! ok {
138154 return mnemonic , fmt .Errorf ("bits-to-word mapping not found for bits: %s" , lb )
@@ -143,7 +159,7 @@ func (d *dictionary) Encode(data []byte) ([]string, error) {
143159
144160 if tailLen > 0 {
145161 tailBits := bits [len (bits )- tailLen :]
146- tailBits += strings .Repeat ("1" , d .bitsBatchSize - tailLen )
162+ tailBits += strings .Repeat ("1" , d .bitsStringLen - tailLen )
147163 tailWord , ok := d .bitsToWord [tailBits ]
148164 if ! ok {
149165 return mnemonic , fmt .Errorf ("bits-to-word mapping not found for tail bits: %s" , tailBits )
@@ -168,7 +184,7 @@ func (d *dictionary) Decode(mnemonic []string) ([]byte, error) {
168184
169185 tailLen := 0
170186 if d .tailChecksumLen > 0 {
171- tailLenBits = strings .Repeat ("0" , d .bitsBatchSize - d .tailChecksumLen ) + tailLenBits
187+ tailLenBits = strings .Repeat ("0" , d .bitsStringLen - d .tailChecksumLen ) + tailLenBits
172188 tailLen , ok = d .bitsToInt [tailLenBits ]
173189 if ! ok {
174190 return nil , errors .New ("invalid tail" )
@@ -186,7 +202,7 @@ func (d *dictionary) Decode(mnemonic []string) ([]byte, error) {
186202
187203 bitString := bitsBuilder .String ()
188204 if tailLen > 0 {
189- paddingLen := d .bitsBatchSize - tailLen
205+ paddingLen := d .bitsStringLen - tailLen
190206 bitString = bitString [:len (bitString )- paddingLen ]
191207 }
192208
@@ -196,7 +212,7 @@ func (d *dictionary) Decode(mnemonic []string) ([]byte, error) {
196212
197213 bitCounter := 0
198214 for b := 0 ; b < len (bitString )/ 8 ; b ++ {
199- for bit := 0 ; bit < 8 ; bit ++ {
215+ for bit := range 8 {
200216 dst [b ] |= (src [bitCounter ] & bitMask ) << (7 - bit )
201217 bitCounter ++
202218 }
@@ -227,7 +243,7 @@ func (d *dictionary) checksum(data []byte) (string, error) {
227243 }
228244
229245 sum := h .Sum (nil )
230- str := fmt .Sprintf ("%08b" , sum [0 ]) + fmt . Sprintf ( "%08b" , sum [1 ])
246+ str := fmt .Sprintf ("%08b%08b " , sum [0 ], sum [1 ])
231247
232248 return str [:d .checksumLen ], nil
233249}
0 commit comments