You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn_evm/arithmetic-checks.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -80,7 +80,7 @@ This is equivalent, because `type(uint256).max` is a 256-bit integer with all it
80
80
Subtracting `b` from `type(uint256).max` can be viewed as inverting each bit in `b`.
81
81
This can be demonstrated by the transformation `~b = ~(0 ^ b) = ~0 ^ b = MAX ^ b = MAX - b`.
82
82
83
-
> It's worth noting that `a - b = a ^ b` is **NOT** a general rule, except in special cases, such as when one of the values `MAX`.
83
+
> It's worth noting that `a - b = a ^ b` is **NOT** a general rule, except in special cases, such as when one of the values equals `MAX`.
84
84
> We also obtain the relation `~b + 1 = 0 - b = -b` if we add `1` mod `2**256` to both sides of the previous equation.
85
85
86
86
By computing the result of the addition first and then performing a check on the sum,
@@ -218,8 +218,8 @@ function checkedAddInt2(int256 a, int256 b) public pure returns (int256 c) {
218
218
}
219
219
```
220
220
221
-
Nevertheless, by utilizing `xor`, which is the bitwise exclusive-or operation, we can combine these checks into one.
222
-
The code is written in assembly, because Solidity lacks an `xor` operation for boolean values.
221
+
Nevertheless, using the boolean exclusive-or lets us combine these checks into one step.
222
+
Solidity doesn't have a built-in operation for boolean values, but we can still make use of it through inline-assembly. In doing so, we need to take care that both inputs are actually boolean (either 0 or 1), as the xor operation works bitwise and isn't restricted to boolean values.
223
223
224
224
```solidity
225
225
function checkedAddInt3(int256 a, int256 b) public pure returns (int256 c) {
@@ -384,7 +384,7 @@ function checkedMulInt2(int256 a, int256 b) public pure returns (int256 c) {
384
384
}
385
385
```
386
386
387
-
When it comes to integer multiplication, it's important to handle the case hen`a < 0` and `b == type(int256).min`.
387
+
When it comes to integer multiplication, it's important to handle the case when`a < 0` and `b == type(int256).min`.
388
388
The actual case, where the product `c` will overflow, is limited to `a == -1` and `b == type(int256).min`.
389
389
This is because `-b` cannot be represented as a positive signed integer, as previously mentioned.
390
390
@@ -422,11 +422,11 @@ For example, when performing addition without knowing what the upper bits are se
> It is crucial to be mindful of when to clean the bits before and after operations.
426
-
> By default, Solidity takes care of cleaning the bits before operations on smaller types and lets the optimizer remove any redundant steps.
427
-
> However, values accessed after operations included by the compiler are not guaranteed to be clean. In particular, this is the case for addition with small data types.
428
-
> The bit cleaning steps will be removed by the optimizer (even without optimizations enabled) if a variable is only accessed in a subsequent assembly block.
429
-
> Refer to the [Solidity documentation](https://docs.soliditylang.org/en/v0.8.18/internals/variable_cleanup.html#cleaning-up-variables) for further information on this matter.
425
+
It is crucial to be mindful of when to clean the bits before and after operations.
426
+
By default, Solidity takes care of cleaning the bits before operations on smaller types and lets the optimizer remove any redundant steps.
427
+
However, values accessed after operations included by the compiler are not guaranteed to be clean. In particular, this is the case for addition with small data types.
428
+
The bit cleaning steps will be removed by the optimizer (even without optimizations enabled) if a variable is only accessed in a subsequent assembly block.
429
+
Refer to the [Solidity documentation](https://docs.soliditylang.org/en/v0.8.18/internals/variable_cleanup.html#cleaning-up-variables) for further information on this matter.
430
430
431
431
When performing arithmetic checks in the same way as before, it is necessary to include a step to clean the bits on the sum.
432
432
One approach to achieve this is by performing `signextend(7, value)`, which extends the sign of a 64-bit (7 + 1 = 8 bytes) integer over all upper bits.
0 commit comments