Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add secure random range reference #22

Merged
merged 14 commits into from
Oct 1, 2024
Merged

Add secure random range reference #22

merged 14 commits into from
Oct 1, 2024

Conversation

sisyphusSmiling
Copy link
Contributor

Closes #21

Description

  • Adds a reference implementation for safely generating a random value within an inclusive range

Copy link
Collaborator

@tarakby tarakby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the range example. We would need to use a PRG in this case, not sure if there is an existing one in Solidity that we could import.

solidity/src/CoinToss.sol Outdated Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Outdated Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Show resolved Hide resolved
*/
function _aggregateRevertibleRandom256() private view returns (uint256) {
// Call _revertibleRandom() 4 times to aggregate 256 bits of randomness
uint256 randomValue = uint256(_revertibleRandom());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case of revertibleRandom, there is no need to create a PRG, because revertibleRandom already behaves like one (actually there is an internal PRG in FVM). Each time you call it, the PRG squeezes out a new random

solidity/src/CadenceRandomConsumer.sol Outdated Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Show resolved Hide resolved
@sisyphusSmiling
Copy link
Contributor Author

@tarakby I've done my best to implement your suggestions, including a Solidity port of the Cadence xorshift128+ implementation. Please lmk if I missed anything

@sisyphusSmiling sisyphusSmiling marked this pull request as ready for review September 27, 2024 02:03
@sisyphusSmiling sisyphusSmiling requested a review from a team as a code owner September 27, 2024 02:03
solidity/src/CoinToss.sol Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Outdated Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Outdated Show resolved Hide resolved
solidity/src/CadenceRandomConsumer.sol Show resolved Hide resolved
solidity/src/Xorshift128plus.sol Show resolved Hide resolved
Copy link
Collaborator

@tarakby tarakby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great code! Thanks for adding the PRG implementation too 🙏🏼

@sisyphusSmiling sisyphusSmiling merged commit 37686fd into main Oct 1, 2024
2 checks passed
@sisyphusSmiling sisyphusSmiling deleted the add-random-range branch October 1, 2024 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

Add example random in range function
3 participants