Gabriel Ponce Laureta
Gabriel Ponce Laureta

Reputation: 42

Generating a random number from smart contract using Hedera Hashgraph

How to securely generate a random number on a smart contract using solidity from Hedera? I've been searching for an answer for a while now and looks like most of them recommend the use of Chainlink VRF?

I'm still new to this but on my limited understanding, nodes on Ethereum can somehow tamper the smart contract states or results. Since Hedera has a governing council nodes that we could somehow trust, maybe a simple generated random number from Solidity can be trusted? Or I'm getting this all wrong since I'm still learning.

Random numbers from smart contract has many use case, if someone from Hedera devs could see this thread please provide an easy solution.

Upvotes: 2

Views: 440

Answers (3)

bguiz
bguiz

Reputation: 28587

As @Greg Scullard notes, HIP-351 proposed a new system contract which includes a precompiled function that generates pseudorandom numbers.

To use this within solidity code, you need an address plus an interface, both of which are defined in HIP-351:

Address: 0x169

Solidity interface:

interface IPrngSystemContract {
    function getPseudorandomSeed() external returns (bytes32);
}

To use it, simply initialise an instance, we'll name it PrngSystemContract:

IPrngSystemContract constant PrngSystemContract =
    IPrngSystemContract(address(0x169));

Then subsequently invoke its getPseudorandomSeed function, which will return a pseudorandom bytes32, which you can then use in your smart contract's logic.

Here's an example smart contract implementation which does what was described above. In addition, it also uses a type conversion and modulo arithmetic choose a random number between a specified range. After generating the random number, it simply emits that result in an event. You should substitute the emit event with the logic you want.

Try this out on Remix

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;

interface IPrngSystemContract {
    // Generates a 256-bit pseudorandom seed using the first 256-bits of running hash of n-3 transaction record.
    // Users can generate a pseudorandom number in a specified range using the seed by (integer value of seed % range)
    function getPseudorandomSeed() external returns (bytes32);
}

contract Prng {
    IPrngSystemContract constant PrngSystemContract =
        IPrngSystemContract(address(0x169));

    event RandomResult(bytes32 randomBytes, uint256 num);

    function getPseudorandomSeed() public returns (bytes32 randomBytes) {
        randomBytes = PrngSystemContract.getPseudorandomSeed();
    }
    
    // Return value in the range [lo, hi)
    function getPseudorandomNumber(uint256 lo, uint256 hi) external returns (uint256 num) {
        bytes32 randomBytes = getPseudorandomSeed();
        num = uint256(randomBytes);
        num = lo + (num % (hi - lo));
        emit RandomResult(randomBytes, num);
    }
}

Upvotes: 0

Greg Scullard
Greg Scullard

Reputation: 471

Note this HIP which introduces a native transaction type for generating random numbers and which will be available as a precompile for use in smart contracts.

https://hips.hedera.com/hip/hip-351

Upvotes: 1

gehrig
gehrig

Reputation: 101

To generate a verifiable random number in a solidity smart contract on Hedera, you'd do the same as you would on Ethereum or any other EVM compatible network. You could use an existing library, like vrf-solidity.

Once you have the solidity file you'd like to use you'd then compile the smart contract, add the file to Hedera, and deploy the smart contract. The steps to do so can be found in the deploy your first smart contract tutorial found on Hedera docs.

Upvotes: 3

Related Questions