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

Allow converting between nil big.Int and nil uint256.Int #137

Merged
merged 5 commits into from
Mar 31, 2023
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
13 changes: 13 additions & 0 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ var (
)

// ToBig returns a big.Int version of z.
holiman marked this conversation as resolved.
Show resolved Hide resolved
// Return `nil` if z is nil
func (z *Int) ToBig() *big.Int {
if z == nil {
return nil
}
b := new(big.Int)
switch maxWords { // Compile-time check.
case 4: // 64-bit architectures.
Expand All @@ -61,15 +65,24 @@ func (z *Int) ToBig() *big.Int {

// FromBig is a convenience-constructor from big.Int.
// Returns a new Int and whether overflow occurred.
holiman marked this conversation as resolved.
Show resolved Hide resolved
// OBS: If b is `nil`, this method returns `nil, false`
func FromBig(b *big.Int) (*Int, bool) {
if b == nil {
return nil, false
}
z := &Int{}
overflow := z.SetFromBig(b)
return z, overflow
}

// MustFromBig is a convenience-constructor from big.Int.
// Returns a new Int and panics if overflow occurred.
holiman marked this conversation as resolved.
Show resolved Hide resolved
// OBS: If b is `nil`, this method does _not_ panic, but
// instead returns `nil`
func MustFromBig(b *big.Int) *Int {
if b == nil {
return nil
}
z := &Int{}
if z.SetFromBig(b) {
panic("overflow")
Expand Down
21 changes: 18 additions & 3 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@ var (
)

func TestFromBig(t *testing.T) {
a := new(big.Int)
var a *big.Int
b, o := FromBig(a)
if o {
t.Fatalf("nil conversion overflowed! big.Int %x", b)
}
if b != nil {
t.Fatalf("got %x, exp %v", b, nil)
}
b2 := MustFromBig(a)
if b2 != nil {
t.Fatalf("got %x, exp %v", b2, nil)
}
a = new(big.Int)
b, o = FromBig(a)
if o {
t.Fatalf("conversion overflowed! big.Int %x", a.Bytes())
}
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 := MustFromBig(a)
b2 = MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
Expand Down Expand Up @@ -211,7 +223,10 @@ func TestFromBigOverflow(t *testing.T) {
}

func TestToBig(t *testing.T) {

var uint256Nil *Int
if bigNil := uint256Nil.ToBig(); bigNil != nil {
t.Errorf("want big.Int <nil>, have %x", bigNil)
}
if bigZero := new(Int).ToBig(); bigZero.Cmp(new(big.Int)) != 0 {
t.Errorf("expected big.Int 0, got %x", bigZero)
}
Expand Down