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.
I also have a proof of correctness here:
Let +, -, * be the usual arithemetic operators on the integers, and / be integer division.
Let the integers in the range [-2^255, 2^255-1] be the space of int256s
Given int256s a and b, define a OP b overflows iff a OP b is not in int256s
Let ⊕, ⊖, ⊗, ⊘ be corresponding operators on int256s, where WLOG for all operators, a ⊕ b is equivalent to a + b (mod 2^256)
Note that WLOG for all operators, a + b == a ⊕ b iff a + b doesn't overflow.
Assume b < 0
Assume b >= 0
(b < 0 and a ⊕ b < a) or (b >= 0 and a ⊕ b >= a) implies a + b doesn't overflow
For integers a and b where b != 0, abs(a / b) == abs(a) / abs(b)
Assume int256s a, b where b != 0
abs(b) >= 1
Assume a != -2^255
Assume a == -2^255
a / b doesn't overflow (a / b == a ⊘ b) iff a != -2^255 or b != -1
Assume a == 0
Assume a != 0
a * b doesn't overflow (a * b == a ⊗ b) iff (a != -1 or b != -2^255) and (a == 0 or a ⊗ b ⊘ a == b)