diff --git a/README.md b/README.md index 55fdaf0..d4dcc9a 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ tag is placed. | beast | [genesis](libraries/beast/openspec/changes/genesis) | beast: existing Boost.Beast applications on WASIp2, WASIp3 and Emscripten | wrapper | implementing | | 2026-11-06 | [code](https://github.com/webcpporg/beast) | | 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) | | 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) | @@ -58,6 +59,7 @@ tag is placed. | crypto | [api-gaps](libraries/crypto/openspec/changes/api-gaps) | crypto: the API gaps of version 0.2 closed, and dudect judged on hosted runners | wrapper | implementing | | 2026-11-20 | [code](https://github.com/webcpporg/crypto) | | crypto | [data-independent-timing](libraries/crypto/openspec/changes/data-independent-timing) | crypto: the library sets arm64's DIT around every cryptographic operation | wrapper | implementing | | 2026-11-20 | [code](https://github.com/webcpporg/crypto) | | crypto | [formats](libraries/crypto/openspec/changes/formats) | crypto: one encode and one decode for every key and signature format, the format as a value | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) | +| crypto | [one-verb](libraries/crypto/openspec/changes/one-verb) | crypto: codecs' verbs are crypto's: crypto::encode and crypto::decode removed, the check declared to codecs, PEM on a fixed label and decoded in place | wrapper | implementing | | 2026-12-02 | [code](https://github.com/webcpporg/crypto) | | crypto | [unified-sign](libraries/crypto/openspec/changes/unified-sign) | crypto: one sign and verify on every curve, the scheme always named, the key pair contract and four forms of each operation | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) | | crypto | [uniform-types](libraries/crypto/openspec/changes/uniform-types) | crypto: typed signatures, typed X25519 keys, and concepts for key pairs, AEADs and boxes | wrapper | implementing | | 2026-11-22 | [code](https://github.com/webcpporg/crypto) | | document | [genesis](libraries/document/openspec/changes/archive/2026-10-12-genesis) | document: a JSON document for C++20 that behaves as JavaScript does, in one relocatable image | own | implemented | 0.1.0 | 2026-10-12 | [code](https://github.com/webcpporg/document/tree/ac29296) | diff --git a/libraries/codecs/openspec/changes/one-verb/.openspec.yaml b/libraries/codecs/openspec/changes/one-verb/.openspec.yaml new file mode 100644 index 0000000..4954838 --- /dev/null +++ b/libraries/codecs/openspec/changes/one-verb/.openspec.yaml @@ -0,0 +1,11 @@ +schema: spec-driven +created: 2026-11-30 +webcpp: + library: codecs + title: "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" + kind: own + port_of: null + status: implementing + release: null + implementation: https://github.com/webcpporg/codecs + decided: 2026-12-02 diff --git a/libraries/codecs/openspec/changes/one-verb/design.md b/libraries/codecs/openspec/changes/one-verb/design.md new file mode 100644 index 0000000..bcc3964 --- /dev/null +++ b/libraries/codecs/openspec/changes/one-verb/design.md @@ -0,0 +1,779 @@ +## Open questions for the owner + +None. Both questions the first design asked were answered by the owner on 2026-11-30: Q1 (the +error of an input past the decode bound) by owner decision 29, Q2 (the cap on the in-place +intermediate) by owner decision 30. Owner decision 31 (2026-12-02, one verb) leaves one choice to +this design, how the PEM label stays the inner format's through codecs' verbs; R23 rules it. The +public names this change adds (`codecs::value_formats`, `codecs::format_list`, +`codecs::direction`, `codecs::check_conversion`, `codecs::formats::basic_armor`) were rulings +(R17, R22, R23); the owner approved them as designed on 2026-12-02 ("Quanto aos nomes, podem ficar +assim", below), so they are owner-approved. + +## Context + +This change builds on webcpp::codecs as merged (codecs#1, `80d90d0`, its genesis +`libraries/codecs/openspec/changes/genesis/`, owner decisions 1 to 27 and the rulings of its +implementation) and on crypto's `formats` change as merged (`libraries/crypto/openspec/changes/formats/`, +crypto#20, webcpp#62), which ran right before it. It began as `over-in-place` (owner decisions 28 +to 30, 2026-11-30); owner decision 31 (2026-12-02) widened it to one verb for every library, and +it was renamed `one-verb` with crypto's follow-up (R16). + +**The verbs today.** crypto's `formats` gave crypto its own verbs, `crypto::encode` and +`crypto::decode` (crypto's owner decision 16 and ruling R18): function templates that check the +pair against crypto's list first and then forward to `codecs::encode` and `codecs::decode`, +because codecs' own refusal of a pair (genesis R2: a `static_assert` whose requirement is +`detail::converts` and whose message is "this format does not convert this type") +cannot name the formats a type has. crypto's check (its D3, as built in `formats.hpp`: +`detail::pair_accepted`, `detail::refuse_pair`, `detail::refusal_reason_v`, +`detail::decode_refusal_reason_v`, `detail::formats_of`, `detail::forms_of`) stops the build, with +that error alone, in this order: the curve's formats header not included ("include +/formats.hpp>"), a `forms_of` without the default form, the pair's own +reason (`der` on an Ed25519 or BIP340 signature, `der` on a recoverable signature, `raw(xonly)` +decoded into a `secp256k1::public_key`, a private format on a key pair whose secret lives in +WebCrypto), then "this type has no such format; the formats it has are the format_list named in +this assertion", the type's `format_list` printed in the failed requirement, then "this type lists +the format, and no codecs::value_codec of the pair encodes (decodes) it". A program that calls +codecs' verbs with crypto's formats gets the same bytes and values and codecs' generic +diagnostic. Two verbs answer one question; owner decision 31 keeps one. + +**The PEM label today.** crypto's `formats::pem(F)` is `codecs::over`, +the armor configured with `detail::pem_label_v` (`PUBLIC KEY`, `PRIVATE KEY`). The armor's label +is a value (`armor_t` holds up to 64 characters, `formats::armor("PUBLIC KEY")`), so a hand-built +`formats::pem_t{codecs::formats::armor("CERTIFICATE"), F{}}` is a valid object; +`crypto::encode` and `crypto::decode` replace its armor by the inner format's (as built, design D2 +(3) of `formats`), and `codecs::encode` with the same object writes `CERTIFICATE`. A PEM result's +capacity is the armor's bound over every label (crypto's R20: 347 characters for P-256's +`pem(pkcs8)`, whose text is 241). + +What codecs does today, in `include/webcpp/codecs/detail/core/over.hpp`: + +- `over` holds its intermediate in `in_place_intermediate` + (a `std::array` in the frame of each of its functions, its written bytes wiped by + `secure::erase_secret` when the chain is secret) when `Bound` is known at compile time, and in + `heap_intermediate` (a `std::vector` or a `basic_secret_byte_vector` of + `over`'s allocator) otherwise. +- For a decode, `Bound` is `decoded_bound>()`, the outer link's + `max_decoded_size` of the text's static size, or `std::dynamic_extent` for a `std::string_view`. + The value decode (`over_value_decode`) asks the outer link's exact `decoded_size` (which checks + the whole text, so the outer link's data errors come first), refuses a size past `Bound` with + `input_too_long`, makes the intermediate, decodes the outer link into it and gives the written + bytes to the inner value codec's `decode`. +- Its `noexcept` (genesis R3) follows the intermediate: `over_value_decode_is_noexcept` is false + whenever `Bound` is `std::dynamic_extent`, so the typed `decode` of such a chain is not + `noexcept`. +- A value codec states `output`, optionally `secret`, optionally `max_encoded_size` (which sizes + the encode's intermediate and the default result); it states nothing about the longest input + its decode takes. `SIZE_MAX` (`detail::size_maximum`, equal to `std::dynamic_extent`) is the + saturated bound that means "no size to hold in place". +- `detail::in_place_scratch_limit = 4096` (tokens.hpp) is the threshold the owner set on + 2026-11-26 for `assign_to`'s scratch: in place up to 4096 elements, the target's allocator + above. + +What crypto does after `formats` (its design R24, R37 and owner decision 23): each crypto value +codec that decodes states `max_decode_input_size` (crypto's `formats.hpp` checks it is there and, +today, that it equals `max_encoded_size`); `crypto::decode` of `pem(F)` calls +`detail::decode_pem`, which decodes the armor through codecs' buffer form into a +`std::array>`, maps `output_too_small` to +`crypto::error::invalid_encoding`, then calls `codecs::decode(format.inner, ...)` and wipes +the buffer of a secret pair; `codecs::decode` of the same format and text allocates, which crypto's +guide and tests state as "the documented difference". + +## Goals / Non-Goals + +**Goals:** one error, codecs' `input_too_long`, for every input past a value codec's decode +bound, on every path (owner decision 29); `codecs::decode(over{outer, inner}, text)` of a text of a dynamic size, when the +inner value codec states a decode bound, holds its intermediate in place, allocates nothing and is +`noexcept` when the links' calls are; the inner value codec never receives more than its decode +bound from `over`; a secret intermediate is wiped on every path; crypto's PEM decode needs +nothing of its own (owner decisions 28 to 30). `codecs::encode` and `codecs::decode` are the +verbs of every library (owner decision 31): a library declares, for each value type it owns, +the formats the type has and its pairs' own reasons, and every operation of codecs refuses a pair +of that type with the type's list in the failed requirement, or with the library's sentence, as +crypto's D3 does today, with that error alone; a type with no declaration keeps the genesis' +diagnostic; an armored format whose label follows from its content carries the label in its type, +so that no value of it holds another (crypto's owner decision 1 through codecs' verbs). + +**Non-goals:** a decode bound on `codec` (a codec's `max_decoded_size(n)` already bounds a +decode from its input's size); a decode bound on `over`'s own value codec (R8); the encode side +of `over` (its intermediate is already in place under `max_encoded_size`, and its outer link's +encode fault after `prepare` stays as the genesis documents, R12); a stream form of a value +codec's `over` (decision 17 of the genesis: a value codec has no transformer); PKCS#8 v2, which +stays deferred (crypto's owner decision 23); a declaration for a byte input or a byte result +(`codec`'s pairs, whose diagnostic stays the genesis'); a format chosen at run time; a +diagnostic text computed at compile time (C++26's `static_assert` with a constant-expression +message; codecs is C++20); removing `formats::armor_t` or its run-time label. + +**Anti-goals:** a heap allocation where the decode bound is known and small; a stack array sized +by a bound the caller cannot see or escape; a secret byte of the intermediate left unwiped; two +errors for one input past the bound depending on the path it took; two verbs for one conversion; +a refusal that is a cascade of errors or an overload-resolution failure where the declaration +holds a sentence; a PEM label a caller can get wrong through any verb. + +## Approaches + +The decode bound and the in-place `over` were chosen on 2026-11-30 (owner decisions 28 to 30, +"Decisions" D1 to D8). The approaches below are those of owner decision 31. + +**The verb.** + +- **A. Keep crypto's verbs** (crypto's R18 as built): two verbs per conversion, the same bytes, + two diagnostics. Rejected by the owner (decision 31, option A of the question). +- **B (chosen, owner decision 31).** One verb, codecs', and an extension point of codecs through + which a library declares a value type's formats and its pairs' reasons, so that codecs' own + refusal names the type's list and says the library's sentence. +- **C. Using-declarations** (`namespace crypto { using codecs::encode; }`): one implementation, + two spellings, and the diagnostics lose the list (crypto's R18's cost line). Rejected: the owner + wants one name and the list. +- **D. The library's diagnostics in the value codec**: a `value_codec` is a pair; a refused pair + has no specialization to hold a sentence, and the list is per type, not per pair. Rejected. + +**The extension point's shape.** + +- **E (chosen). A class template per value type, `codecs::value_formats`, declared and + not defined, as `value_codec` is**: its `formats` (a `codecs::format_list`), an optional + predicate `has_format` for a type whose list names a family of formats (crypto's point + forms), and an optional pair of members `has_reason` and + `refuse()`, the library's own `static_assert` with its fixed text. A partial + specialization constrained by a concept declares a family of types at once. +- **F. Sentences as `std::string_view` data that codecs puts in its own `static_assert`**: + impossible in C++20, whose `static_assert` message is a string literal (the genesis' R2 met the + same limit with `F::name`). Rejected. +- **G. A reason enumeration of codecs'**: codecs would hold crypto's sentences, or a fixed set of + generic ones that cannot say "Ed25519 and BIP340 signatures have no DER form". Rejected. + +**The PEM label through codecs' verbs.** + +- **H (chosen, R23). The label in the armor's type**: codecs gains + `formats::basic_armor