Skip to content

Commit c069468

Browse files
authored
Merge pull request #481 from CedarMist/CedarMist/rng-notes
Sapphire/Precompiles: add notes about <=0.5.2 eth_call RNG behavior + detail on how VRF works
2 parents a4de652 + c019e67 commit c069468

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

docs/dapp/sapphire/precompiles.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,35 @@ Feel free to discover other convenient libraries for Solidity inside the
6060
* Gas cost: 10,000 minimum plus 240 per output word plus 60 per word of
6161
the personalization string.
6262

63-
Generate `num_bytes` pseudo-random bytes, with an optional
64-
personalization string added into the hashing algorithm to increase
65-
domain separation when needed.
63+
Generate `num_bytes` pseudo-random bytes, with an optional personalization
64+
string (`pers`) added into the hashing algorithm to increase domain separation
65+
when needed.
6666

6767
```solidity
6868
bytes memory randomPad = Sapphire.randomBytes(64, "");
6969
```
7070

71+
### Implementation Details
72+
73+
:::danger Prior to 0.6.0
74+
All view queries and simulated transactions (via `eth_call`) would receive the
75+
same entropy in-between blocks if they use the same `num_bytes` and `pers` parameters.
76+
If your contract requires confidentiality you should generate a secret in the constructor
77+
to be used with view calls:
78+
79+
```solidity
80+
Sapphire.randomBytes(64, abi.encodePacked(msg.sender, this.perContactSecret));
81+
```
82+
:::
83+
84+
The mode (e.g. simulation or 'view call' vs transaction execution) is fed to TupleHash (among other
85+
block-dependent components) to derive the "key id", which is then used to derive a per-block VRF key
86+
from epoch-ephemeral entropy (using KMAC256 and cSHAKE) so a different "key id" will result in a
87+
unique per-block VRF key. This per-block VRF key is then used to create the per-block root RNG which
88+
is then used to derive domain-separated (using Merlin transcripts) per-transaction random RNGs which
89+
are then exposed via this precompile. The KMAC, cSHAKE and TupleHash algorithms are SHA-3 derived functions
90+
defined in [NIST Special Publication 800-185](https://nvlpubs.nist.gov/nistpubs/specialpublications/nist.sp.800-185.pdf).
91+
7192
## X25519 Key Derivation
7293

7394
### Overview

0 commit comments

Comments
 (0)