Skip to content

Commit

Permalink
Merge pull request #280 from curveresearch/fix-calc-diff
Browse files Browse the repository at this point in the history
Fix calc diff
  • Loading branch information
chanhosuh authored Nov 5, 2023
2 parents 71c5518 + 06df807 commit d82882a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions changelog.d/20231103_115610_chanhosuh_fix_calc_diff.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed
-----

- Fixed handling of integer signed division in Tricrypto-NG's `get_y` calc.
17 changes: 13 additions & 4 deletions curvesim/pool/cryptoswap/calcs/tricrypto_ng.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def get_y( # noqa: complexity: 18
else:
_c = gamma2 * _c_neg // D * ANN // 27 // A_MULTIPLIER
c += _c
c_is_neg = c < 0

# (10**18 + gamma)**2/27
d: int = (10**18 + gamma) ** 2 // 27
Expand Down Expand Up @@ -131,6 +132,8 @@ def get_y( # noqa: complexity: 18
additional_prec: int = 0
if b_is_neg:
b *= -1
if c_is_neg:
c *= -1
if abs(a) > abs(b):
additional_prec = abs(a // b)
a = a * additional_prec // divider
Expand All @@ -145,19 +148,25 @@ def get_y( # noqa: complexity: 18
d = d // additional_prec // divider
if b_is_neg:
b *= -1
if c_is_neg:
c *= -1

# 3*a*c/b - b
_3ac: int = 3 * a * c
if b_is_neg:
_3ac: int = (3 * a) * c
if sign(_3ac) != sign(b):
delta0: int = -(_3ac // -b) - b
else:
delta0 = _3ac // b - b

# 9*a*c/b - 2*b - 27*a**2/b*d/b
if sign(_3ac) != sign(b):
delta1: int = -(3 * _3ac // -b) - 2 * b
else:
delta1 = 3 * _3ac // b - 2 * b
if b_is_neg:
delta1: int = -(3 * _3ac // -b) - 2 * b - 27 * a**2 // -b * d // -b
delta1 -= 27 * a**2 // -b * d // -b
else:
delta1 = 3 * _3ac // b - 2 * b - 27 * a**2 // b * d // b
delta1 -= 27 * a**2 // b * d // b

# delta1**2 + 4*delta0**2/b*delta0
if b_is_neg:
Expand Down
3 changes: 2 additions & 1 deletion test/unit/test_tricrypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
_newton_y,
wad_exp,
)

from ..fixtures.pool import pack_prices, unpack_prices


Expand Down Expand Up @@ -305,7 +306,7 @@ def test_get_p(tricrypto_math, A, gamma, x0, x1, x2):
st.integers(min_value=0, max_value=2),
st.integers(min_value=0, max_value=2),
).filter(lambda x: x[0] != x[1]),
st.integers(min_value=1, max_value=10000),
st.integers(min_value=1, max_value=5500),
)
@settings(
suppress_health_check=[HealthCheck.function_scoped_fixture],
Expand Down

0 comments on commit d82882a

Please sign in to comment.