Native VRF Randomness
TOS Network provides Verifiable Random Functions (VRF) at the protocol level, enabling secure, unpredictable, and verifiable random numbers for gaming, lotteries, and NFT applications.
Why VRF Matters
The Block Hash Problem
Block N: Player bets → Chooses "High"
Block N+1: Miner sees bet → Can manipulate block hash
by choosing which transactions to include
until hash favors the house
VRF Solution:
- Verifiable: Anyone can verify correctness
- Unpredictable: No one knows result before generation
- Unmanipulable: Generator cannot choose the outputCost Comparison
| Operation | Chainlink VRF (BSC) | TOS Native VRF |
|---|---|---|
| Request Random | $0.25 | $0.01 |
| Get Result | $0.05 | $0.001 |
| Instant Random | N/A | $0.005 |
| Batch Request (10) | $2.50 | $0.08 |
TOS is 25x cheaper than Chainlink VRF.
Comparison with Other Chains
| Blockchain | VRF Support | Cost | Latency |
|---|---|---|---|
| TRON | None | N/A | N/A |
| BSC | Chainlink | $0.25/req | 2-3 blocks |
| Ethereum | Chainlink | $2+/req | 2+ blocks |
| Solana | Native | Low | 1 slot |
| TOS | Native | $0.01 | 1 block |
Two Modes
1. Request-Response Mode (High Security)
Block N: User places bet + requests VRF
Block N+1: Validators generate VRF random number
Block N+1: Contract reads result and settles
Advantages:
- Miner cannot predict or manipulate
- Cryptographically secure
Use for: High-stakes gambling, important lotteries, NFT reveals2. Instant Mode (Low Latency)
Block N: User places bet + gets instant random + settles
Advantages:
- Immediate result, better UX
- Suitable for low-value operations
Use for: Small bets, game item drops, casual gamingAPI Reference
Syscall Interface
pub mod VRFSystem {
/// Request random number (generated in next block)
/// Returns: request_id for later retrieval
pub fn request_random(seed: Hash) -> u64;
/// Get random result
/// Returns: Some(VRFOutput) if ready, None if pending
pub fn get_random(request_id: u64) -> Option<VRFOutput>;
/// Verify random number
pub fn verify_random(output: &VRFOutput) -> bool;
/// Instant random (current block, lower security)
pub fn instant_random(seed: Hash) -> Hash;
/// Batch request multiple random numbers
pub fn request_random_batch(seeds: Vec<Hash>) -> Vec<u64>;
}VRF Output Structure
pub struct VRFOutput {
/// 32-byte random value
pub random: Hash,
/// Cryptographic proof
pub proof: VRFProof,
/// Block when requested
pub request_block: u64,
/// Block when generated
pub fulfill_block: u64,
/// Requester address
pub requester: Address,
/// Input seed
pub seed: Hash,
}
pub struct VRFProof {
pub gamma: [u8; 32],
pub c: [u8; 32],
pub s: [u8; 32],
}Example: Gambling Game
Request-Response Pattern (High Stakes)
use tako_sdk::*;
/// Place bet with VRF
fn place_bet(player: Address, amount: u64, choice: u8) {
// Lock player funds
lock_funds(player, amount);
// Generate unique seed
let seed = blake3_hash(player, amount, choice, get_block_number());
// Request VRF random
let request_id = vrf_request_random(seed);
// Store bet info
store_bet(Bet {
request_id,
player,
amount,
choice, // 0 = Low, 1 = High
timestamp: get_timestamp(),
});
}
/// Settle bet (anyone can call after VRF ready)
fn settle_bet(request_id: u64) {
let bet = get_bet(request_id)?;
let vrf = vrf_get_random(request_id)?;
// Verify VRF proof
assert!(vrf_verify(&vrf));
// Calculate result: 0-49 = Low, 50-99 = High
let result = vrf.random[0] % 100;
let win = (result < 50 && bet.choice == 0) ||
(result >= 50 && bet.choice == 1);
if win {
// 1.95x payout
let payout = bet.amount * 195 / 100;
transfer(&bet.player, payout);
}
delete_bet(request_id);
}Instant Pattern (Low Stakes)
/// Instant lottery (low value)
fn lottery(player: Address) {
let ticket_price = 1_000000; // 1 USDT
// Deduct ticket
deduct(&player, ticket_price);
// Get instant random
let seed = blake3_hash(player, get_block_number(), get_timestamp());
let random = vrf_instant_random(seed);
// ~1.2% chance to win 100 USDT
let roll = random[0];
if roll < 3 {
transfer(&player, 100_000000);
emit_event("BigWin", player, 100_000000);
}
}Example: NFT Mystery Box
/// Open mystery box
fn open_mystery_box(player: Address, box_id: u64) {
// Verify ownership and burn box
require!(owns_box(&player, box_id));
burn_box(box_id);
// Request VRF
let seed = blake3_hash(player, box_id, get_block_number());
let request_id = vrf_request_random(seed);
store_pending_reveal(request_id, player, box_id);
}
/// Reveal box contents (after VRF ready)
fn reveal_mystery_box(request_id: u64) {
let pending = get_pending_reveal(request_id)?;
let vrf = vrf_get_random(request_id)?;
// Determine rarity from random
let roll = u32::from_be_bytes(vrf.random[0..4].try_into().unwrap()) % 10000;
let rarity = match roll {
0..=49 => Rarity::Legendary, // 0.5%
50..=499 => Rarity::Epic, // 4.5%
500..=1999 => Rarity::Rare, // 15%
_ => Rarity::Common, // 80%
};
// Mint NFT with determined rarity
mint_nft(&pending.player, rarity);
delete_pending_reveal(request_id);
}Security Guarantees
Cryptographic Security
- Based on EC-VRF (Elliptic Curve Verifiable Random Function)
- Compliant with IETF draft-irtf-cfrg-vrf-15 standard
Anti-Manipulation
- Random generated by validator set collectively
- Single validator cannot predict or control output
- Request-response separation prevents front-running
Verifiability
- Anyone can verify random number correctness
- Proof stored on-chain permanently
- Mathematical guarantee of fairness
Randomness Implementation Details
TOS provides multiple randomness sources:
| Type | Implementation | Use Case | MEV Protection |
|---|---|---|---|
| Deterministic | Blake3(contract+block+tx) | Simple games | Medium |
| Instant | DAG+POW entropy | Low-stakes | Medium |
| Delayed | Commit-Reveal, 10-block delay | High-stakes | High |
| VRF Proof | Block VRF with cryptographic proof | Maximum security | High |
Gas Costs
| Operation | Gas Units | Approx. Cost |
|---|---|---|
| VRF Request | 10,000 | $0.01 |
| VRF Get Result | 1,000 | $0.001 |
| VRF Verify | 5,000 | $0.005 |
| Instant Random | 5,000 | $0.005 |
| Batch Request (10) | 80,000 | $0.08 |
Summary
TOS Network’s native VRF provides:
- 25x cheaper than Chainlink VRF
- 1-block latency for results
- Two modes: High-security request-response or low-latency instant
- Cryptographic proofs for verification
- Built-in at protocol level, no external dependencies
This enables fair gaming, transparent lotteries, and provably random NFT mechanics at a fraction of the cost of other solutions.
Last updated on