This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Use sensible maths for from_rational
#13660
Merged
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c4a1afc
Use sensible maths for from_rational
gavofyork 7169dad
Fixes
gavofyork 4339ba6
Fixes
gavofyork 738745f
More fixes
gavofyork 8ec5fdc
Remove debugging
gavofyork a74d180
Add fuzzer tests
ggwpez 7e88ac7
Prevent panics
ggwpez 6bba13e
docs
ggwpez a836b85
Clean up old code
ggwpez 4779574
Test all rounding modes of from_rational
ggwpez a0ca0d9
Clean up code
ggwpez 5796b30
Revert "Prevent panics"
ggwpez 8ea9ab8
fix imports
ggwpez b9120f8
cleanup
ggwpez 7d27d29
Fuzz test multiply_rational
ggwpez d38cd82
Fix import
ggwpez 78c9ca3
fmt
ggwpez 223e9d5
Return None in multiply_rational on zero div
ggwpez 62d0fd2
Merge remote-tracking branch 'origin/master' into gav-fix-from-rational
ggwpez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
primitives/arithmetic/fuzzer/src/per_thing_from_rational.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # Running | ||
| //! Running this fuzzer can be done with `cargo hfuzz run per_thing_rational`. `honggfuzz` CLI | ||
| //! options can be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads. | ||
| //! | ||
| //! # Debugging a panic | ||
| //! Once a panic is found, it can be debugged with | ||
| //! `cargo hfuzz run-debug per_thing_rational hfuzz_workspace/per_thing_rational/*.fuzz`. | ||
|
|
||
| use fraction::prelude::BigFraction as Fraction; | ||
| use honggfuzz::fuzz; | ||
| use sp_arithmetic::{traits::SaturatedConversion, PerThing, Perbill, Percent, Perquintill, *}; | ||
|
|
||
| /// Tries to demonstrate that `from_rational` is incorrect. | ||
| /// | ||
| /// NOTE: This `Fraction` library is really slow. Using f128/f256 does not work for the large | ||
| /// numbers. But an optimization could be done do use either floats or Fraction depending on the | ||
| /// size of the inputs. | ||
| fn main() { | ||
| loop { | ||
| fuzz!(|data: (u128, u128)| { | ||
| let (n, d) = (data.0.min(data.1), data.0.max(data.1).max(1)); | ||
|
|
||
| check::<PerU16>(n, d); | ||
| check::<Percent>(n, d); | ||
| check::<Perbill>(n, d); | ||
| check::<Perquintill>(n, d); | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Assert that the parts of `from_rational` are correct. | ||
| fn check<Per: PerThing>(a: u128, b: u128) | ||
| where | ||
| Per::Inner: Into<u128>, | ||
| { | ||
| let approx_ratio = Per::from_rational(a, b); // This rounds down. | ||
| let approx_parts = Fraction::from(approx_ratio.deconstruct().saturated_into::<u128>()); | ||
|
|
||
| let perfect_ratio = if a == 0 && b == 0 { | ||
| Fraction::from(1) | ||
| } else { | ||
| Fraction::from(a) / Fraction::from(b.max(1)) | ||
| }; | ||
| let perfect_parts = (perfect_ratio * Fraction::from(Per::ACCURACY.into())).floor(); | ||
|
|
||
| assert_eq!( | ||
| approx_parts, perfect_parts, | ||
| "approx_parts: {}, perfect_parts: {}, a: {}, b: {}", | ||
| approx_parts, perfect_parts, a, b | ||
| ); | ||
| } |
69 changes: 69 additions & 0 deletions
69
primitives/arithmetic/fuzzer/src/per_thing_mult_fraction.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! # Running | ||
| //! Running this fuzzer can be done with `cargo hfuzz run per_thing_rational`. `honggfuzz` CLI | ||
| //! options can be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads. | ||
| //! | ||
| //! # Debugging a panic | ||
| //! Once a panic is found, it can be debugged with | ||
| //! `cargo hfuzz run-debug per_thing_rational hfuzz_workspace/per_thing_rational/*.fuzz`. | ||
|
|
||
| use honggfuzz::fuzz; | ||
| use sp_arithmetic::{PerThing, Perbill, Percent, Perquintill, *}; | ||
|
|
||
| /// Tries to disprove `(n / d) * d <= n` for any `PerThing`s. | ||
| fn main() { | ||
| loop { | ||
| fuzz!(|data: (u128, u128)| { | ||
| let (n, d) = (data.0.min(data.1), data.0.max(data.1).max(1)); | ||
|
|
||
| check_mul::<PerU16>(n, d); | ||
| check_mul::<Percent>(n, d); | ||
| check_mul::<Perbill>(n, d); | ||
| check_mul::<Perquintill>(n, d); | ||
|
|
||
| check_reciprocal_mul::<PerU16>(n, d); | ||
| check_reciprocal_mul::<Percent>(n, d); | ||
| check_reciprocal_mul::<Perbill>(n, d); | ||
| check_reciprocal_mul::<Perquintill>(n, d); | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// Checks that `(n / d) * d <= n`. | ||
| fn check_mul<P: PerThing>(n: u128, d: u128) | ||
| where | ||
| P: PerThing + core::ops::Mul<u128, Output = u128>, | ||
| { | ||
| let q = P::from_rational_with_rounding(n, d, Rounding::Down).unwrap(); | ||
| assert!(q * d <= n, "{:?} * {:?} <= {:?}", q, d, n); | ||
| } | ||
|
|
||
| /// Checks that `n / (n / d) >= d`. FAIL-CI is this intended? | ||
| fn check_reciprocal_mul<P: PerThing>(n: u128, d: u128) | ||
| where | ||
| P: PerThing + core::ops::Mul<u128, Output = u128>, | ||
| { | ||
| let q = P::from_rational_with_rounding(n, d, Rounding::Down).unwrap(); | ||
| if q.is_zero() { | ||
| return | ||
| } | ||
|
|
||
| let r = q.saturating_reciprocal_mul(n); | ||
| assert!(r >= d, "{} / ({} / {}) != {} but {}", n, n, d, d, r); | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.