Skip to content

Improve QR code encoding efficiency #389

Closed

Description

@nothingmuch

In order to encode smaller QR codes, the following changes could be made to the BIP 21 URI and the nested pj URI. All of these ideas are breaking changes and increase spec & implementation complexity.

  1. Encode bit vomit in a format amenable to alphanumeric encoding mode of QR codes. Base45 (RFC 9285) is information theoretically optimal considered in isolation, but would preclude the next optimization, due to lack of reserved delimiter characters. and the space character potentially requiring %-escaping (note that % is included). The bech32 alphabet could be used, but I would suggest against bech32's error correction as that would negate some of the benefit and would serve no purpose for something like the ohttp parameters. Base32 is standardized, and base36 semi-standardized. Afaik no base42 standardization exists, but in principle to avoid space and reserve two delimiter characters, this would provide the most efficient encoding of the fragment. At 5.5 bits per character, with each character encoding 5 bits in e.g. base32, the overhead is 10%, whereas with 8 bits per byte, and each byte encoding 6 bits in base64, the overhead is 25%. The mode overhead of 13+16=29 bits (alphanumeric and back to bytes) is ~8% overhead for 45 bytes of base64 encoding 256 bits of entropy, so the potential savings is at least 5% per 256 bit chunk of bit vomit (pardon the mixed metaphor ;-), but could be closer to 15% if mode switching overhead is paid only once for all of the fragment encoded data covering both the receiver key and the ohttp config.

  2. Instead of encoding the fragment parameters in the same format as query parameters, with & and = as delimiters, use delimiters from the alphanumeric encoding mode character set. : seems like an intuitive substitude for =, and + could be an intuitive one for &. These can be encoded without requiring URI escaping at the BIP 21 level, saving 2 characters per byte (% encoding is itself encodable as QR alphanumeric so this is not strictly needed for eliminating the need for switching back and forth between byte and alphanumeric modes for each fragment parameter, so in either case the entire fragment can be encoded in a single alphanumeric run, but % encoding is almost as expensive as 2 mode switches, requiring 16.5 bits in alphanumeric mode as opposed to 29 bits for bytes+alnum mode switch). This would also require uppercasing the parameter names encoded in the fragment as well. A minor benefit could be obtained by also encoding the exp field as alphanumeric in this case.

  3. Matt Corrallo brought up the possibility of encoding a 128 bit symmetric encryption key in the fragment, omitting the OHTTP config and receiver public key from the BIP 21 URI, instead relying on the shared secret to derive a subdirectory ID from which these data can be retrieved. This complicates key consistency and adds a round trip but potentially reduces the bit vomit entropy by ~75%. With additional interactivity the BIP 21 entropy could be reduced even further using a PAKE.

Activity

  1. nothingmuch commented on Nov 16, 2024

    @nothingmuch
    ContributorAuthor

    For those following by email, note that I edited the original issue text for correctness and clarity, the most important correction was in regards to mode switching and its interaction with % escaping (% is in the alphanumeric set so as long as the rest of the data is alphanumeric that does not require switching modes, which item 2 previously implied).

  2. nothingmuch commented on Nov 25, 2024

    @nothingmuch
    ContributorAuthor

    Some updates on PR currently in progress:

    1. using bech32 character set with no checksum
    2. use + as param delimiter and kind of use 1 as key/value delimiter in the fragment, since a human readable part is mandatory that basically doubles as a key
    3. for the short ID, the HRP and 1 will be stripped in the path

    Assuming no intermediate path element, the entire pj= value could then be encoded in QR's alphanumeric mode:

    HTTP://UPPERCASEHOST.COM/<subdirectory_id>#OH1<otthp config>+RK1<receiver key>

    1. scheme is case insensitive
    2. :, / are in the QR alphanumeric character set
    3. DNS is case insensitive
    4. subdirectory would be base32, uppercase characters only (<..> denotes bech32 encoded binary data)
    5. # must be % encoded since it's not part of the parent bip21 URI, and % is in the QR alphanumeric character set
    6. + is also in the character set

    This also means that placing the pj parameter last is preferred, as otherwise QR encoding would have to switch back to bytes mode for the & character that would otherwise follow.

  3. added a commit that references this issue on Dec 2, 2024
    c54870d
  4. added a commit that references this issue on Dec 3, 2024
    24ed598
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions