Exact erasure budgets

An erasure function turns a small input into an output that may represent more than one input. Add #[erasure(budget, record_name, undo_name)] to ask the compiler to count the ambiguity exactly and generate a recorder and decoder.

import std.erasure

#[erasure(2, record_bucket, undo_bucket)]
fn bucket(x: u8) -> u8 { x / 3 }

fn main() {
    var tape = erasure.Tape.new()
    let y = record_bucket(8, &tape)
    assert(y == 2 && tape.bit_len() == 2, "three inputs produce bucket two")
    assert(undo_bucket(y, &tape).unwrap() == 8, "the original input")
    assert(tape.is_empty(), "the record was consumed")
}

The annotated function still works as an ordinary function. If it is public, its two companions are public. The budget is an integer literal or the name of an integer const in scope, from 0 through 16. The declaring module imports std.erasure explicitly and can give the import an alias.

Bits per output

For each output y, the compiler counts the inputs producing it. If the count is n, the recorder runs the original function, then writes the index of the input in that output’s numerically ordered list. It writes ceil(log2(n)) bits. The decoder knows the output and therefore the width. In particular, a unique input uses zero bits. An output with n possible inputs needs at least n distinct records, so this width is exact when the output is available. The budget limits the largest width. If the function exceeds it, the diagnostic includes the output, preimage count and required width. On a tie for the largest count it reports the lowest numeric output.

The companions have signatures fn record_name(x: In, inout tape: Tape) -> Out and fn undo_name(y: Out, inout tape: Tape) -> Result(In, UndoError). Keep the outputs in call order and undo them in reverse order. Tape uses a growable byte buffer. bit_len counts only records’ bits and a push can grow the buffer. peek_bits and pop_bits return None when the requested bits are unavailable. A zero-width read returns zero even on an empty tape. push_bits(value, width) accepts widths from 0 through 16 and values that fit the width. clear empties a tape for reuse.

Undo reports UnreachableOutput when the output has no input, TruncatedTape when the tape is too short, and InvalidRank when the read bits give an index outside the group. Each error leaves the tape intact. A budget of zero certifies injectivity and the decoder uses no tape bits. For equal-sized domains that also certifies a permutation.

Supported functions

Use a safe nongeneric free function with one by-value u8 or u16 parameter and a bool, u8 or u16 value result. The compiler evaluates scalar MIR over every input. This supports integer and boolean operations, control flow, and direct calls among supported functions with an acyclic call graph. It reports traps with their input and has deterministic per-input and batch evaluation limits. Analysis runs during plain jihoo check and follows the selected overflow mode. Use explicit wrapping arithmetic when wraparound is part of the function in both checked and release builds.

The inverse uses read-only tables whose size depends on the domains. Compiled programs and compile-time blocks can call both companions. The telemetry and S-box example rewinds a stream whose all-clear frames need zero bits and whose all-alarm frames need 13, and inverts an imperative eight-bit permutation by table lookup.

Esc