|
| 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 | +} |
0 commit comments