-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bitlist and bitvector ssz support #494
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! I reviewed the PR and requested some more changes. Also, this is a nit, but can we have the encoding functions first and the decoding functions afterwards?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Fernando, last changes of the PR probably. Thanks for the work on this.
lib/ssz_ex.ex
Outdated
num_bytes = byte_size(bit_list) | ||
num_bits = num_bytes * @bits_per_byte | ||
padding = num_bits - bit_size(bit_list) | ||
formatted_bit_list = <<0::size(padding), bit_list::bitstring>> | ||
len = length_of_bitlist(formatted_bit_list) | ||
|
||
cond do | ||
len < 0 -> | ||
{:error, "missing length information"} | ||
|
||
div(len, @bits_per_byte) + 1 != num_bytes -> | ||
{:error, "invalid byte count"} | ||
|
||
len > max_size -> | ||
{:error, "out of bounds"} | ||
|
||
true -> | ||
pre_trailing_bits_size = (num_bytes - 1) * @bits_per_byte | ||
<<pre::size(pre_trailing_bits_size), last_byte::binary>> = formatted_bit_list | ||
<<_trailing::1, last::size(7)>> = last_byte | ||
|
||
cond do | ||
last == 0 -> | ||
{:ok, pre} | ||
|
||
pre == 0 -> | ||
{:ok, <<last::size(len)>>} | ||
|
||
true -> | ||
{:ok, <<pre::size(pre_trailing_bits_size), last::size(len - pre_trailing_bits_size)>>} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still overly complicated and doesn't address the padding problem. We can simplify this to something shorter:
Again, let's address this. It can be done in a simpler way:
<<pre::size(byte_size(bitlist)-1), last_byte::1>> = bitlist
decoded = <<pre, remove_trailing_bit(last_byte)>>
cond do
decoded == <<>> -> {:error, "missing length information"}
# Other edge cases
true -> decoded
end
And that's it. The only missing thing is to code the removal of the trailing bit. Instead of using the length_of_bitlist you can just code a new function that's similar to leading_zeros (matching each possible byte) that ignores the first 1
bit and returns the rest. In the case of a <<0>>
byte you can just return <<>>
and the rest of the functionality will stil work.
lib/ssz_ex.ex
Outdated
end | ||
end | ||
|
||
defp encode_bitvector(bit_vector, size) when bit_size(bit_vector) == size, do: {:ok, bit_vector} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of {:ok, bit_vector}
here let's return {:ok, BitVector.to_bytes(bit_vector)}
, which is a fix that has been added a couple of weeks ago (BitVector.new()
was modified too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a warning about the opaque type:
Call does not have expected opaque term of type LambdaEthereumConsensus.Utils.BitVector.t() in the 1st position
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a BitVector.bit_size
guard in the BitVector
and use it here to see if the issue gets fixed? On guards: https://hexdocs.pm/elixir/main/Kernel.html#defguard/1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Motivation
Closes #479 and #480 only encoding/decoding support