Skip to content

Commit

Permalink
Pad upon poly construction
Browse files Browse the repository at this point in the history
Signed-off-by: litt3 <102969658+litt3@users.noreply.github.com>
  • Loading branch information
litt3 committed Jan 16, 2025
1 parent d66ddf2 commit f6021ea
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 42 deletions.
6 changes: 2 additions & 4 deletions api/clients/codecs/coeff_poly.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ type coeffPoly struct {
// coeffPolyFromBytes creates a new coeffPoly from bytes. This function performs the necessary checks to guarantee that the
// bytes are well-formed, and returns a new object if they are
func coeffPolyFromBytes(bytes []byte) (*coeffPoly, error) {
if !encoding.IsPowerOfTwo(len(bytes)) {
return nil, fmt.Errorf("bytes have length %d, expected a power of 2", len(bytes))
}
paddedBytes := encoding.PadToPowerOfTwo(bytes)

fieldElements, err := rs.BytesToFieldElements(bytes)
fieldElements, err := rs.BytesToFieldElements(paddedBytes)
if err != nil {
return nil, fmt.Errorf("deserialize field elements: %w", err)
}
Expand Down
9 changes: 2 additions & 7 deletions api/clients/codecs/encoded_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/encoding/utils/codec"
)

Expand Down Expand Up @@ -49,13 +48,9 @@ func (ep *encodedPayload) decode() (*Payload, error) {
return NewPayload(nonPaddedData[0:claimedLength]), nil
}

// toEvalPoly converts an encodedPayload into an evalPoly, by padding the encodedPayload bytes to the next power of 2
// toEvalPoly converts an encodedPayload into an evalPoly
func (ep *encodedPayload) toEvalPoly() (*evalPoly, error) {
paddedLength := core.NextPowerOf2(len(ep.bytes))
paddedBytes := make([]byte, paddedLength)
copy(paddedBytes, ep.bytes)

evalPoly, err := evalPolyFromBytes(paddedBytes)
evalPoly, err := evalPolyFromBytes(ep.bytes)
if err != nil {
return nil, fmt.Errorf("new eval poly: %w", err)
}
Expand Down
6 changes: 2 additions & 4 deletions api/clients/codecs/eval_poly.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ type evalPoly struct {
// evalPolyFromBytes creates a new evalPoly from bytes. This function performs the necessary checks to guarantee that the
// bytes are well-formed, and returns a new object if they are
func evalPolyFromBytes(bytes []byte) (*evalPoly, error) {
if !encoding.IsPowerOfTwo(len(bytes)) {
return nil, fmt.Errorf("bytes have length %d, expected a power of 2", len(bytes))
}
paddedBytes := encoding.PadToPowerOfTwo(bytes)

fieldElements, err := rs.BytesToFieldElements(bytes)
fieldElements, err := rs.BytesToFieldElements(paddedBytes)
if err != nil {
return nil, fmt.Errorf("deserialize field elements: %w", err)
}
Expand Down
9 changes: 6 additions & 3 deletions encoding/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func NextPowerOf2[T constraints.Integer](d T) T {
return T(math.Pow(2.0, nextPower))
}

// IsPowerOfTwo returns true if the input is a power of 2, otherwise false
func IsPowerOfTwo[T constraints.Integer](input T) bool {
return (input&(input-1) == 0) && input != 0
// PadToPowerOfTwo pads a byte slice to the next power of 2
// TODO: test to make sure this doesn't increase size if already a power of 2
func PadToPowerOfTwo(bytes []byte) []byte {
paddedLength := NextPowerOf2(len(bytes))
padding := make([]byte, paddedLength-len(bytes))
return append(bytes, padding...)
}
24 changes: 0 additions & 24 deletions encoding/utils/utils_test.go

This file was deleted.

0 comments on commit f6021ea

Please sign in to comment.