Skip to content
Smart contracts Audit finding Intermediate

The RandomizerVRF and RandomizerRNG do not produce hash value.

The RandomizerVRF and RandomizerRNG contracts cast the packed random words and token ID directly to bytes32 instead of hashing them, so the stored token hash is effectively just the first random number. This means the token hash is not a hash, ignores the token ID, and does not expand the randomness range as intended.

Haipls Follow with Udsen, PetarTolev and 6 others

Originally disclosed on Code4rena

Submitted by Haipls, also found by Udsen, PetarTolev, r0ck3tz, VAD37, gumgumzum, Draiakoo, ast3ros, and 00xSEV

According to documentation and other randomizer implementations, Randomizers are expected to set a random HASH for the minted tokens.

However, the implementation mistakenly uses bytes32 in the format:

  • bytes32(abi.encodePacked(numbers, requestToToken[id]) or
  • bytes32(abi.encodePacked(_randomWords, requestToToken[_requestId])).
File: 2023-10-nextgen\smart-contracts\RandomizerVRF.sol
65:     function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
66:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[_requestId]], requestToToken[_requestId], bytes32(abi.encodePacked(_randomWords,requestToToken[_requestId])));
67:         emit RequestFulfilled(_requestId, _randomWords);
68:     }
69:

File: 2023-10-nextgen\smart-contracts\RandomizerRNG.sol
48:     function fulfillRandomWords(uint256 id, uint256[] memory numbers) internal override {
49:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[id]], requestToToken[id], bytes32(abi.encodePacked(numbers, requestToToken[id])));
50:     }

The hash is calculated as bytes32 from abi.encodePacked of the random number and tokenId.

Key observations:

  1. bytes32 truncates the result of abi.encodePacked(numbers, requestToToken[id]) and only considers the first element of the random numbers array.
  2. The random number is directly passed to setTokenHash. It is not a hash, and the second parameter meant to contribute to the hash creation is ignored.
  3. Now, the token’s hash is directly dependent on the provided number.

Thus, these contracts only pass the random number instead of generating a hash value.

Proof of Concept

Description

Example:

If _randomWords = [1, 2] and requestToToken = 1000, the result will be 0x0000000000000000000000000000000000000000000000000000000000000001, which equals only the first provided random number.

File: 2023-10-nextgen\smart-contracts\RandomizerVRF.sol
65:     function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
66:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[_requestId]], requestToToken[_requestId], bytes32(abi.encodePacked(_randomWords,requestToToken[_requestId])));
67:         emit RequestFulfilled(_requestId, _randomWords);
68:     }
69:

File: 2023-10-nextgen\smart-contracts\RandomizerRNG.sol
48:     function fulfillRandomWords(uint256 id, uint256[] memory numbers) internal override {
49:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[id]], requestToToken[id], bytes32(abi.encodePacked(numbers, requestToToken[id])));
50:     }

Tools Used

HardHat

Recommended Mitigation Steps

At a minimum, calculate the hash from the provided data using keccak256.

File: 2023-10-nextgen\smart-contracts\RandomizerVRF.sol
65:     function fulfillRandomWords(uint256 _requestId, uint256[] memory _randomWords) internal override {
-66:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[_requestId]], requestToToken[_requestId], bytes32(abi.encodePacked(_randomWords,requestToToken[_requestId])));
+66:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[_requestId]], requestToToken[_requestId], keccak256(abi.encodePacked(_randomWords,requestToToken[_requestId])));
67:         emit RequestFulfilled(_requestId, _randomWords);
68:     }
69:

File: 2023-10-nextgen\smart-contracts\RandomizerRNG.sol
48:     function fulfillRandomWords(uint256 id, uint256[] memory numbers) internal override {
-49:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[id]], requestToToken[id], bytes32(abi.encodePacked(numbers, requestToToken[id])));
+49:         gencoreContract.setTokenHash(tokenIdToCollection[requestToToken[id]], requestToToken[id], keccak256(abi.encodePacked(numbers, requestToToken[id])));

50:     }

This change will ensure compliance with documentation and consider all data involved in the hash creation process.

a2rocket (NextGen) confirmed via duplicate issue #1688

aslanbek (warden) commented via duplicate issue #1688:

I believe this report contradicts itself:

The RandomizerVRF and RandomizerRNG contracts will not return random hashes, instead, they will only return the generated random number.

This results in non-unique/duplicated tokenHash values and non-randomly generated NFTs. How can two random numbers not being hashed result in non-randomly generated NFTs?

There’s indeed a discrepancy between the spec and implementation, but not fixing the issue will simply result in tokens having a random tokenToHash anyway, which is sufficient for correct functioning of the protocol.

d3e4 (warden) commented via duplicate issue #1688:

Only one random number is requested, so nothing is trimmed away. Hashing a random 256-bit value does not make it any more random, it is just an arbitrary bijection. A random number and a hash digest (of an unknown or random number) are equivalent. It is randomness that is sought, not the output of specifically a hash function.

0xsomeone (judge) commented via duplicate issue #1688:

@aslanbek and @d3e4, thanks for your feedback! Let me address both of your points:

Report Contradiction - Indeed the report contradicts itself, however, it was the sole submission of this issue and as such is the primary one.

Randomness Impact - A value not being hashed does not impact its randomness, however, it does impact the range the values will be present in. As such, it allows the “expansion” of a randomness range to cover the full uint256 value range. For example, if the randomness values are in a sub-range [X,Y], the randomness oracles will produce token IDs solely in that range.

If a hash function is utilized (such as keccak256), the range will be expanded to [0, type(uint256).max] which is the desirable outcome. This is further illustrated by the fact that the tokenHash entry specifies it should be a hash.

ARRNG-Specific Impact - Another important trait of hashing is the minimization of monobit bias. A monobit bias occurs when a single bit in the randomness output is more frequently 0 or 1 in relation to the rest of the bits. This is especially prevalent in random sources such as ARRNG which rely on radio waves and other real-world data that may bias their measurements.

A hash will induce the avalanche effect whereby a bit will have a rough chance of being flipped. As such, monobit bias is somewhat eliminated by using hashing functions. While I won’t criticize Chainlink, the ARRNG service relies on RANDOM.ORG which provides publicly accessible data showcasing its monobit bias.

Chainlink-Specific Impact - The Chainlink oracle of NextGen is defined to have the numWords requested as updateable. This is very useful when the perceived entropy of random numbers is small; specifically, a hash of multiple lower-than-maximum (256 in this case) entropy numbers will “compress” the entropy (with some loss of course) into 32 bytes. As an example, hashing two numbers with 16 and 19 bits of entropy each will produce an output that has their entropy combined minus one to account for entropy lost due to compression and collisions.

As the numWords variable can be updated, we can see a problem in the codebase whereby any value of numWords greater than one will not increase the entropy of the randomness generator as expected.

Closing Thought - Given that the code contains an egregious error that directly impacts its functionality, I will maintain the current medium risk rating. The tokenHash mapping is expected to contain a hash, and assigning it otherwise is an invariant that is broken. I will consider inviting other judges to contribute to this particular judgment.

Note: For full discussion, see here.


The original report as published on Code4rena, by Haipls. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the developer used a bytes32 cast of abi.encodePacked instead of applying keccak256, so the packed data is truncated to its first 32 bytes rather than hashed.

Impact

The token hash may be limited to the range of the random source and ignores the token ID, so the stored value is not the hash the documentation expects; commenters noted this does not by itself make the token less random.

What the attacker needed

None

The fix

The hash should be computed with keccak256 over the packed random words and token ID before calling setTokenHash.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A hash is a fixed-length fingerprint produced by a one-way function such as keccak256. Here the code skipped that function and simply forced the random number into a 32-byte slot, so the stored value is the raw random number rather than a fingerprint of all the inputs. The result still looks random, but it does not mix in the token ID and does not spread values across the full range.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by Haipls

View all

7 more disclosed reports, best first.

More from NextGen

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.