Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ark-starknet): add from_hex_be fn to CairoU256 #144

Merged
merged 3 commits into from
Oct 15, 2023
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/ark-starknet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ url = "2.3.1"
regex = "1.9.1"
mockall = "0.11.2"
num-bigint = { version = "0.4.3", default-features = false }
num-traits = "0.2"

[features]
mock = []
89 changes: 89 additions & 0 deletions crates/ark-starknet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod client;
pub mod format;

use anyhow::Result;
use format::to_hex_str;
use num_bigint::BigUint;
use num_traits::Num;

#[derive(Debug, Clone)]
pub struct CairoU256 {
Expand Down Expand Up @@ -37,4 +39,91 @@ impl CairoU256 {
token_id_str
}
}

pub fn from_hex_be(value: &str) -> Result<Self> {
// Remove the "0x" prefix if it exists
let value = value.strip_prefix("0x").unwrap_or(value);

// Parse the hexadecimal string into a BigUint
let biguint = match BigUint::from_str_radix(value, 16) {
Ok(b) => b,
Err(_) => return Err(anyhow::anyhow!("Invalid hexadecimal string")),
};

// Convert the BigUint to a 32-byte buffer
let mut bytes = biguint.to_bytes_be();
let padding = vec![0; 32 - bytes.len()];
bytes.splice(0..0, padding);

// Split the input array into two parts
let (high, low) = bytes.split_at(16);

let low = u128::from_be_bytes(low.try_into()?);
let high = u128::from_be_bytes(high.try_into()?);

Ok(Self { low, high })
}
}

#[cfg(test)]
mod tests {
use num_traits::Num;

use super::*;

#[test]
fn test_to_biguint() {
let u256 = CairoU256 { low: 15, high: 0 };

let result = u256.to_biguint();
assert_eq!(result, BigUint::from_str_radix("15", 10).unwrap());
}

#[test]
fn test_to_hex() {
let u256 = CairoU256 { low: 15, high: 0 };

let expected_hex = "0x000000000000000000000000000000000000000000000000000000000000000f";
let result = u256.to_hex();
assert_eq!(result, expected_hex);
}

#[test]
fn test_to_decimal() {
let u256 = CairoU256 { low: 15, high: 0 };

let expected_decimal = "15";
let result = u256.to_decimal(false);
assert_eq!(result, expected_decimal);

let expected_padded_decimal =
"000000000000000000000000000000000000000000000000000000000000000000000000000015";
let result_padded = u256.to_decimal(true);
assert_eq!(result_padded, expected_padded_decimal);
}

#[test]
fn test_from_hex_be() {
let hex_string = "0x000000000000000000000000000000000000000000000000000000000000000f";
let u256 = CairoU256::from_hex_be(hex_string).unwrap();

assert_eq!(u256.low, 15);
assert_eq!(u256.high, 0);

// Test with invalid hex string
let invalid_hex_string = "invalidhex";
assert!(CairoU256::from_hex_be(invalid_hex_string).is_err());

let hex_string = "0x05f7cd1fd465baff2ba9d2d1501ad0a2eb5337d9a885be319366b5205a414fdd";
let u256 = CairoU256::from_hex_be(hex_string).unwrap();

assert_eq!(
u256.low,
u128::from_str_radix("eb5337d9a885be319366b5205a414fdd", 16).unwrap()
);
assert_eq!(
u256.high,
u128::from_str_radix("05f7cd1fd465baff2ba9d2d1501ad0a2", 16).unwrap()
);
}
}