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 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 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)

* bugfix: Use cairo constants in `ASSERT_250_BIT` hint [#1187](https://github.com/lambdaclass/cairo-rs/pull/1187)

Expand Down
40 changes: 38 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 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,10 +167,32 @@
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.: n = Number(1e27)
let felt = deserialize_scientific_notation(n);
if felt.is_some() {
return Ok(felt);
}

Err(de::Error::custom(String::from(
"felt_from_number parse error",
)))

Check warning on line 180 in src/serde/deserialize_program.rs

View check run for this annotation

Codecov / codecov/patch

src/serde/deserialize_program.rs#L176-L180

Added lines #L176 - L180 were not covered by tests
}
}
}

fn deserialize_scientific_notation(n: Number) -> Option<Felt252> {
let str = n.to_string();
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;

let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
let exponent = list[1].parse::<u32>().ok()?;

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

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct ReferenceManager {
pub references: Vec<Reference>,
Expand Down Expand Up @@ -421,6 +443,7 @@
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 +1386,17 @@
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))
);
}
}
Loading