-
Notifications
You must be signed in to change notification settings - Fork 81
/
OneStepProofEntry.sol
129 lines (115 loc) · 5.04 KB
/
OneStepProofEntry.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro-contracts/blob/main/LICENSE
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "../state/Deserialize.sol";
import "../state/Machine.sol";
import "../state/MerkleProof.sol";
import "./IOneStepProver.sol";
import "./IOneStepProofEntry.sol";
contract OneStepProofEntry is IOneStepProofEntry {
using MerkleProofLib for MerkleProof;
using MachineLib for Machine;
IOneStepProver public prover0;
IOneStepProver public proverMem;
IOneStepProver public proverMath;
IOneStepProver public proverHostIo;
constructor(
IOneStepProver prover0_,
IOneStepProver proverMem_,
IOneStepProver proverMath_,
IOneStepProver proverHostIo_
) {
prover0 = prover0_;
proverMem = proverMem_;
proverMath = proverMath_;
proverHostIo = proverHostIo_;
}
function proveOneStep(
ExecutionContext calldata execCtx,
uint256 machineStep,
bytes32 beforeHash,
bytes calldata proof
) external view override returns (bytes32 afterHash) {
Machine memory mach;
Module memory mod;
MerkleProof memory modProof;
Instruction memory inst;
{
uint256 offset = 0;
(mach, offset) = Deserialize.machine(proof, offset);
require(mach.hash() == beforeHash, "MACHINE_BEFORE_HASH");
if (mach.status != MachineStatus.RUNNING) {
// Machine is halted.
// WARNING: at this point, most machine fields are unconstrained.
return mach.hash();
}
if (machineStep + 1 == OneStepProofEntryLib.MAX_STEPS) {
mach.status = MachineStatus.ERRORED;
return mach.hash();
}
(mod, offset) = Deserialize.module(proof, offset);
(modProof, offset) = Deserialize.merkleProof(proof, offset);
require(
modProof.computeRootFromModule(mach.moduleIdx, mod) == mach.modulesRoot,
"MODULES_ROOT"
);
{
MerkleProof memory instProof;
MerkleProof memory funcProof;
(inst, offset) = Deserialize.instruction(proof, offset);
(instProof, offset) = Deserialize.merkleProof(proof, offset);
(funcProof, offset) = Deserialize.merkleProof(proof, offset);
bytes32 codeHash = instProof.computeRootFromInstruction(mach.functionPc, inst);
bytes32 recomputedRoot = funcProof.computeRootFromFunction(
mach.functionIdx,
codeHash
);
require(recomputedRoot == mod.functionsMerkleRoot, "BAD_FUNCTIONS_ROOT");
}
proof = proof[offset:];
}
uint256 oldModIdx = mach.moduleIdx;
mach.functionPc += 1;
uint16 opcode = inst.opcode;
IOneStepProver prover;
if (
(opcode >= Instructions.I32_LOAD && opcode <= Instructions.I64_LOAD32_U) ||
(opcode >= Instructions.I32_STORE && opcode <= Instructions.I64_STORE32) ||
opcode == Instructions.MEMORY_SIZE ||
opcode == Instructions.MEMORY_GROW
) {
prover = proverMem;
} else if (
(opcode == Instructions.I32_EQZ || opcode == Instructions.I64_EQZ) ||
(opcode >= Instructions.I32_RELOP_BASE &&
opcode <= Instructions.I32_RELOP_BASE + Instructions.IRELOP_LAST) ||
(opcode >= Instructions.I32_UNOP_BASE &&
opcode <= Instructions.I32_UNOP_BASE + Instructions.IUNOP_LAST) ||
(opcode >= Instructions.I32_ADD && opcode <= Instructions.I32_ROTR) ||
(opcode >= Instructions.I64_RELOP_BASE &&
opcode <= Instructions.I64_RELOP_BASE + Instructions.IRELOP_LAST) ||
(opcode >= Instructions.I64_UNOP_BASE &&
opcode <= Instructions.I64_UNOP_BASE + Instructions.IUNOP_LAST) ||
(opcode >= Instructions.I64_ADD && opcode <= Instructions.I64_ROTR) ||
(opcode == Instructions.I32_WRAP_I64) ||
(opcode == Instructions.I64_EXTEND_I32_S || opcode == Instructions.I64_EXTEND_I32_U) ||
(opcode >= Instructions.I32_EXTEND_8S && opcode <= Instructions.I64_EXTEND_32S) ||
(opcode >= Instructions.I32_REINTERPRET_F32 &&
opcode <= Instructions.F64_REINTERPRET_I64)
) {
prover = proverMath;
} else if (
(opcode >= Instructions.GET_GLOBAL_STATE_BYTES32 &&
opcode <= Instructions.SET_GLOBAL_STATE_U64) ||
(opcode >= Instructions.READ_PRE_IMAGE && opcode <= Instructions.HALT_AND_SET_FINISHED)
) {
prover = proverHostIo;
} else {
prover = prover0;
}
(mach, mod) = prover.executeOneStep(execCtx, mach, mod, inst, proof);
mach.modulesRoot = modProof.computeRootFromModule(oldModIdx, mod);
return mach.hash();
}
}