Skip to reference

std.bech32

wolf-std d58f9e1 · toolchain pin verified against wolf 0.2.41 (0261597). Source signatures and documentation; execution support is stated in each module.

All modules · Plain text · Stability tier: core

Module

std.bech32 — BIP-173 checksummed base32. Stability tier: core. The checksum is Bech32 (constant 1), NOT Bech32m. This module has no Bitcoin address or witness-version semantics. Generic data are 5-bit words in List[byte]; the byte helpers additionally check exact decoded length and canonical 8-to-5 padding for native age keys. BIP-173's 90-character bound applies here. Native X25519 age keys fit; this is not the age extension for arbitrarily long PQ keys. Decoders reject mixed case and normalize HRPs to lowercase. Encoders produce lowercase; callers may uppercase the WHOLE result for an age identity's presentation. Neither direction corrects errors or renders input in an error. All failures are parse, not traps. Pure, comptime-safe.

No division, branch or table index on a data character (sc84, wolf-std#151). An age identity is a secret key in this encoding, so the codec treats every data character and word as secret: the alphabet is read by a constant 32-step pass with an arithmetic match (value, char_of), case is folded by arithmetic flags, each checksum step selects its generator terms by masks, the 8-to-5 and 5-to-8 regroupings are shifts and masks on a wrapping[u64] word, and an invalid character is remembered and refused after the whole string has been read. What still branches: lengths, the HRP (public by format), the position of the last 1, and the final verdicts (checksum, padding, length, mixed case), whose outcome is the published answer. This is a source-level statement plus a generated-code gate: cargo xtask codegen fails on any division instruction in this module's default or release objects on the host target. It is not a timing measurement, and the compiler is not audited. One encoder and one decoder serve every caller, secret or public; there is no separate fast path to choose by mistake.

Types and traits

Decoded

struct · Source line 36

pub struct Decoded { hrp: str, data: List[byte], }

A validated lowercase HRP and 5-bit data words, excluding checksum. The words are encoding data, not necessarily whole or canonical bytes.

Functions and methods

encode

fn · Source line 109

pub fn encode(hrp: str, data: List[byte]) -> str ! {parse}

BIP-173 encoding of 5-bit data words. HRP must be 1..83 printable ASCII characters; uppercase letters are normalized to lowercase. Raises parse on invalid HRP, a word above 31, or total length above 90 characters. Total over its input types; pure, comptime-safe.

bech32.encode("a", List[byte]())? == "a12uel5l"

decode

fn · Source line 149

pub fn decode(s: str) -> Decoded ! {parse}

Validates BIP-173 syntax/case/checksum and returns lowercase HRP and 5-bit data excluding checksum. Raises parse on any failure, including non-ASCII, missing/empty HRP, short checksum and length outside 8..90. Uses the LAST separator. No correction, no input in diagnostics. Total over UTF-8; pure, comptime-safe.

let d = bech32.decode("A12UEL5L")?
d.hrp == "a"
d.data.len == 0

encode_bytes

fn · Source line 194

pub fn encode_bytes(hrp: str, b: List[byte]) -> str ! {parse}

Encodes bytes as MSB-first 5-bit words with zero padding, then Bech32. Rows/traps as encode; the output must fit BIP-173's 90 characters. Native age uses HRP age or age-secret-key-, and 32 input bytes. Pure, comptime-safe. No division, branch or table index on the bytes (see the module header); never log a real identity.

let s = bech32.encode_bytes("age", "x".bytes())?
bech32.decode_bytes(s, "age", 1)?.len == 1

decode_bytes

fn · Source line 227

pub fn decode_bytes(s: str, hrp: str, n: int) -> List[byte] ! {parse}

Decodes canonical MSB-first bytes with exactly n bytes and the specified lowercase hrp. Raises parse for any Bech32 failure, a different HRP/length, negative n, five or more residual bits, or nonzero residual bits. A valid checksum alone is NOT enough. Pure, comptime-safe; total, no traps, no input in errors.

let s = bech32.encode_bytes("age", "x".bytes())?
let b = bech32.decode_bytes(s, "age", 1)?
b.len == 1

Constants

No public declarations in this section.

Errors and row tags

Tags named by public signatures: parse. Their conditions are documented on the functions that return them.

No public declarations in this section.