Add more hash primops

This commit is contained in:
Ulf Norell 2019-01-21 16:53:27 +01:00
parent 4bedbfee61
commit 5fd24fec86
2 changed files with 30 additions and 4 deletions

View File

@ -149,11 +149,16 @@ global_env() ->
{["Map", "member"], Fun([K, Map(K, V)], Bool)}, {["Map", "member"], Fun([K, Map(K, V)], Bool)},
{["Map", "size"], Fun1(Map(K, V), Int)}, {["Map", "size"], Fun1(Map(K, V), Int)},
%% Crypto/Curve operations %% Crypto/Curve operations
{["Crypto", "ecverify"], Fun([Hash, Address, SignId], Bool)}, {["Crypto", "ecverify"], Fun([Hash, Address, SignId], Bool)},
{["Crypto", "sha3"], Fun1(A, Hash)},
{["Crypto", "sha256"], Fun1(A, Hash)},
{["Crypto", "blake2b"], Fun1(A, Hash)},
%% Strings %% Strings
{["String", "length"], Fun1(String, Int)}, {["String", "length"], Fun1(String, Int)},
{["String", "concat"], Fun([String, String], String)}, {["String", "concat"], Fun([String, String], String)},
{["String", "sha3"], Fun1(String, Hash)}, {["String", "sha3"], Fun1(String, Hash)},
{["String", "sha256"], Fun1(String, Hash)},
{["String", "blake2b"], Fun1(String, Hash)},
%% Conversion %% Conversion
{["Int", "to_str"], Fun1(Int, String)}, {["Int", "to_str"], Fun1(Int, String)},
{["Address", "to_str"], Fun1(Address, String)} {["Address", "to_str"], Fun1(Address, String)}

View File

@ -322,6 +322,17 @@ ast_body(?qid_app(["Crypto", "ecverify"], [Msg, PK, Sig], _, _), Icode) ->
[ast_body(Msg, Icode), ast_body(PK, Icode), ast_body(Sig, Icode)], [ast_body(Msg, Icode), ast_body(PK, Icode), ast_body(Sig, Icode)],
[word, word, sign_t()], word); [word, word, sign_t()], word);
ast_body(?qid_app(["Crypto", "sha3"], [Term], [Type], _), Icode) ->
generic_hash_primop(?PRIM_CALL_CRYPTO_SHA3, Term, Type, Icode);
ast_body(?qid_app(["Crypto", "sha256"], [Term], [Type], _), Icode) ->
generic_hash_primop(?PRIM_CALL_CRYPTO_SHA256, Term, Type, Icode);
ast_body(?qid_app(["Crypto", "blake2b"], [Term], [Type], _), Icode) ->
generic_hash_primop(?PRIM_CALL_CRYPTO_BLAKE2B, Term, Type, Icode);
ast_body(?qid_app(["String", "sha256"], [String], _, _), Icode) ->
string_hash_primop(?PRIM_CALL_CRYPTO_SHA256_STRING, String, Icode);
ast_body(?qid_app(["String", "blake2b"], [String], _, _), Icode) ->
string_hash_primop(?PRIM_CALL_CRYPTO_BLAKE2B_STRING, String, Icode);
%% Strings %% Strings
%% -- String length %% -- String length
ast_body(?qid_app(["String", "length"], [String], _, _), Icode) -> ast_body(?qid_app(["String", "length"], [String], _, _), Icode) ->
@ -599,6 +610,16 @@ prim_call(Prim, Amount, Args, ArgTypes, OutType) ->
type_hash= #integer{value = TypeHash} type_hash= #integer{value = TypeHash}
}. }.
generic_hash_primop(PrimOp, Term, Type, Icode) ->
ArgType = ast_type(Type, Icode),
TypeValue = type_value(ArgType),
prim_call(PrimOp, #integer{value = 0},
[TypeValue, ast_body(Term, Icode)],
[typerep, ArgType], word).
string_hash_primop(PrimOp, String, Icode) ->
prim_call(PrimOp, #integer{value = 0}, [ast_body(String, Icode)], [string], word).
make_type_def(Args, Def, Icode = #{ type_vars := TypeEnv }) -> make_type_def(Args, Def, Icode = #{ type_vars := TypeEnv }) ->
TVars = [ X || {tvar, _, X} <- Args ], TVars = [ X || {tvar, _, X} <- Args ],
fun(Types) -> fun(Types) ->