-
Notifications
You must be signed in to change notification settings - Fork 22
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
Showing
5 changed files
with
152 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
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,80 @@ | ||
from typing import ( | ||
Sequence, | ||
Union, | ||
) | ||
|
||
from ssz.exceptions import ( | ||
DeserializationError, | ||
SerializationError, | ||
) | ||
from ssz.sedes.base import ( | ||
BaseCompositeSedes, | ||
) | ||
from ssz.utils import ( | ||
merkleize, | ||
mix_in_length, | ||
pack_bitvector_bitlist, | ||
) | ||
|
||
BytesOrByteArray = Union[bytes, bytearray] | ||
|
||
|
||
class Bitlist(BaseCompositeSedes[BytesOrByteArray, bytes]): | ||
def __init__(self, length: int) -> None: | ||
if length < 0: | ||
raise TypeError("Max length cannot be negative") | ||
self.length = length | ||
|
||
# | ||
# Size | ||
# | ||
is_fixed_sized = False | ||
|
||
def get_fixed_size(self): | ||
raise ValueError("byte list has no static size") | ||
|
||
# | ||
# Serialization | ||
# | ||
def serialize(self, value: BytesOrByteArray) -> bytes: | ||
len_value = len(value) | ||
if len_value > self.length: | ||
raise SerializationError( | ||
f"Cannot serialize length {len_value} bytes as Bitlist[{self.length}]" | ||
) | ||
|
||
if len_value == 0: | ||
return b'\x01' | ||
|
||
as_bytearray = [0] * (len_value // 8 + 1) | ||
for i in range(len_value): | ||
as_bytearray[i // 8] |= value[i] << (i % 8) | ||
as_bytearray[len_value // 8] |= 1 << (len_value % 8) | ||
return bytes(as_bytearray) | ||
|
||
# | ||
# Deserialization | ||
# | ||
def deserialize(self, data: bytes) -> bytes: | ||
as_integer = int.from_bytes(data, 'little') | ||
len_value = get_bitlist_len(as_integer) | ||
|
||
if len_value > self.length: | ||
raise DeserializationError( | ||
f"Cannot deserialize length {len_value} data as bytes{self.length}" | ||
) | ||
|
||
return tuple( | ||
bool((data[index // 8] >> index % 8) % 2) | ||
for index in range(len_value) | ||
) | ||
|
||
# | ||
# Tree hashing | ||
# | ||
def hash_tree_root(self, value: Sequence[bool]) -> bytes: | ||
return mix_in_length(merkleize(pack_bitvector_bitlist(value)), len(value)) | ||
|
||
|
||
def get_bitlist_len(x): | ||
return x.bit_length() - 1 |
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 @@ | ||
import pytest | ||
|
||
from ssz import ( | ||
decode, | ||
encode, | ||
) | ||
from ssz.sedes import ( | ||
Bitlist, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'size, value, expected', | ||
( | ||
(16, tuple(), b'\x01'), | ||
(16, (0b1, 0b0,), b'\x05'), | ||
(16, (0b1,) + (0b0,) * 15, b'\x01\x00\x01'), | ||
), | ||
) | ||
def test_bitlist_serialize_values(size, value, expected): | ||
Foo = Bitlist(size) | ||
assert encode(value, Foo) == expected | ||
assert Foo.serialize(bytearray(value)) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'size, value,expected', | ||
( | ||
(16, b'\x01', tuple()), | ||
(16, b'\x05', (True, False,)), | ||
(16, b'\x01\x00\x01', (True,) + (False,) * 15), | ||
), | ||
) | ||
def test_bitlist_deserialize_values(size, value, expected): | ||
Foo = Bitlist(size) | ||
assert Foo.deserialize(value) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'size, value', | ||
( | ||
# (16, tuple()), | ||
(16, (True, False,)), | ||
(16, (True,) + (False,) * 15), | ||
), | ||
) | ||
def test_bitlist_round_trip_no_sedes(size, value): | ||
Foo = Bitlist(size) | ||
assert decode(encode(value, Foo), Foo) == value |
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