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

add MustFromBig for nicer struct initialization #128

Merged
merged 4 commits into from
Mar 21, 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
10 changes: 10 additions & 0 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func FromBig(b *big.Int) (*Int, bool) {
return z, overflow
}

// MustFromBig is a convenience-constructor from big.Int.
// Returns a new Int and panics if overflow occurred.
func MustFromBig(b *big.Int) *Int {
z := &Int{}
if z.SetFromBig(b) {
panic("overflow")
}
return z
}

// SetFromHex sets z from the given string, interpreted as a hexadecimal number.
// OBS! This method is _not_ strictly identical to the (*big.Int).SetString(..., 16) method.
// Notable differences:
Expand Down
62 changes: 58 additions & 4 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func TestFromBig(t *testing.T) {
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 := MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}

a = big.NewInt(1)
b, o = FromBig(a)
Expand All @@ -36,6 +40,10 @@ func TestFromBig(t *testing.T) {
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 = MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}

a = big.NewInt(0x1000000000000000)
b, o = FromBig(a)
Expand All @@ -45,6 +53,10 @@ func TestFromBig(t *testing.T) {
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 = MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}

a = big.NewInt(0x1234)
b, o = FromBig(a)
Expand All @@ -54,6 +66,10 @@ func TestFromBig(t *testing.T) {
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 = MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}

a = big.NewInt(1)
a.Lsh(a, 256)
Expand All @@ -65,6 +81,18 @@ func TestFromBig(t *testing.T) {
if !b.Eq(new(Int)) {
t.Fatalf("got %x exp 0", b.Bytes())
}
done := make(chan struct{})
go func() {
defer func() {
o = recover() != nil
done <- struct{}{}
}()
MustFromBig(a)
}()
<-done
if !o {
t.Fatalf("expected overflow")
}

a.Sub(a, big.NewInt(1))
b, o = FromBig(a)
Expand All @@ -74,6 +102,10 @@ func TestFromBig(t *testing.T) {
if exp, got := a.Bytes(), b.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
b2 = MustFromBig(a)
if exp, got := a.Bytes(), b2.Bytes(); !bytes.Equal(got, exp) {
t.Fatalf("got %x exp %x", got, exp)
}
}

func TestScanScientific(t *testing.T) {
Expand Down Expand Up @@ -160,19 +192,41 @@ func TestScanScientific(t *testing.T) {
}

func TestFromBigOverflow(t *testing.T) {
_, o := FromBig(new(big.Int).SetBytes(hex2Bytes("ababee444444444444ffcc333333333333ddaa222222222222bb8811111111111199")))
// Test overflow with error returns
b := new(big.Int).SetBytes(hex2Bytes("ababee444444444444ffcc333333333333ddaa222222222222bb8811111111111199"))
_, o := FromBig(b)
if !o {
t.Errorf("expected overflow, got %v", o)
}
_, o = FromBig(new(big.Int).SetBytes(hex2Bytes("ee444444444444ffcc333333333333ddaa222222222222bb8811111111111199")))
// Test overflow with panic (recovery is a bit unwieldy)
done := make(chan struct{})
go func() {
defer func() {
o = recover() != nil
done <- struct{}{}
}()
MustFromBig(b)
}()
<-done
if !o {
t.Fatalf("expected overflow")
}
// Test no overflow
b = new(big.Int).SetBytes(hex2Bytes("ee444444444444ffcc333333333333ddaa222222222222bb8811111111111199"))
_, o = FromBig(b)
if o {
t.Errorf("expected no overflow, got %v", o)
}
b := new(big.Int).SetBytes(hex2Bytes("ee444444444444ffcc333333333333ddaa222222222222bb8811111111111199"))
_, o = FromBig(b.Neg(b))
MustFromBig(b)

b = new(big.Int).SetBytes(hex2Bytes("ee444444444444ffcc333333333333ddaa222222222222bb8811111111111199"))
b.Neg(b)

_, o = FromBig(b)
if o {
t.Errorf("expected no overflow, got %v", o)
}
MustFromBig(b)
}

func TestToBig(t *testing.T) {
Expand Down