Documentation

QedShare.Encoding.Bits

Packing bytes into 11-bit word numbers #

A word share writes its bytes as BIP-39 words, and each word carries 11 bits. pack turns bytes into numbers 0 … 2047 (one per word): the bytes' bits, most significant first, cut into groups of 11, the last group padded with zero bits. unpack n reverses it and keeps the first n bytes.

Proven here:

Everything is plain lists: word shares are short (at most a few hundred bytes), so clarity wins over speed here.

Bit lists ⇄ numbers #

The number written by a list of bits, most significant bit first.

Equations
Instances For

    The lowest k bits of v, most significant first.

    Equations
    Instances For

      bitsOfNat k v has exactly k bits.

      A list of m bits writes a number below 2 ^ m.

      Writing bits as a number and reading the same number of bits back gives the bits.

      Bytes ⇄ bits #

      The 8 bits of a byte, most significant first.

      Equations
      Instances For

        The byte written by (the number of) a list of bits.

        Equations
        Instances For

          A byte has 8 bits.

          Reading back the 8 bits of a byte gives the byte (checked for all 256 bytes).

          All bits of a list of bytes, most significant bit of the first byte first.

          Equations
          Instances For
            @[irreducible]

            Bytes from bits, 8 at a time; fewer than 8 leftover bits are dropped.

            Equations
            Instances For

              Reading the 8 bits of a byte, followed by more bits, gives the byte first.

              The bytes' bits, followed by anything, read back as bytes start with the bytes.

              n bytes have 8 · n bits.

              Bits ⇄ 11-bit word numbers #

              Pad a list of at most 11 bits with zero bits to exactly 11.

              Equations
              Instances For
                @[irreducible]

                Cut bits into groups of 11 (the last one padded with zeros) and read each group as a number.

                Equations
                Instances For

                  The 11 bits of each number, one after the other.

                  Equations
                  Instances For

                    Padding the first 11 bits (or fewer) always gives exactly 11 bits.

                    Grouping and ungrouping gives back the bits, followed by some padding.

                    theorem QedShare.Bits.group11_lt (l : List Bool) (n : Nat) :
                    n ∈ group11 l → n < 2048

                    Every number made by group11 is below 2 ^ 11 = 2048.

                    group11 makes ⌈m / 11⌉ numbers from m bits.

                    Bytes ⇄ word numbers #

                    Bytes as word numbers (11 bits each, last one padded with zero bits).

                    Equations
                    Instances For

                      Word numbers back to bytes, keeping the first n bytes.

                      Equations
                      Instances For

                        Round trip. Unpacking the packed bytes (knowing how many there were) gives the bytes.

                        theorem QedShare.Bits.pack_lt (bs : List UInt8) (n : Nat) :
                        n ∈ pack bs → n < 2048

                        Every packed number is below 2048, so it names a word of the BIP-39 list.

                        theorem QedShare.Bits.length_pack (bs : List UInt8) :
                        (pack bs).length = (8 * bs.length + 10) / 11

                        n bytes pack into ⌈8·n / 11⌉ words.

                        theorem QedShare.Bits.unpack_pack_append (bs : List UInt8) (m : Nat) (hm : bs.length ≤ m) :
                        ∃ (extra : List UInt8), unpack m (pack bs) = bs ++ extra

                        Unpacking at least as many bytes as were packed gives the packed bytes, then possibly more (the padding bits can form one extra byte).