Documentation

QedShare.Encoding.Hex

Hexadecimal #

Bytes ⇄ lowercase hex characters. Decoding also accepts uppercase, so shares survive being typed by hand or "fixed" by an editor. Works on List Char so that round-trip proofs are simple inductions; the share format turns the result into a String at the very end.

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

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

    The value of a hex digit (either case), or none for any other character.

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

      Two hex characters for one byte, high nibble first.

      Equations
      Instances For

        Decode hex characters (an even number of hex digits) back to bytes.

        Equations
        Instances For

          Tail-recursive versions for compiled code #

          encode and decode recurse once per byte, so on a large secret the compiled code would run out of stack. The versions below use a loop with an accumulator. Each @[csimp] theorem proves it equal to the specification above, and the compiler then uses it in place of the specification. The proofs keep talking about encode / decode.

          Loop for decodeTR: decoded bytes so far are in acc.

          Equations
          Instances For
            theorem QedShare.Hex.decodeAux_eq (cs : List Char) (acc : Array UInt8) :
            decodeAux cs acc = Option.map (fun (x : List UInt8) => acc.toList ++ x) (decode cs)

            The loop decodeAux decodes like decode, after the bytes already collected.

            @[csimp]

            The loop version computes exactly decode (@[csimp]: compiled code runs it).

            Hex encoding as a flatMap (which Lean's compiler already runs as a loop).

            Equations
            Instances For
              @[csimp]

              The flatMap version computes exactly encode (@[csimp]: compiled code runs it).

              theorem QedShare.Hex.decode_encodeByte (b : UInt8) :
              (do let h ← undigit (digit (b >>> 4)) let l ← undigit (digit b) pure (h <<< 4 ||| l)) = some b

              Decoding one encoded byte gives the byte back (checked for all 256 bytes).

              Hex decoding inverts hex encoding.

              Hex encoding never produces a - (so - can separate fields).