-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decode flag for GetBlob() by reworking codec layer (#592)
- Loading branch information
Showing
11 changed files
with
302 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package codecs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/Layr-Labs/eigenda/encoding/utils/codec" | ||
) | ||
|
||
type DefaultBlobCodec struct{} | ||
|
||
var _ BlobCodec = DefaultBlobCodec{} | ||
|
||
func NewDefaultBlobCodec() DefaultBlobCodec { | ||
return DefaultBlobCodec{} | ||
} | ||
|
||
func (v DefaultBlobCodec) EncodeBlob(rawData []byte) ([]byte, error) { | ||
codecBlobHeader := make([]byte, 32) | ||
// first byte is always 0 to ensure the codecBlobHeader is a valid bn254 element | ||
// encode version byte | ||
codecBlobHeader[1] = byte(DefaultBlobEncoding) | ||
|
||
// 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) | ||
|
||
// encode raw data modulo bn254 | ||
rawDataPadded := codec.ConvertByPaddingEmptyByte(rawData) | ||
|
||
// append raw data | ||
encodedData := append(codecBlobHeader, rawDataPadded...) | ||
|
||
return encodedData, nil | ||
} | ||
|
||
func (v DefaultBlobCodec) DecodeBlob(data []byte) ([]byte, error) { | ||
if len(data) < 32 { | ||
return nil, fmt.Errorf("blob does not contain 32 header bytes, meaning it is malformed") | ||
} | ||
|
||
length := binary.BigEndian.Uint32(data[2:6]) | ||
|
||
// decode raw data modulo bn254 | ||
decodedData := codec.RemoveEmptyByteFromPaddedBytes(data[32:]) | ||
|
||
// get non blob header data | ||
reader := bytes.NewReader(decodedData) | ||
rawData := make([]byte, length) | ||
n, err := reader.Read(rawData) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to copy unpadded data into final buffer, length: %d, bytes read: %d", length, n) | ||
} | ||
if uint32(n) != length { | ||
return nil, fmt.Errorf("data length does not match length prefix") | ||
} | ||
|
||
return rawData, nil | ||
} |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
api/clients/eigenda_client_utils.go → api/clients/codecs/fft.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package clients | ||
package codecs | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package codecs | ||
|
||
import "fmt" | ||
|
||
type IFFTCodec struct { | ||
writeCodec BlobCodec | ||
} | ||
|
||
var _ BlobCodec = IFFTCodec{} | ||
|
||
func NewIFFTCodec(writeCodec BlobCodec) IFFTCodec { | ||
return IFFTCodec{ | ||
writeCodec: writeCodec, | ||
} | ||
} | ||
|
||
func (v IFFTCodec) EncodeBlob(data []byte) ([]byte, error) { | ||
var err error | ||
data, err = v.writeCodec.EncodeBlob(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error encoding data: %w", err) | ||
} | ||
|
||
return IFFT(data) | ||
} | ||
|
||
func (v IFFTCodec) DecodeBlob(data []byte) ([]byte, error) { | ||
if len(data) == 0 { | ||
return nil, fmt.Errorf("blob has length 0, meaning it is malformed") | ||
} | ||
var err error | ||
data, err = FFT(data) | ||
if err != nil { | ||
return nil, fmt.Errorf("error FFTing data: %w", err) | ||
} | ||
|
||
return GenericDecodeBlob(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package codecs | ||
|
||
type NoIFFTCodec struct { | ||
writeCodec BlobCodec | ||
} | ||
|
||
var _ BlobCodec = NoIFFTCodec{} | ||
|
||
func NewNoIFFTCodec(writeCodec BlobCodec) NoIFFTCodec { | ||
return NoIFFTCodec{ | ||
writeCodec: writeCodec, | ||
} | ||
} | ||
|
||
func (v NoIFFTCodec) EncodeBlob(data []byte) ([]byte, error) { | ||
return v.writeCodec.EncodeBlob(data) | ||
} | ||
|
||
func (v NoIFFTCodec) DecodeBlob(data []byte) ([]byte, error) { | ||
return GenericDecodeBlob(data) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.