Skip to content

Commit

Permalink
support Rand core functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebenfield committed Nov 13, 2024
1 parent 330b14d commit d5d9b8a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ version = "0.13.0"
version = "0.8"
default-features = false

[workspace.dependencies.rand_chacha]
version = "0.3.0"
default-features = false

[workspace.dependencies.regex]
version = "1.11.1"

Expand Down Expand Up @@ -190,8 +194,7 @@ workspace = true
workspace = true

[dependencies.rand_chacha]
version = "0.3.0"
default-features = false
workspace = true

[dependencies.self_update]
version = "0.41.0"
Expand Down
6 changes: 6 additions & 0 deletions interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ workspace = true

[dependencies.indexmap]
workspace = true

[dependencies.rand]
workspace = true

[dependencies.rand_chacha]
workspace = true
50 changes: 34 additions & 16 deletions interpreter/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ use std::{

use indexmap::{IndexMap, IndexSet};

use rand::Rng as _;

use rand_chacha::{ChaCha20Rng, rand_core::SeedableRng};

use snarkvm::prelude::{
Address as SvmAddressParam,
Boolean as SvmBooleanParam,
Expand Down Expand Up @@ -560,7 +564,7 @@ pub struct GlobalId {
pub name: Symbol,
}

#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug)]
pub struct Cursor<'a> {
pub frames: Vec<Frame<'a>>,
pub values: Vec<Value>,
Expand All @@ -569,9 +573,23 @@ pub struct Cursor<'a> {
pub mappings: HashMap<GlobalId, HashMap<Value, Value>>,
pub structs: HashMap<GlobalId, IndexSet<Symbol>>,
contexts: ContextStack,
rng: ChaCha20Rng,
}

impl<'a> Cursor<'a> {
pub fn new() -> Self {
Cursor {
frames: Default::default(),
values: Default::default(),
functions: Default::default(),
globals: Default::default(),
mappings: Default::default(),
structs: Default::default(),
contexts: Default::default(),
rng: ChaCha20Rng::from_entropy(),
}
}

fn pop_value(&mut self) -> Result<Value> {
match self.values.pop() {
Some(v) => Ok(v),
Expand Down Expand Up @@ -1140,21 +1158,21 @@ impl<'a> Cursor<'a> {
CoreFunction::BHP1024HashToScalar => {
apply_cast!(TestnetV0::hash_to_group_bhp1024, Scalar, to_bits_le)
}
CoreFunction::ChaChaRandAddress => todo!(),
CoreFunction::ChaChaRandBool => todo!(),
CoreFunction::ChaChaRandField => todo!(),
CoreFunction::ChaChaRandGroup => todo!(),
CoreFunction::ChaChaRandI8 => todo!(),
CoreFunction::ChaChaRandI16 => todo!(),
CoreFunction::ChaChaRandI32 => todo!(),
CoreFunction::ChaChaRandI64 => todo!(),
CoreFunction::ChaChaRandI128 => todo!(),
CoreFunction::ChaChaRandU8 => todo!(),
CoreFunction::ChaChaRandU16 => todo!(),
CoreFunction::ChaChaRandU32 => todo!(),
CoreFunction::ChaChaRandU64 => todo!(),
CoreFunction::ChaChaRandU128 => todo!(),
CoreFunction::ChaChaRandScalar => todo!(),
CoreFunction::ChaChaRandAddress => Value::Address(self.rng.gen()),
CoreFunction::ChaChaRandBool => Value::Bool(self.rng.gen()),
CoreFunction::ChaChaRandField => Value::Field(self.rng.gen()),
CoreFunction::ChaChaRandGroup => Value::Group(self.rng.gen()),
CoreFunction::ChaChaRandI8 => Value::I8(self.rng.gen()),
CoreFunction::ChaChaRandI16 => Value::I16(self.rng.gen()),
CoreFunction::ChaChaRandI32 => Value::I32(self.rng.gen()),
CoreFunction::ChaChaRandI64 => Value::I64(self.rng.gen()),
CoreFunction::ChaChaRandI128 => Value::I128(self.rng.gen()),
CoreFunction::ChaChaRandU8 => Value::U8(self.rng.gen()),
CoreFunction::ChaChaRandU16 => Value::U16(self.rng.gen()),
CoreFunction::ChaChaRandU32 => Value::U32(self.rng.gen()),
CoreFunction::ChaChaRandU64 => Value::U64(self.rng.gen()),
CoreFunction::ChaChaRandU128 => Value::U128(self.rng.gen()),
CoreFunction::ChaChaRandScalar => Value::Scalar(self.rng.gen()),
CoreFunction::Keccak256HashToAddress => apply_cast!(
|v| TestnetV0::hash_to_group_bhp256(&TestnetV0::hash_keccak256(v).expect_tc(span)?),
Address,
Expand Down
2 changes: 1 addition & 1 deletion interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Interpreter {
fn new_impl(source_files: &mut dyn Iterator<Item = &Path>) -> Result<Self> {
let handler = Handler::default();
let node_builder = Default::default();
let mut cursor: Cursor<'_> = Cursor::default();
let mut cursor: Cursor<'_> = Cursor::new();
let mut filename_to_program = HashMap::new();
for path in source_files {
let ast = Self::get_ast(path, &handler, &node_builder)?;
Expand Down

0 comments on commit d5d9b8a

Please sign in to comment.