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

Handle scientific notation in fn felt_from_number #1188

Merged
merged 8 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Cairo-VM Changelog

#### Upcoming Changes
* fix: Handle the deserialization of serde_json::Number with scientific notation (e.g.: Number(1e27)) in felt_from_number function [#1188](https://github.com/lambdaclass/cairo-rs/pull/1188)

* Add `CairoRunner::run_until_pc_with_steps_limit method` [#1181](https://github.com/lambdaclass/cairo-rs/pull/1181)

Expand Down
38 changes: 36 additions & 2 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::{
},
};
use felt::{Felt252, PRIME_STR};
use num_traits::Num;
use num_traits::{Num, Pow};
use serde::{de, de::MapAccess, de::SeqAccess, Deserialize, Deserializer, Serialize};
use serde_json::Number;

Expand Down Expand Up @@ -167,7 +167,27 @@ where
let n = Number::deserialize(deserializer)?;
match Felt252::parse_bytes(n.to_string().as_bytes(), 10) {
Some(x) => Ok(Some(x)),
None => Err(String::from("felt_from_number parse error")).map_err(de::Error::custom),
None => {
// Handle de Number with scientific notation cases
// e.g.: Number(1e27)
let str = n.to_string();
let list: [&str; 2] = str
.split('e')
.collect::<Vec<&str>>()
.try_into()
.map_err(|_| String::from("felt_from_number parse error"))
.map_err(de::Error::custom)?;

let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10).ok_or(
de::Error::custom(String::from("felt_from_number parse error")),
)?;
let exponent = list[1]
.parse::<u32>()
.map_err(|_| de::Error::custom(String::from("felt_from_number parse error")))?;
fmoletta marked this conversation as resolved.
Show resolved Hide resolved

let result = base * Felt252::from(10).pow(exponent);
Ok(Some(result))
}
}
}

Expand Down Expand Up @@ -421,6 +441,7 @@ mod tests {
use super::*;
use assert_matches::assert_matches;
use felt::felt_str;
use num_traits::One;
use num_traits::Zero;

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -1363,4 +1384,17 @@ mod tests {
let iden: Result<Identifier, serde_json::Error> = serde_json::from_str(valid_json);
assert!(iden.err().is_some());
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_felt_from_number_with_scientific_notation() {
let n = Number::deserialize(serde_json::Value::from(1000000000000000000000000000_u128))
.unwrap();
assert_eq!(n.to_string(), "1e27".to_owned());

assert_matches!(
felt_from_number(n),
Ok(x) if x == Some(Felt252::one() * Felt252::from(10).pow(27))
);
}
}