Skip to content

Commit

Permalink
Update stark-felt, expect slowdown here
Browse files Browse the repository at this point in the history
  • Loading branch information
Oppen committed Oct 19, 2023
1 parent f447b91 commit 14ac53f
Show file tree
Hide file tree
Showing 23 changed files with 163 additions and 201 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ readme = "README.md"
keywords = ["starknet", "cairo", "vm", "wasm", "no_std"]

[workspace.dependencies]
felt = { package = "cairo-felt", path = "./felt", version = "0.9.0", default-features = false, features = [
"alloc",
] }
cairo-vm = { path = "./vm", version = "0.9.0", default-features = false }
mimalloc = { version = "0.1.37", default-features = false }
num-bigint = { version = "0.4", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ keccak = { workspace = true }
hashbrown = { workspace = true }
anyhow = { workspace = true }
thiserror-no-std = { workspace = true }
stark-felt = { git = "https://github.com/lambdaclass/types-rs.git", rev = "c2945cfde8fe12744e934e60a2366abbd6250524", default-features = false, features = ["serde"] }
stark-felt = { git = "https://github.com/lambdaclass/types-rs.git", rev = "ea52b64a52fd832222adf400feb2dd3b4dc286a2", default-features = false, features = ["serde"] }

# only for std
num-prime = { version = "0.4.3", features = ["big-int"], optional = true }
Expand Down
70 changes: 28 additions & 42 deletions vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ fn get_maybe_relocatable_array_from_u32(array: &Vec<u32>) -> Vec<MaybeRelocatabl
new_array
}

fn get_maybe_relocatable_array_from_felt(array: &[Felt252]) -> Vec<MaybeRelocatable> {
array.iter().map(MaybeRelocatable::from).collect()
}
/*Helper function for the Cairo blake2s() implementation.
Computes the blake2s compress function and fills the value in the right position.
output_ptr should point to the middle of an instance, right after initial_state, message, t, f,
Expand Down Expand Up @@ -184,31 +181,24 @@ pub fn blake2s_add_uint256(
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
let low_addr = get_relocatable_from_var_name("low", vm, ids_data, ap_tracking)?;
let high_addr = get_relocatable_from_var_name("high", vm, ids_data, ap_tracking)?;
let low = vm.get_integer(low_addr)?.into_owned();
let high = vm.get_integer(high_addr)?.into_owned();
let mut low = vm.get_integer(low_addr)?.into_owned();
let mut high = vm.get_integer(high_addr)?.into_owned();
//Main logic
//Declare constant
const MASK: u32 = u32::MAX;
const B: u32 = 32;
//Convert MASK to felt
let mask = Felt252::from(MASK);
//Build first batch of data
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((low >> (B * i) as usize) & mask);
let b = &Felt252::TWO.pow(32_u8).try_into().unwrap();
let mut data = Vec::<MaybeRelocatable>::with_capacity(8);
for _ in 0..4 {
let (q, r) = low.div_rem(b);
data.push(r.into());
low = q;
}
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((high >> (B * i) as usize) & mask);
for _ in 0..4 {
let (q, r) = high.div_rem(b);
data.push(r.into());
high = q;
}
//Insert second batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data((data_ptr + 4)?, &data)
.map_err(HintError::Memory)?;
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
Ok(())
}

Expand All @@ -227,31 +217,27 @@ pub fn blake2s_add_uint256_bigend(
let data_ptr = get_ptr_from_var_name("data", vm, ids_data, ap_tracking)?;
let low_addr = get_relocatable_from_var_name("low", vm, ids_data, ap_tracking)?;
let high_addr = get_relocatable_from_var_name("high", vm, ids_data, ap_tracking)?;
let low = vm.get_integer(low_addr)?.into_owned();
let high = vm.get_integer(high_addr)?.into_owned();
let mut low = vm.get_integer(low_addr)?.into_owned();
let mut high = vm.get_integer(high_addr)?.into_owned();
//Main logic
//Declare constant
const MASK: u32 = u32::MAX;
const B: u32 = 32;
//Convert MASK to felt
let mask = Felt252::from(MASK);
let b = &Felt252::TWO.pow(32_u8).try_into().unwrap();
let mut data = Vec::<MaybeRelocatable>::with_capacity(8);
//Build first batch of data
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((high >> (B * (3 - i)) as usize) & mask);
for _ in 0..4 {
let (q, r) = low.div_rem(b);
data.push(r.into());
low = q;
}
//Insert first batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
//Build second batch of data
let mut inner_data = Vec::<Felt252>::new();
for i in 0..4 {
inner_data.push((low >> (B * (3 - i)) as usize) & mask);
for _ in 0..4 {
let (q, r) = high.div_rem(b);
data.push(r.into());
high = q;
}
//Reverse to make big-endian
data.reverse();
//Insert second batch of data
let data = get_maybe_relocatable_array_from_felt(&inner_data);
vm.load_data((data_ptr + 4)?, &data)
.map_err(HintError::Memory)?;
vm.load_data(data_ptr, &data).map_err(HintError::Memory)?;
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,16 @@ pub fn keccak_write_args(

let low = get_integer_from_var_name("low", vm, ids_data, ap_tracking)?;
let high = get_integer_from_var_name("high", vm, ids_data, ap_tracking)?;
let low = low.as_ref();
let high = high.as_ref();

let low_args = [low & Felt252::from(u64::MAX), low >> 64_usize];
let high_args = [high & Felt252::from(u64::MAX), high >> 64_usize];
let bound = Felt252::TWO.pow(64_u32).try_into().unwrap();
let (d1, d0) = low.div_rem(&bound);
let (d3, d2) = high.div_rem(&bound);
let args: Vec<_> = [d0, d1, d2, d3]
.into_iter()
.map(MaybeRelocatable::from)
.collect();

let low_args: Vec<_> = low_args.into_iter().map(MaybeRelocatable::from).collect();
vm.write_arg(inputs_ptr, &low_args)
.map_err(HintError::Memory)?;

let high_args: Vec<_> = high_args.into_iter().map(MaybeRelocatable::from).collect();
vm.write_arg((inputs_ptr + 2_i32)?, &high_args)
.map_err(HintError::Memory)?;
vm.write_arg(inputs_ptr, &args)?;

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ mod tests {
vm::vm_core::VirtualMachine,
};
use assert_matches::assert_matches;
use num_traits::Zero;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
Expand Down
7 changes: 3 additions & 4 deletions vm/src/hint_processor/builtin_hint_processor/garaga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub fn get_felt_bitlenght(
ap_tracking: &ApTracking,
) -> Result<(), HintError> {
let x = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?;
let bit_length = x.bits() as usize;
insert_value_from_var_name("bit_length", bit_length, vm, ids_data, ap_tracking)
insert_value_from_var_name("bit_length", x.bits(), vm, ids_data, ap_tracking)
}

#[cfg(test)]
Expand Down Expand Up @@ -65,8 +64,8 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_in_range() {
for i in 0..252_usize {
let x: Felt252 = Felt252::ONE << i;
for i in 0..252_u32 {
let x: Felt252 = Felt252::TWO.pow(i);

let bit_length_result = run_hint(x);
assert!(bit_length_result.is_ok());
Expand Down
20 changes: 6 additions & 14 deletions vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ pub fn unsafe_keccak(
let word = vm.get_integer(word_addr)?;
let n_bytes = cmp::min(16, u64_length - byte_i);

if word.as_ref() < Felt252::ZERO.as_ref()
|| word.as_ref() >= &(Felt252::ONE << (8 * (n_bytes as usize)))
{
if *word >= Felt252::TWO.pow(8 * n_bytes) {
return Err(HintError::InvalidWordSize(Box::new(word.into_owned())));
}

Expand Down Expand Up @@ -192,10 +190,8 @@ pub fn split_output(
num: u32,
) -> Result<(), HintError> {
let output_name = format!("output{}", num);
let output_cow = get_integer_from_var_name(&output_name, vm, ids_data, ap_tracking)?;
let output = output_cow.as_ref();
let low = output & Felt252::from(u128::MAX);
let high = output >> 128_usize;
let output = get_integer_from_var_name(&output_name, vm, ids_data, ap_tracking)?;
let (high, low) = output.div_rem(&Felt252::TWO.pow(128_u32).try_into().unwrap());
insert_value_from_var_name(
&format!("output{}_high", num),
high,
Expand All @@ -222,9 +218,7 @@ pub fn split_input(
) -> Result<(), HintError> {
let inputs_ptr = get_ptr_from_var_name("inputs", vm, ids_data, ap_tracking)?;
let binding = vm.get_integer((inputs_ptr + input_key)?)?;
let input = binding.as_ref();
let low = input & ((Felt252::ONE << (8 * exponent) as usize) - 1u64);
let high = input >> (8 * exponent) as usize;
let (high, low) = binding.div_rem(&Felt252::TWO.pow(8 * exponent).try_into().unwrap());
insert_value_from_var_name(
&format!("high{}", input_key),
high,
Expand Down Expand Up @@ -279,10 +273,8 @@ pub fn split_output_mid_low_high(
) -> Result<(), HintError> {
let binding = get_integer_from_var_name("output1", vm, ids_data, ap_tracking)?;
let output1 = binding.as_ref();
let output1_low = output1 & Felt252::from((1u64 << (8 * 7)) - 1u64);
let tmp = output1 >> (8_usize * 7);
let output1_high = &tmp >> 128_usize;
let output1_mid = tmp & Felt252::from(u128::MAX);
let (tmp, output1_low) = output1.div_rem(&Felt252::TWO.pow(8_u32 * 7_u32).try_into().unwrap());
let (output1_high, output1_mid) = tmp.div_rem(&Felt252::TWO.pow(128_u64).try_into().unwrap());
insert_value_from_var_name("output1_high", output1_high, vm, ids_data, ap_tracking)?;
insert_value_from_var_name("output1_mid", output1_mid, vm, ids_data, ap_tracking)?;
insert_value_from_var_name("output1_low", output1_low, vm, ids_data, ap_tracking)
Expand Down
Loading

0 comments on commit 14ac53f

Please sign in to comment.