Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(math): Backport of pull requests #17352 and #18214 to feature/v.0.47.x-ics-lsm branch #19293

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion math/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func NewIntFromUint64(n uint64) Int {

// NewIntFromBigInt constructs Int from big.Int. If the provided big.Int is nil,
// it returns an empty instance. This function panics if the bit length is > 256.
// Note, the caller can safely mutate the argument after this function returns.
func NewIntFromBigInt(i *big.Int) Int {
if i == nil {
return Int{}
Expand All @@ -112,7 +113,7 @@ func NewIntFromBigInt(i *big.Int) Int {
if i.BitLen() > MaxBitLen {
panic("NewIntFromBigInt() out of bound")
}
return Int{i}
return Int{new(big.Int).Set(i)}
}

// NewIntFromString constructs Int from string
Expand Down
13 changes: 13 additions & 0 deletions math/int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ func (s *intTestSuite) TestFromUint64() {
}
}

func (s *intTestSuite) TestNewIntFromBigInt() {
i := math.NewIntFromBigInt(nil)
s.Require().True(i.IsNil())

r := big.NewInt(42)
i = math.NewIntFromBigInt(r)
s.Require().Equal(r, i.BigInt())

// modify r and ensure i doesn't change
r = r.SetInt64(100)
s.Require().NotEqual(r, i.BigInt())
}

func (s *intTestSuite) TestIntPanic() {
// Max Int = 2^256-1 = 1.1579209e+77
// Min Int = -(2^256-1) = -1.1579209e+77
Expand Down
2 changes: 1 addition & 1 deletion math/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func checkNewUint(i *big.Int) (Uint, error) {
if err := UintOverflow(i); err != nil {
return Uint{}, err
}
return Uint{i}, nil
return Uint{new(big.Int).Set(i)}, nil
}

// RelativePow raises x to the power of n, where x (and the result, z) are scaled by factor b
Expand Down
10 changes: 10 additions & 0 deletions math/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ func (s *uintTestSuite) TestParseUint() {
}
}

func (s *uintTestSuite) TestNewUintFromBigInt() {
r := big.NewInt(42)
i := sdkmath.NewUintFromBigInt(r)
s.Require().Equal(r, i.BigInt())

// modify r and ensure i doesn't change
r = r.SetInt64(100)
s.Require().NotEqual(r, i.BigInt())
}

func randuint() sdkmath.Uint {
return sdkmath.NewUint(rand.Uint64())
}
Expand Down
Loading