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
- QedShare.Cli.hexInto src 0 x✝¹ x✝ = x✝
- QedShare.Cli.hexInto src n.succ x✝¹ x✝ = QedShare.Cli.hexInto src n (x✝¹ + 1) ((x✝.push (QedShare.Cli.hexByte (src.get! x✝¹ >>> 4))).push (QedShare.Cli.hexByte (src.get! x✝¹)))
Instances For
Append the bytes of a list to acc, one by one (so that acc keeps its capacity).
Equations
- QedShare.Cli.pushAll acc l = List.foldl ByteArray.push acc l
Instances For
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
- QedShare.Cli.crcInto src 0 x✝¹ x✝ = x✝
- QedShare.Cli.crcInto src n.succ x✝¹ x✝ = QedShare.Cli.crcInto src n (x✝¹ + 1) (QedShare.Crc32.updateByte x✝ (src.get! x✝¹))
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
- QedShare.Cli.hexAt bs i = do let h ← QedShare.Hex.undigit (Char.ofNat (bs.get! i).toNat) let l ← QedShare.Hex.undigit (Char.ofNat (bs.get! (i + 1)).toNat) pure (h <<< 4 ||| l)
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
- QedShare.Cli.unhexInto bs 0 x✝¹ x✝ = (x✝, x✝¹)
- QedShare.Cli.unhexInto bs n.succ x✝¹ x✝ = match QedShare.Cli.hexAt bs x✝¹ with | some b => QedShare.Cli.unhexInto bs n (x✝¹ + 2) (x✝.push b) | none => (x✝, x✝¹)
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
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
- QedShare.Cli.decodeFormWith other label s = QedShare.Cli.decodeFormB (fun (x : Unit) => other label s) label s.toUTF8
Instances For
decodePacked = decodeShare, packed #
crcInto is the CRC fold over the bytes it reads.
The checksum read from the byte array is the checksum of the record.
The bytes of hex-encoded text are the hex digit bytes of each byte.
Hex-encoded text is ASCII.
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.
With the record's own checksum, encodeCharsCrc is encodeChars.
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.
trimWs removes whitespace around non-empty text that contains none.
Share text, whatever its checksum field, is in the hex form.
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.