A content-addressed (0x00 || sha256(wasm)) WebAssembly codec (Codec ABI v0.1) for eyeglasses and contact lens prescriptions. Encodes a constrained optical prescription JSON model (VisionRx) into a canonical, bit-packed binary payload and decodes it deterministically into a minified FHIR R4 Bundle.
- Module:
vision-rx.wasm(29,823 bytes) - SHA-256:
7e6ae21f82b4f2bc5c9046d0416d2870bdbbf869401b90a645577e5aac3f9cd0 - Audit:
auditWasm(wasm)= 0 violations (0 imports, no start section, no floats, no SIMD/threads, declared max memory 16,384 pages, link-time zero-panic proof,#![no_std]+#![forbid(unsafe_code)]incore) - Exports:
memory,shc_alloc,shc_decode,shc_encode
Benchmark Summary (4 Showcase Samples)
Card size includes the 33-byte content-addressed codec header (0x00 || sha256(wasm)) and 2-byte SHA-256 card check (35 bytes of framing around the wire payload).
| Sample | FHIR R4 JSON | Raw DEFLATE-9 | Wire Payload | SHA-256 Card | QR Digits | QR Symbol (EC-L) | vs FHIR JSON | vs DEFLATE |
|---|---|---|---|---|---|---|---|---|
progressive-prism | 2,158 B | 788 B | 116 B | 151 B | 367 | V8-L (49x49) | 14.3x | 5.2x |
toric-contacts | 1,595 B | 629 B | 109 B | 144 B | 350 | V7-L (45x45) | 11.1x | 4.4x |
simple-myopia | 1,420 B | 537 B | 35 B | 70 B | 170 | V5-L (37x37) | 20.3x | 7.7x |
glasses-plus-contacts | 2,546 B | 819 B | 165 B | 200 B | 486 | V9-L (53x53) | 12.7x | 4.1x |
Input Model (model.d.ts)
patient(family,given[1..4],birthDateYYYY-MM-DD;{}allowed for anonymous prescriptions)prescriber(optional:family,given[1..4],qualification, 10-digit USnpi)dateWritten(YYYY-MM-DD,1900-01-01..2155-12-31)expires(optionalYYYY-MM-DD,1..4095days afterdateWritten)pdMm(45.0..80.0mm, exact0.5mm step),pdRightMm/pdLeftMm(20.0..45.0mm, exact0.5mm step)lensSpecifications(1..4specs):product:"lens" | "contact"eye:"right" | "left"sphere:-25.00..+25.00D (0.25D step)cylinder:-12.00..+12.00D (0.25D step)axis: integer1..180degreesadd:+0.25..+5.00D (0.25D step)prism:1..2entries of{ amount: 0.25..20.00 (0.25 step), base: "up" | "down" | "in" | "out" }power:-25.00..+25.00D (0.25D step)backCurve:6.00..11.00mm (0.01mm step)diameter:8.0..18.0mm (0.1mm step)duration:{ value: 1..365, unit: "d" | "wk" | "mo" }color(<= 32UTF-8 B),brand(<= 64UTF-8 B),note(<= 128UTF-8 B)
shc_encode uses pure integer fixed-point decimal parsing (zero floats) and rejects any out-of-range value, inexact step, unknown field, duplicate key, or control character with status 1 (MALFORMED).
Canonical Bit-Packed Wire Format (v1)
MSB-first bitstream, zero-padded to the final byte boundary (non-zero padding bits and trailing bytes are rejected as MALFORMED):
- Header (8 bits): 4-bit version (
1;0=>MALFORMED,2..15=>UNSUPPORTED) + 4 presence bits (has_patient,has_prescriber,has_expires,has_pd). - Dates:
dateWrittenas 17-bit days since1900-01-01; optionalexpiresas 12-bit offset (1..4095days). - Patient (if present): presence bits + length-prefixed UTF-8 strings (
6-bit length1..64) + optional 17-bitbirthDate. - Prescriber (if present): presence bits + 2-bit qualification tag (
0=none,1="OD",2="MD",3=custom UTF-8 string excluding"OD"/"MD") + optional 34-bit US NPI integer. - Pupillary Distance (if present): 3 presence bits + 7-bit binocular PD + 6-bit right/left monocular PD (exact half-millimeter integers).
- Lens Specifications (
2-bit count1..4): per spec:product(1b),eye(1b), 13-bit presence/prism-count mask, compact fixed-point integer fields, and canonical 1-bit deduplication ofcolor/brand/noteagainst the immediately preceding lens specification when identical (non-canonical literal duplicates are rejected).
FHIR R4 Output (shc_decode)
Emits a deterministic, minified FHIR R4 Bundle (type: "collection"):
resource:0—Patient(id: "0",name,birthDate)resource:1(optional) —Practitioner(id: "1",identifierwithhttp://hl7.org/fhir/sid/us-npi,name,qualification)resource:2(orresource:1if no prescriber) —VisionPrescription(status: "active",created,dateWritten,patient: { reference: "Patient/0" }, optionalprescriber: { reference: "Practitioner/1" }, optionalexpiresextension, andlensSpecification[]usinghttp://terminology.hl7.org/CodeSystem/ex-visionprescriptionproductand UCUM durations)resource:3(optional, when any PD is present) —Observation(LOINC79886-8Pupillary distance,valueQuantityinmmfor binocular PD, and right/left monocular PDcomponent[])
Worst-Case Bounds
- Max wire size:
< 1,700bytes (well under the 4,096-byte QR ceiling) - Max FHIR JSON output:
< 12KiB - WASM memory:
<= 2pages (128KiB) on all valid inputs; zero recursion (O(1)stack)
Build & Test
# Build vision-rx.wasm (requires rustup stable + wasm32-unknown-unknown target)
bash codecs/vision-rx/rust/build.sh
# Run full audit, round-trip, fuzz, and benchmark test suite
bun test codecs/vision-rx/vision-rx.test.ts