Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jianoaix committed Jul 10, 2024
1 parent f43f6ae commit 987362d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 16 additions & 0 deletions core/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ func (b Bundle) Size() uint64 {
return size
}

// Serialize returns the serialized bytes of the bundle.
//
// The bytes are packed in this format:
// <8 bytes header><chunk 1 bytes>chunk 2 bytes>...
//
// The header format:
// - First byte: describes the encoding format. Currently, only GnarkBundleEncodingFormat (1)
// is supported.
// - Remaining 7 bytes: describes the information about chunks.
//
// The chunk format will depend on the encoding format. With the GnarkBundleEncodingFormat,
// each chunk is formated as <32 bytes proof><32 bytes coeff>...<32 bytes coefff>, where the
// proof and coeffs are all encoded with Gnark.
func (b Bundle) Serialize() ([]byte, error) {
if len(b) == 0 {
return []byte{}, nil
Expand Down Expand Up @@ -209,6 +222,9 @@ func (b Bundle) Deserialize(data []byte) (Bundle, error) {
return nil, errors.New("invalid bundle data encoding format")
}
chunkLen := meta >> 8
if chunkLen == 0 {
return nil, errors.New("chunk length must be greater than zero")
}
chunkSize := bn254.SizeOfG1AffineCompressed + encoding.BYTES_PER_SYMBOL*int(chunkLen)
if (len(data)-8)%chunkSize != 0 {
return nil, errors.New("bundle data is invalid")
Expand Down
16 changes: 12 additions & 4 deletions core/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ func TestInvalidBundleDeser(t *testing.T) {
_, err := new(core.Bundle).Deserialize(tooSmallBytes)
assert.EqualError(t, err, "bundle data must have at least 8 bytes")

invalidBundle := make([]byte, 0, 8)
invalidFormat := make([]byte, 0, 8)
for i := 0; i < 7; i++ {
invalidBundle = append(invalidBundle, byte(0))
invalidFormat = append(invalidFormat, byte(0))
}
invalidBundle = append(invalidBundle, byte(0b01000000))
_, err = new(core.Bundle).Deserialize(invalidBundle)
invalidFormat = append(invalidFormat, byte(0b01000000))
_, err = new(core.Bundle).Deserialize(invalidFormat)
assert.EqualError(t, err, "invalid bundle data encoding format")

invliadChunkLen := make([]byte, 0, 8)
invliadChunkLen = append(invliadChunkLen, byte(1))
for i := 0; i < 7; i++ {
invliadChunkLen = append(invliadChunkLen, byte(0))
}
_, err = new(core.Bundle).Deserialize(invliadChunkLen)
assert.EqualError(t, err, "chunk length must be greater than zero")

data := make([]byte, 0, 9)
data = append(data, byte(1))
for i := 0; i < 6; i++ {
Expand Down

0 comments on commit 987362d

Please sign in to comment.