Skip to content

Commit ee98e21

Browse files
committed
oz sqrt
1 parent f3910f1 commit ee98e21

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

src/MaglevEulerSwap.sol

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pragma solidity ^0.8.0;
33

44
import {console} from "forge-std/Test.sol";
5+
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
56
import {MaglevBase} from "./MaglevBase.sol";
67

78
contract MaglevEulerSwap is MaglevBase {
@@ -109,7 +110,7 @@ contract MaglevEulerSwap is MaglevBase {
109110
+ int256(y0) * (1e18 - 2 * int256(cy)) / 1e18;
110111
int256 c = (int256(cy) - 1e18) * int256(y0) ** 2 / 1e18 / 1e18;
111112
uint256 discriminant = uint256(int256(uint256(b ** 2)) - 4 * int256(a) * int256(c));
112-
uint256 numerator = uint256(-b + int256(uint256(sqrt(discriminant))));
113+
uint256 numerator = uint256(-b + int256(uint256(Math.sqrt(discriminant))));
113114
uint256 denominator = 2 * a;
114115
return numerator * 1e18 / denominator;
115116
}
@@ -172,48 +173,4 @@ contract MaglevEulerSwap is MaglevBase {
172173
// if distance is > zero, then point is above the curve, and invariant passes
173174
return (delta >= 0);
174175
}
175-
176-
// EIP-7054
177-
function sqrt(uint256 x) internal pure returns (uint128) {
178-
if (x == 0) {
179-
return 0;
180-
} else {
181-
uint256 xx = x;
182-
uint256 r = 1;
183-
if (xx >= 0x100000000000000000000000000000000) {
184-
xx >>= 128;
185-
r <<= 64;
186-
}
187-
if (xx >= 0x10000000000000000) {
188-
xx >>= 64;
189-
r <<= 32;
190-
}
191-
if (xx >= 0x100000000) {
192-
xx >>= 32;
193-
r <<= 16;
194-
}
195-
if (xx >= 0x10000) {
196-
xx >>= 16;
197-
r <<= 8;
198-
}
199-
if (xx >= 0x100) {
200-
xx >>= 8;
201-
r <<= 4;
202-
}
203-
if (xx >= 0x10) {
204-
xx >>= 4;
205-
r <<= 2;
206-
}
207-
if (xx >= 0x8) r <<= 1;
208-
r = (r + x / r) >> 1;
209-
r = (r + x / r) >> 1;
210-
r = (r + x / r) >> 1;
211-
r = (r + x / r) >> 1;
212-
r = (r + x / r) >> 1;
213-
r = (r + x / r) >> 1;
214-
r = (r + x / r) >> 1;
215-
uint256 r1 = x / r;
216-
return uint128(r < r1 ? r : r1);
217-
}
218-
}
219176
}

0 commit comments

Comments
 (0)