From c5bfcd3bdc7152e3ba9e822e730fe065bab9f136 Mon Sep 17 00:00:00 2001 From: Gaith Hallak Date: Thu, 5 May 2022 13:19:43 +0400 Subject: [PATCH] Add MCL_BLS12_381 types to from_fate_builtin (#382) * Add MCL_BLS12_381 types to from_fate_builtin * Add tests for mcl_bls12_381 types to sophia value --- src/aeso_vm_decode.erl | 7 +++++++ test/aeso_abi_tests.erl | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/aeso_vm_decode.erl b/src/aeso_vm_decode.erl index 54cf299..0a293dd 100644 --- a/src/aeso_vm_decode.erl +++ b/src/aeso_vm_decode.erl @@ -136,6 +136,8 @@ from_fate_builtin(QType, Val) -> Str = {id, [], "string"}, Adr = {id, [], "address"}, Hsh = {bytes_t, [], 32}, + I32 = {bytes_t, [], 32}, + I48 = {bytes_t, [], 48}, Qid = fun(Name) -> {qid, [], Name} end, Map = fun(KT, VT) -> {app_t, [], {id, [], "map"}, [KT, VT]} end, ChainTxArities = [3, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 0], @@ -208,6 +210,11 @@ from_fate_builtin(QType, Val) -> {["Chain", "base_tx"], {variant, ChainTxArities, 21, {}}} -> App(["Chain","GAAttachTx"], []); + {["MCL_BLS12_381", "fp"], X} -> + App(["MCL_BLS12_381", "fp"], [Chk(I32, X)]); + {["MCL_BLS12_381", "fr"], X} -> + App(["MCL_BLS12_381", "fr"], [Chk(I48, X)]); + _ -> throw(cannot_translate_to_sophia) end. diff --git a/test/aeso_abi_tests.erl b/test/aeso_abi_tests.erl index 30ec42c..9461d64 100644 --- a/test/aeso_abi_tests.erl +++ b/test/aeso_abi_tests.erl @@ -72,6 +72,27 @@ encode_decode_sophia_test() -> ok = Check("r", "{x = (\"foo\", 0), y = Red}"), ok. +to_sophia_value_mcl_bls12_381_test() -> + Code = "include \"BLS12_381.aes\"\n" + "contract C =\n" + " entrypoint test_bls12_381_fp(x : int) = BLS12_381.int_to_fp(x)\n" + " entrypoint test_bls12_381_fr(x : int) = BLS12_381.int_to_fr(x)\n" + " entrypoint test_bls12_381_g1(x : int) = BLS12_381.mk_g1(x, x, x)\n", + + Opts = [{backend, fate}], + + CallValue32 = aeb_fate_encoding:serialize({bytes, <<20:256>>}), + CallValue48 = aeb_fate_encoding:serialize({bytes, <<55:384>>}), + CallValueTp = aeb_fate_encoding:serialize({tuple, {{bytes, <<15:256>>}, {bytes, <<160:256>>}, {bytes, <<1234:256>>}}}), + + {ok, _} = aeso_compiler:to_sophia_value(Code, "test_bls12_381_fp", ok, CallValue32, Opts), + {error, _} = aeso_compiler:to_sophia_value(Code, "test_bls12_381_fp", ok, CallValue48, Opts), + {ok, _} = aeso_compiler:to_sophia_value(Code, "test_bls12_381_fr", ok, CallValue48, Opts), + {error, _} = aeso_compiler:to_sophia_value(Code, "test_bls12_381_fr", ok, CallValue32, Opts), + {ok, _} = aeso_compiler:to_sophia_value(Code, "test_bls12_381_g1", ok, CallValueTp, Opts), + + ok. + to_sophia_value_neg_test() -> Code = [ "contract Foo =\n" " entrypoint x(y : int) : string = \"hello\"\n" ],