Skip to content

Commit 846bb37

Browse files
authored
Merge pull request #78 from euler-xyz/sqrt-rounding
chore: add OZ rounding up in sqrt
2 parents 8450fa2 + 40e41de commit 846bb37

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

src/libraries/CurveLib.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,14 @@ library CurveLib {
6868
unchecked {
6969
squaredB = absB * absB; // scale: 1e36
7070
discriminant = squaredB + fourAC; // scale: 1e36
71-
sqrt = Math.sqrt(discriminant); // scale: 1e18
72-
sqrt = (sqrt * sqrt < discriminant) ? sqrt + 1 : sqrt;
71+
sqrt = Math.sqrt(discriminant, Math.Rounding.Ceil); // scale: 1e18
7372
}
7473
} else {
7574
// B^2 cannot be calculated directly at 1e18 scale without overflowing
7675
uint256 scale = computeScale(absB); // calculate the scaling factor such that B^2 can be calculated without overflowing
7776
squaredB = Math.mulDiv(absB / scale, absB, scale, Math.Rounding.Ceil);
7877
discriminant = squaredB + fourAC / (scale * scale);
79-
sqrt = Math.sqrt(discriminant);
80-
sqrt = (sqrt * sqrt < discriminant) ? sqrt + 1 : sqrt;
78+
sqrt = Math.sqrt(discriminant, Math.Rounding.Ceil);
8179
sqrt = sqrt * scale;
8280
}
8381

@@ -86,7 +84,7 @@ library CurveLib {
8684
// use the regular quadratic formula solution (-b + sqrt(b^2 - 4ac)) / 2a
8785
x = Math.mulDiv(absB + sqrt, 1e18, 2 * c, Math.Rounding.Ceil) + 1;
8886
} else {
89-
// use the "citardauq" quadratic formula solution 2c / (-b + sqrt(b^2 - 4ac))
87+
// use the "citardauq" quadratic formula solution 2c / (-b - sqrt(b^2 - 4ac))
9088
x = (2 * C + (absB + sqrt - 1)) / (absB + sqrt) + 1;
9189
}
9290

0 commit comments

Comments
 (0)