Skip to content
Draft
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
5 changes: 1 addition & 4 deletions src/libfuncs/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1304,10 +1304,7 @@ fn is_shared<'ctx, 'this>(
#[cfg(test)]
mod test {
use crate::{
utils::{
felt252_str,
test::{jit_enum, jit_panic, jit_struct, load_cairo, run_program},
},
utils::test::{felt252_str, jit_enum, jit_panic, jit_struct, load_cairo, run_program},
values::Value,
};
use pretty_assertions_sorted::assert_eq;
Expand Down
5 changes: 2 additions & 3 deletions src/libfuncs/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,8 @@ fn build_array_slice<'ctx>(
mod test {

use crate::{
utils::{
felt252_str,
test::{jit_enum, jit_panic, jit_struct, load_cairo, run_program_assert_output},
utils::test::{
felt252_str, jit_enum, jit_panic, jit_struct, load_cairo, run_program_assert_output,
},
values::Value,
};
Expand Down
50 changes: 25 additions & 25 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,6 @@ pub fn find_function_id<'a>(program: &'a Program, function_name: &str) -> Option
.map(|func| &func.id)
}

/// Parse a numeric string into felt, wrapping negatives around the prime modulo.
pub fn felt252_str(value: &str) -> Felt {
let value = value
.parse::<BigInt>()
.expect("value must be a digit number");

let value = match value.sign() {
Sign::Minus => &*PRIME - value.magnitude(),
_ => value.magnitude().clone(),
};

value.into()
}

/// Parse any type that can be a bigint to a felt that can be used in the cairo-native input.
pub fn felt252_bigint(value: impl Into<BigInt>) -> Felt {
let value: BigInt = value.into();
Expand All @@ -266,17 +252,6 @@ pub fn felt252_bigint(value: impl Into<BigInt>) -> Felt {
value.as_ref().into()
}

/// Parse a short string into a felt that can be used in the cairo-native input.
pub fn felt252_short_str(value: &str) -> Felt {
let values: Vec<_> = value
.chars()
.filter_map(|c| c.is_ascii().then_some(c as u8))
.collect();

assert!(values.len() < 32, "A felt can't longer than 32 bytes");
Felt::from_bytes_be_slice(&values)
}

/// Creates the execution engine, with all symbols registered.
pub fn create_engine(
module: &Module,
Expand Down Expand Up @@ -490,6 +465,31 @@ pub mod test {
pub(crate) use jit_panic;
pub(crate) use jit_struct;

/// Parse a numeric string into felt, wrapping negatives around the prime modulo.
pub(crate) fn felt252_str(value: &str) -> Felt {
let value = value
.parse::<BigInt>()
.expect("value must be a digit number");

let value = match value.sign() {
Sign::Minus => &*PRIME - value.magnitude(),
_ => value.magnitude().clone(),
};

value.into()
}

/// Parse a short string into a felt that can be used in the cairo-native input.
pub fn felt252_short_str(value: &str) -> Felt {
let values: Vec<_> = value
.chars()
.filter_map(|c| c.is_ascii().then_some(c as u8))
.collect();

assert!(values.len() < 32, "A felt can't longer than 32 bytes");
Felt::from_bytes_be_slice(&values)
}

pub(crate) fn load_cairo_str(program_str: &str) -> (String, Program) {
compile_program(program_str, RootDatabase::default())
}
Expand Down
Loading