-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathStorage.sol
298 lines (273 loc) · 9.25 KB
/
Storage.sol
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
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.21;
import { leftMask } from "./leftMask.sol";
import { Memory } from "./Memory.sol";
/**
* @title Storage Library
* @dev Provides functions for low-level storage manipulation, including storing and retrieving bytes.
*/
library Storage {
/**
* @notice Store a single word of data at a specific storage pointer.
* @param storagePointer The location to store the data.
* @param data The 32-byte word of data to store.
*/
function store(uint256 storagePointer, bytes32 data) internal {
assembly {
sstore(storagePointer, data)
}
}
/**
* @notice Store bytes of data at a specific storage pointer and offset.
* @param storagePointer The base storage location.
* @param offset Offset within the storage location.
* @param data Bytes to store.
*/
function store(uint256 storagePointer, uint256 offset, bytes memory data) internal {
store(storagePointer, offset, Memory.dataPointer(data), data.length);
}
/**
* @notice Stores raw bytes to storage at a given pointer, offset, and length, keeping the rest of the word intact.
* @param storagePointer The base storage location.
* @param offset Offset within the storage location.
* @param memoryPointer Pointer to the start of the data in memory.
* @param length Length of the data in bytes.
*/
function store(uint256 storagePointer, uint256 offset, uint256 memoryPointer, uint256 length) internal {
if (offset > 0) {
// Support offsets that are greater than 32 bytes by incrementing the storagePointer and decrementing the offset
if (offset >= 32) {
unchecked {
storagePointer += offset / 32;
offset %= 32;
}
}
// For the first word, if there is an offset, apply a mask to beginning
if (offset > 0) {
// Get the word's remaining length after the offset
uint256 wordRemainder;
// (safe because of `offset %= 32` at the start)
unchecked {
wordRemainder = 32 - offset;
}
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
// Load data from memory and offset it to match storage
let bitOffset := mul(offset, 8)
mask := shr(bitOffset, mask)
let offsetData := shr(bitOffset, mload(memoryPointer))
sstore(
storagePointer,
or(
// Store the middle part
and(offsetData, mask),
// Preserve the surrounding parts
and(sload(storagePointer), not(mask))
)
)
}
// Return if done
if (length <= wordRemainder) return;
// Advance pointers
// (safe because of `length <= wordRemainder` earlier)
unchecked {
storagePointer += 1;
memoryPointer += wordRemainder;
length -= wordRemainder;
}
}
}
// Store full words
while (length >= 32) {
/// @solidity memory-safe-assembly
assembly {
sstore(storagePointer, mload(memoryPointer))
}
unchecked {
storagePointer += 1;
memoryPointer += 32;
length -= 32;
}
}
// For the last partial word, apply a mask to the end
if (length > 0) {
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
sstore(
storagePointer,
or(
// store the left part
and(mload(memoryPointer), mask),
// preserve the right part
and(sload(storagePointer), not(mask))
)
)
}
}
}
/**
* @notice Set multiple storage locations to zero.
* @param storagePointer The starting storage location.
* @param length The number of storage locations to set to zero.
*/
function zero(uint256 storagePointer, uint256 length) internal {
// Ceil division to round up to the nearest word
uint256 limit = storagePointer + (length + 31) / 32;
while (storagePointer < limit) {
/// @solidity memory-safe-assembly
assembly {
sstore(storagePointer, 0)
storagePointer := add(storagePointer, 1)
}
}
}
/**
* @notice Load a single word of data from a specific storage pointer.
* @param storagePointer The location to load the data from.
* @return word The loaded 32-byte word of data.
*/
function load(uint256 storagePointer) internal view returns (bytes32 word) {
assembly {
word := sload(storagePointer)
}
}
/**
* @notice Load raw bytes from storage at a given pointer, offset, and length.
* @param storagePointer The base storage location.
* @param length Length of the data in bytes.
* @param offset Offset within the storage location.
* @return result The loaded bytes of data.
*/
function load(uint256 storagePointer, uint256 length, uint256 offset) internal view returns (bytes memory result) {
uint256 memoryPointer;
/// @solidity memory-safe-assembly
assembly {
// Solidity's YulUtilFunctions::roundUpFunction
function round_up_to_mul_of_32(value) -> _result {
_result := and(add(value, 31), not(31))
}
// Allocate memory
result := mload(0x40)
memoryPointer := add(result, 0x20)
mstore(0x40, round_up_to_mul_of_32(add(memoryPointer, length)))
// Store length
mstore(result, length)
}
load(storagePointer, length, offset, memoryPointer);
return result;
}
/**
* @notice Append raw bytes from storage at a given pointer, offset, and length to a specific memory pointer.
* @param storagePointer The base storage location.
* @param length Length of the data in bytes.
* @param offset Offset within the storage location.
* @param memoryPointer Pointer to the location in memory to append the data.
*/
function load(uint256 storagePointer, uint256 length, uint256 offset, uint256 memoryPointer) internal view {
if (offset > 0) {
// Support offsets that are greater than 32 bytes by incrementing the storagePointer and decrementing the offset
if (offset >= 32) {
unchecked {
storagePointer += offset / 32;
offset %= 32;
}
}
// For the first word, if there is an offset, apply a mask to beginning
if (offset > 0) {
// Get the word's remaining length after the offset
uint256 wordRemainder;
// (safe because of `offset %= 32` at the start)
unchecked {
wordRemainder = 32 - offset;
}
uint256 mask = leftMask(wordRemainder);
/// @solidity memory-safe-assembly
assembly {
// Load data from storage and offset it to match memory
let offsetData := shl(mul(offset, 8), sload(storagePointer))
mstore(
memoryPointer,
or(
// store the middle part
and(offsetData, mask),
// preserve the surrounding parts
and(mload(memoryPointer), not(mask))
)
)
}
// Return if done
if (length <= wordRemainder) return;
// Advance pointers
// (safe because of `length <= wordRemainder` earlier)
unchecked {
storagePointer += 1;
memoryPointer += wordRemainder;
length -= wordRemainder;
}
}
}
// Load full words
while (length >= 32) {
/// @solidity memory-safe-assembly
assembly {
mstore(memoryPointer, sload(storagePointer))
}
unchecked {
storagePointer += 1;
memoryPointer += 32;
length -= 32;
}
}
// For the last partial word, apply a mask to the end
if (length > 0) {
uint256 mask = leftMask(length);
/// @solidity memory-safe-assembly
assembly {
mstore(
memoryPointer,
or(
// store the left part
and(sload(storagePointer), mask),
// preserve the right part
and(mload(memoryPointer), not(mask))
)
)
}
}
}
/**
* @notice Load up to 32 bytes from storage at a given pointer and offset.
* @dev Since fields are tightly packed, they can span more than one slot.
* Since the they're max 32 bytes, they can span at most 2 slots.
* @param storagePointer The base storage location.
* @param length Length of the data in bytes.
* @param offset Offset within the storage location.
* @return result The loaded bytes, left-aligned bytes. Bytes beyond the length are zeroed.
*/
function loadField(uint256 storagePointer, uint256 length, uint256 offset) internal view returns (bytes32 result) {
if (offset >= 32) {
unchecked {
storagePointer += offset / 32;
offset %= 32;
}
}
// Extra data past length is not truncated
// This assumes that the caller will handle the overflow bits appropriately
assembly {
result := shl(mul(offset, 8), sload(storagePointer))
}
uint256 wordRemainder;
// (safe because of `offset %= 32` at the start)
unchecked {
wordRemainder = 32 - offset;
}
// Read from the next slot if field spans 2 slots
if (length > wordRemainder) {
assembly {
result := or(result, shr(mul(wordRemainder, 8), sload(add(storagePointer, 1))))
}
}
}
}