Skip to content
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: merklelization of containers #697

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
eac1b66
Fix spec-test runner command
Godspower-Eze Dec 12, 2023
4013dfe
Merge branch 'lambdaclass:main' into main
Godspower-Eze Dec 13, 2023
838c782
Merge branch 'main' of github.com:Godspower-Eze/lambda_ethereum_conse…
Godspower-Eze Dec 14, 2023
2ea4f10
Merge branch 'main' of github.com:Godspower-Eze/lambda_ethereum_conse…
Godspower-Eze Dec 14, 2023
8b29cf4
Merge branch 'main' of github.com:Godspower-Eze/lambda_ethereum_conse…
Godspower-Eze Jan 12, 2024
65e32dc
Merge branch 'main' of github.com:Godspower-Eze/lambda_ethereum_conse…
Godspower-Eze Jan 14, 2024
c0407e5
Merge branch 'main' of github.com:Godspower-Eze/lambda_ethereum_conse…
Godspower-Eze Jan 16, 2024
3a91893
feat: added boilerplate for merkleization benchmark
Godspower-Eze Jan 17, 2024
0073c8b
Merge branch 'main' into #issue597
Godspower-Eze Jan 19, 2024
74e73b4
update
Godspower-Eze Jan 19, 2024
b324445
update
Godspower-Eze Jan 22, 2024
579ef94
Merge branch 'main' into #issue597
Godspower-Eze Jan 22, 2024
3bae020
update
Godspower-Eze Jan 23, 2024
bed96e9
Merge branch '#issue597' of github.com:Godspower-Eze/lambda_ethereum_…
Godspower-Eze Jan 23, 2024
d6a390d
Merge branch 'main' into #issue597
Godspower-Eze Jan 23, 2024
143cca1
update
Godspower-Eze Jan 24, 2024
bad2c97
Merge branch 'main' into #issue597
Godspower-Eze Jan 24, 2024
77013de
Merge branch '#issue597' of github.com:Godspower-Eze/lambda_ethereum_…
Godspower-Eze Jan 24, 2024
208e62c
update
Godspower-Eze Jan 24, 2024
755b740
update
Godspower-Eze Jan 24, 2024
fe46df3
update
Godspower-Eze Jan 25, 2024
4c91091
update
Godspower-Eze Jan 25, 2024
028c9f2
Merge branch 'main' into #issue597
Godspower-Eze Jan 25, 2024
1190247
feat: added merkleization of containers
Godspower-Eze Jan 28, 2024
c923af7
Merge branch 'main' into merklelization-of-containers
Godspower-Eze Jan 28, 2024
9ed34f6
update
Godspower-Eze Jan 28, 2024
92f061a
update
Godspower-Eze Jan 28, 2024
82ca523
Merge branch 'main' into #issue597
Godspower-Eze Jan 28, 2024
5a5f29d
Merge branch '#issue597' of github.com:Godspower-Eze/lambda_ethereum_…
Godspower-Eze Jan 28, 2024
c3e911c
Merge branch '#issue597' into merklelization-of-containers
Godspower-Eze Jan 28, 2024
45a7e7f
Merge branch 'merklelization-of-containers' of github.com:Godspower-E…
Godspower-Eze Jan 28, 2024
8ded18e
update
Godspower-Eze Jan 28, 2024
5173bb8
Merge branch '#issue597' into merklelization-of-containers
Godspower-Eze Jan 28, 2024
c104a48
update
Godspower-Eze Jan 28, 2024
938c4be
update
Godspower-Eze Jan 28, 2024
3adcf8e
Merge branch '#issue597' into merklelization-of-containers
Godspower-Eze Jan 28, 2024
1ee15b7
update
Godspower-Eze Jan 28, 2024
70219b9
update
Godspower-Eze Jan 29, 2024
eb1a6b2
update
Godspower-Eze Jan 29, 2024
dade9d5
Merge branch '#issue597' into merklelization-of-containers
Godspower-Eze Jan 29, 2024
932a413
Merge branch 'main' into merklelization-of-containers
Godspower-Eze Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/spec/runners/ssz_generic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ defmodule SszGenericTestRunner do
{:container, module},
real_serialized,
real_deserialized,
_hash_tree_root
expected_hash_tree_root
) do
real_struct = struct!(module, real_deserialized)
{:ok, deserialized} = SszEx.decode(real_serialized, module)
assert deserialized == real_struct
{:ok, serialized} = SszEx.encode(real_struct, module)
assert serialized == real_serialized
actual_hash_tree_root = SszEx.hash_tree_root!(real_struct, module)
assert actual_hash_tree_root == expected_hash_tree_root
end

defp assert_ssz(
Expand All @@ -92,7 +94,7 @@ defmodule SszGenericTestRunner do

assert serialized == real_serialized

{:ok, actual_hash_tree_root} = SszEx.hash_tree_root(real_deserialized, schema)
actual_hash_tree_root = SszEx.hash_tree_root!(real_deserialized, schema)

assert actual_hash_tree_root == expected_hash_tree_root
end
Expand Down
40 changes: 40 additions & 0 deletions lib/ssz_ex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ defmodule LambdaEthereumConsensus.SszEx do
@spec hash_tree_root!(non_neg_integer, {:int, non_neg_integer}) :: Types.root()
def hash_tree_root!(value, {:int, size}), do: pack(value, {:int, size})

@spec hash_tree_root!(binary, {:bytes, non_neg_integer}) :: Types.root()
def hash_tree_root!(value, {:bytes, size}) do
packed_chunks = pack(value, {:bytes, size})
leaf_count = packed_chunks |> get_chunks_len() |> next_pow_of_two()
root = merkleize_chunks_with_virtual_padding(packed_chunks, leaf_count)
root
end

@spec hash_tree_root!(list(), {:list, any, non_neg_integer}) :: Types.root()
def hash_tree_root!(list, {:list, type, size}) do
{:ok, root} = hash_tree_root(list, {:list, type, size})
root
end

@spec hash_tree_root!(list(), {:vector, any, non_neg_integer}) :: Types.root()
def hash_tree_root!(vector, {:vector, type, size}) do
{:ok, root} = hash_tree_root(vector, {:vector, type, size})
root
end

@spec hash_tree_root!(struct(), atom()) :: Types.root()
def hash_tree_root!(container, module) when is_map(container) do
chunks =
module.schema()
|> Enum.reduce(<<>>, fn {key, schema}, acc_root ->
value = container |> Map.get(key)
root = hash_tree_root!(value, schema)
acc_root <> root
end)

leaf_count = chunks |> get_chunks_len() |> next_pow_of_two()
root = merkleize_chunks_with_virtual_padding(chunks, leaf_count)
root
end

@spec hash_tree_root(list(), {:list, any, non_neg_integer}) ::
{:ok, Types.root()} | {:error, String.t()}
def hash_tree_root(list, {:list, type, size}) do
Expand Down Expand Up @@ -207,6 +242,11 @@ defmodule LambdaEthereumConsensus.SszEx do
<<value::size(size)-little>> |> pack_bytes()
end

@spec pack(binary, {:bytes, non_neg_integer}) :: binary()
def pack(value, {:bytes, _size}) do
value |> pack_bytes()
end

@spec pack(list(), {:list | :vector, any, non_neg_integer}) :: binary() | :error
def pack(list, {type, schema, _}) when type in [:vector, :list] do
if variable_size?(schema) do
Expand Down
Loading