Documentation

QedShareCli.FastDecode

Reading a hex share straight into a packed record #

decodeShare (in Pipeline) is written for the proofs: it turns the text of a share into lists of characters and bytes, 24 bytes of memory per element, several times over. For a share of a 1 MiB secret (2 MiB of hex) that is well over 100 MB while one share is read.

decodePacked reads a hex share from its UTF-8 bytes without any such list. It does not repeat the steps of decodeShare one by one. It reads a candidate record and a checksum from the bytes at the positions the format gives (parsePacked, about which nothing needs to be proven), writes them back as bytes (encodePacked), and uses the candidate only if the input is exactly those bytes, with whitespace before and after. Then the input is the share text of that record with that checksum, and decodeShare_encodeCharsCrc says what decodeShare does with it: it returns the record if the checksum is the record's, and the checksum error otherwise.

Anything else (word shares, upper-case hex, text that is not laid out as a share) goes to another reader, given as an argument: decodeAscii for ASCII text and decodeShare itself for the rest (AsciiDecode). decodeFormWith_eq proves that both branches return exactly what decodeShare returns, packed.

The byte of the lowercase hex digit for the low 4 bits of n.

Equations
Instances For

    Append the hex digits of the n bytes of src from position i to acc.

    Equations
    Instances For

      Append the bytes of a list to acc, one by one (so that acc keeps its capacity).

      Equations
      Instances For
        theorem QedShare.Cli.pushAll_eq (acc : ByteArray) (l : List UInt8) :
        pushAll acc l = acc ++ l.toByteArray

        pushAll appends the list.

        An empty array with room for n bytes is an empty array.

        Feed the n bytes of src from position i into the running CRC-32 state.

        Equations
        Instances For

          The checksum of a packed record, read from the byte array (checksumPacked_eq).

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

            The hex share text of a record, with crc in the checksum field. With the record's own checksum this is encodeChars (encodeCharsCrc_checksum); with another one it is a share whose checksum does not match.

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

              The UTF-8 bytes of the hex share text of a packed record with crc in the checksum field (encodePacked_eq).

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

                The byte written as the two hex digits at positions i and i + 1, if they are hex digits.

                Equations
                Instances For

                  Read hex digit pairs from position i (at most n of them) into acc, up to the first byte that is not a hex digit. Returns the bytes read and the position after them.

                  Equations
                  Instances For

                    A candidate record and checksum, read from the positions a hex share starting at byte o has its fields at: qsN-<8>-<2>-<2>-<payload>[-<32>]-<8>. This only has to be right for well-formed shares: decodePacked checks the candidate against the input before using it.

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

                      Space, tab, carriage return or line feed: the bytes of the characters trimWs removes.

                      Equations
                      Instances For

                        What a reader of shares into packed records has to return: whether the text is in the hex form (looksHex), and the packed record decodeShare returns, or its error.

                        Equations
                        Instances For

                          Read one share from the UTF-8 bytes bs of its text into a packed record, and say whether it is in the hex form. Text laid out as a hex share, with whitespace around it, is read here (also when its checksum does not match); for everything else the answer is other ().

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

                            Read one share, hex or words, into a packed record, and say whether it is in the hex form. A well-formed hex share is read from its bytes (decodeFormB); everything else by other. Returns readerSpec if other does (decodeFormWith_eq).

                            Equations
                            Instances For

                              decodePacked = decodeShare, packed #

                              theorem QedShare.Cli.crcInto_eq (src : ByteArray) (n i : Nat) (st : UInt32) :

                              crcInto is the CRC fold over the bytes it reads.

                              The checksum read from the byte array is the checksum of the record.

                              theorem QedShare.Cli.hexInto_eq (src : ByteArray) (n i : Nat) (acc : ByteArray) :
                              hexInto src n i acc = acc ++ (List.flatMap (fun (b : UInt8) => [hexByte (b >>> 4), hexByte b]) (List.map src.get! (List.range' i n))).toByteArray

                              hexInto appends the hex digits of the bytes it reads.

                              The bytes of hex-encoded text are the hex digit bytes of each byte.

                              An ASCII character is one byte in UTF-8.

                              The UTF-8 bytes of ASCII text are its characters as bytes.

                              A hex digit is ASCII.

                              theorem QedShare.Cli.hexEncode_ascii (bs : List UInt8) (c : Char) :
                              c ∈ Hex.encode bs → c.val.toNat ≤ 127

                              Hex-encoded text is ASCII.

                              theorem QedShare.Cli.hex_ascii (r : Encoding.ShareRecord) (crc : UInt32) (c : Char) :
                              c ∈ encodeCharsCrc r crc → c.val.toNat ≤ 127

                              A hex share is ASCII, whatever its checksum field.

                              A hex share contains no whitespace, whatever its checksum field.

                              A hex share has at most 61 + 2·(payload bytes) characters, whatever its checksum field.

                              Decoding share text with crc in the checksum field gives the record if crc is the record's checksum, and the checksum error otherwise.

                              What the CLI's reader does with share text. Share text of a record with crc in the checksum field is read by decodeShare as that record if crc is the record's checksum, and refused with the checksum error otherwise. (decodeShare_encodeRecord is the first case.)

                              encodePacked is the UTF-8 encoding of the record's hex share text with that checksum.

                              The whitespace character with byte b (a space for any other byte).

                              Equations
                              Instances For

                                A whitespace byte is the UTF-8 encoding of a whitespace character.

                                Whitespace bytes are the UTF-8 encoding of whitespace characters.

                                theorem QedShare.Cli.trimWs_ws_append_ws (pre l post : List Char) (hne : l ≠ []) (h : ∀ (c : Char), c ∈ l → c.isWhitespace = false) (hpre : ∀ (c : Char), c ∈ pre → c.isWhitespace = true) (hp : ∀ (c : Char), c ∈ post → c.isWhitespace = true) :

                                trimWs removes whitespace around non-empty text that contains none.

                                Share text, whatever its checksum field, is in the hex form.

                                theorem QedShare.Cli.decodeFormWith_eq (other : String → String → Bool × Except String PackedRecord) (hother : ∀ (label s : String), other label s = readerSpec label s) (label s : String) :
                                decodeFormWith other label s = readerSpec label s

                                decodeFormWith reads what decodeShare reads, packed, if the reader it is given for the other texts does. For every label and text, it returns whether the text is in the hex form (looksHex), and the packed record of the record decodeShare returns, or the same error.