Function gstd::exec::random

source ·
pub fn random(subject: [u8; 32]) -> Result<([u8; 32], u32), Error>
Expand description

Get the random seed, along with the block number from which it is determinable by chain observers.

subject is a context identifier that allows you to get different results within the execution.

§Security

This must NOT be used for gambling, as it can be influenced by a malicious validator in the short term. It MAY be used in many cryptographic protocols, however, so long as one remembers that this (like everything else on-chain) is public. For example, it can be used when a number is needed that an adversary cannot choose for such purposes as public-coin zero-knowledge proofs.

§Examples

use core::array;
use gcore::exec;

#[no_mangle]
extern "C" fn handle() {
    let subject: [u8; 32] = array::from_fn(|i| i as u8 + 1);
    let (seed, block_number) = exec::random(subject).expect("Error in random");
}