From e629428182c6da751d251814f8357ad92c837e1a Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Tue, 16 Apr 2024 15:00:45 -0300 Subject: [PATCH] Bump `starknet-types-core` version + Use the lib's pedersen hash (#1692) * Bump core types and use its pedersen hash func * Add Changelog entry * Fix param order --------- Co-authored-by: Mario Rugiero --- CHANGELOG.md | 2 ++ Cargo.lock | 13 ++++++------- vm/Cargo.toml | 2 +- vm/src/vm/runners/builtin_runner/hash.rs | 19 ++----------------- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fbaa1b980..a9cc5a97e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* Bump `starknet-types-core` version + Use the lib's pedersen hash [#1692](https://github.com/lambdaclass/cairo-vm/pull/1692) + * refactor: Remove unused code & use constants whenever possible for builtin instance definitions[#1707](https://github.com/lambdaclass/cairo-vm/pull/1707) * feat: missing EC hints for Starknet OS 0.13.1 [#1706](https://github.com/lambdaclass/cairo-vm/pull/1706) diff --git a/Cargo.lock b/Cargo.lock index d51294e05e..499b5fe6c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1892,9 +1892,9 @@ dependencies = [ [[package]] name = "lambdaworks-crypto" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d4c222d5b2fdc0faf702d3ab361d14589b097f40eac9dc550e27083483edc65" +checksum = "458fee521f12d0aa97a2e06eaf134398a5d2ae7b2074af77eb402b0d93138c47" dependencies = [ "lambdaworks-math", "serde", @@ -1904,9 +1904,9 @@ dependencies = [ [[package]] name = "lambdaworks-math" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ee7dcab3968c71896b8ee4dc829147acc918cffe897af6265b1894527fe3add" +checksum = "6c74ce6f0d9cb672330b6ca59e85a6c3607a3329e0372ab0d3fe38c2d38e50f9" dependencies = [ "serde", "serde_json", @@ -3045,15 +3045,14 @@ dependencies = [ [[package]] name = "starknet-types-core" -version = "0.0.9" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d53160556d1f23425100f42b3230df747ea05763efee685a2cd939dfb640701" +checksum = "1051b4f4af0bb9b546388a404873ee1e6b9787b9d5b0b3319ecbfadf315ef276" dependencies = [ "arbitrary", "bitvec", "lambdaworks-crypto", "lambdaworks-math", - "lazy_static", "num-bigint", "num-integer", "num-traits 0.2.18", diff --git a/vm/Cargo.toml b/vm/Cargo.toml index d1b25dace9..e04782221e 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -63,7 +63,7 @@ keccak = { workspace = true } hashbrown = { workspace = true } anyhow = { workspace = true } thiserror-no-std = { workspace = true } -starknet-types-core = { version = "0.0.9", default-features = false, features = ["serde", "curve", "num-traits"] } +starknet-types-core = { version = "0.1.0", default-features = false, features = ["serde", "curve", "num-traits", "hash"] } # only for std num-prime = { version = "0.4.3", features = ["big-int"], optional = true } diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index a6d045ade2..bd641f2690 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -1,6 +1,5 @@ use crate::air_private_input::{PrivateInput, PrivateInputPair}; use crate::stdlib::{cell::RefCell, prelude::*}; -use crate::types::errors::math_errors::MathError; use crate::types::instance_definitions::pedersen_instance_def::CELLS_PER_HASH; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::MemoryError; @@ -8,9 +7,8 @@ use crate::vm::errors::runner_errors::RunnerError; use crate::vm::runners::cairo_pie::BuiltinAdditionalData; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; -use crate::Felt252; use num_integer::{div_ceil, Integer}; -use starknet_crypto::{pedersen_hash, FieldElement}; +use starknet_types_core::hash::StarkHash; #[derive(Debug, Clone)] pub struct HashBuiltinRunner { @@ -89,21 +87,8 @@ impl HashBuiltinRunner { .resize(address.offset + 1, false); } self.verified_addresses.borrow_mut()[address.offset] = true; - - //Convert MaybeRelocatable to FieldElement - let a_be_bytes = num_a.to_bytes_be(); - let b_be_bytes = num_b.to_bytes_be(); - let (y, x) = match ( - FieldElement::from_bytes_be(&a_be_bytes), - FieldElement::from_bytes_be(&b_be_bytes), - ) { - (Ok(field_element_a), Ok(field_element_b)) => (field_element_a, field_element_b), - _ => return Err(MathError::ByteConversionError.into()), - }; //Compute pedersen Hash - let fe_result = pedersen_hash(&x, &y); - //Convert result from FieldElement to MaybeRelocatable - let result = Felt252::from_bytes_be(&fe_result.to_bytes_be()); + let result = starknet_types_core::hash::Pedersen::hash(num_b, num_a); return Ok(Some(MaybeRelocatable::from(result))); } Ok(None)