-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d594e1e
commit 06a2583
Showing
7 changed files
with
408 additions
and
0 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,108 @@ | ||
// Copyright 2023 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package scale | ||
|
||
const ( | ||
// maxLen equivalent of `ARCH32BIT_BITSLICE_MAX_BITS` in parity-scale-codec | ||
maxLen = 268435455 | ||
// bitSize is the number of bits in a byte | ||
bitSize = 8 | ||
) | ||
|
||
// BitVec represents rust's `bitvec::BitVec` in SCALE | ||
// It is encoded as a compact u32 representing the number of bits in the vector | ||
// followed by the actual bits, rounded up to the nearest byte | ||
type BitVec interface { | ||
// Bits returns the bits in the BitVec | ||
Bits() []uint8 | ||
// Bytes returns the byte representation of the Bits | ||
Bytes() []byte | ||
// Size returns the number of bits in the BitVec | ||
Size() uint | ||
} | ||
|
||
// bitVec implements BitVec | ||
type bitVec struct { | ||
size uint `scale:"1"` | ||
bits []uint8 `scale:"2"` | ||
} | ||
|
||
// NewBitVec returns a new BitVec with the given bits | ||
func NewBitVec(bits []uint8) BitVec { | ||
var size uint | ||
if bits != nil { | ||
size = uint(len(bits)) | ||
} | ||
|
||
return &bitVec{ | ||
size: size, | ||
bits: bits, | ||
} | ||
} | ||
|
||
// Bits returns the bits in the BitVec | ||
func (bv *bitVec) Bits() []uint8 { | ||
return bv.bits | ||
} | ||
|
||
// Bytes returns the byte representation of the BitVec.Bits | ||
func (bv *bitVec) Bytes() []byte { | ||
var b []byte | ||
for i := uint(0); i < bv.size; i += bitSize { | ||
end := i + bitSize | ||
if end > bv.size { | ||
end = bv.size | ||
} | ||
chunk := bv.bits[i:end] | ||
b = append(b, bitsToBytes(chunk)...) | ||
} | ||
return b | ||
} | ||
|
||
// Size returns the number of bits in the BitVec | ||
func (bv *bitVec) Size() uint { | ||
return bv.size | ||
} | ||
|
||
// bitsToBytes converts a slice of bits to a slice of bytes | ||
func bitsToBytes(bits []uint8) []byte { | ||
bitLength := len(bits) | ||
numOfBytes := (bitLength + (bitSize - 1)) / bitSize | ||
bytes := make([]byte, numOfBytes) | ||
|
||
if len(bits)%bitSize != 0 { | ||
// Pad with zeros to make the number of bits a multiple of bitSize | ||
pad := make([]uint8, bitSize-len(bits)%bitSize) | ||
bits = append(bits, pad...) | ||
} | ||
|
||
for i := 0; i < bitLength; i++ { | ||
if bits[i] == 1 { | ||
byteIndex := i / 8 | ||
bitIndex := i % 8 | ||
bytes[byteIndex] |= 1 << bitIndex | ||
} | ||
} | ||
|
||
return bytes | ||
} | ||
|
||
// bytesToBits converts a slice of bytes to a slice of bits | ||
func bytesToBits(b []byte, size uint) []uint8 { | ||
var bits []uint8 | ||
for _, uint8val := range b { | ||
end := size | ||
if end > bitSize { | ||
end = bitSize | ||
} | ||
size -= end | ||
|
||
for j := uint(0); j < end; j++ { | ||
bit := (uint8val >> j) & 1 | ||
bits = append(bits, bit) | ||
} | ||
} | ||
|
||
return bits | ||
} |
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,183 @@ | ||
// Copyright 2023 ChainSafe Systems (ON) | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
package scale | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func NewTestBitVec(size uint, bits []uint8) BitVec { | ||
return &bitVec{ | ||
size: size, | ||
bits: bits, | ||
} | ||
} | ||
|
||
func TestBitVec(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
wantBitVec BitVec | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty_bitvec", | ||
in: "0x00", | ||
wantBitVec: NewBitVec(nil), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "1_byte", | ||
in: "0x2055", | ||
wantBitVec: NewBitVec([]uint8{1, 0, 1, 0, 1, 0, 1, 0}), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "4_bytes", | ||
in: "0x645536aa01", | ||
wantBitVec: NewBitVec([]uint8{ | ||
1, 0, 1, 0, 1, 0, 1, 0, | ||
0, 1, 1, 0, 1, 1, 0, 0, | ||
0, 1, 0, 1, 0, 1, 0, 1, | ||
1}), | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
resultBytes, err := common.HexToBytes(tt.in) | ||
require.NoError(t, err) | ||
|
||
bv := NewBitVec(nil) | ||
err = Unmarshal(resultBytes, &bv) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tt.wantBitVec.Size(), bv.Size()) | ||
require.Equal(t, tt.wantBitVec.Size(), bv.Size()) | ||
|
||
b, err := Marshal(bv) | ||
require.NoError(t, err) | ||
require.Equal(t, resultBytes, b) | ||
}) | ||
} | ||
} | ||
|
||
func TestBitVecBytes(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in BitVec | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty_bitvec", | ||
in: NewBitVec(nil), | ||
want: []byte(nil), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "1_byte", | ||
in: NewBitVec([]uint8{1, 0, 1, 0, 1, 0, 1, 0}), | ||
want: []byte{0x55}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "4_bytes", | ||
in: NewBitVec([]uint8{ | ||
1, 0, 1, 0, 1, 0, 1, 0, | ||
0, 1, 1, 0, 1, 1, 0, 0, | ||
0, 1, 0, 1, 0, 1, 0, 1, | ||
1}), | ||
want: []byte{0x55, 0x36, 0xaa, 0x1}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
require.Equal(t, tt.want, tt.in.Bytes()) | ||
}) | ||
} | ||
} | ||
|
||
func TestBitVecBytesToBits(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in []byte | ||
want []uint8 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty", | ||
in: []byte(nil), | ||
want: []uint8(nil), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "1_byte", | ||
in: []byte{0x55}, | ||
want: []uint8{1, 0, 1, 0, 1, 0, 1, 0}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "4_bytes", | ||
in: []byte{0x55, 0x36, 0xaa, 0x1}, | ||
want: []uint8{1, 0, 1, 0, 1, 0, 1, 0, | ||
0, 1, 1, 0, 1, 1, 0, 0, | ||
0, 1, 0, 1, 0, 1, 0, 1, | ||
1, 0, 0, 0, 0, 0, 0, 0}, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
require.Equal(t, tt.want, bytesToBits(tt.in, uint(len(tt.in)*bitSize))) | ||
}) | ||
} | ||
} | ||
|
||
func TestBitVecBitsToBytes(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in []uint8 | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty", | ||
in: []uint8(nil), | ||
want: []byte{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "1_byte", | ||
in: []uint8{1, 0, 1, 0, 1, 0, 1, 0}, | ||
want: []byte{0x55}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "4_bytes", | ||
in: []uint8{1, 0, 1, 0, 1, 0, 1, 0, | ||
0, 1, 1, 0, 1, 1, 0, 0, | ||
0, 1, 0, 1, 0, 1, 0, 1, | ||
1, 0, 0, 0, 0, 0, 0, 0}, | ||
want: []byte{0x55, 0x36, 0xaa, 0x1}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
require.Equal(t, tt.want, bitsToBytes(tt.in)) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.