-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathpath_test.go
284 lines (232 loc) · 8.2 KB
/
path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package sphinx
import (
"bytes"
"encoding/hex"
"encoding/json"
"os"
"testing"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/stretchr/testify/require"
)
const (
routeBlindingTestFileName = "testdata/route-blinding-test.json"
onionRouteBlindingTestFileName = "testdata/onion-route-blinding-test.json"
)
// TestBuildBlindedRoute tests BuildBlindedRoute and decryptBlindedHopData against
// the spec test vectors.
func TestBuildBlindedRoute(t *testing.T) {
t.Parallel()
// First, we'll read out the raw Json file at the target location.
jsonBytes, err := os.ReadFile(routeBlindingTestFileName)
require.NoError(t, err)
// Once we have the raw file, we'll unpack it into our
// blindingJsonTestCase struct defined below.
testCase := &blindingJsonTestCase{}
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
require.Len(t, testCase.Generate.Hops, 4)
// buildPaymentPath is a helper closure used to convert hopData objects
// into BlindedPathHop objects.
buildPaymentPath := func(h []hopData) []*HopInfo {
path := make([]*HopInfo, len(h))
for i, hop := range h {
nodeIDStr, _ := hex.DecodeString(hop.NodeID)
nodeID, _ := btcec.ParsePubKey(nodeIDStr)
payload, _ := hex.DecodeString(hop.EncodedTLVs)
path[i] = &HopInfo{
NodePub: nodeID,
PlainText: payload,
}
}
return path
}
// First, Eve will build a blinded path from Dave to herself.
eveSessKey := privKeyFromString(testCase.Generate.Hops[2].SessionKey)
eveDavePath := buildPaymentPath(testCase.Generate.Hops[2:])
pathED, err := BuildBlindedPath(eveSessKey, eveDavePath)
require.NoError(t, err)
// At this point, Eve will give her blinded path to Bob who will then
// build his own blinded route from himself to Carol. He will then
// concatenate the two paths. Note that in his TLV for Carol, Bob will
// add the `next_blinding_override` field which he will set to the
// first blinding point in Eve's blinded route. This will indicate to
// Carol that she should use this point for the next blinding key
// instead of the next blinding key that she derives.
bobCarolPath := buildPaymentPath(testCase.Generate.Hops[:2])
bobSessKey := privKeyFromString(testCase.Generate.Hops[0].SessionKey)
pathBC, err := BuildBlindedPath(bobSessKey, bobCarolPath)
require.NoError(t, err)
// Construct the concatenated path.
path := &BlindedPath{
IntroductionPoint: pathBC.Path.IntroductionPoint,
BlindingPoint: pathBC.Path.BlindingPoint,
BlindedHops: append(pathBC.Path.BlindedHops,
pathED.Path.BlindedHops...),
}
// Check that the constructed path is equal to the test vector path.
require.True(t, equalPubKeys(
testCase.Route.IntroductionNodeID, path.IntroductionPoint,
))
require.True(t, equalPubKeys(
testCase.Route.Blinding, path.BlindingPoint,
))
for i, hop := range testCase.Route.Hops {
require.True(t, equalPubKeys(
hop.BlindedNodeID, path.BlindedHops[i].BlindedNodePub,
))
data, _ := hex.DecodeString(hop.EncryptedData)
require.True(
t, bytes.Equal(data, path.BlindedHops[i].CipherText),
)
}
// Assert that each hop is able to decode the encrypted data meant for
// it.
for i, hop := range testCase.Unblind.Hops {
priv := privKeyFromString(hop.NodePrivKey)
ephem := pubKeyFromString(hop.EphemeralPubKey)
data, err := decryptBlindedHopData(
&PrivKeyECDH{PrivKey: priv}, ephem,
path.BlindedHops[i].CipherText,
)
require.NoError(t, err)
decoded, _ := hex.DecodeString(hop.DecryptedData)
require.True(t, bytes.Equal(data, decoded))
nextEphem, err := NextEphemeral(&PrivKeyECDH{priv}, ephem)
require.NoError(t, err)
require.True(t, equalPubKeys(
hop.NextEphemeralPubKey, nextEphem,
))
}
}
// TestOnionRouteBlinding tests that an onion packet can correctly be processed
// by a node in a blinded route.
func TestOnionRouteBlinding(t *testing.T) {
t.Parallel()
// First, we'll read out the raw Json file at the target location.
jsonBytes, err := os.ReadFile(onionRouteBlindingTestFileName)
require.NoError(t, err)
// Once we have the raw file, we'll unpack it into our
// blindingJsonTestCase struct defined above.
testCase := &onionBlindingJsonTestCase{}
require.NoError(t, json.Unmarshal(jsonBytes, testCase))
assoc, err := hex.DecodeString(testCase.Generate.AssocData)
require.NoError(t, err)
// Extract the original onion packet to be processed.
onion, err := hex.DecodeString(testCase.Generate.Onion)
require.NoError(t, err)
onionBytes := bytes.NewReader(onion)
onionPacket := &OnionPacket{}
require.NoError(t, onionPacket.Decode(onionBytes))
// peelOnion is a helper closure that can be used to set up a Router
// and use it to process the given onion packet.
peelOnion := func(key *btcec.PrivateKey,
blindingPoint *btcec.PublicKey) *ProcessedPacket {
r := NewRouter(
&PrivKeyECDH{PrivKey: key}, NewMemoryReplayLog(),
)
require.NoError(t, r.Start())
defer r.Stop()
res, err := r.ProcessOnionPacket(
onionPacket, assoc, 10,
WithBlindingPoint(blindingPoint),
)
require.NoError(t, err)
return res
}
hops := testCase.Decrypt.Hops
require.Len(t, hops, 5)
// There are some things that the processor of the onion packet will
// only be able to determine from the actual contents of the encrypted
// data it receives. These things include the next_blinding_point for
// the introduction point and the next_blinding_override. The decryption
// of this data is dependent on the encoding chosen by higher layers.
// The test uses TLVs. Since the extraction of this data is dependent
// on layers outside the scope of this library, we provide handle these
// cases manually for the sake of the test.
var (
introPointIndex = 2
firstBlinding = pubKeyFromString(hops[1].NextBlinding)
concatIndex = 3
blindingOverride = pubKeyFromString(hops[2].NextBlinding)
)
var blindingPoint *btcec.PublicKey
for i, hop := range testCase.Decrypt.Hops {
buff := bytes.NewBuffer(nil)
require.NoError(t, onionPacket.Encode(buff))
require.Equal(t, hop.Onion, hex.EncodeToString(buff.Bytes()))
priv := privKeyFromString(hop.NodePrivKey)
if i == introPointIndex {
blindingPoint = firstBlinding
} else if i == concatIndex {
blindingPoint = blindingOverride
}
processedPkt := peelOnion(priv, blindingPoint)
if blindingPoint != nil {
blindingPoint, err = NextEphemeral(
&PrivKeyECDH{priv}, blindingPoint,
)
require.NoError(t, err)
}
onionPacket = processedPkt.NextPacket
}
}
type onionBlindingJsonTestCase struct {
Generate generateOnionData `json:"generate"`
Decrypt decryptData `json:"decrypt"`
}
type generateOnionData struct {
SessionKey string `json:"session_key"`
AssocData string `json:"associated_data"`
Onion string `json:"onion"`
}
type decryptData struct {
Hops []decryptHops `json:"hops"`
}
type decryptHops struct {
Onion string `json:"onion"`
NodePrivKey string `json:"node_privkey"`
NextBlinding string `json:"next_blinding"`
}
type blindingJsonTestCase struct {
Generate generateData `json:"generate"`
Route routeData `json:"route"`
Unblind unblindData `json:"unblind"`
}
type routeData struct {
IntroductionNodeID string `json:"introduction_node_id"`
Blinding string `json:"blinding"`
Hops []blindedHop `json:"hops"`
}
type unblindData struct {
Hops []unblindedHop `json:"hops"`
}
type generateData struct {
Hops []hopData `json:"hops"`
}
type unblindedHop struct {
NodePrivKey string `json:"node_privkey"`
EphemeralPubKey string `json:"ephemeral_pubkey"`
DecryptedData string `json:"decrypted_data"`
NextEphemeralPubKey string `json:"next_ephemeral_pubkey"`
}
type hopData struct {
SessionKey string `json:"session_key"`
NodeID string `json:"node_id"`
EncodedTLVs string `json:"encoded_tlvs"`
}
type blindedHop struct {
BlindedNodeID string `json:"blinded_node_id"`
EncryptedData string `json:"encrypted_data"`
}
func equalPubKeys(pkStr string, pk *btcec.PublicKey) bool {
return hex.EncodeToString(pk.SerializeCompressed()) == pkStr
}
func privKeyFromString(pkStr string) *btcec.PrivateKey {
bytes, _ := hex.DecodeString(pkStr)
key, _ := btcec.PrivKeyFromBytes(bytes)
return key
}
func pubKeyFromString(pkStr string) *btcec.PublicKey {
bytes, _ := hex.DecodeString(pkStr)
key, _ := btcec.ParsePubKey(bytes)
return key
}