Implementation Guide · BitPads Protocol v2.0 / BitLedger v3.0
Mission
Manual
You have read the specs. Now build the encoder or decoder. This guide walks every algorithm step by step with real values throughout.
Every BitPads implementation starts at the same byte: Meta byte 1. Two paths diverge from there depending on whether you are producing frames or consuming them.
Assemble BitPads frames from application data. Start at the Meta byte, determine mode, populate layers from innermost to outermost, compute CRC-15 last. Output is a byte sequence conforming to the wire format.
Sections: Value Encoding → Layer 1 → Layer 2 → Layer 3
Parse incoming BitPads bytes into structured records. Read the Meta byte first, branch on mode, traverse the fixed component sequence, validate cross-layer rules after each record.
Sections: Meta Byte → State Machine → Layer 3 Validation
The Meta byte is the universal entry point. Both encoder and decoder must implement the full Meta byte interpretation table. All fields are positional — no schema lookup, no field identifiers. Know the position and you know the meaning.
Spec refs: BitPads v2 · BitLedger v3
Bit numbering convention. The spec numbers bits 1–8 from MSB (most significant) to LSB. Bit 1 is the high bit of each byte. Confirm your byte-manipulation code matches this convention before encoding any field. Reversal here corrupts every frame silently. See Common Pitfalls.
Entry Point — All Parsing Starts Here
Meta Byte Decoder
Every BitPads frame begins with Meta byte 1. Read exactly one byte. This byte fully determines what follows before you read a single additional bit. The first branch is on bit 1 — Wave or Record mode.
Bit Layout — Meta Byte 1
SysCtx
Ignored
Full Decision Tree
Wave Category Table — bits 5-8 (Role B, bit4=1)
| Code | Category | Layer 1? | Structure |
|---|---|---|---|
0000 | Plain Value | Session est. | Setup byte (opt.) + Value block at tier |
0001 | Simple Message | Session est. | 1-byte length prefix + content bytes |
0010 | Status / Log | Session est. | Status code or length + log content |
0011 | Command / Request | Session est. | Task short form (1 byte) + optional ext |
0100 | Basic Record | Required | Layer 1 + opt. Layer 2 + value + opt. time |
0101 | Transaction + Message | Required | Layer 1 + Layer 2 + BL ctrl + value + note |
0110 | Rich Log Entry | Required | Layer 1 + all four components |
0111 | Priority Alert | Required | Layer 1 + value + task, priority override |
1000 | Text Stream | Session | Stream-length prefix + UTF-8 bytes |
1001 | Flag / Archetype Stream | Session | Stream-Open ctrl + length + packed flags |
1010 | Variable Field Stream | Session | Stream-Open + length-prefixed fields |
1011 | Binary Blob | Session | Length prefix + raw binary |
1100 | Compact Command | No | Enhancement grammar — upper 3=class, lower 5=code |
1101 | Context Declaration | No | Context Declaration Block, 1–4 bytes |
1110 | Telegraph Emulation | Session | Full C0 Enhancement Grammar stream |
1111 | Extended Category | Depends | Next byte = 8-bit extended category code |
Wave category 1100/1101/1110 engage the Enhancement Sub-Protocol. See Enhancement Grammar for the full C0 byte structure. In these categories every content byte carries 3 flag bits (P/A/C) in its upper 3 bits and 5 content bits in its lower 5.
Algorithm
Value Encoding Algorithm
All values encode as whole integers using the formula N = A × 2S + r. No floating point anywhere. Every integer from 0 to 33,554,431 is reachable without gaps. Rounding, when it occurs, is always explicit — two flag bits declare direction.
A = upper (25−S) bits = floor(N / 2S)
S = Optimal Split from Layer 2 (default 8)
r = lower S bits = N mod 2S
Encoder Algorithm — Step by Step
- Determine the target real value
V(e.g. $4.53). ObtainSF(Scaling Factor) andD(Decimal Position) from Layer 2 batch context. - Compute raw stored integer:
R = V × 10D / SF. Example: V=4.53, SF=1, D=2 → R = 4.53 × 100 / 1 = 453. - Check if
Ris a whole integer. If yes: setN = R, bit 26 = 0, bit 27 = 0 (exact). Skip to step 6. - Rounding required. Choose direction by account type: Liability/Tax → round up (ceiling). Asset/Income → round down (floor). General → round to nearest. Set
N = floor(R)orceil(R). Set bit 26 = 1. Set bit 27 = 0 (down) or 1 (up). - Range check: if
N > 33,554,431, increase SF to next power of 10 and return to step 2. - Obtain Optimal Split
Sfrom Layer 2 (default S=8). Compute:A = floor(N / 2S),r = N mod 2S. Pack A into upper bits, r into lower bits of value block.
Worked Example — $4.53 at SF×1, D=2, S=8
Value Tier Reference
| Tier | Setup bits 1-2 | Block bits | Max N | Max at SF×1, D=2 | When to use |
|---|---|---|---|---|---|
| T1 | 00 |
8 bits | 255 | $2.55 | Status codes, deep space IoT, single-byte values |
| T2 | 01 |
16 bits | 65,535 | $655.35 | Small sensor values, local measurements |
| T3 | 10 (DEFAULT) |
24 bits | 16,777,215 | $167,772.15 | General purpose — default when no Setup byte present |
| T4 | 11 |
32 bits | 4,294,967,295 | $42,949,672.95 | Large assets, high-volume physical quantities |
Session Header
Layer 1 — 8 Bytes
Layer 1 is transmitted once at session open. It carries sender identity, domain, permissions, session defaults, and a CRC-15 checksum over bits 1–49. CRC is computed AFTER all other fields are populated.
CRC-15 Encoding Algorithm
The CRC-15 covers bits 1–49 (the 49-bit session payload). Generator polynomial: G = x15 + x + 1 = 0x8003. At the encoder side, compute the remainder, then embed in bits 50–64.
Worked Example — Layer 1 Construction
v2.0 change: Layer 1 bit 12 is now the Session Enhancement Flag (was Opposing Convention in v1). When bit 12 = 1, a Session Configuration Extension byte follows Layer 1 before any content. It carries Nesting Level Code, Opposing Convention, Compound Mode Active, and BL Block Optional flags.
Session Config Extension Byte (when bit 12 = 1)
| Bits | Field | Values | Meaning |
|---|---|---|---|
1-2 | Nesting Level Code | 00–11 | 00=Flat only. 01=Depth 2. 10=Depth 4. 11=Extended (ext byte follows). |
3 | Opposing Convention | 0/1 | 0=Opposing inferred. 1=Always transmitted explicitly in extension byte. |
4 | Compound Mode Active | 0/1 | 0=Off. 1=1111 continuation markers valid this session. |
5 | BL Block Optional | 0/1 | 0=Accounting block always present. 1=Optional per record. |
6-8 | Reserved | 111 | Transmit as 1. |
Batch Header
Layer 2 — 6 Bytes (48 bits)
Layer 2 establishes the decoding context for an entire batch. Every record in the batch inherits SF, D, and currency without re-stating them. Transmitted once before the first record in each batch.
Encoding Example — USD, D=2, SF×1, Batch 1
Rounding Balance — Sign-Magnitude Encoding
Bits 42-45 track net rounding for the batch. High bit = sign (0=rounded up, 1=rounded down). Lower 3 bits = magnitude in minimum precision units. 0000 = exactly balanced. 1000 = escape — magnitude exceeds 7, see batch-close control record.
The precision unit is always SF / 10D. At SF×1, D=2: precision unit = $0.01.
Transaction Record
Layer 3 — The 40-Bit Record
Five bytes. Both sides of a double-entry transaction. Three independent error detection mechanisms operate on every record without a separate checksum field.
Cross-Layer Validation Rules
These rules provide two of the three independent error detection signals on every record. Any violation means the record is malformed — reject before posting.
Conservation Invariant Check
Run after completing each batch (when batch-end flag is set). The sum of all signed flows must equal zero. A batch that fails this check is invalid — it may contain phantom flows, missing records, or duplicates.
Full BitLedger Frame Example — $100.00 USD Debit, Account Pair 0001
Parser
Decoder State Machine
The decoder is a deterministic state machine. State transitions are driven exclusively by the bytes read and the flags previously set. No lookahead. No ambiguity. Each state reads a fixed number of bytes or derives the count from previously read flags.
Enhancement signal slots. When Layer 1 bit 12 = 1 (Enhancement active), the state machine inserts C0 Enhancement reads at each declared signal slot position. P4 = before Value block, P5 = after Value block, P6 = after Time field, P7 = after Task block, P8 = after Note block. Session-layer slots P1-P3 appear at session open. Batch-layer slots P9-P11 appear at batch open/close. See Enhancement Grammar for full slot map.
C0 Enhancement Grammar
Enhancement Decoder
When Layer 1 bit 12 = 1 and the Signal Slot Presence byte declares one or more slots active, the decoder encounters enhanced C0 bytes at the declared positions. Each enhanced C0 byte carries a 3-bit flag matrix in its upper 3 bits and a 5-bit C0 code identity in its lower 5 bits.
Enhanced C0 Byte Structure
Enhancement Decode Algorithm
- Determine the byte is at a declared signal slot position (P4-P8 per Signal Slot Presence byte, or P1-P3/P9-P13 for session/batch/stream positions when bit12=1).
- Read the byte. Extract upper 3 bits: P (bit1) = Priority flag, A (bit2) = Acknowledge Request, C (bit3) = Continuation flag.
- Extract lower 5 bits (bits 4-8): this is the C0 code identity (0-31). Look up in the agreed control table. 29 unconditional codes always map to the same controls. 4 conditional codes are context-dependent.
- If P=1: process this signal at elevated priority before lower-priority pending items. If A=1: queue an ACK signal to be sent at the corresponding response slot.
- Route to the appropriate handler for the decoded C0 control (e.g. SOH, STX, ETX, BEL, SO, SI, EM, SUB, FS, GS, RS, US, etc.).
- If C=1 (Continuation flag set): read the next byte as another enhanced C0 signal at the same slot position. Repeat from step 2. If C=0: slot consumption complete, resume normal frame parsing.
Implementation Warnings
Common Pitfalls
The following mistakes recur in first-time implementations. Each has a single correct resolution. Click any row to expand.
| Pitfall | Symptom | |
|---|---|---|
| CRITICALBit ordering reversed — MSB/LSB confusion ▾ | All frames decode incorrectly. Mode always wrong. | |
The ProblemThe spec numbers bits 1–8 from MSB (most significant) to LSB (least significant). Bit 1 is the high bit. Many implementors default to LSB-first numbering, which silently reverses every field. The FixPure Signal |
||
| CRITICALCRC-15 computed over wrong byte range ▾ | All sessions rejected at receiver. CRC never validates. | |
The ProblemCRC-15 covers the 49-bit session payload (bits 1–49 of Layer 1). It does NOT cover Layer 2 or Layer 3 independently. A common mistake is computing CRC over the full frame or over only the first byte. The FixCompute CRC-15 over exactly bits 1–49 of Layer 1 (SOH through Sub-Entity ID). Embed the 15-bit remainder at bits 50–64. Decoder: run the same CRC over all 64 bits — a correct frame produces remainder 0x0000. |
||
| HIGHCompound mode without session permission ▾ | 1111 account pair triggers decode errors. Records corrupted. | |
The ProblemAccount pair 1111 is the compound continuation marker. You cannot use it unless Compound Mode Active is set in the Session Config Extension byte (bit 4) AND the Layer 2 Compound Prefix is non-00 for the current batch. Both must be active. The FixAt session open, set Session Enhancement Flag (bit 12 = 1) and include Session Config Extension with bit 4 = 1. At batch open, set Compound Prefix to 01, 10, or 11 as appropriate. Only then is 1111 valid in Layer 3 records. |
||
| HIGHConservation check run after each record ▾ | All multi-record batches flagged as invalid. False positives. | |
The ProblemThe conservation invariant (sum of all signed flows = 0) is a batch-level invariant, not a per-record invariant. Checking it after each record will always fail for batches with more than one record. The FixAccumulate the running batch balance across all records. Run the conservation check exactly once — at batch close (batch-end flag from control record type 010 or batch record count met). Only then check |
||
| MEDIUMRounding balance in Layer 2 not carried across records ▾ | Batches with rounded values fail conservation check despite correct encoding. | |
The ProblemLayer 2 bits 42-45 encode the net rounding balance for the batch in sign-magnitude form. When records are rounded, the accumulated rounding delta is declared here. A decoder that ignores this field will see a non-zero batch balance even for a correctly encoded rounded batch. The FixDecode the 4-bit rounding balance (high bit = sign: 0=rounded up, 1=rounded down; lower 3 bits = magnitude in precision units). Add this to the batch balance before the conservation check. Code 1000 = escape — read the full value from the batch-close control record payload. |
||
| MEDIUMSignal Slot Presence byte assumed always present ▾ | Decoder reads Layer 1 one byte late. All subsequent fields corrupted. | |
The ProblemThe Signal Slot Presence byte (Meta byte 2 bit 8) is CONDITIONAL. It only appears when Meta byte 2 bit 8 = 1. The most common case is bit 8 = 0, in which case the byte is absent and the sequence proceeds directly from Meta byte 2 to Layer 1. The FixAlways read Meta byte 2 bit 8 first. If bit 8 = 0: proceed directly to Layer 1. If bit 8 = 1: read exactly one Signal Slot Presence byte, decode P4-P8 flags (bits 1-5), then proceed to Layer 1. Never assume the byte is present. |
||
| LOWWave mode vs Record mode misidentified ▾ | Frames parsed in wrong mode. Garbled output for all frame types. | |
The ProblemMode is determined by bit 1 of Meta byte 1 only. No other byte, no preamble, no frame length prefix indicates mode. Do not infer mode from byte count, context, or any other field. The FixRead bit 1 of the first byte. 0 = Wave mode. 1 = Record mode. Apply this branch unconditionally before reading any other field. Do not cache mode across frames — read it fresh from every new Meta byte 1. |
||
Verification
Test Vectors
Known-good byte sequences for implementation verification. An encoder producing these exact byte sequences, and a decoder consuming them and producing the stated interpretation, is conformant for the covered cases.
Vector 1 — Pure Signal (1 byte)
Vector 2 — Minimal Wave (4 bytes — anonymous value $4.53)
Vector 3 — Minimal Record with Layer 1 (13 bytes)
Vector 4 — Full BitLedger Frame (22 bytes — $100.00 USD Debit, account pair 0001)
Validating your implementation. A correct encoder produces the exact hex sequences above for the stated inputs. A correct decoder consumes them and produces the stated interpretation. If your byte sequence differs, check: (1) bit ordering — MSB first, (2) CRC-15 polynomial 0x8003 over bits 1-49, (3) value formula N=A×2S+r with default S=8, (4) cross-layer bit mirroring (bit29=bit37, bit30=bit38).