You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/v3/guidelines/smart-contracts/security/random-number-generation.md
+74-82Lines changed: 74 additions & 82 deletions
Original file line number
Diff line number
Diff line change
@@ -12,95 +12,87 @@ These algorithms typically require a _seed_ value to produce a sequence of _pseu
12
12
13
13
To predict the result of the `random()` function in a smart contract, one would need to know the current `seed` of the block, which is impossible unless you are a validator.
14
14
15
-
## Simply use randomize_lt()
15
+
#Approaches and security considerations
16
16
17
-
To make random number generation unpredictable, you can add the current [Logical Time](/v3/documentation/smart-contracts/message-management/messages-and-transactions#what-is-a-logical-time) to the seed. This ensures that different transactions produce different seeds and results.
17
+
There are multiple methods for generating random values, each with distinct trade-offs between speed, security, and decentralization guarantees.
18
18
19
-
Add the `randomize_lt()` call before generating random numbers to make them unpredictable:
19
+
Below we outline three fundamental approaches:
20
20
21
-
```func
22
-
randomize_lt();
23
-
int x = random(); ;; users can't predict this number
24
-
```
21
+
---
25
22
26
-
However, validators or collators can still influence the result as they determine the seed of the current block.
23
+
## Approach 1: randomize_lt
27
24
28
-
## Is there a way to protect against manipulation by validators?
25
+
**Mechanism**: Generates randomness using block logical time (`lt`) and blockchain entropy.
26
+
**Security Model**:
29
27
30
-
You can use more complex schemes to reduce the risk of validators manipulating the seed. For example, you can skip one block before generating a random number, which changes the seed less predictably.
28
+
- ✅ Safe against user manipulation
29
+
- ❌ Vulnerable to colluding validators (could theoretically predict/influence values)
31
30
32
-
Skipping blocks is straightforward. You can achieve this by sending a message to the MasterChain and back to your contract's workchain. Let's explore a simple example!
31
+
**Speed**: Fast (single-block operation)
32
+
**Use Cases**:
33
+
34
+
- Non-critical applications, for example gaming & NFTs
35
+
- Scenarios where validator trust is assumed
36
+
37
+
---
38
+
39
+
## Approach 2: Block skipping
40
+
41
+
**Mechanism**: Uses entropy from skipped blocks in blockchain history.
42
+
**Security Model**:
43
+
44
+
- ✅ Resistant to user manipulation
45
+
- ⚠️ Not fully secure against determined validators (may influence block inclusion timing)
46
+
47
+
**Speed**: Slow (requires multiple blocks to finalize)
48
+
**Use Cases**:
49
+
50
+
- Medium-stakes applications, for example lottery systems
Deploy this contract in any workchain (likely Basechain), and you're done!
97
-
98
-
## Is this method 100% secure?
99
-
100
-
While this method improves security, there is still a tiny chance of manipulation if an attacker controls multiple validators. In such cases, they might influence the _seed_, which affects the random number. Even though the probability is extremely low, it is worth considering.
101
-
102
-
With the latest TVM upgrade, introducing new values to the `c7` register enhances the security of random number generation. Specifically, the upgrade adds information about the last 16 MasterChain blocks to the `c7` register.
103
-
104
-
The MasterChain block information, due to its dynamic nature, serves as an additional source of entropy for random number generation. By incorporating this data into your randomness algorithm, you can create even harder numbers for potential adversaries to predict.
105
-
106
-
For more details on this TVM upgrade, refer to [TVM Upgrade](/v3/documentation/tvm/changelog/tvm-upgrade-2023-07).
96
+
Always audit implementations through formal verification where possible.
97
+
98
+
For working examples, refer to [randomization contracts example](https://github.com/puppycats/ton-random).
0 commit comments