Documentation

QedShare.Encoding.Format

Share text formats qs1, qs2 and qs3 #

qs1-<setid>-<k>-<x>-<payload>-<checksum>
qs2-<setid>-<k>-<x>-<payload>-<checksum>
qs3-<setid>-<k>-<x>-<payload>-<token>-<checksum>

All fields are lowercase hex (README: Share format):

FieldBytesMeaning
qs1/qs2/qs3—Format tag and version
setid4Random, the same for every share of one split
k1Threshold
x1Share index (never 0 for shares made by split)
payloadanyOne byte per byte of secret ‖ tag
token16 (qs3)The share's token G(x) (Token.lean); not interpolated
checksum4CRC-32 of setid ‖ k ‖ x ‖ payload (accidents only); for qs2, of 2 ‖ setid ‖ k ‖ x ‖ payload; for qs3, of 3 ‖ setid ‖ k ‖ x ‖ payload ‖ token

The versions differ in what protects the secret: qs1 carries a 4-byte CRC-32 of the secret (Tag.lean), qs2 a 32-byte AMD tag (Amd.lean), and qs3 the AMD tag over the secret and a token polynomial, plus a token per share (Token.lean), which catches a forged share with exactly k shares in every case. split writes qs3; combine reads all three.

decode_encode proves that decoding an encoded share gives back exactly the same record.

The share format version: v1 (qs1, CRC-32 secret tag), v2 (qs2, AMD tag) or v3 (qs3, AMD tag and share tokens).

  • v1 : Fmt

    qs1: the secret tag is CRC-32.

  • v2 : Fmt

    qs2: the secret tag is the AMD tag over GF(2¹²⁸).

  • v3 : Fmt

    qs3: the AMD tag over the secret and the token polynomial, and a token per share.

Instances For
    @[instance_reducible]
    Equations

    Token bytes in a share: 16 for qs3, none before.

    Equations
    Instances For

      A share together with the metadata written next to it.

      • setId : UInt32

        Random identifier shared by all shares of one split.

      • k : UInt8

        The threshold of the split.

      • share : Shamir.Share

        The share itself.

      • fmt : Fmt

        The format version.

      • token : List GF256

        The share's token (tokenLen fmt bytes: 16 for qs3, empty before).

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

            Why a share string could not be decoded.

            • badLayout : DecodeError

              Not six --separated fields (seven for qs3).

            • unknownTag : DecodeError

              The first field is not qs1, qs2 or qs3.

            • badField : DecodeError

              A field is not valid hex, or has the wrong length.

            • badChecksum : DecodeError

              The checksum does not match: the share was mistyped or damaged.

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

                Read the format tag.

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

                  Reading a written tag gives its format.

                  No tag contains -.

                  Split a character list at every -.

                  Equations
                  Instances For

                    Tail-recursive splitDash for compiled code #

                    splitDash recurses once per character, which overflows the stack on very long shares. The loop below is proved equal to it (splitDash_eq_splitDashTR, @[csimp]), so the compiler uses the loop and the proofs keep using splitDash.

                    Put p in front of the first field (or make it the only field).

                    Equations
                    Instances For

                      Loop for splitDashTR: acc holds the finished fields, cur the current field reversed.

                      Equations
                      Instances For

                        splitDash always returns at least one field.

                        A character other than - goes to the front of the first field.

                        Prepending to the first field twice is prepending the concatenation once.

                        Prepending nothing to a non-empty field list changes nothing.

                        The loop splitDashAux computes splitDash, after the fields already collected.

                        @[csimp]

                        The loop computes exactly splitDash; the compiler uses it in place of splitDash.

                        The bytes covered by the checksum: setid ‖ k ‖ x ‖ payload ‖ token, preceded by the version byte 2 for qs2 and 3 for qs3 (so a mistyped tag fails the checksum).

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

                          The token field with its leading - (only in qs3).

                          Equations
                          Instances For

                            Encode a record as characters.

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

                              Encode a record as a share string qs1-…, qs2-… or qs3-….

                              Equations
                              Instances For

                                Decode the fields after the tag (tk is the token field, empty before qs3).

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

                                  Decode characters into a record, checking layout, tag, field lengths and checksum.

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

                                    Decode a share string. (The CLI trims surrounding whitespace before calling this.)

                                    Equations
                                    Instances For

                                      Round trip #

                                      Text without - is a single field.

                                      theorem QedShare.Encoding.splitDash_append (f rest : List Char) (h : ¬'-' ∈ f) :
                                      splitDash (f ++ '-' :: rest) = f :: splitDash rest

                                      A field without -, then -, then the rest: the field comes first, then the rest's fields.

                                      Reading back the 4 bytes of a 32-bit word gives the word.

                                      Decoding an encoded share gives back exactly the same record (whose token has the length its format needs: 16 bytes for qs3, none before).

                                      Decoding an encoded share gives back exactly the same record (token of the right length).