forked from paradigmxyz/zk-eth-rng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZKBlockhashOracle.sol
89 lines (75 loc) · 3.18 KB
/
ZKBlockhashOracle.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.16;
import "./IBlockhashOracle.sol";
import "./BlockhashOpcodeOracle.sol";
import "./SingleBlockHeaderVerifier.sol";
/// @title Single block ZK Blockhash Oracle
/// @author AmanGotchu <[email protected]>
/// @author Sina Sabet <[email protected]>
/// @notice ZK based blockhash oracle that proves the parent hash of an already verified block.
contract ZKBlockhashOracle is BlockhashOpcodeOracle, SingleBlockHeaderVerifier {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error InvalidProof();
error BlockhashUnvalidated(bytes32 blockHash);
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/// @notice Validates the block hash of the block before the contract was initialized.
constructor() {
poke();
}
/// @notice Verifies a proof attesting to a parent blockhash of a block that has already been validated.
/// @param a Proof a value.
/// @param b Proof b value.
/// @param c Proof c value.
/// @param publicInputs Proof's public inputs (blockHash, parentHash, blockNum).
function verifyParentHash(
uint256[2] memory a,
uint256[2][2] memory b,
uint256[2] memory c,
uint256[198] memory publicInputs
) public returns (bool) {
/// Parse relevant information from the proof's public inputs.
// First 32 bytes are the anchored block hash.
uint256 i = 0;
bytes32 blockHash;
for (; i < 64; i++) {
blockHash <<= 4;
blockHash |= bytes32(publicInputs[i]);
}
// Next 32 bytes are the parentHash.
bytes32 parentHash;
for (; i < 128; i++) {
parentHash <<= 4;
parentHash |= bytes32(publicInputs[i]);
}
// Next 3 bytes are the anchored block's number.
bytes32 blockNumAccumulator;
for (; i < 134; i++) {
blockNumAccumulator <<= 4;
blockNumAccumulator |= bytes32(publicInputs[i]);
}
uint256 blockNum = uint256(blockNumAccumulator);
// If the anchored block hash hasn't been validated, we can try to validate using blockhash opcode.
if (blockhashToBlockNum[blockHash] == 0) {
bytes32 blockHashFromOpcode = blockhash(blockNum);
// If blockhash out of range of opcode, return BlockhashUnvalidated.
if (blockHashFromOpcode == 0) {
revert BlockhashUnvalidated(blockHash);
}
// If within range and doesn't match blockhash from proof, return InvalidProof.
if (blockHashFromOpcode != blockHash) {
revert InvalidProof();
}
setValidBlockhash(blockHash, blockNum);
}
// After fulfilling pre-reqs, we can verify the proof.
if (!verifyProof(a, b, c, publicInputs)) {
revert InvalidProof();
}
setValidBlockhash(parentHash, blockNum - 1);
return true;
}
}