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

conversion: introduce 0-alloc IntoBig method #177

Merged
merged 2 commits into from
Jul 25, 2024
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
1 change: 0 additions & 1 deletion benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,6 @@ func BenchmarkHashTreeRoot(b *testing.B) {
}

func BenchmarkSet(bench *testing.B) {

benchmarkUint256 := func(bench *testing.B) {
a := new(Int).SetBytes(hex2Bytes("f123456789abcdeffedcba9876543210f2f3f4f5f6f7f8f9fff3f4f5f6f7f8f9"))

Expand Down
61 changes: 51 additions & 10 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,62 @@ func (z *Int) ToBig() *big.Int {
if z == nil {
return nil
}
b := new(big.Int)
var b *big.Int
z.IntoBig(&b)
return b
}

// IntoBig sets a provided big.Int to the value of z.
// Sets `nil` if z is nil (thus the double pointer).
func (z *Int) IntoBig(b **big.Int) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like to have a double-pointer in the API. It's not nice to work with, because it's so unusual.
I can be persuaded otherwise, but, why not just pick one of these options:

  1. panic on nil deref if z is nil
  2. Set the b to zero if z is nil

And then get rid of the **?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the purpose is to convert between uint256 and big.Int. If nil is a valid uint256 value, it we should be able to convert it to big.Int. IMO we''ll shoot ourselves in the foot with all the optional uint256 fields if we panic all of a sudden. Also converting an unset-optional into a set-0 seems bad.

Copy link
Contributor Author

@karalabe karalabe Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

**big.Int is similar to *[] (i.e. slice pointer) FWIW. Maybe unusual to see a double pointer like that, but it's not a unique construct. You can definitely work around it by delegating nil checks to outer code, but then half the conversion happens inside the lib, but the other half (allocating/nilling the big.Int happens outside).

IMO the API "strageness" isn't that bad compared to having everyone become responsible for handling nil/non-nil in their code explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Point in case, straight from the Go standard library: https://github.com/golang/go/blob/b8f83e22703ee23d49d95154449ce7066402d5c9/src/crypto/internal/boring/boring.go#L83

Converting big ints by placing them into an existing variable :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But if b is nil, this will panic. Is that intentional? I guess it is, just want to double-check that's what we want

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from sending in nil explicitly, there's no meaningful way you can end up with nil.

i.e. if you have a nil big.int pointer, you can still take it's address and it will be a non-nil pointer you can use to init the big.int. Since nobody's passing double pointers around in general for big.ints, you can't realistically end up with an accidental nil double pointer.

if z == nil {
*b = nil
return
}
if *b == nil {
*b = new(big.Int)
}
switch maxWords { // Compile-time check.
case 4: // 64-bit architectures.
words := [4]big.Word{big.Word(z[0]), big.Word(z[1]), big.Word(z[2]), big.Word(z[3])}
b.SetBits(words[:])
if words := (*b).Bits(); cap(words) >= 4 {
// Enough underlying space to set all the uint256 data
words = words[:4]

words[0] = big.Word(z[0])
words[1] = big.Word(z[1])
words[2] = big.Word(z[2])
words[3] = big.Word(z[3])

// Feed it back to normalize (up or down within the big.Int)
(*b).SetBits(words)
} else {
// Not enough space to set all the words, have to allocate
words := [4]big.Word{big.Word(z[0]), big.Word(z[1]), big.Word(z[2]), big.Word(z[3])}
(*b).SetBits(words[:])
}
case 8: // 32-bit architectures.
words := [8]big.Word{
big.Word(z[0]), big.Word(z[0] >> 32),
big.Word(z[1]), big.Word(z[1] >> 32),
big.Word(z[2]), big.Word(z[2] >> 32),
big.Word(z[3]), big.Word(z[3] >> 32),
if words := (*b).Bits(); cap(words) >= 8 {
// Enough underlying space to set all the uint256 data
words = words[:8]

words[0], words[1] = big.Word(z[0]), big.Word(z[0]>>32)
words[2], words[3] = big.Word(z[1]), big.Word(z[1]>>32)
words[4], words[5] = big.Word(z[2]), big.Word(z[2]>>32)
words[6], words[7] = big.Word(z[3]), big.Word(z[3]>>32)

// Feed it back to normalize (up or down within the big.Int)
(*b).SetBits(words)
} else {
// Not enough space to set all the words, have to allocate
words := [8]big.Word{
big.Word(z[0]), big.Word(z[0] >> 32),
big.Word(z[1]), big.Word(z[1] >> 32),
big.Word(z[2]), big.Word(z[2] >> 32),
big.Word(z[3]), big.Word(z[3] >> 32),
}
(*b).SetBits(words[:])
}
b.SetBits(words[:])
}
return b
}

// FromBig is a convenience-constructor from big.Int.
Expand Down
46 changes: 46 additions & 0 deletions conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,30 @@ func TestToBig(t *testing.T) {
}
}

func TestIntoBig(t *testing.T) {
var uint256Nil *Int

bigNil := new(big.Int)
if uint256Nil.IntoBig(&bigNil); bigNil != nil {
t.Errorf("want big.Int <nil>, have %x", bigNil)
}
var bigZero *big.Int
if new(Int).IntoBig(&bigZero); bigZero.Cmp(new(big.Int)) != 0 {
t.Errorf("expected big.Int 0, got %x", bigZero)
}
var b *big.Int
for i := uint(0); i < 256; i++ {
f := new(Int).SetUint64(1)
f.Lsh(f, i)
f.IntoBig(&b)
expected := big.NewInt(1)
expected.Lsh(expected, i)
if b.Cmp(expected) != 0 {
t.Fatalf("expected %x, got %x", expected, b)
}
}
}

func BenchmarkScanScientific(b *testing.B) {
intsub1 := new(Int)
_ = intsub1.fromDecimal(twoPow256Sub1)
Expand Down Expand Up @@ -324,6 +348,28 @@ func BenchmarkToBig(bench *testing.B) {
bench.Run("4words", func(bench *testing.B) { benchToBig(bench, param4) })
}

func benchIntoBig(bench *testing.B, f *Int) *big.Int {
var b *big.Int
for i := 0; i < bench.N; i++ {
f.IntoBig(&b)
}
return b
}

func BenchmarkIntoBig(bench *testing.B) {
param1 := new(Int).SetUint64(0xff)
bench.Run("1word", func(bench *testing.B) { benchIntoBig(bench, param1) })

param2 := new(Int).Lsh(param1, 64)
bench.Run("2words", func(bench *testing.B) { benchIntoBig(bench, param2) })

param3 := new(Int).Lsh(param2, 64)
bench.Run("3words", func(bench *testing.B) { benchIntoBig(bench, param3) })

param4 := new(Int).Lsh(param3, 64)
bench.Run("4words", func(bench *testing.B) { benchIntoBig(bench, param4) })
}

func TestFormat(t *testing.T) {
testCases := []string{
"0",
Expand Down