-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvmSharpMmrGrowingModule.sol
234 lines (176 loc) · 12.1 KB
/
EvmSharpMmrGrowingModule.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.27;
import {Uint256Splitter} from "src/libraries/internal/Uint256Splitter.sol";
import {IFactsRegistry} from "src/interfaces/external/IFactsRegistry.sol";
import {IEvmSharpMmrGrowingModule} from "src/interfaces/modules/growing/IEvmSharpMmrGrowingModule.sol";
import {ISatellite} from "src/interfaces/ISatellite.sol";
import {LibSatellite} from "src/libraries/LibSatellite.sol";
import {IMmrCoreModule, RootForHashingFunction, GrownBy} from "src/interfaces/modules/IMmrCoreModule.sol";
import {AccessController} from "src/libraries/AccessController.sol";
contract EvmSharpMmrGrowingModule is IEvmSharpMmrGrowingModule, AccessController {
// Using inline library for efficient splitting and joining of uint256 values
using Uint256Splitter for uint256;
// Cairo program hash calculated with Poseidon (i.e., the off-chain block headers accumulator program)
bytes32 public constant PROGRAM_HASH = bytes32(uint256(0x1eca36d586f5356fba096edbf7414017d51cd0ed24b8fde80f78b61a9216ed2));
bytes32 public constant KECCAK_HASHING_FUNCTION = keccak256("keccak");
bytes32 public constant POSEIDON_HASHING_FUNCTION = keccak256("poseidon");
// Default roots for new aggregators:
// poseidon_hash(1, "brave new world")
bytes32 public constant POSEIDON_MMR_INITIAL_ROOT = 0x06759138078831011e3bc0b4a135af21c008dda64586363531697207fb5a2bae;
// keccak_hash(1, "brave new world")
bytes32 public constant KECCAK_MMR_INITIAL_ROOT = 0x5d8d23518dd388daa16925ff9475c5d1c06430d21e0422520d6a56402f42937b;
// ========================= Satellite Module Storage ========================= //
bytes32 constant MODULE_STORAGE_POSITION = keccak256("diamond.standard.satellite.module.storage.evm-sharp-mmr-growing");
function moduleStorage() internal pure returns (EvmSharpMmrGrowingModuleStorage storage s) {
bytes32 position = MODULE_STORAGE_POSITION;
assembly {
s.slot := position
}
}
// ========================= Core Functions ========================= //
function initEvmSharpMmrGrowingModule(IFactsRegistry factsRegistry) external onlyOwner {
EvmSharpMmrGrowingModuleStorage storage ms = moduleStorage();
ms.factsRegistry = factsRegistry;
ms.aggregatedChainId = block.chainid;
}
function createEvmSharpMmr(uint256 newMmrId, uint256 originalMmrId, uint256 mmrSize) external {
bytes32[] memory hashingFunctions = new bytes32[](2);
hashingFunctions[0] = KECCAK_HASHING_FUNCTION;
hashingFunctions[1] = POSEIDON_HASHING_FUNCTION;
EvmSharpMmrGrowingModuleStorage storage ms = moduleStorage();
ISatellite(address(this)).createMmrFromDomestic(newMmrId, originalMmrId, ms.aggregatedChainId, mmrSize, hashingFunctions);
}
function aggregateEvmSharpJobs(uint256 mmrId, IEvmSharpMmrGrowingModule.JobOutputPacked[] calldata outputs) external {
// Ensuring at least one job output is provided
if (outputs.length < 1) {
revert NotEnoughJobs();
}
JobOutputPacked calldata firstOutput = outputs[0];
(uint256 fromBlock, ) = firstOutput.blockNumbersPacked.split128();
// Ensure the first job is continuable
_validateOutput(mmrId, fromBlock, firstOutput);
uint256 limit = outputs.length - 1;
// Iterate over the jobs outputs (aside from the last one)
// and ensure jobs are correctly linked and valid
for (uint256 i = 0; i < limit; ++i) {
JobOutputPacked calldata curOutput = outputs[i];
JobOutputPacked calldata nextOutput = outputs[i + 1];
_ensureValidFact(curOutput);
_ensureConsecutiveJobs(curOutput, nextOutput);
}
JobOutputPacked calldata lastOutput = outputs[limit];
_ensureValidFact(lastOutput);
(, uint256 mmrNewSize) = lastOutput.mmrSizesPacked.split128();
ISatellite.SatelliteStorage storage s = LibSatellite.satelliteStorage();
EvmSharpMmrGrowingModuleStorage storage ms = moduleStorage();
s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].mmrSizeToRoot[mmrNewSize] = lastOutput.mmrNewRootPoseidon;
s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].latestSize = mmrNewSize;
s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].isSiblingSynced = true;
s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].mmrSizeToRoot[mmrNewSize] = lastOutput.mmrNewRootKeccak;
s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].latestSize = mmrNewSize;
s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].isSiblingSynced = true;
(, uint256 toBlock) = lastOutput.blockNumbersPacked.split128();
ISatellite(address(this))._receiveParentHash(ms.aggregatedChainId, KECCAK_HASHING_FUNCTION, toBlock, lastOutput.blockNMinusRPlusOneParentHash);
RootForHashingFunction[] memory rootsForHashingFunctions = new RootForHashingFunction[](2);
rootsForHashingFunctions[0].root = lastOutput.mmrNewRootPoseidon;
rootsForHashingFunctions[0].hashingFunction = POSEIDON_HASHING_FUNCTION;
rootsForHashingFunctions[1].root = lastOutput.mmrNewRootKeccak;
rootsForHashingFunctions[1].hashingFunction = KECCAK_HASHING_FUNCTION;
emit IMmrCoreModule.GrownMmr(fromBlock, toBlock, rootsForHashingFunctions, mmrNewSize, mmrId, ms.aggregatedChainId, GrownBy.EVM_SHARP_GROWER);
}
/// @notice Ensures the job output is cryptographically sound to continue from
/// @param mmrId The MMR ID to validate the output for
/// @param fromBlockNumber The parent hash of the block to start from
/// @param firstOutput The job output to check
function _validateOutput(uint256 mmrId, uint256 fromBlockNumber, IEvmSharpMmrGrowingModule.JobOutputPacked memory firstOutput) internal view {
(uint256 mmrPreviousSize, ) = firstOutput.mmrSizesPacked.split128();
ISatellite.SatelliteStorage storage s = LibSatellite.satelliteStorage();
EvmSharpMmrGrowingModuleStorage storage ms = moduleStorage();
// Retrieve from cache the parent hash of the block to start from
bytes32 fromBlockPlusOneParentHash = s.receivedParentHashes[ms.aggregatedChainId][KECCAK_HASHING_FUNCTION][fromBlockNumber + 1];
uint256 actualMmrSizePoseidon = s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].latestSize;
uint256 actualMmrSizeKeccak = s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].latestSize;
// Check that the job's previous MMR size is the same as the one stored in the contract state
if (mmrPreviousSize != actualMmrSizePoseidon || mmrPreviousSize != actualMmrSizeKeccak) {
revert AggregationError("MMR size mismatch");
}
if (s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].isSiblingSynced == false) {
revert AggregationError("Poseidon MMR not sibling synced");
}
if (s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].isSiblingSynced == false) {
revert AggregationError("Keccak MMR not sibling synced");
}
// Check that the job's previous Poseidon MMR root is the same as the one stored in the contract state
if (firstOutput.mmrPreviousRootPoseidon != s.mmrs[ms.aggregatedChainId][mmrId][POSEIDON_HASHING_FUNCTION].mmrSizeToRoot[mmrPreviousSize])
revert AggregationError("Poseidon root mismatch");
// Check that the job's previous Keccak MMR root is the same as the one stored in the contract state
if (firstOutput.mmrPreviousRootKeccak != s.mmrs[ms.aggregatedChainId][mmrId][KECCAK_HASHING_FUNCTION].mmrSizeToRoot[mmrPreviousSize])
revert AggregationError("Keccak root mismatch");
// If not present in the cache, hash is not authenticated and we cannot continue from it
if (fromBlockPlusOneParentHash == bytes32(0)) {
revert UnknownParentHash();
}
// we check that the job's `blockN + 1 parent hash` is matching with a previously stored parent hash
if (firstOutput.blockNPlusOneParentHash != fromBlockPlusOneParentHash) {
revert AggregationError("Parent hash mismatch: ensureContinuable");
}
}
/// @notice Ensures the fact is regisfirstOutputon SHARP Facts Registry
/// @param output SHARP job output (packed for Solidity)
function _ensureValidFact(JobOutputPacked memory output) internal view {
(uint256 fromBlock, uint256 toBlock) = output.blockNumbersPacked.split128();
(uint256 mmrPreviousSize, uint256 mmrNewSize) = output.mmrSizesPacked.split128();
(uint256 blockNPlusOneParentHashLow, uint256 blockNPlusOneParentHashHigh) = uint256(output.blockNPlusOneParentHash).split128();
(uint256 blockNMinusRPlusOneParentHashLow, uint256 blockNMinusRPlusOneParentHashHigh) = uint256(output.blockNMinusRPlusOneParentHash).split128();
(uint256 mmrPreviousRootKeccakLow, uint256 mmrPreviousRootKeccakHigh) = uint256(output.mmrPreviousRootKeccak).split128();
(uint256 mmrNewRootKeccakLow, uint256 mmrNewRootKeccakHigh) = uint256(output.mmrNewRootKeccak).split128();
// We assemble the outputs in a uint256 array
uint256[] memory outputs = new uint256[](14);
outputs[0] = fromBlock;
outputs[1] = toBlock;
outputs[2] = blockNPlusOneParentHashLow;
outputs[3] = blockNPlusOneParentHashHigh;
outputs[4] = blockNMinusRPlusOneParentHashLow;
outputs[5] = blockNMinusRPlusOneParentHashHigh;
outputs[6] = uint256(output.mmrPreviousRootPoseidon);
outputs[7] = mmrPreviousRootKeccakLow;
outputs[8] = mmrPreviousRootKeccakHigh;
outputs[9] = mmrPreviousSize;
outputs[10] = uint256(output.mmrNewRootPoseidon);
outputs[11] = mmrNewRootKeccakLow;
outputs[12] = mmrNewRootKeccakHigh;
outputs[13] = mmrNewSize;
// We hash the outputs
bytes32 outputHash = keccak256(abi.encodePacked(outputs));
// We compute the deterministic fact bytes32 value
bytes32 fact = keccak256(abi.encode(PROGRAM_HASH, outputHash));
EvmSharpMmrGrowingModuleStorage storage ms = moduleStorage();
// We ensure this fact has been registered on SHARP Facts Registry
if (!ms.factsRegistry.isValid(fact)) {
revert InvalidFact();
}
}
/// @notice Ensures the job outputs are correctly linked
/// @param output The job output to check
/// @param nextOutput The next job output to check
function _ensureConsecutiveJobs(JobOutputPacked memory output, JobOutputPacked memory nextOutput) internal pure {
(, uint256 toBlock) = output.blockNumbersPacked.split128();
// We cannot aggregate further past the genesis block
if (toBlock == 0) {
revert GenesisBlockReached();
}
(uint256 nextFromBlock, ) = nextOutput.blockNumbersPacked.split128();
// We check that the next job's `from block` is the same as the previous job's `to block + 1`
if (toBlock - 1 != nextFromBlock) revert AggregationBlockMismatch("ensureConsecutiveJobs");
(, uint256 outputMmrNewSize) = output.mmrSizesPacked.split128();
(uint256 nextOutputMmrPreviousSize, ) = nextOutput.mmrSizesPacked.split128();
// We check that the previous job's new Poseidon MMR root matches the next job's previous Poseidon MMR root
if (output.mmrNewRootPoseidon != nextOutput.mmrPreviousRootPoseidon) revert AggregationError("Poseidon root mismatch");
// We check that the previous job's new Keccak MMR root matches the next job's previous Keccak MMR root
if (output.mmrNewRootKeccak != nextOutput.mmrPreviousRootKeccak) revert AggregationError("Keccak root mismatch");
// We check that the previous job's new MMR size matches the next job's previous MMR size
if (outputMmrNewSize != nextOutputMmrPreviousSize) revert AggregationError("MMR size mismatch");
// We check that the previous job's lowest block hash matches the next job's highest block hash
if (output.blockNMinusRPlusOneParentHash != nextOutput.blockNPlusOneParentHash) revert AggregationError("Parent hash mismatch: ensureConsecutiveJobs");
}
}