SHC vNext QR proof of concept

Codec interface, version 0.1

Rendered from CODEC-ABI.md in the repository.

A codec turns the compact bytes carried in a dense QR code into a FHIR Bundle. It is a WebAssembly module that can compute and do nothing else.

What a codec is

  • One WebAssembly module.
  • No imports of any kind, and no start function. A host refuses a module that has either.
  • Three exports:
ExportSignatureMeaning
memorylinear memoryHas a declared maximum.
shc_alloc(len: i32) -> i32Returns a pointer to len writable bytes, or 0 if it cannot.
shc_decode(ptr: i32, len: i32) -> i32Decodes the input at ptr. Returns a pointer to a result record.

The result record is 12 bytes, little-endian: status: i32, out_ptr: u32, out_len: u32.

StatusNameMeaningOutput
0OKDecoded.The Bundle, as UTF-8 JSON.
1MALFORMEDNot a valid encoding for this codec.A short message for logs.
2UNSUPPORTEDThis codec's format, in a version or with a feature this module does not implement.A short message.
3LIMITValid, but would exceed the codec's or this interface's limits.A short message.

Hosts act on the status, never on the message.

What a host does

  1. Checks the module: size, no imports, no start function, the three exports.
  2. Creates a new instance for each card. Nothing carries over from one card to the next.
  3. Calls shc_alloc(len), copies the input in, calls shc_decode.
  4. Reads the record and checks that the output lies inside memory.
  5. On status 0, parses the JSON and checks it is a Bundle.

A host never passes more than 4,096 bytes; no QR code holds more. An encoder never produces more: a dataset that would need more is not representable in that codec.

A host lets memory grow up to the module's declared maximum. A host that refused growth earlier would reject cards that other hosts accept.

If the module traps, exhausts its stack, or runs past the host's time limit, the host treats the card as MALFORMED.

shc_alloc and shc_decode are each called once per instance. shc_alloc(0) returns a valid, non-zero pointer.

Rules for every codec

  • Total. For every input, including random bytes, the codec returns a status. It never traps, never reads or writes outside its memory, and never loops without bound.
  • Bounded. For every input the codec finishes, and its work, memory and output are bounded by limits that follow from its own model (so many days, so many series). The codec documents those worst cases.
  • Deterministic. The same bytes always give the same output bytes. To make this checkable, a module contains no floating-point instructions, and uses only WebAssembly 1.0 plus mutable-globals, sign-extension, multi-value, and the memory instructions of bulk-memory (memory.copy, memory.fill, memory.init, data.drop). No tables beyond what function calls need, no reference types, no threads, no SIMD.
  • Bounded stack. A codec's own stack lives in its linear memory and can overflow silently. Recursion depth and stack use are bounded for every input.
  • Canonical. Each dataset has exactly one encoding: if the codec accepts a byte string, encoding its output again gives the same bytes.
  • No artificial ceilings. This interface sets no budget for instructions, time, memory or output, and a codec does not refuse a dataset because decoding it would be slow. What a host applies are safety rails set one or two orders of magnitude beyond anything a real card needs, to stop a broken or hostile module: this project's host allows an 8 MiB module, 1 GiB of memory and 256 MiB of output, and suggests stopping a decode only after tens of seconds. Tools that make cards warn when a card is unusually costly to decode or too dense to scan comfortably; they do not forbid it.

Output

  • Minified UTF-8 JSON with a fixed key order, so that two implementations of a codec can be compared byte for byte.
  • A FHIR R4 Bundle of type collection. Entries have fullUrl values resource:0, resource:1, and so on, and refer to one another by those values.
  • resource:0 is the subject of the record, a Patient, and facts refer to it. A codec whose model has no identity, or whose user chose not to include any, emits the smallest Patient that is valid FHIR (a completely empty resource is not). A host that knows who the card belongs to may replace it.

The developer's side: a constrained input model

The registry accepts only constrained codecs. General-purpose compression of arbitrary Bundles is not registered: it has no input model to write against and cannot give one encoding per dataset. Data with no codec goes in a SMART Health Link.

A codec publishes three things: an input model (a JSON Schema for exactly the facts it can carry), the wire format, and the mapping from model to Bundle. Developers write against the model and never construct FHIR.

  • A codec module may export shc_encode(ptr, len) -> rec, with the same record as shc_decode. Its input is the model as UTF-8 JSON; its output on status 0 is the card's codec input.
  • An encoder refuses a model it cannot represent exactly, with status MALFORMED and a message naming the problem. It never drops or approximates.
  • A change to the model is a new codec, with a new id.

Naming a codec

A card begins with a codec reference:

  • a registry id, written as a LEB128 unsigned integer (ids 1 to 127 take one byte, ids up to 16,383 take two); or
  • the byte 0 followed by the 32-byte SHA-256 of the module, for a codec that is not registered.

A registry id always names the same module bytes; a changed codec gets a new id. A host that meets a hash it does not have fetches the module from wherever it is offered and checks the hash before running it.

The card around the codec input

A card is the codec reference, the codec input, and a 2-byte check: the first 2 bytes of the SHA-256 of everything before it. The host verifies the check and refuses a card that fails it; the codec sees only its input. A codec's input need not detect truncation or damage on its own.

Not decided

  • How a host finds the module for a hash that is not published at https://joshuamandel.com/shc2-poc/codecs/sha256/<hex>.wasm.
  • Where the safety rails should sit, once there is experience with real hosts.