From 3ff523a8689228032d9790a47bf3f153f3b04547 Mon Sep 17 00:00:00 2001 From: R Date: Fri, 4 Dec 2026 00:20:23 -0300 Subject: [PATCH 1/3] codecs: zbase32, the format of pkarr's keys on base32's one implementation (proposal, design with its rulings, spec, tasks, plan) --- README.md | 1 + .../openspec/changes/zbase32/.openspec.yaml | 11 ++ .../codecs/openspec/changes/zbase32/design.md | 97 +++++++++++++++ .../openspec/changes/zbase32/proposal.md | 56 +++++++++ .../changes/zbase32/reference/plan.md | 110 ++++++++++++++++++ .../changes/zbase32/specs/codecs/spec.md | 70 +++++++++++ .../codecs/openspec/changes/zbase32/tasks.md | 26 +++++ 7 files changed, 371 insertions(+) create mode 100644 libraries/codecs/openspec/changes/zbase32/.openspec.yaml create mode 100644 libraries/codecs/openspec/changes/zbase32/design.md create mode 100644 libraries/codecs/openspec/changes/zbase32/proposal.md create mode 100644 libraries/codecs/openspec/changes/zbase32/reference/plan.md create mode 100644 libraries/codecs/openspec/changes/zbase32/specs/codecs/spec.md create mode 100644 libraries/codecs/openspec/changes/zbase32/tasks.md diff --git a/README.md b/README.md index d4dcc9a..8625b7f 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ tag is placed. | cobalt | [genesis](libraries/cobalt/openspec/changes/genesis) | cobalt: existing Boost.Cobalt applications on native, browsers and WASIp2/P3 | wrapper | implementing | | 2026-11-06 | [code](https://github.com/webcpporg/cobalt) | | codecs | [genesis](libraries/codecs/openspec/changes/genesis) | codecs: one contract for turning bytes and values into text and back, one header per format, tokens like Boost.URL | own | implementing | | 2026-11-24 | [code](https://github.com/webcpporg/codecs) | | codecs | [one-verb](libraries/codecs/openspec/changes/one-verb) | codecs: one verb for every library: a value type's formats declared to codecs, the armor's label in its type, over decoding a value in place | own | implementing | | 2026-12-02 | [code](https://github.com/webcpporg/codecs) | +| codecs | [zbase32](libraries/codecs/openspec/changes/zbase32) | codecs: z-base-32, the format of pkarr's and iroh's keys, constant time and streaming, on base32's one implementation | own | implementing | | 2026-12-01 | [code](https://github.com/webcpporg/codecs) | | config | [genesis](libraries/config/openspec/changes/archive/2026-10-09-genesis) | config: webcpp's configuration, the target, exceptions and version every library includes first | own | implemented | 0.1.0 | 2026-10-09 | [code](https://github.com/webcpporg/config/tree/2fad162) | | config | [timing-guard](libraries/config/openspec/changes/archive/2026-11-24-timing-guard) | config: one DIT timing guard for every webcpp library, moved from crypto and hash, Windows on ARM64 covered | own | withdrawn | | 2026-11-24 | | | crypto | [genesis](libraries/crypto/openspec/changes/genesis) | crypto: authenticated encryption, randomness and secp256k1 over C backends | wrapper | implementing | | 2026-10-11 | [code](https://github.com/webcpporg/crypto) | diff --git a/libraries/codecs/openspec/changes/zbase32/.openspec.yaml b/libraries/codecs/openspec/changes/zbase32/.openspec.yaml new file mode 100644 index 0000000..98b8bb9 --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/.openspec.yaml @@ -0,0 +1,11 @@ +schema: spec-driven +created: 2026-12-04 +webcpp: + library: codecs + title: "codecs: z-base-32, the format of pkarr's and iroh's keys, constant time and streaming, on base32's one implementation" + kind: own + port_of: null + status: implementing + release: null + implementation: https://github.com/webcpporg/codecs + decided: 2026-12-01 diff --git a/libraries/codecs/openspec/changes/zbase32/design.md b/libraries/codecs/openspec/changes/zbase32/design.md new file mode 100644 index 0000000..36329aa --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/design.md @@ -0,0 +1,97 @@ +## Context + +codecs (genesis, `libraries/codecs/openspec/changes/genesis/`, and one-verb) has seven formats over +one contract. base32 and base32hex (genesis D13, B13) share one implementation, +`detail/formats/base32.hpp` (the arithmetic, a reader that accumulates its verdict by masks) and the +codec and stream codec templates of ``, parameterised by `bool Hex`; both +are constant time on every call (D12) and stream natively (D20). + +z-base-32 (Zooko O'Whielacronx, "human-oriented base-32 encoding", version 0.9.4.6, November 2009, +) is RFC 4648's base32 with +another alphabet, `ybndrfg8ejkmcpqxot1uwisza345h769`, no padding, and lower case. pkarr +(, read at `3dca3397409109f7da55d1c65ba7c3018dbfb5c7`, 2026-12-01) +writes its keys with the Rust crate `base32` 0.5.1, `Alphabet::Z`: its encoder is the RFC's +grouping over that alphabet, unpadded; its decoder maps lower case alone (its inverse table has no +upper-case entry and no `=`), ignores the bits past the last byte and truncates a text whose length +no byte count has. + +## Goals / Non-Goals + +**Goals:** `formats::zbase32` with codecs' whole contract (sizes, buffer forms, tokens, default and +typed results, secrecy, `over`, streams), byte-exact with pkarr's encoder, constant time as base32, +tested against pkarr's vectors and the document's. + +**Non-Goals:** the document's bit-length mode; a configurable alphabet or case; pkarr's URL parsing +(`pk:`, a key as a TLD), which is pkarr's port's; Crockford's base32 or any other variant. + +## Decisions + +**D1: one implementation, the alphabet a type.** `base32_codec`, +`base32_stream_codec`, `base32_reader` and the group functions take an alphabet +type in place of `bool Hex`: `detail::base32_alphabet` (RFC 4648's two, over the arithmetic as +it is) and `detail::zbase32_alphabet`. An alphabet states `symbol_of`, `five_bits_of` and +`has_padding`. The codec and the stream codec move to `detail/formats/base32_codec.hpp`, which +`base32.hpp` and `zbase32.hpp` include; neither public header includes the other. Alternative +rejected: a copy of the base32 codec for z-base-32 (750 lines of the same state machine twice, two +places to fix a stream bug). + +**D2: the alphabet by masks.** z-base-32's symbols are no ranges of ASCII, so `zbase32_alphabet` +reads its 32 symbols in a loop of `secure::constant_time::is_equal_mask`: a value's symbol is the OR +of each entry masked by whether its index is the value, a character's value the OR of each index +masked by whether its entry is the character, and its fault the complement of the OR of the masks. +32 comparisons a character, no table indexed by a data byte, no branch on one. Alternative +rejected: a 256-entry inverse table, which is what the constant-time rule forbids. + +**D3: no padding.** An alphabet without padding makes the reader treat `=` as any character that is +no symbol, and the codec's padded flag is false for a format with no `padding_option`; nothing else +of the reader changes. `max_encoded_size` is the unpadded size when the format cannot pad, so that +the default result of 32 bytes is `text<52>`. + +## Rulings (the stream's calls; the scope is the owner's) + +- **R1: bytes only.** codecs converts bytes; the document's bit-length mode needs the length in bits + out of band and has no place in `codec`. The document's byte examples (24 bits: `F0BFC7` is + `6n9hq`, `D47A04` is `4t7ye`) are vectors; its 1-, 2-, 10-, 20- and 30-bit rows are not. +- **R2: lower case alone.** Encode writes lower case, the document's alphabet. Decode takes lower + case alone; an upper-case letter is `invalid_character`. pkarr's decoder refuses upper case (its + table) and its JavaScript binding's tests list an upper-case key as invalid; decode stays canonical, + one text for one byte string, as codecs' formats are where their specification does not ask for + either case (base32 and hex take either by RFC 4648, B12 and B13). +- **R3: `=` is no symbol.** z-base-32 has no padding character, so `=` is `invalid_character`, not + `invalid_padding` as in base32 with the padding omitted: there is no padding to be wrong. +- **R4: a length no byte count has** (a remainder of 1, 3 or 6 characters modulo 8) is + `invalid_length`, as unpadded base32's. pkarr's decoder truncates such a text; codecs refuses it. +- **R5: the bits past the last byte are zero,** else `invalid_padding`, as base32's (the document's + encoder writes them zero, so a text with others is no encoding). pkarr's decoder ignores them: + `yg4gx...r5no` decodes in both, `yg4gx...r5nt` (the same bytes, a trailing bit set) in pkarr alone. A key's 52nd character is thus `y` + or `o`. The error's name is base32's, which reports the same fault under it. +- **R6: always constant time** (`codec::constant_time` is true), as base32: + pkarr's keys are public, but the format is also a way to write a secret (a seed), and the cost is + 32 comparisons a character. The constant-time lane runs its paths under memcheck and + MemorySanitizer; dudect gains `zbase32_decode` and `zbase32_encode` with their A/A controls. +- **R7: the name.** `formats::zbase32`, its type `formats::zbase32_t`, `name` `"zbase32"`, the header + ``; no option, so the format is a value with no call. +- **R8: exact sizes.** `max_encoded_size(n)` is the unpadded size, `ceil(8n / 5)`; `max_decoded_size` + is base32's, `floor(5m / 8)`. +- **R9: the order of the errors** is B23's, as base32's: `invalid_character` (an upper-case letter + and `=` among them), then `invalid_length`, then `invalid_padding`. +- **R10: the vectors' provenance.** pkarr's at `3dca339` (`pkarr/src/types/keys.rs`: the key + `yg4gx...r5no` with its 32 bytes, and the keys its tests and docs write; `bindings/js/pkg/test/ + edge-cases.js`: its invalid keys), copied into `test/zbase32_vectors.hpp` with the files, lines + and commit beside them; the document's two byte examples. pkarr is read, never vendored: the + vectors are a few lines, as bech32's BOLT 11 example is. + +## Risks / Trade-offs + +- [The loop of 32 comparisons a character is slower than a table] -> the cost of the constant-time + rule, about 64 mask operations a character; z-base-32's texts are keys and short. +- [The template parameter of base32's detail changes] -> `detail` is not API; base32's tests that + name the detail types follow, and its behaviour is pinned by its unchanged tests. + +## Migration Plan + +None: a new format. The superproject moves `libs/codecs` after webcpp#65 merges. + +## Open Questions + +None for the owner: R1 to R10 are the stream's calls and may be overturned. diff --git a/libraries/codecs/openspec/changes/zbase32/proposal.md b/libraries/codecs/openspec/changes/zbase32/proposal.md new file mode 100644 index 0000000..d6bb541 --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/proposal.md @@ -0,0 +1,56 @@ +## Why + +pkarr writes an Ed25519 public key, its 32 bytes, as a 52-character z-base-32 text, the TLD of its +URLs and the name of its DNS records, and iroh's discovery reaches it through pkarr. webcpp's ports +of them (and any program that reads a pkarr key) need z-base-32 in codecs, with codecs' one +contract, its constant time and its streams, as every other format has. The owner approved the +scope on 2026-12-01 ("codecs zbase32, with pkarr's test vectors"), queued after codecs' formats. + +## What Changes + +- **A new format, `formats::zbase32`** (``): Zooko O'Whielacronx's + human-oriented base-32 (version 0.9.4.6 of its document), the alphabet + `ybndrfg8ejkmcpqxot1uwisza345h769`, five bytes as eight symbols, the bytes' bits most significant + first, no padding, the last symbol's unused bits zero. Bytes only: the document's bit-length mode + (a length in bits known to both sides) is not codecs' (design R1). +- **Canonical decode:** lower case alone (an upper-case letter is `invalid_character`), no `=` (also + `invalid_character`), a length no byte count has (a remainder of 1, 3 or 6 characters) is + `invalid_length`, non-zero bits past the last byte are `invalid_padding`, the lowest value of + several faults reported (B23) (design R2 to R5). +- **Constant time, always, and streaming natively**, as base32: the alphabet's symbols are found by + masks over its 32 entries, never by a table indexed by a data byte; one implementation serves + base32, base32hex and z-base-32, base32's own templates taking the alphabet as a parameter in + place of `bool Hex` (design R6, R7). +- **Its sizes are exact:** a 32-byte key encodes into `text<52>`, and `text<52>` decodes into + `bytes<32>` (design R8). +- **Tests:** pkarr's vectors at a pinned commit and the document's byte examples, every error and + every byte at every place against a plain reference, the stream equal to the one-shot form for + every chunking, the constant-time lane's paths and two dudect cases with their A/A controls, a + fuzz target with its corpus, the `noexcept`, allocation and `` tests. +- **The page:** z-base-32's section in the formats chapter with the example `zbase32_pkarr.cpp`, + the formats that stream natively and are constant time, and the changelog's line. + +## Capabilities + +### New Capabilities + +None. + +### Modified Capabilities + +- `codecs`: the format z-base-32, constant time and streaming natively. + +## Impact + +- **Kind:** webcpp's own (`"port-of": null`), as codecs' genesis. +- **Breaking:** none. base32's and base32hex's behaviour, names and results are unchanged; the + detail templates they share change their parameter from `bool Hex` to an alphabet type. +- **Implementation:** webcpporg/codecs (`include/webcpp/codecs/zbase32.hpp`, + `include/webcpp/codecs/detail/formats/zbase32.hpp`, `base32.hpp` and the new + `detail/formats/base32_codec.hpp`, tests, the constant-time and fuzz lanes, the page, an example, + `meta/`), and the superproject (the gitlink, `AGENTS.md`'s table, `CODING-GUIDELINES.md`'s codecs + section, the release notes' line). The plan is `reference/plan.md`. +- **Status:** scope approved by the owner on 2026-12-01; this design's calls are rulings of the + stream (design R1 to R10), recorded so that the owner can overturn any of them. Built on + 2026-12-04 (`implementing`): `reference/STATUS.md` holds the heads, the verification and the + merges. diff --git a/libraries/codecs/openspec/changes/zbase32/reference/plan.md b/libraries/codecs/openspec/changes/zbase32/reference/plan.md new file mode 100644 index 0000000..70a8b6a --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/reference/plan.md @@ -0,0 +1,110 @@ +# codecs zbase32 Implementation Plan + +**Goal:** `formats::zbase32` in ``, on base32's one implementation, +constant time and streaming natively, tested against pkarr's vectors (design R10). + +**Spec:** `design.md` (D1 to D3, R1 to R10) and `specs/codecs/spec.md` of this change, binding. + +**Paths:** every command runs from the superproject's root, a worktree on `origin/main` with +`libs/codecs` on the branch `codecs/zbase32` of webcpporg/codecs. `B2` is +`env -u CPATH -u CPLUS_INCLUDE_PATH -u C_INCLUDE_PATH b2 --user-config=.local/user-config.jam`. + +## Task 1: the alphabet a type (D1) + +Files: `include/webcpp/codecs/detail/formats/base32.hpp`, new +`include/webcpp/codecs/detail/formats/base32_codec.hpp`, `include/webcpp/codecs/base32.hpp`, +`test/base32_test.cpp`, `test/base32_stream_test.cpp`, `test/constant_time/dudect_test.cpp`. + +1. The alphabet of RFC 4648's two, over the arithmetic as it is: + +```cpp +template +struct base32_alphabet { + static constexpr bool has_padding = true; + [[nodiscard]] static constexpr std::uint8_t symbol_of(std::uint8_t five_bits) noexcept { + return base32_arithmetic::symbol_of(five_bits); + } + [[nodiscard]] static constexpr std::uint8_t five_bits_of(std::uint8_t symbol, + std::uint8_t& error_mask) noexcept { + return base32_arithmetic::five_bits_of(symbol, error_mask); + } +}; +``` + +2. `base32_group_symbols`, `base32_last_group`, `base32_encode_bytes`, `base32_reader`, + `base32_codec` and `base32_stream_codec` take `class Alphabet` in place of `bool Hex`; the reader's + `=` is a padding character only when `Alphabet::has_padding`. The codec and the stream codec move + to `base32_codec.hpp`, with `base32_padded(format)` in place of `padding_is_written`. +3. Run base32's tests, its stream test, `noexcept_test` and `allocation_test` on native clang: + every one passes, unchanged but for the detail names. + +## Task 2: the format (D2, D3, R1 to R10) + +Files: new `include/webcpp/codecs/detail/formats/zbase32.hpp`, `include/webcpp/codecs/zbase32.hpp`, +`test/zbase32_test.cpp`, `test/zbase32_stream_test.cpp`, `test/zbase32_vectors.hpp`, +`test/support/zbase32_reference.hpp`, `test/Jamfile`, `meta/include-boundaries.json`. + +1. Tests first: the vectors (pkarr's key and bytes, pkarr's other keys round trip, pkarr's invalid + keys with codecs' errors, the document's `6n9hq` and `4t7ye`), the forms, every error, every byte + at every place against the reference, the sizes and default results' types, decode in place; the + stream test as base32's. They fail to compile: no header. +2. The alphabet by masks: + +```cpp +struct zbase32_alphabet { + static constexpr bool has_padding = false; + static constexpr std::string_view symbols = "ybndrfg8ejkmcpqxot1uwisza345h769"; + [[nodiscard]] static constexpr std::uint8_t symbol_of(std::uint8_t five_bits) noexcept { + unsigned symbol = 0; + for (unsigned index = 0; index < 32; ++index) { + symbol |= secure::constant_time::is_equal_mask(unsigned{five_bits}, index) & + static_cast(symbols[index]); + } + return static_cast(symbol); + } + // five_bits_of: the OR of each index masked by its symbol's equality, the fault the + // complement of the OR of the masks. +}; +``` + +3. The format and the specializations: + +```cpp +struct zbase32_t { + static constexpr std::string_view name = "zbase32"; +}; +inline constexpr zbase32_t zbase32{}; +template <> struct codec + : detail::base32_codec {}; +template <> struct stream_codec + : detail::base32_stream_codec {}; +``` + +4. The tests pass on native clang; then the five local lanes over the new tests. + +## Task 3: the own lanes (R6) + +Files: `test/constant_time/paths.hpp`, `ctgrind_test.cpp`, `msan_test.cpp`, `dudect_test.cpp`, +`test/fuzz/zbase32_fuzz.cpp`, `test/fuzz/corpus/zbase32/*`, `test/fuzz/Jamfile`. + +1. The paths of 64 secret bytes (103 characters) encoded and decoded, one-shot, buffer, secret + results and the secret stream, and the three faults' texts; dudect's `zbase32_decode` (64 + characters, class 0 every `y`) and `zbase32_encode` (40 bytes, class 0 zeros). +2. The fuzz target, `check_decoder(formats::zbase32, input, round_trip::same_text)`, and its corpus. +3. `B2 toolset=clang variant=release libs/codecs/test/constant_time//dudect_formats`; the Linux + constant-time and fuzz lanes. + +## Task 4: the page + +Files: `doc/formats.adoc`, `doc/streams.adoc`, `doc/secrecy.adoc`, `doc/overview.adoc`, +`doc/changelog.adoc`, `example/zbase32_pkarr.cpp`, `example/zbase32_pkarr.expected`, +`example/Jamfile`, `README.md`, `AGENTS.md`. + +```sh +B2 doc +``` + +## Task 5: the delivery + +Every lane on the final head, the pull requests, their CI, the merges; after webcpp#65, the +superproject's pull request. diff --git a/libraries/codecs/openspec/changes/zbase32/specs/codecs/spec.md b/libraries/codecs/openspec/changes/zbase32/specs/codecs/spec.md new file mode 100644 index 0000000..ba4945e --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/specs/codecs/spec.md @@ -0,0 +1,70 @@ +## ADDED Requirements + +### Requirement: Format: zbase32 +`` SHALL declare `formats::zbase32`, of type `formats::zbase32_t` and name `"zbase32"`, with no option: z-base-32 (version 0.9.4.6 of its document) over bytes, five bytes as eight symbols of the alphabet `ybndrfg8ejkmcpqxot1uwisza345h769`, the bytes' bits most significant first, the last group as short as its bytes make it (2, 4, 5 or 7 symbols) with its unused bits zero, and no padding. Encode MUST write lower case. Decode MUST take the alphabet's lower-case symbols alone and be canonical: an upper-case letter, `=` and any other character that is no symbol are `invalid_character`; a length whose remainder modulo 8 is 1, 3 or 6 is `invalid_length`; non-zero bits past the last byte are `invalid_padding`; a text with several faults reports the lowest value. `max_encoded_size(n)` MUST be the exact size, `(8 * n + 4) / 5`, so that 32 bytes encode into `text<52>`, and `max_decoded_size(m)` `5 * m / 8`, so that `text<52>` decodes into `bytes<32>`. A text MAY be decoded into its own storage. The format MUST stream natively and be constant time on every call, by the implementation base32 uses. + +#### Scenario: pkarr's key +- **WHEN** the 32 bytes pkarr's tests give for the key `yg4gxe7z1r7mr6orids9fh95y7gxhdsxjqi6nngsxxtakqaxr5no` are encoded with `codecs::formats::zbase32`, and the text decoded +- **THEN** the encode is that text, of type `text<52>` from a `std::array`, and the decode is the 32 bytes + +#### Scenario: The document's byte examples +- **WHEN** the bytes `F0 BF C7` and `D4 7A 04` are encoded +- **THEN** the texts are `6n9hq` and `4t7ye`, and each decodes back + +#### Scenario: Canonical decode +- **WHEN** pkarr's key is decoded in upper case, with a `=` appended, cut to 51 characters, and with its last character `t` in place of `o` (the same bits, one trailing bit set) +- **THEN** the results are `invalid_character`, `invalid_character`, `invalid_length` and `invalid_padding`, and the buffer is untouched + +#### Scenario: The stream equals the one-shot form +- **WHEN** pkarr's key and random texts are streamed through `codecs::encoder(formats::zbase32)` and `codecs::decoder(formats::zbase32)`, public and secret, in every chunking +- **THEN** every result equals the one-shot form's bytes or error + +## MODIFIED Requirements + +### Requirement: Constant time: hex, base32, base64, bech32 and armor always, base58 when secret +The encode and decode of `hex`, `base32`, `base32hex`, `zbase32`, `base64`, `base64url`, `bech32`, `bech32m` and `armor`, in their one-shot and stream forms, SHALL, on every call, and of `base58` when the operation is secret (a secret input or a secret destination), index no table by a data byte and branch on no data byte; decode MUST accumulate its verdict over the whole input and report it once, and a secret `base58` MUST run a number of operations that depends on the input's length alone. Only the input's length, the output's length, the format's options, bech32's human-readable part, the armor's structure (its line endings, the optional final line ending, the boundary lines and their positions, and the label) and the result's success MAY be public; the armor's decoder MAY branch on its structure, and only its base64 payload's symbols MUST be decoded in constant time. `codec::constant_time` MUST be true for the always constant-time formats and false for the others, and `codec>::constant_time_when_secret` MUST be true for every instance; a secret base58 decode with a checksum MUST compare it with `secure::constant_time_equal` inside the engaged guard. On arm64 a secret operation MUST set DIT for its duration and restore the thread's state after it through `secure::timing_guard`, engaged only when the operation is secret. The masks MUST be webcpp::secure's constant-time blocks, and a verdict branched on by design MUST pass through `secure::declassify` first; the library MUST hold no timing guard, wipe or constant-time primitive of its own. + +#### Scenario: memcheck sees no secret-dependent access +- **WHEN** the constant-time lane marks the input undefined and runs encode and decode of each always constant-time format, and base58's secret encode and decode, under memcheck +- **THEN** no conditional jump and no address depends on it, while the planted table-indexed hex is reported + +#### Scenario: A chunked stream under memcheck +- **WHEN** the constant-time lane marks the input undefined and streams each always constant-time format's encoder and decoder, made with `codecs::secret_operation`, in chunks of fixed public sizes +- **THEN** no conditional jump and no address depends on the input in any `send`, `receive` or `flush`, except the declassified verdict after `flush` + +#### Scenario: dudect judges +- **WHEN** dudect measures hex, base32, zbase32, base64, bech32 and armor decode, and base58's secret decode of a fixed length, with two classes of input natively +- **THEN** each case's |t| stays below the threshold, its A/A control too, and the planted leak exceeds it + +#### Scenario: A secret operation sets DIT through secure +- **WHEN** a test defines secure's seam (`WEBCPP_SECURE_TIMING_ENTERED`) and encodes a secret type in hex, then encodes public bytes in hex +- **THEN** the seam records one guard entered for the secret call, in codecs' function, and none for the public call + +### Requirement: Streams of the formats: encoder and decoder +`codecs::encoder(format)` and `codecs::decoder(format)` SHALL return a transformer of `std::uint8_t` to `char` (and back) that is a `one_shot_transformer` for every format with a codec, and also a `stream_transformer` when the format's header specializes `codecs::stream_codec`: hex, base32, base32hex, zbase32, base64, base64url, percent, armor, bech32 and bech32m. base58 MUST NOT stream natively and MUST stream through `buffered`. bech32's stream decoder MUST hold the raw characters in place and deliver nothing until `flush` has found the separator (the last `1`) and verified the checksum, with a hold of the format's `MaximumLength` characters by default (90 for `formats::bech32`) and `capacity`, counting characters and at most the stream codec's `maximum_hold` (bech32's `MaximumLength`), for another, a larger `N` stopping the build in one `static_assert` before the decoder type is named; a longer text is read by a larger instance of `basic_bech32`. `encoder(over{outer, inner})` and `decoder(over{outer, inner})` MUST be a one-shot transformer always and a stream transformer, through an inner pipe, when both links stream natively. `codecs::encoder(format, codecs::secret_operation)` MUST make the transformer secret. The one-shot `encode` and `decode` MUST be unchanged. + +#### Scenario: A file in base64 with no heap +- **WHEN** a program reads a file in 4096-byte chunks into one buffer, sends each chunk to `codecs::encoder(codecs::formats::base64)` and drains into a fixed output span before reading the next +- **THEN** the text equals the one-shot encoding of the whole file and no allocation is counted + +#### Scenario: Armor as a stream +- **WHEN** a DER is streamed through `codecs::encoder(codecs::formats::armor("PUBLIC KEY"))` in chunks of 5 bytes, and the result through `codecs::decoder(codecs::formats::armor)` +- **THEN** the text equals the one-shot armor, byte for byte, and the decoder gives the DER and `label()` "PUBLIC KEY" after `flush` + +### Requirement: Tests: vectors, fuzzing and lanes +The library's tests SHALL include RFC 7468's examples with OpenSSL's PEM as the armor's oracle, RFC 4648 section 10's vectors, pkarr's z-base-32 keys and the z-base-32 document's byte examples (copied with their commit and files, never vendored), BIP-173's and BIP-350's lists, Bitcoin Core's `base58_encode_decode.json` (read at run time from the superproject's submodule `third_party/libs/bitcoin`, never copied into the library, CODING-GUIDELINES chapter 10), Boost.URL's functions as the oracle of `percent`, the contract's tests (every token, Boost.URL's tokens, the default results' types, typed decodes, `over`, the concepts and the compile-time refusals, each refusal of a declared type pinned by a message test: the list, a reason in each direction, a listed pair with no value codec, a composition, a buffer form, `check_conversion`, the typed decode's input of the wrong kind), the armor's tests for `basic_armor` beside `armor_t`'s (OpenSSL's PEM, RFC 7468's examples, its stream, its exact capacity, an invalid label's message), the allocation test, the wipe tests, the own lane `constant-time` (memcheck, MemorySanitizer where crypto runs it, dudect with an A/A control, a planted leak that must fail, over every constant-time format, base58's secret operations and a secret value decoded through `over{armor, ...}` from a text of a dynamic size) and the own lane `fuzz`, one target per decoder (the armor's included), one for `over` and one for `over` of a value codec with a decode bound (`over_value`), each asserting no crash, no heap in the buffer form, that what it encodes decodes back to the same bytes, that what decodes encodes back to the same text for the formats whose decode is canonical (case folded where decode takes either case; the armor after normalising its line endings; percent against Boost.URL's `decode`; `over` by bytes alone; `over_value` by value, with no heap at all and against a two-step reference), and that the stream form, split where a prefix the fuzzer controls chooses, gives the one-shot form's bytes and error; the stream tests (every format's stream equal to its one-shot form for every chunking, the protocol's states, a failing call writing nothing, configuration faults first, `pipe`, `drain`, `each`, `buffered`, the guards nesting in a secret pipe), the toy non-byte transformer test, and the allocator tests (an arena resource, a wiping-checking allocator). Every test MUST run on native, wasip2, wasip3 and Emscripten with its `_no_exceptions` twin, and on MSVC and Apple clang as the other libraries. + +#### Scenario: A fuzzed base64 decoder +- **WHEN** the fuzz target of `base64` runs for its bounded time from the vectors' corpus +- **THEN** no input crashes, the buffer form allocates nothing, every accepted text encodes back to itself and every chunking of it gives the one-shot form's bytes and error + +#### Scenario: A secret value through the armor under memcheck +- **WHEN** the constant-time lane marks undefined the payload of an armor decoded through `over` into a secret value of the test's own from a `std::string_view` +- **THEN** no conditional jump and no address depends on it in the armor, the bound's check or the intermediate + +### Requirement: Documentation: the page +The library SHALL have its page under `libs/codecs/doc/`, built by `b2 doc`: what codecs is, one section per format (what it is, an example, its pitfalls: base58's quadratic cost, its bound (100 bytes for `formats::base58`, a larger instance named by `basic_base58`, its scratch in place), an alphabet of a program's own, its checksum parameter (webcpp::hash's `base58_with_checksum` is what Bitcoin calls Base58Check) and its constant time only when secret, bech32's maximum length (a template parameter, a larger instance for Lightning), its words mode (a word is a 5-bit value, not a machine word; a segwit version is a word, never a byte) and what it leaves to webcpp::bitcoin, z-base-32's lower case and its use for pkarr's keys, percent's `CharSet`, the armor's strict form and `basic_armor`, the armor whose label is in its type and whose result is exactly as long as its longest text), the tokens with Boost.URL's, the default and fixed results, secrecy, composition (where a value's intermediate lives under its decode bound, the order of the errors), streams (the two concepts, the flow, the borrowed input, `pipe`, `drain`, `buffered`, which formats stream natively), allocators (the `codecs::pmr` aliases and the tokens with an allocator), writing a format (`codec`, `value_codec` with `max_encoded_size` and `max_decode_input_size`, `is_secret`, `fixed_byte_storage`, `stream_codec`, and declaring a value type's formats: `value_formats`, the order of the refusal, a composition, a reason, `check_conversion`, that codecs' verbs are every library's), writing a transformer of other data, and the changelog, every example built and run in the lanes, `over_value.cpp`, `value_formats.cpp` and `zbase32_pkarr.cpp` among them. + +#### Scenario: The examples run +- **WHEN** the lanes build `libs/codecs/example` +- **THEN** every example of the page builds and runs on every target its Jamfile declares diff --git a/libraries/codecs/openspec/changes/zbase32/tasks.md b/libraries/codecs/openspec/changes/zbase32/tasks.md new file mode 100644 index 0000000..f273b9f --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/tasks.md @@ -0,0 +1,26 @@ +# Tasks + +One stream, one implementer, tests first (design D1 to D3, R1 to R10). The plan is +`reference/plan.md`. + +## 1. The shared implementation + +- [ ] 1.1 base32's detail templates take an alphabet type in place of `bool Hex`; the codec and the stream codec move to `detail/formats/base32_codec.hpp`; base32's tests pass unchanged but for the detail names (D1) + +## 2. The format + +- [ ] 2.1 `zbase32_alphabet` by masks over its 32 symbols, `formats::zbase32`, its codec and stream codec; its tests against pkarr's vectors, the document's, a plain reference, every error and the streams (D2, D3, R1 to R5, R7 to R10) +- [ ] 2.2 The `noexcept`, allocation and `` tests, the include boundaries and `meta/libraries.json` + +## 3. The own lanes + +- [ ] 3.1 The constant-time lane's paths for z-base-32 and two dudect cases with their A/A controls; the fuzz target `zbase32` with its corpus (R6) + +## 4. The page + +- [ ] 4.1 z-base-32's section in the formats chapter, the example `zbase32_pkarr.cpp`, the streams' and secrecy's lists, the changelog's line, the README + +## 5. The superproject and the delivery + +- [ ] 5.1 Every lane locally (native clang and gcc, wasip2, wasip3, Emscripten, the doc, MSVC 14.3 and 14.5 x64 debug, the constant-time and fuzz lanes, the macOS and Linux lints); the pull requests of codecs and proposals, their CI green, merged +- [ ] 5.2 After webcpp#65: the superproject's gitlink, `AGENTS.md`, `CODING-GUIDELINES.md` and the release notes in one pull request on the new `main`, its CI green, merged From 68c3d66cdb72c49dc7f915b797bbd925ef3d210c Mon Sep 17 00:00:00 2001 From: R Date: Fri, 4 Dec 2026 01:13:24 -0300 Subject: [PATCH 2/3] codecs: zbase32 is built (tasks 1 to 4 ticked, STATUS with the heads, the verification and the rulings made while building) --- .../codecs/openspec/changes/zbase32/design.md | 6 ++- .../changes/zbase32/reference/STATUS.md | 51 +++++++++++++++++++ .../codecs/openspec/changes/zbase32/tasks.md | 10 ++-- 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 libraries/codecs/openspec/changes/zbase32/reference/STATUS.md diff --git a/libraries/codecs/openspec/changes/zbase32/design.md b/libraries/codecs/openspec/changes/zbase32/design.md index 36329aa..ebd7236 100644 --- a/libraries/codecs/openspec/changes/zbase32/design.md +++ b/libraries/codecs/openspec/changes/zbase32/design.md @@ -63,8 +63,10 @@ the default result of 32 bytes is `text<52>`. `invalid_length`, as unpadded base32's. pkarr's decoder truncates such a text; codecs refuses it. - **R5: the bits past the last byte are zero,** else `invalid_padding`, as base32's (the document's encoder writes them zero, so a text with others is no encoding). pkarr's decoder ignores them: - `yg4gx...r5no` decodes in both, `yg4gx...r5nt` (the same bytes, a trailing bit set) in pkarr alone. A key's 52nd character is thus `y` - or `o`. The error's name is base32's, which reports the same fault under it. + `yg4gx...r5no` decodes in both, `yg4gx...r5nt` (the same bytes, a trailing bit set) in pkarr + alone. A key's 52nd character is thus `y` or `o`. pkarr's own test of an invalid key, + `c1bkg8...mbus`, is such a text: pkarr decodes it and refuses the point, codecs refuses the text + with `invalid_padding`. The error's name is base32's, which reports the same fault under it. - **R6: always constant time** (`codec::constant_time` is true), as base32: pkarr's keys are public, but the format is also a way to write a secret (a seed), and the cost is 32 comparisons a character. The constant-time lane runs its paths under memcheck and diff --git a/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md b/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md new file mode 100644 index 0000000..f2c5cd3 --- /dev/null +++ b/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md @@ -0,0 +1,51 @@ +# webcpp::codecs zbase32: where the implementation stands + +2026-12-04: built and verified locally; the pull requests are open. Tasks 1 to 4 are done on the +branch below; 5.1's local lanes are green, its CI and merges follow; 5.2 (the superproject) waits +for webcpp#65, another stream's, to merge first. + +## The heads + +| Repository | Branch | Head | Base | +| --- | --- | --- | --- | +| webcpporg/codecs (`libs/codecs`) | `codecs/zbase32` | `e55940b` | `2577892`, its `main` (codecs#5) | +| webcpporg/webcpp | `zbase32` | (the gitlink, after webcpp#65) | `fb1e021`, its `main` (webcpp#64) | +| webcpporg/proposals | `codecs/zbase32` | this commit | `f90af53`, its `main` | +| webcpporg/secure (`libs/secure`) | (read only) | `44b1557`, the superproject's gitlink | | +| webcpporg/config (`libs/config`) | (read only) | `9fb4210`, the superproject's gitlink | | + +## The commits of codecs + +1. `8c3279b` base32's one implementation takes its alphabet as a type, its codec and stream codec + in `detail/formats/base32_codec.hpp` (D1): base32's tests pass, unchanged but for the detail + names. +2. `683bd40` z-base-32: the alphabet by masks, the format, its tests against pkarr's vectors, the + `noexcept`, allocation and `` tests, the include boundaries, `meta/libraries.json`. +3. `1a4365b` the own lanes: the constant-time paths, dudect's two cases, the fuzz target and its + corpus. +4. `cb8afe5` the page: its section, the example `zbase32_pkarr.cpp`, the tables, the changelog. +5. `e55940b` the lint's findings on the tests (trailing commas, a function split). + +## The verification (head `e55940b`, superproject `fb1e021`) + +| Lane | Result | +| --- | --- | +| clang, gcc-15, wasip2, wasip3, Emscripten over codecs' test, example and fuzz | 0 failed, 0 skipped | +| `b2 libs/codecs/doc` | built, the example's region included | +| Native dudect, release (macOS arm64) | every case below 10; zbase32_decode 2.15 (A/A 3.05), zbase32_encode 1.70 (A/A 1.16) | +| Linux constant-time (ctgrind, ctgrind_space, msan, dudect, planted leaks) | status 0, judge 0 | +| Linux fuzz (every target, zbase32's among them) | status 0 | +| MSVC 14.3 and 14.5 x64 debug, codecs' test and example | 220 passed each, new failures 0 | +| macOS lint (clang-format, clang-tidy, the text rules) | clean | +| Linux lint (files mode) | CLEAN: the CI lint would pass | + +## Rulings made while building + +- The decode bound of z-base-32 is exact, so `max_encoded_size` reaches `SIZE_MAX`, the size + sentinel, at `5 * (SIZE_MAX / 8) + 4` bytes (a text of exactly `SIZE_MAX` characters): that input + is refused as too long, one byte below base32's padded limit. No such input fits in memory. +- `secure::constant_time::lookup` gives a value's symbol, as bech32's character does; the inverse is + the masks against the 32 symbols, as bech32's value is. +- codecs' changelog keeps ten lines: z-base-32 joins base32's line. +- pkarr's own invalid key of `keys.rs` (`c1bkg8...mbus`) is a non-canonical text: codecs refuses it + with `invalid_padding` (design R5), recorded in the vectors. diff --git a/libraries/codecs/openspec/changes/zbase32/tasks.md b/libraries/codecs/openspec/changes/zbase32/tasks.md index f273b9f..4f833c2 100644 --- a/libraries/codecs/openspec/changes/zbase32/tasks.md +++ b/libraries/codecs/openspec/changes/zbase32/tasks.md @@ -5,20 +5,20 @@ One stream, one implementer, tests first (design D1 to D3, R1 to R10). The plan ## 1. The shared implementation -- [ ] 1.1 base32's detail templates take an alphabet type in place of `bool Hex`; the codec and the stream codec move to `detail/formats/base32_codec.hpp`; base32's tests pass unchanged but for the detail names (D1) +- [x] 1.1 base32's detail templates take an alphabet type in place of `bool Hex`; the codec and the stream codec move to `detail/formats/base32_codec.hpp`; base32's tests pass unchanged but for the detail names (D1) ## 2. The format -- [ ] 2.1 `zbase32_alphabet` by masks over its 32 symbols, `formats::zbase32`, its codec and stream codec; its tests against pkarr's vectors, the document's, a plain reference, every error and the streams (D2, D3, R1 to R5, R7 to R10) -- [ ] 2.2 The `noexcept`, allocation and `` tests, the include boundaries and `meta/libraries.json` +- [x] 2.1 `zbase32_alphabet` by masks over its 32 symbols, `formats::zbase32`, its codec and stream codec; its tests against pkarr's vectors, the document's, a plain reference, every error and the streams (D2, D3, R1 to R5, R7 to R10) +- [x] 2.2 The `noexcept`, allocation and `` tests, the include boundaries and `meta/libraries.json` ## 3. The own lanes -- [ ] 3.1 The constant-time lane's paths for z-base-32 and two dudect cases with their A/A controls; the fuzz target `zbase32` with its corpus (R6) +- [x] 3.1 The constant-time lane's paths for z-base-32 and two dudect cases with their A/A controls; the fuzz target `zbase32` with its corpus (R6) ## 4. The page -- [ ] 4.1 z-base-32's section in the formats chapter, the example `zbase32_pkarr.cpp`, the streams' and secrecy's lists, the changelog's line, the README +- [x] 4.1 z-base-32's section in the formats chapter, the example `zbase32_pkarr.cpp`, the streams' and secrecy's lists, the changelog's line, the README ## 5. The superproject and the delivery From b0fda6dbf05ab6253b8d37305b87e03f6752434d Mon Sep 17 00:00:00 2001 From: R Date: Fri, 4 Dec 2026 03:38:01 -0300 Subject: [PATCH 3/3] codecs: zbase32 is merged in codecs (codecs#6, 85061b2, main's CI green); the superproject waits for webcpp#65 --- .../changes/zbase32/reference/STATUS.md | 18 +++++++++++++----- .../codecs/openspec/changes/zbase32/tasks.md | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md b/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md index f2c5cd3..1b8b43d 100644 --- a/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md +++ b/libraries/codecs/openspec/changes/zbase32/reference/STATUS.md @@ -1,15 +1,15 @@ # webcpp::codecs zbase32: where the implementation stands -2026-12-04: built and verified locally; the pull requests are open. Tasks 1 to 4 are done on the -branch below; 5.1's local lanes are green, its CI and merges follow; 5.2 (the superproject) waits -for webcpp#65, another stream's, to merge first. +2026-12-04: built, verified locally and merged in codecs. Tasks 1 to 4 and 5.1 are done: codecs#6's +CI was green on every check and it is merged, and the CI of codecs' `main` at the merge is green. +5.2 (the superproject) waits for webcpp#65, another stream's, to merge first; its branch is ready. ## The heads | Repository | Branch | Head | Base | | --- | --- | --- | --- | -| webcpporg/codecs (`libs/codecs`) | `codecs/zbase32` | `e55940b` | `2577892`, its `main` (codecs#5) | -| webcpporg/webcpp | `zbase32` | (the gitlink, after webcpp#65) | `fb1e021`, its `main` (webcpp#64) | +| webcpporg/codecs (`libs/codecs`) | `codecs/zbase32` (deleted after the merge) | `e55940b`, merged as `85061b2` (codecs#6) | `2577892`, its `main` (codecs#5) | +| webcpporg/webcpp | `zbase32` | `ba302a4`: the gitlink at `85061b2`, `AGENTS.md`, `CODING-GUIDELINES.md`, the release notes; `origin/main` merged in after webcpp#65 | `fb1e021`, its `main` (webcpp#64) | | webcpporg/proposals | `codecs/zbase32` | this commit | `f90af53`, its `main` | | webcpporg/secure (`libs/secure`) | (read only) | `44b1557`, the superproject's gitlink | | | webcpporg/config (`libs/config`) | (read only) | `9fb4210`, the superproject's gitlink | | @@ -49,3 +49,11 @@ for webcpp#65, another stream's, to merge first. - codecs' changelog keeps ten lines: z-base-32 joins base32's line. - pkarr's own invalid key of `keys.rs` (`c1bkg8...mbus`) is a non-canonical text: codecs refuses it with `invalid_padding` (design R5), recorded in the vectors. + +## The pull requests and CI + +| Repository | Pull request | CI | Merge commit | +| --- | --- | --- | --- | +| webcpporg/codecs | #6 | run 47264909011: 28 passed, 1 skipped (the tools' tests, not run for a library) | `85061b2`; `main`'s run 47274253723 green | +| webcpporg/proposals | #38 | check, front-matter: passed | this branch's merge | +| webcpporg/webcpp | after webcpp#65 | | | diff --git a/libraries/codecs/openspec/changes/zbase32/tasks.md b/libraries/codecs/openspec/changes/zbase32/tasks.md index 4f833c2..6139bfb 100644 --- a/libraries/codecs/openspec/changes/zbase32/tasks.md +++ b/libraries/codecs/openspec/changes/zbase32/tasks.md @@ -22,5 +22,5 @@ One stream, one implementer, tests first (design D1 to D3, R1 to R10). The plan ## 5. The superproject and the delivery -- [ ] 5.1 Every lane locally (native clang and gcc, wasip2, wasip3, Emscripten, the doc, MSVC 14.3 and 14.5 x64 debug, the constant-time and fuzz lanes, the macOS and Linux lints); the pull requests of codecs and proposals, their CI green, merged +- [x] 5.1 Every lane locally (native clang and gcc, wasip2, wasip3, Emscripten, the doc, MSVC 14.3 and 14.5 x64 debug, the constant-time and fuzz lanes, the macOS and Linux lints); the pull requests of codecs and proposals, their CI green, merged - [ ] 5.2 After webcpp#65: the superproject's gitlink, `AGENTS.md`, `CODING-GUIDELINES.md` and the release notes in one pull request on the new `main`, its CI green, merged