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

uint256: implement fastssz marshaling interfaces #126

Merged
merged 8 commits into from
Mar 9, 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
17 changes: 17 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,20 @@ func BenchmarkMulDivOverflow(b *testing.B) {
b.Run("div256/big", func(b *testing.B) { benchmarkBig(b, &big256SamplesLt, &big256Samples) })

}

func BenchmarkHashTreeRoot(b *testing.B) {
var (
z = &Int{1, 2, 3, 4}
exp = [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0}
)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
r, err := z.HashTreeRoot()
if err != nil {
b.Fatal(err)
}
if r != exp {
b.Fatalf("have %v want %v", r, exp)
}
}
}
65 changes: 58 additions & 7 deletions conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,55 @@ func bigEndianUint56(b []byte) uint64 {
uint64(b[2])<<32 | uint64(b[1])<<40 | uint64(b[0])<<48
}

// MarshalSSZTo implements the fastssz.Marshaler interface and serializes the
// integer into an already pre-allocated buffer.
func (z *Int) MarshalSSZTo(dst []byte) ([]byte, error) {
if len(dst) < 32 {
return nil, fmt.Errorf("%w: have %d, want %d bytes", ErrBadBufferLength, len(dst), 32)
}
binary.LittleEndian.PutUint64(dst[0:8], z[0])
binary.LittleEndian.PutUint64(dst[8:16], z[1])
binary.LittleEndian.PutUint64(dst[16:24], z[2])
binary.LittleEndian.PutUint64(dst[24:32], z[3])

return dst[32:], nil
}

// MarshalSSZ implements the fastssz.Marshaler interface and returns the integer
// marshalled into a newly allocated byte slice.
func (z *Int) MarshalSSZ() ([]byte, error) {
blob := make([]byte, 32)
_, _ = z.MarshalSSZTo(blob) // ignore error, cannot fail, surely have 32 byte space in blob
return blob, nil
}

// SizeSSZ implements the fastssz.Marshaler interface and returns the byte size
// of the 256 bit int.
func (*Int) SizeSSZ() int {
return 32
}

// UnmarshalSSZ implements the fastssz.Unmarshaler interface and parses an encoded
// integer into the local struct.
func (z *Int) UnmarshalSSZ(buf []byte) error {
if len(buf) != 32 {
holiman marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("%w: have %d, want %d bytes", ErrBadEncodedLength, len(buf), 32)
}
z[0] = binary.LittleEndian.Uint64(buf[0:8])
z[1] = binary.LittleEndian.Uint64(buf[8:16])
z[2] = binary.LittleEndian.Uint64(buf[16:24])
z[3] = binary.LittleEndian.Uint64(buf[24:32])

return nil
}

// HashTreeRoot implements the fastssz.HashRoot interface's non-dependent part.
func (z *Int) HashTreeRoot() ([32]byte, error) {
var hash [32]byte
_, _ = z.MarshalSSZTo(hash[:]) // ignore error, cannot fail
return hash, nil
}

// EncodeRLP implements the rlp.Encoder interface from go-ethereum
// and writes the RLP encoding of z to w.
func (z *Int) EncodeRLP(w io.Writer) error {
Expand Down Expand Up @@ -605,13 +654,15 @@ func (src *Int) Value() (driver.Value, error) {
}

var (
ErrEmptyString = errors.New("empty hex string")
ErrSyntax = errors.New("invalid hex string")
ErrMissingPrefix = errors.New("hex string without 0x prefix")
ErrEmptyNumber = errors.New("hex string \"0x\"")
ErrLeadingZero = errors.New("hex number with leading zero digits")
ErrBig256Range = errors.New("hex number > 256 bits")
ErrNonString = errors.New("non-string")
ErrEmptyString = errors.New("empty hex string")
ErrSyntax = errors.New("invalid hex string")
ErrMissingPrefix = errors.New("hex string without 0x prefix")
ErrEmptyNumber = errors.New("hex string \"0x\"")
ErrLeadingZero = errors.New("hex number with leading zero digits")
ErrBig256Range = errors.New("hex number > 256 bits")
ErrNonString = errors.New("non-string")
ErrBadBufferLength = errors.New("bad ssz buffer length")
ErrBadEncodedLength = errors.New("bad ssz encoded length")
)

func checkNumberS(input string) error {
Expand Down
Loading