Skip to content

Commit 3ae1b4a

Browse files
committed
Move boolToUint to SafeCast
1 parent 8417107 commit 3ae1b4a

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

contracts/utils/math/Math.sol

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pragma solidity ^0.8.20;
55

66
import {Address} from "../Address.sol";
77
import {Panic} from "../Panic.sol";
8+
import {SafeCast} from "./SafeCast.sol";
89

910
/**
1011
* @dev Standard math utilities missing in the Solidity language.
@@ -210,7 +211,7 @@ library Math {
210211
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
211212
*/
212213
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
213-
return mulDiv(x, y, denominator) + boolToUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
214+
return mulDiv(x, y, denominator) + SafeCast.toUint(unsignedRoundsUp(rounding) && mulmod(x, y, denominator) > 0);
214215
}
215216

216217
/**
@@ -379,7 +380,7 @@ library Math {
379380
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
380381
unchecked {
381382
uint256 result = sqrt(a);
382-
return result + boolToUint(unsignedRoundsUp(rounding) && result * result < a);
383+
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && result * result < a);
383384
}
384385
}
385386

@@ -391,35 +392,35 @@ library Math {
391392
uint256 result = 0;
392393
uint256 exp;
393394
unchecked {
394-
exp = 128 * boolToUint(value > (1 << 128) - 1);
395+
exp = 128 * SafeCast.toUint(value > (1 << 128) - 1);
395396
value >>= exp;
396397
result += exp;
397398

398-
exp = 64 * boolToUint(value > (1 << 64) - 1);
399+
exp = 64 * SafeCast.toUint(value > (1 << 64) - 1);
399400
value >>= exp;
400401
result += exp;
401402

402-
exp = 32 * boolToUint(value > (1 << 32) - 1);
403+
exp = 32 * SafeCast.toUint(value > (1 << 32) - 1);
403404
value >>= exp;
404405
result += exp;
405406

406-
exp = 16 * boolToUint(value > (1 << 16) - 1);
407+
exp = 16 * SafeCast.toUint(value > (1 << 16) - 1);
407408
value >>= exp;
408409
result += exp;
409410

410-
exp = 8 * boolToUint(value > (1 << 8) - 1);
411+
exp = 8 * SafeCast.toUint(value > (1 << 8) - 1);
411412
value >>= exp;
412413
result += exp;
413414

414-
exp = 4 * boolToUint(value > (1 << 4) - 1);
415+
exp = 4 * SafeCast.toUint(value > (1 << 4) - 1);
415416
value >>= exp;
416417
result += exp;
417418

418-
exp = 2 * boolToUint(value > (1 << 2) - 1);
419+
exp = 2 * SafeCast.toUint(value > (1 << 2) - 1);
419420
value >>= exp;
420421
result += exp;
421422

422-
result += boolToUint(value > 1);
423+
result += SafeCast.toUint(value > 1);
423424
}
424425
return result;
425426
}
@@ -431,7 +432,7 @@ library Math {
431432
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
432433
unchecked {
433434
uint256 result = log2(value);
434-
return result + boolToUint(unsignedRoundsUp(rounding) && 1 << result < value);
435+
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << result < value);
435436
}
436437
}
437438

@@ -480,7 +481,7 @@ library Math {
480481
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
481482
unchecked {
482483
uint256 result = log10(value);
483-
return result + boolToUint(unsignedRoundsUp(rounding) && 10 ** result < value);
484+
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 10 ** result < value);
484485
}
485486
}
486487

@@ -494,23 +495,23 @@ library Math {
494495
uint256 result = 0;
495496
uint256 isGt;
496497
unchecked {
497-
isGt = boolToUint(value > (1 << 128) - 1);
498+
isGt = SafeCast.toUint(value > (1 << 128) - 1);
498499
value >>= isGt * 128;
499500
result += isGt * 16;
500501

501-
isGt = boolToUint(value > (1 << 64) - 1);
502+
isGt = SafeCast.toUint(value > (1 << 64) - 1);
502503
value >>= isGt * 64;
503504
result += isGt * 8;
504505

505-
isGt = boolToUint(value > (1 << 32) - 1);
506+
isGt = SafeCast.toUint(value > (1 << 32) - 1);
506507
value >>= isGt * 32;
507508
result += isGt * 4;
508509

509-
isGt = boolToUint(value > (1 << 16) - 1);
510+
isGt = SafeCast.toUint(value > (1 << 16) - 1);
510511
value >>= isGt * 16;
511512
result += isGt * 2;
512513

513-
result += boolToUint(value > (1 << 8) - 1);
514+
result += SafeCast.toUint(value > (1 << 8) - 1);
514515
}
515516
return result;
516517
}
@@ -522,7 +523,7 @@ library Math {
522523
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
523524
unchecked {
524525
uint256 result = log256(value);
525-
return result + boolToUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
526+
return result + SafeCast.toUint(unsignedRoundsUp(rounding) && 1 << (result << 3) < value);
526527
}
527528
}
528529

@@ -532,14 +533,4 @@ library Math {
532533
function unsignedRoundsUp(Rounding rounding) internal pure returns (bool) {
533534
return uint8(rounding) % 2 == 1;
534535
}
535-
536-
/**
537-
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
538-
*/
539-
function boolToUint(bool b) internal pure returns (uint256 u) {
540-
/// @solidity memory-safe-assembly
541-
assembly {
542-
u := iszero(iszero(b))
543-
}
544-
}
545536
}

contracts/utils/math/SafeCast.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,4 +1150,14 @@ library SafeCast {
11501150
}
11511151
return int256(value);
11521152
}
1153+
1154+
/**
1155+
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
1156+
*/
1157+
function toUint(bool b) internal pure returns (uint256 u) {
1158+
/// @solidity memory-safe-assembly
1159+
assembly {
1160+
u := iszero(iszero(b))
1161+
}
1162+
}
11531163
}

scripts/generate/templates/SafeCast.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const header = `\
77
pragma solidity ^0.8.20;
88
99
/**
10-
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
10+
* @dev Wrappers over Solidity's uintXX/intXX/bool casting operators with added overflow
1111
* checks.
1212
*
1313
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
@@ -116,11 +116,23 @@ function toUint${length}(int${length} value) internal pure returns (uint${length
116116
}
117117
`;
118118

119+
const boolToUint = `
120+
/**
121+
* @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
122+
*/
123+
function toUint(bool b) internal pure returns (uint256 u) {
124+
/// @solidity memory-safe-assembly
125+
assembly {
126+
u := iszero(iszero(b))
127+
}
128+
}
129+
`;
130+
119131
// GENERATE
120132
module.exports = format(
121133
header.trimEnd(),
122134
'library SafeCast {',
123135
errors,
124-
[...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256)],
136+
[...LENGTHS.map(toUintDownCast), toUint(256), ...LENGTHS.map(toIntDownCast), toInt(256), boolToUint],
125137
'}',
126138
);

test/utils/math/Math.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,4 @@ describe('Math', function () {
512512
});
513513
});
514514
});
515-
516-
describe('boolToUint', function () {
517-
it('boolToUint(false) should be 0', async function () {
518-
expect(await this.mock.$boolToUint(false)).to.equal(0n);
519-
});
520-
521-
it('boolToUint(true) should be 1', async function () {
522-
expect(await this.mock.$boolToUint(true)).to.equal(1n);
523-
});
524-
});
525515
});

test/utils/math/SafeCast.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,14 @@ describe('SafeCast', function () {
146146
.withArgs(ethers.MaxUint256);
147147
});
148148
});
149+
150+
describe('toUint (bool)', function () {
151+
it('toUint(false) should be 0', async function () {
152+
expect(await this.mock.$toUint(false)).to.equal(0n);
153+
});
154+
155+
it('toUint(true) should be 1', async function () {
156+
expect(await this.mock.$toUint(true)).to.equal(1n);
157+
});
158+
});
149159
});

0 commit comments

Comments
 (0)