Skip to content

Commit

Permalink
Massage naming
Browse files Browse the repository at this point in the history
Signed-off-by: litt3 <[email protected]>
  • Loading branch information
litt3 committed Jan 14, 2025
1 parent dbd8ea5 commit b98b07a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
30 changes: 20 additions & 10 deletions api/clients/codecs/blob_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ const (
DefaultBlobEncoding BlobEncodingVersion = 0x0
)

// EncodeBlob can never return an error, but to maintain the interface it is included
// so that it can be swapped for the IFFTCodec without changing the interface
func EncodeBlob(rawData []byte) []byte {
// EncodePayload accepts an arbitrary payload byte array, and encodes it.
//
// The returned bytes may be interpreted as a polynomial in evaluation form, where each contained field element of
// length 32 represents the evaluation of the polynomial at an expanded root of unity
//
// The returned bytes may or may not represent a blob. If the system is configured to distribute blobs in Coeff form,
// then the data returned from this function must be IFFTed to produce the final blob. If the system is configured to
// distribute blobs in Eval form, then the data returned from this function is the final blob representation.
func EncodePayload(payload []byte) []byte {
codecBlobHeader := make([]byte, 32)
// first byte is always 0 to ensure the codecBlobHeader is a valid bn254 element
// encode version byte
Expand All @@ -28,26 +34,30 @@ func EncodeBlob(rawData []byte) []byte {
// encode length as uint32
binary.BigEndian.PutUint32(
codecBlobHeader[2:6],
uint32(len(rawData))) // uint32 should be more than enough to store the length (approx 4gb)
uint32(len(payload))) // uint32 should be more than enough to store the length (approx 4gb)

// encode raw data modulo bn254
rawDataPadded := codec.ConvertByPaddingEmptyByte(rawData)
// encode payload modulo bn254
rawDataPadded := codec.ConvertByPaddingEmptyByte(payload)

// append raw data
encodedData := append(codecBlobHeader, rawDataPadded...)

return encodedData
}

func DecodeBlob(data []byte) ([]byte, error) {
if len(data) < 32 {
// DecodePayload accepts bytes representing an encoded payload, and returns the decoded payload
//
// This function expects the parameter bytes to be a polynomial in Eval form. In other words, if blobs in the system
// are being distributed in Coeff form, a blob must be FFTed prior to being passed into the function.
func DecodePayload(encodedPayload []byte) ([]byte, error) {
if len(encodedPayload) < 32 {
return nil, fmt.Errorf("blob does not contain 32 header bytes, meaning it is malformed")
}

length := binary.BigEndian.Uint32(data[2:6])
length := binary.BigEndian.Uint32(encodedPayload[2:6])

// decode raw data modulo bn254
decodedData := codec.RemoveEmptyByteFromPaddedBytes(data[32:])
decodedData := codec.RemoveEmptyByteFromPaddedBytes(encodedPayload[32:])

// get non blob header data
reader := bytes.NewReader(decodedData)
Expand Down
6 changes: 3 additions & 3 deletions api/clients/codecs/blob_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func randomByteSlice(length int64) []byte {
return b
}

// TestIFFTCodec tests the encoding and decoding of random byte streams
// TestCodec tests the encoding and decoding of random byte streams
func TestCodec(t *testing.T) {
// Number of test iterations
const iterations = 100
Expand All @@ -33,10 +33,10 @@ func TestCodec(t *testing.T) {
originalData := randomByteSlice(length.Int64() + 1) // ensure it's not length 0

// Encode the original data
encodedData := codecs.EncodeBlob(originalData)
encodedData := codecs.EncodePayload(originalData)

// Decode the encoded data
decodedData, err := codecs.DecodeBlob(encodedData)
decodedData, err := codecs.DecodePayload(encodedData)
if err != nil {
t.Fatalf("Iteration %d: failed to decode blob: %v", i, err)
}
Expand Down
4 changes: 2 additions & 2 deletions api/clients/eigenda_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (m *EigenDAClient) GetBlob(ctx context.Context, batchHeaderHash []byte, blo
return nil, fmt.Errorf("receive polynomial: %w", err)
}

decodedData, err := codecs.DecodeBlob(encodedBlob)
decodedData, err := codecs.DecodePayload(encodedBlob)
if err != nil {
return nil, fmt.Errorf("error decoding blob: %w", err)
}
Expand Down Expand Up @@ -223,7 +223,7 @@ func (m *EigenDAClient) putBlob(
errChan chan error) {
m.Log.Info("Attempting to disperse blob to EigenDA")

encodedPayload := codecs.EncodeBlob(rawData)
encodedPayload := codecs.EncodePayload(rawData)
polynomial, err := m.preparePolynomial(encodedPayload)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/clients/eigenda_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ func TestPutRetrieveBlobIFFTNoDecodeSuccess(t *testing.T) {

resultBlob, err := eigendaClient.GetBlob(context.Background(), []byte("mock-batch-header-hash"), 100)
require.NoError(t, err)
ifftBlob, err := codecs.IFFT(codecs.EncodeBlob(resultBlob))
ifftBlob, err := codecs.IFFT(codecs.EncodePayload(resultBlob))
require.NoError(t, err)
encodedBlob, err := codecs.FFT(ifftBlob)
require.NoError(t, err)
resultBlob, err = codecs.DecodeBlob(encodedBlob)
resultBlob, err = codecs.DecodePayload(encodedBlob)
require.NoError(t, err)
require.Equal(t, expectedBlob, resultBlob)
}
Expand Down

0 comments on commit b98b07a

Please sign in to comment.