Skip to content

Commit 9e8e29a

Browse files
authored
Complete the Fundamental Types. (lightning#778)
* Rename all the 'varint' to 'bigsize'. Having both is confusing; we chose the name bigsize, so use it explicitly. Signed-off-by: Rusty Russell <[email protected]> * BOLT 7: use `byte` instead of `u8`. `u8` isn't a type; see BOLT #1 "Fundamental Types". Signed-off-by: Rusty Russell <[email protected]> * BOLT 1: promote bigsize to a Fundamental Type. Signed-off-by: Rusty Russell <[email protected]>
1 parent 5322c2b commit 9e8e29a

File tree

4 files changed

+27
-34
lines changed

4 files changed

+27
-34
lines changed

.aspell.en.pws

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ verifier
355355
verifiers
356356
EOF
357357
monotonicity
358-
varint
359358
optimizations
360359
structs
361360
CompactSize
@@ -367,8 +366,6 @@ namespaces
367366
tlvs
368367
fips
369368
rfc
370-
varint
371-
CompactSize
372369
multipath
373370
mpp
374371
tlvs

01-messaging.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,23 @@ the backwards-compatible addition of new fields to existing message types.
110110

111111
A `tlv_record` represents a single field, encoded in the form:
112112

113-
* [`varint`: `type`]
114-
* [`varint`: `length`]
113+
* [`bigsize`: `type`]
114+
* [`bigsize`: `length`]
115115
* [`length`: `value`]
116116

117-
A `varint` is a variable-length, unsigned integer encoding using the
118-
[BigSize](#appendix-a-bigsize-test-vectors) format, which resembles the bitcoin
119-
CompactSize encoding but uses big-endian for multi-byte values rather than
120-
little-endian.
121-
122117
A `tlv_stream` is a series of (possibly zero) `tlv_record`s, represented as the
123118
concatenation of the encoded `tlv_record`s. When used to extend existing
124119
messages, a `tlv_stream` is typically placed after all currently defined fields.
125120

126-
The `type` is a varint encoded using the BigSize format. It functions as a
121+
The `type` is encoded using the BigSize format. It functions as a
127122
message-specific, 64-bit identifier for the `tlv_record` determining how the
128123
contents of `value` should be decoded. `type` identifiers below 2^16 are
129124
reserved for use in this specification. `type` identifiers greater than or equal
130125
to 2^16 are available for custom records. Any record not defined in this
131126
specification is considered a custom record. This includes experimental and
132127
application-specific messages.
133128

134-
The `length` is a varint encoded using the BigSize format signaling the size of
129+
The `length` is encoded using the BigSize format signaling the size of
135130
`value` in bytes.
136131

137132
The `value` depends entirely on the `type`, and should be encoded or decoded
@@ -192,7 +187,7 @@ things, enables the following optimizations:
192187
- variable-size fields can reserve their expected size up front, rather than
193188
appending elements sequentially and incurring double-and-copy overhead.
194189

195-
The use of a varint for `type` and `length` permits a space savings for small
190+
The use of a bigsize for `type` and `length` permits a space savings for small
196191
`type`s or short `value`s. This potentially leaves more space for application
197192
data over the wire or in an onion payload.
198193

@@ -236,6 +231,7 @@ The following convenience types are also defined:
236231
* `signature`: a 64-byte bitcoin Elliptic Curve signature
237232
* `point`: a 33-byte Elliptic Curve point (compressed encoding as per [SEC 1 standard](http://www.secg.org/sec1-v2.pdf#subsubsection.2.3.3))
238233
* `short_channel_id`: an 8 byte value identifying a channel (see [BOLT #7](07-routing-gossip.md#definition-of-short-channel-id))
234+
* `bigsize`: a variable-length, unsigned integer similar to Bitcoin's CompactSize encoding, but big-endian. Described in [BigSize](#appendix-a-bigsize-test-vectors).
239235

240236
## Setup Messages
241237

@@ -474,10 +470,10 @@ decoded with BigSize should be checked to ensure they are minimally encoded.
474470

475471
The following is an example of how to execute the BigSize decoding tests.
476472
```golang
477-
func testReadVarInt(t *testing.T, test varIntTest) {
473+
func testReadBigSize(t *testing.T, test bigSizeTest) {
478474
var buf [8]byte
479475
r := bytes.NewReader(test.Bytes)
480-
val, err := tlv.ReadVarInt(r, &buf)
476+
val, err := tlv.ReadBigSize(r, &buf)
481477
if err != nil && err.Error() != test.ExpErr {
482478
t.Fatalf("expected decoding error: %v, got: %v",
483479
test.ExpErr, err)
@@ -541,19 +537,19 @@ A correct implementation should pass against these test vectors:
541537
"name": "two byte not canonical",
542538
"value": 0,
543539
"bytes": "fd00fc",
544-
"exp_error": "decoded varint is not canonical"
540+
"exp_error": "decoded bigsize is not canonical"
545541
},
546542
{
547543
"name": "four byte not canonical",
548544
"value": 0,
549545
"bytes": "fe0000ffff",
550-
"exp_error": "decoded varint is not canonical"
546+
"exp_error": "decoded bigsize is not canonical"
551547
},
552548
{
553549
"name": "eight byte not canonical",
554550
"value": 0,
555551
"bytes": "ff00000000ffffffff",
556-
"exp_error": "decoded varint is not canonical"
552+
"exp_error": "decoded bigsize is not canonical"
557553
},
558554
{
559555
"name": "two byte short read",
@@ -604,14 +600,14 @@ A correct implementation should pass against these test vectors:
604600

605601
The following is an example of how to execute the BigSize encoding tests.
606602
```golang
607-
func testWriteVarInt(t *testing.T, test varIntTest) {
603+
func testWriteBigSize(t *testing.T, test bigSizeTest) {
608604
var (
609605
w bytes.Buffer
610606
buf [8]byte
611607
)
612-
err := tlv.WriteVarInt(&w, test.Value, &buf)
608+
err := tlv.WriteBigSize(&w, test.Value, &buf)
613609
if err != nil {
614-
t.Fatalf("unable to encode %d as varint: %v",
610+
t.Fatalf("unable to encode %d as bigsize: %v",
615611
test.Value, err)
616612
}
617613

04-onion-routing.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ There are a number of conventions adhered to throughout this document:
9595
- Each hop in the route has a variable length `hop_payload`, or a fixed-size
9696
legacy `hop_data` payload.
9797
- The legacy `hop_data` is identified by a single `0x00`-byte prefix
98-
- The variable length `hop_payload` is prefixed with a `varint` encoding
98+
- The variable length `hop_payload` is prefixed with a `bigsize` encoding
9999
the length in bytes, excluding the prefix and the trailing HMAC.
100100

101101
# Key Generation
@@ -163,7 +163,7 @@ It is 1300 bytes long and has the following structure:
163163

164164
1. type: `hop_payloads`
165165
2. data:
166-
* [`varint`:`length`]
166+
* [`bigsize`:`length`]
167167
* [`hop_payload_length`:`hop_payload`]
168168
* [`32*byte`:`hmac`]
169169
* ...
@@ -523,10 +523,10 @@ For each hop in the route, in reverse order, the sender applies the
523523
following operations:
524524

525525
- The _rho_-key and _mu_-key are generated using the hop's shared secret.
526-
- `shift_size` is defined as the length of the `hop_payload` plus the varint encoding of the length and the length of that HMAC. Thus if the payload length is `l` then the `shift_size` is `1 + l + 32` for `l < 253`, otherwise `3 + l + 32` due to the varint encoding of `l`.
526+
- `shift_size` is defined as the length of the `hop_payload` plus the bigsize encoding of the length and the length of that HMAC. Thus if the payload length is `l` then the `shift_size` is `1 + l + 32` for `l < 253`, otherwise `3 + l + 32` due to the bigsize encoding of `l`.
527527
- The `hop_payload` field is right-shifted by `shift_size` bytes, discarding the last `shift_size`
528528
bytes that exceed its 1300-byte size.
529-
- The varint-serialized length, serialized `hop_payload` and `hmac` are copied into the following `shift_size` bytes.
529+
- The bigsize-serialized length, serialized `hop_payload` and `hmac` are copied into the following `shift_size` bytes.
530530
- The _rho_-key is used to generate 1300 bytes of pseudo-random byte stream
531531
which is then applied, with `XOR`, to the `hop_payloads` field.
532532
- If this is the last hop, i.e. the first iteration, then the tail of the
@@ -662,7 +662,7 @@ The routing information is then deobfuscated, and the information about the
662662
next hop is extracted.
663663
To do so, the processing node copies the `hop_payloads` field, appends 1300 `0x00`-bytes,
664664
generates `2*1300` pseudo-random bytes (using the _rho_-key), and applies the result, using `XOR`, to the copy of the `hop_payloads`.
665-
The first few bytes correspond to the varint-encoded length `l` of the `hop_payload`, followed by `l` bytes of the resulting routing information become the `hop_payload`, and the 32 byte HMAC.
665+
The first few bytes correspond to the bigsize-encoded length `l` of the `hop_payload`, followed by `l` bytes of the resulting routing information become the `hop_payload`, and the 32 byte HMAC.
666666
The next 1300 bytes are the `hop_payloads` for the outgoing packet.
667667

668668
A special `hmac` value of 32 `0x00`-bytes indicates that the currently processing hop is the intended recipient and that the packet should not be forwarded.
@@ -1000,7 +1000,7 @@ The CLTV expiry in the HTLC is too far in the future.
10001000

10011001
1. type: PERM|22 (`invalid_onion_payload`)
10021002
2. data:
1003-
* [`varint`:`type`]
1003+
* [`bigsize`:`type`]
10041004
* [`u16`:`offset`]
10051005

10061006
The decrypted onion per-hop payload was not understood by the processing node

07-routing-gossip.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,10 @@ Nodes can signal that they support extended gossip queries with the `gossip_quer
602602
2. types:
603603
1. type: 1 (`query_flags`)
604604
2. data:
605-
* [`u8`:`encoding_type`]
605+
* [`byte`:`encoding_type`]
606606
* [`...*byte`:`encoded_query_flags`]
607607

608-
`encoded_query_flags` is an array of bitfields, one varint per bitfield, one bitfield for each `short_channel_id`. Bits have the following meaning:
608+
`encoded_query_flags` is an array of bitfields, one bigsize per bitfield, one bitfield for each `short_channel_id`. Bits have the following meaning:
609609

610610
| Bit Position | Meaning |
611611
| ------------- | ---------------------------------------- |
@@ -642,7 +642,7 @@ The sender:
642642
- SHOULD NOT send this if the channel referred to is not an unspent output.
643643
- MAY include an optional `query_flags`. If so:
644644
- MUST set `encoding_type`, as for `encoded_short_ids`.
645-
- Each query flag is a minimally-encoded varint.
645+
- Each query flag is a minimally-encoded bigsize.
646646
- MUST encode one query flag per `short_channel_id`.
647647

648648
The receiver:
@@ -663,7 +663,7 @@ The receiver:
663663
- MUST follow with any `node_announcement`s for each `channel_announcement`
664664
- otherwise:
665665
- We define `query_flag` for the Nth `short_channel_id` in
666-
`encoded_short_ids` to be the Nth varint of the decoded
666+
`encoded_short_ids` to be the Nth bigsize of the decoded
667667
`encoded_query_flags`.
668668
- if bit 0 of `query_flag` is set:
669669
- MUST reply with a `channel_announcement`
@@ -707,9 +707,9 @@ timeouts. It also causes a natural ratelimiting of queries.
707707
2. types:
708708
1. type: 1 (`query_option`)
709709
2. data:
710-
* [`varint`:`query_option_flags`]
710+
* [`bigsize`:`query_option_flags`]
711711

712-
`query_option_flags` is a bitfield represented as a minimally-encoded varint. Bits have the following meaning:
712+
`query_option_flags` is a bitfield represented as a minimally-encoded bigsize. Bits have the following meaning:
713713

714714
| Bit Position | Meaning |
715715
| ------------- | ----------------------- |
@@ -732,7 +732,7 @@ Though it is possible, it would not be very useful to ask for checksums without
732732
2. types:
733733
1. type: 1 (`timestamps_tlv`)
734734
2. data:
735-
* [`u8`:`encoding_type`]
735+
* [`byte`:`encoding_type`]
736736
* [`...*byte`:`encoded_timestamps`]
737737
1. type: 3 (`checksums_tlv`)
738738
2. data:

0 commit comments

Comments
 (0)