Skip to content

Commit 03d4b21

Browse files
committed
Changes to be committed: Semiott Graph Deployment
1 parent 6381338 commit 03d4b21

File tree

12 files changed

+106211
-0
lines changed

12 files changed

+106211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"manifestVersion": "2.2",
3+
"contracts": {},
4+
"dependencies": {
5+
"@openzeppelin/contracts-ethereum-package": "^3.0.0"
6+
},
7+
"name": "compound-quad-vote-vdf-dao",
8+
"version": "1.0.0",
9+
"compiler": {
10+
"compilerSettings": {
11+
"optimizer": {
12+
"enabled": false,
13+
"runs": "200"
14+
}
15+
},
16+
"typechain": {
17+
"enabled": false
18+
},
19+
"manager": "truffle",
20+
"artifactsDir": "build/contracts",
21+
"contractsDir": "contracts"
22+
},
23+
"telemetryOptIn": true
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
pragma solidity ^0.5.16;
2+
3+
import "./IFactRegistry.sol";
4+
import "./PublicInputOffsets.sol";
5+
6+
7+
contract BeaconContract is PublicInputOffsets {
8+
event LogNewRandomness(uint256 blockNumber, bytes32 randomness);
9+
// Mapping: blockNumber -> randomness.
10+
mapping(uint256 => bytes32) private registeredRandomness;
11+
12+
uint256 latestBlockNumber;
13+
address public owner;
14+
uint256 public n_iterations;
15+
IFactRegistry verifierContract;
16+
uint256 internal constant PRIME = 0x30000003000000010000000000000001;
17+
uint256 internal constant MAX_LOG_TRACE_LENGTH = 40;
18+
uint256 internal constant PUBLIC_INPUT_SIZE = 5;
19+
20+
modifier onlyOwner {
21+
require(msg.sender == owner, "Sender is not the owner");
22+
_;
23+
}
24+
25+
constructor(address verifierAddress, uint256 n_iters) public {
26+
owner = msg.sender;
27+
verifierContract = IFactRegistry(verifierAddress);
28+
n_iterations = n_iters;
29+
}
30+
31+
/*
32+
Registers a new randomness if and only if:
33+
1. Can verify the block hash of the given blockNumber and that it indeed equals blockHash.
34+
2. The vdfInput calculted from the given blockHash matches the given proofPublicInput.
35+
3. The proofPublicInput was registered as a fact in the verifier contract.
36+
Updates latest randomness.
37+
*/
38+
function registerNewRandomness(
39+
uint256 blockNumber,
40+
bytes32 blockHash,
41+
uint256[PUBLIC_INPUT_SIZE] calldata proofPublicInput
42+
) external onlyOwner {
43+
// EVM can get block hash only for latest 256 blocks.
44+
require(
45+
blockNumber < block.number && block.number <= blockNumber + 255,
46+
"Block is not within the last 256 blocks."
47+
);
48+
// In case blockNumber refers to a block which is more than 256 blocks old,
49+
// blockhash(blockNumber) returns 0.
50+
require(
51+
blockhash(blockNumber) == blockHash && blockHash != 0,
52+
"blockHash does not match blockNumber or too old."
53+
);
54+
require(
55+
proofPublicInput[OFFSET_LOG_TRACE_LENGTH] < MAX_LOG_TRACE_LENGTH,
56+
"VDF reported length exceeds the integer overflow protection limit."
57+
);
58+
require(
59+
n_iterations == 10 * 2**proofPublicInput[OFFSET_LOG_TRACE_LENGTH] - 1,
60+
"Public input and n_iterations are not compatible."
61+
);
62+
require(
63+
proofPublicInput[OFFSET_VDF_OUTPUT_X] < PRIME &&
64+
proofPublicInput[OFFSET_VDF_OUTPUT_Y] < PRIME,
65+
"Invalid vdf output."
66+
);
67+
// To calculate the input of the VDF we first hash the blockHash with the string "veedo",
68+
// then we split the last 250 bits to two 125 bit field elements.
69+
uint256 vdfInput = uint256(keccak256(abi.encodePacked(blockHash, "veedo")));
70+
require(
71+
vdfInput & ((1 << 125) - 1) == proofPublicInput[OFFSET_VDF_INPUT_X],
72+
"blockHash does not match the given proofPublicInput."
73+
);
74+
require(
75+
((vdfInput >> 125) & ((1 << 125) - 1)) == proofPublicInput[OFFSET_VDF_INPUT_Y],
76+
"blockHash does not match the given proofPublicInput."
77+
);
78+
require(
79+
verifierContract.isValid(keccak256(abi.encodePacked(proofPublicInput))),
80+
"No valid proof provided."
81+
);
82+
// The randomness is the hash of the VDF output and the string "veedo"
83+
bytes32 randomness = keccak256(
84+
abi.encodePacked(
85+
proofPublicInput[OFFSET_VDF_OUTPUT_X],
86+
proofPublicInput[OFFSET_VDF_OUTPUT_Y],
87+
"veedo"
88+
)
89+
);
90+
registeredRandomness[blockNumber] = randomness;
91+
emit LogNewRandomness(blockNumber, randomness);
92+
// Update latestBlockNumber if blockNumber is greater than latestBlockNumber.
93+
if (blockNumber > latestBlockNumber) {
94+
latestBlockNumber = blockNumber;
95+
}
96+
}
97+
98+
/*
99+
If there is a randomness for blockNumber, returns it.
100+
Otherwise, returns 0.
101+
*/
102+
function getRandomness(uint256 blockNumber)
103+
external
104+
view
105+
returns (bytes32)
106+
{
107+
return registeredRandomness[blockNumber];
108+
}
109+
110+
/*
111+
Returns the latest registered (blockNumber, randomness).
112+
*/
113+
function getLatestRandomness() external view returns (uint256, bytes32) {
114+
return (latestBlockNumber, registeredRandomness[latestBlockNumber]);
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pragma solidity ^0.5.16;
2+
3+
/*
4+
The Fact Registry design pattern is a way to separate cryptographic verification from the
5+
business logic of the contract flow.
6+
A fact registry holds a hash table of verified "facts" which are represented by a hash of claims
7+
that the registry hash check and found valid. This table may be queried by accessing the
8+
isValid() function of the registry with a given hash.
9+
In addition, each fact registry exposes a registry specific function for submitting new claims
10+
together with their proofs. The information submitted varies from one registry to the other
11+
depending of the type of fact requiring verification.
12+
For further reading on the Fact Registry design pattern see this
13+
`StarkWare blog post <https://medium.com/starkware/the-fact-registry-a64aafb598b6>`_.
14+
*/
15+
contract IFactRegistry {
16+
17+
function isValid(bytes32 fact) view public returns (bool);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pragma solidity ^0.5.16;
2+
3+
4+
contract PublicInputOffsets {
5+
// The following constants are offsets of data expected in the public input.
6+
uint256 internal constant OFFSET_LOG_TRACE_LENGTH = 0;
7+
uint256 internal constant OFFSET_VDF_OUTPUT_X = 1;
8+
uint256 internal constant OFFSET_VDF_OUTPUT_Y = 2;
9+
uint256 internal constant OFFSET_VDF_INPUT_X = 3;
10+
uint256 internal constant OFFSET_VDF_INPUT_Y = 4;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const Migrations = artifacts.require("Migrations");
2+
3+
module.exports = function (deployer) {
4+
deployer.deploy(Migrations);
5+
};

0 commit comments

Comments
 (0)