Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
11 changes: 11 additions & 0 deletions libraries/codecs/openspec/changes/zbase32/.openspec.yaml
Original file line number Diff line number Diff line change
@@ -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
99 changes: 99 additions & 0 deletions libraries/codecs/openspec/changes/zbase32/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
## 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 `<webcpp/codecs/base32.hpp>`, 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,
<https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>) is RFC 4648's base32 with
another alphabet, `ybndrfg8ejkmcpqxot1uwisza345h769`, no padding, and lower case. pkarr
(<https://github.com/pubky/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<Hex, Format>`,
`base32_stream_codec<Hex, Format>`, `base32_reader<Hex>` and the group functions take an alphabet
type in place of `bool Hex`: `detail::base32_alphabet<Hex>` (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`. 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<formats::zbase32_t>::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
`<webcpp/codecs/zbase32.hpp>`; 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.
56 changes: 56 additions & 0 deletions libraries/codecs/openspec/changes/zbase32/proposal.md
Original file line number Diff line number Diff line change
@@ -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`** (`<webcpp/codecs/zbase32.hpp>`): 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 `<windows.h>` 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.
59 changes: 59 additions & 0 deletions libraries/codecs/openspec/changes/zbase32/reference/STATUS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# webcpp::codecs zbase32: where the implementation stands

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` (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 | |

## 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 `<windows.h>` 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.

## 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 | | |
Loading