Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uint: Fix overflowing_neg by implementing two's complement (#611)
* uint: Fix overflowing_neg with two's complement The operation `overflowing_neg` on the primitive integer types in the Rust standard library computes the negation of the integer value using two's complement, i.e., it returns `!self + 1`. The previous implementation of the uint library implemented `overflowing_neg` using `!self` for non-zero values which is bit-wise negation (NOT). This lead to behavior where 0 - 1 != -1 for U256 with the `overflowing_neg` and `overflow_sub` operations. This patch adapts the `uint_overflowing_binop` macro to implement the two's complement correctly: Starting from the least significant word we apply `u64::overflowing_neg` until we have seen the first one-bit in the original integer, i.e., until `overflowing_neg` reports an overflow. Then we use bit-wise NOT for the remaining words. * Update uint/src/uint.rs * Update uint/src/uint.rs Co-authored-by: Andronik <[email protected]>
- Loading branch information