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

Feature/htr #8

Merged
merged 8 commits into from
Jul 28, 2020
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
70 changes: 57 additions & 13 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import (
"fmt"
)

// MarshalSSZ marshals an object
func MarshalSSZ(m Marshaler) ([]byte, error) {
buf := make([]byte, m.SizeSSZ())
return m.MarshalSSZTo(buf[:0])
}

// Errors

var (
ErrOffset = fmt.Errorf("incorrect offset")
ErrSize = fmt.Errorf("incorrect size")
ErrBytesLength = fmt.Errorf("bytes array does not have the correct length")
ErrVectorLength = fmt.Errorf("vector does not have the correct length")
ErrListTooBig = fmt.Errorf("list length is higher than max value")
)

// ---- Unmarshal functions ----

// UnmarshallUint64 unmarshals a little endian uint64 from the src input
Expand Down Expand Up @@ -37,18 +53,6 @@ func UnmarshalBool(src []byte) bool {

// ---- Marshal functions ----

// MarshalFixedBytes marshals buf of fixed size to dst
func MarshalFixedBytes(dst []byte, buf []byte, size int) ([]byte, error) {
if buf == nil {
buf = make([]byte, size)
}
if len(buf) != size {
return nil, fmt.Errorf("expected size %d but found %d", len(buf), size)
}
dst = append(dst, buf...)
return dst, nil
}

// MarshalUint64 marshals a little endian uint64 to dst
func MarshalUint64(dst []byte, i uint64) []byte {
buf := make([]byte, 8)
Expand Down Expand Up @@ -111,6 +115,14 @@ func safeReadOffset(buf []byte) (uint64, []byte, error) {

// ---- extend functions ----

func extendByteSlice(b []byte, needLen int) []byte {
b = b[:cap(b)]
if n := needLen - cap(b); n > 0 {
b = append(b, make([]byte, n)...)
}
return b[:needLen]
}

// ExtendUint64 extends a uint64 buffer to a given size
func ExtendUint64(b []uint64, needLen int) []uint64 {
b = b[:cap(b)]
Expand All @@ -129,10 +141,31 @@ func ExtendUint16(b []uint16, needLen int) []uint16 {
return b[:needLen]
}

// ---- unmarshal dynami content ----
// ---- unmarshal dynamic content ----

const bytesPerLengthOffset = 4

// ValidateBitlist validates that the bitlist is correct
func ValidateBitlist(buf []byte, bitLimit uint64) error {
byteLen := uint64(len(buf))
delim := (bitLimit >> 3) + 1
if byteLen > delim {
return fmt.Errorf("unexpected number of bytes, got %d but found %d", byteLen, delim)
}
if byteLen == 0 {
return fmt.Errorf("bitlist empty, it does not have length bit")
}

msb := buf[byteLen-1]
if msb == 0 {
return fmt.Errorf("trailing byte is zero")
}
if other := bitLimit - ((byteLen - 1) << 3); uint64(msb) > other {
return fmt.Errorf("too many bits")
}
return nil
}

// DecodeDynamicLength decodes the length from the dynamic input
func DecodeDynamicLength(buf []byte, maxSize int) (int, error) {
if len(buf) == 0 {
Expand Down Expand Up @@ -199,6 +232,17 @@ func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error)
return nil
}

func DivideInt2(a, b, max int) (int, error) {
num, ok := DivideInt(a, b)
if !ok {
return 0, fmt.Errorf("xx")
}
if num > max {
return 0, fmt.Errorf("yy")
}
return num, nil
}

// DivideInt divides the int fully
func DivideInt(a, b int) (int, bool) {
return a / b, a%b == 0
Expand Down
53 changes: 53 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ssz

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)

func TestBitlist(t *testing.T) {
res := []string{}
err := filepath.Walk("./spectests/fixtures/bitlist", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
res = append(res, path)
}
return nil
})
if err != nil {
t.Fatal(err)
}

for _, i := range res {
t.Run(i, func(t *testing.T) {
serialized, err := ioutil.ReadFile(i)
if err != nil {
t.Fatal(err)
}

var maxSize uint64

testName := strings.Split(i, "/")[3]
if strings.Contains(testName, "_no_") {
maxSize = 1000
} else {
// decode maxSize from name
num, err := strconv.Atoi(strings.Split(testName, "_")[1])
if err != nil {
t.Fatal(err)
}
maxSize = uint64(num)
}

if err := ValidateBitlist(serialized, maxSize); err == nil {
t.Fatal("bad")
}
})
}
}
7 changes: 7 additions & 0 deletions fuzz/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ func (fc *fuzzerContext) doFuzz(v reflect.Value, tag reflect.StructTag) {
}
fc.doFuzz(v.Field(i), typ.Field(i).Tag)
}

case reflect.Array:
n := v.Len()
for i := 0; i < n; i++ {
fc.doFuzz(v.Index(i), tag)
}

default:
panic(fmt.Sprintf("Can't handle %#v", v.Interface()))
}
Expand Down
24 changes: 4 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
module github.com/ferranbt/fastssz

go 1.12
go 1.14

require (
github.com/davecgh/go-spew v1.1.0
github.com/dgraph-io/ristretto v0.0.1 // indirect
github.com/ghodss/yaml v1.0.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.3.4
github.com/google/go-cmp v0.4.0
github.com/google/gofuzz v1.1.0
github.com/grpc-ecosystem/grpc-gateway v1.13.0
github.com/minio/highwayhash v1.0.0 // indirect
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/protolambda/zssz v0.1.3
github.com/prysmaticlabs/go-bitfield v0.0.0-20191017011753-53b773adde52
github.com/prysmaticlabs/go-ssz v0.0.0-20200101200214-e24db4d9e963
github.com/stretchr/testify v1.5.1
google.golang.org/genproto v0.0.0-20200228133532-8c2c7df3a383
google.golang.org/grpc v1.27.1
gopkg.in/yaml.v2 v2.2.3
gotest.tools v2.2.0+incompatible
github.com/minio/sha256-simd v0.1.1
github.com/mitchellh/mapstructure v1.3.2
gopkg.in/yaml.v2 v2.3.0
)
Loading