From e38564e1efc3b47ee755652ca17599128a64e4f5 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Tue, 22 Oct 2024 09:08:10 +0200 Subject: [PATCH] working tests scheme --- .gitignore | 4 +- Cargo.lock | 1182 +++++++++++++++++ Cargo.toml | 13 + cairo_vm_hints/Cargo.toml | 12 +- cairo_vm_hints/src/hint_processor.rs | 37 +- .../{get_bit_length.rs => lib/bit_length.rs} | 4 +- cairo_vm_hints/src/hints/lib/mod.rs | 1 + cairo_vm_hints/src/hints/mod.rs | 7 +- .../test_is_valid_mmr_size_generate_random.rs | 84 -- ...t_is_valid_mmr_size_generate_sequential.rs | 77 -- .../hints/test_is_valid_mmr_size_print_1.rs | 17 - .../src/hints/tests/mmr_size_generate.rs | 134 ++ cairo_vm_hints/src/hints/tests/mod.rs | 2 + .../print.rs} | 16 +- cairo_vm_hints/src/lib.rs | 5 + cairo_vm_hints/src/tests/is_valid_mmr_size.rs | 12 + cairo_vm_hints/src/tests/mod.rs | 21 + 17 files changed, 1410 insertions(+), 218 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml rename cairo_vm_hints/src/hints/{get_bit_length.rs => lib/bit_length.rs} (90%) create mode 100644 cairo_vm_hints/src/hints/lib/mod.rs delete mode 100644 cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_random.rs delete mode 100644 cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_sequential.rs delete mode 100644 cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_1.rs create mode 100644 cairo_vm_hints/src/hints/tests/mmr_size_generate.rs create mode 100644 cairo_vm_hints/src/hints/tests/mod.rs rename cairo_vm_hints/src/hints/{test_is_valid_mmr_size_print_2.rs => tests/print.rs} (57%) create mode 100644 cairo_vm_hints/src/tests/is_valid_mmr_size.rs create mode 100644 cairo_vm_hints/src/tests/mod.rs diff --git a/.gitignore b/.gitignore index 9c619db..142eb44 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,6 @@ src/.DS_Store solidity-verifier/lib/* *.log -!tests/fuzzing/fixtures/*.json \ No newline at end of file +!tests/fuzzing/fixtures/*.json + +target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..a4678cf --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1182 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + +[[package]] +name = "allocator-api2" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37bf3594c4c988a53154954629820791dde498571819ae4ca50ca811e060cc95" + +[[package]] +name = "ark-ff" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" +dependencies = [ + "ark-ff-asm", + "ark-ff-macros", + "ark-serialize", + "ark-std", + "derivative", + "digest", + "itertools", + "num-bigint", + "num-traits", + "paste", + "rustc_version", + "zeroize", +] + +[[package]] +name = "ark-ff-asm" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed4aa4fe255d0bc6d79373f7e31d2ea147bcf486cba1be5ba7ea85abdb92348" +dependencies = [ + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-ff-macros" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abe79b0e4288889c4574159ab790824d0033b9fdcb2a112a3182fac2e514565" +dependencies = [ + "num-bigint", + "num-traits", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-std", + "digest", + "num-bigint", +] + +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits", + "rand", +] + +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bincode" +version = "2.0.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95" +dependencies = [ + "serde", +] + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cairo-vm" +version = "1.0.1" +source = "git+https://github.com/lambdaclass/cairo-vm?rev=3fb0344ce038b3a68cae897c403d1f561cfe8da7#3fb0344ce038b3a68cae897c403d1f561cfe8da7" +dependencies = [ + "anyhow", + "bincode", + "bitvec", + "clap", + "generic-array", + "hashbrown 0.14.5", + "hex", + "keccak", + "lazy_static", + "nom", + "num-bigint", + "num-integer", + "num-prime", + "num-traits", + "rand", + "rust_decimal", + "serde", + "serde_json", + "sha2", + "sha3", + "starknet-crypto", + "starknet-types-core", + "thiserror-no-std", + "wasm-bindgen", + "zip", +] + +[[package]] +name = "cairo_vm_hints" +version = "0.1.0" +dependencies = [ + "bincode", + "cairo-vm", + "clap", + "rand", + "starknet-types-core", + "thiserror", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.82", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "cpufeatures" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608697df725056feaccfa42cffdaeeec3fccc4ffc38358ecd19b243e716a78e0" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "crypto-bigint" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dc92fb57ca44df6db8059111ab3af99a63d5d0f8375d9972e319a379c6bab76" +dependencies = [ + "generic-array", + "subtle", + "zeroize", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "flate2" +version = "1.0.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "foldhash" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2" + +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "js-sys", + "libc", + "wasi", + "wasm-bindgen", +] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", + "serde", +] + +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" +dependencies = [ + "allocator-api2", + "equivalent", + "foldhash", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "js-sys" +version = "0.3.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "keccak" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc2af9a1119c51f12a14607e783cb977bde58bc069ff0c3da1095e635d70654" +dependencies = [ + "cpufeatures", +] + +[[package]] +name = "lambdaworks-crypto" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc2a4da0d9e52ccfe6306801a112e81a8fc0c76aa3e4449fefeda7fef72bb34" +dependencies = [ + "lambdaworks-math", + "serde", + "sha2", + "sha3", +] + +[[package]] +name = "lambdaworks-math" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1bd2632acbd9957afc5aeec07ad39f078ae38656654043bf16e046fa2730e23" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" +dependencies = [ + "spin", +] + +[[package]] +name = "libc" +version = "0.2.161" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "lru" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" +dependencies = [ + "hashbrown 0.15.0", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", + "rand", + "serde", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-modular" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a5fe11d4135c3bcdf3a95b18b194afa9608a5f6ff034f5d857bc9a27fb0119" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-prime" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e238432a7881ec7164503ccc516c014bf009be7984cde1ba56837862543bdec3" +dependencies = [ + "bitvec", + "either", + "lru", + "num-bigint", + "num-integer", + "num-modular", + "num-traits", + "rand", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.88" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rfc6979" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dd2a808d456c4a54e300a23e9f5a67e122c3024119acbfd73e3bf664491cb2" +dependencies = [ + "hmac", + "subtle", +] + +[[package]] +name = "rust_decimal" +version = "1.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b082d80e3e3cc52b2ed634388d436fe1f4de6af5786cc2de9ba9737527bdf555" +dependencies = [ + "arrayvec", + "num-traits", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "serde" +version = "1.0.211" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ac55e59090389fb9f0dd9e0f3c09615afed1d19094284d0b200441f13550793" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.211" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54be4f245ce16bc58d57ef2716271d0d4519e0f6defa147f6e081005bcb278ff" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", +] + +[[package]] +name = "serde_json" +version = "1.0.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "sha3" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" +dependencies = [ + "digest", + "keccak", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "starknet-crypto" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e2c30c01e8eb0fc913c4ee3cf676389fffc1d1182bfe5bb9670e4e72e968064" +dependencies = [ + "crypto-bigint", + "hex", + "hmac", + "num-bigint", + "num-integer", + "num-traits", + "rfc6979", + "sha2", + "starknet-crypto-codegen", + "starknet-curve", + "starknet-ff", + "zeroize", +] + +[[package]] +name = "starknet-crypto-codegen" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbc159a1934c7be9761c237333a57febe060ace2bc9e3b337a59a37af206d19f" +dependencies = [ + "starknet-curve", + "starknet-ff", + "syn 2.0.82", +] + +[[package]] +name = "starknet-curve" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1c383518bb312751e4be80f53e8644034aa99a0afb29d7ac41b89a997db875b" +dependencies = [ + "starknet-ff", +] + +[[package]] +name = "starknet-ff" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7abf1b44ec5b18d87c1ae5f54590ca9d0699ef4dd5b2ffa66fc97f24613ec585" +dependencies = [ + "ark-ff", + "crypto-bigint", + "getrandom", + "hex", +] + +[[package]] +name = "starknet-types-core" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa1b9e01ccb217ab6d475c5cda05dbb22c30029f7bb52b192a010a00d77a3d74" +dependencies = [ + "lambdaworks-crypto", + "lambdaworks-math", + "num-bigint", + "num-integer", + "num-traits", + "serde", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + +[[package]] +name = "thiserror" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", +] + +[[package]] +name = "thiserror-impl-no-std" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58e6318948b519ba6dc2b442a6d0b904ebfb8d411a3ad3e07843615a72249758" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "thiserror-no-std" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ad459d94dd517257cc96add8a43190ee620011bb6e6cdc82dafd97dfafafea" +dependencies = [ + "thiserror-impl-no-std", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "wasm-bindgen" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +dependencies = [ + "cfg-if", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.82", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", +] + +[[package]] +name = "zeroize" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.82", +] + +[[package]] +name = "zip" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "760394e246e4c28189f19d488c058bf16f564016aefac5d32bb1f3b51d5e9261" +dependencies = [ + "byteorder", + "crc32fast", + "crossbeam-utils", + "flate2", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..50a5173 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[workspace] +resolver = "2" + +members = ["cairo_vm_hints"] + + +[workspace.dependencies] +bincode = { version = "2.0.0-rc.3", default-features = false, features = ["serde"] } +cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "3fb0344ce038b3a68cae897c403d1f561cfe8da7", features = ["extensive_hints", "clap", "std"] } +clap = { version = "4.3.10", features = ["derive"] } +rand = "0.8" +starknet-types-core = "0.1.7" +thiserror = "1.0.64" \ No newline at end of file diff --git a/cairo_vm_hints/Cargo.toml b/cairo_vm_hints/Cargo.toml index 1cf5e34..cce2d5d 100644 --- a/cairo_vm_hints/Cargo.toml +++ b/cairo_vm_hints/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -cairo-vm = { git = "https://github.com/lambdaclass/cairo-vm", rev = "3fb0344ce038b3a68cae897c403d1f561cfe8da7", features = ["extensive_hints", "clap", "std"] } -starknet-types-core = "0.1.7" -thiserror = "1.0.64" -clap = { version = "4.3.10", features = ["derive"] } -bincode = { version = "2.0.0-rc.3", default-features = false, features = ["serde"] } -rand = "0.8" \ No newline at end of file +bincode.workspace = true +cairo-vm.workspace = true +clap.workspace = true +rand.workspace = true +starknet-types-core.workspace = true +thiserror.workspace = true \ No newline at end of file diff --git a/cairo_vm_hints/src/hint_processor.rs b/cairo_vm_hints/src/hint_processor.rs index fbb7ac4..3d1bef1 100644 --- a/cairo_vm_hints/src/hint_processor.rs +++ b/cairo_vm_hints/src/hint_processor.rs @@ -1,18 +1,5 @@ -use crate::hints::{ - get_bit_length::{get_bit_length, GET_BIT_LENGTH}, - test_is_valid_mmr_size_generate_random::{ - test_is_valid_mmr_size_generate_random, TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM, - }, - test_is_valid_mmr_size_generate_sequential::{ - test_is_valid_mmr_size_generate_sequential, TEST_IS_VALID_MMR_SIZE_GENERATE_SEQUENTIAL, - }, - test_is_valid_mmr_size_print_1::{ - test_is_valid_mmr_size_print_1, TEST_IS_VALID_MMR_SIZE_PRINT_1, - }, - test_is_valid_mmr_size_print_2::{ - test_is_valid_mmr_size_print_2, TEST_IS_VALID_MMR_SIZE_PRINT_2, - }, -}; +use crate::hints::lib; +use crate::hints::tests; use cairo_vm::{ hint_processor::{ builtin_hint_processor::builtin_hint_processor_definition::{ @@ -48,19 +35,21 @@ fn run_hint( constants: &HashMap, ) -> Result<(), HintError> { match hint_data.code.as_str() { - TEST_IS_VALID_MMR_SIZE_PRINT_1 => { - test_is_valid_mmr_size_print_1(vm, exec_scope, hint_data, constants) + tests::print::HINT_PRINT_BREAKLINE => { + tests::print::hint_print_breakline(vm, exec_scope, hint_data, constants) + } + tests::print::HINT_PRINT_PASS => { + tests::print::hint_print_pass(vm, exec_scope, hint_data, constants) } - TEST_IS_VALID_MMR_SIZE_PRINT_2 => { - test_is_valid_mmr_size_print_2(vm, exec_scope, hint_data, constants) + tests::mmr_size_generate::HINT_GENERATE_SEQUENTIAL => { + tests::mmr_size_generate::hint_generate_sequential(vm, exec_scope, hint_data, constants) } - TEST_IS_VALID_MMR_SIZE_GENERATE_SEQUENTIAL => { - test_is_valid_mmr_size_generate_sequential(vm, exec_scope, hint_data, constants) + tests::mmr_size_generate::HINT_GENERATE_RANDOM => { + tests::mmr_size_generate::hint_generate_random(vm, exec_scope, hint_data, constants) } - TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM => { - test_is_valid_mmr_size_generate_random(vm, exec_scope, hint_data, constants) + lib::bit_length::HINT_BIT_LENGTH => { + lib::bit_length::hint_bit_length(vm, exec_scope, hint_data, constants) } - GET_BIT_LENGTH => get_bit_length(vm, exec_scope, hint_data, constants), _ => Err(HintError::UnknownHint( hint_data.code.to_string().into_boxed_str(), )), diff --git a/cairo_vm_hints/src/hints/get_bit_length.rs b/cairo_vm_hints/src/hints/lib/bit_length.rs similarity index 90% rename from cairo_vm_hints/src/hints/get_bit_length.rs rename to cairo_vm_hints/src/hints/lib/bit_length.rs index 9c52062..5199881 100644 --- a/cairo_vm_hints/src/hints/get_bit_length.rs +++ b/cairo_vm_hints/src/hints/lib/bit_length.rs @@ -8,9 +8,9 @@ use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; use cairo_vm::Felt252; use std::collections::HashMap; -pub const GET_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()"; +pub const HINT_BIT_LENGTH: &str = "ids.bit_length = ids.x.bit_length()"; -pub fn get_bit_length( +pub fn hint_bit_length( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, diff --git a/cairo_vm_hints/src/hints/lib/mod.rs b/cairo_vm_hints/src/hints/lib/mod.rs new file mode 100644 index 0000000..c19b9c9 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/mod.rs @@ -0,0 +1 @@ +pub mod bit_length; diff --git a/cairo_vm_hints/src/hints/mod.rs b/cairo_vm_hints/src/hints/mod.rs index 487da46..f69af1d 100644 --- a/cairo_vm_hints/src/hints/mod.rs +++ b/cairo_vm_hints/src/hints/mod.rs @@ -1,5 +1,2 @@ -pub mod get_bit_length; -pub mod test_is_valid_mmr_size_generate_random; -pub mod test_is_valid_mmr_size_generate_sequential; -pub mod test_is_valid_mmr_size_print_1; -pub mod test_is_valid_mmr_size_print_2; +pub mod lib; +pub mod tests; diff --git a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_random.rs b/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_random.rs deleted file mode 100644 index 91a5053..0000000 --- a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_random.rs +++ /dev/null @@ -1,84 +0,0 @@ -use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; -use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{ - get_integer_from_var_name, get_ptr_from_var_name, -}; -use cairo_vm::types::exec_scope::ExecutionScopes; -use cairo_vm::types::relocatable::MaybeRelocatable; -use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; -use cairo_vm::Felt252; -use rand::{thread_rng, Rng}; -use starknet_types_core::felt::Felt; -use std::collections::HashMap; - -fn is_valid_mmr_size(mut mmr_size: u64) -> bool { - if mmr_size == 0 { - return false; - } - let max_height = Felt::from(mmr_size).bits() as u32; - for height in (0..=max_height).rev() { - let node_count = 2u64.pow(height + 1) - 1; - if node_count <= mmr_size { - mmr_size -= node_count; - } - } - mmr_size == 0 -} - -pub const TEST_IS_VALID_MMR_SIZE_GENERATE_RANDOM: &str = "from tools.py.mmr import is_valid_mmr_size -import random -print(f\"Testing is_valid_mmr_size against python implementation with {ids.num_sizes} random sizes in [0, 20000000)...\") -sizes_to_test = random.sample(range(0, 20000000), ids.num_sizes) -expected_output = [is_valid_mmr_size(size) for size in sizes_to_test] -segments.write_arg(ids.expected_output, expected_output) -segments.write_arg(ids.input_array, sizes_to_test)"; - -pub fn test_is_valid_mmr_size_generate_random( - vm: &mut VirtualMachine, - _exec_scope: &mut ExecutionScopes, - hint_data: &HintProcessorData, - _constants: &HashMap, -) -> Result<(), HintError> { - // let ids_data = &hint_data.ids_data; - // let ap_tracking = &hint_data.ap_tracking; - // let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; - // vm.segments.write_arg(vm.seg, arg) - let num_sizes: u64 = - get_integer_from_var_name("num_sizes", vm, &hint_data.ids_data, &hint_data.ap_tracking)? - .try_into() - .unwrap(); - - println!( - "Testing is_valid_mmr_size against python implementation with {} random sizes in [0, 20000000)...", - num_sizes - ); - - let mut rng = thread_rng(); - let mut input_array = vec![]; - let mut expected_output = vec![]; - for _ in 0..num_sizes { - let x = rng.gen_range(0..20000000); - input_array.push(MaybeRelocatable::Int(x.into())); - let y = is_valid_mmr_size(x); - expected_output.push(MaybeRelocatable::Int(y.into())); - } - - let expected_output_ptr = get_ptr_from_var_name( - "expected_output", - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - )?; - - let input_array_ptr = get_ptr_from_var_name( - "input_array", - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - )?; - - vm.segments.load_data(input_array_ptr, &input_array)?; - vm.segments - .load_data(expected_output_ptr, &expected_output)?; - - Ok(()) -} diff --git a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_sequential.rs b/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_sequential.rs deleted file mode 100644 index f3e458a..0000000 --- a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_generate_sequential.rs +++ /dev/null @@ -1,77 +0,0 @@ -use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; -use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{ - get_integer_from_var_name, get_ptr_from_var_name, -}; -use cairo_vm::types::exec_scope::ExecutionScopes; -use cairo_vm::types::relocatable::MaybeRelocatable; -use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; -use cairo_vm::Felt252; -use std::collections::{HashMap, HashSet}; - -pub const TEST_IS_VALID_MMR_SIZE_GENERATE_SEQUENTIAL: &str = "print(f\"Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {ids.num_elems})...\") -from tools.py.mmr import MMR -mmr = MMR() -valid_mmr_sizes = set() -for i in range(ids.num_elems): - mmr.add(i) - valid_mmr_sizes.add(len(mmr.pos_hash)) - -expected_output = [size in valid_mmr_sizes for size in range(0, len(mmr.pos_hash) + 1)] -for out, inp in zip(expected_output, range(0, len(mmr.pos_hash) + 1)): - print(out, inp) -segments.write_arg(ids.expected_output, expected_output) -segments.write_arg(ids.input_array, list(range(0, len(mmr.pos_hash) + 1)))"; - -pub fn test_is_valid_mmr_size_generate_sequential( - vm: &mut VirtualMachine, - _exec_scope: &mut ExecutionScopes, - hint_data: &HintProcessorData, - _constants: &HashMap, -) -> Result<(), HintError> { - // let ids_data = &hint_data.ids_data; - // let ap_tracking = &hint_data.ap_tracking; - // let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; - // vm.segments.write_arg(vm.seg, arg) - let num_elems: u64 = - get_integer_from_var_name("num_elems", vm, &hint_data.ids_data, &hint_data.ap_tracking)? - .try_into() - .unwrap(); - - println!( - "Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {})...", - num_elems - ); - - let mut valid_mmr_sizes = HashSet::new(); - let mut mmr_size = 0; - for leaf_count in 1..=num_elems { - mmr_size = 2 * leaf_count - (leaf_count.count_ones() as u64); - valid_mmr_sizes.insert(mmr_size); - } - let expected_output = (0..=mmr_size) - .map(|i| MaybeRelocatable::Int(valid_mmr_sizes.contains(&i).into())) - .collect::>(); - let input_array = (0..=mmr_size) - .map(|x| MaybeRelocatable::Int(x.into())) - .collect::>(); - - let expected_output_ptr = get_ptr_from_var_name( - "expected_output", - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - )?; - - let input_array_ptr = get_ptr_from_var_name( - "input_array", - vm, - &hint_data.ids_data, - &hint_data.ap_tracking, - )?; - - vm.segments.load_data(input_array_ptr, &input_array)?; - vm.segments - .load_data(expected_output_ptr, &expected_output)?; - - Ok(()) -} diff --git a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_1.rs b/cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_1.rs deleted file mode 100644 index b18c0d4..0000000 --- a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_1.rs +++ /dev/null @@ -1,17 +0,0 @@ -use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; -use cairo_vm::types::exec_scope::ExecutionScopes; -use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; -use cairo_vm::Felt252; -use std::collections::HashMap; - -pub const TEST_IS_VALID_MMR_SIZE_PRINT_1: &str = "print('\\n')"; - -pub fn test_is_valid_mmr_size_print_1( - _vm: &mut VirtualMachine, - _exec_scope: &mut ExecutionScopes, - _hint_data: &HintProcessorData, - _constants: &HashMap, -) -> Result<(), HintError> { - println!("\n"); - Ok(()) -} diff --git a/cairo_vm_hints/src/hints/tests/mmr_size_generate.rs b/cairo_vm_hints/src/hints/tests/mmr_size_generate.rs new file mode 100644 index 0000000..9cd107c --- /dev/null +++ b/cairo_vm_hints/src/hints/tests/mmr_size_generate.rs @@ -0,0 +1,134 @@ +use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; +use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{ + get_integer_from_var_name, get_ptr_from_var_name, +}; +use cairo_vm::types::exec_scope::ExecutionScopes; +use cairo_vm::types::relocatable::MaybeRelocatable; +use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; +use cairo_vm::Felt252; +use rand::{thread_rng, Rng}; +use starknet_types_core::felt::Felt; +use std::collections::{HashMap, HashSet}; + +fn is_valid_mmr_size(mut mmr_size: u64) -> bool { + if mmr_size == 0 { + return false; + } + let max_height = Felt::from(mmr_size).bits() as u32; + for height in (0..=max_height).rev() { + let node_count = 2u64.pow(height + 1) - 1; + if node_count <= mmr_size { + mmr_size -= node_count; + } + } + mmr_size == 0 +} + +pub const HINT_GENERATE_RANDOM: &str = "from tools.py.mmr import is_valid_mmr_size\nimport random\nprint(f\"Testing is_valid_mmr_size against python implementation with {ids.num_sizes} random sizes in [0, 20000000)...\")\nsizes_to_test = random.sample(range(0, 20000000), ids.num_sizes)\nexpected_output = [is_valid_mmr_size(size) for size in sizes_to_test]\nsegments.write_arg(ids.expected_output, expected_output)\nsegments.write_arg(ids.input_array, sizes_to_test)"; + +pub fn hint_generate_random( + vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + // let ids_data = &hint_data.ids_data; + // let ap_tracking = &hint_data.ap_tracking; + // let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; + // vm.segments.write_arg(vm.seg, arg) + let num_sizes: u64 = + get_integer_from_var_name("num_sizes", vm, &hint_data.ids_data, &hint_data.ap_tracking)? + .try_into() + .unwrap(); + + println!( + "Testing is_valid_mmr_size against python implementation with {} random sizes in [0, 20000000)...", + num_sizes + ); + + let mut rng = thread_rng(); + let mut input_array = vec![]; + let mut expected_output = vec![]; + for _ in 0..num_sizes { + let x = rng.gen_range(0..20000000); + input_array.push(MaybeRelocatable::Int(x.into())); + let y = is_valid_mmr_size(x); + expected_output.push(MaybeRelocatable::Int(y.into())); + } + + let expected_output_ptr = get_ptr_from_var_name( + "expected_output", + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + + let input_array_ptr = get_ptr_from_var_name( + "input_array", + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + + vm.segments.load_data(input_array_ptr, &input_array)?; + vm.segments + .load_data(expected_output_ptr, &expected_output)?; + + Ok(()) +} + +pub const HINT_GENERATE_SEQUENTIAL: &str = "print(f\"Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {ids.num_elems})...\")\nfrom tools.py.mmr import MMR\nmmr = MMR()\nvalid_mmr_sizes =set()\nfor i in range(ids.num_elems):\n mmr.add(i)\n valid_mmr_sizes.add(len(mmr.pos_hash))\n\nexpected_output = [size in valid_mmr_sizes for size in range(0, len(mmr.pos_hash) + 1)]\nsegments.write_arg(ids.expected_output, expected_output)\nsegments.write_arg(ids.input_array, list(range(0, len(mmr.pos_hash) + 1)))"; + +pub fn hint_generate_sequential( + vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + // let ids_data = &hint_data.ids_data; + // let ap_tracking = &hint_data.ap_tracking; + // let a = get_integer_from_var_name("x", vm, ids_data, ap_tracking)?; + // vm.segments.write_arg(vm.seg, arg) + let num_elems: u64 = + get_integer_from_var_name("num_elems", vm, &hint_data.ids_data, &hint_data.ap_tracking)? + .try_into() + .unwrap(); + + println!( + "Testing is_valid_mmr_size by creating the mmr for all sizes in [0, {})...", + num_elems + ); + + let mut valid_mmr_sizes = HashSet::new(); + let mut mmr_size = 0; + for leaf_count in 1..=num_elems { + mmr_size = 2 * leaf_count - (leaf_count.count_ones() as u64); + valid_mmr_sizes.insert(mmr_size); + } + let expected_output = (0..=mmr_size) + .map(|i| MaybeRelocatable::Int(valid_mmr_sizes.contains(&i).into())) + .collect::>(); + let input_array = (0..=mmr_size) + .map(|x| MaybeRelocatable::Int(x.into())) + .collect::>(); + + let expected_output_ptr = get_ptr_from_var_name( + "expected_output", + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + + let input_array_ptr = get_ptr_from_var_name( + "input_array", + vm, + &hint_data.ids_data, + &hint_data.ap_tracking, + )?; + + vm.segments.load_data(input_array_ptr, &input_array)?; + vm.segments + .load_data(expected_output_ptr, &expected_output)?; + + Ok(()) +} diff --git a/cairo_vm_hints/src/hints/tests/mod.rs b/cairo_vm_hints/src/hints/tests/mod.rs new file mode 100644 index 0000000..5a638e7 --- /dev/null +++ b/cairo_vm_hints/src/hints/tests/mod.rs @@ -0,0 +1,2 @@ +pub mod mmr_size_generate; +pub mod print; diff --git a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_2.rs b/cairo_vm_hints/src/hints/tests/print.rs similarity index 57% rename from cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_2.rs rename to cairo_vm_hints/src/hints/tests/print.rs index 287b775..ac437b2 100644 --- a/cairo_vm_hints/src/hints/test_is_valid_mmr_size_print_2.rs +++ b/cairo_vm_hints/src/hints/tests/print.rs @@ -4,9 +4,21 @@ use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}; use cairo_vm::Felt252; use std::collections::HashMap; -pub const TEST_IS_VALID_MMR_SIZE_PRINT_2: &str = "print(f\"\\tPass!\\n\\n\")"; +pub const HINT_PRINT_BREAKLINE: &str = "print('\\n')"; -pub fn test_is_valid_mmr_size_print_2( +pub fn hint_print_breakline( + _vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + println!("\n"); + Ok(()) +} + +pub const HINT_PRINT_PASS: &str = "print(f\"\\tPass!\\n\\n\")"; + +pub fn hint_print_pass( _vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, _hint_data: &HintProcessorData, diff --git a/cairo_vm_hints/src/lib.rs b/cairo_vm_hints/src/lib.rs index 6959b41..1ba3091 100644 --- a/cairo_vm_hints/src/lib.rs +++ b/cairo_vm_hints/src/lib.rs @@ -1,2 +1,7 @@ pub mod hint_processor; pub mod hints; + +pub use hint_processor::{CustomHintProcessor, ExtendedHintProcessor}; + +#[cfg(test)] +pub mod tests; diff --git a/cairo_vm_hints/src/tests/is_valid_mmr_size.rs b/cairo_vm_hints/src/tests/is_valid_mmr_size.rs new file mode 100644 index 0000000..4d4315b --- /dev/null +++ b/cairo_vm_hints/src/tests/is_valid_mmr_size.rs @@ -0,0 +1,12 @@ +use super::run_cairo_program; + +#[test] +fn test() { + let cairo_runner = run_cairo_program(include_bytes!( + "../../../build/compiled_cairo_files/is_valid_mmr_size.json" + )) + .unwrap(); + + let execution_resources = cairo_runner.get_execution_resources().unwrap(); + println!("n_steps: {}", execution_resources.n_steps) +} diff --git a/cairo_vm_hints/src/tests/mod.rs b/cairo_vm_hints/src/tests/mod.rs new file mode 100644 index 0000000..00dc9f6 --- /dev/null +++ b/cairo_vm_hints/src/tests/mod.rs @@ -0,0 +1,21 @@ +pub mod is_valid_mmr_size; + +use crate::ExtendedHintProcessor; +use cairo_vm::{ + cairo_run, + vm::{errors::cairo_run_errors::CairoRunError, runners::cairo_runner::CairoRunner}, +}; + +pub fn run_cairo_program(program_content: &[u8]) -> Result { + let cairo_run_config = cairo_run::CairoRunConfig { + layout: cairo_vm::types::layout_name::LayoutName::all_cairo, + allow_missing_builtins: Some(true), + ..Default::default() + }; + + Ok(cairo_run::cairo_run( + program_content, + &cairo_run_config, + &mut ExtendedHintProcessor::new(), + )?) +}