Skip to content

Commit 86d1e7d

Browse files
committed
Fix suggestions
1 parent 5157dd6 commit 86d1e7d

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

learn_evm/arithmetic-checks.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ This is equivalent, because `type(uint256).max` is a 256-bit integer with all it
8080
Subtracting `b` from `type(uint256).max` can be viewed as inverting each bit in `b`.
8181
This can be demonstrated by the transformation `~b = ~(0 ^ b) = ~0 ^ b = MAX ^ b = MAX - b`.
8282

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`.
8484
> We also obtain the relation `~b + 1 = 0 - b = -b` if we add `1` mod `2**256` to both sides of the previous equation.
8585
8686
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) {
218218
}
219219
```
220220

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.
223223

224224
```solidity
225225
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) {
384384
}
385385
```
386386

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`.
388388
The actual case, where the product `c` will overflow, is limited to `a == -1` and `b == type(int256).min`.
389389
This is because `-b` cannot be represented as a positive signed integer, as previously mentioned.
390390

@@ -422,11 +422,11 @@ For example, when performing addition without knowing what the upper bits are se
422422
= 0x????????????????????????????????????????????????0000000000000001 // int64(1)
423423
```
424424

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.
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.
430430

431431
When performing arithmetic checks in the same way as before, it is necessary to include a step to clean the bits on the sum.
432432
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

Comments
 (0)