SHC vNext QR proof of concept
SHA-256 Extension · 0x00 0 WASM imports · 0 floats Lossless time-series + deterministic FHIR summary synthesizer

CGM & Ambulatory Glucose Profile

100% lossless 14-day FreeStyle Libre CGM glucose traces (up to 4,019 readings) expanding into full HL7 FHIR R4 CGM IG Bundles with integer-computed GMI, CV, Mean, and 5-band TIR.

WASM module43,565 Bcgm-agp.wasm
Card sizes519–1,895 Bacross 4 sample records
Max FHIR ÷ card1,386×deterministic FHIR R4 output
Best QR versionVersion 16-Lup to Version 33-L

Try live in Viewer (libre-14d) Download WASM (by hash) View model.d.ts

Quick launch any sample record in the universal viewer:

libre-14d libre-7d-exact t1d-variable-14d clinic-3d-compact

Card header
0x00 followed by the 32-byte SHA-256 digest of the WebAssembly module (35 bytes total card framing including the 2-byte SHA-256 check)
SHA-256
f57b5d846fea9c2ad54a06b77b9c48458d88b367610b0788997e7e00a4e58dac (download by hash)
WASM exports
memory, shc_alloc, shc_decode, shc_encode (0 host imports)
Machine-readable files
model.d.ts · codecs/cgm-agp/cgm-agp.wasm · Source on GitHub

Sample records & benchmarks (4)

Each sample record below was encoded at build time by cgm-agp.wasm, wrapped in a SHA-256 content-addressed card (0x00 || sha256), and decoded back into a FHIR R4 Bundle. Click any record name to open its live QR card in the universal viewer.

RecordCard & QR (L / M)FHIR JSONDEFLATE (L9)FHIR ÷ cardDownloads
libre-14d14-day FreeStyle Libre trace (4,019 readings, real sensor) 1,895 BVersion 33-L / Version 37-M 2,321,772 B4,028 entries 48,728 Bfits none 1,225× model.json · card.txt · fhir.json
libre-7d-exact7-day exact 1-second timestamp trace (2,016 readings) 1,591 BVersion 30-L / Version 34-M 1,172,854 B2,025 entries 26,734 Bfits none 737× model.json · card.txt · fhir.json
t1d-variable-14d14-day high-variability Type 1 diabetes trace (3,792 readings) 1,581 BVersion 30-L / Version 34-M 2,191,891 B3,801 entries 50,361 Bfits none 1,386× model.json · card.txt · fhir.json
clinic-3d-compact3-day compact clinic snapshot (864 readings) 519 BVersion 16-L / Version 19-M 502,720 B873 entries 11,751 Bfits none 969× model.json · card.txt · fhir.json

Technical specification & format

Rendered from codecs/cgm-agp/README.md in the repository.

A content-addressed (0x00 || sha256(wasm)) WebAssembly codec conforming to CODEC-ABI v0.1 that packs 100% lossless Continuous Glucose Monitoring (CGM) histories into a single dense QR code and decodes directly into an HL7 FHIR R4 CGM Implementation Guide Bundle — including deterministically computed CGM Summary / Ambulatory Glucose Profile (AGP) metrics at zero extra QR bytes.

Module Identity & Audit

  • Binary: cgm-agp.wasm — 43,565 bytes (42.5 KiB)
  • SHA-256: f57b5d846fea9c2ad54a06b77b9c48458d88b367610b0788997e7e00a4e58dac
  • Exports: memory, shc_alloc, shc_decode, shc_encode
  • Imports: 0 (link-time zero-panic guarantee — any reachable panic turns into an unresolved import)
  • Static Audit (auditWasm): 0 violations — #![no_std], #![forbid(unsafe_code)] in core, zero floating-point opcodes, zero SIMD/threads/reference-types, declared memory max 1 GiB.

Benchmarks (Real FreeStyle Libre & T1D Showcase Samples)

All SHA-256 cards include the full 33-byte content-addressed header (0x00 || sha256(wasm)) plus the 2-byte SHA-256 card check (35 B envelope overhead), measured at QR Error Correction Level L using numeric-mode packing (bytesToDigits).

SampleReadingsPrecisionFHIR JSON BDEFLATE BCodec Input BSHA-256 Card BQR Version-LRatio vs FHIRRatio vs DEFLATEDecode
libre-14d4,019minute2,321,77248,7241,8601,895V321,225×25.7×~9 ms
libre-7d-exact2,016second1,172,85426,7301,5561,591V29737×16.8×~7 ms
t1d-variable-14d3,792minute2,191,89150,3541,5461,581V291,386×31.8×~8 ms
clinic-3d-compact864minute502,72011,746484519V16969×22.6×~5 ms

Showcase Samples (examples/)

  1. libre-14d.json — Real 14-day FreeStyle Libre dataset (4,019 readings, 2024-08-07 to 2024-08-21, minute grid, mean 104.9 mg/dL, GMI 5.8%, CV 13.8%, 99.2% TIR, 100.0% active wear).
  2. libre-7d-exact.json — Last 7 days of the real FreeStyle Libre dataset (2,016 readings, preserving exact 1-second timestamps and exact integer mg/dL values).
  3. t1d-variable-14d.json — Synthetic 14-day Type 1 Diabetes trace (3,792 readings across 4,032 slots, 94.0% active wear) with post-prandial spikes (220–290+ mg/dL), nocturnal hypoglycemia (<70 and <54 mg/dL), Dawn phenomenon, all 5 TIR bands non-zero (<54: 2.3%, 54–69: 1.9%, 70–180: 58.8%, 181–250: 23.1%, >250: 14.0%), GMI 7.4%, and CV 36.7%.
  4. clinic-3d-compact.json — Last 3 days of the real FreeStyle Libre dataset (864 readings, minute grid) fitting a compact Version 16 QR code.

All showcase records use fictional patient identities. Source data script: scripts/fetch-cgm.ts.

Input Model (CgmRecord)

Defined in model.d.ts and validated in model.ts:

export interface CgmRecord {
  patient?: { family?: string; given?: string[]; birthDate?: string };
  device?: { name: string; serialNumber?: string };
  utcOffset: string; // e.g. "-05:00", "+00:00" (-12:00..+14:00, no "-00:00")
  timePrecision: "minute" | "second";
  nominalIntervalMin: number; // integer 1..60 (typically 5 or 15)
  readings: {
    time: string; // strict chronological ISO timestamp matching timePrecision & utcOffset
    mgdl: number; // exact integer 20..600 mg/dL
  }[];
}

FHIR R4 Output (HL7 CGM IG)

shc_decode emits canonical minified UTF-8 JSON for a FHIR R4 Bundle (type: "collection"):

  1. Patient (id: "0", fullUrl: "resource:0")
  2. Optional Device (id: "1", fullUrl: "resource:1")
  3. 7 Deterministic HL7 CGM IG Summary Observations (computed purely in 64/128-bit integer arithmetic inside WASM — zero floats, zero QR bytes):
    • Observation/cgm-summary (cgm-summary) with effectivePeriod and hasMember references to the 6 metrics below
    • Observation/mean-glucose-mass-per-volume (LOINC 97507-8, 1-decimal mg/dL)
    • Observation/times-in-ranges (times-in-ranges, all 5 disjoint components: <54, 54–69, 70–180 + LOINC 97510-2, 181–250, >250, 1-decimal %)
    • Observation/gmi (LOINC 97506-0, Bergenstal formula 3.31 + 0.02392 * mean_mg_dl in exact 64-bit integer fixed-point math to 1 decimal %)
    • Observation/cv (LOINC 104638-2, 100 * SD / mean via shift-and-subtract isqrt_u128 to 1 decimal %)
    • Observation/days-of-wear (distinct local calendar days with readings, unit d)
    • Observation/sensor-active-percentage (readings.length / expected_slots_in_span, 1-decimal % clamped to 100.0%)
  4. Every individual glucose reading (id: "g-0", "g-1", …) as a FHIR R4 Observation with LOINC 99504-3 (Glucose [Mass/volume] in Interstitial fluid), exact effectiveDateTime, exact integer valueQuantity (mg/dL), subject: Patient/0, and optional device: Device/1.

Wire Format & Compression

  • Header: version byte (0x01), flags (timePrecision, predictor, has_patient, has_device, all_nominal_dt), nominal_interval_min, encoded utcOffset, LEB128 reading count (1..16384), initial local timestamp t0 (u32) and glucose g0 (u16), plus optional length-prefixed Patient/Device metadata.
  • Timestamps: When every step equals nominal_interval, all_nominal_dt = 1 codes timestamps in 0 bits. Otherwise, dt - nominal_step is zigzag-coded through a 2-context adaptive 4-bit binary tree (BitTree16) + bounded Exp-Golomb escape.
  • Glucose Values: The encoder evaluates both 1st-order (x[n-1]) and damped 2nd-order (x[n-1] + ((x[n-1] - x[n-2]) >> 1), clamped to 20..600, falling back to 1st-order across sensor gaps dt > 2 * nominal) and selects the strictly shorter stream (1-bit header flag). Zigzag residuals are coded with a 5-context activity-adaptive range coder keyed by |x[n-1] - x[n-2]| (0, 1, 2–3, 4–7, 8+ or gap) using count-based BitTree16 models + bounded Exp-Golomb escape.
  • Strict Canonicality: decode verifies that re-encoding the decoded record reproduces the input bytes identically, guaranteeing every dataset has exactly one valid encoding.

Building & Testing

# Build cgm-agp.wasm
bash codecs/cgm-agp/rust/build.sh

# Run full audit, round-trip, canonicality, fuzz, and benchmark suite
bun test codecs/cgm-agp/cgm-agp.test.ts