-
Notifications
You must be signed in to change notification settings - Fork 262
Closed as not planned
Description
The implementation of addition and subtraction in the ModInt structure does not seem to assume
Suggested mitigation
- Addition (plan A, cast 32/64-bit integer):
- Cast
this._vandrhs._vto 64-bit integer type. - Performs addition and compares it with the value of
umod(). - If the sum is greater than
umod(), subtract the value ofumod()from the sum. - Cast the result to a 32-bit integer type.
- Cast
- Addition (plan B, 32-bit integer overflow detection):
- Performs addition and detects overflow.
- If the addition is overflowing or the sum is greater than or equal to the value of
umod(), subtractumod()from the sum.
- Subtraction:
- If
rhs._vis greater thanself._v, thenself._v - rhs._v + umod(), otherwiseself._v - rhs._v.
- If
Code
Lines 74 to 83 in 6c88a70
| mint& operator+=(const mint& rhs) { | |
| _v += rhs._v; | |
| if (_v >= umod()) _v -= umod(); | |
| return *this; | |
| } | |
| mint& operator-=(const mint& rhs) { | |
| _v -= rhs._v; | |
| if (_v >= umod()) _v += umod(); | |
| return *this; | |
| } |
Lines 191 to 200 in 6c88a70
| mint& operator+=(const mint& rhs) { | |
| _v += rhs._v; | |
| if (_v >= umod()) _v -= umod(); | |
| return *this; | |
| } | |
| mint& operator-=(const mint& rhs) { | |
| _v += mod() - rhs._v; | |
| if (_v >= umod()) _v -= umod(); | |
| return *this; | |
| } |
rust-lang-ja/ac-library-rs@ab2c3ed#diff-9c0db0574dc35afe73adabeaba95ec11f15af83bb2cf1d5d70b916b6da84e2eaR793-R812
Related Issue/PR
Metadata
Metadata
Assignees
Labels
No labels