Repository navigation
Expand file tree
/
Copy pathMappingSlot.lean
More file actions
403 lines (335 loc) · 18.5 KB
/
Copy pathMappingSlot.lean
File metadata and controls
403 lines (335 loc) · 18.5 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import EvmYul
import Mathlib.Data.List.Nodup
import Compiler.Constants
import Compiler.Proofs.KeccakBound
import Compiler.Proofs.IRGeneration.IRStorageWord
namespace Compiler.Proofs
open Compiler.Proofs.IRGeneration (IRStorageWord IRStorageSlot)
/-!
Mapping slot abstraction used by proof interpreters.
The active backend is keccak-faithful (`solidityMappingSlot`).
## Kernel-proved range and finite accessed-footprint collision certificates
The mapping-slot definition now uses the kernel-computable `KeccakEngine.keccak256`
so that the output-length bound is structurally provable. The FFI version (`ffi.KEC`)
may optionally be registered via `@[implemented_by]` for runtime performance if
build-time benchmarks show a regression.
-/
/-- Mapping-slot backend chosen for proof semantics. -/
inductive MappingSlotBackend where
| keccak
deriving DecidableEq, Repr
/--
Active proof-model backend.
`keccak` is the active, EVM-faithful mapping-slot model.
-/
def activeMappingSlotBackend : MappingSlotBackend := .keccak
/-- Whether the active backend matches EVM keccak-derived slot layout exactly. -/
def activeMappingSlotBackendIsEvmFaithful : Bool := true
/-- ABI-encode `(key, baseSlot)` as two 32-byte words (Solidity mapping convention). -/
def abiEncodeMappingSlot (baseSlot key : Nat) : ByteArray :=
let keyWord : EvmYul.UInt256 := .ofNat key
let baseSlotWord : EvmYul.UInt256 := .ofNat baseSlot
keyWord.toByteArray ++ baseSlotWord.toByteArray
/-- FFI-based mapping slot computation (fast, used at runtime via @[implemented_by]).
Not used in proofs — proofs reason about `solidityMappingSlot` which uses the
kernel-computable Keccak. -/
private def solidityMappingSlot_ffi (baseSlot key : Nat) : Nat :=
EvmYul.fromByteArrayBigEndian (ffi.KEC (abiEncodeMappingSlot baseSlot key))
/-- Solidity mapping storage slot derivation: `keccak256(abi.encode(key, baseSlot))`.
Uses the kernel-computable Keccak engine so proofs can reason about the output
size (always 32 bytes → result < 2^256). The FFI version is registered via
`@[implemented_by]` for runtime performance. -/
@[implemented_by solidityMappingSlot_ffi]
def solidityMappingSlot (baseSlot key : Nat) : Nat :=
EvmYul.fromByteArrayBigEndian (KeccakEngine.keccak256 (abiEncodeMappingSlot baseSlot key))
/-- `EvmYul.UInt256.ofNat` reduces its argument modulo `2^256`. -/
theorem evmYul_uint256_ofNat_mod (n : Nat) :
EvmYul.UInt256.ofNat (n % Compiler.Constants.evmModulus) = EvmYul.UInt256.ofNat n := by
unfold EvmYul.UInt256.ofNat
congr 1
apply BitVec.eq_of_toNat_eq
simp [Compiler.Constants.evmModulus]
/-- The ABI encoding only sees base slot and key modulo `2^256`. -/
theorem abiEncodeMappingSlot_mod (baseSlot key : Nat) :
abiEncodeMappingSlot (baseSlot % Compiler.Constants.evmModulus)
(key % Compiler.Constants.evmModulus) =
abiEncodeMappingSlot baseSlot key := by
simp only [abiEncodeMappingSlot, evmYul_uint256_ofNat_mod]
/-- The mapping-slot derivation only sees base slot and key modulo `2^256`. -/
theorem solidityMappingSlot_mod (baseSlot key : Nat) :
solidityMappingSlot (baseSlot % Compiler.Constants.evmModulus)
(key % Compiler.Constants.evmModulus) =
solidityMappingSlot baseSlot key := by
unfold solidityMappingSlot
rw [abiEncodeMappingSlot_mod]
/-- Injectivity restricted to the concrete finite list of accessed ABI pairs.
This is an explicit hypothesis, never a claim for every finite list.
Even the full in-range ABI domain cannot inject into 256-bit slots. -/
def CollisionFreeOn (accessedPairs : List (Nat × Nat)) : Prop :=
∀ p ∈ accessedPairs, ∀ q ∈ accessedPairs,
solidityMappingSlot p.1 p.2 = solidityMappingSlot q.1 q.2 → p = q
/-- Equal hashes identify only pairs covered by this footprint's certificate. -/
theorem mappingSlot_pair_eq_of_collisionFreeOn
{accessedPairs : List (Nat × Nat)} (cert : CollisionFreeOn accessedPairs)
{base₁ key₁ base₂ key₂ : Nat}
(hp : (base₁, key₁) ∈ accessedPairs) (hq : (base₂, key₂) ∈ accessedPairs)
(h : solidityMappingSlot base₁ key₁ = solidityMappingSlot base₂ key₂) :
base₁ = base₂ ∧ key₁ = key₂ := by
exact Prod.mk.inj (cert _ hp _ hq h)
/-- Modulo equality requires a certificate covering the normalized pairs. -/
theorem mappingSlot_mod_eq_of_collisionFreeOn (base₁ key₁ base₂ key₂ : Nat)
{accessedPairs : List (Nat × Nat)} (cert : CollisionFreeOn accessedPairs)
(hp : (base₁ % Compiler.Constants.evmModulus,
key₁ % Compiler.Constants.evmModulus) ∈ accessedPairs)
(hq : (base₂ % Compiler.Constants.evmModulus,
key₂ % Compiler.Constants.evmModulus) ∈ accessedPairs)
(h : solidityMappingSlot base₁ key₁ = solidityMappingSlot base₂ key₂) :
base₁ % Compiler.Constants.evmModulus = base₂ % Compiler.Constants.evmModulus ∧
key₁ % Compiler.Constants.evmModulus = key₂ % Compiler.Constants.evmModulus := by
apply mappingSlot_pair_eq_of_collisionFreeOn cert hp hq
rw [solidityMappingSlot_mod, solidityMappingSlot_mod]
exact h
theorem solidityMappingSlot_ne {base₁ key₁ base₂ key₂ : Nat}
{accessedPairs : List (Nat × Nat)} (cert : CollisionFreeOn accessedPairs)
(hp : (base₁, key₁) ∈ accessedPairs) (hq : (base₂, key₂) ∈ accessedPairs)
(h : base₁ ≠ base₂ ∨ key₁ ≠ key₂) :
solidityMappingSlot base₁ key₁ ≠ solidityMappingSlot base₂ key₂ := by
intro heq
rcases mappingSlot_pair_eq_of_collisionFreeOn cert hp hq heq with ⟨hb, hk⟩
cases h with
| inl hbase => exact hbase hb
| inr hkey => exact hkey hk
/-- Active proof-model mapping slot encoding backend. -/
def abstractMappingSlot (baseSlot key : Nat) : Nat := solidityMappingSlot baseSlot key
/-- Active proof-model mapping slot tag sentinel (backend-specific). -/
def abstractMappingTag : Nat := 0
/-- Active proof-model mapping slot decoder backend. -/
def abstractDecodeMappingSlot (_slot : Nat) : Option (Nat × Nat) := none
/-- Active proof-model nested mapping slot helper. -/
def abstractNestedMappingSlot (baseSlot key1 key2 : Nat) : Nat :=
abstractMappingSlot (abstractMappingSlot baseSlot key1) key2
/-- Concrete storage location for a mapping value word. Offsets are normalized
to the EVM storage-key width, matching generated mapping-struct member
accesses. -/
def mappingSlotLocation (baseSlot key wordOffset : Nat) : Nat :=
(solidityMappingSlot baseSlot key + wordOffset) % Compiler.Constants.evmModulus
/-- Concrete storage location for a nested-mapping value word. -/
def nestedMappingSlotLocation (baseSlot key1 key2 wordOffset : Nat) : Nat :=
(solidityMappingSlot (solidityMappingSlot baseSlot key1) key2 + wordOffset) %
Compiler.Constants.evmModulus
/-- Finite list of mapping-derived locations touched by one generated proof port. -/
def mappingSlotLocations (baseSlot : Nat) (keysAndOffsets : List (Nat × Nat)) : List Nat :=
keysAndOffsets.map fun keyAndOffset =>
mappingSlotLocation baseSlot keyAndOffset.1 keyAndOffset.2
/-- Finite list of nested-mapping-derived locations touched by one generated proof port. -/
def nestedMappingSlotLocations
(baseSlot : Nat) (keysAndOffsets : List (Nat × Nat × Nat)) : List Nat :=
keysAndOffsets.map fun keyAndOffset =>
nestedMappingSlotLocation baseSlot keyAndOffset.1 keyAndOffset.2.1 keyAndOffset.2.2
/-- Local non-alias proposition for two concrete storage slots. -/
abbrev StorageSlotNonAlias (a b : Nat) : Prop := a ≠ b
/-- Finite distinctness certificate for the concrete storage slots a proof port
actually touches. This is the restricted replacement surface for downstream
global mapping-slot injectivity assumptions: generators may prove or emit one
certificate per finite location set, and consumers derive only the pairwise
facts they use. -/
abbrev StorageSlotsDistinct (slots : List Nat) : Prop := slots.Nodup
/-- Layout-certificate-backed non-alias evidence for one finite location set. -/
structure StorageSlotNonAliasCertificate (slots : List Nat) : Prop where
distinct : StorageSlotsDistinct slots
/-- Derived mapping-table view from flat storage. -/
def storageAsMappings (storage : IRStorageSlot → IRStorageWord) : Nat → Nat → IRStorageWord :=
fun baseSlot key => storage (IRStorageSlot.ofNat (solidityMappingSlot baseSlot key))
/-- Read a mapping entry directly from base slot and key. -/
def abstractLoadMappingEntry
(storage : IRStorageSlot → IRStorageWord)
(baseSlot key : Nat) : IRStorageWord :=
storage (IRStorageSlot.ofNat (solidityMappingSlot baseSlot key))
/-- Write a mapping entry directly from base slot and key. -/
def abstractStoreMappingEntry
(storage : IRStorageSlot → IRStorageWord)
(baseSlot key value : Nat) : IRStorageSlot → IRStorageWord :=
fun s => if s = IRStorageSlot.ofNat (solidityMappingSlot baseSlot key) then
IRStorageWord.ofNat value
else
storage s
/-- Read through the active mapping-slot backend from flat storage. -/
def abstractLoadStorageOrMapping
(storage : IRStorageSlot → IRStorageWord)
(slot : Nat) : IRStorageWord :=
storage (IRStorageSlot.ofNat slot)
/-- Write through the active mapping-slot backend to flat storage. -/
def abstractStoreStorageOrMapping
(storage : IRStorageSlot → IRStorageWord)
(slot value : Nat) : IRStorageSlot → IRStorageWord :=
fun s => if s = IRStorageSlot.ofNat slot then IRStorageWord.ofNat value else storage s
@[simp] theorem abstractMappingSlot_eq_solidity (baseSlot key : Nat) :
abstractMappingSlot baseSlot key = solidityMappingSlot baseSlot key := rfl
@[simp] theorem abstractMappingTag_eq_zero :
abstractMappingTag = 0 := rfl
@[simp] theorem abstractDecodeMappingSlot_eq_none (slot : Nat) :
abstractDecodeMappingSlot slot = none := rfl
@[simp] theorem activeMappingSlotBackend_eq_keccak :
activeMappingSlotBackend = .keccak := rfl
@[simp] theorem activeMappingSlotBackendIsEvmFaithful_eq_true :
activeMappingSlotBackendIsEvmFaithful = true := rfl
@[simp] theorem abstractNestedMappingSlot_eq_solidityNested (baseSlot key1 key2 : Nat) :
abstractNestedMappingSlot baseSlot key1 key2 =
solidityMappingSlot (solidityMappingSlot baseSlot key1) key2 := by
simp [abstractNestedMappingSlot]
theorem StorageSlotNonAliasCertificate.nonAlias_get
{slots : List Nat} (cert : StorageSlotNonAliasCertificate slots)
{i j : Nat} (hi : i < slots.length) (hj : j < slots.length)
(hne : i ≠ j) :
StorageSlotNonAlias slots[i] slots[j] := by
intro hEq
exact hne ((cert.distinct.getElem_inj_iff (i := i) (j := j) (hi := hi) (hj := hj)).mp hEq)
theorem StorageSlotNonAliasCertificate.of_distinct
{slots : List Nat} (h : StorageSlotsDistinct slots) :
StorageSlotNonAliasCertificate slots :=
⟨h⟩
theorem StorageSlotNonAliasCertificate.nonAlias_pair
{a b : Nat} (cert : StorageSlotNonAliasCertificate [a, b]) :
StorageSlotNonAlias a b := by
simpa using
(cert.nonAlias_get (i := 0) (j := 1) (hi := by simp) (hj := by simp)
(hne := by decide))
theorem mappingSlotLocations_nonAlias_get
{baseSlot : Nat} {keysAndOffsets : List (Nat × Nat)}
(cert : StorageSlotNonAliasCertificate
(mappingSlotLocations baseSlot keysAndOffsets))
{i j : Nat}
(hi : i < (mappingSlotLocations baseSlot keysAndOffsets).length)
(hj : j < (mappingSlotLocations baseSlot keysAndOffsets).length)
(hne : i ≠ j) :
StorageSlotNonAlias
(mappingSlotLocations baseSlot keysAndOffsets)[i]
(mappingSlotLocations baseSlot keysAndOffsets)[j] :=
cert.nonAlias_get hi hj hne
theorem nestedMappingSlotLocations_nonAlias_get
{baseSlot : Nat} {keysAndOffsets : List (Nat × Nat × Nat)}
(cert : StorageSlotNonAliasCertificate
(nestedMappingSlotLocations baseSlot keysAndOffsets))
{i j : Nat}
(hi : i < (nestedMappingSlotLocations baseSlot keysAndOffsets).length)
(hj : j < (nestedMappingSlotLocations baseSlot keysAndOffsets).length)
(hne : i ≠ j) :
StorageSlotNonAlias
(nestedMappingSlotLocations baseSlot keysAndOffsets)[i]
(nestedMappingSlotLocations baseSlot keysAndOffsets)[j] :=
cert.nonAlias_get hi hj hne
@[simp] theorem abstractLoadMappingEntry_eq
(storage : IRStorageSlot → IRStorageWord)
(baseSlot key : Nat) :
abstractLoadMappingEntry storage baseSlot key =
storage (IRStorageSlot.ofNat (solidityMappingSlot baseSlot key)) := rfl
@[simp] theorem abstractStoreMappingEntry_eq
(storage : IRStorageSlot → IRStorageWord)
(baseSlot key value : Nat) :
abstractStoreMappingEntry storage baseSlot key value =
(fun s => if s = IRStorageSlot.ofNat (solidityMappingSlot baseSlot key) then
IRStorageWord.ofNat value
else
storage s) := rfl
@[simp] theorem abstractLoadStorageOrMapping_eq
(storage : IRStorageSlot → IRStorageWord)
(slot : Nat) :
abstractLoadStorageOrMapping storage slot = storage (IRStorageSlot.ofNat slot) := rfl
@[simp] theorem abstractStoreStorageOrMapping_eq
(storage : IRStorageSlot → IRStorageWord)
(slot value : Nat) :
abstractStoreStorageOrMapping storage slot value =
(fun s => if s = IRStorageSlot.ofNat slot then IRStorageWord.ofNat value else storage s) := rfl
/-- Keccak256 output interpreted as a big-endian 256-bit natural is less than 2^256.
This is mathematically true because keccak produces exactly 32 bytes, so
`fromByteArrayBigEndian` gives a value < 2^(8*32) = 2^256 = evmModulus.
Previously an axiom — now a theorem thanks to the kernel-computable Keccak engine
which exposes the output length to the proof system. -/
theorem solidityMappingSlot_lt_evmModulus (baseSlot key : Nat) :
solidityMappingSlot baseSlot key < Compiler.Constants.evmModulus := by
unfold solidityMappingSlot
exact fromByteArrayBigEndian_lt_of_size _ (by
rw [KeccakEngine.keccak256_size])
@[simp] theorem mappingSlotLocation_zero (baseSlot key : Nat) :
mappingSlotLocation baseSlot key 0 = solidityMappingSlot baseSlot key := by
unfold mappingSlotLocation
exact Nat.mod_eq_of_lt (solidityMappingSlot_lt_evmModulus baseSlot key)
theorem abstractMappingSlot_lt_evmModulus (baseSlot key : Nat) :
abstractMappingSlot baseSlot key < Compiler.Constants.evmModulus :=
solidityMappingSlot_lt_evmModulus baseSlot key
theorem abstractNestedMappingSlot_injective
(base₁ key₁ key₂ base₂ key₁' key₂' : Nat)
{accessedPairs : List (Nat × Nat)} (cert : CollisionFreeOn accessedPairs)
(hp : (base₁, key₁) ∈ accessedPairs) (hq : (base₂, key₁') ∈ accessedPairs)
(hpOuter : (solidityMappingSlot base₁ key₁, key₂) ∈ accessedPairs)
(hqOuter : (solidityMappingSlot base₂ key₁', key₂') ∈ accessedPairs)
(h : abstractNestedMappingSlot base₁ key₁ key₂ =
abstractNestedMappingSlot base₂ key₁' key₂') :
base₁ = base₂ ∧ key₁ = key₁' ∧ key₂ = key₂' := by
have hinj :=
mappingSlot_pair_eq_of_collisionFreeOn cert hpOuter hqOuter (by
simpa [abstractNestedMappingSlot, abstractMappingSlot] using h)
rcases hinj with ⟨hinner, hkey2⟩
rcases mappingSlot_pair_eq_of_collisionFreeOn cert hp hq hinner
with ⟨hbase, hkey1⟩
exact ⟨hbase, hkey1, hkey2⟩
theorem abstractNestedMappingSlot_ne
{base₁ key₁ key₂ base₂ key₁' key₂' : Nat}
{accessedPairs : List (Nat × Nat)} (cert : CollisionFreeOn accessedPairs)
(hp : (base₁, key₁) ∈ accessedPairs) (hq : (base₂, key₁') ∈ accessedPairs)
(hpOuter : (solidityMappingSlot base₁ key₁, key₂) ∈ accessedPairs)
(hqOuter : (solidityMappingSlot base₂ key₁', key₂') ∈ accessedPairs)
(h : base₁ ≠ base₂ ∨ key₁ ≠ key₁' ∨ key₂ ≠ key₂') :
abstractNestedMappingSlot base₁ key₁ key₂ ≠
abstractNestedMappingSlot base₂ key₁' key₂' := by
intro heq
rcases abstractNestedMappingSlot_injective base₁ key₁ key₂ base₂ key₁' key₂'
cert hp hq hpOuter hqOuter heq
with ⟨hb, hk1, hk2⟩
rcases h with h | h | h
· exact h hb
· exact h hk1
· exact h hk2
theorem solidityMappingSlot_add_lt_evmModulus (baseSlot key wordOffset : Nat)
(h : wordOffset < Compiler.Constants.evmModulus - solidityMappingSlot baseSlot key) :
solidityMappingSlot baseSlot key + wordOffset < Compiler.Constants.evmModulus := by
omega
/-- The sum of a mapping slot and a word offset fits in 256 bits.
This holds because keccak256 output < 2^256 and word offsets are
bounded explicitly by the available headroom under `2^256`. -/
theorem solidityMappingSlot_add_wordOffset_lt_evmModulus
(baseSlot key wordOffset : Nat)
(h : wordOffset < Compiler.Constants.evmModulus - solidityMappingSlot baseSlot key) :
solidityMappingSlot baseSlot key + wordOffset < Compiler.Constants.evmModulus := by
exact solidityMappingSlot_add_lt_evmModulus baseSlot key wordOffset h
/-! ### Hashed mapping-chain layout (interpretation of `StorageKey.mapChain`)
The executable plane keeps nested / struct mapping words under the symbolic
key `Verity.StorageKey.mapChain baseSlot keys wordOffset`. Their Solidity
storage location folds `keccak256(key ‖ acc)` left-to-right over the key path
and adds the word offset modulo `2^256`. None of the facts below use
`solidityMappingSlot_injective`. -/
/-- Solidity storage location of word `wordOffset` of the value stored under
the key path `keys` of the mapping rooted at `baseSlot`. -/
def mappingChainSlotLocation (baseSlot : Nat) (keys : List Nat) (wordOffset : Nat) : Nat :=
(keys.foldl solidityMappingSlot baseSlot + wordOffset) % Compiler.Constants.evmModulus
theorem mappingChainSlotLocation_single (baseSlot key wordOffset : Nat) :
mappingChainSlotLocation baseSlot [key] wordOffset =
mappingSlotLocation baseSlot key wordOffset := rfl
theorem mappingChainSlotLocation_pair (baseSlot key1 key2 wordOffset : Nat) :
mappingChainSlotLocation baseSlot [key1, key2] wordOffset =
nestedMappingSlotLocation baseSlot key1 key2 wordOffset := rfl
theorem foldl_solidityMappingSlot_lt_evmModulus (acc : Nat) (keys : List Nat)
(h : acc < Compiler.Constants.evmModulus) :
keys.foldl solidityMappingSlot acc < Compiler.Constants.evmModulus := by
induction keys generalizing acc with
| nil => exact h
| cons key keys ih => exact ih _ (solidityMappingSlot_lt_evmModulus acc key)
/-- A non-empty key path at word offset `0` is exactly the folded keccak slot. -/
theorem mappingChainSlotLocation_zero (baseSlot key : Nat) (keys : List Nat) :
mappingChainSlotLocation baseSlot (key :: keys) 0 =
(key :: keys).foldl solidityMappingSlot baseSlot := by
unfold mappingChainSlotLocation
rw [Nat.add_zero]
exact Nat.mod_eq_of_lt
(foldl_solidityMappingSlot_lt_evmModulus _ keys (solidityMappingSlot_lt_evmModulus _ _))
end Compiler.Proofs