This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
LibIntegrityState.sol
276 lines (251 loc) · 8.54 KB
/
LibIntegrityState.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
// SPDX-License-Identifier: CAL
pragma solidity ^0.8.15;
import "../run/RainInterpreter.sol";
import "../run/LibStackTop.sol";
import {MathUpgradeable as Math} from "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol";
import "../run/IInterpreterV1.sol";
struct IntegrityState {
// Sources first as we read it in assembly.
bytes[] sources;
uint256 constantsLength;
StackTop stackBottom;
StackTop stackMaxTop;
uint256 contextReads;
function(IntegrityState memory, Operand, StackTop)
view
returns (StackTop)[] integrityFunctionPointers;
}
library LibIntegrityState {
using LibIntegrityState for IntegrityState;
using LibStackTop for StackTop;
using Math for uint256;
function syncStackMaxTop(
IntegrityState memory integrityState_,
StackTop stackTop_
) internal pure {
if (
StackTop.unwrap(stackTop_) >
StackTop.unwrap(integrityState_.stackMaxTop)
) {
integrityState_.stackMaxTop = stackTop_;
}
}
function ensureIntegrity(
IntegrityState memory integrityState_,
SourceIndex sourceIndex_,
StackTop stackTop_,
uint minStackOutputs_
) internal view returns (StackTop) {
unchecked {
uint256 cursor_;
uint256 end_;
assembly ("memory-safe") {
cursor_ := mload(
add(
mload(integrityState_),
add(0x20, mul(0x20, sourceIndex_))
)
)
end_ := add(cursor_, mload(cursor_))
}
// Loop until complete.
while (cursor_ < end_) {
uint256 opcode_;
Operand operand_;
cursor_ += 4;
assembly ("memory-safe") {
let op_ := mload(cursor_)
operand_ := and(op_, 0xFFFF)
opcode_ := and(shr(16, op_), 0xFFFF)
}
// We index into the function pointers here to ensure that any
// opcodes that we don't have a pointer for will error.
stackTop_ = integrityState_.integrityFunctionPointers[opcode_](
integrityState_,
operand_,
stackTop_
);
}
require(
minStackOutputs_ <=
integrityState_.stackBottom.toIndex(stackTop_),
"MIN_FINAL_STACK"
);
return stackTop_;
}
}
function push(
IntegrityState memory integrityState_,
StackTop stackTop_
) internal pure returns (StackTop stackTopAfter_) {
stackTopAfter_ = stackTop_.up();
integrityState_.syncStackMaxTop(stackTopAfter_);
}
function push(
IntegrityState memory integrityState_,
StackTop stackTop_,
uint256 n_
) internal pure returns (StackTop stackTopAfter_) {
stackTopAfter_ = stackTop_.up(n_);
integrityState_.syncStackMaxTop(stackTopAfter_);
}
function popUnderflowCheck(
IntegrityState memory integrityState_,
StackTop stackTop_
) internal pure {
require(
// Stack bottom may be non-zero so check we are above it.
(StackTop.unwrap(stackTop_) >=
StackTop.unwrap(integrityState_.stackBottom)) &&
// If we underflowed zero then we will be above the stack max
// top. Assumes that at least 1 item was popped so we can do a
// strict inequality check here.
(StackTop.unwrap(stackTop_) <
StackTop.unwrap(integrityState_.stackMaxTop)),
"STACK_UNDERFLOW"
);
}
function pop(
IntegrityState memory integrityState_,
StackTop stackTop_
) internal pure returns (StackTop stackTopAfter_) {
stackTopAfter_ = stackTop_.down();
integrityState_.popUnderflowCheck(stackTopAfter_);
}
function pop(
IntegrityState memory integrityState_,
StackTop stackTop_,
uint256 n_
) internal pure returns (StackTop) {
if (n_ > 0) {
stackTop_ = stackTop_.down(n_);
integrityState_.popUnderflowCheck(stackTop_);
}
return stackTop_;
}
function applyFnN(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256) internal view returns (uint256),
uint256 n_
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, n_));
}
function applyFnN(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256) internal view,
uint256 n_
) internal pure returns (StackTop) {
return integrityState_.pop(stackTop_, n_);
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256) internal view returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(Operand, uint256) internal view returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256) internal view
) internal pure returns (StackTop) {
return integrityState_.pop(stackTop_, 2);
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256) internal view returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, 2));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(Operand, uint256, uint256) internal view returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, 2));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256, uint256) internal view returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, 3));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256, uint256, uint)
internal
view
returns (uint256)
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, 4));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256[] memory) internal view returns (uint256),
uint256 length_
) internal pure returns (StackTop) {
return integrityState_.push(integrityState_.pop(stackTop_, length_));
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256, uint256[] memory)
internal
view
returns (uint256),
uint256 length_
) internal pure returns (StackTop) {
unchecked {
return
integrityState_.push(
integrityState_.pop(stackTop_, length_ + 2)
);
}
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256, uint256, uint256[] memory)
internal
view
returns (uint256),
uint256 length_
) internal pure returns (StackTop) {
unchecked {
return
integrityState_.push(
integrityState_.pop(stackTop_, length_ + 3)
);
}
}
function applyFn(
IntegrityState memory integrityState_,
StackTop stackTop_,
function(uint256, uint256[] memory, uint256[] memory)
internal
view
returns (uint256[] memory),
uint256 length_
) internal pure returns (StackTop) {
unchecked {
return
integrityState_.push(
integrityState_.pop(stackTop_, length_ * 2 + 1),
length_
);
}
}
}