Module sui::random
This module provides functionality for generating secure randomness.
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::dynamic_field;
use sui::hex;
use sui::hmac;
use sui::object;
use sui::party;
use sui::transfer;
use sui::tx_context;
use sui::vec_map;
use sui::versioned;
Struct Randomβ
Singleton shared object which stores the global randomness state.
The actual state is stored in a versioned inner field.
public struct Random has key
Fields
- id: sui::object::UID
- inner: sui::versioned::Versioned
Struct RandomInnerβ
public struct RandomInner has store
Fields
- version: u64
- epoch: u64
- randomness_round: u64
- random_bytes: vector<u8>
Struct RandomGeneratorβ
Unique randomness generator, derived from the global randomness.
public struct RandomGenerator has drop
Fields
- seed: vector<u8>
- counter: u16
- buffer: vector<u8>
Constantsβ
const ENotSystemAddress: u64 = 0;
const EWrongInnerVersion: u64 = 1;
const EInvalidRandomnessUpdate: u64 = 2;
const EInvalidRange: u64 = 3;
const EInvalidLength: u64 = 4;
const CURRENT_VERSION: u64 = 1;
const RAND_OUTPUT_LEN: u16 = 32;
const U16_MAX: u64 = 65535;
Function createβ
Create and share the Random object. This function is called exactly once, when
the Random object is first created.
Can only be called by genesis or change_epoch transactions.
fun create(ctx: &mut sui::tx_context::TxContext)
Function load_inner_mutβ
fun load_inner_mut(self: &mut sui::random::Random): &mut sui::random::RandomInner
Function load_innerβ
fun load_inner(self: &sui::random::Random): &sui::random::RandomInner
Function update_randomness_stateβ
Record new randomness. Called when executing the RandomnessStateUpdate system transaction.
fun update_randomness_state(self: &mut sui::random::Random, new_round: u64, new_bytes: vector<u8>, ctx: &sui::tx_context::TxContext)
Function new_generatorβ
Create a generator. Can be used to derive up to MAX_U16 * 32 random bytes.
Using randomness can be error-prone if you don't observe the subtleties in its correct use, for example, randomness dependent code might be exploitable to attacks that carefully set the gas budget in a way that breaks security. For more information, see: https://docs.sui.io/guides/developer/advanced/randomness-onchain
public fun new_generator(r: &sui::random::Random, ctx: &mut sui::tx_context::TxContext): sui::random::RandomGenerator
Function derive_next_blockβ
Get the next block of 32 random bytes.
fun derive_next_block(g: &mut sui::random::RandomGenerator): vector<u8>