Repository navigation
Expand file tree
/
Copy pathAbiMemoryLayout.lean
More file actions
603 lines (548 loc) · 28.6 KB
/
Copy pathAbiMemoryLayout.lean
File metadata and controls
603 lines (548 loc) · 28.6 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
import Compiler.Proofs.AbiEncoding
import Compiler.Proofs.IRGeneration.EventObservable
import Compiler.Proofs.YulGeneration.Calldata
/-!
# ABI argument blocks in memory (#2082 slice 3)
Slices 1 and 2 (`Compiler/Proofs/AbiEncoding.lean`) specify ABI argument
blocks as *words*: `abiEncodeScalarHeads` for the static scalar case and
`abiEncodeArgs` (heads, dynamic heads carrying block-relative tail offsets,
then tails) for the dynamic case. Nothing so far connects those words to the
memory an `mstore` block actually leaves behind.
This module closes that gap. `abiBlockWrites` is the write list that lays a
word sequence out at 32-byte stride from a base pointer, and
`yulLogDataWords_abiBlockWrites` reads it back: under the no-wrap bound the
block is exactly the written words, in order. Instantiating that at the two
slice encoders gives the layout theorems
`abiEncodeScalarHeads_memory_layout` and `abiEncodeArgs_memory_layout`, and
the pointwise corollaries pin the ABI-spec facts individually — head area
size, each head word, and the dynamic tail offset carried in a dynamic head
(`abiEncodeArgs_memory_dynamic_offset_points_at_tail`).
Readback is stated with `yulLogDataWords`, the word-granular payload the
proof IR records for a `log*` instruction, so these theorems compose directly
with the emit lane in `AbiEventObservable.lean`.
-/
namespace Compiler.Proofs.AbiMemoryLayout
open Compiler
open Compiler.Yul
open Compiler.CompilationModel
open Compiler.Proofs.AbiEncoding
open Compiler.Proofs.IRGeneration
/-! ## Word-block writes -/
/-- The `mstore` write list laying `words` out consecutively, one 32-byte ABI
word per entry, starting at byte address `base`. -/
def abiBlockWrites (base : Nat) : List Nat → List (Nat × Nat)
| [] => []
| word :: rest =>
(base % Compiler.Constants.evmModulus, word) :: abiBlockWrites (base + 32) rest
@[simp] theorem abiBlockWrites_nil (base : Nat) : abiBlockWrites base [] = [] := rfl
@[simp] theorem abiBlockWrites_cons (base word : Nat) (rest : List Nat) :
abiBlockWrites base (word :: rest) =
(base % Compiler.Constants.evmModulus, word) :: abiBlockWrites (base + 32) rest := rfl
@[simp] theorem abiBlockWrites_length (base : Nat) (words : List Nat) :
(abiBlockWrites base words).length = words.length := by
induction words generalizing base with
| nil => rfl
| cons _ rest ih => simp [ih]
/-- Positional view of the write list: entry `i` targets `base + i * 32`. This
is the form produced by pointer-offset `mstore` blocks, whose store offsets are
generated by index. -/
theorem abiBlockWrites_eq_zip :
∀ (words : List Nat) (base : Nat),
abiBlockWrites base words =
((List.range words.length).map
(fun i => (base + i * 32) % Compiler.Constants.evmModulus)).zip words
| [], _ => rfl
| word :: rest, base => by
rw [abiBlockWrites_cons, abiBlockWrites_eq_zip rest (base + 32)]
simp only [List.length_cons, List.range_succ_eq_map, List.map_cons, List.map_map,
List.zip_cons_cons, Nat.zero_mul, Nat.add_zero, List.cons.injEq, true_and]
congr 1
apply List.map_congr_left
intro i _
simp only [Function.comp_apply]
congr 1
omega
/-- A block that fits below `2 ^ 256` writes only at addresses from `base`
upward, so no entry can alias an earlier one. -/
private theorem abiBlockWrites_key_ge :
∀ (words : List Nat) (base : Nat),
base + 32 * words.length ≤ Compiler.Constants.evmModulus →
∀ ow ∈ abiBlockWrites base words, base ≤ ow.1 := by
intro words
induction words with
| nil => intro base _ ow hmem; simp at hmem
| cons word rest ih =>
intro base hfit ow hmem
have hbase : base < Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
simp only [abiBlockWrites_cons, List.mem_cons] at hmem
rcases hmem with rfl | hmem
· simp [Nat.mod_eq_of_lt hbase]
· have hfit' : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
have := ih (base + 32) hfit' ow hmem
omega
/-- Later writes shadow earlier ones, so a key that no later write touches
reads back the value written at its own position. -/
theorem applyWrites_hit (k word : Nat) :
∀ (pre post : List (Nat × Nat)) (mem : Nat → Nat),
(∀ ow ∈ post, ow.1 ≠ k) →
applyWrites mem (pre ++ (k, word) :: post) k = word
| [], post, mem, hpost => by
rw [List.nil_append,
show applyWrites mem ((k, word) :: post) k =
applyWrites (fun x => if x = k then word else mem x) post k from rfl,
applyWrites_not_written k post _ hpost]
simp
| (o, v) :: rest, post, mem, hpost => by
rw [List.cons_append,
show applyWrites mem ((o, v) :: (rest ++ (k, word) :: post)) k =
applyWrites (fun x => if x = o then v else mem x)
(rest ++ (k, word) :: post) k from rfl,
applyWrites_hit k word rest post _ hpost]
/-- The first slot of a block survives the rest of the block, because every
later write lands at least 32 bytes higher. -/
theorem applyWrites_abiBlockWrites_head (word : Nat) (rest : List Nat) (base : Nat)
(mem : Nat → Nat)
(hfit : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus) :
applyWrites mem (abiBlockWrites base (word :: rest))
(base % Compiler.Constants.evmModulus) = word := by
have hbase : base < Compiler.Constants.evmModulus := by omega
have hkeys : ∀ ow ∈ abiBlockWrites (base + 32) rest,
ow.1 ≠ base % Compiler.Constants.evmModulus := by
intro ow hmem
have := abiBlockWrites_key_ge rest (base + 32) hfit ow hmem
rw [Nat.mod_eq_of_lt hbase]
omega
simpa using
applyWrites_hit (base % Compiler.Constants.evmModulus) word []
(abiBlockWrites (base + 32) rest) mem hkeys
/-- **Memory layout of a word block.** Reading `words.length` consecutive
words back from `base` returns exactly the words written, provided the block
does not wrap the 256-bit address space. -/
theorem yulLogDataWords_abiBlockWrites :
∀ (words : List Nat) (base : Nat) (mem : Nat → Nat),
base + 32 * words.length ≤ Compiler.Constants.evmModulus →
yulLogDataWords (applyWrites mem (abiBlockWrites base words)) base
(32 * words.length) = words := by
intro words
induction words with
| nil => intro base mem _; simp [yulLogDataWords]
| cons word rest ih =>
intro base mem hfit
have hfit' : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
have hcount : 32 * (word :: rest).length / 32 = rest.length + 1 := by
simp only [List.length_cons]
omega
-- memory after the head write, which the tail block extends
set shadowed : Nat → Nat :=
fun x => if x = base % Compiler.Constants.evmModulus then word else mem x with hshadow
have hsplit :
applyWrites mem (abiBlockWrites base (word :: rest)) =
applyWrites shadowed (abiBlockWrites (base + 32) rest) := rfl
have hhead := applyWrites_abiBlockWrites_head word rest base mem hfit'
have htail := ih (base + 32) shadowed hfit'
rw [yulLogDataWords, hcount, List.range_succ_eq_map]
simp only [List.map_cons, List.map_map, Nat.zero_mul, Nat.add_zero, hhead,
List.cons.injEq, true_and]
rw [yulLogDataWords,
show 32 * rest.length / 32 = rest.length from by omega] at htail
refine Eq.trans ?_ htail
apply List.map_congr_left
intro i _
show applyWrites mem (abiBlockWrites base (word :: rest))
((base + (i + 1) * 32) % Compiler.Constants.evmModulus) =
applyWrites shadowed (abiBlockWrites (base + 32) rest)
((base + 32 + i * 32) % Compiler.Constants.evmModulus)
rw [hsplit, show base + (i + 1) * 32 = base + 32 + i * 32 from by omega]
/-- Pointwise form: the word at ABI slot `index` of the block. -/
theorem applyWrites_abiBlockWrites_getElem
(base : Nat) (words : List Nat) (mem : Nat → Nat) (index : Nat)
(hfit : base + 32 * words.length ≤ Compiler.Constants.evmModulus)
(hindex : index < words.length) :
applyWrites mem (abiBlockWrites base words)
((base + index * 32) % Compiler.Constants.evmModulus) = words[index] := by
have hread := yulLogDataWords_abiBlockWrites words base mem hfit
have hcount : 32 * words.length / 32 = words.length := by omega
have := congrArg (fun ws => ws[index]?) hread
simpa [yulLogDataWords, hcount, List.getElem?_map, List.getElem?_range, hindex,
List.getElem?_eq_getElem] using this
/-! ## Physical word blocks
The compiled `mstore` block writes 32 physical bytes per word. The legacy word
view is recomputed from those bytes, so an aligned, non-wrapping block reads
back each written word reduced to the EVM word size. -/
/-- After at least one physical store the word view is the decoded byte view. -/
theorem applyStores_write_memory (state : IRState) (offset value : Nat) :
∀ (rest : List (Nat × Nat)),
(PhysicalPayloadStores.applyStores (state.writePhysicalMemoryValue offset value) rest).memory =
fun query => (DenoteMemory.wordValue
((PhysicalPayloadStores.applyStores (state.writePhysicalMemoryValue offset value)
rest).byteMemory.readWord query)).val
| [] => rfl
| (o, v) :: rest => by
simp only [PhysicalPayloadStores.applyStores]
exact applyStores_write_memory _ _ _ rest
/-- Stores of a fitting block all land at or above its base. -/
private theorem abiBlockWrites_extent_ge :
∀ (words : List Nat) (base : Nat),
base + 32 * words.length ≤ Compiler.Constants.evmModulus →
∀ ow ∈ abiBlockWrites base words,
base ≤ (Verity.Core.Uint256.ofNat ow.1).val := by
intro words
induction words with
| nil => intro base _ ow hmem; simp at hmem
| cons word rest ih =>
intro base hfit ow hmem
have hbase : base < Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
simp only [abiBlockWrites_cons, List.mem_cons] at hmem
rcases hmem with rfl | hmem
· have hkey : (Verity.Core.Uint256.ofNat (base % Compiler.Constants.evmModulus)).val = base := by
simp only [Verity.Core.Uint256.val_ofNat, Nat.mod_eq_of_lt hbase]
exact Nat.mod_eq_of_lt hbase
show base ≤ (Verity.Core.Uint256.ofNat (base % Compiler.Constants.evmModulus)).val
rw [hkey]
· have hfit' : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
have := ih (base + 32) hfit' ow hmem
omega
/-- The head word of a fitting block survives every later store of the block. -/
theorem applyStores_abiBlockWrites_head (state : IRState) (word : Nat) (rest : List Nat)
(base : Nat)
(hfit : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus) :
(PhysicalPayloadStores.applyStores state (abiBlockWrites base (word :: rest))).memory
(base % Compiler.Constants.evmModulus) = word % Compiler.Constants.evmModulus := by
have hbase : base < Compiler.Constants.evmModulus := by omega
have hkey : (Verity.Core.Uint256.ofNat base).val = base := by
simp only [Verity.Core.Uint256.val_ofNat]
exact Nat.mod_eq_of_lt hbase
rw [abiBlockWrites_cons]
simp only [PhysicalPayloadStores.applyStores]
rw [applyStores_write_memory, Nat.mod_eq_of_lt hbase]
have hword :
(PhysicalPayloadStores.applyStores
(state.writePhysicalMemoryValue (Verity.Core.Uint256.ofNat base).val word)
(abiBlockWrites (base + 32) rest)).byteMemory.readWord base =
DenoteMemory.valueWord (Verity.Core.Uint256.ofNat word) := by
funext index
have hbyte := PhysicalPayloadStores.applyStores_byte_written state
base word (abiBlockWrites (base + 32) rest) index
(fun ow hmem => by
have := abiBlockWrites_extent_ge rest (base + 32) hfit ow hmem
rw [hkey]
have := index.isLt
omega)
rw [hkey] at hbyte
simpa only [PhysicalPayloadStores.applyStores, DenoteMemory.Memory.readWord] using hbyte
beta_reduce
rw [hword, DenoteMemory.wordValue_valueWord]
simp [Verity.Core.Uint256.val_ofNat, Verity.Core.Uint256.modulus,
Compiler.Constants.evmModulus, Verity.Core.UINT256_MODULUS]
/-- **Physical memory layout of a word block.** Reading `words.length`
consecutive words back from `base` returns the written words reduced to EVM
words, provided the block does not wrap the 256-bit address space. -/
theorem yulLogDataWords_applyStores_abiBlockWrites :
∀ (words : List Nat) (base : Nat) (state : IRState),
base + 32 * words.length ≤ Compiler.Constants.evmModulus →
yulLogDataWords
(PhysicalPayloadStores.applyStores state (abiBlockWrites base words)).memory base
(32 * words.length) = words.map (· % Compiler.Constants.evmModulus) := by
intro words
induction words with
| nil => intro base state _; simp [yulLogDataWords]
| cons word rest ih =>
intro base state hfit
have hfit' : base + 32 + 32 * rest.length ≤ Compiler.Constants.evmModulus := by
simp only [List.length_cons] at hfit
omega
have hcount : 32 * (word :: rest).length / 32 = rest.length + 1 := by
simp only [List.length_cons]
omega
set shadowed := state.writePhysicalMemoryValue
(Verity.Core.Uint256.ofNat (base % Compiler.Constants.evmModulus)).val word
have hsplit :
PhysicalPayloadStores.applyStores state (abiBlockWrites base (word :: rest)) =
PhysicalPayloadStores.applyStores shadowed (abiBlockWrites (base + 32) rest) := rfl
have hhead := applyStores_abiBlockWrites_head state word rest base hfit'
have htail := ih (base + 32) shadowed hfit'
rw [yulLogDataWords, hcount, List.range_succ_eq_map]
simp only [List.map_cons, List.map_map, Nat.zero_mul, Nat.add_zero, hhead,
List.cons.injEq, true_and]
rw [yulLogDataWords,
show 32 * rest.length / 32 = rest.length from by omega] at htail
refine Eq.trans ?_ htail
apply List.map_congr_left
intro i _
show (PhysicalPayloadStores.applyStores state (abiBlockWrites base (word :: rest))).memory
((base + (i + 1) * 32) % Compiler.Constants.evmModulus) =
(PhysicalPayloadStores.applyStores shadowed (abiBlockWrites (base + 32) rest)).memory
((base + 32 + i * 32) % Compiler.Constants.evmModulus)
rw [hsplit, show base + (i + 1) * 32 = base + 32 + i * 32 from by omega]
/-- Bounded words read back exactly. -/
theorem yulLogDataWords_applyStores_abiBlockWrites_of_lt (words : List Nat) (base : Nat)
(state : IRState)
(hfit : base + 32 * words.length ≤ Compiler.Constants.evmModulus)
(hwords : ∀ word ∈ words, word < Compiler.Constants.evmModulus) :
yulLogDataWords
(PhysicalPayloadStores.applyStores state (abiBlockWrites base words)).memory base
(32 * words.length) = words := by
rw [yulLogDataWords_applyStores_abiBlockWrites words base state hfit]
conv => rhs; rw [← List.map_id words]
exact List.map_congr_left fun word hmem => Nat.mod_eq_of_lt (hwords word hmem)
/-! ## The slice-1 and slice-2 encoders in memory -/
@[simp] theorem abiEncodeScalarHeads_length (args : List (ParamType × Nat)) :
(abiEncodeScalarHeads args).length = args.length := by
simp [abiEncodeScalarHeads]
/-- **Slice-1 composition.** Writing the scalar ABI heads with `mstore` and
reading the block back yields exactly `abiEncodeScalarHeads`: the memory image
of a static argument block is the ABI head sequence, one word per argument. -/
theorem abiEncodeScalarHeads_memory_layout
(base : Nat) (args : List (ParamType × Nat)) (mem : Nat → Nat)
(hfit : base + 32 * args.length ≤ Compiler.Constants.evmModulus) :
yulLogDataWords
(applyWrites mem (abiBlockWrites base (abiEncodeScalarHeads args)))
base (32 * args.length) = abiEncodeScalarHeads args := by
have hlen : (abiEncodeScalarHeads args).length = args.length :=
abiEncodeScalarHeads_length args
have := yulLogDataWords_abiBlockWrites (abiEncodeScalarHeads args) base mem
(by rw [hlen]; exact hfit)
rwa [hlen] at this
/-- The `index`-th word of a static block is that argument's ABI head. -/
theorem abiEncodeScalarHeads_memory_head
(base : Nat) (args : List (ParamType × Nat)) (mem : Nat → Nat) (index : Nat)
(hfit : base + 32 * args.length ≤ Compiler.Constants.evmModulus)
(hindex : index < args.length) :
applyWrites mem (abiBlockWrites base (abiEncodeScalarHeads args))
((base + index * 32) % Compiler.Constants.evmModulus) =
abiEncodeScalarHead (args[index]).1 (args[index]).2 := by
have hlen : (abiEncodeScalarHeads args).length = args.length :=
abiEncodeScalarHeads_length args
have := applyWrites_abiBlockWrites_getElem base (abiEncodeScalarHeads args) mem index
(by rw [hlen]; exact hfit) (by rw [hlen]; exact hindex)
simpa [abiEncodeScalarHeads, List.getElem_map] using this
/-- **Slice-2 composition.** The full dynamic argument block — one head word
per argument followed by the tails in argument order — is exactly what a
32-byte-stride `mstore` block leaves in memory. -/
theorem abiEncodeArgs_memory_layout
(base : Nat) (args : List AbiArg) (mem : Nat → Nat)
(hfit : base + 32 * (abiEncodeArgs args).length ≤ Compiler.Constants.evmModulus) :
yulLogDataWords
(applyWrites mem (abiBlockWrites base (abiEncodeArgs args)))
base (32 * (abiEncodeArgs args).length) = abiEncodeArgs args :=
yulLogDataWords_abiBlockWrites (abiEncodeArgs args) base mem hfit
/-- The dynamic block's head area is `32 * args.length` bytes, and the words
in it are the ABI heads. -/
theorem abiEncodeArgs_memory_head
(base : Nat) (args : List AbiArg) (mem : Nat → Nat) (index : Nat)
(hfit : base + 32 * (abiEncodeArgs args).length ≤ Compiler.Constants.evmModulus)
(hindex : index < args.length) :
applyWrites mem (abiBlockWrites base (abiEncodeArgs args))
((base + index * 32) % Compiler.Constants.evmModulus) =
(abiEncodeArgHeads args (32 * args.length))[index]'
(by simpa using hindex) := by
have hheads : (abiEncodeArgHeads args (32 * args.length)).length = args.length := by
simp
have hindex' : index < (abiEncodeArgs args).length := by
simp only [abiEncodeArgs, List.length_append, hheads]
omega
have := applyWrites_abiBlockWrites_getElem base (abiEncodeArgs args) mem index hfit hindex'
rw [this]
simp only [abiEncodeArgs]
rw [List.getElem_append_left (by rw [hheads]; exact hindex)]
/-- **ABI dynamic offset, in memory.** The memory word at the head slot of a
dynamic argument is the block-relative byte offset `abiTailOffset` of that
argument's tail. -/
theorem abiEncodeArgs_memory_dynamic_offset_points_at_tail
(base : Nat) (pre : List AbiArg) (arg : AbiArg) (suffix : List AbiArg)
(mem : Nat → Nat) (hdynamic : arg.isDynamic = true)
(hfit : base + 32 * (abiEncodeArgs (pre ++ arg :: suffix)).length ≤
Compiler.Constants.evmModulus) :
applyWrites mem (abiBlockWrites base (abiEncodeArgs (pre ++ arg :: suffix)))
((base + pre.length * 32) % Compiler.Constants.evmModulus) =
abiTailOffset (pre ++ arg :: suffix) pre.length := by
have hindex : pre.length < (pre ++ arg :: suffix).length := by simp
have hhead := abiEncodeArgs_memory_head base (pre ++ arg :: suffix) mem pre.length
hfit hindex
rw [hhead]
have hoffset := abiEncodeArgHeads_dynamic_offset pre arg suffix hdynamic
have hlen : pre.length <
(abiEncodeArgHeads (pre ++ arg :: suffix)
(32 * (pre ++ arg :: suffix).length)).length := by
simp
rw [List.getElem?_eq_getElem hlen] at hoffset
exact Option.some.inj hoffset
end Compiler.Proofs.AbiMemoryLayout
/-! ## Calldatacopy memory readback (#2084)
`dynamicCopyData` (in `Verity.Core.Model.ECM`) materializes dynamic event
tail data with a `calldatacopy` from the transaction's calldata buffer.
**Floor readback** (`yulLogDataWords_calldatacopyMemory`): reading the
`⌊size/32⌋` fully-written words from `dst` returns exactly the calldata
words in order. This covers the aligned-size case but says nothing about the
final partial word when `size` is not a multiple of 32.
**Ceiling readback** (`yulLogDataWords_calldatacopyMemoryPadded`): the full
byte-granular model `calldatacopyMemoryPadded` extends the base
`calldatacopyMemory` with one ceiling word whose high `size % 32` bytes carry
the copied calldata prefix and whose low bytes are preserved from preexisting
memory (matching EVM `CALLDATACOPY` semantics). Reading the `⌈size/32⌉`
words back returns `calldatacopyWord` for each slot — the full calldata word
for interior slots and the masked word for the ceiling slot — under an
explicit fresh-memory premise (`hpad`) requiring the destination's ceiling
slot to have zero low bytes before the copy. The premise is satisfied by the
compiler's use of freshly allocated (zero-initialized) memory. This is the
form the concrete `bytes`/`string` emit lane requires, since `compileEmit`
passes the raw byte length to `calldatacopy` but the LOG reads a
ceiling-padded ABI region. -/
namespace Compiler.Proofs.CalldataMemoryLayout
open Compiler.Proofs.YulGeneration
open Compiler.Proofs.IRGeneration
/-- **Floor readback of a calldatacopy block.** Reading `size / 32` consecutive
words back from `dst` — after `calldatacopy(dst, src, size)` — returns exactly
the calldata words from `src, src + 32, …, src + (size/32 - 1) * 32`, provided
the destination block does not wrap the 256-bit address space.
See `yulLogDataWords_calldatacopyMemoryPadded` for the ceiling-word extension
that also covers the partial final word when `size` is not a multiple of 32. -/
theorem yulLogDataWords_calldatacopyMemory
(selector : Nat) (calldata : List Nat) (dst src size : Nat) (mem : Nat → Nat)
(hfit : dst + 32 * (size / 32) ≤ Compiler.Constants.evmModulus) :
yulLogDataWords (calldatacopyMemory selector calldata dst src size mem) dst
(32 * (size / 32)) =
(List.range (size / 32)).map (fun i =>
calldataloadWord selector calldata (src + i * 32)) := by
simp only [yulLogDataWords, show 32 * (size / 32) / 32 = size / 32 from by omega]
apply List.map_congr_left
intro i hi
rw [List.mem_range] at hi
have hlt : dst + i * 32 < Compiler.Constants.evmModulus := by omega
rw [Nat.mod_eq_of_lt hlt,
calldatacopyMemory_at_index selector calldata dst src size mem i hi]
/-- Pointwise form: the word at ABI slot `index` of the calldatacopy block. -/
theorem calldatacopyMemory_getWord
(selector : Nat) (calldata : List Nat) (dst src size : Nat) (mem : Nat → Nat)
(index : Nat)
(hfit : dst + 32 * (size / 32) ≤ Compiler.Constants.evmModulus)
(hindex : index < size / 32) :
calldatacopyMemory selector calldata dst src size mem
((dst + index * 32) % Compiler.Constants.evmModulus) =
calldataloadWord selector calldata (src + index * 32) := by
have hlt : dst + index * 32 < Compiler.Constants.evmModulus := by omega
rw [Nat.mod_eq_of_lt hlt,
calldatacopyMemory_at_index selector calldata dst src size mem index hindex]
/-! ### Ceiling readback -/
/-- **Ceiling readback of a calldatacopy block.** Reading `⌈size/32⌉`
consecutive words back from `dst` — using the byte-granular model
`calldatacopyMemoryPadded` — returns `calldatacopyWord` at each slot: the
full calldata word for complete interior slots and the masked word for the
final partial slot when `size % 32 ≠ 0`. The `hpad` premise requires the
preexisting memory's low bytes at the ceiling slot to be zero; this holds when
the destination is freshly allocated memory (as the compiler guarantees).
Composes with the concrete `bytes`/`string` emit lane where LOG reads a
ceiling-padded region. -/
theorem yulLogDataWords_calldatacopyMemoryPadded
(selector : Nat) (calldata : List Nat) (dst src size : Nat) (mem : Nat → Nat)
(hfit : dst + 32 * ((size + 31) / 32) ≤ Compiler.Constants.evmModulus)
(hpad : size % 32 = 0 ∨
mem (dst + (size / 32) * 32) % 2 ^ (8 * (32 - size % 32)) = 0) :
yulLogDataWords (calldatacopyMemoryPadded selector calldata dst src size mem) dst
(32 * ((size + 31) / 32)) =
(List.range ((size + 31) / 32)).map (fun i =>
calldatacopyWord selector calldata src size i) := by
simp only [yulLogDataWords,
show 32 * ((size + 31) / 32) / 32 = (size + 31) / 32 from by omega]
apply List.map_congr_left
intro i hi
rw [List.mem_range] at hi
have hlt : dst + i * 32 < Compiler.Constants.evmModulus := by omega
rw [Nat.mod_eq_of_lt hlt]
simp only [calldatacopyWord]
split
· next hfull =>
exact calldatacopyMemoryPadded_at_index selector calldata dst src size mem i hfull
· next hceil =>
have hrem : size % 32 ≠ 0 := by omega
have heq : i = size / 32 := by omega
have hmem : mem (dst + (size / 32) * 32) % 2 ^ (8 * (32 - size % 32)) = 0 :=
hpad.elim (absurd · hrem) id
rw [heq, calldatacopyMemoryPadded_at_ceil selector calldata dst src size mem hrem,
hmem, Nat.add_zero]
/-- Pointwise ceiling form: the word at ABI slot `index` of the
ceiling-padded calldatacopy block. Requires the same `hpad` fresh-memory
premise as the list-level readback. -/
theorem calldatacopyMemoryPadded_getWord
(selector : Nat) (calldata : List Nat) (dst src size : Nat) (mem : Nat → Nat)
(index : Nat)
(hfit : dst + 32 * ((size + 31) / 32) ≤ Compiler.Constants.evmModulus)
(hindex : index < (size + 31) / 32)
(hpad : size % 32 = 0 ∨
mem (dst + (size / 32) * 32) % 2 ^ (8 * (32 - size % 32)) = 0) :
calldatacopyMemoryPadded selector calldata dst src size mem
((dst + index * 32) % Compiler.Constants.evmModulus) =
calldatacopyWord selector calldata src size index := by
have hlt : dst + index * 32 < Compiler.Constants.evmModulus := by omega
rw [Nat.mod_eq_of_lt hlt]
simp only [calldatacopyWord]
split
· next hfull =>
exact calldatacopyMemoryPadded_at_index selector calldata dst src size mem index hfull
· next hceil =>
have hrem : size % 32 ≠ 0 := by omega
have heq : index = size / 32 := by omega
have hmem : mem (dst + (size / 32) * 32) % 2 ^ (8 * (32 - size % 32)) = 0 :=
hpad.elim (absurd · hrem) id
rw [heq, calldatacopyMemoryPadded_at_ceil selector calldata dst src size mem hrem,
hmem, Nat.add_zero]
/-! ### Ceiling-word regression checks
Executable checks exercising the partial-word cases flagged in the Codex P1
review (PR #2395 `discussion_r3830312802`):
* a 1-byte value (`size = 1`) produces one ceiling word with 1 high byte kept
* a 33-byte value (`size = 33`) produces two words where the second is
ceiling-masked
These run during `lake build` and fail the build if the mask is wrong.
Memory-preservation regression covering the P2 claim
(`discussion_r3831501335`): `ceilWord_regression_preserves_memory` proves
that when preexisting memory has nonzero low bytes, the corrected model's
output differs from a zero-padded result — confirming that the model no
longer silently drops the memory contribution. -/
private def regressionCalldata : List Nat := [Compiler.Constants.evmModulus - 1,
Compiler.Constants.evmModulus - 1]
/-- 1-byte calldatacopy: ceiling word keeps only the topmost byte. -/
theorem ceilWord_regression_1byte :
calldatacopyWord 0 regressionCalldata 4 1 0 =
calldataloadWord 0 regressionCalldata 4 /
2 ^ (8 * (32 - 1)) * 2 ^ (8 * (32 - 1)) := by
simp [calldatacopyWord]
/-- 33-byte calldatacopy: floor word is unchanged; ceiling word is masked. -/
theorem ceilWord_regression_33byte_floor :
calldatacopyWord 0 regressionCalldata 4 33 0 =
calldataloadWord 0 regressionCalldata 4 := by
simp [calldatacopyWord]
theorem ceilWord_regression_33byte_ceil :
calldatacopyWord 0 regressionCalldata 4 33 1 =
calldataloadWord 0 regressionCalldata 36 /
2 ^ (8 * 31) * 2 ^ (8 * 31) := by
simp [calldatacopyWord]
/-- Ceiling readback for 33 bytes yields exactly two words. -/
theorem ceilWord_regression_33byte_count :
(33 + 31) / 32 = 2 := by omega
/-- Aligned size: ceiling count = floor count. -/
theorem ceilWord_regression_aligned :
(64 + 31) / 32 = 64 / 32 := by omega
/-- Regression: ceiling word preserves (rather than zeroes) nonzero preexisting
memory low bytes, matching EVM CALLDATACOPY semantics (#2395 P2). -/
theorem ceilWord_regression_preserves_memory
(selector : Nat) (calldata : List Nat)
(dst src size : Nat) (mem : Nat → Nat)
(hrem : size % 32 ≠ 0)
(hmem : mem (dst + (size / 32) * 32) % 2 ^ (8 * (32 - size % 32)) ≠ 0) :
calldatacopyMemoryPadded selector calldata dst src size mem
(dst + (size / 32) * 32) ≠
(calldataloadWord selector calldata (src + (size / 32) * 32) /
2 ^ (8 * (32 - size % 32))) * 2 ^ (8 * (32 - size % 32)) := by
rw [calldatacopyMemoryPadded_at_ceil selector calldata dst src size mem hrem]
omega
end Compiler.Proofs.CalldataMemoryLayout