Pure CLI pipeline #
The glue between bytes and share strings, with no IO, so the whole CLI flow can be tested
deterministically. QedShareCli.Main only reads input, fetches OS randomness, and writes output.
makeShares: secret bytes + random bytes → share strings (qs3-…hex or BIP-39 words)recoverSecret: share strings (hex, words, or a mix) → secret bytesconvertShares: share strings → the same shares in the other formverifyShares: share strings → "they rebuild a secret", without returning the secretdescribeShares: share strings → what each share says about itself (info)stdinShares,fileShares(andstdinShareRanges,fileShareRanges, which do not copy the shares out of the text): the text of stdin or of a file → labelled share strings
Error messages never contain secret bytes or share payloads.
The largest secret the CLI accepts: 1 MiB. Shamir sharing is meant for keys and passwords; to protect a large file, encrypt it and split the key. (The core works on lists, so much larger inputs would use a lot of memory.)
Equations
- QedShare.Cli.maxSecretBytes = 1024 * 1024
Instances For
Longest share string recoverSecret accepts: the encoding of a share of a
maxSecretBytes secret (qs3 with k = 255: 6 dashes, set id, k, x, token, checksum, and
the payload, all hex).
Equations
- QedShare.Cli.maxShareChars = 3 + 6 + 8 + 2 + 2 + 32 + 8 + 2 * (QedShare.Cli.maxSecretBytes + QedShare.Cli.tagBytes QedShare.Encoding.Fmt.v3 255)
Instances For
The largest secret a word share can hold: its payload must fit the one-byte length field, so
223 − 16k bytes for qs3 (0 for k ≥ 14), 223 for qs2 and 251 for qs1
(README: Share format).
Equations
- QedShare.Cli.maxWordsSecretBytes k fmt = 255 - QedShare.Cli.tagBytes fmt k
Instances For
Longest text accepted as a word share. The longest real one (255-byte payload) has ⌈8·267/11⌉ = 195 words of at most 8 letters; this leaves plenty of room for extra whitespace.
Equations
Instances For
Write a share record in the chosen form.
Equations
Instances For
Number of random bytes makeShares needs: k − 1 coefficients per payload byte, plus the
16-byte AMD key (qs2, qs3) and the 16k bytes of the token polynomial (qs3).
Equations
- QedShare.Cli.randomBytesNeeded k secretLen QedShare.Encoding.Fmt.v1 = (secretLen + 4) * (k - 1)
- QedShare.Cli.randomBytesNeeded k secretLen QedShare.Encoding.Fmt.v2 = 16 + (secretLen + 32) * (k - 1)
- QedShare.Cli.randomBytesNeeded k secretLen = 16 + 16 * k + (secretLen + 32 + 16 * k) * (k - 1)
Instances For
The bytes to split for the format, the token polynomial (qs3; empty before), and the
random bytes left for the coefficients. The AMD key is the first 16 random bytes; for qs3 the
next 16k are the token polynomial.
Equations
- One or more equations did not get rendered due to their size.
- QedShare.Cli.tagSecret QedShare.Encoding.Fmt.v1 k secret random = (List.map QedShare.GF256.mk (QedShare.Encoding.withTag secret), [], random)
Instances For
Check and remove the secret tag (and, for qs3, the tokens of the shares used): some secret, or none if a check fails.
Equations
- One or more equations did not get rendered due to their size.
- QedShare.Cli.untagSecret QedShare.Encoding.Fmt.v1 k tagged toks = QedShare.Encoding.stripTag (List.map (fun (x : QedShare.GF256) => x.val) tagged)
- QedShare.Cli.untagSecret QedShare.Encoding.Fmt.v2 k tagged toks = Option.map (fun (x : List QedShare.GF256) => List.map (fun (x : QedShare.GF256) => x.val) x) (QedShare.Amd.decode tagged)
Instances For
The (index, token) pairs of some records.
Equations
- QedShare.Cli.tokensOf records = List.map (fun (r : QedShare.Encoding.ShareRecord) => (r.share.x, r.token)) records
Instances For
Cut bytes into len coefficient lists of k − 1 field elements each (chunk b is
bytes[b·(k−1) … (b+1)·(k−1))). Slicing an Array keeps this linear in the secret length.
Equations
- QedShare.Cli.chunkRandomness k len bytes = List.map (fun (b : Nat) => List.map QedShare.GF256.mk (bytes.toArray.extract (b * (k - 1)) ((b + 1) * (k - 1))).toList) (List.range len)
Instances For
Read 4 bytes as a big-endian UInt32 (or 0 if there are fewer than 4).
Equations
- QedShare.Cli.setIdOf bytes = (QedShare.Encoding.fromBytes4 (List.take 4 bytes)).getD 0
Instances For
Split secret into share strings with threshold k and n shares, using random
(at least randomBytesNeeded k secret.length bytes) and setIdBytes (4 bytes). The CLI always
writes qs3; fmt := .v1/.v2 exist only so the tests can make old-format shares.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Remove leading and trailing whitespace.
Equations
Instances For
Why a set of share strings could not be turned back into the secret.
Equations
- QedShare.Cli.describeDecodeError QedShare.Encoding.DecodeError.badLayout = "not a qedshare share (expected qs3-<setid>-<k>-<x>-<payload>-<token>-<checksum>)"
- QedShare.Cli.describeDecodeError QedShare.Encoding.DecodeError.unknownTag = "unknown share format (expected a share starting with qs3-, qs2- or qs1-)"
- QedShare.Cli.describeDecodeError QedShare.Encoding.DecodeError.badField = "a field is not valid hex or has the wrong length"
- QedShare.Cli.describeDecodeError QedShare.Encoding.DecodeError.badChecksum = "checksum mismatch: the share was mistyped or damaged"
Instances For
Why a word share could not be read. Positions are shown counting from 1.
Equations
- QedShare.Cli.describeWordsError (QedShare.Mnemonic.DecodeError.unknownWord pos) = toString "word " ++ toString (pos + 1) ++ toString " is not in the word list (check its spelling)"
- QedShare.Cli.describeWordsError QedShare.Mnemonic.DecodeError.badLength = "wrong number of words for a word share (a word missing or extra?)"
- QedShare.Cli.describeWordsError QedShare.Mnemonic.DecodeError.badVersion = "unknown word-share version"
- QedShare.Cli.describeWordsError QedShare.Mnemonic.DecodeError.notCanonical = "the words do not form a valid word share (a word mistyped or out of order?)"
- QedShare.Cli.describeWordsError QedShare.Mnemonic.DecodeError.badChecksum = "checksum mismatch: a word was mistyped, changed or swapped"
Instances For
true for the hex form: a single run of text containing - (qs3-…, qs2-…, qs1-…).
Anything else is read as words.
Equations
- QedShare.Cli.looksHex s = (s.toList.contains '-' && !s.toList.any Char.isWhitespace)
Instances For
Read one share, hex or words (label names the file or line in errors).
Equations
- One or more equations did not get rendered due to their size.
Instances For
Describe a combine error.
Equations
- QedShare.Cli.describeCombineError QedShare.Shamir.CombineError.tooFewShares = "not enough shares for this threshold"
- QedShare.Cli.describeCombineError QedShare.Shamir.CombineError.zeroIndex = "a share has index 0, which qedshare never produces"
- QedShare.Cli.describeCombineError QedShare.Shamir.CombineError.duplicateIndex = "the same share was given twice"
- QedShare.Cli.describeCombineError QedShare.Shamir.CombineError.lengthMismatch = "the shares have different lengths (they are not from the same split)"
- QedShare.Cli.describeCombineError QedShare.Shamir.CombineError.inconsistent = "the shares do not all fit together: at least one of them is wrong or forged"
Instances For
Above this much work (shares² × payload bytes), oddOneOut is skipped: it runs combine
once per share, and is only a hint.
Equations
- QedShare.Cli.maxOddOneOutWork = 64 * 1024 * 1024
Instances For
After combine found that the shares do not all fit together: if leaving out exactly one
share makes the rest fit (and pass the secret tag), return that share's label.
Only tried with k + 2 or more shares, so the rest still has k + 1 shares to check against.
If exactly one share is bad, this always finds it (oddOneOut_names_bad, in proofs/): without
it, the k + 1 or more genuine shares fit (combine_split); without any other share, the bad
one is still among k or more genuine ones and is caught (combine_detects_forgery). With several bad shares it may find none,
or (if the cheaters work together and few genuine shares are given) point at the wrong one, so it
is reported as a hint, never as proof.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Rewrite each share in the given form. Every share is fully checked (checksums included) on the way; nothing is combined, so no secret is ever rebuilt.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Checking and describing shares #
What verify prints for count shares that rebuild a secret of secretLen bytes: only
those two numbers, never the secret. weak adds the note for old qs1 shares given without an
extra share.
Equations
- One or more equations did not get rendered due to their size.
Instances For
What verify prints for shares that rebuild a secret of secretLen bytes.
Equations
- One or more equations did not get rendered due to their size.
- QedShare.Cli.verifyReport [] secretLen = QedShare.Cli.verifyMessage [].length secretLen false
Instances For
Check that the shares rebuild a secret, exactly as recoverSecret does, and say so without
returning the secret: the message only has the number of shares and the secret's length.
Equations
- QedShare.Cli.verifyShares inputs = do let secret ← QedShare.Cli.recoverSecret inputs pure (QedShare.Cli.verifyReport inputs secret.length)
Instances For
The name of a share format: qs1, qs2 or qs3.
Equations
Instances For
A set id as 8 hex digits, as in the hex form of a share.
Equations
- QedShare.Cli.setIdHex setId = String.ofList (QedShare.Hex.encode (QedShare.Crc32.toBytes setId))
Instances For
What info reads from one share: everything except the payload bytes, of which only the
number is kept. All of it is written in the clear in the share, so none of it is secret.
- fmt : Encoding.Fmt
The share format.
- k : UInt8
The threshold of the split.
- x : GF256
The share index.
- setId : UInt32
The set id.
- len : Nat
The number of payload bytes.
Instances For
What one share says about itself.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The lines of info for shares already read (label, whether in the hex form, and what was
read or the error): one line per share, then whether there are enough of them. The Bool is
false if a share could not be read.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Describe each share (info), then say whether there are enough of them. Unlike combine,
this keeps going past a share that cannot be read; the Bool is false if there was one.
Nothing is combined, so no secret is rebuilt, and whether the shares fit together is not checked
(verifyShares does that).
Equations
- One or more equations did not get rendered due to their size.
Instances For
From input text to share strings #
true if the text is empty or only whitespace: what (trimWs s).isEmpty says, without
building the trimmed text (isBlank_eq).
Equations
Instances For
Position of the first byte from i on that is not whitespace (at most n steps).
Equations
Instances For
Position after the last byte before j that is not whitespace (at most n steps).
Equations
- One or more equations did not get rendered due to their size.
- QedShare.Cli.skipWsEnd bs 0 x✝ = x✝
Instances For
Loop for lineRanges: start is where the current line begins, i the byte looked at.
Equations
Instances For
The lines of the bytes from start on, as byte ranges (from, to) without the line feeds.
(A line feed is a single byte in UTF-8, and no byte of a longer character equals it.)
Equations
- QedShare.Cli.lineRanges bs start = QedShare.Cli.lineRangesFrom bs bs.size start start #[]
Instances For
Bytes a to b as text. (Empty if they are not UTF-8, which does not happen for a line or
the whole of a text.)
Equations
- QedShare.Cli.sliceText bs a b = (String.fromUTF8? (bs.extract a b)).getD ""
Instances For
The non-blank lines of a text (given as its UTF-8 bytes): line number (counting from 1) and byte range. The lines are not copied out of the text: a share can be megabytes long, and a file can hold many.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The shares on stdin (given as UTF-8 bytes): one per non-blank line, labelled by line number,
as byte ranges (sliceText gives the share).
Equations
- One or more equations did not get rendered due to their size.
Instances For
The shares in the file name with contents bs (UTF-8 bytes), as byte ranges (sliceText
gives the share).
A file usually holds one share, and a word share may be wrapped over several lines, so the whole
file is one share when it has a single non-blank line or reads as one share. A file may also hold
several shares, one per line (what split prints): that is how it is read when every line looks
like a hex share, or when the file as a whole is not a share but at least one of its lines is.
Then each share is labelled with its line number.
isHex a b says whether bytes a to b look like a hex share: looksHex on the trimmed text
(fileShares), or the executable's looksHexFast, which is the same (looksHexFast_eq).
isShare label a b says whether they read as a share: decodeShare succeeds on them
(fileShares), or the executable's reader does, which is the same (decodeBytes_eq).
Equations
- One or more equations did not get rendered due to their size.
Instances For
The shares in the file name with contents text (see fileShareRanges).
Equations
- One or more equations did not get rendered due to their size.