Documentation

QedShare.Encoding.Mnemonic

Word shares #

The word form of a share: the same record as the qs1/qs2/qs3 hex form, written as BIP-39 English words so it is easier to write on paper and read aloud (README: Share format).

bytes = version(1) ‖ setid(4) ‖ k(1) ‖ x(1) ‖ len(1) ‖ payload(len) ‖ token ‖ crc32(4)
words = the bytes in 11-bit groups (`Bits.pack`), each group as a word, separated by spaces

version is 1 for the qs1 record (CRC-32 secret tag), 2 for qs2 (AMD tag) and 3 for qs3 (AMD tag and share token). token is the 16-byte share token in version 3 and absent before. crc32 is the CRC-32 of all bytes before it (accident detection only, as in the hex form). The one-byte len limits word shares to payloads of at most 255 bytes, i.e. secrets of at most 251 bytes in version 1 (4-byte tag), 223 bytes in version 2 (32-byte tag) and 223 − 16k bytes in version 3 (the token polynomial adds 16k).

Decoding accepts any whitespace between words, upper case and 4-letter abbreviations (Wordlist.lookup), and checks, in order: every word is known, the version, the payload length, that the words are exactly the canonical encoding of the bytes they decode to (right word count, zero padding bits), and the checksum.

Not BIP-39. These words are not a BIP-39 mnemonic and must never be used as a wallet seed.

decode_encode proves that decoding an encoded share gives back exactly the same record.

Reading a written version byte gives its format.

Everything the checksum covers: version ‖ setid ‖ k ‖ x ‖ len ‖ payload ‖ token.

Equations
  • One or more equations did not get rendered due to their size.
Instances For

    The word for an 11-bit number ("" outside 0 … 2047, which pack never produces).

    Equations
    Instances For

      The words of a record as characters, separated by single spaces.

      Equations
      Instances For

        Encode a record as a word share (lowercase words separated by spaces).

        Equations
        Instances For

          Splitting text into words #

          Split characters at every character satisfying p (empty pieces are kept).

          Equations
          Instances For

            The words of a text: pieces between whitespace, empty pieces dropped.

            Equations
            Instances For
              theorem QedShare.Mnemonic.splitBy_of_forall {p : Char → Bool} (f : List Char) (h : ∀ (c : Char), c ∈ f → p c = false) :
              splitBy p f = [f]

              Text with no separator is a single piece.

              theorem QedShare.Mnemonic.splitBy_append {p : Char → Bool} (f rest : List Char) (h : ∀ (c : Char), c ∈ f → p c = false) (c : Char) (hc : p c = true) :
              splitBy p (f ++ c :: rest) = f :: splitBy p rest

              A piece without separators, then a separator, then the rest: the piece comes first.

              theorem QedShare.Mnemonic.splitWords_intercalate (ws : List (List Char)) (h : ∀ (w : List Char), w ∈ ws → w ≠ [] ∧ ∀ (c : Char), c ∈ w → c.isWhitespace = false) :

              Joining non-empty, whitespace-free words with spaces and splitting again gives the words.

              Decoding #

              Why a word share could not be decoded. Errors name positions, never the words themselves.

              • unknownWord (pos : Nat) : DecodeError

                The word at this position (counting from 0) is not in the word list.

              • badLength : DecodeError

                Too few or too many words, or the length field does not fit.

              • badVersion : DecodeError

                The version number is not 1, 2 or 3.

              • notCanonical : DecodeError

                The words are not exactly what encoding the decoded bytes gives (e.g. wrong padding).

              • badChecksum : DecodeError

                The checksum does not match: the share was mistyped or damaged.

              Instances For
                Equations
                Instances For
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For

                    Look up every word; pos is the position of the first one.

                    Equations
                    Instances For

                      The set id from its 4 bytes.

                      Equations
                      Instances For

                        Decode word numbers into a record (see the module comment for the checks).

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For

                          Round trip #

                          theorem QedShare.Mnemonic.wordOf_eq {n : Nat} (hn : n < 2048) :

                          Below 2048, wordOf n is word number n of the list.

                          theorem QedShare.Mnemonic.lookupAll_map (ns : List Nat) (hns : ∀ (n : Nat), n ∈ ns → n < 2048) (pos : Nat) :
                          lookupAll (List.map (fun (n : Nat) => (wordOf n).toList) ns) pos = Except.ok ns

                          Looking up the words of numbers below 2048 gives back the numbers.

                          Reading back the 4 bytes of a set id gives the set id.

                          Taking the bytes of field elements and wrapping them again gives the same elements.

                          Decoding the word numbers of a record with a payload of at most 255 bytes (and a token of the length its format needs) gives the record.

                          Decoding the characters of an encoded record gives back the record.

                          Round trip. Decoding the word form of a share gives back exactly the same record, for every record whose payload is at most 255 bytes and whose token has the length its format needs.