Developers on the Sui Testnet now have access to a new source of secure randomness generated by Sui validators, according to The Sui Blog. This essential feature, powered by the sui::random module, opens up various possibilities for developers and builders on the platform.
Applications of Onchain Randomness
Onchain randomness can be utilized in numerous ways, from simulating dice rolls to ensuring fair ticket allocations. Key use cases include:
- Games of chance: Lotteries, card games, and casino games can leverage onchain randomness to ensure fairness and transparency, allowing anyone to verify the game’s integrity.
- Random sampling: Useful in governance, dispute resolution, and randomized audits. It can also be employed in committee selections for oracles and DAOs.
- NFTs: Randomness can create NFTs with varying rarity levels, adding uniqueness and creativity to crypto collectibles.
- Contests and player matching: Random placement in tournament brackets, matching players for games, and determining the order of play in turn-based games.
Ensuring Secure Randomness
For onchain randomness to be effective, it must be both unpredictable and unbiasable. Unpredictability prevents attackers from manipulating outcomes, while unbiasability ensures no single party can influence the randomness generation process. Existing solutions often fall short due to predictability and bias issues.
Mysten Labs has developed a solution leveraging threshold cryptography and Distributed Key Generation (DKG) to achieve these qualities. This solution, supported by the validator network, allows for fast and secure randomness generation. Validators initiate a DKG protocol to generate secret shares of a distributed key at the start of each epoch. They use these shares to produce randomness continuously during the epoch.
Advantages of Sui’s Native Randomness
Sui’s native randomness solution is faster and more secure than existing methods. It operates parallel to the consensus mechanism, providing random values quickly after a transaction is ordered but before execution. Additionally, Sui’s Move programming language and Programmable Transaction Blocks (PTBs) offer powerful compositions while preventing potential manipulation through built-in restrictions and compiler warnings.
Using the sui::random Module
The sui::random module provides access to pseudorandomness within Sui, enabling various applications. For instance, developers can implement a Move function to select a random winner for a raffle:
entry set_winner(game: &mut Raffle, rnd: &Random, ctx: &TxContext) {
assert!(is_none(game.winner), EWinnerAlreadySet); // winner is of type Option
let gen = new_generator(rnd, ctx);
game.winner = Some(gen.generate_u32_in_range(1, game.num_of_tickets)); // num_of_tickets is u32 that represents the number of participants
}
This function ensures that a winner has not already been selected, initializes a new random generator, and generates a random number within the range of ticket numbers. The result is a securely and unpredictably chosen winner, thanks to Sui’s robust random generation guarantees.
Sui’s introduction of secure and scalable randomness is a significant advancement, enabling developers to build more robust and secure applications. The feature is now available on the Sui Testnet, and developers are encouraged to explore its capabilities and provide feedback. For more detailed technical information, refer to the Onchain Randomness documentation.
Image source: Shutterstock