Skip to content

Bolt 1: Clarify the definition of tu fields #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2023

Conversation

nepet
Copy link
Contributor

@nepet nepet commented May 2, 2023

The sentence ..., leading zeros in integers can be omitted in the Bolt 1 fundamental type section: https://github.com/lightning/bolts/blob/50b2df24a27879e8329712c275db78876fd022fe/01-messaging.md#fundamental-types imposes that it is not necessary, but optional to truncate leading zeros in tuXX fields.

Having a look at the tuXX parsers across the ln implementations, it seems that it is expected to omit leading zeros.
This PR tries to find a better explanation/definition to avoid uncertainties.

@morehouse
Copy link
Contributor

Which implementations require leading zeros to be truncated? From a quick look at CLN and LND, they appear to work fine for untruncated integers.

@t-bast
Copy link
Collaborator

t-bast commented May 2, 2023

Eclair requires those values to be minimally-encoded, but I never realized the spec didn't make it a requirement...

I don't see any good reason to keep the redundant leading zeros, if we're using a truncated integer that's specifically because we want to remove them, right? So I'd be inclined to accept this PR to make eclair's behavior official 😄

@TheBlueMatt
Copy link
Collaborator

SGTM

Copy link
Collaborator

@t-bast t-bast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 60b77f6

@nepet
Copy link
Contributor Author

nepet commented May 3, 2023

Which implementations require leading zeros to be truncated? From a quick look at CLN and LND, they appear to work fine for untruncated integers.

Actually afaict, cln does check on leading zero bytes and fails if there are any. The following lines from fromwire_tlv_uint(const u8 **cursor, size_t *max, size_t maxlen) check that the first value read from stream that was stored in the bytes buffer in big-endian order is not zero.

	if (length > 0 && bytes[sizeof(bytes) - length] == 0) {
		SUPERVERBOSE("not minimal");
		fromwire_fail(cursor, max);
		return 0;
	}

Also, sorry for the (maybe) wrong terminology, I was in believe that the t in tuXX stands for truncated instead of the obvious tlv.

Let me know if you would like me to change the wording.

@morehouse
Copy link
Contributor

Actually afaict, cln does check on leading zero bytes and fails if there are any. The following lines from fromwire_tlv_uint(const u8 **cursor, size_t *max, size_t maxlen) check that the first value read from stream that was stored in the bytes buffer in big-endian order is not zero.

	if (length > 0 && bytes[sizeof(bytes) - length] == 0) {
		SUPERVERBOSE("not minimal");
		fromwire_fail(cursor, max);
		return 0;
	}

Ah, you're right. I completely skipped over that check.

Also, sorry for the (maybe) wrong terminology, I was in believe that the t in tuXX stands for truncated instead of the obvious tlv.

Let me know if you would like me to change the wording.

I also assumed it meant "truncated unsigned integer".

@Roasbeef
Copy link
Collaborator

Roasbeef commented May 3, 2023

ACK on the changes, could also be an opportunity to add some basic test vectors here to tighten things up.

@nepet
Copy link
Contributor Author

nepet commented May 4, 2023

If I understand the following lines from the ## Appendix B: Type-Length-Value Test Vectors section correctly, the minimum encoding is already addressed.

bolts/01-messaging.md

Lines 709 to 715 in 50b2df2

The n1 namespace supports the following TLV types:
1. `tlv_stream`: `n1`
2. types:
1. type: 1 (`tlv1`)
2. data:
* [`tu64`:`amount_msat`]

bolts/01-messaging.md

Lines 791 to 799 in 50b2df2

1. Invalid stream: 0x01 01 00
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 02 0001
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal
1. Invalid stream: 0x01 03 000100
2. Reason: encoding for `n1`s `tlv1`s `amount_msat` is not minimal

Maybe we can further clarify it by providing separate test vectors for the fundamental tuXX type. But if people already are using the given set of test vectors the cases should be covered and the rewording of the definition might be sufficient to grasp the requirements of those fields.

@nepet nepet force-pushed the 202305-leading-zeros-in-tu-fields branch from 4c527aa to 45e4cbb Compare May 4, 2023 14:24
Truncated integer fields, e.g. tu16 cannot have leading zeros
judging by the implementations of the parsers, which are used
across the different ln implementations.
The `can` imposed that it is not necessary to truncate the fields
to values of information.

Signed-off-by: Peter Neuroth <[email protected]>

Bolt 1: Add where tuXX fields may be used.

As truncated integers are not limited to TLV records with single
values (e.g. onion-routing payload type 8 - payment data) we want
to further clarify where the tuXX fields can be used.

Signed-off-by: Peter Neuroth <[email protected]>
@nepet nepet force-pushed the 202305-leading-zeros-in-tu-fields branch from 45e4cbb to bf97a79 Compare May 4, 2023 15:49
Copy link
Collaborator

@t-bast t-bast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK bf97a79

Copy link
Contributor

@morehouse morehouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK bf97a79

Copy link
Contributor

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK bf97a79

@rustyrussell
Copy link
Collaborator

Ack bf97a79

@t-bast t-bast merged commit 5635a36 into lightning:master May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants