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

Fix deserialization of scientific notation with fractional values #1202

Merged
merged 5 commits into from
Jun 5, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* bugfix: Fix deserialization of scientific notation with fractional values [#1202](https://github.com/lambdaclass/cairo-rs/pull/1202)

* feat: implement `mem_eq` function to test for equality of two ranges in memory [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)
* perf: use `mem_eq` in `set_add` [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198)

Expand Down
44 changes: 37 additions & 7 deletions src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,17 @@ where
}

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()?;
match n.as_f64() {
None => {
let str = n.to_string();
let list: [&str; 2] = str.split('e').collect::<Vec<&str>>().try_into().ok()?;

let result = base * Felt252::from(10).pow(exponent);
Some(result)
let exponent = list[1].parse::<u32>().ok()?;
let base = Felt252::parse_bytes(list[0].to_string().as_bytes(), 10)?;
Some(base * Felt252::from(10).pow(exponent))
}
Some(float) => Felt252::parse_bytes(float.round().to_string().as_bytes(), 10),
}
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -1399,4 +1402,31 @@ mod tests {
Ok(x) if x == Some(Felt252::one() * Felt252::from(10).pow(27))
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_felt_from_number_with_scientific_notation_with_fractional_part() {
let n = serde_json::Value::Number(Number::from_f64(64e+74).unwrap());

assert_matches!(
felt_from_number(n),
Ok(x) if x == Some(Felt252::from_str_radix("64", 10).unwrap() * Felt252::from(10).pow(74))
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_felt_from_number_with_scientific_notation_with_fractional_part_f64_max() {
let n = serde_json::Value::Number(Number::from_f64(f64::MAX).unwrap());
assert_eq!(
felt_from_number(n).unwrap(),
Some(
Felt252::from_str_radix(
"2082797363194934431336897723140298717588791783575467744530053896730196177808",
10
)
.unwrap()
)
);
}
}