Skip to content

Commit

Permalink
Update type-rs commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Nov 24, 2023
1 parent 74c728b commit 55eaaa7
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 72 deletions.
31 changes: 16 additions & 15 deletions Cargo.lock

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

48 changes: 20 additions & 28 deletions fuzzer/Cargo.lock

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

6 changes: 3 additions & 3 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std = [
"serde_json/std",
"bincode/std",
"anyhow/std",
"stark-felt/std",
"starknet-types-core/std",
"starknet-crypto/std",
"dep:num-prime",
"thiserror-no-std/std",
Expand All @@ -34,7 +34,7 @@ test_utils = [
] # This feature will reference every test-oriented feature
skip_next_instruction_hint = []
hooks = []
arbitrary = ["dep:arbitrary", "std", "stark-felt/arbitrary", "stark-felt/std"]
arbitrary = ["dep:arbitrary", "std", "starknet-types-core/arbitrary", "starknet-types-core/std"]

[dependencies]
mimalloc = { workspace = true, optional = true }
Expand All @@ -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 = "ea52b64a52fd832222adf400feb2dd3b4dc286a2", default-features = false, features = ["serde"] }
starknet-types-core = { git = "https://github.com/starknet-io/types-rs.git", rev = "5c77a9184aaecfaddf39c49d8bf64343fa7c0720", default-features = false, features = ["serde"] }

# only for std
num-prime = { version = "0.4.3", features = ["big-int"], optional = true }
Expand Down
8 changes: 4 additions & 4 deletions vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ pub fn unsafe_keccak(
high_bytes.extend_from_slice(&hashed[0..16]);
low_bytes.extend_from_slice(&hashed[16..32]);

let high = Felt252::from_bytes_be(&high_bytes).map_err(|_| MathError::ByteConversionError)?;
let low = Felt252::from_bytes_be(&low_bytes).map_err(|_| MathError::ByteConversionError)?;
let high = Felt252::from_bytes_be_slice(&high_bytes);
let low = Felt252::from_bytes_be_slice(&low_bytes);

vm.insert_value(high_addr, high)?;
vm.insert_value(low_addr, low)?;
Expand Down Expand Up @@ -177,8 +177,8 @@ pub fn unsafe_keccak_finalize(
let high_addr = get_relocatable_from_var_name("high", vm, ids_data, ap_tracking)?;
let low_addr = get_relocatable_from_var_name("low", vm, ids_data, ap_tracking)?;

let high = Felt252::from_bytes_be(&high_bytes).map_err(|_| MathError::ByteConversionError)?;
let low = Felt252::from_bytes_be(&low_bytes).map_err(|_| MathError::ByteConversionError)?;
let high = Felt252::from_bytes_be_slice(&high_bytes);
let low = Felt252::from_bytes_be_slice(&low_bytes);

vm.insert_value(high_addr, high)?;
vm.insert_value(low_addr, low)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn split_64(
bytes[16..24].copy_from_slice(digits[3].to_le_bytes().as_slice());

let low = Felt252::from(digits[0]);
let high = Felt252::from_bytes_le(bytes.as_slice()).expect("come on!");
let high = Felt252::from_bytes_le(&bytes);
insert_value_from_var_name("high", high, vm, ids_data, ap_tracking)?;
insert_value_from_var_name("low", low, vm, ids_data, ap_tracking)
}
Expand Down
2 changes: 1 addition & 1 deletion vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub mod utils;
pub mod vm;

// TODO: use `Felt` directly
pub use stark_felt::Felt as Felt252;
pub use starknet_types_core::felt::Felt as Felt252;

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion vm/src/math_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use num_bigint::{BigInt, BigUint, RandBigInt, ToBigInt};
use num_integer::Integer;
use num_traits::{One, Signed, Zero};
use rand::{rngs::SmallRng, SeedableRng};
use stark_felt::NonZeroFelt;
use starknet_types_core::felt::NonZeroFelt;

lazy_static! {
pub static ref SIGNED_FELT_MAX: BigUint = (&*CAIRO_PRIME).shr(1_u32);
Expand Down
2 changes: 1 addition & 1 deletion vm/src/types/relocatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ mod tests {
proptest! {
#[test]
fn add_relocatable_felt(offset in any::<u64>(), ref bigint in any::<[u8; 32]>()) {
let big = &Felt252::from_bytes_be(bigint).unwrap();
let big = &Felt252::from_bytes_be(bigint);
let rel = Relocatable::from((0, offset as usize));

let sum = (big + offset).to_usize()
Expand Down
10 changes: 4 additions & 6 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,15 @@ pub fn felt_to_bigint(felt: crate::Felt252) -> BigInt {
}

pub fn biguint_to_felt(biguint: &BigUint) -> Result<crate::Felt252, MathError> {
let mut bytes = biguint.to_bytes_le();
bytes.resize(32, 0);
crate::Felt252::from_bytes_le(&bytes).map_err(|_| MathError::ByteConversionError)
// TODO This funtions should return a Felt252 instead of a Result
Ok(crate::Felt252::from_bytes_le_slice(&biguint.to_bytes_le()))
}

pub fn bigint_to_felt(bigint: &BigInt) -> Result<crate::Felt252, MathError> {
let (sign, mut bytes) = bigint
let (sign, bytes) = bigint
.mod_floor(&CAIRO_PRIME.to_bigint().unwrap())
.to_bytes_le();
bytes.resize(32, 0);
let felt = crate::Felt252::from_bytes_le(&bytes).map_err(|_| MathError::ByteConversionError)?;
let felt = crate::Felt252::from_bytes_le_slice(&bytes);
if sign == Sign::Minus {
Ok(felt.neg())
} else {
Expand Down
6 changes: 3 additions & 3 deletions vm/src/vm/runners/builtin_runner/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ impl BitwiseBuiltinRunner {
bytes_xy[8..16].copy_from_slice(limbs_xy[1].to_le_bytes().as_slice());
bytes_xy[16..24].copy_from_slice(limbs_xy[2].to_le_bytes().as_slice());
bytes_xy[24..].copy_from_slice(limbs_xy[3].to_le_bytes().as_slice());
Ok(Some(MaybeRelocatable::from(
Felt252::from_bytes_le(&bytes_xy).unwrap(),
)))
Ok(Some(MaybeRelocatable::from(Felt252::from_bytes_le_slice(
&bytes_xy,
))))
}

pub fn get_memory_segment_addresses(&self) -> (usize, Option<usize>) {
Expand Down
4 changes: 1 addition & 3 deletions vm/src/vm/runners/builtin_runner/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ impl HashBuiltinRunner {
//Compute pedersen Hash
let fe_result = pedersen_hash(&x, &y);
//Convert result from FieldElement to MaybeRelocatable
let r_byte_slice = fe_result.to_bytes_be();
let result = Felt252::from_bytes_be(&r_byte_slice)
.map_err(|_| MathError::ByteConversionError)?;
let result = Felt252::from_bytes_be(&fe_result.to_bytes_be());
return Ok(Some(MaybeRelocatable::from(result)));
}
Ok(None)
Expand Down
3 changes: 1 addition & 2 deletions vm/src/vm/runners/builtin_runner/keccak.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::math_utils::safe_div_usize;
use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*};
use crate::types::errors::math_errors::MathError;
use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef;
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
use crate::vm::errors::memory_errors::MemoryError;
Expand Down Expand Up @@ -122,7 +121,7 @@ impl KeccakBuiltinRunner {
self.cache.borrow_mut().insert((first_output_addr + i)?, {
let mut bytes = keccak_result[start_index..end_index].to_vec();
bytes.resize(32, 0);
Felt252::from_bytes_le(&bytes).map_err(|_| MathError::ByteConversionError)?
Felt252::from_bytes_le_slice(&bytes)
});
start_index = end_index;
}
Expand Down
3 changes: 1 addition & 2 deletions vm/src/vm/runners/builtin_runner/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ impl PoseidonBuiltinRunner {
for (i, elem) in poseidon_state.iter().enumerate() {
self.cache.borrow_mut().insert(
(first_output_addr + i)?,
Felt252::from_bytes_be(&elem.to_bytes_be())
.map_err(|_| MathError::ByteConversionError)?,
Felt252::from_bytes_be(&elem.to_bytes_be()),
);
}

Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm/runners/builtin_runner/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ impl SignatureBuiltinRunner {
(
*k,
(
Felt252::from_bytes_be(&v.r.to_bytes_be()).unwrap_or_default(),
Felt252::from_bytes_be(&v.s.to_bytes_be()).unwrap_or_default(),
Felt252::from_bytes_be(&v.r.to_bytes_be()),
Felt252::from_bytes_be(&v.s.to_bytes_be()),
),
)
})
Expand Down

0 comments on commit 55eaaa7

Please sign in to comment.