kuatia-types (AccountId string form), kuatia
(inflight)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?
ToBytes, serde, the SQL schema and every content
hash keep the two full i64 legs, so there is no migration.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:
i64 legs. So there is no migration, exactly as in ADR-0012.Cons:
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.Leave the form of ADR-0012 as it is.
Pros:
(i64, i64) space and needs no change.Cons:
Encode the id leg alone when sub == 0, which takes about 15 characters. Encode
both legs otherwise, which takes about 21.
Pros:
Cons:
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.
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.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.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.AccountId { id: 5, sub: 7 } renders
FK9RA6QALU15JZ7DZM81 (grouped FK9R A6QA LU15 JZ7D ZM81).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.
id and id.sub.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.