diff --git a/go.mod b/go.mod index c4ff837d399..f2a7b01b2de 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.17 github.com/ory/dockertest/v3 v3.10.0 github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 - github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36 github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231229191315-aff18520757d github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231229191315-aff18520757d diff --git a/go.sum b/go.sum index 875c47237f1..8c35e444d2e 100644 --- a/go.sum +++ b/go.sum @@ -1466,8 +1466,10 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-osmo-4 h1:RCd53Gnnn9yrjjdh8sBkhitqTz/ github.com/osmosis-labs/cosmos-sdk v0.47.5-osmo-4/go.mod h1:EHwCeN9IXonsjKcjpS12MqeStdZvIdxt3VYXhus3G3c= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY= github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d h1:/vmj70Yv4D4PbPmNxLRxj80aVmHllT0UD4ThvjbRChA= -github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d/go.mod h1:CfSV2Zn8fYu+Jwv1zNhb45dbG4O79cZ/VM3ey79+Xq8= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231215538-1a8c243520e9 h1:uv3HgeYw9QArhkO9YQGVfsnm/xS79ldMBJAMFZwXZno= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231215538-1a8c243520e9/go.mod h1:FQ45vPTc2l+NUA/gk7wZTxUh/hrDRWrRwZj8Ua5cpSU= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36 h1:UOpEx+lWFd9uX0goJMLaIvSjMCsWOmBzLOEVGZCi2Qk= +github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36/go.mod h1:Ger7K14gEgLOF+4qHNqicrGkNP9Ubg2tBnKkIzvXABE= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d h1:VKMEmZBVt6VrrjBA9dMsxqWJlbrzqbMXX69iOjuevRc= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d/go.mod h1:Jg/goBqOHnJSr4qgDLtVC88eU7MgtLhyc5+plNEj1Ag= github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231229191315-aff18520757d h1:hEwBZm3JibS2RSZat6YQ68RF6ocL1zxdbNgN7viM8K8= diff --git a/osmomath/binary_search.go b/osmomath/binary_search.go index 17ef1d9c129..e6d449fb9b7 100644 --- a/osmomath/binary_search.go +++ b/osmomath/binary_search.go @@ -279,8 +279,10 @@ func BinarySearchBigDec(f func(BigDec) BigDec, curIteration := 0 for ; curIteration < maxIterations; curIteration += 1 { - // TODO: Try replacing this with RSH - curEstimate = lowerbound.Add(upperbound).QuoRaw(2) + // (lowerbound + upperbound) / 2 + curEstimate = lowerbound.Add(upperbound) + curEstimateBi := curEstimate.BigIntMut() + curEstimateBi.Rsh(curEstimateBi, 1) curOutput = f(curEstimate) // fmt.Println("binary search, input, target output, cur output", curEstimate, targetOutput, curOutput) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 99591a51727..02db83a5264 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -253,8 +253,8 @@ func (d BigDec) BigInt() *big.Int { return cp.Set(d.i) } -// BigIntMut converts BigDec to big.Int, mutative the input -func (d BigDec) ToBigInt() *big.Int { +// BigIntMut returns the pointer of the underlying big.Int. +func (d BigDec) BigIntMut() *big.Int { if d.IsNil() { return nil } @@ -290,6 +290,11 @@ func (d BigDec) Sub(d2 BigDec) BigDec { return BigDec{res} } +func (d BigDec) NegMut() BigDec { + d.i.Neg(d.i) + return d +} // reverse the decimal sign + // Clone performs a deep copy of the receiver // and returns the new result. func (d BigDec) Clone() BigDec { diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index d6f517ea65a..eb5bb6bb8a6 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -1707,17 +1707,17 @@ func (s *decimalTestSuite) TestBigIntMut() { d := osmomath.NewBigDecFromBigInt(r) // Compare value of BigInt & BigIntMut - s.Require().Equal(d.BigInt(), d.ToBigInt()) + s.Require().Equal(d.BigInt(), d.BigIntMut()) // Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change - p1 := d.ToBigInt() + p1 := d.BigIntMut() p1.SetInt64(40) - s.Require().Equal(big.NewInt(40), d.ToBigInt()) + s.Require().Equal(big.NewInt(40), d.BigIntMut()) s.Require().Equal(big.NewInt(40), d.BigInt()) // Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change p2 := d.BigInt() p2.SetInt64(50) - s.Require().NotEqual(big.NewInt(50), d.ToBigInt()) + s.Require().NotEqual(big.NewInt(50), d.BigIntMut()) s.Require().NotEqual(big.NewInt(50), d.BigInt()) } diff --git a/osmomath/sigfig_round.go b/osmomath/sigfig_round.go index 692d5c72262..26ccbaa19fd 100644 --- a/osmomath/sigfig_round.go +++ b/osmomath/sigfig_round.go @@ -20,7 +20,8 @@ func SigFigRound(d Dec, tenToSigFig Int) Dec { dkSigFig := dTimesK.MulInt(tenToSigFig) numerator := dkSigFig.RoundInt().ToLegacyDec() + // TODO: Use pre-computed table for 10^k tenToK := NewInt(10).ToLegacyDec().Power(k) denominator := tenToSigFig.Mul(tenToK.TruncateInt()) - return numerator.QuoInt(denominator) + return numerator.QuoIntMut(denominator) } diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index 80f009996b2..50337581abc 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -63,15 +63,16 @@ func targetKCalculator(x0, y0, w, yf osmomath.BigDec) osmomath.BigDec { // $$k_{iter}(x_f) = -x_{out}^3 + 3 x_0 x_{out}^2 - (y_f^2 + w + 3x_0^2)x_{out}$$ // where x_out = x_0 - x_f func iterKCalculator(x0, w, yf osmomath.BigDec) func(osmomath.BigDec) osmomath.BigDec { - // compute coefficients first - cubicCoeff := osmomath.OneBigDec().Neg() + // compute coefficients first. Notice that the leading coefficient is -1, we will use this to compute faster. + // cubicCoeff := -1 quadraticCoeff := x0.MulInt64(3) - linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).Neg() + linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).NegMut() return func(xf osmomath.BigDec) osmomath.BigDec { xOut := x0.Sub(xf) // horners method // ax^3 + bx^2 + cx = x(c + x(b + ax)) - res := cubicCoeff.Mul(xOut) + // a = -1 + res := xOut.Neg() res = res.AddMut(quadraticCoeff).MulMut(xOut) res = res.AddMut(linearCoeff).MulMut(xOut) return res