onchain-randomness
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOnchain Randomness
链上随机性
MCP tool: When available in your environment, also query the Sui documentation MCP server () for up-to-date answers. Use it for verification and for details not covered by these reference files.https://sui.mcp.kapa.ai
Source constraint: All information in this skill is sourced exclusively from docs.sui.io/sui-stack/on-chain-primitives/randomness-onchain. When extending or updating this skill, only pull from this source. Do not use third-party blogs, tutorials, or unofficial documentation.
Sui provides native onchain randomness through the shared object at address . Move functions create instances from and use them to produce unpredictable values. This skill covers the API, usage patterns, security considerations, and attack mitigations for onchain randomness.
Random0x8RandomGeneratorRandomMCP 工具: 如果你的环境中提供了该工具,还应查询 Sui 文档 MCP 服务器 () 以获取最新答案。使用它进行验证,并获取这些参考文件未涵盖的详细信息。https://sui.mcp.kapa.ai
来源约束: 此 skill 中的所有信息均仅来自 docs.sui.io/sui-stack/on-chain-primitives/randomness-onchain。扩展或更新此 skill 时,只能从此来源提取内容。不要使用第三方博客、教程或非官方文档。
Sui 通过位于地址 的 shared object 提供原生链上随机性。Move 函数从 创建 实例,并使用它们生成不可预测的值。本 skill 介绍链上随机性的 API、使用模式、安全注意事项和攻击缓解措施。
0x8RandomRandomRandomGeneratorReference files
参考文件
usage -- API and Usage Patterns
usage -- API 与使用模式
Path:
Load when: generating random values in Move, creating a , calling , using , , passing the object from TypeScript, or writing basic randomness-consuming functions.
Covers: the object (address ), , the API (full function list including , value generation, range generation, , ), basic implementation examples, TypeScript integration.
usage.mdRandomGeneratornew_generatorgenerate_u128generate_u8_in_rangeRandomRandom0x8RandomGeneratorrandom.movenew_generatorshufflegenerate_bytes路径:
加载条件: 在 Move 中生成随机值、创建 、调用 、使用 、使用 、从 TypeScript 传入 对象,或编写基础的随机性消费函数时。
涵盖内容: 对象(地址 )、、 API(完整函数列表,包括 、值生成、范围生成、、)、基础实现示例和 TypeScript 集成。
usage.mdRandomGeneratornew_generatorgenerate_u128generate_u8_in_rangeRandomRandom0x8RandomGeneratorrandom.movenew_generatorshufflegenerate_bytessecurity -- Security and Attack Vectors
security -- 安全与攻击向量
Path:
Load when: securing randomness-consuming functions, preventing composition attacks, mitigating PTB attacks, avoiding state leakage, implementing commit-reveal patterns, balancing gas across winning and losing paths, or choosing function visibility for randomness functions.
Covers: composition attacks (abort-filtering via public functions), PTB attacks (command chaining after consuming ), state leakage via , compiler lints on public functions accepting or , two-step commit-reveal pattern, balanced gas usage, entry function restrictions.
security.mdRandomGeneratorMoveCallRandomRandomGeneratorbcs::to_bytesRandomRandomGenerator路径:
加载条件: 保护随机性消费函数、防止组合攻击、缓解 PTB 攻击、避免 状态泄漏、实现 commit-reveal 模式、平衡中奖与未中奖路径的 gas 消耗,或选择随机性函数的可见性时。
涵盖内容: 组合攻击(通过 public 函数实施 abort-filtering)、PTB 攻击(在消费 的 之后链接命令)、通过 引发 状态泄漏、针对接受 或 参数的 public 函数的编译器 lint、两步 commit-reveal 模式、gas 消耗均衡以及 entry 函数限制。
security.mdRandomGeneratorRandomMoveCallbcs::to_bytesRandomGeneratorRandomRandomGeneratorRouting guide
路由指南
| Task | Load |
|---|---|
| Generating random values in Move | usage |
| Creating a RandomGenerator | usage |
| Calling random API functions | usage |
| Passing Random from TypeScript | usage |
| Securing a randomness-consuming function | security |
| Preventing composition or abort-filtering attacks | security |
| Mitigating PTB-based attacks | security |
| Avoiding RandomGenerator state leakage | security |
| Implementing commit-reveal for randomness | security |
| Balancing gas across execution paths | security |
| Choosing function visibility for randomness | security |
| Writing a complete randomness feature | usage + security |
| Reviewing randomness code for vulnerabilities | usage + security |
| 任务 | 加载 |
|---|---|
| 在 Move 中生成随机值 | usage |
| 创建 RandomGenerator | usage |
| 调用 random API 函数 | usage |
| 从 TypeScript 传入 Random | usage |
| 保护随机性消费函数 | security |
| 防止组合攻击或 abort-filtering 攻击 | security |
| 缓解基于 PTB 的攻击 | security |
| 避免 RandomGenerator 状态泄漏 | security |
| 为随机性实现 commit-reveal | security |
| 平衡各执行路径的 gas 消耗 | security |
| 选择随机性函数的可见性 | security |
| 编写完整的随机性功能 | usage + security |
| 审查随机性代码中的漏洞 | usage + security |
Rules
规则
- The object lives at reserved address
Randomand is immutable for transactions. Any attempt to modify it causes transaction failure.0x8 - Always create locally within the consuming function using
RandomGenerator. Never passr.new_generator(ctx)as aRandomGeneratorfunction parameter. Passing it topublicor private functions is acceptable for testing and in-package logic.public(package) - Define randomness-consuming functions as private functions. The Move compiler lints against
entryfunctions that acceptpublicorRandomas parameters.RandomGenerator - Sui rejects PTBs containing commands other than or
TransferObjectsfollowing aMergeCoinsthat consumesMoveCall. In other words, a call acceptingRandommust be the lastRandomin a PTB.MoveCall - Ensure winning and losing execution paths consume approximately equal total gas (both compute and storage) to prevent attackers from inferring outcomes from gas consumption. Measure gas carefully and test in each published environment before going live.
- Use the two-step commit-reveal pattern when the outcome of randomness must not be observable before a follow-up action completes.
- 对象位于保留地址
Random,并且在交易中不可变。任何修改尝试都会导致交易失败。0x8 - 始终在随机性消费函数内部使用 在本地创建
r.new_generator(ctx)。绝不将RandomGenerator作为RandomGenerator函数参数传递。将其传递给public或 private 函数对于测试和 package 内部逻辑是可接受的。public(package) - 将随机性消费函数定义为 private 函数。Move 编译器会对接受
entry或Random参数的 public 函数发出 lint 警告。RandomGenerator - Sui 会拒绝在消费 的
Random之后包含除MoveCall或TransferObjects之外命令的 PTB。换言之,接受MergeCoins的调用必须是 PTB 中最后一个Random。MoveCall - 确保中奖和未中奖的执行路径消耗大致相同的总 gas(计算和存储),以免攻击者通过 gas 消耗推断结果。上线前,应在每个发布环境中仔细测量 gas 并进行测试。
- 如果必须在后续操作完成前隐藏随机性结果,请使用两步 commit-reveal 模式。
Common mistakes
常见错误
- Making randomness functions instead of private
public. This is a fatal security flaw, not just a mistake. Public functions acceptingentryenable abort-filtering attacks where an attacker wraps the function and reverts unfavorable outcomes. The compiler lints against this.Random - Accepting as a
RandomGeneratorfunction parameter. Callers can predict outputs by serializing the generator's internal state withpublic. The compiler lints againstbcs::to_bytes(&generator)functions withpublicparameters. UsingRandomGeneratoras a parameter inRandomGeneratoror private functions is acceptable for testing and in-package logic.public(package) - Unbalanced gas consumption between branches. If winning and losing paths consume significantly different total gas (compute + storage), attackers can infer outcomes from gas usage. The success path should cost equal or more gas than the failure path. To achieve this, either perform a simple standard update (e.g., set a bool flag) and claim the result in a follow-up function, or create an equally sized object (e.g., a dynamic field) in the failure path. Gas values must be measured carefully and tested in each published environment before going live.
- Skipping commit-reveal when outcomes influence subsequent user actions. If a user can observe and act on a random outcome in the same transaction, they can abort unfavorable results.
- 将随机性函数设为 ,而不是 private
public。 这是致命的安全缺陷,而不只是一个小错误。接受entry的 public 函数会启用 abort-filtering 攻击,攻击者可以包装该函数,并对不利结果执行 revert。编译器会对此发出 lint 警告。Random - 将 作为 public 函数参数接受。 调用者可以通过
RandomGenerator序列化生成器的内部状态,从而预测输出结果。编译器会对带bcs::to_bytes(&generator)参数的 public 函数发出 lint 警告。在测试和 package 内部逻辑中,可以将RandomGenerator作为RandomGenerator或 private 函数的参数使用。public(package) - 分支之间的 gas 消耗不均衡。 如果中奖和未中奖路径消耗的总 gas(计算和存储)存在显著差异,攻击者便可以通过 gas 使用量推断结果。成功路径的 gas 成本应等于或高于失败路径。要实现这一点,可以执行简单的标准更新(例如,设置 bool 标志),并在后续函数中领取结果;或者在失败路径中创建一个大小相同的对象(例如 dynamic field)。上线前,必须在每个发布环境中仔细测量并测试 gas 数值。
- 在结果会影响后续用户操作时跳过 commit-reveal。 如果用户可以在同一交易中观察并作用于随机结果,那么他们便能中止对自己不利的结果。