-
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 IFFT Encoding to the High Level Client (#583)
Co-authored-by: Teddy Knox <[email protected]>
- Loading branch information
Showing
10 changed files
with
316 additions
and
129 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package codecs | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type BlobEncodingVersion byte | ||
|
||
// All blob encodings are IFFT'd before being dispersed | ||
const ( | ||
// This minimal blob encoding includes a version byte, a length uint32, and 31 byte field element mapping. | ||
DefaultBlobEncoding BlobEncodingVersion = 0x0 | ||
) | ||
|
||
type BlobCodec interface { | ||
DecodeBlob(encodedData []byte) ([]byte, error) | ||
EncodeBlob(rawData []byte) ([]byte, error) | ||
} | ||
|
||
func BlobEncodingVersionToCodec(version BlobEncodingVersion) (BlobCodec, error) { | ||
switch version { | ||
case DefaultBlobEncoding: | ||
return DefaultBlobEncodingCodec{}, nil | ||
default: | ||
return nil, fmt.Errorf("unsupported blob encoding version: %x", version) | ||
} | ||
} |
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,49 @@ | ||
package codecs | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
|
||
"github.com/Layr-Labs/eigenda/encoding/utils/codec" | ||
) | ||
|
||
type DefaultBlobEncodingCodec struct{} | ||
|
||
var _ BlobCodec = DefaultBlobEncodingCodec{} | ||
|
||
func (v DefaultBlobEncodingCodec) EncodeBlob(rawData []byte) ([]byte, error) { | ||
// encode blob encoding version byte | ||
codecBlobHeader := EncodeCodecBlobHeader(byte(DefaultBlobEncoding), uint32(len(rawData))) | ||
|
||
// encode raw data modulo bn254 | ||
rawDataPadded := codec.ConvertByPaddingEmptyByte(rawData) | ||
|
||
// append raw data | ||
encodedData := append(codecBlobHeader, rawDataPadded...) | ||
|
||
return encodedData, nil | ||
} | ||
|
||
func (v DefaultBlobEncodingCodec) DecodeBlob(encodedData []byte) ([]byte, error) { | ||
|
||
_, length, err := DecodeCodecBlobHeader(encodedData[:32]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// decode raw data modulo bn254 | ||
decodedData := codec.RemoveEmptyByteFromPaddedBytes(encodedData[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 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,30 @@ | ||
package codecs | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
) | ||
|
||
func EncodeCodecBlobHeader(version byte, length uint32) []byte { | ||
codecBlobHeader := make([]byte, 32) | ||
// first byte is always 0 to ensure the codecBlobHeader is a valid bn254 element | ||
// encode version byte | ||
codecBlobHeader[1] = version | ||
|
||
// encode length as uint32 | ||
binary.BigEndian.PutUint32(codecBlobHeader[2:6], length) // uint32 should be more than enough to store the length (approx 4gb) | ||
return codecBlobHeader | ||
} | ||
|
||
func DecodeCodecBlobHeader(codecBlobHeader []byte) (byte, uint32, error) { | ||
// make sure the codecBlobHeader is 32 bytes long | ||
if len(codecBlobHeader) != 32 { | ||
err := fmt.Errorf("codecBlobHeader must be exactly 32 bytes long, but got %d bytes", len(codecBlobHeader)) | ||
return 0, 0, err | ||
} | ||
|
||
version := codecBlobHeader[1] | ||
length := binary.BigEndian.Uint32(codecBlobHeader[2:6]) | ||
|
||
return version, length, nil | ||
} |
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
Oops, something went wrong.