From 4660d3763433218a0ed2dd178ab47d1c6418409b Mon Sep 17 00:00:00 2001 From: Rog3rSm1th Date: Tue, 11 Jun 2024 11:58:42 +0200 Subject: [PATCH 1/2] Truncate the user-defined types in decompiler output --- lib/src/decompiler/decompiler.rs | 18 ++++++++++++----- lib/src/decompiler/libfuncs_patterns.rs | 5 +++++ lib/src/decompiler/utils.rs | 26 +++++++++++++++++-------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/lib/src/decompiler/decompiler.rs b/lib/src/decompiler/decompiler.rs index 29c2082..9db9426 100644 --- a/lib/src/decompiler/decompiler.rs +++ b/lib/src/decompiler/decompiler.rs @@ -12,7 +12,7 @@ use crate::decompiler::cfg::EdgeType; use crate::decompiler::function::Function; use crate::decompiler::function::SierraStatement; use crate::decompiler::libfuncs_patterns::IS_ZERO_REGEX; -use crate::decompiler::utils::decrypt_user_defined_type_id; +use crate::decompiler::utils::decode_user_defined_type_id; use crate::decompiler::utils::replace_types_id; use crate::graph::callgraph::process_callgraph; use crate::parse_element_name; @@ -117,11 +117,19 @@ impl<'a> Decompiler<'a> { if let Some(name) = &t.debug_name { format!("ut@{}", name) } - // use id - // Convert it to string if possible - // User defined typed ids are the 250 first bits of the id name Keccak hash + // use ID else { - decrypt_user_defined_type_id(t.id.clone()) + // We first format as ut@[\[[0-9]+\])").unwrap(); + // User-defined type ID + // User defined types IDs are the 250 first bits of the id name Keccak hash + // https://github.com/starkware-libs/cairo/blob/b29f639c2090822914f52db6696d71748a8b93a6/crates/cairo-lang-sierra/src/ids.rs#L118 + pub static ref USER_DEFINED_TYPE_ID_REGEX: Regex = Regex::new(r"ut@\[(?[0-9]+)\]").unwrap(); + /// Irrelevant callgraph functions regexes pub static ref IRRELEVANT_CALLGRAPH_FUNCTIONS_REGEXES: Vec = { let mut regexes = vec![ diff --git a/lib/src/decompiler/utils.rs b/lib/src/decompiler/utils.rs index a2941c0..a476316 100644 --- a/lib/src/decompiler/utils.rs +++ b/lib/src/decompiler/utils.rs @@ -1,7 +1,8 @@ -use num_bigint::{BigInt, BigUint}; +use num_bigint::BigInt; use std::str; use crate::decompiler::libfuncs_patterns::TYPE_ID_REGEX; +use crate::decompiler::libfuncs_patterns::USER_DEFINED_TYPE_ID_REGEX; /// Convert an integer to it's string value or hex value /// Used to decode consts @@ -59,11 +60,20 @@ pub fn replace_types_id(declared_types_names: &Vec, invocation: &str) -> .to_string() } -/// Decrypts a user-defined type ID and returns the corresponding type name -/// If the type ID is not found, returns the string "ut@[]" where is the integer value of the type ID -pub fn decrypt_user_defined_type_id(type_id: BigUint) -> String { - // TODO: Implement the decryption logic here - - // For now, return the string "ut@[]" where is the integer value of the type ID - format!("ut@[{}]", type_id) +/// "Decode" (simplify) a user-defined type ID by truncating it to the 4th character +/// or return the type_id if it does not match the USER_DEFINED_TYPE_ID_REGEX regex pattern +pub fn decode_user_defined_type_id(type_id: String) -> String { + if let Some(captures) = USER_DEFINED_TYPE_ID_REGEX.captures(&type_id) { + // If the type ID matches the regex pattern, truncate it to the 4th character + if let Some(type_id_match) = captures.name("type_id") { + let truncated_type_id = &type_id_match.as_str()[..4]; + format!("ut@[{}...]", truncated_type_id) + } else { + // If the type ID does not match the regex pattern, return the original input string + type_id + } + } else { + // If the input string does not match the regex pattern, return the original input string + type_id + } } From 74e9560857292c8ae7965cbaa6a23c9dc0868044 Mon Sep 17 00:00:00 2001 From: Rog3rSm1th Date: Tue, 11 Jun 2024 13:58:42 +0200 Subject: [PATCH 2/2] Various optimizations --- Cargo.toml | 8 +++++++- lib/src/decompiler/decompiler.rs | 1 + lib/src/decompiler/libfuncs_patterns.rs | 1 - lib/src/decompiler/utils.rs | 1 + 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 484c1fe..31ce4ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,4 +2,10 @@ resolver = "2" members = [ "sierra-decompiler", -] \ No newline at end of file +] + +[profile.dev] +opt-level = 0 + +[profile.release] +opt-level = 3 diff --git a/lib/src/decompiler/decompiler.rs b/lib/src/decompiler/decompiler.rs index 9db9426..e9814a2 100644 --- a/lib/src/decompiler/decompiler.rs +++ b/lib/src/decompiler/decompiler.rs @@ -630,6 +630,7 @@ impl<'a> Decompiler<'a> { } /// Generate a callgraph representation in DOT Format + #[inline] pub fn generate_callgraph(&mut self) -> String { process_callgraph(&self.functions) } diff --git a/lib/src/decompiler/libfuncs_patterns.rs b/lib/src/decompiler/libfuncs_patterns.rs index e8eddef..4bf2fac 100644 --- a/lib/src/decompiler/libfuncs_patterns.rs +++ b/lib/src/decompiler/libfuncs_patterns.rs @@ -55,7 +55,6 @@ lazy_static! { // Used to match and replace them in remote contracts pub static ref TYPE_ID_REGEX: Regex = Regex::new(r"(?\[[0-9]+\])").unwrap(); - // User-defined type ID // User defined types IDs are the 250 first bits of the id name Keccak hash // https://github.com/starkware-libs/cairo/blob/b29f639c2090822914f52db6696d71748a8b93a6/crates/cairo-lang-sierra/src/ids.rs#L118 pub static ref USER_DEFINED_TYPE_ID_REGEX: Regex = Regex::new(r"ut@\[(?[0-9]+)\]").unwrap(); diff --git a/lib/src/decompiler/utils.rs b/lib/src/decompiler/utils.rs index a476316..46e46b8 100644 --- a/lib/src/decompiler/utils.rs +++ b/lib/src/decompiler/utils.rs @@ -6,6 +6,7 @@ use crate::decompiler::libfuncs_patterns::USER_DEFINED_TYPE_ID_REGEX; /// Convert an integer to it's string value or hex value /// Used to decode consts +#[inline] pub fn decode_hex_bigint(bigint: &BigInt) -> Option { // Convert the BigInt to a hexadecimal string let hex_string = format!("{:x}", bigint);