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

Feat: generate random felt #2634

Merged
merged 16 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
manlikeHB marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Forge

#### Added

- `generate_random_felt()` for generating (pseudo) random felt value.

### Cast

#### Added
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/cheatnet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ universal-sierra-compiler-api = { path = "../universal-sierra-compiler-api" }
k256.workspace = true
p256.workspace = true
shared.workspace = true
rand.workspace = true

[dev-dependencies]
ctor.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use cairo_vm::Felt252;
use num_bigint::{BigUint, RandBigInt};

#[must_use]
pub fn generate_random_felt() -> Felt252 {
let mut rng = rand::thread_rng();

let random_number: BigUint = rng.gen_biguint(251);
Felt252::from(random_number)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod cheat_execution_info;
pub mod cheat_sequencer_address;
pub mod declare;
pub mod deploy;
pub mod generate_random_felt;
pub mod get_class_hash;
pub mod l1_handler_execute;
pub mod mock_call;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::runtime_extensions::{
forge_runtime_extension::cheatcodes::{
declare::declare,
deploy::{deploy, deploy_at},
generate_random_felt::generate_random_felt,
get_class_hash::get_class_hash,
l1_handler_execute::l1_handler_execute,
storage::{calculate_variable_address, load, store},
Expand Down Expand Up @@ -473,6 +474,9 @@ impl<'a> ExtensionLogic for ForgeExtension<'a> {
map_entry_address,
))
}
"generate_random_felt" => Ok(CheatcodeHandlingResult::from_serializable(
generate_random_felt(),
)),
_ => Ok(CheatcodeHandlingResult::Forwarded),
}
}
Expand Down
23 changes: 23 additions & 0 deletions crates/cheatnet/tests/cheatcodes/generate_random_felt.rs
manlikeHB marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use cairo_vm::Felt252;
use std::collections::HashSet;

use cheatnet::runtime_extensions::forge_runtime_extension::cheatcodes::generate_random_felt::generate_random_felt;

#[test]
fn test_generate_random_felt_range_and_uniqueness() {
let mut random_values = vec![];

let max_felt: Felt252 = Felt252::MAX;

for _ in 0..10 {
let random_value = generate_random_felt();
assert!(random_value < max_felt, "Value out of range");
random_values.push(random_value);
}

let unique_values: HashSet<_> = random_values.iter().collect();
assert!(
unique_values.len() > 1,
"Random values should not all be identical."
);
}
1 change: 1 addition & 0 deletions crates/cheatnet/tests/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod cheat_execution_info;
mod cheat_sequencer_address;
mod declare;
mod deploy;
mod generate_random_felt;
mod get_class_hash;
mod load;
mod mock_call;
Expand Down
43 changes: 43 additions & 0 deletions crates/forge/tests/integration/generate_random_felt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use indoc::indoc;
use test_utils::runner::assert_passed;
use test_utils::running_tests::run_test_case;
use test_utils::test_case;

#[test]
fn simple_generate_random_felt() {
let test = test_case!(indoc!(
r"
use snforge_std::generate_random_felt;

#[test]
fn simple_generate_random_felt() {
let mut random_values = array![];
let mut unique_values = array![];
let mut i = 10;

while i != 0 {
let random_value = generate_random_felt();
random_values.append(random_value);
i -= 1;
};

for element in random_values.span() {
let mut k = 0;

while k != random_values.len() {
if element != random_values.at(k) {
unique_values.append(element);
};
k += 1;
};
manlikeHB marked this conversation as resolved.
Show resolved Hide resolved
};

assert(unique_values.len() > 1, 'Identical values');
}
"
),);

let result = run_test_case(&test);

assert_passed(&result);
}
1 change: 1 addition & 0 deletions crates/forge/tests/integration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod dispatchers;
mod env;
mod fuzzing;
mod gas;
mod generate_random_felt;
mod get_class_hash;
mod l1_handler_executor;
mod message_to_l1;
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
* [spy_messages_to_l1](appendix/cheatcodes/spy_messages_to_l1.md)
* [store](appendix/cheatcodes/store.md)
* [load](appendix/cheatcodes/load.md)
* [generate_random_felt](appendix/cheatcodes/generate_random_felt.md)
* [`snforge` Library Reference](appendix/snforge-library.md)
* [byte_array](appendix/snforge-library/byte_array.md)
* [declare](appendix/snforge-library/declare.md)
Expand Down
5 changes: 5 additions & 0 deletions docs/src/appendix/cheatcodes/generate_random_felt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `generate_random_felt`

> `fn generate_random_felt() -> Felt252`

Generates a (pseudo) random `Felt252` value.
1 change: 1 addition & 0 deletions snforge_std/src/cheatcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod fork;
mod storage;
mod execution_info;
mod message_to_l1;
mod generate_random_felt;

/// Enum used to specify how long the target should be cheated for.
#[derive(Copy, Drop, Serde, PartialEq, Clone, Debug)]
Expand Down
13 changes: 13 additions & 0 deletions snforge_std/src/cheatcodes/generate_random_felt.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use starknet::testing::cheatcode;
use core::serde::Serde;
use super::super::_cheatcode::handle_cheatcode;


/// Generates a random felt value
///
/// Returns a random felt within the range of 0 and 2^252 - 1
fn generate_random_felt() -> felt252 {
let mut random_felt = handle_cheatcode(cheatcode::<'generate_random_felt'>(array![].span()));

Serde::deserialize(ref random_felt).unwrap()
}
cptartur marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions snforge_std/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ use cheatcodes::execution_info::account_contract_address::stop_cheat_account_con
use cheatcodes::execution_info::account_contract_address::stop_cheat_account_contract_address_global;
use cheatcodes::execution_info::account_contract_address::start_cheat_account_contract_address;

use cheatcodes::generate_random_felt::generate_random_felt;


mod fs;

Expand Down
Loading