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

Use from_str_radix instead of from_bytes_be #692

Merged
merged 2 commits into from
Jan 16, 2023
Merged
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
19 changes: 5 additions & 14 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
},
};
use felt::{Felt, FeltOps, PRIME_STR};
use num_traits::Num;
use serde::{de, de::MapAccess, de::SeqAccess, Deserialize, Deserializer};
use serde_json::Number;
use std::{collections::HashMap, fmt, io::Read};
Expand Down Expand Up @@ -191,12 +192,7 @@ impl<'de> de::Visitor<'de> for FeltVisitor {
if let Some(no_prefix_hex) = value.strip_prefix("0x") {
// Add padding if necessary
let no_prefix_hex = deserialize_utils::maybe_add_padding(no_prefix_hex.to_string());
let decoded_result: Result<Vec<u8>, hex::FromHexError> = hex::decode(no_prefix_hex);

match decoded_result {
Ok(decoded_hex) => Ok(Felt::from_bytes_be(&decoded_hex)),
Err(e) => Err(e).map_err(de::Error::custom),
}
Ok(Felt::from_str_radix(&no_prefix_hex, 16).map_err(de::Error::custom)?)
} else {
Err(String::from("hex prefix error")).map_err(de::Error::custom)
}
Expand All @@ -222,14 +218,9 @@ impl<'de> de::Visitor<'de> for MaybeRelocatableVisitor {
if let Some(no_prefix_hex) = value.strip_prefix("0x") {
// Add padding if necessary
let no_prefix_hex = deserialize_utils::maybe_add_padding(no_prefix_hex.to_string());
let decoded_result: Result<Vec<u8>, hex::FromHexError> = hex::decode(no_prefix_hex);

match decoded_result {
Ok(decoded_hex) => {
data.push(MaybeRelocatable::Int(Felt::from_bytes_be(&decoded_hex)))
}
Err(e) => return Err(e).map_err(de::Error::custom),
};
data.push(MaybeRelocatable::Int(
Felt::from_str_radix(&no_prefix_hex, 16).map_err(de::Error::custom)?,
));
} else {
return Err(String::from("hex prefix error")).map_err(de::Error::custom);
};
Expand Down