Skip to content

Commit

Permalink
randomBytes (#55)
Browse files Browse the repository at this point in the history
* add randomUint256 function to suavelib

* add randomBytes precompile handler

* add randomUintX functions, deprecate randomUint256 precompile call

* remove randomUint256 precompile address

* Done

---------

Co-authored-by: Ferran Borreguero <[email protected]>
  • Loading branch information
zeroXbrock and ferranbt authored Mar 2, 2024
1 parent 018ccd4 commit 51f5ecf
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Random.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.8;

import "./suavelib/Suave.sol";

library Random {
function randomUint8() internal returns (uint8 value) {
bytes memory random = Suave.randomBytes(1);
assembly {
value := mload(add(random, 0x01))
}
}

function randomUint16() internal returns (uint16 value) {
bytes memory random = Suave.randomBytes(2);
assembly {
value := mload(add(random, 0x02))
}
}

function randomUint32() internal returns (uint32 value) {
bytes memory random = Suave.randomBytes(4);
assembly {
value := mload(add(random, 0x04))
}
}

function randomUint64() internal returns (uint64 value) {
bytes memory random = Suave.randomBytes(8);
assembly {
value := mload(add(random, 0x08))
}
}

function randomUint128() internal returns (uint128 value) {
bytes memory random = Suave.randomBytes(16);
assembly {
value := mload(add(random, 0x10))
}
}

function randomUint256() internal returns (uint256 value) {
bytes memory random = Suave.randomBytes(32);
assembly {
value := mload(add(random, 0x20))
}
}
}
52 changes: 52 additions & 0 deletions test/Random.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import {console2} from "forge-std/console2.sol";
import "src/suavelib/Suave.sol";
import "src/Random.sol";
import "src/Test.sol";

contract TestRandom is Test, SuaveEnabled {
function testRandomBytes() public {
bytes memory random = Suave.randomBytes(32);
console2.logBytes(random);
assert(random.length == 32);
}

function testRandomUint8() public {
uint8 random = Random.randomUint8();
console2.log("random uint8: %d", random);
assert(random > 0);
}

function testRandomUint16() public {
uint16 random = Random.randomUint16();
console2.log("random uint16: %d", random);
assert(random > 0);
}

function testRandomUint32() public {
uint32 random = Random.randomUint32();
console2.log("random uint32: %d", random);
assert(random > 0);
}

function testRandomUint64() public {
uint64 random = Random.randomUint64();
console2.log("random uint64: %d", random);
assert(random > 0);
}

function testRandomUint128() public {
uint128 random = Random.randomUint128();
console2.log("random uint128: %d", random);
assert(random > 0);
}

function testRandomUint256() public {
uint256 random = Random.randomUint256();
console2.log("random uint256: %d", random);
assert(random > 0);
}
}

0 comments on commit 51f5ecf

Please sign in to comment.