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
4 changes: 2 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ specific to codecs.
## What codecs is

- **webcpp's own library** (`"port-of": null`): one contract for converting bytes and typed
values to text and back, and any data through one-shot and stream transformers, with seven
formats (hex, base32, base58, base64, bech32, percent-encoding and RFC 7468's armor).
values to text and back, and any data through one-shot and stream transformers, with eight
formats (hex, base32, z-base-32, base58, base64, bech32, percent-encoding and RFC 7468's armor).
- **Header-only**, built on secure (`<library>/webcpp/secure//secure`) and config. percent alone
depends on Boost.URL, through the target `/webcpp/codecs//percent`.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
codecs holds webcpp's one contract for converting bytes and typed values to text and back, and any
data through one-shot and stream transformers, for C++20 on every target
[webcpp](https://github.com/webcpporg/webcpp) has: native (Linux, macOS, Windows with MSVC),
wasip2, wasip3 and Emscripten. Over that contract it ships seven formats: hex, base32, base58,
base64, bech32, percent-encoding and RFC 7468's armor, every name in `webcpp::codecs`. It is
wasip2, wasip3 and Emscripten. Over that contract it ships eight formats: hex, base32, z-base-32,
base58, base64, bech32, percent-encoding and RFC 7468's armor, every name in `webcpp::codecs`. It is
header-only and builds on webcpp::secure for its secret and constant-time operations.

Every format is a value (`codecs::formats::hex`), an operation a function of it
Expand Down
2 changes: 1 addition & 1 deletion doc/allocators.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ codecs allocates through it and through nothing else.
| `text<N>`, `bytes<N>`, `tokens::fixed<N>`, and the secret results in place
| nothing

| the conversion of every format, in every form: hex, base32, base58, base64, bech32, percent and the armor
| the conversion of every format, in every form: hex, base32, z-base-32, base58, base64, bech32, percent and the armor
| nothing: the scratch of each is in place; a result on the heap, as the default result of a run-time size or `armor_parts`, allocates through its own allocator

| `over` with a static bound
Expand Down
2 changes: 1 addition & 1 deletion doc/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ of every version are on webcpp's index page.
* Added codecs.hpp and config.hpp: encode, decode, tokens, results, secrecy and over, errors as values, value_formats for a library's value types, and max_decode_input_size as a value codec's bound on every path.
* Added the stream contract: one-shot and stream transformers, pipe, drain, each, buffered, encoder and decoder.
* Added hex.hpp: hex, constant time, streaming natively and decoding in place.
* Added base32.hpp: base32 and base32hex of RFC 4648, padding an option, constant time, streaming natively and decoding in place.
* Added base32.hpp and zbase32.hpp: base32 and base32hex of RFC 4648, padding an option, and z-base-32, pkarr's keys, constant time, streaming natively and decoding in place.
* Added base64.hpp: base64 and base64url of RFC 4648, canonical on decode, constant time, streaming natively and decoding in place.
* Added base58.hpp: basic_base58 with a bound, an alphabet and a checksum as parameters, constant time when secret, streaming through buffered.
* Added bech32.hpp: bech32 and bech32m with a maximum length, words and bytes data, constant time and streaming natively.
Expand Down
6 changes: 3 additions & 3 deletions doc/codecs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ codecs is the conversion library of
https://github.com/webcpporg/webcpp[webcpp], for a {cpp}20 program on every
target webcpp has: one contract for converting bytes and typed values to text
and back, and any data through one-shot and stream transformers, with hex,
base32, base58, base64, bech32, percent-encoding and RFC 7468's armor. It is
built on webcpp::secure, so that the conversions of secrets keep their timing
and their memory clean.
base32, z-base-32, base58, base64, bech32, percent-encoding and RFC 7468's
armor. It is built on webcpp::secure, so that the conversions of secrets keep
their timing and their memory clean.

The page teaches the library in the order a program meets it. <<codecs-overview>>
says what codecs is and what it is not, <<codecs-formats>> has one section for
Expand Down
41 changes: 40 additions & 1 deletion doc/formats.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ A decode reads its whole text before it writes a byte, so that on every error
nothing is written; its buffer form leaves the bytes past the result as they
were. Which formats decode into the text's own storage, which the table in
<<codecs-overview>> lists, is a promise of the format's page and not of the
core: hex, base32 and base64 write byte `i` after the characters it comes from
core: hex, base32, z-base-32 and base64 write byte `i` after the characters it comes from
are read, and base58, bech32, the armor and percent ask that the input and the
output do not overlap (percent's decode is Boost.URL's, which promises nothing
of overlapping buffers).
Expand Down Expand Up @@ -96,6 +96,45 @@ include::{examples}/rfc4648.expected[]
than the text seems to give is not stuck: it waits for the next character or
for `flush`.

[#codecs-zbase32]
=== z-base-32

`formats::zbase32` is z-base-32, Zooko O'Whielacronx's human-oriented base-32
encoding: base32's grouping of five bytes as eight symbols, over the alphabet
`ybndrfg8ejkmcpqxot1uwisza345h769`, in lower case and with no padding. pkarr
writes an Ed25519 public key in it, 32 bytes as the 52 characters of a name,
and iroh reaches pkarr's names. It has no option. Encode writes the exact size,
so a 32-byte key is a `text<52>`, and decode is canonical: an upper-case
letter, a `=` and any other character that is no symbol are
`invalid_character`, a length no byte count has is `invalid_length`, and
non-zero bits past the last byte are `invalid_padding`. It is constant time on
every call and streams natively, by base32's implementation over its own
alphabet.

[source,cpp]
----
include::{examples}/zbase32_pkarr.cpp[tag=example,indent=0]
----

[listing]
----
include::{examples}/zbase32_pkarr.expected[]
----

*Pitfalls.*

* z-base-32 is lower case alone. A name that went through a system that
changes case (a DNS label is compared without case) is lowered by the
program before it is decoded.
* pkarr's decoder is more lenient than this one: it ignores the bits past the
last byte and truncates a length no byte count has. A key's 52nd character is
`y` or `o`; a text that pkarr takes and codecs refuses is no text a z-base-32
encoder writes.
* The document's bit-length mode, a length in bits known to both sides, is not
codecs': the format converts bytes.
* codecs reads the text alone: a `pk:` prefix, a key in a URL's host and the
check that the bytes are a point of the curve are the program's, or pkarr's.

[#codecs-base64]
=== base64 and base64url

Expand Down
9 changes: 8 additions & 1 deletion doc/overview.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ typed decode gives the digest back as a `std::array`, and a fault is a value of
| yes
| <<codecs-base32>>

| `zbase32`
| `<webcpp/codecs/zbase32.hpp>`
| always
| yes
| yes
| <<codecs-zbase32>>

| `base64`, `base64url`
| `<webcpp/codecs/base64.hpp>`
| always
Expand Down Expand Up @@ -118,7 +125,7 @@ The values are fixed and never reused:

| 1 | `invalid_character` | a character is outside the format's alphabet (a byte of 128 or more included), or a word of bech32 is above 31
| 2 | `invalid_length` | the length is one no encoding of the format has, or a typed decode into a `std::array` finds another number of bytes than the array's size
| 3 | `invalid_padding` | the padding is missing, misplaced or not zero: base32 and base64's `=` and trailing bits, bech32's padding bits
| 3 | `invalid_padding` | the padding is missing, misplaced or not zero: base32 and base64's `=` and trailing bits, z-base-32's trailing bits, bech32's padding bits
| 4 | `checksum_mismatch` | the checksum of bech32 or of a base58 with a checksum is wrong
| 5 | `invalid_prefix` | the human-readable part or the label is not the format's, or is a fault of the format's configuration
| 6 | `mixed_case` | a bech32 text holds both cases
Expand Down
2 changes: 1 addition & 1 deletion doc/secrecy.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ and is not offered on a temporary.

=== What is constant time

The encode and decode of `hex`, `base32`, `base32hex`, `base64`, `base64url`,
The encode and decode of `hex`, `base32`, `base32hex`, `zbase32`, `base64`, `base64url`,
`bech32`, `bech32m` and the armor's payload are constant time on every call, one-shot and
streamed, public or secret: they index no table by a data byte and branch on no
data byte, a decode accumulates its verdict over the whole input and reports it
Expand Down
2 changes: 1 addition & 1 deletion doc/streams.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ before it returns, by every path.
| `hex`
| an encoder that holds the low digit of a byte, a decoder that holds one digit of a pair

| `base32`, `base32hex`, `base64`, `base64url`
| `base32`, `base32hex`, `zbase32`, `base64`, `base64url`
| an encoder that holds the bytes of a group not yet whole, a decoder that holds the bits of a group; a padded decoder holds the bytes of the group's last places until the next character or `flush`

| `percent`
Expand Down
1 change: 1 addition & 0 deletions example/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ webcpp.example percent_segment.cpp : <library>/webcpp/codecs//percent ;
webcpp.example base58_address.cpp ;
webcpp.example own_format.cpp ;
webcpp.example rfc4648.cpp ;
webcpp.example zbase32_pkarr.cpp ;
webcpp.example tokens.cpp ;
webcpp.example results.cpp ;
webcpp.example over_chain.cpp ;
Expand Down
59 changes: 59 additions & 0 deletions example/zbase32_pkarr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2026 WebCpp.org
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// z-base-32 in a program: a pkarr public key, the 32 bytes of an Ed25519 key, as the 52 characters
// of a pkarr name, held in place both ways, and the texts a canonical decode refuses. codecs reads
// the text alone: the `pk:` prefix, a key in a URL's host and the check that the bytes are a point
// of the curve are pkarr's.

// tag::example[]
#include <webcpp/codecs.hpp>
#include <webcpp/codecs/zbase32.hpp>

#include <array>
#include <cstdint>
#include <iostream>
#include <string_view>

namespace codecs = webcpp::codecs;
namespace formats = webcpp::codecs::formats;

namespace {

// The public key of pkarr's own tests: 32 bytes, so its text is a text<52>, with no heap.
constexpr std::array<std::uint8_t, 32> public_key{
0x01, 0xb4, 0x67, 0xa3, 0xb7, 0x91, 0x3a, 0xb2, 0x7a, 0x04, 0xa8, 0xed, 0xf2, 0xf3, 0xfb, 0x07,
0x4c, 0xfe, 0x0e, 0xcf, 0x4b, 0xab, 0xe1, 0x08, 0xd6, 0x7b, 0xe3, 0x85, 0x3b, 0x0f, 0x26, 0xc5,
};

// What a decode says of a text.
void show(std::string_view label, std::string_view text) {
auto const key = codecs::decode<std::array<std::uint8_t, 32>>(formats::zbase32, text);
std::cout << label << ": " << (key ? "a key" : key.error().message()) << '\n';
}

} // namespace

int main() {
codecs::text<52> const name = codecs::encode(formats::zbase32, public_key).value();
std::cout << "pkarr name: " << name.view() << '\n';

// The name back to the key, into an array of exactly 32 bytes.
auto const key = codecs::decode<std::array<std::uint8_t, 32>>(formats::zbase32, name);
std::cout << "decodes back: " << (key && *key == public_key) << '\n';

// z-base-32 is lower case alone, has no padding, and its bits past the last byte are zero: a
// 52nd character other than `y` or `o` is no encoding of 32 bytes.
show("upper case", "YG4GXE7Z1R7MR6ORIDS9FH95Y7GXHDSXJQI6NNGSXXTAKQAXR5NO");
show("a trailing bit", "yg4gxe7z1r7mr6orids9fh95y7gxhdsxjqi6nngsxxtakqaxr5nt");
show("51 characters", "yg4gxe7z1r7mr6orids9fh95y7gxhdsxjqi6nngsxxtakqaxr5n");
// A valid text of another length is no key: decode into 32 bytes is exact.
codecs::text<7> const four_bytes =
codecs::encode(formats::zbase32, std::array<std::uint8_t, 4>{}).value();
show("4 bytes", four_bytes.view());
}

// end::example[]
6 changes: 6 additions & 0 deletions example/zbase32_pkarr.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pkarr name: yg4gxe7z1r7mr6orids9fh95y7gxhdsxjqi6nngsxxtakqaxr5no
decodes back: 1
upper case: a character is outside the format's alphabet
a trailing bit: the padding is missing, misplaced or not zero
51 characters: the length is one no encoding of the format has
4 bytes: the length is one no encoding of the format has
Loading
Loading