Repository navigation
Expand file tree
/
Copy pathPacket.lean
More file actions
302 lines (244 loc) · 11.1 KB
/
Copy pathPacket.lean
File metadata and controls
302 lines (244 loc) · 11.1 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
module
public import EthLib.Networking.Enr.V4
public import EthLib.Networking.Rlpx.Handshake
/-!
# `Networking.Discv4.Packet`
The packets of the Node Discovery Protocol v4, from "Wire Protocol" of [`discv4.md`][discv4].
[discv4]: https://github.com/ethereum/devp2p/blob/88f1c80800d622cc3a542a2125bbe33cc18c9f9c/discv4.md#wire-protocol
A packet is `hash || signature || packet-type || packet-data`, at most 1280 bytes.
The hash is the Keccak-256 hash of the bytes after it.
The signature is `r || s || v`, with the recovery id `v`.
It signs the Keccak-256 hash of `packet-type || packet-data`.
The spec writes only `sign`, and the packets of EIP-8 recover their key from that hash.
The data is an RLP list, one form for each packet type.
EIP-8 asks the reader to ignore the Ping version, extra list items and the bytes after the list.
EIP-868 adds the record packets, and the record sequence number of Ping and Pong.
EIP-8 says that its Ping of version 555 and its Pong should be accepted.
In both, the first item after the fixed fields is a list.
EIP-868 later put the sequence number at that place.
EthLib reads that item as the sequence number, so it rejects both packets.
-/
@[expose] public section
namespace EthLib.Networking.Discv4
open EthLib.Cryptography
open EthLib.Cryptography.Secp256k1
open EthLib.Serialization.Rlp
/-- The largest packet, in bytes. -/
def MAX_PACKET_SIZE : Nat := 1280
/-- The size of the hash, 32 bytes, and of the signature, 65 bytes, before the packet type. -/
def HEADER_SIZE : Nat := 97
/-- Every way a packet can be rejected. -/
inductive Discv4Error where
/-- The packet has `size` bytes, more than 1280. -/
| tooLarge (size : Nat)
/-- The packet has `size` bytes, too few for the hash, the signature and the packet type. -/
| tooShort (size : Nat)
/-- The hash is not the Keccak-256 hash of the bytes after it. -/
| hashMismatch
/-- No public key is recovered from the signature. -/
| invalidSignature
/-- Signing failed, which happens only for the private key zero. -/
| secp256k1 (error : Secp256k1Error)
/-- The packet type is not one of 1 to 6, so the packet is dropped. -/
| unknownType (type : UInt8)
/-- The data is not RLP of the shape that the packet type asks for. -/
| rlp (error : RlpError)
/--
The stable code of an error, one per constructor, for the C exports.
The code ignores the arguments of the constructor, which the message carries.
-/
def Discv4Error.code : Discv4Error → UInt32
| .tooLarge _ => 1
| .tooShort _ => 2
| .hashMismatch => 3
| .invalidSignature => 4
| .secp256k1 _ => 5
| .unknownType _ => 6
| .rlp _ => 7
/-- A node's address: `[ip, udp-port, tcp-port]`. -/
structure Endpoint where
/-- The IP address: 4 bytes for IPv4, 16 for IPv6. -/
ip : ByteArray
/-- The UDP port of discovery. -/
udpPort : UInt16
/-- The TCP port of RLPx, or 0. -/
tcpPort : UInt16
deriving DecidableEq
/-- An endpoint as the RLP list `[ip, udp-port, tcp-port]`. -/
def Endpoint.toItem (e : Endpoint) : Item :=
.list [Codec.toItem e.ip, Codec.toItem e.udpPort, Codec.toItem e.tcpPort]
/-- The endpoint in the RLP list `[ip, udp-port, tcp-port]`. -/
def Endpoint.ofItem (item : Item) : Except RlpError Endpoint := do
match ← item.fields 3 with
| [ip, u, t] => return ⟨← Codec.ofItem ip, ← Codec.ofItem u, ← Codec.ofItem t⟩
| items => throw (.wrongFieldCount 3 items.length)
instance : Codec Endpoint := ⟨Endpoint.toItem, Endpoint.ofItem⟩
/-- A node of a Neighbors packet: `[ip, udp-port, tcp-port, node-id]`. -/
structure Node where
/-- The address of the node. -/
endpoint : Endpoint
/-- The node ID: the public key as the 64 bytes `x || y`. -/
id : Vector UInt8 64
deriving DecidableEq
/-- A node as the RLP list `[ip, udp-port, tcp-port, node-id]`. -/
def Node.toItem (n : Node) : Item :=
.list [Codec.toItem n.endpoint.ip, Codec.toItem n.endpoint.udpPort,
Codec.toItem n.endpoint.tcpPort, Codec.toItem n.id]
/-- The node in the RLP list `[ip, udp-port, tcp-port, node-id]`. -/
def Node.ofItem (item : Item) : Except RlpError Node := do
match ← item.fields 4 with
| [ip, u, t, id] =>
return ⟨⟨← Codec.ofItem ip, ← Codec.ofItem u, ← Codec.ofItem t⟩, ← Codec.ofItem id⟩
| items => throw (.wrongFieldCount 4 items.length)
instance : Codec Node := ⟨Node.toItem, Node.ofItem⟩
/-- A packet, by its type. -/
inductive Packet where
/-- Ping (0x01): `[version, from, to, expiration, enr-seq]`, with an optional `enr-seq`.
The version is 4, and the reader does not check it. -/
| ping (version : Nat) (sender recipient : Endpoint) (expiration : Nat) (enrSeq : Option UInt64)
/-- Pong (0x02): `[to, ping-hash, expiration, enr-seq]`, where `enr-seq` is optional. -/
| pong (recipient : Endpoint) (pingHash : Vector UInt8 32) (expiration : Nat)
(enrSeq : Option UInt64)
/-- FindNode (0x03): `[target, expiration]`, with a 64-byte public key as the target. -/
| findNode (target : Vector UInt8 64) (expiration : Nat)
/-- Neighbors (0x04): `[nodes, expiration]`. -/
| neighbors (nodes : List Node) (expiration : Nat)
/-- ENRRequest (0x05) of EIP-868: `[expiration]`. -/
| enrRequest (expiration : Nat)
/-- ENRResponse (0x06) of EIP-868: `[request-hash, ENR]`, with the record as an RLP item. -/
| enrResponse (requestHash : Vector UInt8 32) (record : Item)
deriving DecidableEq
namespace Packet
/-- The packet type. -/
def type : Packet → UInt8
| ping .. => 0x01
| pong .. => 0x02
| findNode .. => 0x03
| neighbors .. => 0x04
| enrRequest _ => 0x05
| enrResponse .. => 0x06
/-- The item of an optional sequence number: one item when present, none when absent. -/
def enrSeqItems : Option UInt64 → List Item
| none => []
| some n => [Codec.toItem n]
/-- The items of the packet data. -/
def fields : Packet → List Item
| ping v f t e s =>
[Codec.toItem v, Codec.toItem f, Codec.toItem t, Codec.toItem e] ++ enrSeqItems s
| pong t h e s => [Codec.toItem t, Codec.toItem h, Codec.toItem e] ++ enrSeqItems s
| findNode t e => [Codec.toItem t, Codec.toItem e]
| neighbors ns e => [Codec.toItem ns, Codec.toItem e]
| enrRequest e => [Codec.toItem e]
| enrResponse h r => [Codec.toItem h, r]
/--
The optional sequence number in the items after the fixed fields.
EIP-868 makes the first of them the sequence number, so it must be an integer below `2^64`.
The items after it are ignored, as EIP-8 asks.
-/
def readEnrSeq : List Item → Except RlpError (Option UInt64)
| [] => .ok none
| item :: _ => some <$> Codec.ofItem item
/--
The packet of the type `type` whose data is the RLP list of the items `items`.
The fixed fields come first, and the reader ignores the items after them.
-/
def ofItems (type : UInt8) (items : List Item) : Except Discv4Error Packet :=
if type = 0x01 then
match items with
| v :: f :: t :: e :: rest => Except.mapError .rlp do
return ping (← Codec.ofItem v) (← Codec.ofItem f) (← Codec.ofItem t) (← Codec.ofItem e)
(← readEnrSeq rest)
| _ => .error (.rlp (.wrongFieldCount 4 items.length))
else if type = 0x02 then
match items with
| t :: h :: e :: rest => Except.mapError .rlp do
return pong (← Codec.ofItem t) (← Codec.ofItem h) (← Codec.ofItem e) (← readEnrSeq rest)
| _ => .error (.rlp (.wrongFieldCount 3 items.length))
else if type = 0x03 then
match items with
| t :: e :: _ => Except.mapError .rlp do
return findNode (← Codec.ofItem t) (← Codec.ofItem e)
| _ => .error (.rlp (.wrongFieldCount 2 items.length))
else if type = 0x04 then
match items with
| ns :: e :: _ => Except.mapError .rlp do
return neighbors (← Codec.ofItem ns) (← Codec.ofItem e)
| _ => .error (.rlp (.wrongFieldCount 2 items.length))
else if type = 0x05 then
match items with
| e :: _ => Except.mapError .rlp do return enrRequest (← Codec.ofItem e)
| _ => .error (.rlp (.wrongFieldCount 1 items.length))
else if type = 0x06 then
match items with
| h :: r :: _ => Except.mapError .rlp do return enrResponse (← Codec.ofItem h) r
| _ => .error (.rlp (.wrongFieldCount 2 items.length))
else .error (.unknownType type)
end Packet
/-- A packet that was read, with what the reader learns from its header. -/
structure Received where
/-- The packet. -/
packet : Packet
/-- The sender's public key, recovered from the signature. -/
sender : Point
/-- The hash at the start of the packet, which Pong and ENRResponse echo. -/
hash : Vector UInt8 32
/-- The Keccak-256 hash of a list of bytes. -/
def keccak (bytes : List UInt8) : Vector UInt8 32 :=
Hash.Keccak.keccak256 ⟨bytes.toArray⟩
/--
The packet that the node with the private key `key` sends.
The signature is deterministic, by RFC 6979, with the low `s` of EIP-2.
It fails when the packet would exceed 1280 bytes.
-/
def encode (key : Fn) (p : Packet) : Except Discv4Error ByteArray := do
let data ← (Serialization.Rlp.encode (.list p.fields)).mapError .rlp
let signed := p.type :: data.data.toList
let sig ← (Ecdsa.sign key (keccak signed)).mapError .secp256k1
let body := (Rlpx.signatureBytes sig).toList ++ signed
let packet := (keccak body).toList ++ body
if MAX_PACKET_SIZE < packet.length then throw (.tooLarge packet.length)
return ⟨packet.toArray⟩
/-- The signature in a packet: its bytes 32 to 96. -/
def signatureOf (bytes : List UInt8) : Vector UInt8 65 :=
Vector.ofFn fun i => bytes.getD (32 + i) 0
/--
The packet of the type `type` with the data `data`, signed by `sig`, under the hash `hash`.
It reads one RLP item from the data and ignores the bytes after it, as EIP-8 asks.
-/
def readBody (hash : Vector UInt8 32) (sig : Vector UInt8 65) (type : UInt8)
(data : List UInt8) : Except Discv4Error Received := do
let sender ← (Rlpx.recoverSigner (keccak (type :: data)) sig).mapError
fun _ => .invalidSignature
let (item, _) ← (decodeItem ⟨data.toArray⟩ 0 data.length).mapError .rlp
let p ← Packet.ofItems type (← item.toItems.mapError .rlp)
return ⟨p, sender, hash⟩
/--
The packet in the bytes `packet`, with its sender and its hash.
It fails on more than 1280 bytes, on a wrong hash and on a signature that recovers no key.
It does not check the expiration, which needs the reader's clock.
-/
def decode (packet : ByteArray) : Except Discv4Error Received :=
let bytes := packet.data.toList
if MAX_PACKET_SIZE < bytes.length then .error (.tooLarge bytes.length)
else
match bytes.drop HEADER_SIZE with
| [] => .error (.tooShort bytes.length)
| type :: data =>
if bytes.take 32 = (keccak (bytes.drop 32)).toList then
readBody (keccak (bytes.drop 32)) (signatureOf bytes) type data
else .error .hashMismatch
/--
Whether the record of an ENRResponse is signed by the sender of the packet.
The record must be a valid "v4" record, and its public key must be the sender's key.
-/
def recordSignedBy (sender : Point) (record : Item) : Bool :=
match Serialization.Rlp.encode record with
| .error _ => false
| .ok bytes =>
match Enr.decode bytes with
| .error _ => false
| .ok r =>
match r.publicKeyV4 with
| .error _ => false
| .ok key => Enr.verifyV4 r && decide (key = sender)
end EthLib.Networking.Discv4