From a9905927824ea868ea5c2dddd9559de08a6dfe68 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 17 Sep 2024 13:40:56 +0200 Subject: [PATCH 01/15] feat: enable ec_add and ec_mul --- Makefile | 6 +- kakarot_scripts/utils/starknet.py | 11 +- poetry.lock | 165 +++++++++--------- .../src/EvmPrecompiles/EvmPrecompiles.sol | 52 ++++++ src/kakarot/precompiles/precompiles.cairo | 4 +- .../EvmPrecompiles/test_evm_precompiles.py | 95 ++++++++++ .../src/kakarot/precompiles/test_ec_add.cairo | 69 -------- tests/src/kakarot/precompiles/test_ec_add.py | 11 -- tests/src/kakarot/precompiles/test_ec_mul.py | 8 - .../kakarot/precompiles/test_ec_recover.cairo | 26 --- 10 files changed, 244 insertions(+), 203 deletions(-) create mode 100644 solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol create mode 100644 tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py delete mode 100644 tests/src/kakarot/precompiles/test_ec_add.cairo delete mode 100644 tests/src/kakarot/precompiles/test_ec_add.py delete mode 100644 tests/src/kakarot/precompiles/test_ec_mul.py delete mode 100644 tests/src/kakarot/precompiles/test_ec_recover.cairo diff --git a/Makefile b/Makefile index 0cce68d3b..717118495 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,11 @@ endif .PHONY: build test coverage clean -# 154615699 corresponds to release v0.1.7 of Kakarot SSJ. -KKRT_SSJ_RELEASE_ID = 154615699 +# 173874367 corresponds to release v0.1.12 of Kakarot SSJ. +KKRT_SSJ_RELEASE_ID = 173874367 # Kakarot SSJ artifacts for precompiles. KKRT_SSJ_BUILD_ARTIFACT_URL = $(shell curl -L https://api.github.com/repos/kkrt-labs/kakarot-ssj/releases/${KKRT_SSJ_RELEASE_ID} | jq -r '.assets[0].browser_download_url') -KATANA_VERSION = v1.0.0-alpha.11 +KATANA_VERSION = v1.0.0-alpha.12 ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) BUILD_DIR = build diff --git a/kakarot_scripts/utils/starknet.py b/kakarot_scripts/utils/starknet.py index e1a81898b..aef217e78 100644 --- a/kakarot_scripts/utils/starknet.py +++ b/kakarot_scripts/utils/starknet.py @@ -243,6 +243,12 @@ def get_deployments(): @cache def get_artifact(contract_name): + # Cairo 0 artifacts + artifacts = list(BUILD_DIR.glob(f"*{contract_name}*.json")) + if artifacts: + return Artifact(sierra=None, casm=artifacts[0]) + + # Cairo 1 artifacts artifacts = list(CAIRO_DIR.glob(f"**/*{contract_name}.*.json")) or list( BUILD_DIR_SSJ.glob(f"**/*{contract_name}.*.json") ) @@ -255,10 +261,7 @@ def get_artifact(contract_name): ) return Artifact(sierra=sierra, casm=casm) - artifacts = list(BUILD_DIR.glob(f"**/*{contract_name}*.json")) - if not artifacts: - raise FileNotFoundError(f"No artifact found for {contract_name}") - return Artifact(sierra=None, casm=artifacts[0]) + raise FileNotFoundError(f"No artifact found for {contract_name}") @cache diff --git a/poetry.lock b/poetry.lock index 60116a536..f686b83dd 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2847,77 +2847,77 @@ testing = ["pytest", "pytest-benchmark"] [[package]] name = "poseidon-py" -version = "0.1.4" +version = "0.1.5" description = "Python implementation of Poseidon hash" optional = false python-versions = ">=3.8" files = [ - {file = "poseidon_py-0.1.4-cp310-cp310-macosx_12_0_universal2.whl", hash = "sha256:8c2c81636b8389168181425f214da0e75f0b58df89241719373fcbba45c7b061"}, - {file = "poseidon_py-0.1.4-cp310-cp310-macosx_13_0_universal2.whl", hash = "sha256:6103971eb4c4e6f6a8b8d6d83a1e722771dbc04b29e94da7c202bc904efd1aed"}, - {file = "poseidon_py-0.1.4-cp310-cp310-macosx_14_0_universal2.whl", hash = "sha256:f1d269ef1dfaec4fe41226158cd914e8f74b75e937428d2c3de6c7c0cad8ab74"}, - {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:195a3b8acb9a3cb27c8a822619663f25f075057471f392ea18098ed83e920e10"}, - {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ee3f2995b0023dec31dee06cc5d9c12f5aa1d0e8d7cca7fba7b005145c04afcc"}, - {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a08ce457a4a01a3f0c83a5556c21e2e95f86fd7ec594f73f72704c239a489b23"}, - {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:71423bc1a8e422a1c82a5a786a0c01b8a024a00fc7cba883e01e85410d5bf275"}, - {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:3c8c4e54a616451c3420cdce1a8e52b5ffcdc604f431a9c6a0f3cd50ef2e1292"}, - {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:1ab0d2d10cdb23be125a918e553bcc7cdd27c48c30339d8d8822757e918706b5"}, - {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_s390x.whl", hash = "sha256:e57cbdf75398a4faf7b71b2c2b7462975e81047c3a592a2654e0d020aab184e4"}, - {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:64bff7984b02a79d4f6864a2d9276ad52d96a933483293bdf5eb2e1558e0994d"}, - {file = "poseidon_py-0.1.4-cp310-cp310-win32.whl", hash = "sha256:0b5fe9eb6e21a8478ea739d3316b28bf2e8de49dc2d2305768b2f14be04bc143"}, - {file = "poseidon_py-0.1.4-cp310-cp310-win_amd64.whl", hash = "sha256:fdabee5ffceb74836979a4497402dd0dd351ec387d3ecad5c627df8da8cf0f04"}, - {file = "poseidon_py-0.1.4-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:c1b8778a8b51d980b6780daf3fb11357942d941465f55ebf9eb0702638396b61"}, - {file = "poseidon_py-0.1.4-cp311-cp311-macosx_13_0_universal2.whl", hash = "sha256:89694d6ebaee6086f1d3e6223707acaeb4e9b32c61faf20c6c2685baaeba6326"}, - {file = "poseidon_py-0.1.4-cp311-cp311-macosx_14_0_universal2.whl", hash = "sha256:7cc93c044a03d7067d37c79a697a81562ae5899bd0150bf496520d418a4a774e"}, - {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f36dc34704540f2a2bbe18e2f195bcd0fa504b93962bd72f535e37578f264385"}, - {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:24a3b643f7e071883686bcb8422558fc1cf485b61a26a028554d60713cdd9aea"}, - {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:12237601c95127c0cb8875e81c04f26720069aed544dfff499a473e86a63f14f"}, - {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b421fa3e01e0a072622dcc770635dd1aeb2d8a77d334d5af40ae54033587a1df"}, - {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:947164c40ef49b47e366b3b838edab9efb42e598a7f95483880e1b3b84536131"}, - {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:95724cc0f6708a097521f7193af20e62ee63b2927b9775bcbf42ed1ada0653ae"}, - {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_s390x.whl", hash = "sha256:790a44a2858dc8bab9d80d9527a67232823c3fda2216cc4fb8e37775904b103b"}, - {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:f658750b779ebd09b30c047d671e4e18e90cbc4ab7bb006dfb5c4468fd5b9f61"}, - {file = "poseidon_py-0.1.4-cp311-cp311-win32.whl", hash = "sha256:48e689782cefd5237fac1001df34c2750e707721d309d9811e0095d3139a47d2"}, - {file = "poseidon_py-0.1.4-cp311-cp311-win_amd64.whl", hash = "sha256:5ecc75a778740fff3d4a3bc374fa9fe6868c08091ac199c43ef7f1d9e40c737c"}, - {file = "poseidon_py-0.1.4-cp312-cp312-macosx_12_0_universal2.whl", hash = "sha256:0eef537ac31cbf195a170e696c9f5b6d2d13ce3a0da98f37cf03f0d855ae2be1"}, - {file = "poseidon_py-0.1.4-cp312-cp312-macosx_13_0_universal2.whl", hash = "sha256:446048c88e8ee97c569032171c7cb057b9c739d2985109c55eb234ce66cd2d16"}, - {file = "poseidon_py-0.1.4-cp312-cp312-macosx_14_0_universal2.whl", hash = "sha256:91c259f85cd1b3a5f2d450a481d90bdd4b911d05bd6bcc51fd290d2fd7d66613"}, - {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b53d6eaecac2c7a37a66f7a55a451fe5abed0e317ebb26022e9937b43a54f8c9"}, - {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8ac18854c6305d80f0ccb270b261eb278bbc21f055b43737a8b55726d4caee3a"}, - {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:53edc8142cca4fe56eb2e2451eb77165925e2d7df25186fd5ab5b7d813555381"}, - {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6880dc2461a2c7336abb7f4774971a053bc04169dbfce5e08feecefad4fd49c2"}, - {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:514bc3df634a4ca5621bc0ea60e253257f8cc7773e1d3701e5e8f36a35f4d0f3"}, - {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:76d7cdb3c4a744573627f8170f659236737218245d04a7ead00e74bfb48ab28a"}, - {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_s390x.whl", hash = "sha256:8fa27f2797abdec641a26abb05fee82194ebb40fd4ded3fa385b8b143bef4d7f"}, - {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:53a78d0fcd1a7742f1d72ce9a2871790c0ae862dec62b18f58982c0ca07a3e79"}, - {file = "poseidon_py-0.1.4-cp312-cp312-win32.whl", hash = "sha256:1176e308c18ff878cc8763cb0dc03541d56775a75b8128f5da6d55ad041a6bd8"}, - {file = "poseidon_py-0.1.4-cp312-cp312-win_amd64.whl", hash = "sha256:9efac87f378ceb64c2b2bad5a2bf8678459ce7ae7947c12326effc4a700fe9e5"}, - {file = "poseidon_py-0.1.4-cp38-cp38-macosx_12_0_universal2.whl", hash = "sha256:d38726b1e330000a91fd01cce53c5ed6aaec030fa91f8c23d8a598eb630f6227"}, - {file = "poseidon_py-0.1.4-cp38-cp38-macosx_13_0_universal2.whl", hash = "sha256:026fd9b53c21e1ee9ca15c956e3650e15842f4e0f517580f7a645ac3c71b4b46"}, - {file = "poseidon_py-0.1.4-cp38-cp38-macosx_14_0_universal2.whl", hash = "sha256:f1940fae0aeb2aaa04abb71924c09b54cad845d273ec5a306c1ee27563efd3d3"}, - {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3021938f639c55d45584f8cb66d1d5a2bf50d98f2f536fa8ec9da63ff840631a"}, - {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:fcb8f9060de484c44c015c667527183cdf96f1f8e70df3e0c3e10a60900f81a0"}, - {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:06fc95034cd5a2843a06f0bcc9772ed8785c0092fccdc0516fb8c260bb77bf49"}, - {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d7064ea9b2e4f15761ffdd477cc034cef715c4e0c69724f448d525bd5fceefdc"}, - {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:ec15d4fdbdefc976340a18d3fe7525f8957ecd7552fd011dbda890b037aef28d"}, - {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:be89d40c36d815e7668321e2faea01ec2db434ab266685d67e8872c781ad6013"}, - {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_s390x.whl", hash = "sha256:cfcca7b463d247f423f420dff6b151cc6bf23771d86b637c5da3b14f08b5268b"}, - {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:e7eae5e8036bbc4ea01a056aa2eb687fb5ca7d91adfc242fdc672391886063a9"}, - {file = "poseidon_py-0.1.4-cp38-cp38-win32.whl", hash = "sha256:96dbe49c2af157982f090dde7c66bc4490bfd16ec3d0ad709add40ba23c3dd71"}, - {file = "poseidon_py-0.1.4-cp38-cp38-win_amd64.whl", hash = "sha256:baa43eed066e6985da5045dc1f0a432f60838582bfbfefe630ce2e506ac18acc"}, - {file = "poseidon_py-0.1.4-cp39-cp39-macosx_12_0_universal2.whl", hash = "sha256:a3aaadec7f16739f7ec9084c19b344bc4d937689c00a22e371e7f02993ac9199"}, - {file = "poseidon_py-0.1.4-cp39-cp39-macosx_13_0_universal2.whl", hash = "sha256:dc812bd192be78b28a06124e847122969ecd7fe217c957a2f177f11225cb09e3"}, - {file = "poseidon_py-0.1.4-cp39-cp39-macosx_14_0_universal2.whl", hash = "sha256:3753cd12acde11192eb9e61e7308cc254e6323d24d6d9e55177d5b295cc3d2e6"}, - {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:08deb5310f5f7d1aeff3b1ac3ed01cfae95be64adf9b4e6e7ef990d50f7348f6"}, - {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2cf8582f721410764b2da734b79be93123a32fc6b2710e78b36685514a8c6713"}, - {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:abdc9f2580144887e3d10a63a00dcde7c9180ac7683fddf314581a32ecc70cba"}, - {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8b39d3faae6164157a1b2ef0c6e5ef5658a7f48df18c6c090998c566b8447603"}, - {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:841ddcc7d7be6fde7b8927bb6cd47fedd8e8f83b761c95ee7a686f1d794df6b8"}, - {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:f06638fe92366bc99a2badf0ec052b02e3b89d79cc2ef400864707701c3919a6"}, - {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_s390x.whl", hash = "sha256:9c586a42871ddc1c9af22e15825f250152b961cf3a7f1144faed10589d1aa505"}, - {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:4a0caf2fa878cc2b7f4781e7f2574c65231b422257addca64d146b34bc15a7ad"}, - {file = "poseidon_py-0.1.4-cp39-cp39-win32.whl", hash = "sha256:d14aa6c9d76279e46eb6fde66e3e2629e3e461069dd76644b65c5ebed69114c8"}, - {file = "poseidon_py-0.1.4-cp39-cp39-win_amd64.whl", hash = "sha256:e246d46d81bcd465b399c5d7663ef59b31fe0acd3966253cbb53e12508b41d80"}, - {file = "poseidon_py-0.1.4.tar.gz", hash = "sha256:416139d80931c85fd117cbc5b47d2eb1daf25e52acd77b6dd889c4820592a479"}, + {file = "poseidon_py-0.1.5-cp310-cp310-macosx_12_0_universal2.whl", hash = "sha256:3a8b4cc09d28439764956c47a00e49aa36c23421f724b0ffbd2c62550325240e"}, + {file = "poseidon_py-0.1.5-cp310-cp310-macosx_13_0_universal2.whl", hash = "sha256:a7e611fe04d200eb782a0e7b6f8ee8e34da531a527f96685ff5f12117014fca8"}, + {file = "poseidon_py-0.1.5-cp310-cp310-macosx_14_0_universal2.whl", hash = "sha256:5070cb74a20c433c2a20e91e14f4a92a2f66b8001adaf15ed5febc941d5ea00f"}, + {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:15a6c334161ee8444fb908c0c2f9f2a4d14c6a6ace3397bd3b1ed7673244870b"}, + {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9a8899e5ea0ba5d1bd41cc54b337780030d9f68859e94bb124d76a751b124a63"}, + {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:02c1206449d327ffd8eadca80d7c122d8adfaa529639cf5e5a186fa81cd19977"}, + {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:755ea18163c156702160c5ede046903c7c2350de806ed92da80e7bae16f61e23"}, + {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:ec16f0c8f74d9de6b2083006e4f408ea77c1b68375a2670a43c04754fff92f05"}, + {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:67d31aebd0e00b1f27e27642e086b04951fa2696417a2ef8b5e11f1d86d097f2"}, + {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_s390x.whl", hash = "sha256:c47f68fbe13b9e68a0f0af005b329639f75edcb7c44def9030c0fdf37602ca3e"}, + {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:14ea566f3e79e59c5d30dfca1f8130953f95cc65aa259424a9c2d37906c63fa5"}, + {file = "poseidon_py-0.1.5-cp310-cp310-win32.whl", hash = "sha256:618c6af537bf0a2efdaacd5a9d511615d50ee21c31f098b1247f24a5b3542893"}, + {file = "poseidon_py-0.1.5-cp310-cp310-win_amd64.whl", hash = "sha256:c6778847c2f6a6a22c8898425adc960ef284d18925780ccd56f53821a66acf4b"}, + {file = "poseidon_py-0.1.5-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:af55e19a30e4c75a63c353e1f616d04e188bf901ddce1df3e8bc4aadaf6769e8"}, + {file = "poseidon_py-0.1.5-cp311-cp311-macosx_13_0_universal2.whl", hash = "sha256:c1c68d5db997356941433f95808885395f6c2f4132c8a80d6e189881a553d7df"}, + {file = "poseidon_py-0.1.5-cp311-cp311-macosx_14_0_universal2.whl", hash = "sha256:eead4b7b489e66747e6ff8bb73ef0ace3ae8a15e0622ff0b9415e0cf9d2e6c1c"}, + {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2efb15008ed0dad41777ffe427432107140313c77c764965612d4e4a6a426925"}, + {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:b218cd61003175791cd1d168559b4aa12cbe9fba2b630e34431d653227c6da05"}, + {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:57e67d2dfabab2085e8f30353c4c19eb53b02cd858ca138cb3aa32a03c34e673"}, + {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fcde98980879d4d145f25c51c06284564a255cbbd6726a1874712474a8b04137"}, + {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:aaa220a9a28246f3b76353697322b6588052d38e7ecaf18337eecaa6cfa7e6d2"}, + {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:b1750a301b426e3f6ca805672752860e81cf4f6cc2b3e6cd6816d4da78f2dd46"}, + {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_s390x.whl", hash = "sha256:1ffe57ffacab103c37606b70eda7dd97c857b3d462a7926038123b05be5ba457"}, + {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:4faeb29252770e10a08c4d6aa2a095c171668640f21d5721441f5015ab1dbc4c"}, + {file = "poseidon_py-0.1.5-cp311-cp311-win32.whl", hash = "sha256:df84226f66078d1aca460db3590b65400fcc1aa593c205baba96c0d25b80b39d"}, + {file = "poseidon_py-0.1.5-cp311-cp311-win_amd64.whl", hash = "sha256:b520591c30af7f0be29ea88404b931eecee8babe0d734e7446aed3e164f8ef83"}, + {file = "poseidon_py-0.1.5-cp312-cp312-macosx_12_0_universal2.whl", hash = "sha256:4f2d28448e5b0bea2622ca23667de8a27b1a6056068181c5a41e78be65f0d980"}, + {file = "poseidon_py-0.1.5-cp312-cp312-macosx_13_0_universal2.whl", hash = "sha256:311291b8caa39f7ebeb3c837f239a46e8214d30722f9e01ded62d79c0b061b8a"}, + {file = "poseidon_py-0.1.5-cp312-cp312-macosx_14_0_universal2.whl", hash = "sha256:06c07b1e2ad273d50b243121b1e94b28bf893bd4942fe1808f98d33cd2de2790"}, + {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:55501c7c881eaf406568db70a16c56c5f92c325eda65f9f741e75ec5cced7ad6"}, + {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:cbd1ed7d8567e057cc181542aff18dc0b11cf2c67593243830cc2fedb73b112f"}, + {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:19ae4ddd2b1d05d1ed2e3b38e58c3ac5940a46001b080d5112ce346be4d09681"}, + {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a1ba4956c6b886457d43e43a4465aed18f9bd1f23e7bd2c5388c9a74ae8f0842"}, + {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:3e4e76745181fb50c77813b1c7c845f92f2d3b4620bf784f43e01be5713e1c14"}, + {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:28c0f134533b827eb0bc883347ec38dca2a5559fb277aeeb4ac9cce7f6d30a16"}, + {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_s390x.whl", hash = "sha256:7bc3995f771b7d98f05b200c2c19c1ae6238bb9f625733042a6df9633219e762"}, + {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:f09faa440ff5f10099a3e667bb5f8781d27a7bc719f45119d08d12574a4d9281"}, + {file = "poseidon_py-0.1.5-cp312-cp312-win32.whl", hash = "sha256:4a7fad5110fc64125ef8fbbdc1d49798159b568e239dbef18ada8fd87eed1fc9"}, + {file = "poseidon_py-0.1.5-cp312-cp312-win_amd64.whl", hash = "sha256:0261221c68c36fd11cfcb91e5074953bfbe7a33031d806d3ab2dc5c7c1e70a2b"}, + {file = "poseidon_py-0.1.5-cp38-cp38-macosx_12_0_universal2.whl", hash = "sha256:ed73929274da455b214bc32e548888094ab42a02af683ccdad006a204299cd06"}, + {file = "poseidon_py-0.1.5-cp38-cp38-macosx_13_0_universal2.whl", hash = "sha256:5037c878654a5546e154f28a3f7a85fbfae3e6aaac6d02552fa233e382aa0b73"}, + {file = "poseidon_py-0.1.5-cp38-cp38-macosx_14_0_universal2.whl", hash = "sha256:38ede222b125a2d09e8f76c59289b1cd0d06c135428ca81f567fc5ae17216e03"}, + {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:68ea474600e445946f96bebadab480c12b11828d9942dbcc534707e0fceb0e6b"}, + {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e7c8d4a37474d2481c6e5f7016102cd7a3dc85f6c3a7b687ad1982e307a1f812"}, + {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:76d2a99a976126dc5632905a4e971b1f4477366f88757228b04de10b624b307e"}, + {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b504ee79a14e7456e5a7462792afe32d696fdb33819cdf447f9d2d3faa410690"}, + {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:13529af9d43bb3a200819b053a4f8b283b60b9f60dbdcc15343d3a41e0db4256"}, + {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:29ed2a89d0f5417d27130503f6c36e4df4649fdc947ff0b813368e914d562f83"}, + {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_s390x.whl", hash = "sha256:6cad01961590f198939e46554e50c506f908f7fd680407792501cc337a33cf05"}, + {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:c7d3ae493cb4b325dd4d394159f7b76a316f5da74682772060498985d2f838ad"}, + {file = "poseidon_py-0.1.5-cp38-cp38-win32.whl", hash = "sha256:95cbb69541bc1fba2497cb8709f4ab96fec847b2c261129bd9b3bec1ce832bf2"}, + {file = "poseidon_py-0.1.5-cp38-cp38-win_amd64.whl", hash = "sha256:4727b0d32fc80db43a2b6cc3a14a2250ae8c6cb8e30d342f84e68c72ae053032"}, + {file = "poseidon_py-0.1.5-cp39-cp39-macosx_12_0_universal2.whl", hash = "sha256:41bb6269dcb1ac1b57bbaf5d90167ef745541ce4199b0b94ee5a05c0f5c8b189"}, + {file = "poseidon_py-0.1.5-cp39-cp39-macosx_13_0_universal2.whl", hash = "sha256:8c98c7a810adc88695e83dbab94075deec8f2503fe29fc838f680b9eab57e837"}, + {file = "poseidon_py-0.1.5-cp39-cp39-macosx_14_0_universal2.whl", hash = "sha256:b296fc5f2e9fedf03ed8d112b5bc517654ed4d265e60e9029607b6221233d466"}, + {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cfc14ee08cd547e8d118a9d8e23455ae114df329a6f7822b2d32e2c8238a04a9"}, + {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6cc58d3a0f361739259f2140bbe2f85b6fcb9373664d5b3a504af79a687d9d45"}, + {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b878efb1e76c0287f0ca8f583dca74b8c3cc124dab972832af4bef6e017740a2"}, + {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d28bb34be71a105c89d7e4c2071afc568ed5851390fd70b009b50fe5e466ddd1"}, + {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:f7342a365ab53ad9524b2e1c6192dfbee6cd10505af4ed5381fe0c0397ccb62a"}, + {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:3b5d9876c7f3cdbbac1a6ab00e0b84c675a67ae23ccc6fd7d47922660797d1e0"}, + {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_s390x.whl", hash = "sha256:8582bd1bba9b6b16266dd50eea3ecb4ade6fac48f71d91bf11fc86697f80466b"}, + {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:70fb652a80fad42278b6a8d847938785fba773ccbb3ba5be012c66e68b3195af"}, + {file = "poseidon_py-0.1.5-cp39-cp39-win32.whl", hash = "sha256:ed7acb78ef78ab18f0df3bdf5fa75c27dfe8cc66c3892d9a8363bc2ede911f79"}, + {file = "poseidon_py-0.1.5-cp39-cp39-win_amd64.whl", hash = "sha256:c1daece917148c841dcdbade3dea7fdabb6ca45d2453d7f36984bb20e17ed18c"}, + {file = "poseidon_py-0.1.5.tar.gz", hash = "sha256:acfa0f79176505226dc79c27e1a6a55e1184753920463826101a2f1c2dd2fbf6"}, ] [[package]] @@ -4227,30 +4227,35 @@ tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] [[package]] name = "starknet-py" -version = "0.23.0" +version = "0.24.1" description = "A python SDK for Starknet" optional = false -python-versions = "<3.13,>=3.8" -files = [ - {file = "starknet_py-0.23.0.tar.gz", hash = "sha256:3dbab1a608e4bc67e646f3d096bcf346383da26472cfdb60409b5478c3432e39"}, -] +python-versions = ">=3.8, <3.13" +files = [] +develop = false [package.dependencies] -aiohttp = ">=3.8.4,<4.0.0" -asgiref = ">=3.4.1,<4.0.0" +aiohttp = "^3.8.4" +asgiref = "^3.4.1" crypto-cpp-py = "1.4.4" -eth-keyfile = ">=0.8.1,<0.9.0" -lark = ">=1.1.5,<2.0.0" -marshmallow = ">=3.15.0,<4.0.0" +eth-keyfile = "^0.8.1" +lark = "^1.1.5" +marshmallow = "^3.15.0" marshmallow-dataclass = "<8.8.0" marshmallow-oneofschema = "3.1.1" -poseidon-py = "0.1.4" -pycryptodome = ">=3.17,<4.0" -typing-extensions = ">=4.3.0,<5.0.0" +poseidon-py = "0.1.5" +pycryptodome = "^3.17" +typing-extensions = "^4.3.0" [package.extras] docs = ["enum-tools[sphinx] (==0.12.0)", "furo (>=2024.5.6,<2025.0.0)", "sphinx (>=4.3.1,<8.0.0)"] +[package.source] +type = "git" +url = "https://github.com/kkrt-labs/starknet.py" +reference = "cc272228b66c84079b01034f0d205aae8e7bc113" +resolved_reference = "cc272228b66c84079b01034f0d205aae8e7bc113" + [[package]] name = "sympy" version = "1.11.1" diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol new file mode 100644 index 000000000..e17f457a4 --- /dev/null +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: AGPL-3.0-only +pragma solidity >=0.8.0; + +/// @title EVM Precompiles Integration +/// @notice Contract for integration testing of EVM precompiles. +/// @dev Implements functions for ECADD and ECMUL precompiles. +contract EvmPrecompiles { + /// @dev Address of the ECADD precompile + address private constant ECADD_PRECOMPILE = address(0x06); + /// @dev Address of the ECMUL precompile + address private constant ECMUL_PRECOMPILE = address(0x07); + + /// @dev Gas cost for ECADD operation + uint256 private constant ECADD_GAS = 150; + /// @dev Gas cost for ECMUL operation + uint256 private constant ECMUL_GAS = 6000; + + /*////////////////////////////////////////////////////////////// + CONSTRUCTOR + //////////////////////////////////////////////////////////////*/ + constructor() {} + + /*////////////////////////////////////////////////////////////// + FUNCTIONS FOR PRECOMPILES + //////////////////////////////////////////////////////////////*/ + /// @notice Performs elliptic curve addition + /// @param x1 X coordinate of the first point + /// @param y1 Y coordinate of the first point + /// @param x2 X coordinate of the second point + /// @param y2 Y coordinate of the second point + /// @return x X coordinate of the result point + /// @return y Y coordinate of the result point + function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) public view returns (uint256 x, uint256 y) { + bytes memory input = abi.encodePacked(x1, y1, x2, y2); + (bool success, bytes memory result) = ECADD_PRECOMPILE.staticcall{gas: ECADD_GAS}(input); + require(success, "ECADD precompile call failed"); + return abi.decode(result, (uint256, uint256)); + } + + /// @notice Performs elliptic curve scalar multiplication + /// @param x1 X coordinate of the point + /// @param y1 Y coordinate of the point + /// @param s Scalar for multiplication + /// @return x X coordinate of the result point + /// @return y Y coordinate of the result point + function ecMul(uint256 x1, uint256 y1, uint256 s) public view returns (uint256 x, uint256 y) { + bytes memory input = abi.encodePacked(x1, y1, s); + (bool success, bytes memory result) = ECMUL_PRECOMPILE.staticcall{gas: ECMUL_GAS}(input); + require(success, "ECMUL precompile call failed"); + return abi.decode(result, (uint256, uint256)); + } +} diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 7c9652517..f921670b0 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -116,9 +116,9 @@ namespace Precompiles { ret; call not_implemented_precompile; // 0x5 ret; - call not_implemented_precompile; // 0x6 + call external_precompile; // 0x6 ret; - call not_implemented_precompile; // 0x7 + call external_precompile; // 0x7 ret; call not_implemented_precompile; // 0x8 ret; diff --git a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py new file mode 100644 index 000000000..76945dd23 --- /dev/null +++ b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py @@ -0,0 +1,95 @@ +import pytest +import pytest_asyncio +from ethereum.base_types import U256, Uint +from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP +from ethereum.exceptions import OutOfGasError + +from kakarot_scripts.utils.kakarot import deploy + + +@pytest_asyncio.fixture(scope="package") +async def evm_precompiles(owner): + return await deploy( + "EvmPrecompiles", + "EvmPrecompiles", + caller_eoa=owner.starknet_contract, + ) + + +def ref_alt_bn128_add(x0, y0, x1, y1): + x0_value = U256.from_signed(x0) + y0_value = U256.from_signed(y0) + x1_value = U256.from_signed(x1) + y1_value = U256.from_signed(y1) + + for i in (x0_value, y0_value, x1_value, y1_value): + if i >= ALT_BN128_PRIME: + raise OutOfGasError + + try: + p0 = BNP(BNF(x0_value), BNF(y0_value)) + p1 = BNP(BNF(x1_value), BNF(y1_value)) + except ValueError: + raise OutOfGasError from None + + p = p0 + p1 + + x_bytes = p.x.to_be_bytes32() + y_bytes = p.y.to_be_bytes32() + + x = Uint(x_bytes) + y = Uint(y_bytes) + + return x, y + + +def ref_alt_bn128_mul(x0, y0, s): + x0_value = U256.from_signed(x0) + y0_value = U256.from_signed(y0) + U256.from_signed(s) + + for i in (x0_value, y0_value): + if i >= ALT_BN128_PRIME: + raise OutOfGasError + + try: + p0 = BNP(BNF(x0_value), BNF(y0_value)) + except ValueError: + raise OutOfGasError from None + + p = p0.mul_by(s) + + x_bytes = p.x.to_be_bytes32() + y_bytes = p.y.to_be_bytes32() + + x = Uint(x_bytes) + y = Uint(y_bytes) + + return x, y + + +@pytest.mark.asyncio(scope="package") +@pytest.mark.EvmPrecompiles +@pytest.mark.xfail(reason="Katana doesn't support new builtins") +class TestEvmPrecompiles: + class TestEcAdd: + async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): + result = await evm_precompiles.ecAdd(0, 0, 0, 0) + expected = ref_alt_bn128_add(0, 0, 0, 0) + assert result == expected + + async def test_should_return_ec_add_with_coordinates(self, evm_precompiles): + result = await evm_precompiles.ecAdd(1, 2, 1, 2) + expected = ref_alt_bn128_add(1, 2, 1, 2) + assert result == expected + + class TestEcMul: + async def test_should_return_ec_mul_with_coordinates(self, evm_precompiles): + result = await evm_precompiles.ecMul(1, 2, 2) + expected = ref_alt_bn128_mul(1, 2, 2) + assert result == expected + + async def test_should_return_ec_mul_point_at_infinity(self, evm_precompiles): + result = await evm_precompiles.ecMul(0, 0, 0) + expected = ref_alt_bn128_mul(0, 0, 0) + assert result == expected diff --git a/tests/src/kakarot/precompiles/test_ec_add.cairo b/tests/src/kakarot/precompiles/test_ec_add.cairo deleted file mode 100644 index 8d756ec82..000000000 --- a/tests/src/kakarot/precompiles/test_ec_add.cairo +++ /dev/null @@ -1,69 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Starkware dependencies -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.cairo_secp.bigint import BigInt3, bigint_to_uint256, uint256_to_bigint -from starkware.cairo.common.uint256 import Uint256, assert_uint256_eq -from starkware.cairo.common.math import split_felt -from starkware.cairo.common.memcpy import memcpy -from starkware.cairo.common.alloc import alloc - -// Local dependencies -from utils.utils import Helpers -from utils.alt_bn128.alt_bn128_g1 import ALT_BN128, G1Point -from kakarot.precompiles.ecadd import PrecompileEcAdd -from kakarot.model import model -from kakarot.memory import Memory -from kakarot.constants import Constants -from kakarot.stack import Stack -from kakarot.evm import EVM -from kakarot.instructions.memory_operations import MemoryOperations -from kakarot.instructions.system_operations import SystemOperations, CallHelper, CreateHelper -from tests.utils.helpers import TestHelpers - -const G1POINT_BYTES_LEN = 32; - -func test__ecadd_impl{ - syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin* -}() { - // Given - alloc_locals; - local calldata_len: felt; - let (calldata: felt*) = alloc(); - %{ - ids.calldata_len = len(program_input["calldata"]); - segments.write_arg(ids.calldata, program_input["calldata"]); - %} - - let x0: BigInt3 = Helpers.bytes32_to_bigint(calldata); - let y0: BigInt3 = Helpers.bytes32_to_bigint(calldata + G1POINT_BYTES_LEN); - let x1: BigInt3 = Helpers.bytes32_to_bigint(calldata + G1POINT_BYTES_LEN * 2); - let y1: BigInt3 = Helpers.bytes32_to_bigint(calldata + G1POINT_BYTES_LEN * 3); - - // When - let point0: G1Point = G1Point(x0, y0); - let point1: G1Point = G1Point(x1, y1); - let (expected_point: G1Point) = ALT_BN128.ec_add(point0, point1); - let (bytes_expected_x_len, bytes_expected_result: felt*) = Helpers.bigint_to_bytes_array( - expected_point.x - ); - let (bytes_expected_y_len, bytes_expected_y: felt*) = Helpers.bigint_to_bytes_array( - expected_point.y - ); - memcpy(bytes_expected_result + bytes_expected_x_len, bytes_expected_y, bytes_expected_y_len); - let (output_len, output: felt*, gas_used, reverted) = PrecompileEcAdd.run( - PrecompileEcAdd.PRECOMPILE_ADDRESS, calldata_len, calldata - ); - - // Then - TestHelpers.assert_array_equal( - array_0_len=bytes_expected_x_len + bytes_expected_y_len, - array_0=bytes_expected_result, - array_1_len=output_len, - array_1=output, - ); - - return (); -} diff --git a/tests/src/kakarot/precompiles/test_ec_add.py b/tests/src/kakarot/precompiles/test_ec_add.py deleted file mode 100644 index 9c1437925..000000000 --- a/tests/src/kakarot/precompiles/test_ec_add.py +++ /dev/null @@ -1,11 +0,0 @@ -import hypothesis.strategies as st -import pytest -from hypothesis import given, settings - - -@pytest.mark.EC_ADD -class TestEcAdd: - @given(calldata=st.binary(min_size=128, max_size=128)) - @settings(max_examples=5) - def test_ecadd(self, cairo_run, calldata): - cairo_run("test__ecadd_impl", calldata=list(calldata)) diff --git a/tests/src/kakarot/precompiles/test_ec_mul.py b/tests/src/kakarot/precompiles/test_ec_mul.py deleted file mode 100644 index b9db12788..000000000 --- a/tests/src/kakarot/precompiles/test_ec_mul.py +++ /dev/null @@ -1,8 +0,0 @@ -import pytest - - -@pytest.mark.EC_MUL -class TestEcMul: - @pytest.mark.slow - def test_ec_mul(self, cairo_run): - cairo_run("test__ecmul_impl") diff --git a/tests/src/kakarot/precompiles/test_ec_recover.cairo b/tests/src/kakarot/precompiles/test_ec_recover.cairo deleted file mode 100644 index 8790ddb01..000000000 --- a/tests/src/kakarot/precompiles/test_ec_recover.cairo +++ /dev/null @@ -1,26 +0,0 @@ -%lang starknet - -from starkware.cairo.common.alloc import alloc -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.math import unsigned_div_rem, assert_not_zero -from starkware.cairo.common.memset import memset -from starkware.cairo.common.memcpy import memcpy - -from kakarot.precompiles.ec_recover import PrecompileEcRecover -from utils.utils import Helpers - -func test__ec_recover{ - syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin* -}() -> (output: felt*) { - alloc_locals; - let (local input) = alloc(); - tempvar input_len: felt; - %{ - ids.input_len = len(program_input["input"]); - segments.write_arg(ids.input, program_input["input"]) - %} - let (output_len: felt, output: felt*, gas_used: felt, reverted: felt) = PrecompileEcRecover.run( - PrecompileEcRecover.PRECOMPILE_ADDRESS, input_len, input - ); - return (output=output); -} From c9970b3d2530a754a1cbe091992a863b7edcf9ca Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 17 Sep 2024 13:44:34 +0200 Subject: [PATCH 02/15] remove fetch-ssj-artifacts from setup --- Makefile | 2 +- poetry.lock | 167 ++++++++++++++++++++++++------------------------- pyproject.toml | 2 +- 3 files changed, 83 insertions(+), 88 deletions(-) diff --git a/Makefile b/Makefile index 717118495..7c4acfb3f 100644 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ $(SSJ_ZIP): fetch-ef-tests: poetry run python ./kakarot_scripts/ef_tests/fetch.py -setup: fetch-ssj-artifacts +setup: poetry install test: deploy diff --git a/poetry.lock b/poetry.lock index f686b83dd..88ea80843 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2847,77 +2847,77 @@ testing = ["pytest", "pytest-benchmark"] [[package]] name = "poseidon-py" -version = "0.1.5" +version = "0.1.4" description = "Python implementation of Poseidon hash" optional = false python-versions = ">=3.8" files = [ - {file = "poseidon_py-0.1.5-cp310-cp310-macosx_12_0_universal2.whl", hash = "sha256:3a8b4cc09d28439764956c47a00e49aa36c23421f724b0ffbd2c62550325240e"}, - {file = "poseidon_py-0.1.5-cp310-cp310-macosx_13_0_universal2.whl", hash = "sha256:a7e611fe04d200eb782a0e7b6f8ee8e34da531a527f96685ff5f12117014fca8"}, - {file = "poseidon_py-0.1.5-cp310-cp310-macosx_14_0_universal2.whl", hash = "sha256:5070cb74a20c433c2a20e91e14f4a92a2f66b8001adaf15ed5febc941d5ea00f"}, - {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:15a6c334161ee8444fb908c0c2f9f2a4d14c6a6ace3397bd3b1ed7673244870b"}, - {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9a8899e5ea0ba5d1bd41cc54b337780030d9f68859e94bb124d76a751b124a63"}, - {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:02c1206449d327ffd8eadca80d7c122d8adfaa529639cf5e5a186fa81cd19977"}, - {file = "poseidon_py-0.1.5-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:755ea18163c156702160c5ede046903c7c2350de806ed92da80e7bae16f61e23"}, - {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:ec16f0c8f74d9de6b2083006e4f408ea77c1b68375a2670a43c04754fff92f05"}, - {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:67d31aebd0e00b1f27e27642e086b04951fa2696417a2ef8b5e11f1d86d097f2"}, - {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_s390x.whl", hash = "sha256:c47f68fbe13b9e68a0f0af005b329639f75edcb7c44def9030c0fdf37602ca3e"}, - {file = "poseidon_py-0.1.5-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:14ea566f3e79e59c5d30dfca1f8130953f95cc65aa259424a9c2d37906c63fa5"}, - {file = "poseidon_py-0.1.5-cp310-cp310-win32.whl", hash = "sha256:618c6af537bf0a2efdaacd5a9d511615d50ee21c31f098b1247f24a5b3542893"}, - {file = "poseidon_py-0.1.5-cp310-cp310-win_amd64.whl", hash = "sha256:c6778847c2f6a6a22c8898425adc960ef284d18925780ccd56f53821a66acf4b"}, - {file = "poseidon_py-0.1.5-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:af55e19a30e4c75a63c353e1f616d04e188bf901ddce1df3e8bc4aadaf6769e8"}, - {file = "poseidon_py-0.1.5-cp311-cp311-macosx_13_0_universal2.whl", hash = "sha256:c1c68d5db997356941433f95808885395f6c2f4132c8a80d6e189881a553d7df"}, - {file = "poseidon_py-0.1.5-cp311-cp311-macosx_14_0_universal2.whl", hash = "sha256:eead4b7b489e66747e6ff8bb73ef0ace3ae8a15e0622ff0b9415e0cf9d2e6c1c"}, - {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2efb15008ed0dad41777ffe427432107140313c77c764965612d4e4a6a426925"}, - {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:b218cd61003175791cd1d168559b4aa12cbe9fba2b630e34431d653227c6da05"}, - {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:57e67d2dfabab2085e8f30353c4c19eb53b02cd858ca138cb3aa32a03c34e673"}, - {file = "poseidon_py-0.1.5-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:fcde98980879d4d145f25c51c06284564a255cbbd6726a1874712474a8b04137"}, - {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:aaa220a9a28246f3b76353697322b6588052d38e7ecaf18337eecaa6cfa7e6d2"}, - {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:b1750a301b426e3f6ca805672752860e81cf4f6cc2b3e6cd6816d4da78f2dd46"}, - {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_s390x.whl", hash = "sha256:1ffe57ffacab103c37606b70eda7dd97c857b3d462a7926038123b05be5ba457"}, - {file = "poseidon_py-0.1.5-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:4faeb29252770e10a08c4d6aa2a095c171668640f21d5721441f5015ab1dbc4c"}, - {file = "poseidon_py-0.1.5-cp311-cp311-win32.whl", hash = "sha256:df84226f66078d1aca460db3590b65400fcc1aa593c205baba96c0d25b80b39d"}, - {file = "poseidon_py-0.1.5-cp311-cp311-win_amd64.whl", hash = "sha256:b520591c30af7f0be29ea88404b931eecee8babe0d734e7446aed3e164f8ef83"}, - {file = "poseidon_py-0.1.5-cp312-cp312-macosx_12_0_universal2.whl", hash = "sha256:4f2d28448e5b0bea2622ca23667de8a27b1a6056068181c5a41e78be65f0d980"}, - {file = "poseidon_py-0.1.5-cp312-cp312-macosx_13_0_universal2.whl", hash = "sha256:311291b8caa39f7ebeb3c837f239a46e8214d30722f9e01ded62d79c0b061b8a"}, - {file = "poseidon_py-0.1.5-cp312-cp312-macosx_14_0_universal2.whl", hash = "sha256:06c07b1e2ad273d50b243121b1e94b28bf893bd4942fe1808f98d33cd2de2790"}, - {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:55501c7c881eaf406568db70a16c56c5f92c325eda65f9f741e75ec5cced7ad6"}, - {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:cbd1ed7d8567e057cc181542aff18dc0b11cf2c67593243830cc2fedb73b112f"}, - {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:19ae4ddd2b1d05d1ed2e3b38e58c3ac5940a46001b080d5112ce346be4d09681"}, - {file = "poseidon_py-0.1.5-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a1ba4956c6b886457d43e43a4465aed18f9bd1f23e7bd2c5388c9a74ae8f0842"}, - {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:3e4e76745181fb50c77813b1c7c845f92f2d3b4620bf784f43e01be5713e1c14"}, - {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:28c0f134533b827eb0bc883347ec38dca2a5559fb277aeeb4ac9cce7f6d30a16"}, - {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_s390x.whl", hash = "sha256:7bc3995f771b7d98f05b200c2c19c1ae6238bb9f625733042a6df9633219e762"}, - {file = "poseidon_py-0.1.5-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:f09faa440ff5f10099a3e667bb5f8781d27a7bc719f45119d08d12574a4d9281"}, - {file = "poseidon_py-0.1.5-cp312-cp312-win32.whl", hash = "sha256:4a7fad5110fc64125ef8fbbdc1d49798159b568e239dbef18ada8fd87eed1fc9"}, - {file = "poseidon_py-0.1.5-cp312-cp312-win_amd64.whl", hash = "sha256:0261221c68c36fd11cfcb91e5074953bfbe7a33031d806d3ab2dc5c7c1e70a2b"}, - {file = "poseidon_py-0.1.5-cp38-cp38-macosx_12_0_universal2.whl", hash = "sha256:ed73929274da455b214bc32e548888094ab42a02af683ccdad006a204299cd06"}, - {file = "poseidon_py-0.1.5-cp38-cp38-macosx_13_0_universal2.whl", hash = "sha256:5037c878654a5546e154f28a3f7a85fbfae3e6aaac6d02552fa233e382aa0b73"}, - {file = "poseidon_py-0.1.5-cp38-cp38-macosx_14_0_universal2.whl", hash = "sha256:38ede222b125a2d09e8f76c59289b1cd0d06c135428ca81f567fc5ae17216e03"}, - {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:68ea474600e445946f96bebadab480c12b11828d9942dbcc534707e0fceb0e6b"}, - {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e7c8d4a37474d2481c6e5f7016102cd7a3dc85f6c3a7b687ad1982e307a1f812"}, - {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:76d2a99a976126dc5632905a4e971b1f4477366f88757228b04de10b624b307e"}, - {file = "poseidon_py-0.1.5-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b504ee79a14e7456e5a7462792afe32d696fdb33819cdf447f9d2d3faa410690"}, - {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:13529af9d43bb3a200819b053a4f8b283b60b9f60dbdcc15343d3a41e0db4256"}, - {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:29ed2a89d0f5417d27130503f6c36e4df4649fdc947ff0b813368e914d562f83"}, - {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_s390x.whl", hash = "sha256:6cad01961590f198939e46554e50c506f908f7fd680407792501cc337a33cf05"}, - {file = "poseidon_py-0.1.5-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:c7d3ae493cb4b325dd4d394159f7b76a316f5da74682772060498985d2f838ad"}, - {file = "poseidon_py-0.1.5-cp38-cp38-win32.whl", hash = "sha256:95cbb69541bc1fba2497cb8709f4ab96fec847b2c261129bd9b3bec1ce832bf2"}, - {file = "poseidon_py-0.1.5-cp38-cp38-win_amd64.whl", hash = "sha256:4727b0d32fc80db43a2b6cc3a14a2250ae8c6cb8e30d342f84e68c72ae053032"}, - {file = "poseidon_py-0.1.5-cp39-cp39-macosx_12_0_universal2.whl", hash = "sha256:41bb6269dcb1ac1b57bbaf5d90167ef745541ce4199b0b94ee5a05c0f5c8b189"}, - {file = "poseidon_py-0.1.5-cp39-cp39-macosx_13_0_universal2.whl", hash = "sha256:8c98c7a810adc88695e83dbab94075deec8f2503fe29fc838f680b9eab57e837"}, - {file = "poseidon_py-0.1.5-cp39-cp39-macosx_14_0_universal2.whl", hash = "sha256:b296fc5f2e9fedf03ed8d112b5bc517654ed4d265e60e9029607b6221233d466"}, - {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:cfc14ee08cd547e8d118a9d8e23455ae114df329a6f7822b2d32e2c8238a04a9"}, - {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:6cc58d3a0f361739259f2140bbe2f85b6fcb9373664d5b3a504af79a687d9d45"}, - {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:b878efb1e76c0287f0ca8f583dca74b8c3cc124dab972832af4bef6e017740a2"}, - {file = "poseidon_py-0.1.5-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d28bb34be71a105c89d7e4c2071afc568ed5851390fd70b009b50fe5e466ddd1"}, - {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:f7342a365ab53ad9524b2e1c6192dfbee6cd10505af4ed5381fe0c0397ccb62a"}, - {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:3b5d9876c7f3cdbbac1a6ab00e0b84c675a67ae23ccc6fd7d47922660797d1e0"}, - {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_s390x.whl", hash = "sha256:8582bd1bba9b6b16266dd50eea3ecb4ade6fac48f71d91bf11fc86697f80466b"}, - {file = "poseidon_py-0.1.5-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:70fb652a80fad42278b6a8d847938785fba773ccbb3ba5be012c66e68b3195af"}, - {file = "poseidon_py-0.1.5-cp39-cp39-win32.whl", hash = "sha256:ed7acb78ef78ab18f0df3bdf5fa75c27dfe8cc66c3892d9a8363bc2ede911f79"}, - {file = "poseidon_py-0.1.5-cp39-cp39-win_amd64.whl", hash = "sha256:c1daece917148c841dcdbade3dea7fdabb6ca45d2453d7f36984bb20e17ed18c"}, - {file = "poseidon_py-0.1.5.tar.gz", hash = "sha256:acfa0f79176505226dc79c27e1a6a55e1184753920463826101a2f1c2dd2fbf6"}, + {file = "poseidon_py-0.1.4-cp310-cp310-macosx_12_0_universal2.whl", hash = "sha256:8c2c81636b8389168181425f214da0e75f0b58df89241719373fcbba45c7b061"}, + {file = "poseidon_py-0.1.4-cp310-cp310-macosx_13_0_universal2.whl", hash = "sha256:6103971eb4c4e6f6a8b8d6d83a1e722771dbc04b29e94da7c202bc904efd1aed"}, + {file = "poseidon_py-0.1.4-cp310-cp310-macosx_14_0_universal2.whl", hash = "sha256:f1d269ef1dfaec4fe41226158cd914e8f74b75e937428d2c3de6c7c0cad8ab74"}, + {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:195a3b8acb9a3cb27c8a822619663f25f075057471f392ea18098ed83e920e10"}, + {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:ee3f2995b0023dec31dee06cc5d9c12f5aa1d0e8d7cca7fba7b005145c04afcc"}, + {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:a08ce457a4a01a3f0c83a5556c21e2e95f86fd7ec594f73f72704c239a489b23"}, + {file = "poseidon_py-0.1.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:71423bc1a8e422a1c82a5a786a0c01b8a024a00fc7cba883e01e85410d5bf275"}, + {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:3c8c4e54a616451c3420cdce1a8e52b5ffcdc604f431a9c6a0f3cd50ef2e1292"}, + {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_ppc64le.whl", hash = "sha256:1ab0d2d10cdb23be125a918e553bcc7cdd27c48c30339d8d8822757e918706b5"}, + {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_s390x.whl", hash = "sha256:e57cbdf75398a4faf7b71b2c2b7462975e81047c3a592a2654e0d020aab184e4"}, + {file = "poseidon_py-0.1.4-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:64bff7984b02a79d4f6864a2d9276ad52d96a933483293bdf5eb2e1558e0994d"}, + {file = "poseidon_py-0.1.4-cp310-cp310-win32.whl", hash = "sha256:0b5fe9eb6e21a8478ea739d3316b28bf2e8de49dc2d2305768b2f14be04bc143"}, + {file = "poseidon_py-0.1.4-cp310-cp310-win_amd64.whl", hash = "sha256:fdabee5ffceb74836979a4497402dd0dd351ec387d3ecad5c627df8da8cf0f04"}, + {file = "poseidon_py-0.1.4-cp311-cp311-macosx_12_0_universal2.whl", hash = "sha256:c1b8778a8b51d980b6780daf3fb11357942d941465f55ebf9eb0702638396b61"}, + {file = "poseidon_py-0.1.4-cp311-cp311-macosx_13_0_universal2.whl", hash = "sha256:89694d6ebaee6086f1d3e6223707acaeb4e9b32c61faf20c6c2685baaeba6326"}, + {file = "poseidon_py-0.1.4-cp311-cp311-macosx_14_0_universal2.whl", hash = "sha256:7cc93c044a03d7067d37c79a697a81562ae5899bd0150bf496520d418a4a774e"}, + {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f36dc34704540f2a2bbe18e2f195bcd0fa504b93962bd72f535e37578f264385"}, + {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:24a3b643f7e071883686bcb8422558fc1cf485b61a26a028554d60713cdd9aea"}, + {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:12237601c95127c0cb8875e81c04f26720069aed544dfff499a473e86a63f14f"}, + {file = "poseidon_py-0.1.4-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b421fa3e01e0a072622dcc770635dd1aeb2d8a77d334d5af40ae54033587a1df"}, + {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:947164c40ef49b47e366b3b838edab9efb42e598a7f95483880e1b3b84536131"}, + {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_ppc64le.whl", hash = "sha256:95724cc0f6708a097521f7193af20e62ee63b2927b9775bcbf42ed1ada0653ae"}, + {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_s390x.whl", hash = "sha256:790a44a2858dc8bab9d80d9527a67232823c3fda2216cc4fb8e37775904b103b"}, + {file = "poseidon_py-0.1.4-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:f658750b779ebd09b30c047d671e4e18e90cbc4ab7bb006dfb5c4468fd5b9f61"}, + {file = "poseidon_py-0.1.4-cp311-cp311-win32.whl", hash = "sha256:48e689782cefd5237fac1001df34c2750e707721d309d9811e0095d3139a47d2"}, + {file = "poseidon_py-0.1.4-cp311-cp311-win_amd64.whl", hash = "sha256:5ecc75a778740fff3d4a3bc374fa9fe6868c08091ac199c43ef7f1d9e40c737c"}, + {file = "poseidon_py-0.1.4-cp312-cp312-macosx_12_0_universal2.whl", hash = "sha256:0eef537ac31cbf195a170e696c9f5b6d2d13ce3a0da98f37cf03f0d855ae2be1"}, + {file = "poseidon_py-0.1.4-cp312-cp312-macosx_13_0_universal2.whl", hash = "sha256:446048c88e8ee97c569032171c7cb057b9c739d2985109c55eb234ce66cd2d16"}, + {file = "poseidon_py-0.1.4-cp312-cp312-macosx_14_0_universal2.whl", hash = "sha256:91c259f85cd1b3a5f2d450a481d90bdd4b911d05bd6bcc51fd290d2fd7d66613"}, + {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b53d6eaecac2c7a37a66f7a55a451fe5abed0e317ebb26022e9937b43a54f8c9"}, + {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:8ac18854c6305d80f0ccb270b261eb278bbc21f055b43737a8b55726d4caee3a"}, + {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:53edc8142cca4fe56eb2e2451eb77165925e2d7df25186fd5ab5b7d813555381"}, + {file = "poseidon_py-0.1.4-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6880dc2461a2c7336abb7f4774971a053bc04169dbfce5e08feecefad4fd49c2"}, + {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:514bc3df634a4ca5621bc0ea60e253257f8cc7773e1d3701e5e8f36a35f4d0f3"}, + {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_ppc64le.whl", hash = "sha256:76d7cdb3c4a744573627f8170f659236737218245d04a7ead00e74bfb48ab28a"}, + {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_s390x.whl", hash = "sha256:8fa27f2797abdec641a26abb05fee82194ebb40fd4ded3fa385b8b143bef4d7f"}, + {file = "poseidon_py-0.1.4-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:53a78d0fcd1a7742f1d72ce9a2871790c0ae862dec62b18f58982c0ca07a3e79"}, + {file = "poseidon_py-0.1.4-cp312-cp312-win32.whl", hash = "sha256:1176e308c18ff878cc8763cb0dc03541d56775a75b8128f5da6d55ad041a6bd8"}, + {file = "poseidon_py-0.1.4-cp312-cp312-win_amd64.whl", hash = "sha256:9efac87f378ceb64c2b2bad5a2bf8678459ce7ae7947c12326effc4a700fe9e5"}, + {file = "poseidon_py-0.1.4-cp38-cp38-macosx_12_0_universal2.whl", hash = "sha256:d38726b1e330000a91fd01cce53c5ed6aaec030fa91f8c23d8a598eb630f6227"}, + {file = "poseidon_py-0.1.4-cp38-cp38-macosx_13_0_universal2.whl", hash = "sha256:026fd9b53c21e1ee9ca15c956e3650e15842f4e0f517580f7a645ac3c71b4b46"}, + {file = "poseidon_py-0.1.4-cp38-cp38-macosx_14_0_universal2.whl", hash = "sha256:f1940fae0aeb2aaa04abb71924c09b54cad845d273ec5a306c1ee27563efd3d3"}, + {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3021938f639c55d45584f8cb66d1d5a2bf50d98f2f536fa8ec9da63ff840631a"}, + {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:fcb8f9060de484c44c015c667527183cdf96f1f8e70df3e0c3e10a60900f81a0"}, + {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:06fc95034cd5a2843a06f0bcc9772ed8785c0092fccdc0516fb8c260bb77bf49"}, + {file = "poseidon_py-0.1.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d7064ea9b2e4f15761ffdd477cc034cef715c4e0c69724f448d525bd5fceefdc"}, + {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:ec15d4fdbdefc976340a18d3fe7525f8957ecd7552fd011dbda890b037aef28d"}, + {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_ppc64le.whl", hash = "sha256:be89d40c36d815e7668321e2faea01ec2db434ab266685d67e8872c781ad6013"}, + {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_s390x.whl", hash = "sha256:cfcca7b463d247f423f420dff6b151cc6bf23771d86b637c5da3b14f08b5268b"}, + {file = "poseidon_py-0.1.4-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:e7eae5e8036bbc4ea01a056aa2eb687fb5ca7d91adfc242fdc672391886063a9"}, + {file = "poseidon_py-0.1.4-cp38-cp38-win32.whl", hash = "sha256:96dbe49c2af157982f090dde7c66bc4490bfd16ec3d0ad709add40ba23c3dd71"}, + {file = "poseidon_py-0.1.4-cp38-cp38-win_amd64.whl", hash = "sha256:baa43eed066e6985da5045dc1f0a432f60838582bfbfefe630ce2e506ac18acc"}, + {file = "poseidon_py-0.1.4-cp39-cp39-macosx_12_0_universal2.whl", hash = "sha256:a3aaadec7f16739f7ec9084c19b344bc4d937689c00a22e371e7f02993ac9199"}, + {file = "poseidon_py-0.1.4-cp39-cp39-macosx_13_0_universal2.whl", hash = "sha256:dc812bd192be78b28a06124e847122969ecd7fe217c957a2f177f11225cb09e3"}, + {file = "poseidon_py-0.1.4-cp39-cp39-macosx_14_0_universal2.whl", hash = "sha256:3753cd12acde11192eb9e61e7308cc254e6323d24d6d9e55177d5b295cc3d2e6"}, + {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:08deb5310f5f7d1aeff3b1ac3ed01cfae95be64adf9b4e6e7ef990d50f7348f6"}, + {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2cf8582f721410764b2da734b79be93123a32fc6b2710e78b36685514a8c6713"}, + {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:abdc9f2580144887e3d10a63a00dcde7c9180ac7683fddf314581a32ecc70cba"}, + {file = "poseidon_py-0.1.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8b39d3faae6164157a1b2ef0c6e5ef5658a7f48df18c6c090998c566b8447603"}, + {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:841ddcc7d7be6fde7b8927bb6cd47fedd8e8f83b761c95ee7a686f1d794df6b8"}, + {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_ppc64le.whl", hash = "sha256:f06638fe92366bc99a2badf0ec052b02e3b89d79cc2ef400864707701c3919a6"}, + {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_s390x.whl", hash = "sha256:9c586a42871ddc1c9af22e15825f250152b961cf3a7f1144faed10589d1aa505"}, + {file = "poseidon_py-0.1.4-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:4a0caf2fa878cc2b7f4781e7f2574c65231b422257addca64d146b34bc15a7ad"}, + {file = "poseidon_py-0.1.4-cp39-cp39-win32.whl", hash = "sha256:d14aa6c9d76279e46eb6fde66e3e2629e3e461069dd76644b65c5ebed69114c8"}, + {file = "poseidon_py-0.1.4-cp39-cp39-win_amd64.whl", hash = "sha256:e246d46d81bcd465b399c5d7663ef59b31fe0acd3966253cbb53e12508b41d80"}, + {file = "poseidon_py-0.1.4.tar.gz", hash = "sha256:416139d80931c85fd117cbc5b47d2eb1daf25e52acd77b6dd889c4820592a479"}, ] [[package]] @@ -4227,35 +4227,30 @@ tests = ["cython", "littleutils", "pygments", "pytest", "typeguard"] [[package]] name = "starknet-py" -version = "0.24.1" +version = "0.23.0" description = "A python SDK for Starknet" optional = false -python-versions = ">=3.8, <3.13" -files = [] -develop = false +python-versions = "<3.13,>=3.8" +files = [ + {file = "starknet_py-0.23.0.tar.gz", hash = "sha256:3dbab1a608e4bc67e646f3d096bcf346383da26472cfdb60409b5478c3432e39"}, +] [package.dependencies] -aiohttp = "^3.8.4" -asgiref = "^3.4.1" +aiohttp = ">=3.8.4,<4.0.0" +asgiref = ">=3.4.1,<4.0.0" crypto-cpp-py = "1.4.4" -eth-keyfile = "^0.8.1" -lark = "^1.1.5" -marshmallow = "^3.15.0" +eth-keyfile = ">=0.8.1,<0.9.0" +lark = ">=1.1.5,<2.0.0" +marshmallow = ">=3.15.0,<4.0.0" marshmallow-dataclass = "<8.8.0" marshmallow-oneofschema = "3.1.1" -poseidon-py = "0.1.5" -pycryptodome = "^3.17" -typing-extensions = "^4.3.0" +poseidon-py = "0.1.4" +pycryptodome = ">=3.17,<4.0" +typing-extensions = ">=4.3.0,<5.0.0" [package.extras] docs = ["enum-tools[sphinx] (==0.12.0)", "furo (>=2024.5.6,<2025.0.0)", "sphinx (>=4.3.1,<8.0.0)"] -[package.source] -type = "git" -url = "https://github.com/kkrt-labs/starknet.py" -reference = "cc272228b66c84079b01034f0d205aae8e7bc113" -resolved_reference = "cc272228b66c84079b01034f0d205aae8e7bc113" - [[package]] name = "sympy" version = "1.11.1" @@ -4692,4 +4687,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.10,<3.11" -content-hash = "9683cf7dacce11dd7a98325daaa9f1b56f136db87b14157f53cfafd3214ddcb6" +content-hash = "271fd12f0f0bc0cc2ce2ce46de531c36d22e5a0304c4569dc4015ce78e35e738" diff --git a/pyproject.toml b/pyproject.toml index c83b8e134..c3f4c768f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,6 @@ packages = [{ include = "kakarot_scripts" }, { include = "tests" }] [tool.poetry.dependencies] python = ">=3.10,<3.11" -starknet-py = "^0.23.0" cairo-lang = "0.13.1" python-dotenv = "^0.21.0" async-lru = "^2.0.4" @@ -16,6 +15,7 @@ toml = "^0.10.2" scikit-learn = "^1.5.1" seaborn = "^0.13.2" boto3 = "^1.35.12" +starknet-py = "0.23.0" [tool.poetry.group.dev.dependencies] pytest = "^8.1.1" From 639c6c65ff9aea98e2d894877465662b1bdfd254 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 17 Sep 2024 22:58:30 +0200 Subject: [PATCH 03/15] wip --- .../EvmPrecompiles/test_evm_precompiles.py | 4 +-- .../kakarot/precompiles/test_ec_recover.cairo | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/src/kakarot/precompiles/test_ec_recover.cairo diff --git a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py index 76945dd23..f86426481 100644 --- a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py +++ b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py @@ -2,7 +2,7 @@ import pytest_asyncio from ethereum.base_types import U256, Uint from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP -from ethereum.exceptions import OutOfGasError +from ethereum.cancun.vm.exceptions import OutOfGasError from kakarot_scripts.utils.kakarot import deploy @@ -70,7 +70,7 @@ def ref_alt_bn128_mul(x0, y0, s): @pytest.mark.asyncio(scope="package") @pytest.mark.EvmPrecompiles -@pytest.mark.xfail(reason="Katana doesn't support new builtins") +# @pytest.mark.xfail(reason="Katana doesn't support new builtins") class TestEvmPrecompiles: class TestEcAdd: async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): diff --git a/tests/src/kakarot/precompiles/test_ec_recover.cairo b/tests/src/kakarot/precompiles/test_ec_recover.cairo new file mode 100644 index 000000000..8790ddb01 --- /dev/null +++ b/tests/src/kakarot/precompiles/test_ec_recover.cairo @@ -0,0 +1,26 @@ +%lang starknet + +from starkware.cairo.common.alloc import alloc +from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin +from starkware.cairo.common.math import unsigned_div_rem, assert_not_zero +from starkware.cairo.common.memset import memset +from starkware.cairo.common.memcpy import memcpy + +from kakarot.precompiles.ec_recover import PrecompileEcRecover +from utils.utils import Helpers + +func test__ec_recover{ + syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin* +}() -> (output: felt*) { + alloc_locals; + let (local input) = alloc(); + tempvar input_len: felt; + %{ + ids.input_len = len(program_input["input"]); + segments.write_arg(ids.input, program_input["input"]) + %} + let (output_len: felt, output: felt*, gas_used: felt, reverted: felt) = PrecompileEcRecover.run( + PrecompileEcRecover.PRECOMPILE_ADDRESS, input_len, input + ); + return (output=output); +} From 1a4601e2da8e338d78af68713e997b3b6ee045c4 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 14:40:51 +0200 Subject: [PATCH 04/15] use hypothesis to generate test values --- Makefile | 4 +- .../src/EvmPrecompiles/EvmPrecompiles.sol | 32 ++++--- .../EvmPrecompiles/test_evm_precompiles.py | 83 ++++++++++++++----- 3 files changed, 85 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 7c4acfb3f..a9edfa766 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ endif .PHONY: build test coverage clean -# 173874367 corresponds to release v0.1.12 of Kakarot SSJ. -KKRT_SSJ_RELEASE_ID = 173874367 +# 173874367 corresponds to release v0.1.13 of Kakarot SSJ. +KKRT_SSJ_RELEASE_ID = 176384150 # Kakarot SSJ artifacts for precompiles. KKRT_SSJ_BUILD_ARTIFACT_URL = $(shell curl -L https://api.github.com/repos/kkrt-labs/kakarot-ssj/releases/${KKRT_SSJ_RELEASE_ID} | jq -r '.assets[0].browser_download_url') KATANA_VERSION = v1.0.0-alpha.12 diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol index e17f457a4..ea846d045 100644 --- a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -10,10 +10,10 @@ contract EvmPrecompiles { /// @dev Address of the ECMUL precompile address private constant ECMUL_PRECOMPILE = address(0x07); - /// @dev Gas cost for ECADD operation - uint256 private constant ECADD_GAS = 150; - /// @dev Gas cost for ECMUL operation - uint256 private constant ECMUL_GAS = 6000; + /// @dev Gas cost for ECADD call is 150 + uint256 private constant ECADD_GAS = 1000000; + /// @dev Gas cost for ECMUL call is 6000 + uint256 private constant ECMUL_GAS = 1000000; /*////////////////////////////////////////////////////////////// CONSTRUCTOR @@ -28,25 +28,37 @@ contract EvmPrecompiles { /// @param y1 Y coordinate of the first point /// @param x2 X coordinate of the second point /// @param y2 Y coordinate of the second point + /// @return success True if the operation was successful, false otherwise /// @return x X coordinate of the result point /// @return y Y coordinate of the result point - function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) public view returns (uint256 x, uint256 y) { + function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) + public + view + returns (bool success, uint256 x, uint256 y) + { bytes memory input = abi.encodePacked(x1, y1, x2, y2); (bool success, bytes memory result) = ECADD_PRECOMPILE.staticcall{gas: ECADD_GAS}(input); - require(success, "ECADD precompile call failed"); - return abi.decode(result, (uint256, uint256)); + if (!success) { + return (false, 0, 0); + } + (x, y) = abi.decode(result, (uint256, uint256)); + return (true, x, y); } /// @notice Performs elliptic curve scalar multiplication /// @param x1 X coordinate of the point /// @param y1 Y coordinate of the point /// @param s Scalar for multiplication + /// @return success True if the operation was successful, false otherwise /// @return x X coordinate of the result point /// @return y Y coordinate of the result point - function ecMul(uint256 x1, uint256 y1, uint256 s) public view returns (uint256 x, uint256 y) { + function ecMul(uint256 x1, uint256 y1, uint256 s) public view returns (bool success, uint256 x, uint256 y) { bytes memory input = abi.encodePacked(x1, y1, s); (bool success, bytes memory result) = ECMUL_PRECOMPILE.staticcall{gas: ECMUL_GAS}(input); - require(success, "ECMUL precompile call failed"); - return abi.decode(result, (uint256, uint256)); + if (!success) { + return (false, 0, 0); + } + (x, y) = abi.decode(result, (uint256, uint256)); + return (true, x, y); } } diff --git a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py index f86426481..1619c65db 100644 --- a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py +++ b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py @@ -1,8 +1,10 @@ import pytest import pytest_asyncio from ethereum.base_types import U256, Uint -from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP from ethereum.cancun.vm.exceptions import OutOfGasError +from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP +from hypothesis import given, settings +from hypothesis.strategies import integers from kakarot_scripts.utils.kakarot import deploy @@ -37,10 +39,10 @@ def ref_alt_bn128_add(x0, y0, x1, y1): x_bytes = p.x.to_be_bytes32() y_bytes = p.y.to_be_bytes32() - x = Uint(x_bytes) - y = Uint(y_bytes) + x = Uint(int.from_bytes(x_bytes, "big")) + y = Uint(int.from_bytes(y_bytes, "big")) - return x, y + return [x, y] def ref_alt_bn128_mul(x0, y0, s): @@ -62,10 +64,10 @@ def ref_alt_bn128_mul(x0, y0, s): x_bytes = p.x.to_be_bytes32() y_bytes = p.y.to_be_bytes32() - x = Uint(x_bytes) - y = Uint(y_bytes) + x = Uint(int.from_bytes(x_bytes, "big")) + y = Uint(int.from_bytes(y_bytes, "big")) - return x, y + return [x, y] @pytest.mark.asyncio(scope="package") @@ -73,23 +75,60 @@ def ref_alt_bn128_mul(x0, y0, s): # @pytest.mark.xfail(reason="Katana doesn't support new builtins") class TestEvmPrecompiles: class TestEcAdd: - async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): - result = await evm_precompiles.ecAdd(0, 0, 0, 0) - expected = ref_alt_bn128_add(0, 0, 0, 0) - assert result == expected + @given( + x0=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + y0=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + x1=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + y1=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + ) + @settings(max_examples=10) + async def test_should_return_ec_add_with_coordinates( + self, evm_precompiles, x0, y0, x1, y1 + ): + try: + expected = ref_alt_bn128_add(x0, y0, x1, y1) + result = await evm_precompiles.ecAdd(x0, y0, x1, y1) + assert result[0] is True + assert result[1:] == expected + except OutOfGasError: + result = await evm_precompiles.ecAdd(x0, y0, x1, y1) + assert result[0] is False - async def test_should_return_ec_add_with_coordinates(self, evm_precompiles): - result = await evm_precompiles.ecAdd(1, 2, 1, 2) - expected = ref_alt_bn128_add(1, 2, 1, 2) - assert result == expected + async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): + try: + expected = ref_alt_bn128_add(0, 0, 0, 0) + result = await evm_precompiles.ecAdd(0, 0, 0, 0) + assert result[0] is True + assert result[1:] == expected + except OutOfGasError: + result = await evm_precompiles.ecAdd(0, 0, 0, 0) + assert result[0] is False class TestEcMul: - async def test_should_return_ec_mul_with_coordinates(self, evm_precompiles): - result = await evm_precompiles.ecMul(1, 2, 2) - expected = ref_alt_bn128_mul(1, 2, 2) - assert result == expected + @given( + x0=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + y0=integers(min_value=0, max_value=ALT_BN128_PRIME - 1), + s=integers(min_value=0, max_value=2**256 - 1), + ) + @settings(max_examples=10) + async def test_should_return_ec_mul_with_coordinates( + self, evm_precompiles, x0, y0, s + ): + try: + expected = ref_alt_bn128_mul(x0, y0, s) + result = await evm_precompiles.ecMul(x0, y0, s) + assert result[0] is True + assert result[1:] == expected + except OutOfGasError: + result = await evm_precompiles.ecMul(x0, y0, s) + assert result[0] is False async def test_should_return_ec_mul_point_at_infinity(self, evm_precompiles): - result = await evm_precompiles.ecMul(0, 0, 0) - expected = ref_alt_bn128_mul(0, 0, 0) - assert result == expected + try: + expected = ref_alt_bn128_mul(0, 0, 0) + result = await evm_precompiles.ecMul(0, 0, 0) + assert result[0] is True + assert result[1:] == expected + except OutOfGasError: + result = await evm_precompiles.ecMul(0, 0, 0) + assert result[0] is False From 2f019808a2e17bc9e082e5897bf1ff7e392ac6e0 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 16:17:11 +0200 Subject: [PATCH 05/15] delete altbn128 code --- .../src/EvmPrecompiles/EvmPrecompiles.sol | 8 +- src/kakarot/precompiles/ecadd.cairo | 58 -- src/kakarot/precompiles/ecmul.cairo | 55 -- src/utils/alt_bn128/alt_bn128_def.cairo | 22 - src/utils/alt_bn128/alt_bn128_field.cairo | 556 ------------------ src/utils/alt_bn128/alt_bn128_g1.cairo | 338 ----------- src/utils/alt_bn128/bigint.cairo | 61 -- .../src/kakarot/precompiles/test_ec_mul.cairo | 57 -- 8 files changed, 2 insertions(+), 1153 deletions(-) delete mode 100644 src/kakarot/precompiles/ecadd.cairo delete mode 100644 src/kakarot/precompiles/ecmul.cairo delete mode 100644 src/utils/alt_bn128/alt_bn128_def.cairo delete mode 100644 src/utils/alt_bn128/alt_bn128_field.cairo delete mode 100644 src/utils/alt_bn128/alt_bn128_g1.cairo delete mode 100644 src/utils/alt_bn128/bigint.cairo delete mode 100644 tests/src/kakarot/precompiles/test_ec_mul.cairo diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol index ea846d045..c23cff244 100644 --- a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -31,11 +31,7 @@ contract EvmPrecompiles { /// @return success True if the operation was successful, false otherwise /// @return x X coordinate of the result point /// @return y Y coordinate of the result point - function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) - public - view - returns (bool success, uint256 x, uint256 y) - { + function ecAdd(uint256 x1, uint256 y1, uint256 x2, uint256 y2) external view returns (bool, uint256 x, uint256 y) { bytes memory input = abi.encodePacked(x1, y1, x2, y2); (bool success, bytes memory result) = ECADD_PRECOMPILE.staticcall{gas: ECADD_GAS}(input); if (!success) { @@ -52,7 +48,7 @@ contract EvmPrecompiles { /// @return success True if the operation was successful, false otherwise /// @return x X coordinate of the result point /// @return y Y coordinate of the result point - function ecMul(uint256 x1, uint256 y1, uint256 s) public view returns (bool success, uint256 x, uint256 y) { + function ecMul(uint256 x1, uint256 y1, uint256 s) external view returns (bool, uint256 x, uint256 y) { bytes memory input = abi.encodePacked(x1, y1, s); (bool success, bytes memory result) = ECMUL_PRECOMPILE.staticcall{gas: ECMUL_GAS}(input); if (!success) { diff --git a/src/kakarot/precompiles/ecadd.cairo b/src/kakarot/precompiles/ecadd.cairo deleted file mode 100644 index 72162097b..000000000 --- a/src/kakarot/precompiles/ecadd.cairo +++ /dev/null @@ -1,58 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Starkware dependencies -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.cairo_secp.bigint import BigInt3 -from starkware.cairo.common.uint256 import Uint256 -from starkware.cairo.common.memcpy import memcpy - -// Internal dependencies -from utils.alt_bn128.alt_bn128_g1 import G1Point, ALT_BN128 -from utils.utils import Helpers - -// @title EcAdd Precompile related functions. -// @notice This file contains the logic required to run the ec_add precompile -// using alt_bn128 library -// @author @pedrobergamini -// @custom:namespace PrecompileEcAdd -namespace PrecompileEcAdd { - const PRECOMPILE_ADDRESS = 0x06; - const GAS_COST_EC_ADD = 150; - const G1POINT_BYTES_LEN = 32; - - // @notice Run the precompile. - // @param input_len The length of input array. - // @param input The input array. - // @return output_len The output length. - // @return output The output array. - // @return gas_used The gas usage of precompile. - func run{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr, - bitwise_ptr: BitwiseBuiltin*, - }(_address: felt, input_len: felt, input: felt*) -> ( - output_len: felt, output: felt*, gas_used: felt, reverted: felt - ) { - alloc_locals; - - let x0: BigInt3 = Helpers.bytes32_to_bigint(input); - let y0: BigInt3 = Helpers.bytes32_to_bigint(input + G1POINT_BYTES_LEN); - let x1: BigInt3 = Helpers.bytes32_to_bigint(input + G1POINT_BYTES_LEN * 2); - let y1: BigInt3 = Helpers.bytes32_to_bigint(input + G1POINT_BYTES_LEN * 3); - - with_attr error_message("Kakarot: ec_add failed") { - let point0: G1Point = G1Point(x0, y0); - let point1: G1Point = G1Point(x1, y1); - let result: G1Point = ALT_BN128.ec_add(point0, point1); - } - - let (bytes_x_len, output: felt*) = Helpers.bigint_to_bytes_array(result.x); - let (bytes_y_len, bytes_y: felt*) = Helpers.bigint_to_bytes_array(result.y); - memcpy(output + bytes_x_len, bytes_y, bytes_y_len); - - return (G1POINT_BYTES_LEN * 2, output, GAS_COST_EC_ADD, 0); - } -} diff --git a/src/kakarot/precompiles/ecmul.cairo b/src/kakarot/precompiles/ecmul.cairo deleted file mode 100644 index 102781fe8..000000000 --- a/src/kakarot/precompiles/ecmul.cairo +++ /dev/null @@ -1,55 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Starkware dependencies -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.cairo_secp.bigint import BigInt3 -from starkware.cairo.common.uint256 import Uint256 -from starkware.cairo.common.memcpy import memcpy - -// Internal dependencies -from utils.alt_bn128.alt_bn128_g1 import G1Point, ALT_BN128 -from utils.utils import Helpers - -// @title EcMul Precompile related functions. -// @notice This file contains the logic required to run the ec_mul precompile -// using alt_bn128 library -// @author @pedrobergamini -// @custom:namespace PrecompileEcMul -namespace PrecompileEcMul { - const PRECOMPILE_ADDRESS = 0x07; - const GAS_COST_EC_MUL = 6000; - const G1POINT_BYTES_LEN = 32; - - // @notice Run the precompile. - // @param input_len The length of input array. - // @param input The input array. - // @return output_len The output length. - // @return output The output array. - // @return gas_used The gas usage of precompile. - func run{ - syscall_ptr: felt*, - pedersen_ptr: HashBuiltin*, - range_check_ptr, - bitwise_ptr: BitwiseBuiltin*, - }(_address: felt, input_len: felt, input: felt*) -> ( - output_len: felt, output: felt*, gas_used: felt, reverted: felt - ) { - alloc_locals; - - let x: BigInt3 = Helpers.bytes32_to_bigint(input); - let y: BigInt3 = Helpers.bytes32_to_bigint(input + G1POINT_BYTES_LEN); - let scalar: BigInt3 = Helpers.bytes32_to_bigint(input + G1POINT_BYTES_LEN * 2); - - with_attr error_message("Kakarot: ec_mul failed") { - let result: G1Point = ALT_BN128.ec_mul(G1Point(x, y), scalar); - } - - let (bytes_x_len, output: felt*) = Helpers.bigint_to_bytes_array(result.x); - let (bytes_y_len, bytes_y: felt*) = Helpers.bigint_to_bytes_array(result.y); - memcpy(output + bytes_x_len, bytes_y, bytes_y_len); - - return (G1POINT_BYTES_LEN * 2, output, GAS_COST_EC_MUL, 0); - } -} diff --git a/src/utils/alt_bn128/alt_bn128_def.cairo b/src/utils/alt_bn128/alt_bn128_def.cairo deleted file mode 100644 index a6b97e6a4..000000000 --- a/src/utils/alt_bn128/alt_bn128_def.cairo +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Library copied from https://github.com/tekkac/cairo-alt_bn128 - -// Basic definitions for the alt_bn128 elliptic curve. -// The curve is given by the equation -// y^2 = x^3 + 3 -// over the field Z/p for -// p = p(u) = 36u^4 + 36u^3 + 24u^2 + 6u + 1 -// const p = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 -const P0 = 0x31ca8d3c208c16d87cfd47; -const P1 = 0x16da060561765e05aa45a1; -const P2 = 0x30644e72e131a029b8504; - -// The following constants represent the size of the curve: -// n = n(u) = 36u^4 + 36u^3 + 18u^2 + 6u + 1 -// const n = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001 -const N0 = 0x39709143e1f593f0000001; -const N1 = 0x16da06056174a0cfa121e6; -const N2 = 0x30644e72e131a029b8504; diff --git a/src/utils/alt_bn128/alt_bn128_field.cairo b/src/utils/alt_bn128/alt_bn128_field.cairo deleted file mode 100644 index a247912ee..000000000 --- a/src/utils/alt_bn128/alt_bn128_field.cairo +++ /dev/null @@ -1,556 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Library copied from https://github.com/tekkac/cairo-alt_bn128 - -// Starkware dependencies -from starkware.cairo.common.bitwise import bitwise_and -from starkware.cairo.common.cairo_builtins import BitwiseBuiltin - -// Internal dependencies -from utils.alt_bn128.bigint import ( - BASE, - BigInt3, - UnreducedBigInt3, - UnreducedBigInt5, - nondet_bigint3, - bigint_mul, -) -from utils.alt_bn128.alt_bn128_def import P0, P1, P2 - -// FIELD STRUCTURES -struct FQ2 { - e0: BigInt3, - e1: BigInt3, -} - -struct FQ12 { - e0: BigInt3, - e1: BigInt3, - e2: BigInt3, - e3: BigInt3, - e4: BigInt3, - e5: BigInt3, - e6: BigInt3, - e7: BigInt3, - e8: BigInt3, - e9: BigInt3, - eA: BigInt3, - eB: BigInt3, -} - -struct unreducedFQ12 { - e0: UnreducedBigInt5, - e1: UnreducedBigInt5, - e2: UnreducedBigInt5, - e3: UnreducedBigInt5, - e4: UnreducedBigInt5, - e5: UnreducedBigInt5, - e6: UnreducedBigInt5, - e7: UnreducedBigInt5, - e8: UnreducedBigInt5, - e9: UnreducedBigInt5, - eA: UnreducedBigInt5, - eB: UnreducedBigInt5, -} - -// FIELD CONSTANTS -func fq_zero() -> (res: BigInt3) { - return (BigInt3(0, 0, 0),); -} - -func fq2_zero() -> (res: FQ2) { - return (FQ2(e0=BigInt3(0, 0, 0), e1=BigInt3(0, 0, 0)),); -} - -func fq12_zero() -> (res: FQ12) { - return ( - FQ12( - e0=BigInt3(0, 0, 0), - e1=BigInt3(0, 0, 0), - e2=BigInt3(0, 0, 0), - e3=BigInt3(0, 0, 0), - e4=BigInt3(0, 0, 0), - e5=BigInt3(0, 0, 0), - e6=BigInt3(0, 0, 0), - e7=BigInt3(0, 0, 0), - e8=BigInt3(0, 0, 0), - e9=BigInt3(0, 0, 0), - eA=BigInt3(0, 0, 0), - eB=BigInt3(0, 0, 0), - ), - ); -} - -func fq12_one() -> (res: FQ12) { - return ( - FQ12( - e0=BigInt3(1, 0, 0), - e1=BigInt3(0, 0, 0), - e2=BigInt3(0, 0, 0), - e3=BigInt3(0, 0, 0), - e4=BigInt3(0, 0, 0), - e5=BigInt3(0, 0, 0), - e6=BigInt3(0, 0, 0), - e7=BigInt3(0, 0, 0), - e8=BigInt3(0, 0, 0), - e9=BigInt3(0, 0, 0), - eA=BigInt3(0, 0, 0), - eB=BigInt3(0, 0, 0), - ), - ); -} - -func verify_zero3{range_check_ptr}(val: BigInt3) { - alloc_locals; - local flag; - local q; - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - from starkware.cairo.common.math_utils import as_int - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - - v = pack(ids.val, PRIME) - q, r = divmod(v, P) - assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2}." - - ids.flag = 1 if q > 0 else 0 - q = q if q > 0 else 0-q - ids.q = q % PRIME - %} - assert [range_check_ptr] = q + 2 ** 127; - - tempvar carry1 = ((2 * flag - 1) * q * P0 - val.d0) / BASE; - assert [range_check_ptr + 1] = carry1 + 2 ** 127; - - tempvar carry2 = ((2 * flag - 1) * q * P1 - val.d1 + carry1) / BASE; - assert [range_check_ptr + 2] = carry2 + 2 ** 127; - - assert (2 * flag - 1) * q * P2 - val.d2 + carry2 = 0; - - let range_check_ptr = range_check_ptr + 3; - - return (); -} - -func verify_zero5{range_check_ptr}(val: UnreducedBigInt5) { - alloc_locals; - local flag; - local q1; - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - from starkware.cairo.common.math_utils import as_int - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - - v3 = as_int(ids.val.d3, PRIME) - v4 = as_int(ids.val.d4, PRIME) - v = pack(ids.val, PRIME) + v3*2**258 + v4*2**344 - - q, r = divmod(v, P) - assert r == 0, f"verify_zero: Invalid input {ids.val.d0, ids.val.d1, ids.val.d2, ids.val.d3, ids.val.d4}." - - # Since q usually doesn't fit BigInt3, divide it again - ids.flag = 1 if q > 0 else 0 - q = q if q > 0 else 0-q - q1, q2 = divmod(q, P) - ids.q1 = q1 - value = k = q2 - %} - let (k) = nondet_bigint3(); - let fullk = BigInt3(q1 * P0 + k.d0, q1 * P1 + k.d1, q1 * P2 + k.d2); - let P = BigInt3(P0, P1, P2); - let (k_n) = bigint_mul(fullk, P); - - // val mod n = 0, so val = k_n - tempvar carry1 = ((2 * flag - 1) * k_n.d0 - val.d0) / BASE; - assert [range_check_ptr + 0] = carry1 + 2 ** 127; - - tempvar carry2 = ((2 * flag - 1) * k_n.d1 - val.d1 + carry1) / BASE; - assert [range_check_ptr + 1] = carry2 + 2 ** 127; - - tempvar carry3 = ((2 * flag - 1) * k_n.d2 - val.d2 + carry2) / BASE; - assert [range_check_ptr + 2] = carry3 + 2 ** 127; - - tempvar carry4 = ((2 * flag - 1) * k_n.d3 - val.d3 + carry3) / BASE; - assert [range_check_ptr + 3] = carry4 + 2 ** 127; - - assert (2 * flag - 1) * k_n.d4 - val.d4 + carry4 = 0; - - let range_check_ptr = range_check_ptr + 4; - - return (); -} - -// returns 1 if x ==0 mod alt_bn128 prime -func is_zero{range_check_ptr}(x: BigInt3) -> (res: felt) { - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - x = pack(ids.x, PRIME) % P - %} - if (nondet %{ x == 0 %} != 0) { - verify_zero3(x); - // verify_zero5(UnreducedBigInt5(d0=x.d0, d1=x.d1, d2=x.d2, d3=0, d4=0)) - return (res=1); - } - - %{ - from starkware.python.math_utils import div_mod - value = x_inv = div_mod(1, x, P) - %} - let (x_inv) = nondet_bigint3(); - let (x_x_inv) = bigint_mul(x, x_inv); - - // Check that x * x_inv = 1 to verify that x != 0. - verify_zero5( - UnreducedBigInt5( - d0=x_x_inv.d0 - 1, d1=x_x_inv.d1, d2=x_x_inv.d2, d3=x_x_inv.d3, d4=x_x_inv.d4 - ), - ); - return (res=0); -} - -// FQ12 verify - -func verify_zero_fq12{range_check_ptr}(x: FQ12) { - verify_zero3(x.e0); - verify_zero3(x.e1); - verify_zero3(x.e2); - verify_zero3(x.e3); - verify_zero3(x.e4); - verify_zero3(x.e5); - verify_zero3(x.e6); - verify_zero3(x.e7); - verify_zero3(x.e8); - verify_zero3(x.e9); - verify_zero3(x.eA); - verify_zero3(x.eB); - return (); -} - -func fq_eq_zero(x: BigInt3) -> (res: felt) { - if (x.d0 != 0) { - return (res=0); - } - if (x.d1 != 0) { - return (res=0); - } - if (x.d2 != 0) { - return (res=0); - } - return (res=1); -} - -func fq12_eq_zero(x: FQ12) -> (res: felt) { - let (e0_is_zero) = fq_eq_zero(x.e0); - if (e0_is_zero == 0) { - return (res=0); - } - let (e1_is_zero) = fq_eq_zero(x.e1); - if (e1_is_zero == 0) { - return (res=0); - } - let (e2_is_zero) = fq_eq_zero(x.e2); - if (e2_is_zero == 0) { - return (res=0); - } - let (e3_is_zero) = fq_eq_zero(x.e3); - if (e3_is_zero == 0) { - return (res=0); - } - let (e4_is_zero) = fq_eq_zero(x.e4); - if (e4_is_zero == 0) { - return (res=0); - } - let (e5_is_zero) = fq_eq_zero(x.e5); - if (e5_is_zero == 0) { - return (res=0); - } - let (e6_is_zero) = fq_eq_zero(x.e6); - if (e6_is_zero == 0) { - return (res=0); - } - let (e7_is_zero) = fq_eq_zero(x.e7); - if (e7_is_zero == 0) { - return (res=0); - } - let (e8_is_zero) = fq_eq_zero(x.e8); - if (e8_is_zero == 0) { - return (res=0); - } - let (e9_is_zero) = fq_eq_zero(x.e9); - if (e9_is_zero == 0) { - return (res=0); - } - let (eA_is_zero) = fq_eq_zero(x.eA); - if (eA_is_zero == 0) { - return (res=0); - } - let (eB_is_zero) = fq_eq_zero(x.eB); - if (eB_is_zero == 0) { - return (res=0); - } - return (res=1); -} - -func fq12_is_zero{range_check_ptr}(x: FQ12) -> (res: felt) { - %{ - import sys, os - cwd = os.getcwd() - sys.path.append(cwd) - - from utils.bn128_field import FQ, FQ12 - from utils.bn128_utils import parse_fq12 - - val = list(map(FQ, parse_fq12(ids.x))) - - if FQ12(val) == FQ12([0]*12): - x = 0 - else: - x = 1 - %} - if (nondet %{ x == 0 %} != 0) { - verify_zero_fq12(x); - return (res=1); - } - - %{ - val = list(map(FQ, parse_fq12(ids.x))) - val = FQ12(val).inv() - value = list(map(lambda x: x.n, val.coeffs)) - %} - let (x_inv) = nondet_fq12(); - - // TODO VERIF x * x_inv - 1 = 0 - return (res=0); -} - -// Difference of two FQ12, resulting FQ12 BigInt3 limbs can be negative -func fq12_diff(x: FQ12, y: FQ12) -> (res: FQ12) { - return ( - res=FQ12( - BigInt3(d0=x.e0.d0 - y.e0.d0, d1=x.e0.d1 - y.e0.d1, d2=x.e0.d2 - y.e0.d2), - BigInt3(d0=x.e1.d0 - y.e1.d0, d1=x.e1.d1 - y.e1.d1, d2=x.e1.d2 - y.e1.d2), - BigInt3(d0=x.e2.d0 - y.e2.d0, d1=x.e2.d1 - y.e2.d1, d2=x.e2.d2 - y.e2.d2), - BigInt3(d0=x.e3.d0 - y.e3.d0, d1=x.e3.d1 - y.e3.d1, d2=x.e3.d2 - y.e3.d2), - BigInt3(d0=x.e4.d0 - y.e4.d0, d1=x.e4.d1 - y.e4.d1, d2=x.e4.d2 - y.e4.d2), - BigInt3(d0=x.e5.d0 - y.e5.d0, d1=x.e5.d1 - y.e5.d1, d2=x.e5.d2 - y.e5.d2), - BigInt3(d0=x.e6.d0 - y.e6.d0, d1=x.e6.d1 - y.e6.d1, d2=x.e6.d2 - y.e6.d2), - BigInt3(d0=x.e7.d0 - y.e7.d0, d1=x.e7.d1 - y.e7.d1, d2=x.e7.d2 - y.e7.d2), - BigInt3(d0=x.e8.d0 - y.e8.d0, d1=x.e8.d1 - y.e8.d1, d2=x.e8.d2 - y.e8.d2), - BigInt3(d0=x.e9.d0 - y.e9.d0, d1=x.e9.d1 - y.e9.d1, d2=x.e9.d2 - y.e9.d2), - BigInt3(d0=x.eA.d0 - y.eA.d0, d1=x.eA.d1 - y.eA.d1, d2=x.eA.d2 - y.eA.d2), - BigInt3(d0=x.eB.d0 - y.eB.d0, d1=x.eB.d1 - y.eB.d1, d2=x.eB.d2 - y.eB.d2), - ), - ); -} - -func fq12_sum(x: FQ12, y: FQ12) -> (res: FQ12) { - return ( - res=FQ12( - BigInt3(d0=x.e0.d0 + y.e0.d0, d1=x.e0.d1 + y.e0.d1, d2=x.e0.d2 + y.e0.d2), - BigInt3(d0=x.e1.d0 + y.e1.d0, d1=x.e1.d1 + y.e1.d1, d2=x.e1.d2 + y.e1.d2), - BigInt3(d0=x.e2.d0 + y.e2.d0, d1=x.e2.d1 + y.e2.d1, d2=x.e2.d2 + y.e2.d2), - BigInt3(d0=x.e3.d0 + y.e3.d0, d1=x.e3.d1 + y.e3.d1, d2=x.e3.d2 + y.e3.d2), - BigInt3(d0=x.e4.d0 + y.e4.d0, d1=x.e4.d1 + y.e4.d1, d2=x.e4.d2 + y.e4.d2), - BigInt3(d0=x.e5.d0 + y.e5.d0, d1=x.e5.d1 + y.e5.d1, d2=x.e5.d2 + y.e5.d2), - BigInt3(d0=x.e6.d0 + y.e6.d0, d1=x.e6.d1 + y.e6.d1, d2=x.e6.d2 + y.e6.d2), - BigInt3(d0=x.e7.d0 + y.e7.d0, d1=x.e7.d1 + y.e7.d1, d2=x.e7.d2 + y.e7.d2), - BigInt3(d0=x.e8.d0 + y.e8.d0, d1=x.e8.d1 + y.e8.d1, d2=x.e8.d2 + y.e8.d2), - BigInt3(d0=x.e9.d0 + y.e9.d0, d1=x.e9.d1 + y.e9.d1, d2=x.e9.d2 + y.e9.d2), - BigInt3(d0=x.eA.d0 + y.eA.d0, d1=x.eA.d1 + y.eA.d1, d2=x.eA.d2 + y.eA.d2), - BigInt3(d0=x.eB.d0 + y.eB.d0, d1=x.eB.d1 + y.eB.d1, d2=x.eB.d2 + y.eB.d2), - ), - ); -} - -// TODO - -// FIELD MULTIPLICATION - -// func ufq12_mul{range_check_ptr}(a : FQ12, b : FQ12) -> (res : unreducedFQ12): -// let ab_e0 = bigint_mul(a.e0, b.d0) -// let ab_e0 = bigint_mul(a.e0, b.d0) -// return ( -// unreducedFQ12( -// e0= - -// end - -// TODO deterministic (unreduced FQ12?) -func fq12_mul{range_check_ptr}(a: FQ12, b: FQ12) -> (res: FQ12) { - %{ - import sys, os - cwd = os.getcwd() - sys.path.append(cwd) - from utils.bn128_field import FQ, FQ12 - from utils.bn128_utils import parse_fq12, print_g12 - a = FQ12(list(map(FQ, parse_fq12(ids.a)))) - b = FQ12(list(map(FQ, parse_fq12(ids.b)))) - value = res = list(map(lambda x: x.n, (a*b).coeffs)) - # print("a*b =", value) - %} - let (res) = nondet_fq12(); - // TODO CHECKS - return (res=res); -} - -func fq12_pow_inner{range_check_ptr}(x: FQ12, n: felt, m: felt) -> (pow2: FQ12, res: FQ12) { - if (m == 0) { - assert n = 0; - let (one) = fq12_one(); - return (pow2=x, res=one); - } - - alloc_locals; - let (x_sqr) = fq12_mul(x, x); - - %{ memory[ap] = (ids.n % PRIME) % 2 %} - jmp odd if [ap] != 0, ap++; - return fq12_pow_inner(x=x_sqr, n=n / 2, m=m - 1); - - odd: - let (inner_pow, inner_res) = fq12_pow_inner(x=x_sqr, n=(n - 1) / 2, m=m - 1); - let (res) = fq12_mul(inner_res, x); - return (inner_pow, res); -} - -func fq12_pow_3{range_check_ptr}(x: FQ12, n: BigInt3) -> (pow2: FQ12, res: FQ12) { - alloc_locals; - let (pow2_0: FQ12, local res0: FQ12) = fq12_pow_inner(x, n.d0, 86); - let (pow2_1: FQ12, local res1: FQ12) = fq12_pow_inner(pow2_0, n.d1, 86); - let (pow2_2, local res2: FQ12) = fq12_pow_inner(pow2_1, n.d2, 86); - let (res: FQ12) = fq12_mul(res0, res1); - let (res: FQ12) = fq12_mul(res, res2); - return (pow2_2, res); -} - -func fq12_pow_12{range_check_ptr}(x: FQ12, n: FQ12) -> (res: FQ12) { - alloc_locals; - let (pow2_0: FQ12, local res0: FQ12) = fq12_pow_3(x, n.e0); - let (pow2_1: FQ12, local res1: FQ12) = fq12_pow_3(pow2_0, n.e1); - let (pow2_2: FQ12, local res2: FQ12) = fq12_pow_3(pow2_1, n.e2); - let (pow2_3: FQ12, local res3: FQ12) = fq12_pow_3(pow2_2, n.e3); - let (pow2_4: FQ12, local res4: FQ12) = fq12_pow_3(pow2_3, n.e4); - let (pow2_5: FQ12, local res5: FQ12) = fq12_pow_3(pow2_4, n.e5); - let (pow2_6: FQ12, local res6: FQ12) = fq12_pow_3(pow2_5, n.e6); - let (pow2_7: FQ12, local res7: FQ12) = fq12_pow_3(pow2_6, n.e7); - let (pow2_8: FQ12, local res8: FQ12) = fq12_pow_3(pow2_7, n.e8); - let (pow2_9: FQ12, local res9: FQ12) = fq12_pow_3(pow2_8, n.e9); - let (pow2_A: FQ12, local resA: FQ12) = fq12_pow_3(pow2_9, n.eA); - // Simplifications since eB = 0 - // let (pow2_B : FQ12, local resB : FQ12) = fq12_pow_3(pow2_A, n.eB) - let (local res01: FQ12) = fq12_mul(res0, res1); - let (local res23: FQ12) = fq12_mul(res2, res3); - let (local res45: FQ12) = fq12_mul(res4, res5); - let (local res67: FQ12) = fq12_mul(res6, res7); - let (local res89: FQ12) = fq12_mul(res8, res9); - // let (local resAB : FQ12) = fq12_mul(resA, resB) - let (local res0123: FQ12) = fq12_mul(res01, res23); - let (local res4567: FQ12) = fq12_mul(res45, res67); - // let (local res89AB : FQ12) = fq12_mul(res89, resAB) - let (local res89A: FQ12) = fq12_mul(res89, resA); - let (local res0123: FQ12) = fq12_mul(res01, res23); - let (local res0__7: FQ12) = fq12_mul(res0123, res4567); - let (res: FQ12) = fq12_mul(res0__7, res89A); - return (res,); -} - -// Hint argument: value -// a 12 element list of field elements -func nondet_fq12{range_check_ptr}() -> (res: FQ12) { - let res: FQ12 = [cast(ap + 38, FQ12*)]; - %{ - from starkware.cairo.common.cairo_secp.secp_utils import split - - r = ids.res - var_list = [r.e0, r.e1, r.e2, r.e3, r.e4, r.e5, - r.e6, r.e7, r.e8, r.e9, r.eA, r.eB] - #segments.write_arg(ids.res.e0.address_, split(val[0])) - for (var, val) in zip(var_list, value): - segments.write_arg(var.address_, split(val)) - %} - const MAX_SUM = 12 * 3 * (2 ** 86 - 1); - // TODO RANGE CHECKS? (WHY THE ASSERT LIKE THIS BTW?) - assert [range_check_ptr] = MAX_SUM - ( - res.e0.d0 + - res.e0.d1 + - res.e0.d2 + - res.e1.d0 + - res.e1.d1 + - res.e1.d2 + - res.e2.d0 + - res.e2.d1 + - res.e2.d2 + - res.e3.d0 + - res.e3.d1 + - res.e3.d2 + - res.e4.d0 + - res.e4.d1 + - res.e4.d2 + - res.e5.d0 + - res.e5.d1 + - res.e5.d2 + - res.e6.d0 + - res.e6.d1 + - res.e6.d2 + - res.e7.d0 + - res.e7.d1 + - res.e7.d2 + - res.e8.d0 + - res.e8.d1 + - res.e8.d2 + - res.e9.d0 + - res.e9.d1 + - res.e9.d2 + - res.eA.d0 + - res.eA.d1 + - res.eA.d2 + - res.eB.d0 + - res.eB.d1 + - res.eB.d2 - ); - - tempvar range_check_ptr = range_check_ptr + 37; - [range_check_ptr - 1] = res.e0.d0, ap++; - [range_check_ptr - 2] = res.e0.d1, ap++; - [range_check_ptr - 3] = res.e0.d2, ap++; - [range_check_ptr - 4] = res.e1.d0, ap++; - [range_check_ptr - 5] = res.e1.d1, ap++; - [range_check_ptr - 6] = res.e1.d2, ap++; - [range_check_ptr - 7] = res.e2.d0, ap++; - [range_check_ptr - 8] = res.e2.d1, ap++; - [range_check_ptr - 9] = res.e2.d2, ap++; - [range_check_ptr - 10] = res.e3.d0, ap++; - [range_check_ptr - 11] = res.e3.d1, ap++; - [range_check_ptr - 12] = res.e3.d2, ap++; - [range_check_ptr - 13] = res.e4.d0, ap++; - [range_check_ptr - 14] = res.e4.d1, ap++; - [range_check_ptr - 15] = res.e4.d2, ap++; - [range_check_ptr - 16] = res.e5.d0, ap++; - [range_check_ptr - 17] = res.e5.d1, ap++; - [range_check_ptr - 18] = res.e5.d2, ap++; - [range_check_ptr - 19] = res.e6.d0, ap++; - [range_check_ptr - 20] = res.e6.d1, ap++; - [range_check_ptr - 21] = res.e6.d2, ap++; - [range_check_ptr - 22] = res.e7.d0, ap++; - [range_check_ptr - 23] = res.e7.d1, ap++; - [range_check_ptr - 24] = res.e7.d2, ap++; - [range_check_ptr - 25] = res.e8.d0, ap++; - [range_check_ptr - 26] = res.e8.d1, ap++; - [range_check_ptr - 27] = res.e8.d2, ap++; - [range_check_ptr - 28] = res.e9.d0, ap++; - [range_check_ptr - 29] = res.e9.d1, ap++; - [range_check_ptr - 30] = res.e9.d2, ap++; - [range_check_ptr - 31] = res.eA.d0, ap++; - [range_check_ptr - 32] = res.eA.d1, ap++; - [range_check_ptr - 33] = res.eA.d2, ap++; - [range_check_ptr - 34] = res.eB.d0, ap++; - [range_check_ptr - 35] = res.eB.d1, ap++; - [range_check_ptr - 36] = res.eB.d2, ap++; - static_assert &res + FQ12.SIZE == ap; - return (res=res); -} diff --git a/src/utils/alt_bn128/alt_bn128_g1.cairo b/src/utils/alt_bn128/alt_bn128_g1.cairo deleted file mode 100644 index fc1ee60db..000000000 --- a/src/utils/alt_bn128/alt_bn128_g1.cairo +++ /dev/null @@ -1,338 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Library copied from https://github.com/tekkac/cairo-alt_bn128 - -// Starkware dependencies -from starkware.cairo.common.cairo_secp.bigint import UnreducedBigInt3, BigInt3 - -// Internal dependencies -from utils.alt_bn128.bigint import nondet_bigint3, UnreducedBigInt5, bigint_mul -from utils.alt_bn128.alt_bn128_field import is_zero, verify_zero5 -from utils.alt_bn128.alt_bn128_def import P0, P1, P2 - -// Represents a point on the elliptic curve. -// The zero point is represented using pt.x=0, as there is no point on the curve with this x value. -struct G1Point { - x: BigInt3, - y: BigInt3, -} - -namespace ALT_BN128 { - // Returns the slope of the elliptic curve at the given point. - // The slope is used to compute pt + pt. - // Assumption: pt != 0. - func compute_doubling_slope{range_check_ptr}(pt: G1Point) -> (slope: BigInt3) { - // Note that y cannot be zero: assume that it is, then pt = -pt, so 2 * pt = 0, which - // contradicts the fact that the size of the curve is odd. - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - from starkware.python.math_utils import div_mod - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - - # Compute the slope. - x = pack(ids.pt.x, PRIME) - y = pack(ids.pt.y, PRIME) - value = slope = div_mod(3 * x ** 2, 2 * y, P) - %} - let (slope: BigInt3) = nondet_bigint3(); - - let (x_sqr: UnreducedBigInt5) = bigint_mul(pt.x, pt.x); - let (slope_y: UnreducedBigInt5) = bigint_mul(slope, pt.y); - - verify_zero5( - UnreducedBigInt5( - d0=3 * x_sqr.d0 - 2 * slope_y.d0, - d1=3 * x_sqr.d1 - 2 * slope_y.d1, - d2=3 * x_sqr.d2 - 2 * slope_y.d2, - d3=3 * x_sqr.d3 - 2 * slope_y.d3, - d4=3 * x_sqr.d4 - 2 * slope_y.d4, - ), - ); - - return (slope=slope); - } - - // Returns the slope of the line connecting the two given points. - // The slope is used to compute pt0 + pt1. - // Assumption: pt0.x != pt1.x (mod field prime). - func compute_slope{range_check_ptr}(pt0: G1Point, pt1: G1Point) -> (slope: BigInt3) { - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - from starkware.python.math_utils import div_mod - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - # Compute the slope. - x0 = pack(ids.pt0.x, PRIME) - y0 = pack(ids.pt0.y, PRIME) - x1 = pack(ids.pt1.x, PRIME) - y1 = pack(ids.pt1.y, PRIME) - value = slope = div_mod(y0 - y1, x0 - x1, P) - %} - let (slope) = nondet_bigint3(); - - let x_diff = BigInt3( - d0=pt0.x.d0 - pt1.x.d0, d1=pt0.x.d1 - pt1.x.d1, d2=pt0.x.d2 - pt1.x.d2 - ); - let (x_diff_slope: UnreducedBigInt5) = bigint_mul(x_diff, slope); - - verify_zero5( - UnreducedBigInt5( - d0=x_diff_slope.d0 - pt0.y.d0 + pt1.y.d0, - d1=x_diff_slope.d1 - pt0.y.d1 + pt1.y.d1, - d2=x_diff_slope.d2 - pt0.y.d2 + pt1.y.d2, - d3=x_diff_slope.d3, - d4=x_diff_slope.d4, - ), - ); - - return (slope,); - } - - // Given a point 'pt' on the elliptic curve, computes pt + pt. - func ec_double{range_check_ptr}(pt: G1Point) -> (res: G1Point) { - if (pt.x.d0 == 0) { - if (pt.x.d1 == 0) { - if (pt.x.d2 == 0) { - return (pt,); - } - } - } - - let (slope: BigInt3) = compute_doubling_slope(pt); - let (slope_sqr: UnreducedBigInt5) = bigint_mul(slope, slope); - - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - slope = pack(ids.slope, PRIME) - x = pack(ids.pt.x, PRIME) - y = pack(ids.pt.y, PRIME) - - value = new_x = (pow(slope, 2, P) - 2 * x) % P - %} - let (new_x: BigInt3) = nondet_bigint3(); - - %{ value = new_y = (slope * (x - new_x) - y) % P %} - let (new_y: BigInt3) = nondet_bigint3(); - - verify_zero5( - UnreducedBigInt5( - d0=slope_sqr.d0 - new_x.d0 - 2 * pt.x.d0, - d1=slope_sqr.d1 - new_x.d1 - 2 * pt.x.d1, - d2=slope_sqr.d2 - new_x.d2 - 2 * pt.x.d2, - d3=slope_sqr.d3, - d4=slope_sqr.d4, - ), - ); - - let (x_diff_slope: UnreducedBigInt5) = bigint_mul( - BigInt3(d0=pt.x.d0 - new_x.d0, d1=pt.x.d1 - new_x.d1, d2=pt.x.d2 - new_x.d2), slope - ); - - verify_zero5( - UnreducedBigInt5( - d0=x_diff_slope.d0 - pt.y.d0 - new_y.d0, - d1=x_diff_slope.d1 - pt.y.d1 - new_y.d1, - d2=x_diff_slope.d2 - pt.y.d2 - new_y.d2, - d3=x_diff_slope.d3, - d4=x_diff_slope.d4, - ), - ); - - return (G1Point(new_x, new_y),); - } - - // Adds two points on the elliptic curve. - // Assumption: pt0.x != pt1.x (however, pt0 = pt1 = 0 is allowed). - // Note that this means that the function cannot be used if pt0 = pt1 - // (use ec_double() in this case) or pt0 = -pt1 (the result is 0 in this case). - func fast_ec_add{range_check_ptr}(pt0: G1Point, pt1: G1Point) -> (res: G1Point) { - if (pt0.x.d0 == 0) { - if (pt0.x.d1 == 0) { - if (pt0.x.d2 == 0) { - return (pt1,); - } - } - } - if (pt1.x.d0 == 0) { - if (pt1.x.d1 == 0) { - if (pt1.x.d2 == 0) { - return (pt0,); - } - } - } - - let (slope: BigInt3) = compute_slope(pt0, pt1); - let (slope_sqr: UnreducedBigInt5) = bigint_mul(slope, slope); - - %{ - from starkware.cairo.common.cairo_secp.secp_utils import pack - - P = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47 - slope = pack(ids.slope, PRIME) - x0 = pack(ids.pt0.x, PRIME) - x1 = pack(ids.pt1.x, PRIME) - y0 = pack(ids.pt0.y, PRIME) - - value = new_x = (pow(slope, 2, P) - x0 - x1) % P - %} - let (new_x: BigInt3) = nondet_bigint3(); - - %{ value = new_y = (slope * (x0 - new_x) - y0) % P %} - let (new_y: BigInt3) = nondet_bigint3(); - - verify_zero5( - UnreducedBigInt5( - d0=slope_sqr.d0 - new_x.d0 - pt0.x.d0 - pt1.x.d0, - d1=slope_sqr.d1 - new_x.d1 - pt0.x.d1 - pt1.x.d1, - d2=slope_sqr.d2 - new_x.d2 - pt0.x.d2 - pt1.x.d2, - d3=slope_sqr.d3, - d4=slope_sqr.d4, - ), - ); - - let (x_diff_slope: UnreducedBigInt5) = bigint_mul( - BigInt3(d0=pt0.x.d0 - new_x.d0, d1=pt0.x.d1 - new_x.d1, d2=pt0.x.d2 - new_x.d2), slope - ); - - verify_zero5( - UnreducedBigInt5( - d0=x_diff_slope.d0 - pt0.y.d0 - new_y.d0, - d1=x_diff_slope.d1 - pt0.y.d1 - new_y.d1, - d2=x_diff_slope.d2 - pt0.y.d2 - new_y.d2, - d3=x_diff_slope.d3, - d4=x_diff_slope.d4, - ), - ); - - return (G1Point(new_x, new_y),); - } - - // Same as fast_ec_add, except that the cases pt0 = ±pt1 are supported. - func ec_add{range_check_ptr}(pt0: G1Point, pt1: G1Point) -> (res: G1Point) { - let x_diff = BigInt3( - d0=pt0.x.d0 - pt1.x.d0, d1=pt0.x.d1 - pt1.x.d1, d2=pt0.x.d2 - pt1.x.d2 - ); - let (same_x: felt) = is_zero(x_diff); - if (same_x == 0) { - // pt0.x != pt1.x so we can use fast_ec_add. - return fast_ec_add(pt0, pt1); - } - - // We have pt0.x = pt1.x. This implies pt0.y = ±pt1.y. - // Check whether pt0.y = -pt1.y. - let y_sum = BigInt3(d0=pt0.y.d0 + pt1.y.d0, d1=pt0.y.d1 + pt1.y.d1, d2=pt0.y.d2 + pt1.y.d2); - let (opposite_y: felt) = is_zero(y_sum); - if (opposite_y != 0) { - // pt0.y = -pt1.y. - // Note that the case pt0 = pt1 = 0 falls into this branch as well. - let ZERO_POINT = G1Point(BigInt3(0, 0, 0), BigInt3(0, 0, 0)); - return (ZERO_POINT,); - } else { - // pt0.y = pt1.y. - return ec_double(pt0); - } - } - - // Given 0 <= m < 250, a scalar and a point on the elliptic curve, pt, - // verifies that 0 <= scalar < 2**m and returns (2**m * pt, scalar * pt). - func ec_mul_inner{range_check_ptr}(pt: G1Point, scalar: felt, m: felt) -> ( - pow2: G1Point, res: G1Point - ) { - if (m == 0) { - assert scalar = 0; - let ZERO_POINT = G1Point(BigInt3(0, 0, 0), BigInt3(0, 0, 0)); - return (pow2=pt, res=ZERO_POINT); - } - - alloc_locals; - let (double_pt: G1Point) = ec_double(pt); - %{ memory[ap] = (ids.scalar % PRIME) % 2 %} - jmp odd if [ap] != 0, ap++; - return ec_mul_inner(pt=double_pt, scalar=scalar / 2, m=m - 1); - - odd: - let (local inner_pow2: G1Point, inner_res: G1Point) = ec_mul_inner( - pt=double_pt, scalar=(scalar - 1) / 2, m=m - 1 - ); - // Here inner_res = (scalar - 1) / 2 * double_pt = (scalar - 1) * pt. - // Assume pt != 0 and that inner_res = ±pt. We obtain (scalar - 1) * pt = ±pt => - // scalar - 1 = ±1 (mod N) => scalar = 0 or 2. - // In both cases (scalar - 1) / 2 cannot be in the range [0, 2**(m-1)), so we get a - // contradiction. - let (res: G1Point) = fast_ec_add(pt0=pt, pt1=inner_res); - return (pow2=inner_pow2, res=res); - } - - func ec_mul{range_check_ptr}(pt: G1Point, scalar: BigInt3) -> (res: G1Point) { - alloc_locals; - let (pow2_0: G1Point, local res0: G1Point) = ec_mul_inner(pt, scalar.d0, 86); - let (pow2_1: G1Point, local res1: G1Point) = ec_mul_inner(pow2_0, scalar.d1, 86); - let (_, local res2: G1Point) = ec_mul_inner(pow2_1, scalar.d2, 84); - let (res: G1Point) = ec_add(res0, res1); - let (res: G1Point) = ec_add(res, res2); - return (res,); - } - - // CONSTANTS - func g1() -> (res: G1Point) { - return (res=G1Point(BigInt3(1, 0, 0), BigInt3(2, 0, 0))); - } - - func g1_two() -> (res: G1Point) { - return ( - G1Point( - BigInt3(0x71ca8d3c208c16d87cfd3, 0x116da060561765e05aa45a, 0x30644e72e131a029b850), - BigInt3( - 0x138fc7ff3ebf7a5a18a2c4, 0x3e5acaba7029a29a91278d, 0x15ed738c0e0a7c92e7845 - ), - ), - ); - } - - func g1_three() -> (res: G1Point) { - return ( - G1Point( - BigInt3(0x38e679f2d355961915abf0, 0xaf2c6daf4564c57611c56, 0x769bf9ac56bea3ff4023), - BigInt3( - 0x1c5b57cdf1ff3dd9fe2261, 0x2df2342191d4c6798ed02e, 0x2ab799bee0489429554fd - ), - ), - ); - } - - func g1_negone() -> (res: G1Point) { - return ( - G1Point( - BigInt3(0x1, 0x0, 0x0), - BigInt3( - 0x31ca8d3c208c16d87cfd45, 0x16da060561765e05aa45a1, 0x30644e72e131a029b8504 - ), - ), - ); - } - - func g1_negtwo() -> (res: G1Point) { - return ( - G1Point( - BigInt3(0x71ca8d3c208c16d87cfd3, 0x116da060561765e05aa45a, 0x30644e72e131a029b850), - BigInt3( - 0x1e3ac53ce1cc9c7e645a83, 0x187f3b4af14cbb6b191e14, 0x1a76dae6d3272396d0cbe - ), - ), - ); - } - - func g1_negthree() -> (res: G1Point) { - return ( - G1Point( - BigInt3(0x38e679f2d355961915abf0, 0xaf2c6daf4564c57611c56, 0x769bf9ac56bea3ff4023), - BigInt3(0x156f356e2e8cd8fe7edae6, 0x28e7d1e3cfa1978c1b7573, 0x5acb4b400e90c0063006), - ), - ); - } -} diff --git a/src/utils/alt_bn128/bigint.cairo b/src/utils/alt_bn128/bigint.cairo deleted file mode 100644 index 5aef7765d..000000000 --- a/src/utils/alt_bn128/bigint.cairo +++ /dev/null @@ -1,61 +0,0 @@ -// SPDX-License-Identifier: MIT - -%lang starknet - -// Library copied from https://github.com/tekkac/cairo-alt_bn128 - -// Starkware dependencies -from starkware.cairo.common.cairo_secp.bigint import UnreducedBigInt3, BigInt3 - -// The base of the representation. -const BASE = 2 ** 86; - -// Represents a big integer: sum_i(BASE**i * d_i). -// Note that the limbs (d_i) are NOT restricted to the range [0, BASE) and in particular they -// can be negative. -struct UnreducedBigInt5 { - d0: felt, - d1: felt, - d2: felt, - d3: felt, - d4: felt, -} - -func bigint_mul(x: BigInt3, y: BigInt3) -> (res: UnreducedBigInt5) { - return ( - UnreducedBigInt5( - d0=x.d0 * y.d0, - d1=x.d0 * y.d1 + x.d1 * y.d0, - d2=x.d0 * y.d2 + x.d1 * y.d1 + x.d2 * y.d0, - d3=x.d1 * y.d2 + x.d2 * y.d1, - d4=x.d2 * y.d2, - ), - ); -} - -// Returns a BigInt3 instance whose value is controlled by a prover hint. -// -// Soundness guarantee: each limb is in the range [0, 3 * BASE). -// Completeness guarantee (honest prover): the value is in reduced form and in particular, -// each limb is in the range [0, BASE). -// -// Hint arguments: value. -func nondet_bigint3{range_check_ptr}() -> (res: BigInt3) { - // The result should be at the end of the stack after the function returns. - let res: BigInt3 = [cast(ap + 5, BigInt3*)]; - %{ - from starkware.cairo.common.cairo_secp.secp_utils import split - segments.write_arg(ids.res.address_, split(value)) - %} - // The maximal possible sum of the limbs, assuming each of them is in the range [0, BASE). - const MAX_SUM = 3 * (BASE - 1); - assert [range_check_ptr] = MAX_SUM - (res.d0 + res.d1 + res.d2); - - // Prepare the result at the end of the stack. - tempvar range_check_ptr = range_check_ptr + 4; - [range_check_ptr - 3] = res.d0, ap++; - [range_check_ptr - 2] = res.d1, ap++; - [range_check_ptr - 1] = res.d2, ap++; - static_assert &res + BigInt3.SIZE == ap; - return (res=res); -} diff --git a/tests/src/kakarot/precompiles/test_ec_mul.cairo b/tests/src/kakarot/precompiles/test_ec_mul.cairo deleted file mode 100644 index 25f990099..000000000 --- a/tests/src/kakarot/precompiles/test_ec_mul.cairo +++ /dev/null @@ -1,57 +0,0 @@ -%lang starknet - -from starkware.cairo.common.alloc import alloc -from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin -from starkware.cairo.common.cairo_secp.bigint import BigInt3, bigint_to_uint256, uint256_to_bigint -from starkware.cairo.common.uint256 import Uint256 -from starkware.cairo.common.memcpy import memcpy - -from utils.utils import Helpers -from utils.alt_bn128.alt_bn128_g1 import ALT_BN128, G1Point -from kakarot.precompiles.ecmul import PrecompileEcMul -from tests.utils.helpers import TestHelpers - -const G1POINT_BYTES_LEN = 32; - -func test__ecmul_impl{ - syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin* -}() { - // Given - alloc_locals; - - let x: BigInt3 = uint256_to_bigint(Uint256(1, 0)); - let y: BigInt3 = uint256_to_bigint(Uint256(2, 0)); - let scalar: BigInt3 = uint256_to_bigint(Uint256(2, 0)); - let (x_bytes_len, x_bytes: felt*) = Helpers.bigint_to_bytes_array(x); - let (y_bytes_len, y_bytes: felt*) = Helpers.bigint_to_bytes_array(y); - let (scalar_bytes_len, scalar_bytes: felt*) = Helpers.bigint_to_bytes_array(scalar); - - // When - let point: G1Point = G1Point(x, y); - let (expected_point: G1Point) = ALT_BN128.ec_mul(point, scalar); - let (bytes_expected_x_len, bytes_expected_result: felt*) = Helpers.bigint_to_bytes_array( - expected_point.x - ); - let (bytes_expected_y_len, bytes_expected_y: felt*) = Helpers.bigint_to_bytes_array( - expected_point.y - ); - memcpy(bytes_expected_result + bytes_expected_x_len, bytes_expected_y, bytes_expected_y_len); - let input_len = 96; - let (input: felt*) = alloc(); - memcpy(input, x_bytes, x_bytes_len); - memcpy(input + 32, y_bytes, y_bytes_len); - memcpy(input + 64, scalar_bytes, scalar_bytes_len); - let (output_len, output: felt*, gas_used, reverted) = PrecompileEcMul.run( - PrecompileEcMul.PRECOMPILE_ADDRESS, input_len, input - ); - - // Then - TestHelpers.assert_array_equal( - array_0_len=bytes_expected_x_len + bytes_expected_y_len, - array_0=bytes_expected_result, - array_1_len=output_len, - array_1=output, - ); - - return (); -} From bc2b547ffd075a9824e1ffe9b82ee213b22246f4 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 16:58:53 +0200 Subject: [PATCH 06/15] fix: gas used on failing external precompiles --- src/kakarot/precompiles/precompiles.cairo | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index f921670b0..5cdbffafa 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -24,6 +24,7 @@ from kakarot.precompiles.precompiles_helpers import ( FIRST_KAKAROT_PRECOMPILE_ADDRESS, ) from utils.utils import Helpers +from tests.utils.debug import Debug // @title Precompile related functions. namespace Precompiles { @@ -224,11 +225,11 @@ namespace Precompiles { assert [calldata + 1] = input_len; memcpy(calldata + 2, input, input_len); let ( - success, gas, return_data_len, return_data + success, gas_if_success, return_data_len, return_data ) = ICairo1Helpers.library_call_exec_precompile( class_hash=implementation, address=evm_address, data_len=input_len, data=input ); - + let gas = gas_if_success * success; return (return_data_len, return_data, gas, 1 - success); } } From ebb1f75b885b15c9532e26d9fca388cbbe1ff238 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 17:12:09 +0200 Subject: [PATCH 07/15] feat: consume all gas of reverted precompiles --- src/kakarot/interpreter.cairo | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/kakarot/interpreter.cairo b/src/kakarot/interpreter.cairo index c15d7e918..773992875 100644 --- a/src/kakarot/interpreter.cairo +++ b/src/kakarot/interpreter.cairo @@ -85,9 +85,16 @@ namespace Interpreter { caller_code_address, caller_address, ); - let evm = EVM.charge_gas(evm, gas_used); let evm_reverted = is_not_zero(evm.reverted); let success = (1 - precompile_reverted) * (1 - evm_reverted); + // Consume all gas if precompile execution __failed__ + if (success == FALSE) { + tempvar gas_used = evm.gas_left; + } else { + tempvar gas_used = gas_used; + } + let gas_used = [ap - 1]; + let evm = EVM.charge_gas(evm, gas_used); let evm = EVM.stop(evm, output_len, output, 1 - success); let is_cairo_precompile_called = PrecompilesHelpers.is_kakarot_precompile( evm.message.code_address.evm From 517c28941766c8f844cba5ff9d276f2ca6fd95e8 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 17:13:43 +0200 Subject: [PATCH 08/15] remove constructor in test contract --- solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol | 5 ----- 1 file changed, 5 deletions(-) diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol index c23cff244..5fc94c9a2 100644 --- a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -15,11 +15,6 @@ contract EvmPrecompiles { /// @dev Gas cost for ECMUL call is 6000 uint256 private constant ECMUL_GAS = 1000000; - /*////////////////////////////////////////////////////////////// - CONSTRUCTOR - //////////////////////////////////////////////////////////////*/ - constructor() {} - /*////////////////////////////////////////////////////////////// FUNCTIONS FOR PRECOMPILES //////////////////////////////////////////////////////////////*/ From d8c164982395d3d288dc76003c64d3908060bc0d Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 18:34:12 +0200 Subject: [PATCH 09/15] fix: consumption of gas on reverted precompiles --- src/kakarot/interpreter.cairo | 11 +++++------ .../precompiles/{datacopy.cairo => identity.cairo} | 9 +++++---- src/kakarot/precompiles/precompiles.cairo | 4 ++-- tests/src/kakarot/precompiles/test_datacopy.cairo | 6 +++--- 4 files changed, 15 insertions(+), 15 deletions(-) rename src/kakarot/precompiles/{datacopy.cairo => identity.cairo} (83%) diff --git a/src/kakarot/interpreter.cairo b/src/kakarot/interpreter.cairo index 773992875..1b8157672 100644 --- a/src/kakarot/interpreter.cairo +++ b/src/kakarot/interpreter.cairo @@ -76,25 +76,24 @@ namespace Interpreter { tempvar caller_code_address = parent_context.evm.message.code_address.evm; } tempvar caller_address = evm.message.caller; - let ( - output_len, output, gas_used, precompile_reverted - ) = Precompiles.exec_precompile( + let (output_len, output, gas_used, revert_code) = Precompiles.exec_precompile( evm.message.code_address.evm, evm.message.calldata_len, evm.message.calldata, caller_code_address, caller_address, ); - let evm_reverted = is_not_zero(evm.reverted); - let success = (1 - precompile_reverted) * (1 - evm_reverted); + let precompile_reverted = is_not_zero(revert_code); // Consume all gas if precompile execution __failed__ - if (success == FALSE) { + if (precompile_reverted != FALSE) { tempvar gas_used = evm.gas_left; } else { tempvar gas_used = gas_used; } let gas_used = [ap - 1]; let evm = EVM.charge_gas(evm, gas_used); + let evm_reverted = is_not_zero(evm.reverted); + let success = (1 - precompile_reverted) * (1 - evm_reverted); let evm = EVM.stop(evm, output_len, output, 1 - success); let is_cairo_precompile_called = PrecompilesHelpers.is_kakarot_precompile( evm.message.code_address.evm diff --git a/src/kakarot/precompiles/datacopy.cairo b/src/kakarot/precompiles/identity.cairo similarity index 83% rename from src/kakarot/precompiles/datacopy.cairo rename to src/kakarot/precompiles/identity.cairo index e3c2d8d86..a852e2e3f 100644 --- a/src/kakarot/precompiles/datacopy.cairo +++ b/src/kakarot/precompiles/identity.cairo @@ -11,13 +11,14 @@ from utils.utils import Helpers from kakarot.memory import Memory from kakarot.evm import EVM -// @title DataCopy precompile +// @title Identity precompile // @custom:precompile // @custom:address 0x04 // @notice This precompile serves as a cheaper way to copy data in memory -namespace PrecompileDataCopy { +namespace PrecompileIdentity { const PRECOMPILE_ADDRESS = 0x04; - const GAS_COST_DATACOPY = 15; + const GAS_IDENTITY = 15; + const GAS_IDENTITY_WORD = 3; // @notice Run the precompile. // @param input_len The length of input array. @@ -34,6 +35,6 @@ namespace PrecompileDataCopy { output_len: felt, output: felt*, gas_used: felt, reverted: felt ) { let (minimum_word_size) = Helpers.minimum_word_count(input_len); - return (input_len, input, 3 * minimum_word_size + GAS_COST_DATACOPY, 0); + return (input_len, input, GAS_IDENTITY_WORD * minimum_word_size + GAS_IDENTITY, 0); } } diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 5cdbffafa..3131eb455 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -12,7 +12,7 @@ from kakarot.storages import Kakarot_cairo1_helpers_class_hash from kakarot.errors import Errors from kakarot.precompiles.blake2f import PrecompileBlake2f from kakarot.precompiles.kakarot_precompiles import KakarotPrecompiles -from kakarot.precompiles.datacopy import PrecompileDataCopy +from kakarot.precompiles.identity import PrecompileIdentity from kakarot.precompiles.ec_recover import PrecompileEcRecover from kakarot.precompiles.p256verify import PrecompileP256Verify from kakarot.precompiles.ripemd160 import PrecompileRIPEMD160 @@ -113,7 +113,7 @@ namespace Precompiles { ret; call PrecompileRIPEMD160.run; // 0x3 ret; - call PrecompileDataCopy.run; // 0x4 + call PrecompileIdentity.run; // 0x4 ret; call not_implemented_precompile; // 0x5 ret; diff --git a/tests/src/kakarot/precompiles/test_datacopy.cairo b/tests/src/kakarot/precompiles/test_datacopy.cairo index 467afd040..212103acd 100644 --- a/tests/src/kakarot/precompiles/test_datacopy.cairo +++ b/tests/src/kakarot/precompiles/test_datacopy.cairo @@ -4,7 +4,7 @@ from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin from starkware.cairo.common.alloc import alloc from utils.utils import Helpers -from kakarot.precompiles.datacopy import PrecompileDataCopy +from kakarot.precompiles.identity import PrecompileIdentity from tests.utils.helpers import TestHelpers func test__datacopy_impl{ @@ -20,8 +20,8 @@ func test__datacopy_impl{ %} // When - let result = PrecompileDataCopy.run( - PrecompileDataCopy.PRECOMPILE_ADDRESS, calldata_len, calldata + let result = PrecompileIdentity.run( + PrecompileIdentity.PRECOMPILE_ADDRESS, calldata_len, calldata ); TestHelpers.assert_array_equal( From d54dd34d829185a3a08d49f1cb82f4a19461f4e3 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 18:37:59 +0200 Subject: [PATCH 10/15] disable fail-test for ecadd /ecmul --- tests/src/kakarot/precompiles/test_precompiles.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/src/kakarot/precompiles/test_precompiles.py b/tests/src/kakarot/precompiles/test_precompiles.py index 489ee596e..be9355c2d 100644 --- a/tests/src/kakarot/precompiles/test_precompiles.py +++ b/tests/src/kakarot/precompiles/test_precompiles.py @@ -28,8 +28,6 @@ class TestEthereumPrecompiles: "address, error_message", [ (0x0, "Kakarot: UnknownPrecompile 0"), - (0x6, "Kakarot: NotImplementedPrecompile 6"), - (0x7, "Kakarot: NotImplementedPrecompile 7"), (0x8, "Kakarot: NotImplementedPrecompile 8"), (0x0A, "Kakarot: NotImplementedPrecompile 10"), ], From cc95f8692116d648613db4d2ce7e8cf8ea0815c6 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 18:39:13 +0200 Subject: [PATCH 11/15] check modexp is disabled --- tests/src/kakarot/precompiles/test_precompiles.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/src/kakarot/precompiles/test_precompiles.py b/tests/src/kakarot/precompiles/test_precompiles.py index be9355c2d..289727991 100644 --- a/tests/src/kakarot/precompiles/test_precompiles.py +++ b/tests/src/kakarot/precompiles/test_precompiles.py @@ -28,6 +28,7 @@ class TestEthereumPrecompiles: "address, error_message", [ (0x0, "Kakarot: UnknownPrecompile 0"), + (0x5, "Kakarot: NotImplementedPrecompile 5"), (0x8, "Kakarot: NotImplementedPrecompile 8"), (0x0A, "Kakarot: NotImplementedPrecompile 10"), ], From f7829dbad72e492117815daa4b6ebedca43eb3fe Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 22:25:56 +0200 Subject: [PATCH 12/15] fix logic regarding failure code of precompiles --- src/kakarot/interpreter.cairo | 21 +++++++++++++-------- src/kakarot/precompiles/precompiles.cairo | 3 +-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/kakarot/interpreter.cairo b/src/kakarot/interpreter.cairo index 1b8157672..0c90821f3 100644 --- a/src/kakarot/interpreter.cairo +++ b/src/kakarot/interpreter.cairo @@ -83,18 +83,23 @@ namespace Interpreter { caller_code_address, caller_address, ); + let precompile_reverted = is_not_zero(revert_code); - // Consume all gas if precompile execution __failed__ if (precompile_reverted != FALSE) { - tempvar gas_used = evm.gas_left; + // No need to charge gas as precompiles can only trigger EXCEPTIONAL_REVERT + // which will consume the entire gas of the context. + let evm = EVM.stop(evm, output_len, output, revert_code); + tempvar range_check_ptr = range_check_ptr; + tempvar evm = evm; } else { - tempvar gas_used = gas_used; + // Charge gas before stopping + let evm = EVM.charge_gas(evm, gas_used); + let evm = EVM.stop(evm, output_len, output, evm.reverted); + tempvar range_check_ptr = range_check_ptr; + tempvar evm = evm; } - let gas_used = [ap - 1]; - let evm = EVM.charge_gas(evm, gas_used); - let evm_reverted = is_not_zero(evm.reverted); - let success = (1 - precompile_reverted) * (1 - evm_reverted); - let evm = EVM.stop(evm, output_len, output, 1 - success); + let range_check_ptr = [ap - 2]; + let evm = cast([ap - 1], model.EVM*); let is_cairo_precompile_called = PrecompilesHelpers.is_kakarot_precompile( evm.message.code_address.evm ); diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index 3131eb455..f2060008e 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -24,7 +24,6 @@ from kakarot.precompiles.precompiles_helpers import ( FIRST_KAKAROT_PRECOMPILE_ADDRESS, ) from utils.utils import Helpers -from tests.utils.debug import Debug // @title Precompile related functions. namespace Precompiles { @@ -38,7 +37,7 @@ namespace Precompiles { // @return output_len The output length. // @return output The output array. // @return gas_used The gas usage of precompile. - // @return reverted Whether the precompile ran successfully or not + // @return reverted The reverted code in {0(success), REVERTED, EXCEPTIONAL_HALT}. func exec_precompile{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, From 9353a8df778349254417f29fa81e4d2a329b51c5 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 22:30:53 +0200 Subject: [PATCH 13/15] add code doc and fix return code in precompiles --- src/kakarot/precompiles/precompiles.cairo | 54 ++++++++++++++++------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index f2060008e..f2c2cdad5 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -161,10 +161,11 @@ namespace Precompiles { } // @notice A placeholder for attempts to call a precompile without permissions - // @dev Halts execution. - // @param evm_address The evm_address. - // @param input_len The length of the input array. - // @param input The input array. + // @dev Halts execution with an unauthorized precompile error. + // @return output_len The length of the error message. + // @return output The error message. + // @return gas_used The gas used (always 0 for this function). + // @return reverted The reverted code (EXCEPTIONAL_HALT). func unauthorized_precompile{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, @@ -172,14 +173,18 @@ namespace Precompiles { bitwise_ptr: BitwiseBuiltin*, }() -> (output_len: felt, output: felt*, gas_used: felt, reverted: felt) { let (revert_reason_len, revert_reason) = Errors.unauthorizedPrecompile(); - return (revert_reason_len, revert_reason, 0, Errors.REVERT); + return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT); } - // @notice A placeholder for precompile that don't exist. - // @dev Halts execution. - // @param evm_address The evm_address. - // @param input_len The length of the input array. - // @param input The input array. + // @notice A placeholder for precompiles that don't exist. + // @dev Halts execution with an unknown precompile error. + // @param evm_address The address of the unknown precompile. + // @param input_len The length of the input array (unused). + // @param input The input array (unused). + // @return output_len The length of the error message. + // @return output The error message. + // @return gas_used The gas used (always 0 for this function). + // @return reverted The reverted code (EXCEPTIONAL_HALT). func unknown_precompile{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, @@ -192,11 +197,15 @@ namespace Precompiles { return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT); } - // @notice A placeholder for precompile that are not implemented yet. - // @dev Halts execution. - // @param evm_address The evm_address. - // @param input_len The length of the input array. - // @param input The input array. + // @notice A placeholder for precompiles that are not implemented yet. + // @dev Halts execution with a not implemented precompile error. + // @param evm_address The address of the not implemented precompile. + // @param input_len The length of the input array (unused). + // @param input The input array (unused). + // @return output_len The length of the error message. + // @return output The error message. + // @return gas_used The gas used (always 0 for this function). + // @return reverted The reverted code (EXCEPTIONAL_HALT). func not_implemented_precompile{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, @@ -209,6 +218,15 @@ namespace Precompiles { return (revert_reason_len, revert_reason, 0, Errors.EXCEPTIONAL_HALT); } + // @notice Executes an external precompile using a Cairo 1 helper contract. + // @dev Calls the library_call_exec_precompile function of the ICairo1Helpers interface. + // @param evm_address The address of the external precompile. + // @param input_len The length of the input array. + // @param input The input array. + // @return output_len The length of the output data. + // @return output The output data. + // @return gas_used The gas used by the precompile execution. + // @return reverted 0 if successful, EXCEPTIONAL_HALT if execution failed. func external_precompile{ syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, @@ -229,6 +247,10 @@ namespace Precompiles { class_hash=implementation, address=evm_address, data_len=input_len, data=input ); let gas = gas_if_success * success; - return (return_data_len, return_data, gas, 1 - success); + if (success != FALSE) { + return (return_data_len, return_data, gas, 0); + } + // Precompiles can only revert with exceptions. Thus if the execution failed, it's an error EXCEPTIONAL_HALT. + return (return_data_len, return_data, gas, Errors.EXCEPTIONAL_HALT); } } From 9650acabb79356525208c5b28c1586e3332c7838 Mon Sep 17 00:00:00 2001 From: enitrat Date: Mon, 23 Sep 2024 22:34:41 +0200 Subject: [PATCH 14/15] remove extra 'gas_if_success' step --- src/kakarot/precompiles/precompiles.cairo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/kakarot/precompiles/precompiles.cairo b/src/kakarot/precompiles/precompiles.cairo index f2c2cdad5..aea8dac01 100644 --- a/src/kakarot/precompiles/precompiles.cairo +++ b/src/kakarot/precompiles/precompiles.cairo @@ -242,15 +242,14 @@ namespace Precompiles { assert [calldata + 1] = input_len; memcpy(calldata + 2, input, input_len); let ( - success, gas_if_success, return_data_len, return_data + success, gas, return_data_len, return_data ) = ICairo1Helpers.library_call_exec_precompile( class_hash=implementation, address=evm_address, data_len=input_len, data=input ); - let gas = gas_if_success * success; if (success != FALSE) { return (return_data_len, return_data, gas, 0); } // Precompiles can only revert with exceptions. Thus if the execution failed, it's an error EXCEPTIONAL_HALT. - return (return_data_len, return_data, gas, Errors.EXCEPTIONAL_HALT); + return (return_data_len, return_data, 0, Errors.EXCEPTIONAL_HALT); } } From d9cae56d9da7d3c66d0f333ac393070fe641d6a6 Mon Sep 17 00:00:00 2001 From: enitrat Date: Tue, 24 Sep 2024 10:26:59 +0200 Subject: [PATCH 15/15] update from code review --- Makefile | 2 +- .../src/EvmPrecompiles/EvmPrecompiles.sol | 4 +- .../EvmPrecompiles/test_evm_precompiles.py | 75 ++++++++----------- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index a9edfa766..f29626580 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ endif .PHONY: build test coverage clean -# 173874367 corresponds to release v0.1.13 of Kakarot SSJ. +# 176384150 corresponds to release v0.1.13 of Kakarot SSJ. KKRT_SSJ_RELEASE_ID = 176384150 # Kakarot SSJ artifacts for precompiles. KKRT_SSJ_BUILD_ARTIFACT_URL = $(shell curl -L https://api.github.com/repos/kkrt-labs/kakarot-ssj/releases/${KKRT_SSJ_RELEASE_ID} | jq -r '.assets[0].browser_download_url') diff --git a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol index 5fc94c9a2..1bd48ffa8 100644 --- a/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol +++ b/solidity_contracts/src/EvmPrecompiles/EvmPrecompiles.sol @@ -11,9 +11,9 @@ contract EvmPrecompiles { address private constant ECMUL_PRECOMPILE = address(0x07); /// @dev Gas cost for ECADD call is 150 - uint256 private constant ECADD_GAS = 1000000; + uint256 private constant ECADD_GAS = 150; /// @dev Gas cost for ECMUL call is 6000 - uint256 private constant ECMUL_GAS = 1000000; + uint256 private constant ECMUL_GAS = 6000; /*////////////////////////////////////////////////////////////// FUNCTIONS FOR PRECOMPILES diff --git a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py index 1619c65db..b7c945259 100644 --- a/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py +++ b/tests/end_to_end/EvmPrecompiles/test_evm_precompiles.py @@ -1,7 +1,6 @@ import pytest import pytest_asyncio from ethereum.base_types import U256, Uint -from ethereum.cancun.vm.exceptions import OutOfGasError from ethereum.crypto.alt_bn128 import ALT_BN128_PRIME, BNF, BNP from hypothesis import given, settings from hypothesis.strategies import integers @@ -10,15 +9,19 @@ @pytest_asyncio.fixture(scope="package") -async def evm_precompiles(owner): +async def evm_precompiles(): return await deploy( "EvmPrecompiles", "EvmPrecompiles", - caller_eoa=owner.starknet_contract, ) def ref_alt_bn128_add(x0, y0, x1, y1): + """ + # ruff: noqa: D401 + Reference implementation of the alt_bn128_add precompile. + Source: https://github.com/ethereum/execution-specs/blob/07f5747a43d62ef7f203d41d77005cb15ca5e434/src/ethereum/cancun/vm/precompiled_contracts/alt_bn128.py#L32-L103. + """ x0_value = U256.from_signed(x0) y0_value = U256.from_signed(y0) x1_value = U256.from_signed(x1) @@ -26,13 +29,13 @@ def ref_alt_bn128_add(x0, y0, x1, y1): for i in (x0_value, y0_value, x1_value, y1_value): if i >= ALT_BN128_PRIME: - raise OutOfGasError + return [False, 0, 0] try: p0 = BNP(BNF(x0_value), BNF(y0_value)) p1 = BNP(BNF(x1_value), BNF(y1_value)) except ValueError: - raise OutOfGasError from None + return [False, 0, 0] p = p0 + p1 @@ -42,24 +45,29 @@ def ref_alt_bn128_add(x0, y0, x1, y1): x = Uint(int.from_bytes(x_bytes, "big")) y = Uint(int.from_bytes(y_bytes, "big")) - return [x, y] + return [True, x, y] def ref_alt_bn128_mul(x0, y0, s): + """ + # ruff: noqa: D401 + Reference implementation of the alt_bn128_mul precompile. + Source: https://github.com/ethereum/execution-specs/blob/07f5747a43d62ef7f203d41d77005cb15ca5e434/src/ethereum/cancun/vm/precompiled_contracts/alt_bn128.py#L32-L103. + """ x0_value = U256.from_signed(x0) y0_value = U256.from_signed(y0) - U256.from_signed(s) + s_value = U256.from_signed(s) for i in (x0_value, y0_value): if i >= ALT_BN128_PRIME: - raise OutOfGasError + return [False, 0, 0] try: p0 = BNP(BNF(x0_value), BNF(y0_value)) except ValueError: - raise OutOfGasError from None + return [False, 0, 0] - p = p0.mul_by(s) + p = p0.mul_by(s_value) x_bytes = p.x.to_be_bytes32() y_bytes = p.y.to_be_bytes32() @@ -67,12 +75,11 @@ def ref_alt_bn128_mul(x0, y0, s): x = Uint(int.from_bytes(x_bytes, "big")) y = Uint(int.from_bytes(y_bytes, "big")) - return [x, y] + return [True, x, y] @pytest.mark.asyncio(scope="package") @pytest.mark.EvmPrecompiles -# @pytest.mark.xfail(reason="Katana doesn't support new builtins") class TestEvmPrecompiles: class TestEcAdd: @given( @@ -85,24 +92,14 @@ class TestEcAdd: async def test_should_return_ec_add_with_coordinates( self, evm_precompiles, x0, y0, x1, y1 ): - try: - expected = ref_alt_bn128_add(x0, y0, x1, y1) - result = await evm_precompiles.ecAdd(x0, y0, x1, y1) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecAdd(x0, y0, x1, y1) - assert result[0] is False + expected = ref_alt_bn128_add(x0, y0, x1, y1) + result = await evm_precompiles.ecAdd(x0, y0, x1, y1) + assert result == expected async def test_should_return_ec_add_point_at_infinity(self, evm_precompiles): - try: - expected = ref_alt_bn128_add(0, 0, 0, 0) - result = await evm_precompiles.ecAdd(0, 0, 0, 0) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecAdd(0, 0, 0, 0) - assert result[0] is False + expected = ref_alt_bn128_add(0, 0, 0, 0) + result = await evm_precompiles.ecAdd(0, 0, 0, 0) + assert result == expected class TestEcMul: @given( @@ -114,21 +111,11 @@ class TestEcMul: async def test_should_return_ec_mul_with_coordinates( self, evm_precompiles, x0, y0, s ): - try: - expected = ref_alt_bn128_mul(x0, y0, s) - result = await evm_precompiles.ecMul(x0, y0, s) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecMul(x0, y0, s) - assert result[0] is False + expected = ref_alt_bn128_mul(x0, y0, s) + result = await evm_precompiles.ecMul(x0, y0, s) + assert result == expected async def test_should_return_ec_mul_point_at_infinity(self, evm_precompiles): - try: - expected = ref_alt_bn128_mul(0, 0, 0) - result = await evm_precompiles.ecMul(0, 0, 0) - assert result[0] is True - assert result[1:] == expected - except OutOfGasError: - result = await evm_precompiles.ecMul(0, 0, 0) - assert result[0] is False + expected = ref_alt_bn128_mul(0, 0, 0) + result = await evm_precompiles.ecMul(0, 0, 0) + assert result == expected