Skip to content

Commit

Permalink
Add support for uint32 en/decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Lavishq committed Jul 26, 2024
1 parent 450df98 commit ca92f0f
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ The table below is a summary of the methods available for `SizeSSZ` and `DefineS
| `bool` | `1 byte` | [`DefineBool`](https://pkg.go.dev/github.com/karalabe/ssz#DefineBool) | [`EncodeBool`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeBool) | [`DecodeBool`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeBool) | [`HashBool`](https://pkg.go.dev/github.com/karalabe/ssz#HashBool) |
| `uint8` | `1 bytes` | [`DefineUint8`](https://pkg.go.dev/github.com/karalabe/ssz#DefineUint8) | [`EncodeUint8`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeUint8) | [`DecodeUint8`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeUint8) | [`HashUint8`](https://pkg.go.dev/github.com/karalabe/ssz#HashUint8) |
| `uint16` | `2 bytes` | [`DefineUint16`](https://pkg.go.dev/github.com/karalabe/ssz#DefineUint16) | [`EncodeUint16`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeUint16) | [`DecodeUint16`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeUint16) | [`HashUint16`](https://pkg.go.dev/github.com/karalabe/ssz#HashUint16) |
| `uint32` | `4 bytes` | [`DefineUint32`](https://pkg.go.dev/github.com/karalabe/ssz#DefineUint32) | [`EncodeUint32`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeUint32) | [`DecodeUint32`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeUint32) | [`HashUint32`](https://pkg.go.dev/github.com/karalabe/ssz#HashUint32) |
| `uint64` | `8 bytes` | [`DefineUint64`](https://pkg.go.dev/github.com/karalabe/ssz#DefineUint64) | [`EncodeUint64`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeUint64) | [`DecodeUint64`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeUint64) | [`HashUint64`](https://pkg.go.dev/github.com/karalabe/ssz#HashUint64) |
| `[N]byte` as `bitvector[N]` | `N bytes` | [`DefineArrayOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#DefineArrayOfBits) | [`EncodeArrayOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeArrayOfBits) | [`DecodeArrayOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeArrayOfBits) | [`HashArrayOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#HashArrayOfBits) |
| `bitfield.Bitlist`² | [`SizeSliceOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#SizeSliceOfBits) | [`DefineSliceOfBitsOffset`](https://pkg.go.dev/github.com/karalabe/ssz#DefineSliceOfBitsOffset) [`DefineSliceOfBitsContent`](https://pkg.go.dev/github.com/karalabe/ssz#DefineSliceOfBitsContent) | [`EncodeSliceOfBitsOffset`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeSliceOfBitsOffset) [`EncodeSliceOfBitsContent`](https://pkg.go.dev/github.com/karalabe/ssz#EncodeSliceOfBitsContent) | [`DecodeSliceOfBitsOffset`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeSliceOfBitsOffset) [`DecodeSliceOfBitsContent`](https://pkg.go.dev/github.com/karalabe/ssz#DecodeSliceOfBitsContent) | [`HashSliceOfBits`](https://pkg.go.dev/github.com/karalabe/ssz#HashSliceOfBits) |
Expand Down
10 changes: 10 additions & 0 deletions cmd/sszgen/opset.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ func (p *parseContext) resolveBasicOpset(typ *types.Basic, tags *sizeTag) (opset
"DecodeUint16({{.Codec}}, &{{.Field}})",
[]int{2},
}, nil
case types.Uint32:
if tags != nil && tags.size[0] != 4 {
return nil, fmt.Errorf("byte basic type requires ssz-size=4: have %d", tags.size[0])
}
return &opsetStatic{
"DefineUint32({{.Codec}}, &{{.Field}})",
"EncodeUint32({{.Codec}}, &{{.Field}})",
"DecodeUint32({{.Codec}}, &{{.Field}})",
[]int{4},
}, nil
case types.Uint64:
if tags != nil && tags.size[0] != 8 {
return nil, fmt.Errorf("uint64 basic type requires ssz-size=8: have %d", tags.size[0])
Expand Down
13 changes: 13 additions & 0 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ func DefineUint16[T ~uint16](c *Codec, n *T) {
HashUint16(c.has, *n)
}

// DefineUint16 defines the next field as a uint16.
func DefineUint32[T ~uint32](c *Codec, n *T) {
if c.enc != nil {
EncodeUint32(c.enc, *n)
return
}
if c.dec != nil {
DecodeUint32(c.dec, n)
return
}
HashUint32(c.has, *n)
}

// DefineUint64 defines the next field as a uint64.
func DefineUint64[T ~uint64](c *Codec, n *T) {
if c.enc != nil {
Expand Down
19 changes: 19 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ func DecodeUint16[T ~uint16](dec *Decoder, n *T) {
}
}

// DecodeUint32 parses a uint32.
func DecodeUint32[T ~uint32](dec *Decoder, n *T) {
if dec.err != nil {
return
}
if dec.inReader != nil {
_, dec.err = io.ReadFull(dec.inReader, dec.buf[:4])
*n = T(binary.LittleEndian.Uint32(dec.buf[:4]))
dec.inRead += 4
} else {
if len(dec.inBuffer) < 4 {
dec.err = io.ErrUnexpectedEOF
return
}
*n = T(binary.LittleEndian.Uint32(dec.inBuffer))
dec.inBuffer = dec.inBuffer[4:]
}
}

// DecodeUint64 parses a uint64.
func DecodeUint64[T ~uint64](dec *Decoder, n *T) {
if dec.err != nil {
Expand Down
14 changes: 14 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ func EncodeUint16[T ~uint16](enc *Encoder, n T) {
}
}

// EncodeUint32 serializes a uint32.
func EncodeUint32[T ~uint32](enc *Encoder, n T) {
if enc.outWriter != nil {
if enc.err != nil {
return
}
binary.LittleEndian.PutUint32(enc.buf[:4], (uint32)(n))
_, enc.err = enc.outWriter.Write(enc.buf[:4])
} else {
binary.LittleEndian.PutUint32(enc.outBuffer, (uint32)(n))
enc.outBuffer = enc.outBuffer[4:]
}
}

// EncodeUint64 serializes a uint64.
func EncodeUint64[T ~uint64](enc *Encoder, n T) {
// Nope, dive into actual encoding
Expand Down
7 changes: 7 additions & 0 deletions hasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func HashUint16[T ~uint16](h *Hasher, n T) {
h.insertChunk(buffer, 0)
}

// HashUint32 hashes a uint32.
func HashUint32[T ~uint32](h *Hasher, n T) {
var buffer [32]byte
binary.LittleEndian.PutUint32(buffer[:], uint32(n))
h.insertChunk(buffer, 0)
}

// HashUint64 hashes a uint64.
func HashUint64[T ~uint64](h *Hasher, n T) {
var buffer [32]byte
Expand Down

0 comments on commit ca92f0f

Please sign in to comment.