Skip to content

Commit 60a856a

Browse files
committed
record/routing: set minimum padding size
1 parent 398623b commit 60a856a

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

record/blinded_data.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import (
1010
"github.com/lightningnetwork/lnd/tlv"
1111
)
1212

13+
// AverageDummyHopPayloadSize is the size of a standard blinded path dummy hop
14+
// payload. In most cases, this is larger than the other payload types and so
15+
// to make sure that a sender cannot use this fact to know if a dummy hop is
16+
// present or not, we'll make sure to always pad all payloads to at least this
17+
// size.
18+
const AverageDummyHopPayloadSize = 51
19+
1320
// BlindedRouteData contains the information that is included in a blinded
1421
// route encrypted data blob that is created by the recipient to provide
1522
// forwarding information.

record/blinded_data_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func TestDummyHopBlindedDataEncoding(t *testing.T) {
196196
encoded, err := EncodeBlindedRouteData(routeData)
197197
require.NoError(t, err)
198198

199+
// Assert the size of an average dummy hop payload in case we need to
200+
// update this constant in future.
201+
require.Len(t, encoded, AverageDummyHopPayloadSize)
202+
199203
b := bytes.NewBuffer(encoded)
200204
decodedData, err := DecodeBlindedRouteData(b)
201205
require.NoError(t, err)

routing/blindedpath/blinded_path.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ func buildBlindedPaymentPath(cfg *BuildBlindedPathCfg, path *candidatePath) (
257257

258258
// Add padding to each route data instance until the encrypted data
259259
// blobs are all the same size.
260-
paymentPath, _, err := padHopInfo(hopDataSet, true)
260+
paymentPath, _, err := padHopInfo(
261+
hopDataSet, true, record.AverageDummyHopPayloadSize,
262+
)
261263
if err != nil {
262264
return nil, err
263265
}
@@ -731,9 +733,10 @@ type padStats struct {
731733
// edges. The number of iterations that this function takes is also returned for
732734
// testing purposes. If prePad is true, then zero byte padding is added to each
733735
// payload that does not yet have padding. This will save some iterations for
734-
// the majority of cases.
735-
func padHopInfo(hopInfo []*hopData, prePad bool) ([]*sphinx.HopInfo, *padStats,
736-
error) {
736+
// the majority of cases. minSize can be used to specify a minimum size that all
737+
// payloads should be.
738+
func padHopInfo(hopInfo []*hopData, prePad bool, minSize int) (
739+
[]*sphinx.HopInfo, *padStats, error) {
737740

738741
var (
739742
paymentPath = make([]*sphinx.HopInfo, len(hopInfo))
@@ -759,7 +762,7 @@ func padHopInfo(hopInfo []*hopData, prePad bool) ([]*sphinx.HopInfo, *padStats,
759762
// current largest encoded data blob size. This will be the
760763
// size we aim to get the others to match.
761764
var (
762-
maxLen int
765+
maxLen = minSize
763766
minLen = math.MaxInt8
764767
)
765768
for i, hop := range hopInfo {

routing/blindedpath/blinded_path_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ func TestPadBlindedHopInfo(t *testing.T) {
237237
// prePad is true if all the hop payloads should be pre-padded
238238
// with a zero length TLV Padding field.
239239
prePad bool
240+
241+
// minPayloadSize can be used to set the minimum number of bytes
242+
// that the resulting records should be.
243+
minPayloadSize int
240244
}{
241245
{
242246
// If there is only one entry, then no padding is
@@ -250,6 +254,15 @@ func TestPadBlindedHopInfo(t *testing.T) {
250254
// bytes.
251255
expectedFinalSize: 12,
252256
},
257+
{
258+
// Same as the above example but with a minimum final
259+
// size specified.
260+
name: "single entry with min size",
261+
expectedIterations: 2,
262+
pathIDs: []int{10},
263+
minPayloadSize: 500,
264+
expectedFinalSize: 504,
265+
},
253266
{
254267
// All the payloads are the same size from the get go
255268
// meaning that no padding is expected.
@@ -376,7 +389,7 @@ func TestPadBlindedHopInfo(t *testing.T) {
376389
}
377390

378391
hopInfo, stats, err := padHopInfo(
379-
hopDataSet, test.prePad,
392+
hopDataSet, test.prePad, test.minPayloadSize,
380393
)
381394
require.NoError(t, err)
382395
require.Equal(t, test.expectedIterations,
@@ -400,7 +413,7 @@ func TestPadBlindedHopInfo(t *testing.T) {
400413
// asserts that the resulting padded set always has the same encoded length.
401414
func TestPadBlindedHopInfoBlackBox(t *testing.T) {
402415
fn := func(data hopDataList) bool {
403-
resultList, _, err := padHopInfo(data, true)
416+
resultList, _, err := padHopInfo(data, true, 0)
404417
require.NoError(t, err)
405418

406419
// There should be a resulting sphinx.HopInfo struct for each

0 commit comments

Comments
 (0)