Skip to content

Commit

Permalink
feat(bytes): Add SSZ logic (berachain#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
Devon Bear authored Jul 3, 2024
1 parent face56e commit bc9f002
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 91 deletions.
1 change: 1 addition & 0 deletions mod/primitives/pkg/bytes/b.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
//

package bytes

Expand Down
103 changes: 103 additions & 0 deletions mod/primitives/pkg/bytes/b20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2024, Berachain Foundation. All rights reserved.
// Use of this software is governed by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
//

package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

const (
// B20Size represents a 20-byte size.
B20Size = 20
)

var _ types.MinimalSSZType = (*B20)(nil)

// B20 represents a 20-byte fixed-size byte array.
// For SSZ purposes it is serialized a `Vector[Byte, 20]`.
type B20 [20]byte

// ToBytes20 is a utility function that transforms a byte slice into a fixed
// 20-byte array. If the input exceeds 20 bytes, it gets truncated.
func ToBytes20(input []byte) B20 {
return B20(ExtendToSize(input, B20Size))
}

/* -------------------------------------------------------------------------- */
/* TextMarshaler */
/* -------------------------------------------------------------------------- */

// MarshalText implements the encoding.TextMarshaler interface for B20.
func (h B20) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}

// UnmarshalText implements the encoding.TextUnmarshaler interface for B20.
func (h *B20) UnmarshalText(text []byte) error {
return UnmarshalTextHelper(h[:], text)
}

// String returns the hex string representation of B20.
func (h *B20) String() string {
return hex.FromBytes(h[:]).Unwrap()
}

/* -------------------------------------------------------------------------- */
/* JSONMarshaler */
/* -------------------------------------------------------------------------- */

// UnmarshalJSON implements the json.Unmarshaler interface for B20.
func (h *B20) UnmarshalJSON(input []byte) error {
return unmarshalJSONHelper(h[:], input)
}

/* -------------------------------------------------------------------------- */
/* SSZMarshaler */
/* -------------------------------------------------------------------------- */

// SizeSSZ returns the size of its SSZ encoding in bytes.
func (h B20) SizeSSZ() int {
return B20Size
}

// MarshalSSZ implements the SSZ marshaling for B20.
func (h B20) MarshalSSZ() ([]byte, error) {
return h[:], nil
}

// IsFixed returns true if the length of the B20 is fixed.
func (h B20) IsFixed() bool {
return true
}

// Type returns the type of the B20.
func (h B20) Type() types.Type {
return types.Composite
}

// HashTreeRoot returns the hash tree root of the B20.
func (h B20) HashTreeRoot() ([32]byte, error) {
var result [32]byte
copy(result[:], h[:])
return result, nil
}
64 changes: 48 additions & 16 deletions mod/primitives/pkg/bytes/b32.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN AS IS BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
//

package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

const (
// B32Size represents a 32-byte size.
B32Size = 32
)

// B32 represents a 32-byte array.
var _ types.MinimalSSZType = (*B32)(nil)

// B32 represents a 32-byte fixed-size byte array.
// For SSZ purposes it is serialized a `Vector[Byte, 32]`.
type B32 [32]byte

// ToBytes32 is a utility function that transforms a byte slice into a fixed
Expand All @@ -33,20 +43,9 @@ func ToBytes32(input []byte) B32 {
return B32(ExtendToSize(input, B32Size))
}

// UnmarshalJSON implements the json.Unmarshaler interface for B32.
func (h *B32) UnmarshalJSON(input []byte) error {
return unmarshalJSONHelper(h[:], input)
}

// String returns the hex string representation of B32.
func (h B32) String() string {
return hex.FromBytes(h[:]).Unwrap()
}

// HashTreeRoot returns the hash tree root of the B32.
func (h B32) HashTreeRoot() ([32]byte, error) {
return h, nil
}
/* -------------------------------------------------------------------------- */
/* TextMarshaler */
/* -------------------------------------------------------------------------- */

// MarshalText implements the encoding.TextMarshaler interface for B32.
func (h B32) MarshalText() ([]byte, error) {
Expand All @@ -58,6 +57,24 @@ func (h *B32) UnmarshalText(text []byte) error {
return UnmarshalTextHelper(h[:], text)
}

// String returns the hex string representation of B32.
func (h B32) String() string {
return hex.FromBytes(h[:]).Unwrap()
}

/* -------------------------------------------------------------------------- */
/* JSONMarshaler */
/* -------------------------------------------------------------------------- */

// UnmarshalJSON implements the json.Unmarshaler interface for B32.
func (h *B32) UnmarshalJSON(input []byte) error {
return unmarshalJSONHelper(h[:], input)
}

/* -------------------------------------------------------------------------- */
/* SSZMarshaler */
/* -------------------------------------------------------------------------- */

// SizeSSZ returns the size of its SSZ encoding in bytes.
func (h B32) SizeSSZ() int {
return B32Size
Expand All @@ -67,3 +84,18 @@ func (h B32) SizeSSZ() int {
func (h B32) MarshalSSZ() ([]byte, error) {
return h[:], nil
}

// IsFixed returns true if the length of the B32 is fixed.
func (h B32) IsFixed() bool {
return true
}

// Type returns the type of the B32.
func (h B32) Type() types.Type {
return types.Composite
}

// HashTreeRoot returns the hash tree root of the B32.
func (h B32) HashTreeRoot() ([32]byte, error) {
return h, nil
}
73 changes: 61 additions & 12 deletions mod/primitives/pkg/bytes/b4.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,35 @@
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.

//
//nolint:dupl // it's okay to have similar code for different types
package bytes

import (
"github.com/berachain/beacon-kit/mod/primitives/pkg/hex"
"github.com/berachain/beacon-kit/mod/primitives/pkg/ssz/types"
)

// B4 represents a 4-byte array.
type B4 [4]byte
const (
// B4Size represents a 4-byte size.
B4Size = 4
)

// UnmarshalJSON implements the json.Unmarshaler interface for B4.
func (h *B4) UnmarshalJSON(input []byte) error {
return unmarshalJSONHelper(h[:], input)
}
var _ types.MinimalSSZType = (*B4)(nil)

// B4 represents a 4-byte fixed-size byte array.
// For SSZ purposes it is serialized a `Vector[Byte, 4]`.
type B4 [4]byte

// ToBytes4 is a utility function that transforms a byte slice into a fixed
// 4-byte array. If the input exceeds 4 bytes, it gets truncated.
func ToBytes4(input []byte) B4 {
return [4]byte(ExtendToSize(input, B4Size))
return B4(ExtendToSize(input, B4Size))
}

// String returns the hex string representation of B4.
func (h B4) String() string {
return hex.FromBytes(h[:]).Unwrap()
}
/* -------------------------------------------------------------------------- */
/* TextMarshaler */
/* -------------------------------------------------------------------------- */

// MarshalText implements the encoding.TextMarshaler interface for B4.
func (h B4) MarshalText() ([]byte, error) {
Expand All @@ -52,3 +56,48 @@ func (h B4) MarshalText() ([]byte, error) {
func (h *B4) UnmarshalText(text []byte) error {
return UnmarshalTextHelper(h[:], text)
}

// String returns the hex string representation of B4.
func (h B4) String() string {
return hex.FromBytes(h[:]).Unwrap()
}

/* -------------------------------------------------------------------------- */
/* JSONMarshaler */
/* -------------------------------------------------------------------------- */

// UnmarshalJSON implements the json.Unmarshaler interface for B4.
func (h *B4) UnmarshalJSON(input []byte) error {
return unmarshalJSONHelper(h[:], input)
}

/* -------------------------------------------------------------------------- */
/* SSZMarshaler */
/* -------------------------------------------------------------------------- */

// SizeSSZ returns the size of its SSZ encoding in bytes.
func (h B4) SizeSSZ() int {
return B4Size
}

// MarshalSSZ implements the SSZ marshaling for B4.
func (h B4) MarshalSSZ() ([]byte, error) {
return h[:], nil
}

// IsFixed returns true if the length of the B4 is fixed.
func (h B4) IsFixed() bool {
return true
}

// Type returns the type of the B4.
func (h B4) Type() types.Type {
return types.Composite
}

// HashTreeRoot returns the hash tree root of the B4.
func (h B4) HashTreeRoot() ([32]byte, error) {
var result [32]byte
copy(result[:], h[:])
return result, nil
}
Loading

0 comments on commit bc9f002

Please sign in to comment.