std:EVM/State/Layout/Address
lib/EVM/State/Layout/Address.omg
lib/EVM/State/Layout/Address.omg207 lines
1{- Slot and bit formulas of storage format version 2, over exact naturals,
2 and the measures of a description that validity and derivation share.
3 `std:EVM/State/Layout/Laws` proves what the formulas satisfy. The access
4 code's Haskell copies of `confined`, `place`, `slotsFor`, `directBits`,
5 `directSlot` and `checkedAdd` are tested against their normal forms on
6 generated naturals.
7-}
8let D = std:EVM/State/Layout/Description
9let L = std:List
10let N = std:Natural
11let O = std:Optional
12
13-- The slots that hold `bits` bits.
14let slotsOf : Natural -> Natural = \bits -> Natural/div (bits + 255) 256
15
16-- The first slot boundary at or after a bit offset.
17let alignUp : Natural -> Natural = \bits -> 256 * slotsOf bits
18
19-- The relative slot of word `offset` of the object whose preimage hashes to
20-- `hash`, in the confined block [start, start + 2^bits).
21let confined
22 : Natural -> Natural -> Natural -> Natural -> Natural
23 = \start bits hash offset -> start + Natural/mod (hash + offset) (N.pow2 bits)
24
25-- The bit offset of entry `i` at stride `s` from the slot-aligned start of
26-- a list's data or a direct array. An entry of at most 256 bits never
27-- crosses a slot; a wider stride is a multiple of 256 and covers whole slots.
28let place
29 : Natural -> Natural -> Natural
30 = \s i ->
31 if N.isZero s
32 then 0
33 else if N.lessThanEqual s 256
34 then let e = Natural/div 256 s in 256 * Natural/div i e + Natural/mod i e * s
35 else i * s
36
37-- The slots of `n` entries at stride `s`.
38let slotsFor
39 : Natural -> Natural -> Natural
40 = \n s ->
41 if N.isZero s
42 then 0
43 else if N.lessThanEqual s 256
44 then let e = Natural/div 256 s in Natural/div (n + N.minus e 1) e
45 else Natural/div (n * s) 256
46
47-- The inline bits of a direct mapping whose key has `width` bits and whose
48-- entries have stride `s`: an entry for every key encoding, from a slot
49-- boundary.
50let directBits
51 : Natural -> Natural -> Natural
52 = \width s -> 256 * slotsFor (N.pow2 width) s
53
54-- The relative slot of the entry for the key encoding `n` of a direct
55-- mapping at stride `s` whose array starts at relative slot `start`; the
56-- entry starts `place s n mod 256` bits up in it.
57let directSlot
58 : Natural -> Natural -> Natural -> Natural
59 = \start s n -> start + Natural/div (place s n) 256
60
61-- `count + added`, if it stays below 2^limitBits: the checked length of a
62-- list or a blob after an append.
63let checkedAdd
64 : Natural -> Natural -> Natural -> Optional Natural
65 = \limitBits count added ->
66 if N.lessThan (count + added) (N.pow2 limitBits)
67 then Some (count + added)
68 else None Natural
69
70-- The `width` bits at bit `offset` of `object`.
71let field
72 : Natural -> Natural -> Natural -> Natural
73 = \offset width object -> Natural/mod (Natural/div object (N.pow2 offset)) (N.pow2 width)
74
75-- `object` with the `width` bits at bit `offset` replaced by `value`,
76-- reduced modulo 2^width.
77let setField
78 : Natural -> Natural -> Natural -> Natural -> Natural
79 = \offset width object value ->
80 let low = N.pow2 offset
81 let high = N.pow2 (offset + width)
82 in Natural/mod object low
83 + Natural/mod value (N.pow2 width) * low
84 + Natural/div object high * high
85
86-- The largest data object of a list at stride `s`: 2^lengthBits - 1
87-- elements.
88let listSlots
89 : Natural -> Natural
90 = \stride -> slotsFor (N.minus (N.pow2 D.lengthBits) 1) stride
91
92-- The largest data object of a blob: fewer than 2^blobBytesBits bytes, or
93-- as many 256-bit limbs of a number.
94let blobSlots : Natural = N.pow2 (N.minus D.blobBytesBits 5)
95
96-- The bits that address every word of an object of `slots` slots, that is
97-- ceil(log2 slots); 0 for an empty object.
98let extentBits
99 : Natural -> Natural
100 = \slots -> if N.isZero slots then 0 else N.bitLength (N.minus slots 1)
101
102-- The node with an identifier.
103let find
104 : List D.Node -> Natural -> Optional D.Node
105 = \nodes id -> L.find (\(node : D.Node) -> N.equal node.id id) nodes
106
107-- The inline bits of a node, 0 for an unknown identifier.
108let width
109 : List D.Node -> Natural -> Natural
110 = \nodes id -> O.fold (\(node : D.Node) -> node.bits) 0 (find nodes id)
111
112-- The slots of the largest hashed object a node owns, 0 if it owns none.
113let ownSlots
114 : List D.Node -> D.Form -> Natural
115 = \nodes form ->
116 merge
117 { Primitive = \(_ : D.Scalar) -> 0
118 , Blob = \(_ : D.Scalar) -> blobSlots
119 , Fields = \(_ : List D.Member) -> 0
120 , Choice =
121 \(_ : { width : Natural, payload : Natural, alternatives : List D.Alternative }) -> 0
122 , List = \(list : { element : Natural, stride : Natural }) -> listSlots list.stride
123 , Dictionary =
124 \(entry : { key : Natural, value : Natural, addressing : D.Addressing }) ->
125 merge
126 { Hashed = slotsOf (width nodes entry.value), Direct = \(_ : Natural) -> 0 }
127 entry.addressing
128 , Unsupported = \(_ : Text) -> 0
129 }
130 form
131
132-- The slots of the root object.
133let rootSlots
134 : D.Description -> Natural
135 = \description -> slotsOf (width description.nodes description.root)
136
137-- The slots of the largest hashed object of any node, 0 if there is none.
138-- Like every local clause of validity, it counts unreachable nodes too.
139let objectSlots
140 : List D.Node -> Natural
141 = \nodes ->
142 L.fold
143 (\(node : D.Node) (largest : Natural) -> N.max (ownSlots nodes node.form) largest)
144 0
145 nodes
146
147let require
148 : Bool -> Text -> List Text
149 = \condition message -> if condition then ([] : List Text) else [ message ]
150
151-- Where hashed objects may live (clause 10 of validity), given the
152-- reservation, the root object's slots and the slots of the largest hashed
153-- object. `relocate` keeps a policy exactly when this list is empty.
154let hashingProblems
155 : std:Region.Region -> Natural -> Natural -> D.Hashing -> List Text
156 = \space rootSlots objects hashing ->
157 let required = extentBits objects + D.margin
158 let used =
159 require
160 (if N.isZero objects then False else True)
161 "a layout without hashed objects must have Absent hashing"
162 in merge
163 { Absent = require (N.isZero objects) "hashed storage objects need a hashing policy"
164 , Confined =
165 \(block : { start : Natural, bits : Natural }) ->
166 used
167 # require
168 (N.lessThanEqual rootSlots block.start)
169 "hashed block overlaps the root object"
170 # require
171 (if N.lessThanEqual block.bits 256
172 then N.lessThanEqual (block.start + N.pow2 block.bits) space.size
173 else False)
174 "hashed block extends beyond the reservation"
175 # require (N.lessThanEqual required block.bits) "hashed block too small"
176 , Solidity =
177 used
178 # require
179 (N.isZero space.start && N.equal space.size (N.pow2 256))
180 "the Solidity hashing policy needs the whole address space"
181 # require (N.lessThanEqual required 256) "hashed block too small"
182 # require
183 (N.lessThanEqual (N.bitLength (rootSlots + objects) + D.margin) 256)
184 "the Solidity hashing policy leaves too little margin above the root object"
185 }
186 hashing
187
188in { slotsOf
189 , alignUp
190 , confined
191 , place
192 , slotsFor
193 , directBits
194 , directSlot
195 , checkedAdd
196 , field
197 , setField
198 , listSlots
199 , blobSlots
200 , extentBits
201 , find
202 , width
203 , ownSlots
204 , rootSlots
205 , objectSlots
206 , hashingProblems
207 }