Pre-compute and switch to Blake2b for event name hash

This commit is contained in:
Hans Svensson 2019-06-26 13:30:45 +02:00
parent 02ba4b265b
commit 2bf5e59e2b
4 changed files with 10 additions and 10 deletions

View File

@ -4,6 +4,7 @@
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"c63ac88"}}}
, {getopt, "1.0.1"}
, {eblake2, "1.0.0"}
, {jsx, {git, "https://github.com/talentdeficit/jsx.git",
{tag, "2.8.0"}}}
]}.

View File

@ -137,6 +137,7 @@
vars => [var_name()],
functions := #{ fun_name() => fun_def() } }.
-define(HASH_BYTES, 32).
%% -- Entrypoint -------------------------------------------------------------
%% Main entrypoint. Takes typed syntax produced by aeso_ast_infer_types:infer/1,2
@ -829,8 +830,8 @@ event_function(_Env = #{event_type := {variant_t, EventCons}}, EventType = {vari
|| {I, {constr_t, Ann, {con, _, Name}, _}} <- indexed(EventCons) ],
Arities = [length(Ts) || Ts <- FCons],
Case = fun({Name, Tag, Ixs}) ->
%% TODO: precompute (needs dependency)
Hash = {op, crypto_sha3, [{lit, {string, list_to_binary(Name)}}]},
{ok, HashValue} = eblake2:blake2b(?HASH_BYTES, list_to_binary(Name)),
Hash = {lit, {bytes, HashValue}},
Vars = [ "arg" ++ integer_to_list(I) || I <- lists:seq(1, length(Ixs)) ],
IVars = lists:zip(Ixs, Vars),
Payload =

View File

@ -62,6 +62,8 @@ v(X) when is_list(X) -> #var_ref{name = X}.
option_none() -> {tuple, [{integer, 0}]}.
option_some(X) -> {tuple, [{integer, 1}, X]}.
-define(HASH_BYTES, 32).
-define(call(Fun, Args), #funcall{ function = #var_ref{ name = {builtin, Fun} }, args = Args }).
-define(I(X), {integer, X}).
-define(V(X), v(X)).
@ -93,12 +95,6 @@ operand(A) when is_atom(A) -> v(A);
operand(I) when is_integer(I) -> {integer, I};
operand(T) -> T.
str_to_icode(String) when is_list(String) ->
str_to_icode(list_to_binary(String));
str_to_icode(BinStr) ->
Cpts = [size(BinStr) | aeb_memory:binary_to_words(BinStr)],
#tuple{ cpts = [ #integer{value = X} || X <- Cpts ] }.
check_event_type(Icode) ->
case maps:get(event_type, Icode) of
{variant_t, Cons} ->
@ -192,7 +188,8 @@ builtin_event(EventT) ->
Types = [ T || {_Ix, T} <- IxTypes ],
Indexed = [ Ix(Type, Var) || {Var, {indexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
Data = [ {Type, Var} || {Var, {notindexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
EvtIndex = {unop, 'sha3', str_to_icode(Con)},
{ok, <<EvtIndexN:256>>} = eblake2:blake2b(?HASH_BYTES, list_to_binary(Con)),
EvtIndex = {integer, EvtIndexN},
{event, lists:reverse(Indexed) ++ [EvtIndex], Payload(Data)}
end,
Pat = fun(Tag, Types) -> {tuple, [{integer, Tag} | ArgPats(Types)]} end,

View File

@ -8,7 +8,8 @@
jsx,
syntax_tools,
getopt,
aebytecode
aebytecode,
eblake2
]},
{env,[]},
{modules, []},