Documentation

QedShareCli.AsciiDecode

Reading any ASCII share text from its bytes #

decodePackedForm (in FastDecode) reads well-formed hex shares from their bytes and hands everything else to the list-based decodeShare: a share that is cut short, has a character that is not a hex digit, or is written in upper case. For a share of a 1 MiB secret that costs about 100 MB.

decodeAscii reads any text made of ASCII bytes without a list as long as the text. It follows decodeShare: trim, decide between hex and words, check the length, count the -, cut at them, decode the fields. Every field is hex-decoded straight from the bytes into a byte array; a field other than the payload that is too long to be valid is not decoded at all (clip). decodeAscii_eq proves that it returns exactly what decodeShare returns.

ASCII bytes and characters #

The character with code b. For b < 128 its UTF-8 encoding is the byte b.

Equations
Instances For

    What asciiChar does on ASCII bytes.

    Bytes x to y of the array, as a list.

    Equations
    Instances For
      def QedShare.Cli.seg (bs : ByteArray) (x y : Nat) :

      Bytes x to y of the array, as characters.

      Equations
      Instances For
        theorem QedShare.Cli.take_drop_split {α : Type} (L : List α) {x y z : Nat} (h1 : x ≤ y) (h2 : y ≤ z) :

        Positions x to z of a list are positions x to y, then y to z.

        theorem QedShare.Cli.segB_append (bs : ByteArray) {x y z : Nat} (h1 : x ≤ y) (h2 : y ≤ z) :
        segB bs x z = segB bs x y ++ segB bs y z

        A segment is two adjacent segments.

        theorem QedShare.Cli.seg_append (bs : ByteArray) {x y z : Nat} (h1 : x ≤ y) (h2 : y ≤ z) :
        seg bs x z = seg bs x y ++ seg bs y z

        A segment is two adjacent segments.

        theorem QedShare.Cli.segB_all (bs : ByteArray) :
        segB bs 0 bs.size = bs.data.toList

        The whole array as a segment.

        theorem QedShare.Cli.data_extract_eq (bs : ByteArray) (x y : Nat) :
        (bs.extract x y).data.toList = segB bs x y

        A slice of the array is a segment.

        theorem QedShare.Cli.map_get!_seg (bs : ByteArray) {x y : Nat} (h1 : x ≤ y) (h2 : y ≤ bs.size) :
        List.map bs.get! (List.range' x (y - x)) = segB bs x y

        Reading bytes x to y one by one gives the segment.

        theorem QedShare.Cli.segB_one (bs : ByteArray) {x : Nat} (h : x < bs.size) :
        segB bs x (x + 1) = [bs.get! x]

        The segment of one position is the byte there.

        Loops over a range of bytes #

        theorem QedShare.Cli.allBytes_seg (p : UInt8 → Bool) (bs : ByteArray) {x y : Nat} (h1 : x ≤ y) (h2 : y ≤ bs.size) :
        allBytes p bs (y - x) x = (segB bs x y).all p

        allBytes on a segment.

        theorem QedShare.Cli.ofList_seg (s : String) (h : (s.toUTF8.data.toList.all fun (x : UInt8) => decide (x < 128)) = true) :

        A string whose bytes are all ASCII is the string of those bytes as characters.

        Hex digits from bytes #

        Read n pairs of hex digits from position i into acc; none at a byte that is not a hex digit.

        Equations
        Instances For

          Text with an odd number of characters is not hex.

          theorem QedShare.Cli.unhexAll_eq (bs : ByteArray) (n i : Nat) (acc : ByteArray) :
          unhexAll bs n i acc = Option.map (fun (l : List UInt8) => acc ++ l.toByteArray) (Hex.decode (List.map asciiChar (List.map bs.get! (List.range' i (2 * n)))))

          unhexAll is Hex.decode on the bytes it reads, as characters.

          Decoding the fields, with the payload as a byte array #

          The bytes of a hex field, as a byte array.

          Equations
          Instances For

            decodeFields on fields that are already hex-decoded into byte arrays, giving a packed record (fieldsPacked_eq). Only the payload stays an array; the other fields are short in any share, and are looked at as lists like in decodeFields.

            Equations
            Instances For

              decodeChars after the text is cut into its tag t and the other fields, hex-decoded (tagged_eq).

              Equations
              Instances For

                The list of a byte array made from a list is that list.

                theorem QedShare.Cli.fieldsPacked_eq (f : Encoding.Fmt) (sid kk xx pl tk cc : List Char) :
                fieldsPacked f (hexB sid) (hexB kk) (hexB xx) (hexB pl) (hexB tk) (hexB cc) = Except.map packRecord (Encoding.decodeFields f sid kk xx pl tk cc)

                fieldsPacked on the hex-decoded fields is decodeFields, packed.

                decodeTagged on the tag and the hex-decoded other fields is decodeChars, packed.

                Trimming #

                theorem QedShare.Cli.trimWs_sandwich (pre m post : List Char) (hne : m ≠ []) (hh : ∀ (c : Char), c ∈ m.head? → c.isWhitespace = false) (hl : ∀ (c : Char), c ∈ m.getLast? → c.isWhitespace = false) (hpre : ∀ (c : Char), c ∈ pre → c.isWhitespace = true) (hpost : ∀ (c : Char), c ∈ post → c.isWhitespace = true) :

                trimWs removes the whitespace around text that starts and ends with other characters.

                decodeShare only looks at the trimmed text.

                Segments of ASCII bytes #

                theorem QedShare.Cli.mem_segB {bs : ByteArray} {x y : Nat} {b : UInt8} (h : b ∈ segB bs x y) :

                The bytes of a segment are bytes of the array.

                theorem QedShare.Cli.length_segB (bs : ByteArray) {x y : Nat} (h : y ≤ bs.size) :
                (segB bs x y).length = y - x

                The length of a segment.

                theorem QedShare.Cli.contains_dash_ascii (l : List UInt8) (h : ∀ (b : UInt8), b ∈ l → b < 128) :
                (List.map asciiChar l).contains '-' = !l.all fun (x : UInt8) => x != 45

                A - among ASCII characters is a byte 45.

                theorem QedShare.Cli.any_ws_ascii (l : List UInt8) (h : ∀ (b : UInt8), b ∈ l → b < 128) :

                Whitespace among ASCII characters is a whitespace byte.

                theorem QedShare.Cli.looksHex_ascii (l : List UInt8) (h : ∀ (b : UInt8), b ∈ l → b < 128) :
                looksHex (String.ofList (List.map asciiChar l)) = ((!l.all fun (x : UInt8) => x != 45) && l.all fun (b : UInt8) => !isWsByte b)

                Whether ASCII text looks like a hex share, read from its bytes.

                theorem QedShare.Cli.trimWs_seg (s : String) (i j : Nat) (hasc : ∀ (b : UInt8), b ∈ s.toUTF8.data.toList → b < 128) (hij : i < j) (hjn : j ≤ s.toUTF8.size) (hpre : (segB s.toUTF8 0 i).all isWsByte = true) (hpost : (segB s.toUTF8 j s.toUTF8.size).all isWsByte = true) (hi : isWsByte (s.toUTF8.get! i) = false) (hj : isWsByte (s.toUTF8.get! (j - 1)) = false) :

                The trimmed text of an ASCII string, as a segment of its bytes: whitespace before i and from j on, other bytes at i and j − 1.

                The fields of a hex share #

                def QedShare.Cli.countBytes (p : UInt8 → Bool) (bs : ByteArray) :
                Nat → Nat → Nat → Nat

                How many of the n bytes from position i satisfy p, added to acc.

                Equations
                Instances For
                  theorem QedShare.Cli.countBytes_eq (p : UInt8 → Bool) (bs : ByteArray) (n i acc : Nat) :
                  countBytes p bs n i acc = acc + List.countP p (List.map bs.get! (List.range' i n))

                  countBytes is List.countP on the bytes it reads.

                  theorem QedShare.Cli.countBytes_seg (p : UInt8 → Bool) (bs : ByteArray) {x y : Nat} (h1 : x ≤ y) (h2 : y ≤ bs.size) :
                  countBytes p bs (y - x) x 0 = List.countP p (segB bs x y)

                  countBytes on a segment.

                  Text has one field more than it has -.

                  theorem QedShare.Cli.count_dash_ascii (l : List UInt8) (h : ∀ (b : UInt8), b ∈ l → b < 128) :
                  List.count '-' (List.map asciiChar l) = List.countP (fun (x : UInt8) => x == 45) l

                  The - among ASCII characters are the bytes 45.

                  theorem QedShare.Cli.not_dash_seg (bs : ByteArray) (a b : Nat) (hasc : ∀ (c : UInt8), c ∈ bs.data.toList → c < 128) (hP : ((segB bs a b).all fun (x : UInt8) => x != 45) = true) :
                  ¬'-' ∈ seg bs a b

                  A segment of ASCII bytes other than 45 has no -.

                  def QedShare.Cli.cutAt (bs : ByteArray) (j : Nat) :
                  Nat → List Nat → Option (List (Nat × Nat))

                  Check that the positions ps are the - between i and j, and give the fields they cut that range into, as ranges.

                  Equations
                  Instances For
                    theorem QedShare.Cli.cutAt_eq (bs : ByteArray) (j : Nat) (hasc : ∀ (c : UInt8), c ∈ bs.data.toList → c < 128) (hjn : j ≤ bs.size) (ps : List Nat) (i : Nat) (rs : List (Nat × Nat)) (h : cutAt bs j i ps = some rs) :
                    Encoding.splitDash (seg bs i j) = List.map (fun (r : Nat × Nat) => seg bs r.fst r.snd) rs ∧ ∀ (r : Nat × Nat), r ∈ rs → r.fst ≤ r.snd ∧ r.snd ≤ j

                    The ranges cutAt gives are the fields of the text, in order.

                    Loop for dashes.

                    Equations
                    Instances For

                      The positions of the bytes 45 between i and j. Nothing is proven about it: cutAt checks them.

                      Equations
                      Instances For
                        theorem QedShare.Cli.unhex_seg (bs : ByteArray) (a b : Nat) (hab : a ≤ b) (hbn : b ≤ bs.size) :
                        (if (b - a) % 2 = 1 then none else unhexAll bs ((b - a) / 2) a (ByteArray.emptyWithCapacity ((b - a) / 2))) = hexB (seg bs a b)

                        The long field, hex-decoded from the bytes.

                        The reader #

                        Texts up to this many bytes are read by the list-based reader: lists that short cost nothing worth saving.

                        Equations
                        Instances For

                          Hex text of 2n characters decodes to n bytes.

                          A hex field that is too long for any field but the payload reads as "not hex": every such field has at most 16 bytes, so the result is the same (fieldsPacked_clip_sid and the following), and no list of a long field is ever built.

                          Equations
                          Instances For

                            More than four bytes are not a 32-bit number.

                            theorem QedShare.Cli.two_of_long {α : Type} (l : List α) (h : l.length > 16) :
                            ∃ (x : α), ∃ (y : α), ∃ (r : List α), l = x :: y :: r

                            A long list has two elements.

                            theorem QedShare.Cli.fieldsPacked_clip_sid (f : Encoding.Fmt) (sid kk xx pl tk cc : Option ByteArray) :
                            fieldsPacked f (clip sid) kk xx pl tk cc = fieldsPacked f sid kk xx pl tk cc

                            fieldsPacked gives the same for a set-id field that is too long as for one that is not hex.

                            theorem QedShare.Cli.fieldsPacked_clip_cc (f : Encoding.Fmt) (sid kk xx pl tk cc : Option ByteArray) :
                            fieldsPacked f sid kk xx pl tk (clip cc) = fieldsPacked f sid kk xx pl tk cc

                            fieldsPacked gives the same for a set-id field that is too long as for one that is not hex.

                            theorem QedShare.Cli.fieldsPacked_clip_kk (f : Encoding.Fmt) (sid kk xx pl tk cc : Option ByteArray) :
                            fieldsPacked f sid (clip kk) xx pl tk cc = fieldsPacked f sid kk xx pl tk cc

                            fieldsPacked gives the same for a set-id field that is too long as for one that is not hex.

                            theorem QedShare.Cli.fieldsPacked_clip_xx (f : Encoding.Fmt) (sid kk xx pl tk cc : Option ByteArray) :
                            fieldsPacked f sid kk (clip xx) pl tk cc = fieldsPacked f sid kk xx pl tk cc

                            fieldsPacked gives the same for a set-id field that is too long as for one that is not hex.

                            theorem QedShare.Cli.fieldsPacked_clip_tk (f : Encoding.Fmt) (sid kk xx pl tk cc : Option ByteArray) :
                            fieldsPacked f sid kk xx pl (clip tk) cc = fieldsPacked f sid kk xx pl tk cc

                            fieldsPacked gives the same for a set-id field that is too long as for one that is not hex.

                            Clipping a hex field: none if it has more than 32 characters.

                            The payload field r, hex-decoded from the bytes.

                            Equations
                            Instances For

                              A field r other than the payload, hex-decoded from the bytes if it is short enough to be one.

                              Equations
                              Instances For
                                theorem QedShare.Cli.unhexR_eq (bs : ByteArray) (r : Nat × Nat) (h : r.fst ≤ r.snd ∧ r.snd ≤ bs.size) :
                                unhexR bs r = hexB (seg bs r.fst r.snd)

                                unhexR is Hex.decode on the field.

                                theorem QedShare.Cli.shortR_eq (bs : ByteArray) (r : Nat × Nat) (h : r.fst ≤ r.snd ∧ r.snd ≤ bs.size) :
                                shortR bs r = clip (hexB (seg bs r.fst r.snd))

                                shortR is Hex.decode on the field, clipped.

                                The fields after the tag, hex-decoded from the bytes: the payload in full, the others only if they are short.

                                Equations
                                Instances For
                                  theorem QedShare.Cli.decodeTagged_fieldsOf (bs : ByteArray) (t : List Char) (rs : List (Nat × Nat)) (h : ∀ (r : Nat × Nat), r ∈ rs → r.fst ≤ r.snd ∧ r.snd ≤ bs.size) :
                                  decodeTagged t (fieldsOf bs rs) = decodeTagged t (List.map (fun (r : Nat × Nat) => hexB (seg bs r.fst r.snd)) rs)

                                  decodeTagged gives the same on fieldsOf as on the hex-decoded fields.

                                  A tag has three characters.

                                  The tag field r as characters, if it has the length of a tag.

                                  Equations
                                  Instances For

                                    tagOf names the same format as the field.

                                    decodeTagged only looks at the format the tag names.

                                    Read one share from ASCII bytes without a list as long as the text; none if the bytes are not all ASCII (then the caller uses decodeShare). What it returns is what looksHex and decodeShare return (decodeAscii_eq).

                                    It follows decodeShare: trim (the ends found by skipWs and skipWsEnd are checked), decide between hex and words, check the length, count the - (anything but 5 or 6 is not a share), cut at them (the positions found by dashes are checked by cutAt), and decode the fields from the bytes.

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

                                      decodeAscii reads what decodeShare reads. Whenever it gives an answer for the bytes of a text, that answer is whether the text is in the hex form (looksHex), and the packed record decodeShare returns, or the same error.