Module sui::random
This module provides functionality for generating secure randomness.
- Struct Random
- Struct RandomInner
- Struct RandomGenerator
- Constants
- Function create
- Function load_inner_mut
- Function load_inner
- Function update_randomness_state
- Function new_generator
- Function derive_next_block
- Function generate_bytes
- Macro function uint_from_bytes
- Function generate_u256
- Function generate_u128
- Function generate_u64
- Function generate_u32
- Function generate_u16
- Function generate_u8
- Function generate_bool
- Macro function uint_in_range
- Function generate_u128_in_range
- Function generate_u64_in_range
- Function generate_u32_in_range
- Function generate_u16_in_range
- Function generate_u8_in_range
- Function shuffle
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>
Function generate_bytesβ
Generate n random bytes.
public fun generate_bytes(g: &mut sui::random::RandomGenerator, num_of_bytes: u16): vector<u8>
Macro function uint_from_bytesβ
macro fun uint_from_bytes<$T: drop>($g: &mut sui::random::RandomGenerator, $num_of_bytes: u8): $T
Function generate_u256β
Generate a u256.
public fun generate_u256(g: &mut sui::random::RandomGenerator): u256
Function generate_u128β
Generate a u128.
public fun generate_u128(g: &mut sui::random::RandomGenerator): u128
Function generate_u64β
Generate a u64.
public fun generate_u64(g: &mut sui::random::RandomGenerator): u64
Function generate_u32β
Generate a u32.
public fun generate_u32(g: &mut sui::random::RandomGenerator): u32