-
Notifications
You must be signed in to change notification settings - Fork 422
Closed
Description
Bech32 logic is used for Lightning invoice decoding & encoding, and the bech32 crate is used for that.
This analysis is triggered by the upgrade from bech32 v0.9.1 to bech32 v0.11.0, during which the library API has changed significantly.
The upgrade is being handled by PR #3181 ( #3176 ) .
Here is an analysis of needed/used functionalities, and recommendations:
-
Type for 5-bit numbers
- used to be
u5type, a wrapped byte, but that's replaced byFe32, which is roughly similar, but has different focus (field element arithmetic) Fe32has some minor shortcomings (noOrdordering, no default)- Recommendation is to create own type for this. It is a simple
u8wrapper with a few conversion methods. - As this type is used in many places, rolling own implementation minimizes the dependency points to an external crate.
- used to be
-
Bech32 character encoding.
- This can be used from
Fe32 - Recommendation is to duplicate logic, as it's rather simple (2 lookup tables)
- This can be used from
-
Encoding/decoding packed 5 bit values to bytes
- this is an important operation, can be used from
bech32crate (Fe32IterExt, ByteIterExt). - it's also possible to duplicate logic, either way seems fine
- this is an important operation, can be used from
-
Checksum verification and creation
- This can be used from
bech32create - As this has some hashing operations, recommendation is to use from external (implementation can be taken over later; lightning needs only one of the hash method)
- This can be used from
-
Parsing of HRP, data part and Checksum
- Can be used from external create, or taken over, minimal logic.
-
Parsing of tagged values
- This is own implementation, as
bech32has no lightning-specific logic (and probably will not have later, not in scope).
- This is own implementation, as
Conclusion:
- Best seems to be to re-implement some logic (
u5type, packed decoding, etc.), while reusing some logic from the external crate,
but in a reduced and isolated way (checksum handling). Getting rid of the external dependency entirely is also a possibility. - Own implementation can have the API finetuned for the lightning use case, and reduce the clutter due to type and error conversions.
Options for location of bech32 logic:
- A mod within
lightningcrate (e.g.bech32.rs) - A separate create within the repo (e.g.
bech32-lightning) - The
lightning-invoicecrate is NOT a good option, aslightningcrate cannot use it, and currently there is some need there (invoice encoding for signing). - Later it could be isolated into a new repo, potentially usable by other Rust lightning implementations
Metadata
Metadata
Assignees
Labels
No labels