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:
unpack_pack:unpack bs.length (pack bs) = bs, for every byte list;pack_lt: every numberpackproduces is below 2048, so it names a word;length_pack:packproduces⌈8·n / 11⌉numbers fornbytes.
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
- QedShare.Bits.natOfBits [] = 0
- QedShare.Bits.natOfBits (b :: l) = b.toNat * 2 ^ l.length + QedShare.Bits.natOfBits l
Instances For
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
Bytes from bits, 8 at a time; fewer than 8 leftover bits are dropped.
Equations
- QedShare.Bits.bytesOfBits l = if 8 ≤ l.length then QedShare.Bits.byteOfBits (List.take 8 l) :: QedShare.Bits.bytesOfBits (List.drop 8 l) else []
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
- QedShare.Bits.pad11 l = l ++ List.replicate (11 - l.length) false
Instances For
Cut bits into groups of 11 (the last one padded with zeros) and read each group as a number.
Equations
- QedShare.Bits.group11 l = if l = [] then [] else QedShare.Bits.natOfBits (QedShare.Bits.pad11 (List.take 11 l)) :: QedShare.Bits.group11 (List.drop 11 l)
Instances For
The 11 bits of each number, one after the other.
Equations
Instances For
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.