# Shorten the account code to a fixed 20 characters * Status: accepted * Authors: Cesar Rodas * Date: 2026-07-09 * Targeted modules: `kuatia-types` (`AccountId` string form), `kuatia` (`inflight`) * Associated tickets/PRs: N/A ## Context and Problem Statement ADR-0012 gave `AccountId` an IBAN-style string form. It was two mod-97 check digits at the front and a 26-character base-36 body, so 28 characters in total. The body encoded both `i64` legs at full width, through a 128-bit Feistel permutation and then 13 base-36 characters per 64-bit half. 28 characters is long. It is long to read, long to transcribe and long to speak. It also does not group evenly. So the presentation form was more awkward than an IBAN or a card number. The length comes from the encode of two full 64-bit values. So can the code be much shorter, and still keep the checksum, the obfuscation and a parse that round-trips? ## Decision Drivers * **Shorter, and evenly grouped**: the code a person sees must be short, and it must group cleanly, like an IBAN or a card number. 28 is an odd length. * **Keep the checksum**: a mistyped id must still be rejected before it reaches the store. * **Keep the obfuscation**: two ids in sequence must not produce two codes that look related. A base account and its subaccount must not share a prefix. * **No effect on storage and no effect on a content hash**: the change touches the presentation alone. `ToBytes`, serde, the SQL schema and every content hash keep the two full `i64` legs, so there is no migration. ## Considered Options #### Option 1: Fixed 20 characters, an 18-char body plus two trailing check digits (chosen) Pack the base id and the subaccount into one 93-bit value. The base id takes 63 bits, because a snowflake never sets the sign bit, and the subaccount takes 30. Permute that value, then encode it in 18 base-36 characters, then append two mod-97 check digits. `36^18 > 2^93`, so 18 characters always fit. The total is 20 characters, in five groups of four. **Pros:** * Good, because 20 characters group evenly into five blocks of four. The code then reads like an IBAN or a card number. * Good, because it keeps the mod-97 checksum and the keyed obfuscation. So the parse still rejects a mistyped id, and two codes still look unrelated. * Good, because nothing touches the storage or a content hash. Both still hold two full `i64` legs. So there is no migration, exactly as in ADR-0012. **Cons:** * Bad, because the subaccount now encodes 30 bits alone. A subaccount id must fit that range to round-trip through the string form. An inflight subaccount that comes from a hash (ADR-0014) needs a mask to 30 bits, which raises its collision domain. See the Negative Consequences. * Bad, because the string form now loses a leg that falls outside its range. An `id` past 63 bits, or a `sub` past 30 bits, truncates rather than round-trips. Every real id is inside the range, and those are a snowflake `id`, a masked inflight `sub`, and a small explicit bucket. #### Option 2: Keep 28 characters Leave the form of ADR-0012 as it is. **Pros:** * Good, because it round-trips the whole `(i64, i64)` space and needs no change. **Cons:** * Bad, because 28 characters is long and does not group evenly. That was the complaint. #### Option 3: Variable-length (main account short, subaccount long) Encode the id leg alone when `sub == 0`, which takes about 15 characters. Encode both legs otherwise, which takes about 21. **Pros:** * Good, because a main account is the common case, and it becomes very short. Neither leg loses range. **Cons:** * Bad, because a code then has one of two lengths. That reads less like a uniform account number, and it complicates a fixed-width UI and a fixed-width validation. * Bad, because 15 is not a clean multiple of four, so the groups are uneven. ## Decision Outcome Chosen option: **Option 1, a fixed 20-character code**. It is short. It groups into a uniform five by four. It keeps the checksum and the obfuscation. It touches the presentation alone, so there is no migration. The one real cost is the cap on the subaccount at 30 bits. That is ample for an explicit bucket, and acceptable for an inflight subaccount that comes from a hash. ### The encoding * The constants `ID_BITS = 63` and `SUB_BITS = 30` are public on `kuatia-types`, and together they pack 93 bits. `pack` places the low `ID_BITS` of `id` above the low `SUB_BITS` of `sub`, and `unpack` inverts it. * The obfuscation is a keyed format-preserving permutation over the 93-bit domain. It is a Feistel network over two 47-bit halves, which is a 94-bit block. A cycle walk restricts it to the domain, so it re-encrypts while the result passes `2^93 - 1`. The domain is half of `2^94`, so that averages about two iterations, and the permutation is a bijection over exactly the packable values. The seed is still the global `set_id_seed` key. * The body is 18 base-36 characters of the permuted value. The two check digits at the end use the same ISO 7064 mod-97 scheme as ADR-0012. They moved to the end, so that the code reads as the body and then the check. * `FromStr` strips a space and a dash, and upper-cases the text. It then needs 20 characters. It validates the checksum. It rejects a decoded body outside the 93-bit domain. It then inverts the permutation and unpacks. * Under the default seed, `AccountId { id: 5, sub: 7 }` renders `FK9RA6QALU15JZ7DZM81` (grouped `FK9R A6QA LU15 JZ7D ZM81`). ### Inflight subaccounts The per-destination inflight hold subaccount (ADR-0014) was a 63-bit truncation of a trade hash. A mask now takes it to the low `SUB_BITS`, so every hold has a code the encoder accepts. It stays deterministic, and it stays specific to its trade. ### Positive Consequences * The code a person sees drops from 28 characters to 20, and it groups evenly. * Nothing changes in the storage, in serde or in a content hash, so there is no migration. The debug output is the same, as `id` and `id.sub`. ### Negative Consequences * The subaccount space is 30 bits, which is about 1.07 billion, rather than a full `i64`. An explicit bucket is unaffected. An inflight subaccount that comes from a hash collides sooner. At 30 bits the birthday bound is about a 1 per cent chance at around 4,600 concurrent inflight trades to one destination. A deployment with a very high concurrent inflight fan-in to one destination must account for this. * The string form loses a leg outside the range the encoder accepts. Such a value still hashes, still persists and still compares correctly, and it does not round-trip through the code. No real source of an id produces a leg outside the range. ## Links * Supersedes the IBAN-style account code section of [ADR-0012](0012-subaccounts.md). The rest of ADR-0012 stands, and that is the identity model, the reads and the balances. * Constrains the inflight hold subaccount of [ADR-0014](0014-inflight-holds-via-holding-accounts.md).