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

Random.int added #773

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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 docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,12 @@ The random library is designed to enable generation of cryptogrphically secure r

The <b>random.bool</b> method returns a randomly sourced boolean value.

### random.int

`random.int(min: i32, max: i32) -> i32`

The <b>random.int</b> method returns randomly generated integer value between a specified range. The range is inclusive on the minimum and exclusive on the maximum.

### random.string

`random.string(length: uint, charset: Optional<str>) -> str`
Expand Down
50 changes: 50 additions & 0 deletions implants/lib/eldritch/src/random/int_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anyhow::Result;
use rand::Rng;
use rand_chacha::rand_core::SeedableRng;

pub fn int(min: i32, max: i32) -> Result<i32> {
if min >= max {
return Err(anyhow::anyhow!("Invalid range"));
}
let mut rng = rand_chacha::ChaCha20Rng::from_entropy();
Ok(rng.gen_range(min..max))
}

#[cfg(test)]
mod tests {
use super::*;

const NUM_ITERATION: i32 = 50000;
const MIN_VALUE: i32 = 0;
const MAX_VALUE: i32 = 1000;

#[test]
fn test_random_int() -> anyhow::Result<()> {
let random_number = int(MIN_VALUE, MAX_VALUE)?;
assert!(random_number >= MIN_VALUE && random_number < MAX_VALUE);
Ok(())
}

#[test]
fn test_random_int_uniform() -> anyhow::Result<()> {
let mut counts = vec![0; MAX_VALUE as usize];
for _ in 0..NUM_ITERATION {
let random_number = int(MIN_VALUE, MAX_VALUE)?;
counts[random_number as usize] += 1;
}

let lower_bound = 0.90 * NUM_ITERATION as f64 / MAX_VALUE as f64;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know math but seems okay.

let upper_bound = 1.10 * NUM_ITERATION as f64 / MAX_VALUE as f64;

for count in counts {
assert!(
count as f64 >= lower_bound && count as f64 <= upper_bound,
"Count {} is not within the acceptable bounds of ({},{})",
count,
lower_bound,
upper_bound
);
}
Ok(())
}
}
6 changes: 6 additions & 0 deletions implants/lib/eldritch/src/random/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod bool_impl;
mod int_impl;
mod string_impl;

use starlark::{environment::MethodsBuilder, starlark_module, values::starlark_value};
Expand All @@ -21,6 +22,11 @@ fn methods(builder: &mut MethodsBuilder) {
bool_impl::bool()
}

#[allow(unused_variables)]
fn int<'v>(this: &RandomLibrary, min: i32, max: i32) -> anyhow::Result<i32> {
int_impl::int(min, max)
}

#[allow(unused_variables)]
fn string<'v>(this: &RandomLibrary, length: u64, charset: Option<String>) -> anyhow::Result<String> {
string_impl::string(length, charset)
Expand Down
Loading