Repository navigation
backend concepts: one concept per backend capability - #31
Merged
Merged
Conversation
added 29 commits
December 5, 2026 08:24
…(backend-concepts task 1.1): block_cipher, gcm, aead, authenticated_box, stream_cipher, authenticator, box, sealed_box and kx, each shipped engine checked and a stand-in per requirement
…s review fixes - test/support/backend_stand_in.hpp: a stand-in engine is a class template over a defect (one operation missing, not noexcept or of another result; one parameter of another size or type); rejects_every_defect and rejects_every_shape assert that a concept takes the complete stand-in and refuses every defect it lists. - The symmetric test uses it for block_cipher, gcm, aead, authenticated_box, stream_cipher and authenticator; the box concepts move to backend_concepts_boxes_test, with box, sealed_box and kx, and their sizes checked against kx_keypair's. - move, reset and abandon must return void; box takes no family; kx's sizes are named constants; block_cipher and gcm document why they take no key size; the stand-ins' sizes are the families'.
- keys.hpp: key_import, key_generate, key_public and key_export, with the same operation names on every curve (import_secret, generate, public_key, export_secret); an opaque key implements generate, public and the uses without import or export. - ed25519.hpp, x25519.hpp, p256.hpp: each curve's capabilities and named sizes; Ed25519's signing, verifying, map to X25519 and Ed25519ph; X25519's agreement; P-256's points, ECDSA, ECDH and DER. - entropy.hpp: entropy_source; asynchronous.hpp: the async_engine operations async.hpp calls, over the program's executor and token types; concepts.hpp includes them all. - backend_concepts_keys_test: the shipped engines whose shape already matches; per curve a defect-selector stand-in refused for every operation and shape; the opaque keys of P-256 and X25519. backend_concepts_asynchronous_test: every operation's absence and a wrong executor refused.
- Each backend header asserts, after each specialization, the concepts its table cells promise (design D5): GCM and the block cipher per key size, the AEADs, ChaCha20, Poly1305, the secret boxes, the boxes and kx, and the Ed25519, Ed25519ph, X25519 and P-256 key engines. - The key engines take one set of names: import_secret, public_key and export_secret (both fallible), move and reset on every curve; Ed25519's seed_keypair and seed and P-256's public_point and scalar are renamed. - X25519's engine is state-based (x25519_state), so that an opaque key has a state; the key pair imports, exports, agrees, moves and resets through it. - The Ed25519 and X25519 formats engines dissolve into detail::rfc8410: RFC 8410's SPKI and PKCS#8 are a fixed prefix and the key, with no backend; PKCS#8 reads the secret through the key engine's export_secret. - The sized symmetric families move to detail/families.hpp, which their public headers include, so that a backend header can read their sizes. - detail::system_entropy is the system's entropy as an entropy_source; random_device draws through it. - A crypto_kx key pair on a backend without crypto_kx holds no X25519 key pair, so that X25519's refusal does not follow kx's.
…er_overflows readability-redundant-parentheses flags (std::numeric_limits<Counter>::max)(), whose parentheses stop <windows.h>'s macro max: suppressed with the reason, as random_device and the OpenSSL backend already do.
The lint's no-clock-disk-or-network rule reads random_device in a header as a reach for the system's entropy: the comment says the library's generator.
- The new failure paths are tested (I1): on stand-in engines that fail, public_key() and secret() pass backend_failure on for Ed25519, X25519 and P-256, and the PKCS#8 encode of Ed25519 and X25519 writes nothing (keypair_engine_failure, x25519_mock). - A public key's RFC 8410 formats are a table row of their own, on no backend, beside the key pair's formats on libsodium; formats.adoc says so; the generator names no default for an operation on no backend (M1). - rfc8410_no_backend links no backend, round-trips an Ed25519 and an X25519 SPKI and tests detail::rfc8410 directly (M2). - x25519_access::secret_bytes refuses by name a backend whose X25519 state has no secret_key (x25519_state_holds_secret_key) (M3). - libsodium's header includes each concept header in the section that asserts it (M4), and a blank line after the sealed boxes' #endif (M5).
- The asynchronous concepts mirror async.hpp's calls exactly (I1): the executor as Executor const&, key pairs, public keys, signatures, schemes and policies as const lvalues, views of dynamic extent, the AEAD as a mutable lvalue, the token forwarded, from_secret, from_key and generate as member templates; each proven by a stand-in that takes it otherwise. The header states the contract the concepts cannot. - async_import and async_from_key are async_import_secret and async_import_key; async_export is gone, with no caller and none planned (I3); async.hpp's engine documentation names the list. - P-256 signs and verifies in a message form beside the digest form (p256_ecdsa_message_signing, p256_ecdsa_message_verifying), the form of a backend that hashes inside as WebCrypto does (I2); a WebCrypto-like stand-in has it alone, BearSSL's engine the digest form alone. - Docs: x25519_secret is an export in all but name (M1); the key concepts take no curve, over the shared secret_key_size (M3); entropy_source's contract (M4). - Wrong-shape stand-ins for every dynamic view and the parity byte (M6); every stand-in numbers its operations from first_op (M7).
- x25519_mock: a knob that fails public_key alone, so from_secret's pass of the engine's public_key error after a successful import is tested (m1). - generate.py: llms.txt names no default for an operation that runs on no backend, and the backends table gives such an operation its own header, not error.hpp's; a generator test of both; regenerated (m2).
…test inside the anonymous namespace (lint)
- p256_ecdsa_message_signing's doc: a message signs on either form, a prehashed digest only on the digest form; the two forms differ by name on purpose, since a digest view converts to a view of any length (N1, N4). - A test that an engine whose sign and verify take a view of any length satisfies the digest concepts and never the message ones (N4). - async.hpp states its own reason for no asynchronous export (N5).
libsecp256k1's calls move from the curve's headers into secp256k1_engine<Capability, backends::secp256k1>, one specialization per capability tag (key, point, ecdsa, schnorr, recoverable, ecdh, tweak, ellswift, musig, silent_payments), each asserted against its concept in detail/backend/secp256k1.hpp. The key pair keeps allocating the context's storage; the no-allocation and constant-time tests are unchanged. backend_concepts_secp256k1 proves each concept with defect-selector stand-ins.
…backend's own generation (task 2.3) test/backends/opaque.hpp: P-256 and X25519 keys the backend draws and never gives out, handles into its table of BearSSL and libsodium states; opaque and opaque_async, whose async_engine posts every completion. generate() on Ed25519, X25519 and P-256 calls the engine's generate when it has one (design R3); detail::async_access opens a key pair's generation, agreement and digest signing to an asynchronous engine. opaque_backend_test checks the engines' concepts, the async_* concepts, the keys against BearSSL and libsodium, moves, a full table and the asynchronous functions.
M1 every parameter of every secp256k1 stand-in has a wrong-shape selector; M2 the engines' own guards run directly: extra entropy of a size other than 0 or 32 is refused by the ECDSA engine, a recovery id above 3 by recover, DER that is no signature by from_der, which also refuses an empty view before libsecp256k1; M3 the scan runs the self test again; M4 two unused includes of the curve's header and one of the tweaks' gone, the wipe test's comment names the key engine's reset.
…mments generate.py --check would have failed: task 2.3 changed the Doc Comments of the key pairs' generate() and did not run the generator.
…gines (task 3.1) Each family of crypto.json lists its capabilities (kind, concept, engines) and each backend's cell those its engines have. generate.py writes the capability tags, detail::capability_supported, the secret-key traits and detail::shipped_backends; supports.hpp adds supports_capability_v, exports_secret_keys_v and imports_secret_keys_v, all header-free. capabilities_test checks every cell against the engines both ways, counting an engine only where its specialisation marker, detail::specialized_engine_v, is true, and the traits against the export and import engines. BearSSL's refusing block_engine carries no marker; supports_header_free asks the traits with no backend header and no library.
- I1: an Ed25519 stand-in engine (libsodium's, with a generate that draws its seed) drives the key pair's generate() through the engine's own generation: signs, matches libsodium's key of the drawn seed, and returns the engine's error. - M1: message test opaque_async_synchronous (and its MSVC twin): generate, sign and agree on the opaque backend's asynchronous form are refused by name before its engine; async_generate on the synchronous form runs. - M2: the opaque P-256 engine has no message form, asserted and documented. - M3: a full table on X25519 and on async_generate, a low-order X25519 peer, a compressed and an off-curve point and DER on the opaque public key, sign and agree on a moved-from key pair. - M4: asio::deferred on async_generate and async_agree; the completion runs on the executor passed, not on another context's. - M5: opaque.hpp includes signing.hpp; is_opaque_v removed. - M6: async_engine's docs name the tests' opaque backend beside part B's; key_table::at() is private, the engines its friends.
- I1: each capability carries a program-facing text in crypto.json, which its public tag's comment says; the internal concept stays in the generated test. The generator refuses a capability with no text or one naming detail::, and generate_test pins that no tag comment names detail::backend. - M1: capabilities that stand for each other name their form; the generator refuses a cell in which no one form has every use. - M2: the generated test writes one engine a line, says why the formatter is off, and drops the engine count from its comments; the trait checks are wrapped. - M3: crypto.json's comment records that entropy and async_* get their tags with the first backend that has them. - M4: capability_cells loses its unused parameter. - M5: supports_header_free asks AES-256-GCM's public type; generate.py checks that the test refuses every backend tag's header guard. - M7: the either case is renamed for what it asserts, with the message-only half and a cell that has neither form. - generate_test: task 3.1's capability cases were never listed in CASES and did not run; they are listed, and a defined case not listed is now refused.
- 2.3 N1: the opaque_async_synchronous fix names no test-only backend. - 2.3 N2: its MSVC twin drops the empty clang-format pair and the -verify comment it has no directive for. - 2.3 N3: the message test refuses X25519's agree on the opaque backend's asynchronous form too. - 3.1 N1: a capability's text may not name its concept, backend:: or an engine. - 3.1 N2: generate_test's unlisted-case check is a function with a case. - 3.1 N3: check_header_free reads the test's code, not its comments.
Task 3.2 (design D9, D5, D3's and D6's amendments): - the curves (P-256, Ed25519, X25519, P-256's DER) assert each member's capability for an engine a backend's header marks (specialized_engine_v), alone so that clang's notes name the concept and the operation the engine lacks, and call the engine only in the branch capability_usable_v opens: one error per refusal; - the secret-key traits (detail/secret_keys.hpp) speak first in from_secret, secret(), generate() without the backend's own generation, Ed25519's to_x25519() and async_from_secret; a key pair's private formats are refused for a kept key by the new refusal_reason::private_kept, before WebCrypto's reason; - the symmetric families and the boxes ask one helper per family from every member that calls the engine; - the opaque backend's engines are marked; test/backends/message_only.hpp is a marked P-256 engine with the message form alone and no decompress; - message tests, each alone with an MSVC twin and a fix: opaque_keys_kept, opaque_async_from_secret, p256_prehashed_message_only, p256_operation_named, secp256k1_other_backend_first, chacha20_capability_missing, secretbox_capability_missing.
- P-256's sign and verify of a message take the engine's message form (sign_message, verify_message) when it has one, else the message's SHA-256 on the digest form; the signature is made low S either way and the policy is checked first. A prehashed digest asserts the digest form alone, and an engine with neither form gets one message naming both, so sign and verify agree with supports_v (I1). Run test p256_message_form signs and verifies a message on the message-only stand-in (M3). - The verifications and the DER codec ask one helper per engine, so a refusal is said once whatever the call shape; p256_prehashed_message_only pins it with crypto's function and the member (M2). - A kept key's private formats are refused with two reasons, raw's and the one pkcs8 and pem(pkcs8) share, which the tests tell apart (M1). - The asynchronous members assert their engine's async_* concept, and complete through refused_by_engine in the branch no program compiles (M4). - The fix texts speak in the program's terms (M5); every message names a capability as crypto::capabilities::<name> and a trait as crypto::imports_secret_keys_v (M6).
- The message-only stand-in counts its verify_message calls, so that p256_message_form proves a high S is refused under low_s_only before the engine, which any_s then reaches (N1). - Message test opaque_async_engine_missing, with its MSVC twin and fix, pins an asynchronous member's async_* assertion alone: Ed25519's async_verify on the opaque backend's asynchronous form (N2). - p256_message_form's header says what it checks of supports_v's predicate, not supports_v itself (N3).
- What a backend has, operation by operation: what supports_v counts (the use operations), a partial cell, supports_capability_v, the generated catalogue of every capability with the backends that have it, P-256's digest and message forms, keys that never leave their backend (the two secret-key traits and the calls they refuse), and the promises every backend keeps; the backend interfaces stay crypto's own (design D10). - generate.py writes generated/capabilities.adoc from crypto.json's capability cells, with each backend's secret-key traits; generate_test checks it. - choosing_backend.adoc names supports_v's use operations and supports_capability_v in place of "has the algorithm"; the changelog line. - The reference's Doc Comment faults the page's build found: the briefs of supports.hpp's three traits, and the secp256k1 engines' same-type checks.
The contract names each backend's allocations against the facts page (libsecp256k1 allocates in the program's memory resource), the imports_secret_keys_v brief names the key pair operations that consult it, errc::not_supported is named as such, the opening paragraph leaves the key operations to supports_capability_v, P-256's message form is said to have no shipped backend yet, and the secret-key traits are a generated page of their own.
The allocation promise is true as written (libsecp256k1's default resource is the C++ heap), OpenSSL's row and the chapter name Poly1305's per-call MAC context, and two paragraphs are rewrapped.
async_from_secret asks the import trait whatever its engine has (message test kept_async_engine_imports, with its MSVC twin); can_sign and can_verify ask P-256's digest form for a prehashed input, as sign and verify do; the public-key formats have a use case of their own, so llms.txt promises them no PKCS#8; async_engine's comment reworded.
can_sign and can_verify of a prehashed P-256 digest form the digest concept only for a specialised engine, so the query answers on a backend without P-256 and on the default key pair without its header, as before (p256_message_form asserts both); the asynchronous Doc Comments say what they check as can_sign and can_verify do.
…byte MSVC's C4267, an error under /WX, refused the implicit size_t to uint8_t assignment (the final lanes' MSVC 14.3 and 14.5).
…r column The family's title spans its rows, which makes the table complex in WCAG's terms; the site's html-validate (wcag/h63) then asks every header cell for its scope, which a header column gives (the head's col, the family's row). Found by webcpp#71's Site job.
pinhopro
pushed a commit
to webcpporg/webcpp
that referenced
this pull request
Dec 6, 2026
pinhopro
added a commit
to webcpporg/webcpp
that referenced
this pull request
Dec 6, 2026
libs/crypto: backend concepts (webcpporg/crypto#31)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
crypto's change backend-concepts (proposals: libraries/crypto/openspec/changes/backend-concepts), tasks 1.1 to 4.2: one C++20 concept per backend capability, every engine checked against them, and the public API refusing by capability.
detail/backend/*.hpp(common,keys,symmetric,boxes,ed25519,x25519,p256,secp256k1,entropy,asynchronous,concepts), each with its semantic contract; every shipped engine asserted against the concepts its table cells promise, after each specialisation. Stand-ins prove each requirement both ways.sign_message,verify_message), the message given whole to a backend that has it, low S either way, the policy before the engine. secp256k1 behind one engine per capability.<webcpp/crypto/supports.hpp>:crypto::capabilities::*tags,supports_capability_v,exports_secret_keys_v,imports_secret_keys_v;supports_vcounts the use operations alone. The table,detail/capabilities.hppandcapabilities_testare generated fromcrypto.json, and the test checks every cell against the concepts both ways.private_kept_rawandprivate_kept_pkcs8. Test-only backends:opaque/opaque_async(keys that never leave, asynchronous engine) andmessage_only(P-256's message form alone). Every refusal is a message test with its MSVC twin.Verification, local, on this head's predecessor
b56e53a(the head adds only an MSVC C4267 cast in a test, rerun green on MSVC 14.3 and 14.5): clang 820, gcc-15 584, wasip2 767, wasip3 748, emscripten 489, 0 failed; c-suites, page, generator and tools tests, both full lints, Linux constant-time lane, Mac dudect, MSVC 14.3 and 14.5 x64 debug (fresh third party on 14.5), arm64 14.5 timing set (proposals' reference/STATUS.md). Per-task reviews and a final whole-branch review, all clean.🤖 Generated with Claude Code