From ad090c0dfe4e69bb971987328d524cbfcbef08ad Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 28 Apr 2023 12:35:00 -0300 Subject: [PATCH 01/84] Implement TryFrom for Program --- Cargo.lock | 1116 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/types/program.rs | 28 ++ 3 files changed, 1126 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5778386a06..3c4285eb81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -24,6 +24,64 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6342bd4f5a1205d7f41e94a41a901f5647c938cdfa96036338e8533c9d6c2450" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is-terminal", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" + +[[package]] +name = "anstyle-parse" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +dependencies = [ + "anstyle", + "windows-sys 0.48.0", +] + [[package]] name = "anyhow" version = "1.0.70" @@ -42,7 +100,7 @@ dependencies = [ "ark-std", "derivative", "num-bigint", - "num-traits", + "num-traits 0.2.15", "paste", "rustc_version 0.3.3", "zeroize", @@ -65,7 +123,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db2fd794a08ccb318058009eefdf15bcaaaaf6f8161eb3345f907222bac38b20" dependencies = [ "num-bigint", - "num-traits", + "num-traits 0.2.15", "quote", "syn 1.0.109", ] @@ -86,10 +144,19 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1df2c09229cbc5a028b1d70e00fdb2acee28b1055dfb5ca73eea49c5a25c4e7c" dependencies = [ - "num-traits", + "num-traits 0.2.15", "rand", ] +[[package]] +name = "ascii-canvas" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6" +dependencies = [ + "term", +] + [[package]] name = "assert_matches" version = "1.5.0" @@ -191,11 +258,401 @@ dependencies = [ "lazy_static", "num-bigint", "num-integer", - "num-traits", + "num-traits 0.2.15", "proptest", "serde", ] +[[package]] +name = "cairo-felt" +version = "0.3.0-rc1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a93dedd19b8edf685798f1f12e4e0ac21ac196ea5262c300783f69f3fa0cb28b" +dependencies = [ + "lazy_static", + "num-bigint", + "num-integer", + "num-traits 0.2.15", + "serde", +] + +[[package]] +name = "cairo-lang-casm" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-utils", + "indoc", + "num-bigint", + "num-traits 0.2.15", + "serde", + "thiserror", +] + +[[package]] +name = "cairo-lang-compiler" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "anyhow", + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-lowering", + "cairo-lang-parser", + "cairo-lang-plugins", + "cairo-lang-project", + "cairo-lang-semantic", + "cairo-lang-sierra", + "cairo-lang-sierra-generator", + "cairo-lang-syntax", + "cairo-lang-utils", + "clap 4.2.5", + "log", + "salsa", + "smol_str", + "thiserror", +] + +[[package]] +name = "cairo-lang-debug" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" + +[[package]] +name = "cairo-lang-defs" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-parser", + "cairo-lang-syntax", + "cairo-lang-utils", + "indexmap", + "itertools", + "salsa", + "smol_str", +] + +[[package]] +name = "cairo-lang-diagnostics" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-filesystem", + "cairo-lang-utils", + "itertools", + "salsa", +] + +[[package]] +name = "cairo-lang-eq-solver" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-utils", + "good_lp", + "indexmap", + "itertools", +] + +[[package]] +name = "cairo-lang-filesystem" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-utils", + "path-clean", + "salsa", + "serde", + "smol_str", +] + +[[package]] +name = "cairo-lang-lowering" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-parser", + "cairo-lang-proc-macros", + "cairo-lang-semantic", + "cairo-lang-syntax", + "cairo-lang-utils", + "id-arena", + "indexmap", + "itertools", + "log", + "num-bigint", + "num-traits 0.2.15", + "salsa", + "smol_str", +] + +[[package]] +name = "cairo-lang-parser" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-syntax", + "cairo-lang-syntax-codegen", + "cairo-lang-utils", + "colored", + "itertools", + "log", + "num-bigint", + "num-traits 0.2.15", + "salsa", + "smol_str", + "unescaper", +] + +[[package]] +name = "cairo-lang-plugins" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-parser", + "cairo-lang-semantic", + "cairo-lang-syntax", + "cairo-lang-utils", + "indoc", + "itertools", + "salsa", + "smol_str", +] + +[[package]] +name = "cairo-lang-proc-macros" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "cairo-lang-project" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-filesystem", + "serde", + "smol_str", + "thiserror", + "toml 0.4.10", +] + +[[package]] +name = "cairo-lang-semantic" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-parser", + "cairo-lang-proc-macros", + "cairo-lang-syntax", + "cairo-lang-utils", + "id-arena", + "itertools", + "log", + "num-bigint", + "num-traits 0.2.15", + "salsa", + "smol_str", +] + +[[package]] +name = "cairo-lang-sierra" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-utils", + "const-fnv1a-hash", + "convert_case", + "derivative", + "itertools", + "lalrpop", + "lalrpop-util", + "num-bigint", + "num-traits 0.2.15", + "regex", + "salsa", + "serde", + "sha3", + "smol_str", + "thiserror", +] + +[[package]] +name = "cairo-lang-sierra-ap-change" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-eq-solver", + "cairo-lang-sierra", + "cairo-lang-utils", + "itertools", + "thiserror", +] + +[[package]] +name = "cairo-lang-sierra-gas" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-eq-solver", + "cairo-lang-sierra", + "cairo-lang-utils", + "itertools", + "thiserror", +] + +[[package]] +name = "cairo-lang-sierra-generator" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-lowering", + "cairo-lang-parser", + "cairo-lang-plugins", + "cairo-lang-proc-macros", + "cairo-lang-semantic", + "cairo-lang-sierra", + "cairo-lang-syntax", + "cairo-lang-utils", + "id-arena", + "indexmap", + "itertools", + "num-bigint", + "salsa", + "smol_str", +] + +[[package]] +name = "cairo-lang-sierra-to-casm" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "anyhow", + "assert_matches", + "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-lang-casm", + "cairo-lang-sierra", + "cairo-lang-sierra-ap-change", + "cairo-lang-sierra-gas", + "cairo-lang-utils", + "clap 4.2.5", + "indoc", + "itertools", + "log", + "num-bigint", + "num-traits 0.2.15", + "thiserror", +] + +[[package]] +name = "cairo-lang-starknet" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "anyhow", + "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", + "cairo-lang-casm", + "cairo-lang-compiler", + "cairo-lang-defs", + "cairo-lang-diagnostics", + "cairo-lang-filesystem", + "cairo-lang-lowering", + "cairo-lang-parser", + "cairo-lang-plugins", + "cairo-lang-semantic", + "cairo-lang-sierra", + "cairo-lang-sierra-ap-change", + "cairo-lang-sierra-gas", + "cairo-lang-sierra-generator", + "cairo-lang-sierra-to-casm", + "cairo-lang-syntax", + "cairo-lang-utils", + "clap 4.2.5", + "convert_case", + "genco", + "indoc", + "itertools", + "log", + "num-bigint", + "num-integer", + "num-traits 0.2.15", + "once_cell", + "serde", + "serde_json", + "sha3", + "smol_str", + "thiserror", +] + +[[package]] +name = "cairo-lang-syntax" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-debug", + "cairo-lang-filesystem", + "cairo-lang-utils", + "num-bigint", + "num-traits 0.2.15", + "salsa", + "smol_str", + "thiserror", + "unescaper", +] + +[[package]] +name = "cairo-lang-syntax-codegen" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-utils", + "genco", + "log", + "xshell", +] + +[[package]] +name = "cairo-lang-utils" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "env_logger", + "indexmap", + "itertools", + "log", + "num-bigint", + "num-integer", + "num-traits 0.2.15", + "serde", + "time", +] + [[package]] name = "cairo-take_until_unbalanced" version = "0.24.2-rc1" @@ -212,7 +669,8 @@ dependencies = [ "assert_matches", "bincode", "bitvec", - "cairo-felt", + "cairo-felt 0.3.0-rc1", + "cairo-lang-starknet", "cairo-take_until_unbalanced", "criterion", "generic-array", @@ -226,7 +684,7 @@ dependencies = [ "num-bigint", "num-integer", "num-prime", - "num-traits", + "num-traits 0.2.15", "proptest", "rand", "rand_core", @@ -303,8 +761,8 @@ checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" dependencies = [ "atty", "bitflags", - "clap_derive", - "clap_lex", + "clap_derive 3.2.18", + "clap_lex 0.2.4", "indexmap", "once_cell", "strsim", @@ -312,19 +770,55 @@ dependencies = [ "textwrap 0.16.0", ] +[[package]] +name = "clap" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a1f23fa97e1d1641371b51f35535cb26959b8e27ab50d167a8b996b5bada819" +dependencies = [ + "clap_builder", + "clap_derive 4.2.0", + "once_cell", +] + +[[package]] +name = "clap_builder" +version = "4.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fdc5d93c358224b4d6867ef1356d740de2303e9892edc06c5340daeccd96bab" +dependencies = [ + "anstream", + "anstyle", + "bitflags", + "clap_lex 0.4.1", + "strsim", +] + [[package]] name = "clap_derive" version = "3.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" dependencies = [ - "heck", + "heck 0.4.1", "proc-macro-error", "proc-macro2", "quote", "syn 1.0.109", ] +[[package]] +name = "clap_derive" +version = "4.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "syn 2.0.13", +] + [[package]] name = "clap_lex" version = "0.2.4" @@ -334,6 +828,29 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "clap_lex" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -344,6 +861,21 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "const-fnv1a-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32b13ea120a812beba79e34316b3942a857c86ec1593cb34f27bb28272ce2cca" + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "cpufeatures" version = "0.2.6" @@ -366,7 +898,7 @@ dependencies = [ "csv", "itertools", "lazy_static", - "num-traits", + "num-traits 0.2.15", "oorandom", "plotters", "rayon", @@ -432,6 +964,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crunchy" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" + [[package]] name = "crypto-bigint" version = "0.4.9" @@ -485,6 +1023,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.9.0" @@ -505,12 +1049,55 @@ dependencies = [ "subtle", ] +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "either" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +[[package]] +name = "ena" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c533630cf40e9caa44bd91aadc88a75d75a4c3a12b4cfde353cbed41daa1e1f1" +dependencies = [ + "log", +] + +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "envmnt" version = "0.8.4" @@ -551,6 +1138,12 @@ dependencies = [ "instant", ] +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "fnv" version = "1.0.7" @@ -664,6 +1257,28 @@ dependencies = [ "slab", ] +[[package]] +name = "genco" +version = "0.17.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6973ce8518068a71d404f428f6a5b563088545546a6bd8f9c0a7f2608149bc8a" +dependencies = [ + "genco-macros", + "relative-path", + "smallvec", +] + +[[package]] +name = "genco-macros" +version = "0.17.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c2c778cf01917d0fbed53900259d6604a421fab4916a2e738856ead9f1d926a" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -696,6 +1311,16 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "good_lp" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eed4d07599e3cdb52477f1d36bef936c89ce854c452e7026b2ba327b93c86f61" +dependencies = [ + "fnv", + "minilp", +] + [[package]] name = "half" version = "1.8.2" @@ -721,6 +1346,15 @@ dependencies = [ "serde", ] +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] + [[package]] name = "heck" version = "0.4.1" @@ -775,12 +1409,24 @@ dependencies = [ "digest 0.10.6", ] +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "iai" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678" +[[package]] +name = "id-arena" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25a2bc672d1148e28034f176e01fffebb08b35768468cc954630da77a1449005" + [[package]] name = "indexmap" version = "1.9.3" @@ -789,8 +1435,15 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] +[[package]] +name = "indoc" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f2cb48b81b1dc9f39676bf99f5499babfec7cd8fe14307f7b3d747208fb5690" + [[package]] name = "instant" version = "0.1.12" @@ -811,6 +1464,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "is-terminal" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f" +dependencies = [ + "hermit-abi 0.3.1", + "io-lifetimes", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "itertools" version = "0.10.5" @@ -844,6 +1509,38 @@ dependencies = [ "cpufeatures", ] +[[package]] +name = "lalrpop" +version = "0.19.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ada7ece1f5bc6d36eec2a4dc204135f14888209b3773df8fefcfe990fd4cbc" +dependencies = [ + "ascii-canvas", + "bit-set", + "diff", + "ena", + "is-terminal", + "itertools", + "lalrpop-util", + "petgraph", + "pico-args", + "regex", + "regex-syntax", + "string_cache", + "term", + "tiny-keccak", + "unicode-xid", +] + +[[package]] +name = "lalrpop-util" +version = "0.19.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d3b45d694c8074f77bc24fc26e47633c862a9cd3b48dd51209c02ba4c434d68" +dependencies = [ + "regex", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -881,6 +1578,16 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.17" @@ -899,6 +1606,15 @@ dependencies = [ "hashbrown 0.12.3", ] +[[package]] +name = "matrixmultiply" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "916806ba0031cd542105d916a97c8572e1fa6dd79c9c51e7eb43a09ec2dd84c1" +dependencies = [ + "rawpointer", +] + [[package]] name = "memchr" version = "2.5.0" @@ -923,12 +1639,41 @@ dependencies = [ "libmimalloc-sys", ] +[[package]] +name = "minilp" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82a7750a9e5076c660b7bec5e6457b4dbff402b9863c8d112891434e18fd5385" +dependencies = [ + "log", + "sprs", +] + [[package]] name = "minimal-lexical" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "ndarray" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac06db03ec2f46ee0ecdca1a1c34a99c0d188a0d83439b84bf0cb4b386e4ab09" +dependencies = [ + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits 0.2.15", + "rawpointer", +] + +[[package]] +name = "new_debug_unreachable" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" + [[package]] name = "nias" version = "0.5.0" @@ -953,11 +1698,21 @@ checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" dependencies = [ "autocfg", "num-integer", - "num-traits", + "num-traits 0.2.15", "rand", "serde", ] +[[package]] +name = "num-complex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95" +dependencies = [ + "autocfg", + "num-traits 0.2.15", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -965,7 +1720,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" dependencies = [ "autocfg", - "num-traits", + "num-traits 0.2.15", ] [[package]] @@ -976,7 +1731,7 @@ checksum = "64a5fe11d4135c3bcdf3a95b18b194afa9608a5f6ff034f5d857bc9a27fb0119" dependencies = [ "num-bigint", "num-integer", - "num-traits", + "num-traits 0.2.15", ] [[package]] @@ -991,10 +1746,19 @@ dependencies = [ "num-bigint", "num-integer", "num-modular", - "num-traits", + "num-traits 0.2.15", "rand", ] +[[package]] +name = "num-traits" +version = "0.1.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" +dependencies = [ + "num-traits 0.2.15", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1015,6 +1779,15 @@ dependencies = [ "libc", ] +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", +] + [[package]] name = "once_cell" version = "1.17.1" @@ -1033,12 +1806,66 @@ version = "6.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267" +[[package]] +name = "parking_lot" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core 0.8.6", +] + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core 0.9.7", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "winapi", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall 0.2.16", + "smallvec", + "windows-sys 0.45.0", +] + [[package]] name = "paste" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" +[[package]] +name = "path-clean" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd" + [[package]] name = "pest" version = "2.5.7" @@ -1049,6 +1876,31 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "petgraph" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dd7d28ee937e54fe3080c91faa1c3a46c06de6252988a7f4592ba2310ef22a4" +dependencies = [ + "fixedbitset", + "indexmap", +] + +[[package]] +name = "phf_shared" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" +dependencies = [ + "siphasher", +] + +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + [[package]] name = "pin-project-lite" version = "0.2.9" @@ -1067,7 +1919,7 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97" dependencies = [ - "num-traits", + "num-traits 0.2.15", "plotters-backend", "plotters-svg", "wasm-bindgen", @@ -1095,6 +1947,12 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1138,7 +1996,7 @@ dependencies = [ "bitflags", "byteorder", "lazy_static", - "num-traits", + "num-traits 0.2.15", "quick-error 2.0.1", "rand", "rand_chacha", @@ -1215,6 +2073,12 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + [[package]] name = "rayon" version = "1.7.0" @@ -1237,6 +2101,15 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_syscall" version = "0.3.5" @@ -1246,12 +2119,25 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall 0.2.16", + "thiserror", +] + [[package]] name = "regex" version = "1.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" dependencies = [ + "aho-corasick", + "memchr", "regex-syntax", ] @@ -1261,6 +2147,12 @@ version = "0.6.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" +[[package]] +name = "relative-path" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bf2521270932c3c7bed1a59151222bd7643c79310f2916f01925e1e16255698" + [[package]] name = "rfc6979" version = "0.3.1" @@ -1298,6 +2190,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc_version" version = "0.3.3" @@ -1330,6 +2228,12 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "rustversion" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06" + [[package]] name = "rusty-fork" version = "0.3.0" @@ -1351,7 +2255,7 @@ dependencies = [ "ci_info", "getopts", "nias", - "toml", + "toml 0.5.11", ] [[package]] @@ -1360,6 +2264,35 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +[[package]] +name = "salsa" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b84d9f96071f3f3be0dc818eae3327625d8ebc95b58da37d6850724f31d3403" +dependencies = [ + "crossbeam-utils", + "indexmap", + "lock_api", + "log", + "oorandom", + "parking_lot 0.11.2", + "rustc-hash", + "salsa-macros", + "smallvec", +] + +[[package]] +name = "salsa-macros" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd3904a4ba0a9d0211816177fd34b04c7095443f8cdacd11175064fe541c8fe2" +dependencies = [ + "heck 0.3.3", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "same-file" version = "1.0.6" @@ -1476,6 +2409,12 @@ dependencies = [ "keccak", ] +[[package]] +name = "siphasher" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" + [[package]] name = "slab" version = "0.4.8" @@ -1485,12 +2424,38 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "smol_str" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74212e6bbe9a4352329b2f68ba3130c15a3f26fe88ff22dbdc6cdd58fa85e99c" +dependencies = [ + "serde", +] + [[package]] name = "spin" version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" +[[package]] +name = "sprs" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec63571489873d4506683915840eeb1bb16b3198ee4894cc6f2fe3013d505e56" +dependencies = [ + "ndarray", + "num-complex", + "num-traits 0.1.43", +] + [[package]] name = "starknet-crypto" version = "0.4.3" @@ -1502,7 +2467,7 @@ dependencies = [ "hmac", "num-bigint", "num-integer", - "num-traits", + "num-traits 0.2.15", "rfc6979", "sha2", "starknet-crypto-codegen", @@ -1543,6 +2508,19 @@ dependencies = [ "hex", ] +[[package]] +name = "string_cache" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91138e76242f575eb1d3b38b4f1362f10d3a43f47d182a5b359af488a02293b" +dependencies = [ + "new_debug_unreachable", + "once_cell", + "parking_lot 0.12.1", + "phf_shared", + "precomputed-hash", +] + [[package]] name = "strsim" version = "0.10.0" @@ -1591,11 +2569,22 @@ checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" dependencies = [ "cfg-if", "fastrand", - "redox_syscall", + "redox_syscall 0.3.5", "rustix", "windows-sys 0.45.0", ] +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + [[package]] name = "termcolor" version = "1.2.0" @@ -1660,6 +2649,44 @@ dependencies = [ "thiserror-impl-no-std", ] +[[package]] +name = "time" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0cbfecb4d19b5ea75bb31ad904eb5b9fa13f21079c3b92017ebdf4999a5890" +dependencies = [ + "itoa", + "libc", + "num_threads", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" + +[[package]] +name = "time-macros" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd80a657e71da814b8e5d60d3374fc6d35045062245d80224748ae522dd76f36" +dependencies = [ + "time-core", +] + +[[package]] +name = "tiny-keccak" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" +dependencies = [ + "crunchy", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -1670,6 +2697,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "toml" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f" +dependencies = [ + "serde", +] + [[package]] name = "toml" version = "0.5.11" @@ -1697,18 +2733,45 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" +[[package]] +name = "unescaper" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "995483205de764db1185c9461a000fff73fa4b9ee2bbe4c8b4027a94692700fe" +dependencies = [ + "thiserror", +] + [[package]] name = "unicode-ident" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +[[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + [[package]] name = "unicode-width" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "version_check" version = "0.9.4" @@ -2018,6 +3081,21 @@ dependencies = [ "tap", ] +[[package]] +name = "xshell" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "962c039b3a7b16cf4e9a4248397c6585c07547412e7d6a6e035389a802dcfe90" +dependencies = [ + "xshell-macros", +] + +[[package]] +name = "xshell-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dbabb1cbd15a1d6d12d9ed6b35cc6777d4af87ab3ba155ea37215f20beab80c" + [[package]] name = "zeroize" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 3fa3785db3..cc058f5c39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -79,6 +79,7 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default "alloc", ] } bitvec = { version = "1", default-features = false, features = ["alloc"] } +cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46"} [dev-dependencies] assert_matches = "1.5.0" diff --git a/src/types/program.rs b/src/types/program.rs index 3d29aeaa60..243b6af5fe 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -7,6 +7,7 @@ use crate::{ }, types::{errors::program_errors::ProgramError, relocatable::MaybeRelocatable}, }; +use cairo_lang_starknet::casm_contract_class::CasmContractClass; use felt::{Felt252, PRIME_STR}; #[cfg(feature = "std")] @@ -145,6 +146,33 @@ impl Default for Program { } } +// Note: This Program will only work when using run_from_entrypoint +impl TryFrom for Program { + type Error = ProgramError; + fn try_from(value: CasmContractClass) -> Result { + let data = value + .bytecode + .iter() + .map(|x| MaybeRelocatable::from(Felt252::from(&x.value))) + .collect(); + let hints = HashMap::new(); // Hint data is going to be hosted processor-side, handle this later + let error_message_attributes = Vec::new(); + let reference_manager = ReferenceManager { + references: Vec::new(), + }; + Self::new( + vec![], + data, + None, + hints, + reference_manager, + HashMap::new(), + error_message_attributes, + None, + ) + } +} + #[cfg(test)] mod tests { use super::*; From 814f62f1253923b3df564324d83857028651e90e Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 28 Apr 2023 13:16:41 -0300 Subject: [PATCH 02/84] Add cairo1hintprocessor dependency --- Cargo.lock | 386 ++++++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 1 + 2 files changed, 356 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3c4285eb81..c8504e3045 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,6 +33,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anstream" version = "0.3.1" @@ -94,10 +103,10 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b3235cc41ee7a12aaaf2c575a2ad7b46713a8a50bda2fc3b003a04845c05dd6" dependencies = [ - "ark-ff-asm", - "ark-ff-macros", - "ark-serialize", - "ark-std", + "ark-ff-asm 0.3.0", + "ark-ff-macros 0.3.0", + "ark-serialize 0.3.0", + "ark-std 0.3.0", "derivative", "num-bigint", "num-traits 0.2.15", @@ -106,6 +115,26 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ark-ff" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec847af850f44ad29048935519032c33da8aa03340876d351dfab5660d2966ba" +dependencies = [ + "ark-ff-asm 0.4.2", + "ark-ff-macros 0.4.2", + "ark-serialize 0.4.2", + "ark-std 0.4.0", + "derivative", + "digest 0.10.6", + "itertools", + "num-bigint", + "num-traits 0.2.15", + "paste", + "rustc_version 0.4.0", + "zeroize", +] + [[package]] name = "ark-ff-asm" version = "0.3.0" @@ -116,6 +145,16 @@ dependencies = [ "syn 1.0.109", ] +[[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.3.0" @@ -128,16 +167,40 @@ dependencies = [ "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 0.2.15", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "ark-serialize" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d6c2b318ee6e10f8c2853e73a83adc0ccb88995aa978d8a3408d492ab2ee671" dependencies = [ - "ark-std", + "ark-std 0.3.0", "digest 0.9.0", ] +[[package]] +name = "ark-serialize" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb7b85a02b83d2f22f89bd5cac66c9c89474240cb6207cb1efc16d098e822a5" +dependencies = [ + "ark-std 0.4.0", + "digest 0.10.6", + "num-bigint", +] + [[package]] name = "ark-std" version = "0.3.0" @@ -148,6 +211,16 @@ dependencies = [ "rand", ] +[[package]] +name = "ark-std" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94893f1e0c6eeab764ade8dc4c0db24caf4fe7cbbaafc0eba0a9030f447b5185" +dependencies = [ + "num-traits 0.2.15", + "rand", +] + [[package]] name = "ascii-canvas" version = "3.0.0" @@ -251,6 +324,21 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cairo-1-hint-processor" +version = "0.1.0" +source = "git+https://github.com/lambdaclass/starknet_in_rust.git?rev=2dbde61f42073b4615a63e3b2475ddb861109409#2dbde61f42073b4615a63e3b2475ddb861109409" +dependencies = [ + "ark-ff 0.4.2", + "ark-std 0.3.0", + "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", + "cairo-lang-utils 0.1.0", + "cairo-vm 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "num-bigint", + "num-integer", + "num-traits 0.2.15", +] + [[package]] name = "cairo-felt" version = "0.3.0-rc1" @@ -276,12 +364,37 @@ dependencies = [ "serde", ] +[[package]] +name = "cairo-felt" +version = "0.3.0-rc1" +source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +dependencies = [ + "lazy_static", + "num-bigint", + "num-integer", + "num-traits 0.2.15", + "serde", +] + [[package]] name = "cairo-lang-casm" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "indoc", + "num-bigint", + "num-traits 0.2.15", + "serde", + "thiserror", +] + +[[package]] +name = "cairo-lang-casm" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", "indoc", "num-bigint", "num-traits 0.2.15", @@ -306,7 +419,7 @@ dependencies = [ "cairo-lang-sierra", "cairo-lang-sierra-generator", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "clap 4.2.5", "log", "salsa", @@ -329,7 +442,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-parser", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "indexmap", "itertools", "salsa", @@ -342,7 +455,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-filesystem", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "itertools", "salsa", ] @@ -352,7 +465,7 @@ name = "cairo-lang-eq-solver" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "good_lp", "indexmap", "itertools", @@ -364,7 +477,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-debug", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "path-clean", "salsa", "serde", @@ -384,7 +497,7 @@ dependencies = [ "cairo-lang-proc-macros", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "id-arena", "indexmap", "itertools", @@ -404,7 +517,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-syntax", "cairo-lang-syntax-codegen", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "colored", "itertools", "log", @@ -426,7 +539,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "indoc", "itertools", "salsa", @@ -467,7 +580,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-proc-macros", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "id-arena", "itertools", "log", @@ -482,7 +595,7 @@ name = "cairo-lang-sierra" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "const-fnv1a-hash", "convert_case", "derivative", @@ -506,7 +619,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "itertools", "thiserror", ] @@ -518,7 +631,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "itertools", "thiserror", ] @@ -539,7 +652,7 @@ dependencies = [ "cairo-lang-semantic", "cairo-lang-sierra", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "id-arena", "indexmap", "itertools", @@ -556,11 +669,11 @@ dependencies = [ "anyhow", "assert_matches", "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", - "cairo-lang-casm", + "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "cairo-lang-sierra", "cairo-lang-sierra-ap-change", "cairo-lang-sierra-gas", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "clap 4.2.5", "indoc", "itertools", @@ -577,7 +690,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "anyhow", "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", - "cairo-lang-casm", + "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-diagnostics", @@ -592,7 +705,7 @@ dependencies = [ "cairo-lang-sierra-generator", "cairo-lang-sierra-to-casm", "cairo-lang-syntax", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "clap 4.2.5", "convert_case", "genco", @@ -617,7 +730,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "num-bigint", "num-traits 0.2.15", "salsa", @@ -631,12 +744,25 @@ name = "cairo-lang-syntax-codegen" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils", + "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", "genco", "log", "xshell", ] +[[package]] +name = "cairo-lang-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7196aada3418cb09e7aa2b39f0a6bb660e09adccf565d340457bbcbeef439106" +dependencies = [ + "chrono", + "env_logger", + "indexmap", + "itertools", + "log", +] + [[package]] name = "cairo-lang-utils" version = "1.0.0-alpha.7" @@ -650,7 +776,23 @@ dependencies = [ "num-integer", "num-traits 0.2.15", "serde", - "time", + "time 0.3.20", +] + +[[package]] +name = "cairo-lang-utils" +version = "1.0.0-alpha.7" +source = "git+https://github.com/starkware-libs/cairo#a08bedbf26b711e81cec8b7660698a3788e67d46" +dependencies = [ + "env_logger", + "indexmap", + "itertools", + "log", + "num-bigint", + "num-integer", + "num-traits 0.2.15", + "serde", + "time 0.3.20", ] [[package]] @@ -661,6 +803,14 @@ dependencies = [ "wasm-bindgen-test", ] +[[package]] +name = "cairo-take_until_unbalanced" +version = "0.24.2-rc1" +source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +dependencies = [ + "nom", +] + [[package]] name = "cairo-vm" version = "0.3.0-rc1" @@ -669,9 +819,10 @@ dependencies = [ "assert_matches", "bincode", "bitvec", + "cairo-1-hint-processor", "cairo-felt 0.3.0-rc1", "cairo-lang-starknet", - "cairo-take_until_unbalanced", + "cairo-take_until_unbalanced 0.24.2-rc1", "criterion", "generic-array", "hashbrown 0.13.2", @@ -701,13 +852,46 @@ dependencies = [ "wasm-bindgen-test", ] +[[package]] +name = "cairo-vm" +version = "0.3.0-rc1" +source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +dependencies = [ + "anyhow", + "bincode", + "bitvec", + "cairo-felt 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "cairo-take_until_unbalanced 0.24.2-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "generic-array", + "hashbrown 0.13.2", + "hex", + "keccak", + "lazy_static", + "mimalloc", + "nom", + "num-bigint", + "num-integer", + "num-prime", + "num-traits 0.2.15", + "rand", + "rand_core", + "serde", + "serde_bytes", + "serde_json", + "sha2", + "sha3", + "starknet-crypto", + "thiserror", + "thiserror-no-std", +] + [[package]] name = "cairo-vm-cli" version = "0.3.0-rc1" dependencies = [ "assert_matches", "bincode", - "cairo-vm", + "cairo-vm 0.3.0-rc1", "clap 3.2.23", "mimalloc", "nom", @@ -733,6 +917,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-integer", + "num-traits 0.2.15", + "time 0.1.45", + "wasm-bindgen", + "winapi", +] + [[package]] name = "ci_info" version = "0.10.2" @@ -834,6 +1033,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -876,6 +1085,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + [[package]] name = "cpufeatures" version = "0.2.6" @@ -1012,6 +1227,50 @@ dependencies = [ "memchr", ] +[[package]] +name = "cxx" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn 2.0.13", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.13", +] + [[package]] name = "derivative" version = "2.2.0" @@ -1307,7 +1566,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", "wasm-bindgen", ] @@ -1395,7 +1654,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" name = "hint_accountant" version = "0.3.0-rc1" dependencies = [ - "cairo-vm", + "cairo-vm 0.3.0-rc1", "serde", "serde_json", ] @@ -1421,6 +1680,30 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678" +[[package]] +name = "iana-time-zone" +version = "0.1.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" +dependencies = [ + "cxx", + "cxx-build", +] + [[package]] name = "id-arena" version = "2.2.1" @@ -1572,6 +1855,15 @@ dependencies = [ "libc", ] +[[package]] +name = "link-cplusplus" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" +dependencies = [ + "cc", +] + [[package]] name = "linux-raw-sys" version = "0.3.1" @@ -2314,6 +2606,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "scratch" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" + [[package]] name = "semver" version = "0.11.0" @@ -2502,7 +2800,7 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78d484109da192f3a8cd58f674861c2d5e4b3e1765a466362c6f350ef213dfd1" dependencies = [ - "ark-ff", + "ark-ff 0.3.0", "crypto-bigint", "getrandom", "hex", @@ -2649,6 +2947,17 @@ dependencies = [ "thiserror-impl-no-std", ] +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "time" version = "0.3.20" @@ -2803,6 +3112,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -2940,6 +3255,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.0", +] + [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index cc058f5c39..ef15d38248 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,7 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default ] } bitvec = { version = "1", default-features = false, features = ["alloc"] } cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46"} +cairo-1-hint-processor = {git = "https://github.com/lambdaclass/starknet_in_rust.git", rev = "2dbde61f42073b4615a63e3b2475ddb861109409"} [dev-dependencies] assert_matches = "1.5.0" From 5ad52b3df4c95ade2410cf4895be94f39fdc182a Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 14:54:07 -0300 Subject: [PATCH 03/84] Map hints to pc --- src/types/program.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/types/program.rs b/src/types/program.rs index 243b6af5fe..dad8e8af70 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -1,3 +1,4 @@ +use crate::serde::deserialize_program::{ApTracking, FlowTrackingData}; use crate::stdlib::{collections::HashMap, prelude::*, sync::Arc}; use crate::{ @@ -155,7 +156,25 @@ impl TryFrom for Program { .iter() .map(|x| MaybeRelocatable::from(Felt252::from(&x.value))) .collect(); - let hints = HashMap::new(); // Hint data is going to be hosted processor-side, handle this later + //Hint data is going to be hosted processor-side, hints fields will only store the pc where hints are located. + // Only one pc will be stored, so the hint processor will be responsible for executing all hints for a given pc + let hints = value + .hints + .iter() + .map(|(x, _)| { + ( + *x, + vec![HintParams { + code: x.to_string(), + accessible_scopes: Vec::new(), + flow_tracking_data: FlowTrackingData { + ap_tracking: ApTracking::default(), + reference_ids: HashMap::new(), + }, + }], + ) + }) + .collect(); let error_message_attributes = Vec::new(); let reference_manager = ReferenceManager { references: Vec::new(), From 2c809357a71e29572e3e60a67a354465aae6005f Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 15:49:56 -0300 Subject: [PATCH 04/84] Add Cairo1HintProcessor --- Cargo.lock | 28 +- Cargo.toml | 2 +- cairo-1-hint-processor/Cargo.toml | 15 + cairo-1-hint-processor/src/dict_manager.rs | 125 +++ cairo-1-hint-processor/src/hint_processor.rs | 835 +++++++++++++++++++ cairo-1-hint-processor/src/lib.rs | 2 + src/types/program.rs | 2 +- 7 files changed, 1000 insertions(+), 9 deletions(-) create mode 100644 cairo-1-hint-processor/Cargo.toml create mode 100644 cairo-1-hint-processor/src/dict_manager.rs create mode 100644 cairo-1-hint-processor/src/hint_processor.rs create mode 100644 cairo-1-hint-processor/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index c8504e3045..76be079dc2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -324,6 +324,20 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "cairo-1-hint-processor" +version = "0.1.0" +dependencies = [ + "ark-ff 0.4.2", + "ark-std 0.3.0", + "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", + "cairo-lang-utils 0.1.0", + "cairo-vm 0.3.0-rc1", + "num-bigint", + "num-integer", + "num-traits 0.2.15", +] + [[package]] name = "cairo-1-hint-processor" version = "0.1.0" @@ -333,7 +347,7 @@ dependencies = [ "ark-std 0.3.0", "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", "cairo-lang-utils 0.1.0", - "cairo-vm 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "cairo-vm 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", "num-bigint", "num-integer", "num-traits 0.2.15", @@ -367,7 +381,7 @@ dependencies = [ [[package]] name = "cairo-felt" version = "0.3.0-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" dependencies = [ "lazy_static", "num-bigint", @@ -806,7 +820,7 @@ dependencies = [ [[package]] name = "cairo-take_until_unbalanced" version = "0.24.2-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" dependencies = [ "nom", ] @@ -819,7 +833,7 @@ dependencies = [ "assert_matches", "bincode", "bitvec", - "cairo-1-hint-processor", + "cairo-1-hint-processor 0.1.0 (git+https://github.com/lambdaclass/starknet_in_rust.git?rev=2dbde61f42073b4615a63e3b2475ddb861109409)", "cairo-felt 0.3.0-rc1", "cairo-lang-starknet", "cairo-take_until_unbalanced 0.24.2-rc1", @@ -855,13 +869,13 @@ dependencies = [ [[package]] name = "cairo-vm" version = "0.3.0-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" +source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" dependencies = [ "anyhow", "bincode", "bitvec", - "cairo-felt 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", - "cairo-take_until_unbalanced 0.24.2-rc1 (git+https://github.com/lambdaclass/cairo-rs?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "cairo-felt 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", + "cairo-take_until_unbalanced 0.24.2-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", "generic-array", "hashbrown 0.13.2", "hex", diff --git a/Cargo.toml b/Cargo.toml index ef15d38248..36efadf78d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [".", "cairo-vm-cli", "felt", "hint_accountant", "./deps/parse-hyperlinks"] +members = [".", "cairo-vm-cli", "felt", "hint_accountant", "./deps/parse-hyperlinks", "cairo-1-hint-processor"] [package] name = "cairo-vm" diff --git a/cairo-1-hint-processor/Cargo.toml b/cairo-1-hint-processor/Cargo.toml new file mode 100644 index 0000000000..b43a69abfe --- /dev/null +++ b/cairo-1-hint-processor/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "cairo-1-hint-processor" +version = "0.1.0" +edition = "2021" + +[dependencies] +cairo-vm = { path = ".." } +cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo" } +ark-ff = "0.4.0-alpha.7" +ark-std = "0.3.0" +num-bigint = { version = "0.4", default-features = false } +cairo-lang-utils = "0.1.0" +num-integer = "0.1.45" +num-traits = "0.2.15" + diff --git a/cairo-1-hint-processor/src/dict_manager.rs b/cairo-1-hint-processor/src/dict_manager.rs new file mode 100644 index 0000000000..c47c8f2cd3 --- /dev/null +++ b/cairo-1-hint-processor/src/dict_manager.rs @@ -0,0 +1,125 @@ +#![allow(dead_code)] //TODO: remove after implementing all hints + +use std::collections::HashMap; + +use cairo_vm::felt::Felt252; +use cairo_vm::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; + +/// Stores the data of a specific dictionary. +pub struct DictTrackerExecScope { + /// The data of the dictionary. + data: HashMap, + /// The index of the dictionary in the dict_infos segment. + #[allow(dead_code)] + idx: usize, +} + +/// Helper object to allocate, track and destruct all dictionaries in the run. +#[derive(Default)] +pub struct DictManagerExecScope { + /// Maps between a segment index and the DictTrackerExecScope associated with it. + trackers: HashMap, +} + +impl DictTrackerExecScope { + /// Creates a new tracker placed in index `idx` in the dict_infos segment. + pub fn new(idx: usize) -> Self { + Self { + data: HashMap::default(), + idx, + } + } +} + +impl DictManagerExecScope { + pub const DICT_DEFAULT_VALUE: usize = 0; + + /// Allocates a new segment for a new dictionary and return the start of the segment. + pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Relocatable { + let dict_segment = vm.add_memory_segment(); + assert!( + self.trackers + .insert( + dict_segment.segment_index, + DictTrackerExecScope::new(self.trackers.len()) + ) + .is_none(), + "Segment index already in use." + ); + dict_segment + } + + /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. + fn get_dict_tracker(&self, dict_end: Relocatable) -> &DictTrackerExecScope { + self.trackers + .get(&dict_end.segment_index) + .expect("The given value does not point to a known dictionary.") + } + + /// Returns a mut reference for a dict tracker corresponding to a given pointer to a dict + /// segment. + fn get_dict_tracker_mut(&mut self, dict_end: Relocatable) -> &mut DictTrackerExecScope { + self.trackers + .get_mut(&dict_end.segment_index) + .expect("The given value does not point to a known dictionary.") + } + + /// Returns the index of the dict tracker corresponding to a given pointer to a dict segment. + pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> usize { + self.get_dict_tracker(dict_end).idx + } + + /// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment. + pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: Felt252) { + self.get_dict_tracker_mut(dict_end).data.insert(key, value); + } + + /// Gets a value from the dict tracker corresponding to a given pointer to a dict segment. + /// None if the key does not exist in the tracker data. + pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option { + self.get_dict_tracker(dict_end).data.get(key).cloned() + } +} + +/// Helper object for the management of dict_squash hints. +#[derive(Default, Debug)] +pub struct DictSquashExecScope { + /// A map from key to the list of indices accessing it, each list in reverse order. + pub access_indices: HashMap>, + /// Descending list of keys. + pub keys: Vec, +} + +impl DictSquashExecScope { + /// Returns the current key to process. + pub fn current_key(&self) -> Option { + self.keys.last().cloned() + } + + /// Returns and removes the current key, and its access indices. Should be called when only the + /// last key access is in the corresponding indices list. + pub fn pop_current_key(&mut self) -> Option { + let key_accesses = self.access_indices.remove(&self.current_key().unwrap()); + assert!( + key_accesses.unwrap().len() == 1, + "Key popped but not all accesses were processed." + ); + self.keys.pop() + } + + /// Returns a reference to the access indices list of the current key. + pub fn current_access_indices(&mut self) -> Option<&mut Vec> { + let current_key = self.current_key()?; + self.access_indices.get_mut(¤t_key) + } + + /// Returns a reference to the last index in the current access indices list. + pub fn current_access_index(&mut self) -> Option<&Felt252> { + self.current_access_indices()?.last() + } + + /// Returns and removes the current access index. + pub fn pop_current_access_index(&mut self) -> Option { + self.current_access_indices()?.pop() + } +} diff --git a/cairo-1-hint-processor/src/hint_processor.rs b/cairo-1-hint-processor/src/hint_processor.rs new file mode 100644 index 0000000000..6c89666e9e --- /dev/null +++ b/cairo-1-hint-processor/src/hint_processor.rs @@ -0,0 +1,835 @@ +use super::dict_manager::DictManagerExecScope; +use crate::dict_manager::DictSquashExecScope; +use ark_ff::fields::{Fp256, MontBackend, MontConfig}; +use ark_ff::{Field, PrimeField}; +use ark_std::UniformRand; +use cairo_lang_casm::{ + hints::{CoreHint, Hint}, + operand::{BinOpOperand, CellRef, DerefOrImmediate, Operation, Register, ResOperand}, +}; +use cairo_lang_utils::extract_matches; +use cairo_vm::any_box; +use cairo_vm::felt::{felt_str, Felt252}; +use cairo_vm::{ + hint_processor::hint_processor_definition::HintProcessor, + types::exec_scope::ExecutionScopes, + types::relocatable::MaybeRelocatable, + types::relocatable::Relocatable, + vm::errors::vm_errors::VirtualMachineError, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, +}; +use num_bigint::BigUint; +use num_integer::Integer; +use num_traits::cast::ToPrimitive; +use num_traits::identities::Zero; +use std::{collections::HashMap, ops::Mul}; + +#[derive(MontConfig)] +#[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"] +#[generator = "3"] + +/// Returns the Beta value of the Starkware elliptic curve. +struct FqConfig; +type Fq = Fp256>; + +fn get_beta() -> Felt252 { + felt_str!("3141592653589793238462643383279502884197169399375105820974944592307816406665") +} + +/// HintProcessor for Cairo 1 compiler hints. +struct Cairo1HintProcessor { + hints: HashMap>, +} + +/// Extracts a parameter assumed to be a buffer. +fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { + let (cell, base_offset) = match buffer { + ResOperand::Deref(cell) => (cell, 0.into()), + ResOperand::BinOp(BinOpOperand { + op: Operation::Add, + a, + b, + }) => ( + a, + extract_matches!(b, DerefOrImmediate::Immediate) + .clone() + .value + .into(), + ), + _ => { + return Err(HintError::CustomHint( + "Illegal argument for a buffer.".to_string(), + )) + } + }; + Ok((cell, base_offset)) +} + +fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable { + let base = match cell_ref.register { + Register::AP => vm.get_ap(), + Register::FP => vm.get_fp(), + }; + (base + (cell_ref.offset as i32)).unwrap() +} + +fn get_cell_val(vm: &VirtualMachine, cell: &CellRef) -> Result { + Ok(vm + .get_integer(cell_ref_to_relocatable(cell, vm))? + .as_ref() + .clone()) +} + +fn get_ptr( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm))? + offset)?) +} + +fn as_relocatable(vm: &mut VirtualMachine, value: &ResOperand) -> Result { + let (base, offset) = extract_buffer(value)?; + get_ptr(vm, base, &offset).map_err(HintError::from) +} + +fn get_double_deref_val( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok(vm.get_integer(get_ptr(vm, cell, offset)?)?.as_ref().clone()) +} + +/// Fetches the value of `res_operand` from the vm. +fn res_operand_get_val( + vm: &VirtualMachine, + res_operand: &ResOperand, +) -> Result { + match res_operand { + ResOperand::Deref(cell) => get_cell_val(vm, cell), + ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), + ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), + ResOperand::BinOp(op) => { + let a = get_cell_val(vm, &op.a)?; + let b = match &op.b { + DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, + DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()), + }; + match op.op { + Operation::Add => Ok(a + b), + Operation::Mul => Ok(a * b), + } + } + } +} + +fn as_cairo_short_string(value: &Felt252) -> Option { + let mut as_string = String::default(); + let mut is_end = false; + for byte in value.to_bytes_be() { + if byte == 0 { + is_end = true; + } else if is_end || !byte.is_ascii() { + return None; + } else { + as_string.push(byte as char); + } + } + Some(as_string) +} + +impl Cairo1HintProcessor { + pub fn new(hints: Vec<(usize, Vec)>) -> Self { + Self { + hints: hints.iter().cloned().collect(), + } + } + // Runs a single Hint + pub fn execute( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint: &Hint, + ) -> Result<(), HintError> { + match hint { + Hint::Core(CoreHint::AllocSegment { dst }) => self.alloc_segment(vm, dst), + Hint::Core(CoreHint::TestLessThan { lhs, rhs, dst }) => { + self.test_less_than(vm, lhs, rhs, dst) + } + Hint::Core(CoreHint::TestLessThanOrEqual { lhs, rhs, dst }) => { + self.test_less_than_or_equal(vm, lhs, rhs, dst) + } + Hint::Core(CoreHint::Felt252DictRead { + dict_ptr, + key, + value_dst, + }) => self.dict_read(vm, exec_scopes, dict_ptr, key, value_dst), + Hint::Core(CoreHint::SquareRoot { value, dst }) => self.square_root(vm, value, dst), + Hint::Core(CoreHint::GetSegmentArenaIndex { + dict_end_ptr, + dict_index, + }) => self.get_segment_arena_index(vm, exec_scopes, dict_end_ptr, dict_index), + + Hint::Core(CoreHint::DivMod { + lhs, + rhs, + quotient, + remainder, + }) => self.div_mod(vm, lhs, rhs, quotient, remainder), + Hint::Core(CoreHint::DebugPrint { start, end }) => self.debug_print(vm, start, end), + + Hint::Core(CoreHint::Uint256SquareRoot { + value_low, + value_high, + sqrt0, + sqrt1, + remainder_low, + remainder_high, + sqrt_mul_2_minus_remainder_ge_u128, + }) => self.uint256_square_root( + vm, + value_low, + value_high, + sqrt0, + sqrt1, + remainder_low, + remainder_high, + sqrt_mul_2_minus_remainder_ge_u128, + ), + + Hint::Core(CoreHint::GetNextDictKey { next_key }) => { + self.get_next_dict_key(vm, exec_scopes, next_key) + } + + Hint::Core(CoreHint::Uint256DivMod { + dividend_low, + dividend_high, + divisor_low, + divisor_high, + quotient0, + quotient1, + divisor0, + divisor1, + extra0, + extra1, + remainder_low, + remainder_high, + }) => self.uint256_div_mod( + vm, + dividend_low, + dividend_high, + divisor_low, + divisor_high, + quotient0, + quotient1, + divisor0, + divisor1, + extra0, + extra1, + remainder_low, + remainder_high, + ), + Hint::Core(CoreHint::Felt252DictWrite { + dict_ptr, + key, + value, + }) => self.dict_write(exec_scopes, vm, dict_ptr, key, value), + Hint::Core(CoreHint::AssertLeIsFirstArcExcluded { + skip_exclude_a_flag, + }) => self.assert_le_if_first_arc_exclueded(vm, skip_exclude_a_flag, exec_scopes), + + Hint::Core(CoreHint::AssertAllAccessesUsed { n_used_accesses }) => { + self.assert_all_accesses_used(vm, exec_scopes, n_used_accesses) + } + + Hint::Core(CoreHint::AssertLeIsSecondArcExcluded { + skip_exclude_b_minus_a, + }) => self.assert_le_is_second_excluded(vm, skip_exclude_b_minus_a, exec_scopes), + + Hint::Core(CoreHint::LinearSplit { + value, + scalar, + max_x, + x, + y, + }) => self.linear_split(vm, value, scalar, max_x, x, y), + + Hint::Core(CoreHint::AllocFelt252Dict { segment_arena_ptr }) => { + self.alloc_felt_256_dict(vm, segment_arena_ptr, exec_scopes) + } + + Hint::Core(CoreHint::AssertLeFindSmallArcs { + range_check_ptr, + a, + b, + }) => self.assert_le_find_small_arcs(vm, exec_scopes, range_check_ptr, a, b), + + Hint::Core(CoreHint::RandomEcPoint { x, y }) => self.random_ec_point(vm, x, y), + + Hint::Core(CoreHint::ShouldSkipSquashLoop { should_skip_loop }) => { + self.should_skip_squash_loop(vm, exec_scopes, should_skip_loop) + } + _ => todo!(), + } + } + + fn alloc_segment(&mut self, vm: &mut VirtualMachine, dst: &CellRef) -> Result<(), HintError> { + let segment = vm.add_memory_segment(); + vm.insert_value(cell_ref_to_relocatable(dst, vm), segment) + .map_err(HintError::from) + } + + fn test_less_than( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?; + let rhs_value = res_operand_get_val(vm, rhs)?; + let result = if lhs_value < rhs_value { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn square_root( + &self, + vm: &mut VirtualMachine, + value: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let value = res_operand_get_val(vm, value)?; + let result = value.sqrt(); + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn test_less_than_or_equal( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?; + let rhs_value = res_operand_get_val(vm, rhs)?; + let result = if lhs_value <= rhs_value { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn assert_le_find_small_arcs( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + range_check_ptr: &ResOperand, + a: &ResOperand, + b: &ResOperand, + ) -> Result<(), HintError> { + let a_val = res_operand_get_val(vm, a)?; + let b_val = res_operand_get_val(vm, b)?; + let mut lengths_and_indices = vec![ + (a_val.clone(), 0), + (b_val.clone() - a_val, 1), + (Felt252::from(-1) - b_val, 2), + ]; + lengths_and_indices.sort(); + exec_scopes.assign_or_update_variable("excluded_arc", Box::new(lengths_and_indices[2].1)); + // ceil((PRIME / 3) / 2 ** 128). + let prime_over_3_high = 3544607988759775765608368578435044694_u128; + // ceil((PRIME / 2) / 2 ** 128). + let prime_over_2_high = 5316911983139663648412552867652567041_u128; + let (range_check_base, range_check_offset) = extract_buffer(range_check_ptr)?; + let range_check_ptr = get_ptr(vm, range_check_base, &range_check_offset)?; + vm.insert_value( + range_check_ptr, + Felt252::from(lengths_and_indices[0].0.to_biguint() % prime_over_3_high), + )?; + vm.insert_value( + (range_check_ptr + 1)?, + Felt252::from(lengths_and_indices[0].0.to_biguint() / prime_over_3_high), + )?; + vm.insert_value( + (range_check_ptr + 2)?, + Felt252::from(lengths_and_indices[1].0.to_biguint() % prime_over_2_high), + )?; + vm.insert_value( + (range_check_ptr + 3)?, + Felt252::from(lengths_and_indices[1].0.to_biguint() / prime_over_2_high), + ) + .map_err(HintError::from) + } + + fn dict_read( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + dict_ptr: &ResOperand, + key: &ResOperand, + value_dst: &CellRef, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let key = res_operand_get_val(vm, key)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let value = dict_manager_exec_scope + .get_from_tracker(dict_address, &key) + .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); + + vm.insert_value(cell_ref_to_relocatable(value_dst, vm), value) + .map_err(HintError::from) + } + + fn div_mod( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + quotient: &CellRef, + remainder: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?.to_biguint(); + let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); + let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); + let remainder_value = Felt252::new(lhs_value % rhs_value); + vm.insert_value( + cell_ref_to_relocatable(quotient, vm), + MaybeRelocatable::from(quotient_value), + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder, vm), + MaybeRelocatable::from(remainder_value), + ) + .map_err(HintError::from) + } + + fn get_segment_arena_index( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + dict_end_ptr: &ResOperand, + dict_index: &CellRef, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_end_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); + vm.insert_value( + cell_ref_to_relocatable(dict_index, vm), + Felt252::from(dict_infos_index), + ) + .map_err(HintError::from) + } + + #[allow(clippy::too_many_arguments)] + fn uint256_div_mod( + &self, + vm: &mut VirtualMachine, + dividend_low: &ResOperand, + dividend_high: &ResOperand, + divisor_low: &ResOperand, + divisor_high: &ResOperand, + quotient0: &CellRef, + quotient1: &CellRef, + divisor0: &CellRef, + divisor1: &CellRef, + extra0: &CellRef, + extra1: &CellRef, + remainder_low: &CellRef, + remainder_high: &CellRef, + ) -> Result<(), HintError> { + let pow_2_128 = Felt252::from(u128::MAX) + 1u32; + let pow_2_64 = Felt252::from(u64::MAX) + 1u32; + let dividend_low = res_operand_get_val(vm, dividend_low)?; + let dividend_high = res_operand_get_val(vm, dividend_high)?; + let divisor_low = res_operand_get_val(vm, divisor_low)?; + let divisor_high = res_operand_get_val(vm, divisor_high)?; + let dividend = dividend_low + dividend_high.mul(pow_2_128.clone()); + let divisor = divisor_low + divisor_high.clone() * pow_2_128.clone(); + let quotient = dividend.clone() / divisor.clone(); + let remainder = dividend % divisor.clone(); + + // Guess quotient limbs. + let (quotient, limb) = quotient.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(quotient0, vm), limb)?; + let (quotient, limb) = quotient.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(quotient1, vm), limb)?; + let (quotient, limb) = quotient.div_rem(&pow_2_64); + if divisor_high.is_zero() { + vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm), quotient)?; + } + + // Guess divisor limbs. + let (divisor, limb) = divisor.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(divisor0, vm), limb)?; + let (divisor, limb) = divisor.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(divisor1, vm), limb)?; + let (divisor, limb) = divisor.div_rem(&pow_2_64); + if !divisor_high.is_zero() { + vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm), divisor)?; + } + + // Guess remainder limbs. + vm.insert_value( + cell_ref_to_relocatable(remainder_low, vm), + remainder.clone() % pow_2_128.clone(), + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder_high, vm), + remainder / pow_2_128, + )?; + Ok(()) + } + + fn assert_le_if_first_arc_exclueded( + &self, + vm: &mut VirtualMachine, + skip_exclude_a_flag: &CellRef, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; + let val = if excluded_arc != 0 { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm), val)?; + Ok(()) + } + + fn linear_split( + &self, + vm: &mut VirtualMachine, + value: &ResOperand, + scalar: &ResOperand, + max_x: &ResOperand, + x: &CellRef, + y: &CellRef, + ) -> Result<(), HintError> { + let value = res_operand_get_val(vm, value)?; + let scalar = res_operand_get_val(vm, scalar)?; + let max_x = res_operand_get_val(vm, max_x)?; + let x_value = (value.clone() / scalar.clone()).min(max_x); + let y_value = value - x_value.clone() * scalar; + + vm.insert_value(cell_ref_to_relocatable(x, vm), x_value) + .map_err(HintError::from)?; + vm.insert_value(cell_ref_to_relocatable(y, vm), y_value) + .map_err(HintError::from)?; + + Ok(()) + } + + fn random_ec_point( + &self, + vm: &mut VirtualMachine, + x: &CellRef, + y: &CellRef, + ) -> Result<(), HintError> { + let beta = Fq::from(get_beta().to_biguint()); + + let mut rng = ark_std::test_rng(); + let (random_x, random_y_squared) = loop { + let random_x = Fq::rand(&mut rng); + let random_y_squared = random_x * random_x * random_x + random_x + beta; + if random_y_squared.legendre().is_qr() { + break (random_x, random_y_squared); + } + }; + + let x_bigint: BigUint = random_x.into_bigint().into(); + let y_bigint: BigUint = random_y_squared + .sqrt() + .ok_or(HintError::CustomHint("Failed to compute sqrt".to_string()))? + .into_bigint() + .into(); + + vm.insert_value(cell_ref_to_relocatable(x, vm), Felt252::from(x_bigint))?; + vm.insert_value(cell_ref_to_relocatable(y, vm), Felt252::from(y_bigint))?; + + Ok(()) + } + + fn get_next_dict_key( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + next_key: &CellRef, + ) -> Result<(), HintError> { + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + dict_squash_exec_scope.pop_current_key(); + if let Some(current_key) = dict_squash_exec_scope.current_key() { + return vm + .insert_value(cell_ref_to_relocatable(next_key, vm), current_key) + .map_err(HintError::from); + } + Err(HintError::KeyNotFound) + } + + fn alloc_felt_256_dict( + &self, + vm: &mut VirtualMachine, + segment_arena_ptr: &ResOperand, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let (cell, base_offset) = extract_buffer(segment_arena_ptr)?; + let dict_manager_address = get_ptr(vm, cell, &base_offset)?; + + let n_dicts = vm + .get_integer((dict_manager_address - 2)?)? + .into_owned() + .to_usize() + .ok_or(HintError::CustomHint( + "Invalid number of dictionaries.".to_string(), + ))?; + + let dict_infos_base = vm.get_relocatable((dict_manager_address - 3)?)?; + + let dict_manager_exec_scope = + match exec_scopes.get_mut_ref::("dict_manager_exec_scope") { + Ok(dict_manager_exec_scope) => dict_manager_exec_scope, + Err(_) => { + exec_scopes.assign_or_update_variable( + "dict_manager_exec_scope", + Box::::default(), + ); + exec_scopes.get_mut_ref::("dict_manager_exec_scope")? + } + }; + let new_dict_segment = dict_manager_exec_scope.new_default_dict(vm); + vm.insert_value((dict_infos_base + 3 * n_dicts)?, new_dict_segment)?; + + Ok(()) + } + + fn assert_le_is_second_excluded( + &self, + vm: &mut VirtualMachine, + skip_exclude_b_minus_a: &CellRef, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; + let val = if excluded_arc != 1 { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm), val)?; + + Ok(()) + } + + fn dict_write( + &self, + exec_scopes: &mut ExecutionScopes, + vm: &mut VirtualMachine, + dict_ptr: &ResOperand, + key: &ResOperand, + value: &ResOperand, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let key = res_operand_get_val(vm, key)?; + let value = res_operand_get_val(vm, value)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let prev_value = dict_manager_exec_scope + .get_from_tracker(dict_address, &key) + .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); + + vm.insert_value((dict_address + 1)?, prev_value)?; + dict_manager_exec_scope.insert_to_tracker(dict_address, key, value); + Ok(()) + } + + #[allow(clippy::too_many_arguments)] + fn uint256_square_root( + &self, + vm: &mut VirtualMachine, + value_low: &ResOperand, + value_high: &ResOperand, + sqrt0: &CellRef, + sqrt1: &CellRef, + remainder_low: &CellRef, + remainder_high: &CellRef, + sqrt_mul_2_minus_remainder_ge_u128: &CellRef, + ) -> Result<(), HintError> { + let pow_2_128 = Felt252::from(u128::MAX) + 1u32; + let pow_2_64 = Felt252::from(u64::MAX) + 1u32; + let value_low = res_operand_get_val(vm, value_low)?; + let value_high = res_operand_get_val(vm, value_high)?; + let value = value_low + value_high * pow_2_128.clone(); + let sqrt = value.sqrt(); + let remainder = value - sqrt.clone() * sqrt.clone(); + let sqrt_mul_2_minus_remainder_ge_u128_val = + sqrt.clone() * Felt252::from(2u32) - remainder.clone() >= pow_2_128; + + let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(sqrt0, vm), sqrt0_val)?; + vm.insert_value(cell_ref_to_relocatable(sqrt1, vm), sqrt1_val)?; + + let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); + + vm.insert_value( + cell_ref_to_relocatable(remainder_low, vm), + remainder_low_val, + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder_high, vm), + remainder_high_val, + )?; + vm.insert_value( + cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm), + usize::from(sqrt_mul_2_minus_remainder_ge_u128_val), + )?; + + Ok(()) + } + + fn debug_print( + &self, + vm: &mut VirtualMachine, + start: &ResOperand, + end: &ResOperand, + ) -> Result<(), HintError> { + let mut curr = as_relocatable(vm, start)?; + let end = as_relocatable(vm, end)?; + while curr != end { + let value = vm.get_integer(curr)?; + if let Some(shortstring) = as_cairo_short_string(&value) { + println!("[DEBUG]\t{shortstring: <31}\t(raw: {value: <31})"); + } else { + println!("[DEBUG]\t{0: <31}\t(raw: {value: <31}) ", ' '); + } + curr += 1; + } + println!(); + Ok(()) + } + + fn assert_all_accesses_used( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + n_used_accesses: &CellRef, + ) -> Result<(), HintError> { + let key = exec_scopes.get::("key")?; + let n = get_cell_val(vm, n_used_accesses)?; + + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + + let access_indices_at_key = dict_squash_exec_scope + .access_indices + .get(&key.clone()) + .ok_or_else(|| HintError::NoKeyInAccessIndices(key.clone()))?; + + if n != Felt252::new(access_indices_at_key.len()) { + return Err(HintError::NumUsedAccessesAssertFail( + n, + access_indices_at_key.len(), + key, + )); + } + + Ok(()) + } + + fn should_skip_squash_loop( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + should_skip_loop: &CellRef, + ) -> Result<(), HintError> { + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + + let val = if dict_squash_exec_scope + .current_access_indices() + .ok_or(HintError::CustomHint("no indices accessed".to_string()))? + .len() + > 1 + { + Felt252::from(0) + } else { + Felt252::from(1) + }; + + vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm), val)?; + + Ok(()) + } +} + +impl HintProcessor for Cairo1HintProcessor { + // Ignores all data except for the code that should contain + fn compile_hint( + &self, + //Block of hint code as String + hint_code: &str, + //Ap Tracking Data corresponding to the Hint + _ap_tracking_data: &cairo_vm::serde::deserialize_program::ApTracking, + //Map from variable name to reference id number + //(may contain other variables aside from those used by the hint) + _reference_ids: &HashMap, + //List of all references (key corresponds to element of the previous dictionary) + _references: &HashMap< + usize, + cairo_vm::hint_processor::hint_processor_definition::HintReference, + >, + ) -> Result, VirtualMachineError> { + let data = hint_code.parse().ok().and_then(|x| self.hints.get(&x)).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; + Ok(any_box!(data)) + } + + fn execute_hint( + &mut self, + //Proxy to VM, contains refrences to necessary data + //+ MemoryProxy, which provides the necessary methods to manipulate memory + vm: &mut VirtualMachine, + //Proxy to ExecutionScopes, provides the necessary methods to manipulate the scopes and + //access current scope variables + exec_scopes: &mut ExecutionScopes, + //Data structure that can be downcasted to the structure generated by compile_hint + hint_data: &Box, + //Constant values extracted from the program specification. + _constants: &HashMap, + ) -> Result<(), HintError> { + let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; + for hint in hints { + self.execute(vm, exec_scopes, hint)?; + } + Ok(()) + } +} diff --git a/cairo-1-hint-processor/src/lib.rs b/cairo-1-hint-processor/src/lib.rs new file mode 100644 index 0000000000..e295b1a59a --- /dev/null +++ b/cairo-1-hint-processor/src/lib.rs @@ -0,0 +1,2 @@ +pub mod dict_manager; +pub mod hint_processor; diff --git a/src/types/program.rs b/src/types/program.rs index dad8e8af70..55e7f27396 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -147,7 +147,7 @@ impl Default for Program { } } -// Note: This Program will only work when using run_from_entrypoint +// Note: This Program will only work when using run_from_entrypoint, and the Cairo1Hintprocesso impl TryFrom for Program { type Error = ProgramError; fn try_from(value: CasmContractClass) -> Result { From 31833c0e0451fbecf8690c2a68ceee1b96dd771e Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 16:21:51 -0300 Subject: [PATCH 05/84] Move cairo-1-hint-processor to cairo-rs crate --- Cargo.lock | 161 +--- Cargo.toml | 8 +- cairo-1-hint-processor/Cargo.toml | 15 - cairo-1-hint-processor/src/dict_manager.rs | 125 --- cairo-1-hint-processor/src/hint_processor.rs | 835 ------------------- cairo-1-hint-processor/src/lib.rs | 2 - src/hint_processor/mod.rs | 1 + src/tests/mod.rs | 34 + src/types/program.rs | 2 +- 9 files changed, 68 insertions(+), 1115 deletions(-) delete mode 100644 cairo-1-hint-processor/Cargo.toml delete mode 100644 cairo-1-hint-processor/src/dict_manager.rs delete mode 100644 cairo-1-hint-processor/src/hint_processor.rs delete mode 100644 cairo-1-hint-processor/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 76be079dc2..407bd4d6d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -324,35 +324,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "cairo-1-hint-processor" -version = "0.1.0" -dependencies = [ - "ark-ff 0.4.2", - "ark-std 0.3.0", - "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", - "cairo-lang-utils 0.1.0", - "cairo-vm 0.3.0-rc1", - "num-bigint", - "num-integer", - "num-traits 0.2.15", -] - -[[package]] -name = "cairo-1-hint-processor" -version = "0.1.0" -source = "git+https://github.com/lambdaclass/starknet_in_rust.git?rev=2dbde61f42073b4615a63e3b2475ddb861109409#2dbde61f42073b4615a63e3b2475ddb861109409" -dependencies = [ - "ark-ff 0.4.2", - "ark-std 0.3.0", - "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", - "cairo-lang-utils 0.1.0", - "cairo-vm 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", - "num-bigint", - "num-integer", - "num-traits 0.2.15", -] - [[package]] name = "cairo-felt" version = "0.3.0-rc1" @@ -378,37 +349,12 @@ dependencies = [ "serde", ] -[[package]] -name = "cairo-felt" -version = "0.3.0-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" -dependencies = [ - "lazy_static", - "num-bigint", - "num-integer", - "num-traits 0.2.15", - "serde", -] - [[package]] name = "cairo-lang-casm" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", - "indoc", - "num-bigint", - "num-traits 0.2.15", - "serde", - "thiserror", -] - -[[package]] -name = "cairo-lang-casm" -version = "1.0.0-alpha.7" -source = "git+https://github.com/starkware-libs/cairo#a08bedbf26b711e81cec8b7660698a3788e67d46" -dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo)", + "cairo-lang-utils 1.0.0-alpha.7", "indoc", "num-bigint", "num-traits 0.2.15", @@ -433,7 +379,7 @@ dependencies = [ "cairo-lang-sierra", "cairo-lang-sierra-generator", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "clap 4.2.5", "log", "salsa", @@ -456,7 +402,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-parser", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "indexmap", "itertools", "salsa", @@ -469,7 +415,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-filesystem", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "itertools", "salsa", ] @@ -479,7 +425,7 @@ name = "cairo-lang-eq-solver" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "good_lp", "indexmap", "itertools", @@ -491,7 +437,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-debug", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "path-clean", "salsa", "serde", @@ -511,7 +457,7 @@ dependencies = [ "cairo-lang-proc-macros", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "id-arena", "indexmap", "itertools", @@ -531,7 +477,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-syntax", "cairo-lang-syntax-codegen", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "colored", "itertools", "log", @@ -553,7 +499,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "indoc", "itertools", "salsa", @@ -594,7 +540,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-proc-macros", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "id-arena", "itertools", "log", @@ -609,7 +555,7 @@ name = "cairo-lang-sierra" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "const-fnv1a-hash", "convert_case", "derivative", @@ -633,7 +579,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "itertools", "thiserror", ] @@ -645,7 +591,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "itertools", "thiserror", ] @@ -666,7 +612,7 @@ dependencies = [ "cairo-lang-semantic", "cairo-lang-sierra", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "id-arena", "indexmap", "itertools", @@ -683,11 +629,11 @@ dependencies = [ "anyhow", "assert_matches", "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", - "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-casm", "cairo-lang-sierra", "cairo-lang-sierra-ap-change", "cairo-lang-sierra-gas", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "clap 4.2.5", "indoc", "itertools", @@ -704,7 +650,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "anyhow", "cairo-felt 0.3.0-rc1 (registry+https://github.com/rust-lang/crates.io-index)", - "cairo-lang-casm 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-casm", "cairo-lang-compiler", "cairo-lang-defs", "cairo-lang-diagnostics", @@ -719,7 +665,7 @@ dependencies = [ "cairo-lang-sierra-generator", "cairo-lang-sierra-to-casm", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "clap 4.2.5", "convert_case", "genco", @@ -744,7 +690,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "num-bigint", "num-traits 0.2.15", "salsa", @@ -758,7 +704,7 @@ name = "cairo-lang-syntax-codegen" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7 (git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46)", + "cairo-lang-utils 1.0.0-alpha.7", "genco", "log", "xshell", @@ -793,22 +739,6 @@ dependencies = [ "time 0.3.20", ] -[[package]] -name = "cairo-lang-utils" -version = "1.0.0-alpha.7" -source = "git+https://github.com/starkware-libs/cairo#a08bedbf26b711e81cec8b7660698a3788e67d46" -dependencies = [ - "env_logger", - "indexmap", - "itertools", - "log", - "num-bigint", - "num-integer", - "num-traits 0.2.15", - "serde", - "time 0.3.20", -] - [[package]] name = "cairo-take_until_unbalanced" version = "0.24.2-rc1" @@ -817,26 +747,20 @@ dependencies = [ "wasm-bindgen-test", ] -[[package]] -name = "cairo-take_until_unbalanced" -version = "0.24.2-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" -dependencies = [ - "nom", -] - [[package]] name = "cairo-vm" version = "0.3.0-rc1" dependencies = [ "anyhow", + "ark-ff 0.4.2", + "ark-std 0.3.0", "assert_matches", "bincode", "bitvec", - "cairo-1-hint-processor 0.1.0 (git+https://github.com/lambdaclass/starknet_in_rust.git?rev=2dbde61f42073b4615a63e3b2475ddb861109409)", "cairo-felt 0.3.0-rc1", "cairo-lang-starknet", - "cairo-take_until_unbalanced 0.24.2-rc1", + "cairo-lang-utils 0.1.0", + "cairo-take_until_unbalanced", "criterion", "generic-array", "hashbrown 0.13.2", @@ -866,46 +790,13 @@ dependencies = [ "wasm-bindgen-test", ] -[[package]] -name = "cairo-vm" -version = "0.3.0-rc1" -source = "git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad#327ee7d6778bdab9573573e6af008fcc6041f9ad" -dependencies = [ - "anyhow", - "bincode", - "bitvec", - "cairo-felt 0.3.0-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", - "cairo-take_until_unbalanced 0.24.2-rc1 (git+https://github.com/lambdaclass/cairo-rs/?rev=327ee7d6778bdab9573573e6af008fcc6041f9ad)", - "generic-array", - "hashbrown 0.13.2", - "hex", - "keccak", - "lazy_static", - "mimalloc", - "nom", - "num-bigint", - "num-integer", - "num-prime", - "num-traits 0.2.15", - "rand", - "rand_core", - "serde", - "serde_bytes", - "serde_json", - "sha2", - "sha3", - "starknet-crypto", - "thiserror", - "thiserror-no-std", -] - [[package]] name = "cairo-vm-cli" version = "0.3.0-rc1" dependencies = [ "assert_matches", "bincode", - "cairo-vm 0.3.0-rc1", + "cairo-vm", "clap 3.2.23", "mimalloc", "nom", @@ -1668,7 +1559,7 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" name = "hint_accountant" version = "0.3.0-rc1" dependencies = [ - "cairo-vm 0.3.0-rc1", + "cairo-vm", "serde", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 36efadf78d..67e1fdba7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [".", "cairo-vm-cli", "felt", "hint_accountant", "./deps/parse-hyperlinks", "cairo-1-hint-processor"] +members = [".", "cairo-vm-cli", "felt", "hint_accountant", "./deps/parse-hyperlinks"] [package] name = "cairo-vm" @@ -80,7 +80,11 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default ] } bitvec = { version = "1", default-features = false, features = ["alloc"] } cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46"} -cairo-1-hint-processor = {git = "https://github.com/lambdaclass/starknet_in_rust.git", rev = "2dbde61f42073b4615a63e3b2475ddb861109409"} + +# TODO: check these dependencies for wasm compatibility +cairo-lang-utils = "0.1.0" +ark-ff = "0.4.0-alpha.7" +ark-std = "0.3.0" [dev-dependencies] assert_matches = "1.5.0" diff --git a/cairo-1-hint-processor/Cargo.toml b/cairo-1-hint-processor/Cargo.toml deleted file mode 100644 index b43a69abfe..0000000000 --- a/cairo-1-hint-processor/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "cairo-1-hint-processor" -version = "0.1.0" -edition = "2021" - -[dependencies] -cairo-vm = { path = ".." } -cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo" } -ark-ff = "0.4.0-alpha.7" -ark-std = "0.3.0" -num-bigint = { version = "0.4", default-features = false } -cairo-lang-utils = "0.1.0" -num-integer = "0.1.45" -num-traits = "0.2.15" - diff --git a/cairo-1-hint-processor/src/dict_manager.rs b/cairo-1-hint-processor/src/dict_manager.rs deleted file mode 100644 index c47c8f2cd3..0000000000 --- a/cairo-1-hint-processor/src/dict_manager.rs +++ /dev/null @@ -1,125 +0,0 @@ -#![allow(dead_code)] //TODO: remove after implementing all hints - -use std::collections::HashMap; - -use cairo_vm::felt::Felt252; -use cairo_vm::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; - -/// Stores the data of a specific dictionary. -pub struct DictTrackerExecScope { - /// The data of the dictionary. - data: HashMap, - /// The index of the dictionary in the dict_infos segment. - #[allow(dead_code)] - idx: usize, -} - -/// Helper object to allocate, track and destruct all dictionaries in the run. -#[derive(Default)] -pub struct DictManagerExecScope { - /// Maps between a segment index and the DictTrackerExecScope associated with it. - trackers: HashMap, -} - -impl DictTrackerExecScope { - /// Creates a new tracker placed in index `idx` in the dict_infos segment. - pub fn new(idx: usize) -> Self { - Self { - data: HashMap::default(), - idx, - } - } -} - -impl DictManagerExecScope { - pub const DICT_DEFAULT_VALUE: usize = 0; - - /// Allocates a new segment for a new dictionary and return the start of the segment. - pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Relocatable { - let dict_segment = vm.add_memory_segment(); - assert!( - self.trackers - .insert( - dict_segment.segment_index, - DictTrackerExecScope::new(self.trackers.len()) - ) - .is_none(), - "Segment index already in use." - ); - dict_segment - } - - /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. - fn get_dict_tracker(&self, dict_end: Relocatable) -> &DictTrackerExecScope { - self.trackers - .get(&dict_end.segment_index) - .expect("The given value does not point to a known dictionary.") - } - - /// Returns a mut reference for a dict tracker corresponding to a given pointer to a dict - /// segment. - fn get_dict_tracker_mut(&mut self, dict_end: Relocatable) -> &mut DictTrackerExecScope { - self.trackers - .get_mut(&dict_end.segment_index) - .expect("The given value does not point to a known dictionary.") - } - - /// Returns the index of the dict tracker corresponding to a given pointer to a dict segment. - pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> usize { - self.get_dict_tracker(dict_end).idx - } - - /// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment. - pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: Felt252) { - self.get_dict_tracker_mut(dict_end).data.insert(key, value); - } - - /// Gets a value from the dict tracker corresponding to a given pointer to a dict segment. - /// None if the key does not exist in the tracker data. - pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option { - self.get_dict_tracker(dict_end).data.get(key).cloned() - } -} - -/// Helper object for the management of dict_squash hints. -#[derive(Default, Debug)] -pub struct DictSquashExecScope { - /// A map from key to the list of indices accessing it, each list in reverse order. - pub access_indices: HashMap>, - /// Descending list of keys. - pub keys: Vec, -} - -impl DictSquashExecScope { - /// Returns the current key to process. - pub fn current_key(&self) -> Option { - self.keys.last().cloned() - } - - /// Returns and removes the current key, and its access indices. Should be called when only the - /// last key access is in the corresponding indices list. - pub fn pop_current_key(&mut self) -> Option { - let key_accesses = self.access_indices.remove(&self.current_key().unwrap()); - assert!( - key_accesses.unwrap().len() == 1, - "Key popped but not all accesses were processed." - ); - self.keys.pop() - } - - /// Returns a reference to the access indices list of the current key. - pub fn current_access_indices(&mut self) -> Option<&mut Vec> { - let current_key = self.current_key()?; - self.access_indices.get_mut(¤t_key) - } - - /// Returns a reference to the last index in the current access indices list. - pub fn current_access_index(&mut self) -> Option<&Felt252> { - self.current_access_indices()?.last() - } - - /// Returns and removes the current access index. - pub fn pop_current_access_index(&mut self) -> Option { - self.current_access_indices()?.pop() - } -} diff --git a/cairo-1-hint-processor/src/hint_processor.rs b/cairo-1-hint-processor/src/hint_processor.rs deleted file mode 100644 index 6c89666e9e..0000000000 --- a/cairo-1-hint-processor/src/hint_processor.rs +++ /dev/null @@ -1,835 +0,0 @@ -use super::dict_manager::DictManagerExecScope; -use crate::dict_manager::DictSquashExecScope; -use ark_ff::fields::{Fp256, MontBackend, MontConfig}; -use ark_ff::{Field, PrimeField}; -use ark_std::UniformRand; -use cairo_lang_casm::{ - hints::{CoreHint, Hint}, - operand::{BinOpOperand, CellRef, DerefOrImmediate, Operation, Register, ResOperand}, -}; -use cairo_lang_utils::extract_matches; -use cairo_vm::any_box; -use cairo_vm::felt::{felt_str, Felt252}; -use cairo_vm::{ - hint_processor::hint_processor_definition::HintProcessor, - types::exec_scope::ExecutionScopes, - types::relocatable::MaybeRelocatable, - types::relocatable::Relocatable, - vm::errors::vm_errors::VirtualMachineError, - vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, -}; -use num_bigint::BigUint; -use num_integer::Integer; -use num_traits::cast::ToPrimitive; -use num_traits::identities::Zero; -use std::{collections::HashMap, ops::Mul}; - -#[derive(MontConfig)] -#[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"] -#[generator = "3"] - -/// Returns the Beta value of the Starkware elliptic curve. -struct FqConfig; -type Fq = Fp256>; - -fn get_beta() -> Felt252 { - felt_str!("3141592653589793238462643383279502884197169399375105820974944592307816406665") -} - -/// HintProcessor for Cairo 1 compiler hints. -struct Cairo1HintProcessor { - hints: HashMap>, -} - -/// Extracts a parameter assumed to be a buffer. -fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { - let (cell, base_offset) = match buffer { - ResOperand::Deref(cell) => (cell, 0.into()), - ResOperand::BinOp(BinOpOperand { - op: Operation::Add, - a, - b, - }) => ( - a, - extract_matches!(b, DerefOrImmediate::Immediate) - .clone() - .value - .into(), - ), - _ => { - return Err(HintError::CustomHint( - "Illegal argument for a buffer.".to_string(), - )) - } - }; - Ok((cell, base_offset)) -} - -fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable { - let base = match cell_ref.register { - Register::AP => vm.get_ap(), - Register::FP => vm.get_fp(), - }; - (base + (cell_ref.offset as i32)).unwrap() -} - -fn get_cell_val(vm: &VirtualMachine, cell: &CellRef) -> Result { - Ok(vm - .get_integer(cell_ref_to_relocatable(cell, vm))? - .as_ref() - .clone()) -} - -fn get_ptr( - vm: &VirtualMachine, - cell: &CellRef, - offset: &Felt252, -) -> Result { - Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm))? + offset)?) -} - -fn as_relocatable(vm: &mut VirtualMachine, value: &ResOperand) -> Result { - let (base, offset) = extract_buffer(value)?; - get_ptr(vm, base, &offset).map_err(HintError::from) -} - -fn get_double_deref_val( - vm: &VirtualMachine, - cell: &CellRef, - offset: &Felt252, -) -> Result { - Ok(vm.get_integer(get_ptr(vm, cell, offset)?)?.as_ref().clone()) -} - -/// Fetches the value of `res_operand` from the vm. -fn res_operand_get_val( - vm: &VirtualMachine, - res_operand: &ResOperand, -) -> Result { - match res_operand { - ResOperand::Deref(cell) => get_cell_val(vm, cell), - ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), - ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), - ResOperand::BinOp(op) => { - let a = get_cell_val(vm, &op.a)?; - let b = match &op.b { - DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, - DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()), - }; - match op.op { - Operation::Add => Ok(a + b), - Operation::Mul => Ok(a * b), - } - } - } -} - -fn as_cairo_short_string(value: &Felt252) -> Option { - let mut as_string = String::default(); - let mut is_end = false; - for byte in value.to_bytes_be() { - if byte == 0 { - is_end = true; - } else if is_end || !byte.is_ascii() { - return None; - } else { - as_string.push(byte as char); - } - } - Some(as_string) -} - -impl Cairo1HintProcessor { - pub fn new(hints: Vec<(usize, Vec)>) -> Self { - Self { - hints: hints.iter().cloned().collect(), - } - } - // Runs a single Hint - pub fn execute( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - hint: &Hint, - ) -> Result<(), HintError> { - match hint { - Hint::Core(CoreHint::AllocSegment { dst }) => self.alloc_segment(vm, dst), - Hint::Core(CoreHint::TestLessThan { lhs, rhs, dst }) => { - self.test_less_than(vm, lhs, rhs, dst) - } - Hint::Core(CoreHint::TestLessThanOrEqual { lhs, rhs, dst }) => { - self.test_less_than_or_equal(vm, lhs, rhs, dst) - } - Hint::Core(CoreHint::Felt252DictRead { - dict_ptr, - key, - value_dst, - }) => self.dict_read(vm, exec_scopes, dict_ptr, key, value_dst), - Hint::Core(CoreHint::SquareRoot { value, dst }) => self.square_root(vm, value, dst), - Hint::Core(CoreHint::GetSegmentArenaIndex { - dict_end_ptr, - dict_index, - }) => self.get_segment_arena_index(vm, exec_scopes, dict_end_ptr, dict_index), - - Hint::Core(CoreHint::DivMod { - lhs, - rhs, - quotient, - remainder, - }) => self.div_mod(vm, lhs, rhs, quotient, remainder), - Hint::Core(CoreHint::DebugPrint { start, end }) => self.debug_print(vm, start, end), - - Hint::Core(CoreHint::Uint256SquareRoot { - value_low, - value_high, - sqrt0, - sqrt1, - remainder_low, - remainder_high, - sqrt_mul_2_minus_remainder_ge_u128, - }) => self.uint256_square_root( - vm, - value_low, - value_high, - sqrt0, - sqrt1, - remainder_low, - remainder_high, - sqrt_mul_2_minus_remainder_ge_u128, - ), - - Hint::Core(CoreHint::GetNextDictKey { next_key }) => { - self.get_next_dict_key(vm, exec_scopes, next_key) - } - - Hint::Core(CoreHint::Uint256DivMod { - dividend_low, - dividend_high, - divisor_low, - divisor_high, - quotient0, - quotient1, - divisor0, - divisor1, - extra0, - extra1, - remainder_low, - remainder_high, - }) => self.uint256_div_mod( - vm, - dividend_low, - dividend_high, - divisor_low, - divisor_high, - quotient0, - quotient1, - divisor0, - divisor1, - extra0, - extra1, - remainder_low, - remainder_high, - ), - Hint::Core(CoreHint::Felt252DictWrite { - dict_ptr, - key, - value, - }) => self.dict_write(exec_scopes, vm, dict_ptr, key, value), - Hint::Core(CoreHint::AssertLeIsFirstArcExcluded { - skip_exclude_a_flag, - }) => self.assert_le_if_first_arc_exclueded(vm, skip_exclude_a_flag, exec_scopes), - - Hint::Core(CoreHint::AssertAllAccessesUsed { n_used_accesses }) => { - self.assert_all_accesses_used(vm, exec_scopes, n_used_accesses) - } - - Hint::Core(CoreHint::AssertLeIsSecondArcExcluded { - skip_exclude_b_minus_a, - }) => self.assert_le_is_second_excluded(vm, skip_exclude_b_minus_a, exec_scopes), - - Hint::Core(CoreHint::LinearSplit { - value, - scalar, - max_x, - x, - y, - }) => self.linear_split(vm, value, scalar, max_x, x, y), - - Hint::Core(CoreHint::AllocFelt252Dict { segment_arena_ptr }) => { - self.alloc_felt_256_dict(vm, segment_arena_ptr, exec_scopes) - } - - Hint::Core(CoreHint::AssertLeFindSmallArcs { - range_check_ptr, - a, - b, - }) => self.assert_le_find_small_arcs(vm, exec_scopes, range_check_ptr, a, b), - - Hint::Core(CoreHint::RandomEcPoint { x, y }) => self.random_ec_point(vm, x, y), - - Hint::Core(CoreHint::ShouldSkipSquashLoop { should_skip_loop }) => { - self.should_skip_squash_loop(vm, exec_scopes, should_skip_loop) - } - _ => todo!(), - } - } - - fn alloc_segment(&mut self, vm: &mut VirtualMachine, dst: &CellRef) -> Result<(), HintError> { - let segment = vm.add_memory_segment(); - vm.insert_value(cell_ref_to_relocatable(dst, vm), segment) - .map_err(HintError::from) - } - - fn test_less_than( - &self, - vm: &mut VirtualMachine, - lhs: &ResOperand, - rhs: &ResOperand, - dst: &CellRef, - ) -> Result<(), HintError> { - let lhs_value = res_operand_get_val(vm, lhs)?; - let rhs_value = res_operand_get_val(vm, rhs)?; - let result = if lhs_value < rhs_value { - Felt252::from(1) - } else { - Felt252::from(0) - }; - - vm.insert_value( - cell_ref_to_relocatable(dst, vm), - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) - } - - fn square_root( - &self, - vm: &mut VirtualMachine, - value: &ResOperand, - dst: &CellRef, - ) -> Result<(), HintError> { - let value = res_operand_get_val(vm, value)?; - let result = value.sqrt(); - vm.insert_value( - cell_ref_to_relocatable(dst, vm), - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) - } - - fn test_less_than_or_equal( - &self, - vm: &mut VirtualMachine, - lhs: &ResOperand, - rhs: &ResOperand, - dst: &CellRef, - ) -> Result<(), HintError> { - let lhs_value = res_operand_get_val(vm, lhs)?; - let rhs_value = res_operand_get_val(vm, rhs)?; - let result = if lhs_value <= rhs_value { - Felt252::from(1) - } else { - Felt252::from(0) - }; - - vm.insert_value( - cell_ref_to_relocatable(dst, vm), - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) - } - - fn assert_le_find_small_arcs( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - range_check_ptr: &ResOperand, - a: &ResOperand, - b: &ResOperand, - ) -> Result<(), HintError> { - let a_val = res_operand_get_val(vm, a)?; - let b_val = res_operand_get_val(vm, b)?; - let mut lengths_and_indices = vec![ - (a_val.clone(), 0), - (b_val.clone() - a_val, 1), - (Felt252::from(-1) - b_val, 2), - ]; - lengths_and_indices.sort(); - exec_scopes.assign_or_update_variable("excluded_arc", Box::new(lengths_and_indices[2].1)); - // ceil((PRIME / 3) / 2 ** 128). - let prime_over_3_high = 3544607988759775765608368578435044694_u128; - // ceil((PRIME / 2) / 2 ** 128). - let prime_over_2_high = 5316911983139663648412552867652567041_u128; - let (range_check_base, range_check_offset) = extract_buffer(range_check_ptr)?; - let range_check_ptr = get_ptr(vm, range_check_base, &range_check_offset)?; - vm.insert_value( - range_check_ptr, - Felt252::from(lengths_and_indices[0].0.to_biguint() % prime_over_3_high), - )?; - vm.insert_value( - (range_check_ptr + 1)?, - Felt252::from(lengths_and_indices[0].0.to_biguint() / prime_over_3_high), - )?; - vm.insert_value( - (range_check_ptr + 2)?, - Felt252::from(lengths_and_indices[1].0.to_biguint() % prime_over_2_high), - )?; - vm.insert_value( - (range_check_ptr + 3)?, - Felt252::from(lengths_and_indices[1].0.to_biguint() / prime_over_2_high), - ) - .map_err(HintError::from) - } - - fn dict_read( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - dict_ptr: &ResOperand, - key: &ResOperand, - value_dst: &CellRef, - ) -> Result<(), HintError> { - let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; - let dict_address = get_ptr(vm, dict_base, &dict_offset)?; - let key = res_operand_get_val(vm, key)?; - let dict_manager_exec_scope = - exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; - - let value = dict_manager_exec_scope - .get_from_tracker(dict_address, &key) - .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); - - vm.insert_value(cell_ref_to_relocatable(value_dst, vm), value) - .map_err(HintError::from) - } - - fn div_mod( - &self, - vm: &mut VirtualMachine, - lhs: &ResOperand, - rhs: &ResOperand, - quotient: &CellRef, - remainder: &CellRef, - ) -> Result<(), HintError> { - let lhs_value = res_operand_get_val(vm, lhs)?.to_biguint(); - let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); - let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); - let remainder_value = Felt252::new(lhs_value % rhs_value); - vm.insert_value( - cell_ref_to_relocatable(quotient, vm), - MaybeRelocatable::from(quotient_value), - )?; - vm.insert_value( - cell_ref_to_relocatable(remainder, vm), - MaybeRelocatable::from(remainder_value), - ) - .map_err(HintError::from) - } - - fn get_segment_arena_index( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - dict_end_ptr: &ResOperand, - dict_index: &CellRef, - ) -> Result<(), HintError> { - let (dict_base, dict_offset) = extract_buffer(dict_end_ptr)?; - let dict_address = get_ptr(vm, dict_base, &dict_offset)?; - let dict_manager_exec_scope = - exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; - - let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); - vm.insert_value( - cell_ref_to_relocatable(dict_index, vm), - Felt252::from(dict_infos_index), - ) - .map_err(HintError::from) - } - - #[allow(clippy::too_many_arguments)] - fn uint256_div_mod( - &self, - vm: &mut VirtualMachine, - dividend_low: &ResOperand, - dividend_high: &ResOperand, - divisor_low: &ResOperand, - divisor_high: &ResOperand, - quotient0: &CellRef, - quotient1: &CellRef, - divisor0: &CellRef, - divisor1: &CellRef, - extra0: &CellRef, - extra1: &CellRef, - remainder_low: &CellRef, - remainder_high: &CellRef, - ) -> Result<(), HintError> { - let pow_2_128 = Felt252::from(u128::MAX) + 1u32; - let pow_2_64 = Felt252::from(u64::MAX) + 1u32; - let dividend_low = res_operand_get_val(vm, dividend_low)?; - let dividend_high = res_operand_get_val(vm, dividend_high)?; - let divisor_low = res_operand_get_val(vm, divisor_low)?; - let divisor_high = res_operand_get_val(vm, divisor_high)?; - let dividend = dividend_low + dividend_high.mul(pow_2_128.clone()); - let divisor = divisor_low + divisor_high.clone() * pow_2_128.clone(); - let quotient = dividend.clone() / divisor.clone(); - let remainder = dividend % divisor.clone(); - - // Guess quotient limbs. - let (quotient, limb) = quotient.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(quotient0, vm), limb)?; - let (quotient, limb) = quotient.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(quotient1, vm), limb)?; - let (quotient, limb) = quotient.div_rem(&pow_2_64); - if divisor_high.is_zero() { - vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; - vm.insert_value(cell_ref_to_relocatable(extra1, vm), quotient)?; - } - - // Guess divisor limbs. - let (divisor, limb) = divisor.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(divisor0, vm), limb)?; - let (divisor, limb) = divisor.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(divisor1, vm), limb)?; - let (divisor, limb) = divisor.div_rem(&pow_2_64); - if !divisor_high.is_zero() { - vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; - vm.insert_value(cell_ref_to_relocatable(extra1, vm), divisor)?; - } - - // Guess remainder limbs. - vm.insert_value( - cell_ref_to_relocatable(remainder_low, vm), - remainder.clone() % pow_2_128.clone(), - )?; - vm.insert_value( - cell_ref_to_relocatable(remainder_high, vm), - remainder / pow_2_128, - )?; - Ok(()) - } - - fn assert_le_if_first_arc_exclueded( - &self, - vm: &mut VirtualMachine, - skip_exclude_a_flag: &CellRef, - exec_scopes: &mut ExecutionScopes, - ) -> Result<(), HintError> { - let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; - let val = if excluded_arc != 0 { - Felt252::from(1) - } else { - Felt252::from(0) - }; - - vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm), val)?; - Ok(()) - } - - fn linear_split( - &self, - vm: &mut VirtualMachine, - value: &ResOperand, - scalar: &ResOperand, - max_x: &ResOperand, - x: &CellRef, - y: &CellRef, - ) -> Result<(), HintError> { - let value = res_operand_get_val(vm, value)?; - let scalar = res_operand_get_val(vm, scalar)?; - let max_x = res_operand_get_val(vm, max_x)?; - let x_value = (value.clone() / scalar.clone()).min(max_x); - let y_value = value - x_value.clone() * scalar; - - vm.insert_value(cell_ref_to_relocatable(x, vm), x_value) - .map_err(HintError::from)?; - vm.insert_value(cell_ref_to_relocatable(y, vm), y_value) - .map_err(HintError::from)?; - - Ok(()) - } - - fn random_ec_point( - &self, - vm: &mut VirtualMachine, - x: &CellRef, - y: &CellRef, - ) -> Result<(), HintError> { - let beta = Fq::from(get_beta().to_biguint()); - - let mut rng = ark_std::test_rng(); - let (random_x, random_y_squared) = loop { - let random_x = Fq::rand(&mut rng); - let random_y_squared = random_x * random_x * random_x + random_x + beta; - if random_y_squared.legendre().is_qr() { - break (random_x, random_y_squared); - } - }; - - let x_bigint: BigUint = random_x.into_bigint().into(); - let y_bigint: BigUint = random_y_squared - .sqrt() - .ok_or(HintError::CustomHint("Failed to compute sqrt".to_string()))? - .into_bigint() - .into(); - - vm.insert_value(cell_ref_to_relocatable(x, vm), Felt252::from(x_bigint))?; - vm.insert_value(cell_ref_to_relocatable(y, vm), Felt252::from(y_bigint))?; - - Ok(()) - } - - fn get_next_dict_key( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - next_key: &CellRef, - ) -> Result<(), HintError> { - let dict_squash_exec_scope: &mut DictSquashExecScope = - exec_scopes.get_mut_ref("dict_squash_exec_scope")?; - dict_squash_exec_scope.pop_current_key(); - if let Some(current_key) = dict_squash_exec_scope.current_key() { - return vm - .insert_value(cell_ref_to_relocatable(next_key, vm), current_key) - .map_err(HintError::from); - } - Err(HintError::KeyNotFound) - } - - fn alloc_felt_256_dict( - &self, - vm: &mut VirtualMachine, - segment_arena_ptr: &ResOperand, - exec_scopes: &mut ExecutionScopes, - ) -> Result<(), HintError> { - let (cell, base_offset) = extract_buffer(segment_arena_ptr)?; - let dict_manager_address = get_ptr(vm, cell, &base_offset)?; - - let n_dicts = vm - .get_integer((dict_manager_address - 2)?)? - .into_owned() - .to_usize() - .ok_or(HintError::CustomHint( - "Invalid number of dictionaries.".to_string(), - ))?; - - let dict_infos_base = vm.get_relocatable((dict_manager_address - 3)?)?; - - let dict_manager_exec_scope = - match exec_scopes.get_mut_ref::("dict_manager_exec_scope") { - Ok(dict_manager_exec_scope) => dict_manager_exec_scope, - Err(_) => { - exec_scopes.assign_or_update_variable( - "dict_manager_exec_scope", - Box::::default(), - ); - exec_scopes.get_mut_ref::("dict_manager_exec_scope")? - } - }; - let new_dict_segment = dict_manager_exec_scope.new_default_dict(vm); - vm.insert_value((dict_infos_base + 3 * n_dicts)?, new_dict_segment)?; - - Ok(()) - } - - fn assert_le_is_second_excluded( - &self, - vm: &mut VirtualMachine, - skip_exclude_b_minus_a: &CellRef, - exec_scopes: &mut ExecutionScopes, - ) -> Result<(), HintError> { - let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; - let val = if excluded_arc != 1 { - Felt252::from(1) - } else { - Felt252::from(0) - }; - - vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm), val)?; - - Ok(()) - } - - fn dict_write( - &self, - exec_scopes: &mut ExecutionScopes, - vm: &mut VirtualMachine, - dict_ptr: &ResOperand, - key: &ResOperand, - value: &ResOperand, - ) -> Result<(), HintError> { - let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; - let dict_address = get_ptr(vm, dict_base, &dict_offset)?; - let key = res_operand_get_val(vm, key)?; - let value = res_operand_get_val(vm, value)?; - let dict_manager_exec_scope = - exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; - - let prev_value = dict_manager_exec_scope - .get_from_tracker(dict_address, &key) - .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); - - vm.insert_value((dict_address + 1)?, prev_value)?; - dict_manager_exec_scope.insert_to_tracker(dict_address, key, value); - Ok(()) - } - - #[allow(clippy::too_many_arguments)] - fn uint256_square_root( - &self, - vm: &mut VirtualMachine, - value_low: &ResOperand, - value_high: &ResOperand, - sqrt0: &CellRef, - sqrt1: &CellRef, - remainder_low: &CellRef, - remainder_high: &CellRef, - sqrt_mul_2_minus_remainder_ge_u128: &CellRef, - ) -> Result<(), HintError> { - let pow_2_128 = Felt252::from(u128::MAX) + 1u32; - let pow_2_64 = Felt252::from(u64::MAX) + 1u32; - let value_low = res_operand_get_val(vm, value_low)?; - let value_high = res_operand_get_val(vm, value_high)?; - let value = value_low + value_high * pow_2_128.clone(); - let sqrt = value.sqrt(); - let remainder = value - sqrt.clone() * sqrt.clone(); - let sqrt_mul_2_minus_remainder_ge_u128_val = - sqrt.clone() * Felt252::from(2u32) - remainder.clone() >= pow_2_128; - - let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(sqrt0, vm), sqrt0_val)?; - vm.insert_value(cell_ref_to_relocatable(sqrt1, vm), sqrt1_val)?; - - let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); - - vm.insert_value( - cell_ref_to_relocatable(remainder_low, vm), - remainder_low_val, - )?; - vm.insert_value( - cell_ref_to_relocatable(remainder_high, vm), - remainder_high_val, - )?; - vm.insert_value( - cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm), - usize::from(sqrt_mul_2_minus_remainder_ge_u128_val), - )?; - - Ok(()) - } - - fn debug_print( - &self, - vm: &mut VirtualMachine, - start: &ResOperand, - end: &ResOperand, - ) -> Result<(), HintError> { - let mut curr = as_relocatable(vm, start)?; - let end = as_relocatable(vm, end)?; - while curr != end { - let value = vm.get_integer(curr)?; - if let Some(shortstring) = as_cairo_short_string(&value) { - println!("[DEBUG]\t{shortstring: <31}\t(raw: {value: <31})"); - } else { - println!("[DEBUG]\t{0: <31}\t(raw: {value: <31}) ", ' '); - } - curr += 1; - } - println!(); - Ok(()) - } - - fn assert_all_accesses_used( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - n_used_accesses: &CellRef, - ) -> Result<(), HintError> { - let key = exec_scopes.get::("key")?; - let n = get_cell_val(vm, n_used_accesses)?; - - let dict_squash_exec_scope: &mut DictSquashExecScope = - exec_scopes.get_mut_ref("dict_squash_exec_scope")?; - - let access_indices_at_key = dict_squash_exec_scope - .access_indices - .get(&key.clone()) - .ok_or_else(|| HintError::NoKeyInAccessIndices(key.clone()))?; - - if n != Felt252::new(access_indices_at_key.len()) { - return Err(HintError::NumUsedAccessesAssertFail( - n, - access_indices_at_key.len(), - key, - )); - } - - Ok(()) - } - - fn should_skip_squash_loop( - &self, - vm: &mut VirtualMachine, - exec_scopes: &mut ExecutionScopes, - should_skip_loop: &CellRef, - ) -> Result<(), HintError> { - let dict_squash_exec_scope: &mut DictSquashExecScope = - exec_scopes.get_mut_ref("dict_squash_exec_scope")?; - - let val = if dict_squash_exec_scope - .current_access_indices() - .ok_or(HintError::CustomHint("no indices accessed".to_string()))? - .len() - > 1 - { - Felt252::from(0) - } else { - Felt252::from(1) - }; - - vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm), val)?; - - Ok(()) - } -} - -impl HintProcessor for Cairo1HintProcessor { - // Ignores all data except for the code that should contain - fn compile_hint( - &self, - //Block of hint code as String - hint_code: &str, - //Ap Tracking Data corresponding to the Hint - _ap_tracking_data: &cairo_vm::serde::deserialize_program::ApTracking, - //Map from variable name to reference id number - //(may contain other variables aside from those used by the hint) - _reference_ids: &HashMap, - //List of all references (key corresponds to element of the previous dictionary) - _references: &HashMap< - usize, - cairo_vm::hint_processor::hint_processor_definition::HintReference, - >, - ) -> Result, VirtualMachineError> { - let data = hint_code.parse().ok().and_then(|x| self.hints.get(&x)).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; - Ok(any_box!(data)) - } - - fn execute_hint( - &mut self, - //Proxy to VM, contains refrences to necessary data - //+ MemoryProxy, which provides the necessary methods to manipulate memory - vm: &mut VirtualMachine, - //Proxy to ExecutionScopes, provides the necessary methods to manipulate the scopes and - //access current scope variables - exec_scopes: &mut ExecutionScopes, - //Data structure that can be downcasted to the structure generated by compile_hint - hint_data: &Box, - //Constant values extracted from the program specification. - _constants: &HashMap, - ) -> Result<(), HintError> { - let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; - for hint in hints { - self.execute(vm, exec_scopes, hint)?; - } - Ok(()) - } -} diff --git a/cairo-1-hint-processor/src/lib.rs b/cairo-1-hint-processor/src/lib.rs deleted file mode 100644 index e295b1a59a..0000000000 --- a/cairo-1-hint-processor/src/lib.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod dict_manager; -pub mod hint_processor; diff --git a/src/hint_processor/mod.rs b/src/hint_processor/mod.rs index 095e3314a8..641409f7ae 100644 --- a/src/hint_processor/mod.rs +++ b/src/hint_processor/mod.rs @@ -1,3 +1,4 @@ pub mod builtin_hint_processor; +pub mod cairo_1_hint_processor; pub mod hint_processor_definition; pub mod hint_processor_utils; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index f2f77ecd79..b808da5785 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,11 +1,13 @@ use crate::stdlib::prelude::*; +use crate::vm::runners::cairo_runner::CairoArg; use crate::{ cairo_run::{cairo_run, CairoRunConfig}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, vm::trace::trace_entry::TraceEntry, }; +use cairo_lang_starknet::casm_contract_class::CasmContractClass; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -79,3 +81,35 @@ pub(self) fn run_program( assert_eq!(runner.get_memory_holes(&vm).unwrap(), holes); } } + +pub(self) fn run_cairo_1_entrypoint( + program_content: &[u8], + entrypoint: usize, + args: &[&CairoArg], + verify_secure: bool, +) { + let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); + let mut hint_executor = Cairo1HintProcessor::new(contract_class.hints); + let res = cairo_run(data, &cairo_run_config, &mut hint_executor); + if let Some(error) = error { + assert!(res.is_err()); + assert!(res.err().unwrap().to_string().contains(error)); + return; + } + let (runner, vm) = res.expect("Execution failed"); + if let Some(trace) = trace { + let expected_trace: Vec<_> = trace + .iter() + .copied() + .map(|(pc, ap, fp)| TraceEntry { pc, ap, fp }) + .collect(); + let trace = vm.get_relocated_trace().unwrap(); + assert_eq!(trace.len(), expected_trace.len()); + for (entry, expected) in trace.iter().zip(expected_trace.iter()) { + assert_eq!(entry, expected); + } + } + if let Some(holes) = memory_holes { + assert_eq!(runner.get_memory_holes(&vm).unwrap(), holes); + } +} diff --git a/src/types/program.rs b/src/types/program.rs index 55e7f27396..c547bf553c 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -156,7 +156,7 @@ impl TryFrom for Program { .iter() .map(|x| MaybeRelocatable::from(Felt252::from(&x.value))) .collect(); - //Hint data is going to be hosted processor-side, hints fields will only store the pc where hints are located. + //Hint data is going to be hosted processor-side, hints field will only store the pc where hints are located. // Only one pc will be stored, so the hint processor will be responsible for executing all hints for a given pc let hints = value .hints From a0974fc6d65698c5f57d9d767db9e805079a3879 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 17:09:44 -0300 Subject: [PATCH 06/84] fixes --- Cargo.lock | 1 + Cargo.toml | 1 + .../cairo_1_hint_processor/dict_manager.rs | 125 +++ .../cairo_1_hint_processor/hint_processor.rs | 834 ++++++++++++++++++ .../cairo_1_hint_processor/mod.rs | 2 + src/tests/mod.rs | 1 + 6 files changed, 964 insertions(+) create mode 100644 src/hint_processor/cairo_1_hint_processor/dict_manager.rs create mode 100644 src/hint_processor/cairo_1_hint_processor/hint_processor.rs create mode 100644 src/hint_processor/cairo_1_hint_processor/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 407bd4d6d2..0ba529f459 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -758,6 +758,7 @@ dependencies = [ "bincode", "bitvec", "cairo-felt 0.3.0-rc1", + "cairo-lang-casm", "cairo-lang-starknet", "cairo-lang-utils 0.1.0", "cairo-take_until_unbalanced", diff --git a/Cargo.toml b/Cargo.toml index 67e1fdba7f..e397b3af9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -80,6 +80,7 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default ] } bitvec = { version = "1", default-features = false, features = ["alloc"] } cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46"} +cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46" } # TODO: check these dependencies for wasm compatibility cairo-lang-utils = "0.1.0" diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs new file mode 100644 index 0000000000..b1febbe72d --- /dev/null +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -0,0 +1,125 @@ +#![allow(dead_code)] //TODO: remove after implementing all hints + +use std::collections::HashMap; + +use crate::felt::Felt252; +use crate::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; + +/// Stores the data of a specific dictionary. +pub struct DictTrackerExecScope { + /// The data of the dictionary. + data: HashMap, + /// The index of the dictionary in the dict_infos segment. + #[allow(dead_code)] + idx: usize, +} + +/// Helper object to allocate, track and destruct all dictionaries in the run. +#[derive(Default)] +pub struct DictManagerExecScope { + /// Maps between a segment index and the DictTrackerExecScope associated with it. + trackers: HashMap, +} + +impl DictTrackerExecScope { + /// Creates a new tracker placed in index `idx` in the dict_infos segment. + pub fn new(idx: usize) -> Self { + Self { + data: HashMap::default(), + idx, + } + } +} + +impl DictManagerExecScope { + pub const DICT_DEFAULT_VALUE: usize = 0; + + /// Allocates a new segment for a new dictionary and return the start of the segment. + pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Relocatable { + let dict_segment = vm.add_memory_segment(); + assert!( + self.trackers + .insert( + dict_segment.segment_index, + DictTrackerExecScope::new(self.trackers.len()) + ) + .is_none(), + "Segment index already in use." + ); + dict_segment + } + + /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. + fn get_dict_tracker(&self, dict_end: Relocatable) -> &DictTrackerExecScope { + self.trackers + .get(&dict_end.segment_index) + .expect("The given value does not point to a known dictionary.") + } + + /// Returns a mut reference for a dict tracker corresponding to a given pointer to a dict + /// segment. + fn get_dict_tracker_mut(&mut self, dict_end: Relocatable) -> &mut DictTrackerExecScope { + self.trackers + .get_mut(&dict_end.segment_index) + .expect("The given value does not point to a known dictionary.") + } + + /// Returns the index of the dict tracker corresponding to a given pointer to a dict segment. + pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> usize { + self.get_dict_tracker(dict_end).idx + } + + /// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment. + pub fn insert_to_tracker(&mut self, dict_end: Relocatable, key: Felt252, value: Felt252) { + self.get_dict_tracker_mut(dict_end).data.insert(key, value); + } + + /// Gets a value from the dict tracker corresponding to a given pointer to a dict segment. + /// None if the key does not exist in the tracker data. + pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option { + self.get_dict_tracker(dict_end).data.get(key).cloned() + } +} + +/// Helper object for the management of dict_squash hints. +#[derive(Default, Debug)] +pub struct DictSquashExecScope { + /// A map from key to the list of indices accessing it, each list in reverse order. + pub access_indices: HashMap>, + /// Descending list of keys. + pub keys: Vec, +} + +impl DictSquashExecScope { + /// Returns the current key to process. + pub fn current_key(&self) -> Option { + self.keys.last().cloned() + } + + /// Returns and removes the current key, and its access indices. Should be called when only the + /// last key access is in the corresponding indices list. + pub fn pop_current_key(&mut self) -> Option { + let key_accesses = self.access_indices.remove(&self.current_key().unwrap()); + assert!( + key_accesses.unwrap().len() == 1, + "Key popped but not all accesses were processed." + ); + self.keys.pop() + } + + /// Returns a reference to the access indices list of the current key. + pub fn current_access_indices(&mut self) -> Option<&mut Vec> { + let current_key = self.current_key()?; + self.access_indices.get_mut(¤t_key) + } + + /// Returns a reference to the last index in the current access indices list. + pub fn current_access_index(&mut self) -> Option<&Felt252> { + self.current_access_indices()?.last() + } + + /// Returns and removes the current access index. + pub fn pop_current_access_index(&mut self) -> Option { + self.current_access_indices()?.pop() + } +} diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs new file mode 100644 index 0000000000..48415f79b1 --- /dev/null +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -0,0 +1,834 @@ +use super::dict_manager::DictManagerExecScope; +use crate::any_box; +use crate::felt::{felt_str, Felt252}; +use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope; +use crate::hint_processor::hint_processor_definition::HintReference; +use crate::{ + hint_processor::hint_processor_definition::HintProcessor, + types::exec_scope::ExecutionScopes, + types::relocatable::MaybeRelocatable, + types::relocatable::Relocatable, + vm::errors::vm_errors::VirtualMachineError, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, +}; +use ark_ff::fields::{Fp256, MontBackend, MontConfig}; +use ark_ff::{Field, PrimeField}; +use ark_std::UniformRand; +use cairo_lang_casm::{ + hints::{CoreHint, Hint}, + operand::{BinOpOperand, CellRef, DerefOrImmediate, Operation, Register, ResOperand}, +}; +use cairo_lang_utils::extract_matches; +use num_bigint::BigUint; +use num_integer::Integer; +use num_traits::cast::ToPrimitive; +use num_traits::identities::Zero; +use std::{collections::HashMap, ops::Mul}; + +#[derive(MontConfig)] +#[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"] +#[generator = "3"] + +/// Returns the Beta value of the Starkware elliptic curve. +struct FqConfig; +type Fq = Fp256>; + +fn get_beta() -> Felt252 { + felt_str!("3141592653589793238462643383279502884197169399375105820974944592307816406665") +} + +/// HintProcessor for Cairo 1 compiler hints. +pub struct Cairo1HintProcessor { + hints: HashMap>, +} + +/// Extracts a parameter assumed to be a buffer. +fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { + let (cell, base_offset) = match buffer { + ResOperand::Deref(cell) => (cell, 0.into()), + ResOperand::BinOp(BinOpOperand { + op: Operation::Add, + a, + b, + }) => ( + a, + extract_matches!(b, DerefOrImmediate::Immediate) + .clone() + .value + .into(), + ), + _ => { + return Err(HintError::CustomHint( + "Illegal argument for a buffer.".to_string(), + )) + } + }; + Ok((cell, base_offset)) +} + +fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable { + let base = match cell_ref.register { + Register::AP => vm.get_ap(), + Register::FP => vm.get_fp(), + }; + (base + (cell_ref.offset as i32)).unwrap() +} + +fn get_cell_val(vm: &VirtualMachine, cell: &CellRef) -> Result { + Ok(vm + .get_integer(cell_ref_to_relocatable(cell, vm))? + .as_ref() + .clone()) +} + +fn get_ptr( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm))? + offset)?) +} + +fn as_relocatable(vm: &mut VirtualMachine, value: &ResOperand) -> Result { + let (base, offset) = extract_buffer(value)?; + get_ptr(vm, base, &offset).map_err(HintError::from) +} + +fn get_double_deref_val( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok(vm.get_integer(get_ptr(vm, cell, offset)?)?.as_ref().clone()) +} + +/// Fetches the value of `res_operand` from the vm. +fn res_operand_get_val( + vm: &VirtualMachine, + res_operand: &ResOperand, +) -> Result { + match res_operand { + ResOperand::Deref(cell) => get_cell_val(vm, cell), + ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), + ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), + ResOperand::BinOp(op) => { + let a = get_cell_val(vm, &op.a)?; + let b = match &op.b { + DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, + DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()), + }; + match op.op { + Operation::Add => Ok(a + b), + Operation::Mul => Ok(a * b), + } + } + } +} + +fn as_cairo_short_string(value: &Felt252) -> Option { + let mut as_string = String::default(); + let mut is_end = false; + for byte in value.to_bytes_be() { + if byte == 0 { + is_end = true; + } else if is_end || !byte.is_ascii() { + return None; + } else { + as_string.push(byte as char); + } + } + Some(as_string) +} + +impl Cairo1HintProcessor { + pub fn new(hints: Vec<(usize, Vec)>) -> Self { + Self { + hints: hints.iter().cloned().collect(), + } + } + // Runs a single Hint + pub fn execute( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + hint: &Hint, + ) -> Result<(), HintError> { + match hint { + Hint::Core(CoreHint::AllocSegment { dst }) => self.alloc_segment(vm, dst), + Hint::Core(CoreHint::TestLessThan { lhs, rhs, dst }) => { + self.test_less_than(vm, lhs, rhs, dst) + } + Hint::Core(CoreHint::TestLessThanOrEqual { lhs, rhs, dst }) => { + self.test_less_than_or_equal(vm, lhs, rhs, dst) + } + Hint::Core(CoreHint::Felt252DictRead { + dict_ptr, + key, + value_dst, + }) => self.dict_read(vm, exec_scopes, dict_ptr, key, value_dst), + Hint::Core(CoreHint::SquareRoot { value, dst }) => self.square_root(vm, value, dst), + Hint::Core(CoreHint::GetSegmentArenaIndex { + dict_end_ptr, + dict_index, + }) => self.get_segment_arena_index(vm, exec_scopes, dict_end_ptr, dict_index), + + Hint::Core(CoreHint::DivMod { + lhs, + rhs, + quotient, + remainder, + }) => self.div_mod(vm, lhs, rhs, quotient, remainder), + Hint::Core(CoreHint::DebugPrint { start, end }) => self.debug_print(vm, start, end), + + Hint::Core(CoreHint::Uint256SquareRoot { + value_low, + value_high, + sqrt0, + sqrt1, + remainder_low, + remainder_high, + sqrt_mul_2_minus_remainder_ge_u128, + }) => self.uint256_square_root( + vm, + value_low, + value_high, + sqrt0, + sqrt1, + remainder_low, + remainder_high, + sqrt_mul_2_minus_remainder_ge_u128, + ), + + Hint::Core(CoreHint::GetNextDictKey { next_key }) => { + self.get_next_dict_key(vm, exec_scopes, next_key) + } + + Hint::Core(CoreHint::Uint256DivMod { + dividend_low, + dividend_high, + divisor_low, + divisor_high, + quotient0, + quotient1, + divisor0, + divisor1, + extra0, + extra1, + remainder_low, + remainder_high, + }) => self.uint256_div_mod( + vm, + dividend_low, + dividend_high, + divisor_low, + divisor_high, + quotient0, + quotient1, + divisor0, + divisor1, + extra0, + extra1, + remainder_low, + remainder_high, + ), + Hint::Core(CoreHint::Felt252DictWrite { + dict_ptr, + key, + value, + }) => self.dict_write(exec_scopes, vm, dict_ptr, key, value), + Hint::Core(CoreHint::AssertLeIsFirstArcExcluded { + skip_exclude_a_flag, + }) => self.assert_le_if_first_arc_exclueded(vm, skip_exclude_a_flag, exec_scopes), + + Hint::Core(CoreHint::AssertAllAccessesUsed { n_used_accesses }) => { + self.assert_all_accesses_used(vm, exec_scopes, n_used_accesses) + } + + Hint::Core(CoreHint::AssertLeIsSecondArcExcluded { + skip_exclude_b_minus_a, + }) => self.assert_le_is_second_excluded(vm, skip_exclude_b_minus_a, exec_scopes), + + Hint::Core(CoreHint::LinearSplit { + value, + scalar, + max_x, + x, + y, + }) => self.linear_split(vm, value, scalar, max_x, x, y), + + Hint::Core(CoreHint::AllocFelt252Dict { segment_arena_ptr }) => { + self.alloc_felt_256_dict(vm, segment_arena_ptr, exec_scopes) + } + + Hint::Core(CoreHint::AssertLeFindSmallArcs { + range_check_ptr, + a, + b, + }) => self.assert_le_find_small_arcs(vm, exec_scopes, range_check_ptr, a, b), + + Hint::Core(CoreHint::RandomEcPoint { x, y }) => self.random_ec_point(vm, x, y), + + Hint::Core(CoreHint::ShouldSkipSquashLoop { should_skip_loop }) => { + self.should_skip_squash_loop(vm, exec_scopes, should_skip_loop) + } + _ => todo!(), + } + } + + fn alloc_segment(&self, vm: &mut VirtualMachine, dst: &CellRef) -> Result<(), HintError> { + let segment = vm.add_memory_segment(); + vm.insert_value(cell_ref_to_relocatable(dst, vm), segment) + .map_err(HintError::from) + } + + fn test_less_than( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?; + let rhs_value = res_operand_get_val(vm, rhs)?; + let result = if lhs_value < rhs_value { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn square_root( + &self, + vm: &mut VirtualMachine, + value: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let value = res_operand_get_val(vm, value)?; + let result = value.sqrt(); + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn test_less_than_or_equal( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + dst: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?; + let rhs_value = res_operand_get_val(vm, rhs)?; + let result = if lhs_value <= rhs_value { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value( + cell_ref_to_relocatable(dst, vm), + MaybeRelocatable::from(result), + ) + .map_err(HintError::from) + } + + fn assert_le_find_small_arcs( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + range_check_ptr: &ResOperand, + a: &ResOperand, + b: &ResOperand, + ) -> Result<(), HintError> { + let a_val = res_operand_get_val(vm, a)?; + let b_val = res_operand_get_val(vm, b)?; + let mut lengths_and_indices = vec![ + (a_val.clone(), 0), + (b_val.clone() - a_val, 1), + (Felt252::from(-1) - b_val, 2), + ]; + lengths_and_indices.sort(); + exec_scopes.assign_or_update_variable("excluded_arc", Box::new(lengths_and_indices[2].1)); + // ceil((PRIME / 3) / 2 ** 128). + let prime_over_3_high = 3544607988759775765608368578435044694_u128; + // ceil((PRIME / 2) / 2 ** 128). + let prime_over_2_high = 5316911983139663648412552867652567041_u128; + let (range_check_base, range_check_offset) = extract_buffer(range_check_ptr)?; + let range_check_ptr = get_ptr(vm, range_check_base, &range_check_offset)?; + vm.insert_value( + range_check_ptr, + Felt252::from(lengths_and_indices[0].0.to_biguint() % prime_over_3_high), + )?; + vm.insert_value( + (range_check_ptr + 1)?, + Felt252::from(lengths_and_indices[0].0.to_biguint() / prime_over_3_high), + )?; + vm.insert_value( + (range_check_ptr + 2)?, + Felt252::from(lengths_and_indices[1].0.to_biguint() % prime_over_2_high), + )?; + vm.insert_value( + (range_check_ptr + 3)?, + Felt252::from(lengths_and_indices[1].0.to_biguint() / prime_over_2_high), + ) + .map_err(HintError::from) + } + + fn dict_read( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + dict_ptr: &ResOperand, + key: &ResOperand, + value_dst: &CellRef, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let key = res_operand_get_val(vm, key)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let value = dict_manager_exec_scope + .get_from_tracker(dict_address, &key) + .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); + + vm.insert_value(cell_ref_to_relocatable(value_dst, vm), value) + .map_err(HintError::from) + } + + fn div_mod( + &self, + vm: &mut VirtualMachine, + lhs: &ResOperand, + rhs: &ResOperand, + quotient: &CellRef, + remainder: &CellRef, + ) -> Result<(), HintError> { + let lhs_value = res_operand_get_val(vm, lhs)?.to_biguint(); + let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); + let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); + let remainder_value = Felt252::new(lhs_value % rhs_value); + vm.insert_value( + cell_ref_to_relocatable(quotient, vm), + MaybeRelocatable::from(quotient_value), + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder, vm), + MaybeRelocatable::from(remainder_value), + ) + .map_err(HintError::from) + } + + fn get_segment_arena_index( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + dict_end_ptr: &ResOperand, + dict_index: &CellRef, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_end_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); + vm.insert_value( + cell_ref_to_relocatable(dict_index, vm), + Felt252::from(dict_infos_index), + ) + .map_err(HintError::from) + } + + #[allow(clippy::too_many_arguments)] + fn uint256_div_mod( + &self, + vm: &mut VirtualMachine, + dividend_low: &ResOperand, + dividend_high: &ResOperand, + divisor_low: &ResOperand, + divisor_high: &ResOperand, + quotient0: &CellRef, + quotient1: &CellRef, + divisor0: &CellRef, + divisor1: &CellRef, + extra0: &CellRef, + extra1: &CellRef, + remainder_low: &CellRef, + remainder_high: &CellRef, + ) -> Result<(), HintError> { + let pow_2_128 = Felt252::from(u128::MAX) + 1u32; + let pow_2_64 = Felt252::from(u64::MAX) + 1u32; + let dividend_low = res_operand_get_val(vm, dividend_low)?; + let dividend_high = res_operand_get_val(vm, dividend_high)?; + let divisor_low = res_operand_get_val(vm, divisor_low)?; + let divisor_high = res_operand_get_val(vm, divisor_high)?; + let dividend = dividend_low + dividend_high.mul(pow_2_128.clone()); + let divisor = divisor_low + divisor_high.clone() * pow_2_128.clone(); + let quotient = dividend.clone() / divisor.clone(); + let remainder = dividend % divisor.clone(); + + // Guess quotient limbs. + let (quotient, limb) = quotient.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(quotient0, vm), limb)?; + let (quotient, limb) = quotient.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(quotient1, vm), limb)?; + let (quotient, limb) = quotient.div_rem(&pow_2_64); + if divisor_high.is_zero() { + vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm), quotient)?; + } + + // Guess divisor limbs. + let (divisor, limb) = divisor.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(divisor0, vm), limb)?; + let (divisor, limb) = divisor.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(divisor1, vm), limb)?; + let (divisor, limb) = divisor.div_rem(&pow_2_64); + if !divisor_high.is_zero() { + vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm), divisor)?; + } + + // Guess remainder limbs. + vm.insert_value( + cell_ref_to_relocatable(remainder_low, vm), + remainder.clone() % pow_2_128.clone(), + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder_high, vm), + remainder / pow_2_128, + )?; + Ok(()) + } + + fn assert_le_if_first_arc_exclueded( + &self, + vm: &mut VirtualMachine, + skip_exclude_a_flag: &CellRef, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; + let val = if excluded_arc != 0 { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm), val)?; + Ok(()) + } + + fn linear_split( + &self, + vm: &mut VirtualMachine, + value: &ResOperand, + scalar: &ResOperand, + max_x: &ResOperand, + x: &CellRef, + y: &CellRef, + ) -> Result<(), HintError> { + let value = res_operand_get_val(vm, value)?; + let scalar = res_operand_get_val(vm, scalar)?; + let max_x = res_operand_get_val(vm, max_x)?; + let x_value = (value.clone() / scalar.clone()).min(max_x); + let y_value = value - x_value.clone() * scalar; + + vm.insert_value(cell_ref_to_relocatable(x, vm), x_value) + .map_err(HintError::from)?; + vm.insert_value(cell_ref_to_relocatable(y, vm), y_value) + .map_err(HintError::from)?; + + Ok(()) + } + + fn random_ec_point( + &self, + vm: &mut VirtualMachine, + x: &CellRef, + y: &CellRef, + ) -> Result<(), HintError> { + let beta = Fq::from(get_beta().to_biguint()); + + let mut rng = ark_std::test_rng(); + let (random_x, random_y_squared) = loop { + let random_x = Fq::rand(&mut rng); + let random_y_squared = random_x * random_x * random_x + random_x + beta; + if random_y_squared.legendre().is_qr() { + break (random_x, random_y_squared); + } + }; + + let x_bigint: BigUint = random_x.into_bigint().into(); + let y_bigint: BigUint = random_y_squared + .sqrt() + .ok_or(HintError::CustomHint("Failed to compute sqrt".to_string()))? + .into_bigint() + .into(); + + vm.insert_value(cell_ref_to_relocatable(x, vm), Felt252::from(x_bigint))?; + vm.insert_value(cell_ref_to_relocatable(y, vm), Felt252::from(y_bigint))?; + + Ok(()) + } + + fn get_next_dict_key( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + next_key: &CellRef, + ) -> Result<(), HintError> { + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + dict_squash_exec_scope.pop_current_key(); + if let Some(current_key) = dict_squash_exec_scope.current_key() { + return vm + .insert_value(cell_ref_to_relocatable(next_key, vm), current_key) + .map_err(HintError::from); + } + Err(HintError::KeyNotFound) + } + + fn alloc_felt_256_dict( + &self, + vm: &mut VirtualMachine, + segment_arena_ptr: &ResOperand, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let (cell, base_offset) = extract_buffer(segment_arena_ptr)?; + let dict_manager_address = get_ptr(vm, cell, &base_offset)?; + + let n_dicts = vm + .get_integer((dict_manager_address - 2)?)? + .into_owned() + .to_usize() + .ok_or(HintError::CustomHint( + "Invalid number of dictionaries.".to_string(), + ))?; + + let dict_infos_base = vm.get_relocatable((dict_manager_address - 3)?)?; + + let dict_manager_exec_scope = + match exec_scopes.get_mut_ref::("dict_manager_exec_scope") { + Ok(dict_manager_exec_scope) => dict_manager_exec_scope, + Err(_) => { + exec_scopes.assign_or_update_variable( + "dict_manager_exec_scope", + Box::::default(), + ); + exec_scopes.get_mut_ref::("dict_manager_exec_scope")? + } + }; + let new_dict_segment = dict_manager_exec_scope.new_default_dict(vm); + vm.insert_value((dict_infos_base + 3 * n_dicts)?, new_dict_segment)?; + + Ok(()) + } + + fn assert_le_is_second_excluded( + &self, + vm: &mut VirtualMachine, + skip_exclude_b_minus_a: &CellRef, + exec_scopes: &mut ExecutionScopes, + ) -> Result<(), HintError> { + let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; + let val = if excluded_arc != 1 { + Felt252::from(1) + } else { + Felt252::from(0) + }; + + vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm), val)?; + + Ok(()) + } + + fn dict_write( + &self, + exec_scopes: &mut ExecutionScopes, + vm: &mut VirtualMachine, + dict_ptr: &ResOperand, + key: &ResOperand, + value: &ResOperand, + ) -> Result<(), HintError> { + let (dict_base, dict_offset) = extract_buffer(dict_ptr)?; + let dict_address = get_ptr(vm, dict_base, &dict_offset)?; + let key = res_operand_get_val(vm, key)?; + let value = res_operand_get_val(vm, value)?; + let dict_manager_exec_scope = + exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; + + let prev_value = dict_manager_exec_scope + .get_from_tracker(dict_address, &key) + .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); + + vm.insert_value((dict_address + 1)?, prev_value)?; + dict_manager_exec_scope.insert_to_tracker(dict_address, key, value); + Ok(()) + } + + #[allow(clippy::too_many_arguments)] + fn uint256_square_root( + &self, + vm: &mut VirtualMachine, + value_low: &ResOperand, + value_high: &ResOperand, + sqrt0: &CellRef, + sqrt1: &CellRef, + remainder_low: &CellRef, + remainder_high: &CellRef, + sqrt_mul_2_minus_remainder_ge_u128: &CellRef, + ) -> Result<(), HintError> { + let pow_2_128 = Felt252::from(u128::MAX) + 1u32; + let pow_2_64 = Felt252::from(u64::MAX) + 1u32; + let value_low = res_operand_get_val(vm, value_low)?; + let value_high = res_operand_get_val(vm, value_high)?; + let value = value_low + value_high * pow_2_128.clone(); + let sqrt = value.sqrt(); + let remainder = value - sqrt.clone() * sqrt.clone(); + let sqrt_mul_2_minus_remainder_ge_u128_val = + sqrt.clone() * Felt252::from(2u32) - remainder.clone() >= pow_2_128; + + let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); + vm.insert_value(cell_ref_to_relocatable(sqrt0, vm), sqrt0_val)?; + vm.insert_value(cell_ref_to_relocatable(sqrt1, vm), sqrt1_val)?; + + let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); + + vm.insert_value( + cell_ref_to_relocatable(remainder_low, vm), + remainder_low_val, + )?; + vm.insert_value( + cell_ref_to_relocatable(remainder_high, vm), + remainder_high_val, + )?; + vm.insert_value( + cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm), + usize::from(sqrt_mul_2_minus_remainder_ge_u128_val), + )?; + + Ok(()) + } + + fn debug_print( + &self, + vm: &mut VirtualMachine, + start: &ResOperand, + end: &ResOperand, + ) -> Result<(), HintError> { + let mut curr = as_relocatable(vm, start)?; + let end = as_relocatable(vm, end)?; + while curr != end { + let value = vm.get_integer(curr)?; + if let Some(shortstring) = as_cairo_short_string(&value) { + println!("[DEBUG]\t{shortstring: <31}\t(raw: {value: <31})"); + } else { + println!("[DEBUG]\t{0: <31}\t(raw: {value: <31}) ", ' '); + } + curr += 1; + } + println!(); + Ok(()) + } + + fn assert_all_accesses_used( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + n_used_accesses: &CellRef, + ) -> Result<(), HintError> { + let key = exec_scopes.get::("key")?; + let n = get_cell_val(vm, n_used_accesses)?; + + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + + let access_indices_at_key = dict_squash_exec_scope + .access_indices + .get(&key.clone()) + .ok_or_else(|| HintError::NoKeyInAccessIndices(key.clone()))?; + + if n != Felt252::new(access_indices_at_key.len()) { + return Err(HintError::NumUsedAccessesAssertFail( + n, + access_indices_at_key.len(), + key, + )); + } + + Ok(()) + } + + fn should_skip_squash_loop( + &self, + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + should_skip_loop: &CellRef, + ) -> Result<(), HintError> { + let dict_squash_exec_scope: &mut DictSquashExecScope = + exec_scopes.get_mut_ref("dict_squash_exec_scope")?; + + let val = if dict_squash_exec_scope + .current_access_indices() + .ok_or(HintError::CustomHint("no indices accessed".to_string()))? + .len() + > 1 + { + Felt252::from(0) + } else { + Felt252::from(1) + }; + + vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm), val)?; + + Ok(()) + } +} + +impl HintProcessor for Cairo1HintProcessor { + // Ignores all data except for the code that should contain + fn compile_hint( + &self, + //Block of hint code as String + hint_code: &str, + //Ap Tracking Data corresponding to the Hint + _ap_tracking_data: &crate::serde::deserialize_program::ApTracking, + //Map from variable name to reference id number + //(may contain other variables aside from those used by the hint) + _reference_ids: &HashMap, + //List of all references (key corresponds to element of the previous dictionary) + _references: &HashMap, + ) -> Result, VirtualMachineError> { + let data = hint_code.parse().ok().and_then(|x| self.hints.get(&x).cloned()).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; + Ok(any_box!(data)) + } + + // Executes all the hints for a given pc + fn execute_hint( + &mut self, + //Proxy to VM, contains refrences to necessary data + //+ MemoryProxy, which provides the necessary methods to manipulate memory + vm: &mut VirtualMachine, + //Proxy to ExecutionScopes, provides the necessary methods to manipulate the scopes and + //access current scope variables + exec_scopes: &mut ExecutionScopes, + //Data structure that can be downcasted to the structure generated by compile_hint + hint_data: &Box, + //Constant values extracted from the program specification. + _constants: &HashMap, + ) -> Result<(), HintError> { + let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; + for hint in hints { + self.execute(vm, exec_scopes, hint)?; + } + Ok(()) + } +} diff --git a/src/hint_processor/cairo_1_hint_processor/mod.rs b/src/hint_processor/cairo_1_hint_processor/mod.rs new file mode 100644 index 0000000000..e295b1a59a --- /dev/null +++ b/src/hint_processor/cairo_1_hint_processor/mod.rs @@ -0,0 +1,2 @@ +pub mod dict_manager; +pub mod hint_processor; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index b808da5785..8685c79ac2 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,3 +1,4 @@ +use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; use crate::stdlib::prelude::*; use crate::vm::runners::cairo_runner::CairoArg; From 3257b9df9e5fdf9dece1ec9d7e76d3c761f34f3d Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 17:19:15 -0300 Subject: [PATCH 07/84] Fix test helper --- .../cairo_1_hint_processor/hint_processor.rs | 2 +- src/tests/mod.rs | 41 ++++++++----------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 48415f79b1..9e96ac2a1e 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -141,7 +141,7 @@ fn as_cairo_short_string(value: &Felt252) -> Option { } impl Cairo1HintProcessor { - pub fn new(hints: Vec<(usize, Vec)>) -> Self { + pub fn new(hints: &Vec<(usize, Vec)>) -> Self { Self { hints: hints.iter().cloned().collect(), } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 8685c79ac2..4d42e0cd67 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,7 +1,8 @@ use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; use crate::stdlib::prelude::*; -use crate::vm::runners::cairo_runner::CairoArg; +use crate::vm::runners::cairo_runner::{CairoArg, CairoRunner}; +use crate::vm::vm_core::VirtualMachine; use crate::{ cairo_run::{cairo_run, CairoRunConfig}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, @@ -90,27 +91,19 @@ pub(self) fn run_cairo_1_entrypoint( verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); - let mut hint_executor = Cairo1HintProcessor::new(contract_class.hints); - let res = cairo_run(data, &cairo_run_config, &mut hint_executor); - if let Some(error) = error { - assert!(res.is_err()); - assert!(res.err().unwrap().to_string().contains(error)); - return; - } - let (runner, vm) = res.expect("Execution failed"); - if let Some(trace) = trace { - let expected_trace: Vec<_> = trace - .iter() - .copied() - .map(|(pc, ap, fp)| TraceEntry { pc, ap, fp }) - .collect(); - let trace = vm.get_relocated_trace().unwrap(); - assert_eq!(trace.len(), expected_trace.len()); - for (entry, expected) in trace.iter().zip(expected_trace.iter()) { - assert_eq!(entry, expected); - } - } - if let Some(holes) = memory_holes { - assert_eq!(runner.get_memory_holes(&vm).unwrap(), holes); - } + let mut hint_processor = Cairo1HintProcessor::new(&contract_class.hints); + + let mut runner = + CairoRunner::new(&(contract_class.try_into().unwrap()), "all_cairo", false).unwrap(); + let mut vm = VirtualMachine::new(false); + runner + .run_from_entrypoint( + entrypoint, + args, + verify_secure, + None, + &mut vm, + &mut hint_processor, + ) + .unwrap(); } From 6eb32a6178cda0bc70abac368e20ddef14685848 Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 2 May 2023 17:20:13 -0300 Subject: [PATCH 08/84] Remove allow --- src/hint_processor/cairo_1_hint_processor/dict_manager.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index b1febbe72d..93e57b58f4 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] //TODO: remove after implementing all hints - use std::collections::HashMap; use crate::felt::Felt252; From 95be38608078d262c7415698a6a2604fc5cc3507 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 May 2023 12:39:15 -0300 Subject: [PATCH 09/84] fix test func --- src/tests/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4d42e0cd67..4f8a57d143 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -17,6 +17,7 @@ mod bitwise_test; mod cairo_run_test; mod pedersen_test; mod struct_test; +mod cairo_1_run_from_entrypoint_tests; #[cfg(feature = "skip_next_instruction_hint")] mod skip_instruction_test; @@ -96,6 +97,7 @@ pub(self) fn run_cairo_1_entrypoint( let mut runner = CairoRunner::new(&(contract_class.try_into().unwrap()), "all_cairo", false).unwrap(); let mut vm = VirtualMachine::new(false); + runner.initialize_function_runner(&mut vm, false).unwrap(); runner .run_from_entrypoint( entrypoint, @@ -107,3 +109,5 @@ pub(self) fn run_cairo_1_entrypoint( ) .unwrap(); } + +//TODO: add cairo_args! macro From 0ce7380ef627dc03ac9b271915c92623b473ee30 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 May 2023 15:12:29 -0300 Subject: [PATCH 10/84] Add builtins to test --- src/tests/mod.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 4f8a57d143..889fd8c750 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,6 +1,7 @@ use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; use crate::stdlib::prelude::*; +use crate::types::relocatable::MaybeRelocatable; use crate::vm::runners::cairo_runner::{CairoArg, CairoRunner}; use crate::vm::vm_core::VirtualMachine; use crate::{ @@ -14,10 +15,10 @@ use cairo_lang_starknet::casm_contract_class::CasmContractClass; use wasm_bindgen_test::*; mod bitwise_test; +mod cairo_1_run_from_entrypoint_tests; mod cairo_run_test; mod pedersen_test; mod struct_test; -mod cairo_1_run_from_entrypoint_tests; #[cfg(feature = "skip_next_instruction_hint")] mod skip_instruction_test; @@ -88,7 +89,8 @@ pub(self) fn run_program( pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], entrypoint: usize, - args: &[&CairoArg], + builtins: Vec<&'static str>, + function_args: &[&CairoArg], verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); @@ -98,10 +100,17 @@ pub(self) fn run_cairo_1_entrypoint( CairoRunner::new(&(contract_class.try_into().unwrap()), "all_cairo", false).unwrap(); let mut vm = VirtualMachine::new(false); runner.initialize_function_runner(&mut vm, false).unwrap(); + let mut cairo_args: Vec<&CairoArg> = vm + .get_builtin_runners() + .iter() + .filter(|b| builtins.contains(&b.name())) + .map(|b| &CairoArg::from(MaybeRelocatable::from((b.base() as isize, 0_usize)))) + .collect(); + cairo_args.extend(function_args); runner .run_from_entrypoint( entrypoint, - args, + &cairo_args, verify_secure, None, &mut vm, From ee4db2ca028cfcbd0c97526e9bf1f597c9341b13 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 May 2023 15:21:57 -0300 Subject: [PATCH 11/84] Extract builtins from contract_class --- src/tests/mod.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 889fd8c750..195cf260e1 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -89,24 +89,42 @@ pub(self) fn run_program( pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], entrypoint: usize, - builtins: Vec<&'static str>, function_args: &[&CairoArg], verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); let mut hint_processor = Cairo1HintProcessor::new(&contract_class.hints); - let mut runner = - CairoRunner::new(&(contract_class.try_into().unwrap()), "all_cairo", false).unwrap(); + let mut runner = CairoRunner::new( + &(contract_class.clone().try_into().unwrap()), + "all_cairo", + false, + ) + .unwrap(); let mut vm = VirtualMachine::new(false); + runner.initialize_function_runner(&mut vm, false).unwrap(); - let mut cairo_args: Vec<&CairoArg> = vm + + // Get builtin bases + // Extract builtins from CasmContractClass entrypoint data from the entrypoint which's offset is being ran + let builtins = contract_class + .entry_points_by_type + .external + .iter() + .find(|e| e.offset == entrypoint) + .unwrap() + .builtins + .clone(); + let cairo_args: Vec = vm .get_builtin_runners() .iter() - .filter(|b| builtins.contains(&b.name())) - .map(|b| &CairoArg::from(MaybeRelocatable::from((b.base() as isize, 0_usize)))) + .filter(|b| builtins.contains(&(b.name().to_string()))) + .map(|b| CairoArg::from(MaybeRelocatable::from((b.base() as isize, 0_usize)))) .collect(); + + let mut cairo_args: Vec<&CairoArg> = cairo_args.iter().collect(); cairo_args.extend(function_args); + runner .run_from_entrypoint( entrypoint, From f0d41515e4288dffde431fdb582b2d17242dcc24 Mon Sep 17 00:00:00 2001 From: Federica Date: Wed, 3 May 2023 15:32:19 -0300 Subject: [PATCH 12/84] Add _builtin to builtin names in etrypoint data --- src/tests/mod.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 195cf260e1..da7377ab11 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -88,7 +88,7 @@ pub(self) fn run_program( pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], - entrypoint: usize, + entrypoint_offset: usize, function_args: &[&CairoArg], verify_secure: bool, ) { @@ -107,14 +107,17 @@ pub(self) fn run_cairo_1_entrypoint( // Get builtin bases // Extract builtins from CasmContractClass entrypoint data from the entrypoint which's offset is being ran - let builtins = contract_class + let builtins: Vec = contract_class .entry_points_by_type .external .iter() - .find(|e| e.offset == entrypoint) + .find(|e| e.offset == entrypoint_offset) .unwrap() .builtins - .clone(); + .iter() + .map(|n| format!("{}_builtin", n)) + .collect(); + let cairo_args: Vec = vm .get_builtin_runners() .iter() @@ -127,7 +130,7 @@ pub(self) fn run_cairo_1_entrypoint( runner .run_from_entrypoint( - entrypoint, + entrypoint_offset, &cairo_args, verify_secure, None, From 4032a94ec84009706f7072ccd5de460d8facf6e7 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 10:54:51 -0300 Subject: [PATCH 13/84] Copy logic from cairo1 contract execution in starknet --- src/tests/mod.rs | 64 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index da7377ab11..bcd264ead8 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,5 +1,6 @@ use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; use crate::stdlib::prelude::*; +use num_traits::Bounded; use crate::types::relocatable::MaybeRelocatable; use crate::vm::runners::cairo_runner::{CairoArg, CairoRunner}; @@ -11,6 +12,7 @@ use crate::{ }; use cairo_lang_starknet::casm_contract_class::CasmContractClass; +use felt::Felt252; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -89,7 +91,7 @@ pub(self) fn run_program( pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], entrypoint_offset: usize, - function_args: &[&CairoArg], + args: &Vec, verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); @@ -102,8 +104,10 @@ pub(self) fn run_cairo_1_entrypoint( ) .unwrap(); let mut vm = VirtualMachine::new(false); + dbg!(&vm.segments.memory.data); - runner.initialize_function_runner(&mut vm, false).unwrap(); + runner.initialize_function_runner(&mut vm, true).unwrap(); + dbg!(&vm.segments.memory.data); // Get builtin bases // Extract builtins from CasmContractClass entrypoint data from the entrypoint which's offset is being ran @@ -118,20 +122,68 @@ pub(self) fn run_cairo_1_entrypoint( .map(|n| format!("{}_builtin", n)) .collect(); - let cairo_args: Vec = vm + let builtin_bases: Vec = vm .get_builtin_runners() .iter() .filter(|b| builtins.contains(&(b.name().to_string()))) .map(|b| CairoArg::from(MaybeRelocatable::from((b.base() as isize, 0_usize)))) .collect(); - let mut cairo_args: Vec<&CairoArg> = cairo_args.iter().collect(); - cairo_args.extend(function_args); + // Implicit Args + let syscall_segment = MaybeRelocatable::from(vm.add_memory_segment()); + + let builtin_segment: Vec = vm + .get_builtin_runners() + .iter() + .filter(|b| builtins.contains(&(b.name().to_string()))) + .map(|b| b.initial_stack()) + .flatten() + .collect(); + + let initial_gas = MaybeRelocatable::from(Felt252::max_value()); + + let mut implicit_args = builtin_segment; + implicit_args.extend([initial_gas]); + implicit_args.extend([syscall_segment.clone()]); + + // Other args + + // Load builtin costs + let builtin_costs: Vec = + vec![0.into(), 0.into(), 0.into(), 0.into(), 0.into()]; + let builtin_costs_ptr = vm.add_memory_segment(); + vm.load_data(builtin_costs_ptr, &builtin_costs).unwrap(); + + // Load extra data + let core_program_end_ptr = + (runner.program_base.unwrap() + runner.program.shared_program_data.data.len()).unwrap(); + let program_extra_data: Vec = + vec![0x208B7FFF7FFF7FFE.into(), builtin_costs_ptr.into()]; + vm.load_data(core_program_end_ptr, &program_extra_data) + .unwrap(); + + // Load calldata + let calldata_start = vm.add_memory_segment(); + let calldata_end = vm.load_data(calldata_start, args).unwrap(); + + // Create entrypoint_args + + let mut entrypoint_args: Vec = implicit_args + .iter() + .map(|m| CairoArg::from(m.clone())) + .collect(); + entrypoint_args.extend([ + MaybeRelocatable::from(calldata_start).into(), + MaybeRelocatable::from(calldata_end).into(), + ]); + let entrypoint_args: Vec<&CairoArg> = entrypoint_args.iter().collect(); + + dbg!(&vm.segments.memory.data); runner .run_from_entrypoint( entrypoint_offset, - &cairo_args, + &entrypoint_args, verify_secure, None, &mut vm, From 6c9be6310e9771a037630a07f0f7d6a01953b20e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 10:56:20 -0300 Subject: [PATCH 14/84] Remove unused code --- src/tests/mod.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index bcd264ead8..8b748c646a 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -122,13 +122,6 @@ pub(self) fn run_cairo_1_entrypoint( .map(|n| format!("{}_builtin", n)) .collect(); - let builtin_bases: Vec = vm - .get_builtin_runners() - .iter() - .filter(|b| builtins.contains(&(b.name().to_string()))) - .map(|b| CairoArg::from(MaybeRelocatable::from((b.base() as isize, 0_usize)))) - .collect(); - // Implicit Args let syscall_segment = MaybeRelocatable::from(vm.add_memory_segment()); @@ -178,8 +171,6 @@ pub(self) fn run_cairo_1_entrypoint( ]); let entrypoint_args: Vec<&CairoArg> = entrypoint_args.iter().collect(); - dbg!(&vm.segments.memory.data); - runner .run_from_entrypoint( entrypoint_offset, @@ -191,5 +182,3 @@ pub(self) fn run_cairo_1_entrypoint( ) .unwrap(); } - -//TODO: add cairo_args! macro From 52984d7799872412b2bc14653d3ce670a872367b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 10:57:40 -0300 Subject: [PATCH 15/84] Use lower initial_gas value --- src/tests/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 8b748c646a..07cf1c8bcf 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,6 +1,5 @@ use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; use crate::stdlib::prelude::*; -use num_traits::Bounded; use crate::types::relocatable::MaybeRelocatable; use crate::vm::runners::cairo_runner::{CairoArg, CairoRunner}; @@ -12,7 +11,6 @@ use crate::{ }; use cairo_lang_starknet::casm_contract_class::CasmContractClass; -use felt::Felt252; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -133,7 +131,7 @@ pub(self) fn run_cairo_1_entrypoint( .flatten() .collect(); - let initial_gas = MaybeRelocatable::from(Felt252::max_value()); + let initial_gas = MaybeRelocatable::from(usize::MAX); let mut implicit_args = builtin_segment; implicit_args.extend([initial_gas]); From 151375fe942d42bbc307eb53f3718732921cdf29 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 10:59:32 -0300 Subject: [PATCH 16/84] Add program segment size argument --- src/tests/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 07cf1c8bcf..74ea93bf2c 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -174,7 +174,7 @@ pub(self) fn run_cairo_1_entrypoint( entrypoint_offset, &entrypoint_args, verify_secure, - None, + Some(runner.program.shared_program_data.data.len() + program_extra_data.len()), &mut vm, &mut hint_processor, ) From f11e3500ad355c53ae58b278eab5abe3c5b2b64c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:30:23 -0300 Subject: [PATCH 17/84] Check return values in run_cairo_1_entrypoint fn --- .../cairo_1_hint_processor/hint_processor.rs | 1 + src/tests/mod.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 9e96ac2a1e..ef856ebe22 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -827,6 +827,7 @@ impl HintProcessor for Cairo1HintProcessor { ) -> Result<(), HintError> { let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; for hint in hints { + dbg!(hint); self.execute(vm, exec_scopes, hint)?; } Ok(()) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 74ea93bf2c..577ee31eb2 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -11,6 +11,7 @@ use crate::{ }; use cairo_lang_starknet::casm_contract_class::CasmContractClass; +use felt::Felt252; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -86,10 +87,13 @@ pub(self) fn run_program( } } +// Runs a contract entrypoint with given arguments and checks its return values +// Doesn't use a syscall_handler pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], entrypoint_offset: usize, args: &Vec, + expected_retdata: &Vec, verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); @@ -169,6 +173,8 @@ pub(self) fn run_cairo_1_entrypoint( ]); let entrypoint_args: Vec<&CairoArg> = entrypoint_args.iter().collect(); + // Run contract entrypoint + runner .run_from_entrypoint( entrypoint_offset, @@ -179,4 +185,16 @@ pub(self) fn run_cairo_1_entrypoint( &mut hint_processor, ) .unwrap(); + + // Check return values + let return_values = vm.get_return_values(5).unwrap(); + let retdata_start = return_values[3].get_relocatable().unwrap(); + let retdata_end = return_values[4].get_relocatable().unwrap(); + let retdata: Vec = vm + .get_integer_range(retdata_start, (retdata_end - retdata_start).unwrap()) + .unwrap() + .iter() + .map(|c| c.clone().into_owned()) + .collect(); + assert_eq!(expected_retdata, &retdata); } From f466aa75d1073643d0f8e4e9feaf3d6ce8a3c8c4 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:31:11 -0300 Subject: [PATCH 18/84] Remove debug prints --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 1 - src/tests/mod.rs | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index ef856ebe22..9e96ac2a1e 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -827,7 +827,6 @@ impl HintProcessor for Cairo1HintProcessor { ) -> Result<(), HintError> { let hints: &Vec = hint_data.downcast_ref().ok_or(HintError::WrongHintData)?; for hint in hints { - dbg!(hint); self.execute(vm, exec_scopes, hint)?; } Ok(()) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 577ee31eb2..3b1e222b0d 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -106,10 +106,8 @@ pub(self) fn run_cairo_1_entrypoint( ) .unwrap(); let mut vm = VirtualMachine::new(false); - dbg!(&vm.segments.memory.data); runner.initialize_function_runner(&mut vm, true).unwrap(); - dbg!(&vm.segments.memory.data); // Get builtin bases // Extract builtins from CasmContractClass entrypoint data from the entrypoint which's offset is being ran From 8a528a02812b3f9c89addd3152733f73f9bfaa2f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:33:09 -0300 Subject: [PATCH 19/84] Add basic fibonacci test --- cairo_programs/cairo-1-contracts/fib.cairo | 11 +++++++++++ src/tests/cairo_1_run_from_entrypoint_tests.rs | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 cairo_programs/cairo-1-contracts/fib.cairo create mode 100644 src/tests/cairo_1_run_from_entrypoint_tests.rs diff --git a/cairo_programs/cairo-1-contracts/fib.cairo b/cairo_programs/cairo-1-contracts/fib.cairo new file mode 100644 index 0000000000..2c1396270d --- /dev/null +++ b/cairo_programs/cairo-1-contracts/fib.cairo @@ -0,0 +1,11 @@ +#[contract] +mod Fibonacci { + + #[external] + fn fib(a: felt252, b: felt252, n: felt252) -> felt252 { + match n { + 0 => a, + _ => fib(b, a + b, n - 1), + } + } +} diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs new file mode 100644 index 0000000000..bd0ffa04a9 --- /dev/null +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -0,0 +1,14 @@ +use crate::tests::*; + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn fibonacci_1() { + let program_data = include_bytes!("../../cairo_programs/cairo-1-contracts/fib.casm"); + run_cairo_1_entrypoint( + program_data.as_slice(), + 0, + &vec![1_usize.into(), 1_usize.into(), 1_usize.into()], + &vec![1_usize.into()], + true, + ); +} From c4fbe31034b2e5136f4a7784a8f3e291e33e3693 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:35:30 -0300 Subject: [PATCH 20/84] Add another fibonacci case --- src/tests/cairo_1_run_from_entrypoint_tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index bd0ffa04a9..0f195ca7a0 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -12,3 +12,16 @@ fn fibonacci_1() { true, ); } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn fibonacci_3() { + let program_data = include_bytes!("../../cairo_programs/cairo-1-contracts/fib.casm"); + run_cairo_1_entrypoint( + program_data.as_slice(), + 0, + &vec![3_usize.into(), 3_usize.into(), 3_usize.into()], + &vec![9_usize.into()], + true, + ); +} From 5debb706bb20506b8d40af50e5a2b7e52b18b2e1 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:36:19 -0300 Subject: [PATCH 21/84] Always verify secure --- src/tests/cairo_1_run_from_entrypoint_tests.rs | 2 -- src/tests/mod.rs | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index 0f195ca7a0..2357d54b50 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -9,7 +9,6 @@ fn fibonacci_1() { 0, &vec![1_usize.into(), 1_usize.into(), 1_usize.into()], &vec![1_usize.into()], - true, ); } @@ -22,6 +21,5 @@ fn fibonacci_3() { 0, &vec![3_usize.into(), 3_usize.into(), 3_usize.into()], &vec![9_usize.into()], - true, ); } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3b1e222b0d..8f765fae03 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -94,7 +94,6 @@ pub(self) fn run_cairo_1_entrypoint( entrypoint_offset: usize, args: &Vec, expected_retdata: &Vec, - verify_secure: bool, ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); let mut hint_processor = Cairo1HintProcessor::new(&contract_class.hints); @@ -177,7 +176,7 @@ pub(self) fn run_cairo_1_entrypoint( .run_from_entrypoint( entrypoint_offset, &entrypoint_args, - verify_secure, + true, Some(runner.program.shared_program_data.data.len() + program_extra_data.len()), &mut vm, &mut hint_processor, From 4d614a814d76a8b4cbba4be0bcb4827453050450 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 11:39:24 -0300 Subject: [PATCH 22/84] Clippy --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- src/tests/mod.rs | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 9e96ac2a1e..dd11acef18 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -141,7 +141,7 @@ fn as_cairo_short_string(value: &Felt252) -> Option { } impl Cairo1HintProcessor { - pub fn new(hints: &Vec<(usize, Vec)>) -> Self { + pub fn new(hints: &[(usize, Vec)]) -> Self { Self { hints: hints.iter().cloned().collect(), } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 8f765fae03..cfb99ce711 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -128,15 +128,14 @@ pub(self) fn run_cairo_1_entrypoint( .get_builtin_runners() .iter() .filter(|b| builtins.contains(&(b.name().to_string()))) - .map(|b| b.initial_stack()) - .flatten() + .flat_map(|b| b.initial_stack()) .collect(); let initial_gas = MaybeRelocatable::from(usize::MAX); let mut implicit_args = builtin_segment; implicit_args.extend([initial_gas]); - implicit_args.extend([syscall_segment.clone()]); + implicit_args.extend([syscall_segment]); // Other args From df1bf8f7788a910b4a834c853b280972036878e8 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 12:51:38 -0300 Subject: [PATCH 23/84] Compile casm contracts when running test target --- Makefile | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c2239e092a..cdf40d8ada 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,21 @@ COMPILED_BAD_TESTS:=$(patsubst $(BAD_TEST_DIR)/%.cairo, $(BAD_TEST_DIR)/%.json, $(BAD_TEST_DIR)/%.json: $(BAD_TEST_DIR)/%.cairo cairo-compile $< --output $@ +# ====================== +# Test Cairo 1 Contracts +# ====================== + +CAIRO_1_CONTRACTS_TEST_DIR=cairo_programs/cairo-1-contracts +CAIRO_1_CONTRACTS_TEST_CAIRO_FILES:=$(wildcard $(CAIRO_1_CONTRACTS_TEST_DIR)/*.cairo) +COMPILED_SIERRA_CONTRACTS:=$(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo, $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra, $(CAIRO_1_CONTRACTS_TEST_CAIRO_FILES)) +COMPILED_CASM_CONTRACTS:= $(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra, $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm, $(COMPILED_SIERRA_CONTRACTS)) + +$(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo + cairo/target/debug/starknet-compile -- $< $@ + +$(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra + cairo/target/debug/starknet-sierra-compile -- $< $@ + cargo-deps: cargo install --version 1.1.0 cargo-criterion cargo install --version 0.6.1 flamegraph @@ -141,7 +156,7 @@ cairo-rs_proof_trace: $(CAIRO_RS_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF) cairo_trace: $(CAIRO_TRACE) $(CAIRO_MEM) cairo-rs_trace: $(CAIRO_RS_TRACE) $(CAIRO_RS_MEM) -test: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) +test: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) $(COMPILED_CASM_CONTRACTS) cargo llvm-cov nextest --no-report --workspace --features test_utils test-no_std: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) cargo llvm-cov nextest --no-report --workspace --features test_utils --no-default-features @@ -201,6 +216,8 @@ clean: rm -f $(TEST_DIR)/*.trace rm -f $(BENCH_DIR)/*.json rm -f $(BAD_TEST_DIR)/*.json + rm -f $(CAIRO_1_CONTRACTS_TEST_DIR)/*.sierra + rm -f $(CAIRO_1_CONTRACTS_TEST_DIR)/*.casm rm -f $(TEST_PROOF_DIR)/*.json rm -f $(TEST_PROOF_DIR)/*.memory rm -f $(TEST_PROOF_DIR)/*.trace From 59b638102ab2bcd211196573f1b289328ccc9503 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 13:15:33 -0300 Subject: [PATCH 24/84] Remove unwrap from cell_ref_to_relocatable --- .../cairo_1_hint_processor/hint_processor.rs | 74 ++++++++++--------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index dd11acef18..fcb7449f7a 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -3,6 +3,7 @@ use crate::any_box; use crate::felt::{felt_str, Felt252}; use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope; use crate::hint_processor::hint_processor_definition::HintReference; +use crate::types::errors::math_errors::MathError; use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, @@ -66,17 +67,20 @@ fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> Ok((cell, base_offset)) } -fn cell_ref_to_relocatable(cell_ref: &CellRef, vm: &VirtualMachine) -> Relocatable { +fn cell_ref_to_relocatable( + cell_ref: &CellRef, + vm: &VirtualMachine, +) -> Result { let base = match cell_ref.register { Register::AP => vm.get_ap(), Register::FP => vm.get_fp(), }; - (base + (cell_ref.offset as i32)).unwrap() + base + (cell_ref.offset as i32) } fn get_cell_val(vm: &VirtualMachine, cell: &CellRef) -> Result { Ok(vm - .get_integer(cell_ref_to_relocatable(cell, vm))? + .get_integer(cell_ref_to_relocatable(cell, vm)?)? .as_ref() .clone()) } @@ -86,7 +90,7 @@ fn get_ptr( cell: &CellRef, offset: &Felt252, ) -> Result { - Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm))? + offset)?) + Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm)?)? + offset)?) } fn as_relocatable(vm: &mut VirtualMachine, value: &ResOperand) -> Result { @@ -277,7 +281,7 @@ impl Cairo1HintProcessor { fn alloc_segment(&self, vm: &mut VirtualMachine, dst: &CellRef) -> Result<(), HintError> { let segment = vm.add_memory_segment(); - vm.insert_value(cell_ref_to_relocatable(dst, vm), segment) + vm.insert_value(cell_ref_to_relocatable(dst, vm)?, segment) .map_err(HintError::from) } @@ -297,7 +301,7 @@ impl Cairo1HintProcessor { }; vm.insert_value( - cell_ref_to_relocatable(dst, vm), + cell_ref_to_relocatable(dst, vm)?, MaybeRelocatable::from(result), ) .map_err(HintError::from) @@ -312,7 +316,7 @@ impl Cairo1HintProcessor { let value = res_operand_get_val(vm, value)?; let result = value.sqrt(); vm.insert_value( - cell_ref_to_relocatable(dst, vm), + cell_ref_to_relocatable(dst, vm)?, MaybeRelocatable::from(result), ) .map_err(HintError::from) @@ -334,7 +338,7 @@ impl Cairo1HintProcessor { }; vm.insert_value( - cell_ref_to_relocatable(dst, vm), + cell_ref_to_relocatable(dst, vm)?, MaybeRelocatable::from(result), ) .map_err(HintError::from) @@ -400,7 +404,7 @@ impl Cairo1HintProcessor { .get_from_tracker(dict_address, &key) .unwrap_or_else(|| DictManagerExecScope::DICT_DEFAULT_VALUE.into()); - vm.insert_value(cell_ref_to_relocatable(value_dst, vm), value) + vm.insert_value(cell_ref_to_relocatable(value_dst, vm)?, value) .map_err(HintError::from) } @@ -417,11 +421,11 @@ impl Cairo1HintProcessor { let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); let remainder_value = Felt252::new(lhs_value % rhs_value); vm.insert_value( - cell_ref_to_relocatable(quotient, vm), + cell_ref_to_relocatable(quotient, vm)?, MaybeRelocatable::from(quotient_value), )?; vm.insert_value( - cell_ref_to_relocatable(remainder, vm), + cell_ref_to_relocatable(remainder, vm)?, MaybeRelocatable::from(remainder_value), ) .map_err(HintError::from) @@ -441,7 +445,7 @@ impl Cairo1HintProcessor { let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); vm.insert_value( - cell_ref_to_relocatable(dict_index, vm), + cell_ref_to_relocatable(dict_index, vm)?, Felt252::from(dict_infos_index), ) .map_err(HintError::from) @@ -477,33 +481,33 @@ impl Cairo1HintProcessor { // Guess quotient limbs. let (quotient, limb) = quotient.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(quotient0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(quotient0, vm)?, limb)?; let (quotient, limb) = quotient.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(quotient1, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(quotient1, vm)?, limb)?; let (quotient, limb) = quotient.div_rem(&pow_2_64); if divisor_high.is_zero() { - vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; - vm.insert_value(cell_ref_to_relocatable(extra1, vm), quotient)?; + vm.insert_value(cell_ref_to_relocatable(extra0, vm)?, limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm)?, quotient)?; } // Guess divisor limbs. let (divisor, limb) = divisor.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(divisor0, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(divisor0, vm)?, limb)?; let (divisor, limb) = divisor.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(divisor1, vm), limb)?; + vm.insert_value(cell_ref_to_relocatable(divisor1, vm)?, limb)?; let (divisor, limb) = divisor.div_rem(&pow_2_64); if !divisor_high.is_zero() { - vm.insert_value(cell_ref_to_relocatable(extra0, vm), limb)?; - vm.insert_value(cell_ref_to_relocatable(extra1, vm), divisor)?; + vm.insert_value(cell_ref_to_relocatable(extra0, vm)?, limb)?; + vm.insert_value(cell_ref_to_relocatable(extra1, vm)?, divisor)?; } // Guess remainder limbs. vm.insert_value( - cell_ref_to_relocatable(remainder_low, vm), + cell_ref_to_relocatable(remainder_low, vm)?, remainder.clone() % pow_2_128.clone(), )?; vm.insert_value( - cell_ref_to_relocatable(remainder_high, vm), + cell_ref_to_relocatable(remainder_high, vm)?, remainder / pow_2_128, )?; Ok(()) @@ -522,7 +526,7 @@ impl Cairo1HintProcessor { Felt252::from(0) }; - vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm), val)?; + vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm)?, val)?; Ok(()) } @@ -541,9 +545,9 @@ impl Cairo1HintProcessor { let x_value = (value.clone() / scalar.clone()).min(max_x); let y_value = value - x_value.clone() * scalar; - vm.insert_value(cell_ref_to_relocatable(x, vm), x_value) + vm.insert_value(cell_ref_to_relocatable(x, vm)?, x_value) .map_err(HintError::from)?; - vm.insert_value(cell_ref_to_relocatable(y, vm), y_value) + vm.insert_value(cell_ref_to_relocatable(y, vm)?, y_value) .map_err(HintError::from)?; Ok(()) @@ -573,8 +577,8 @@ impl Cairo1HintProcessor { .into_bigint() .into(); - vm.insert_value(cell_ref_to_relocatable(x, vm), Felt252::from(x_bigint))?; - vm.insert_value(cell_ref_to_relocatable(y, vm), Felt252::from(y_bigint))?; + vm.insert_value(cell_ref_to_relocatable(x, vm)?, Felt252::from(x_bigint))?; + vm.insert_value(cell_ref_to_relocatable(y, vm)?, Felt252::from(y_bigint))?; Ok(()) } @@ -590,7 +594,7 @@ impl Cairo1HintProcessor { dict_squash_exec_scope.pop_current_key(); if let Some(current_key) = dict_squash_exec_scope.current_key() { return vm - .insert_value(cell_ref_to_relocatable(next_key, vm), current_key) + .insert_value(cell_ref_to_relocatable(next_key, vm)?, current_key) .map_err(HintError::from); } Err(HintError::KeyNotFound) @@ -645,7 +649,7 @@ impl Cairo1HintProcessor { Felt252::from(0) }; - vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm), val)?; + vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm)?, val)?; Ok(()) } @@ -697,21 +701,21 @@ impl Cairo1HintProcessor { sqrt.clone() * Felt252::from(2u32) - remainder.clone() >= pow_2_128; let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); - vm.insert_value(cell_ref_to_relocatable(sqrt0, vm), sqrt0_val)?; - vm.insert_value(cell_ref_to_relocatable(sqrt1, vm), sqrt1_val)?; + vm.insert_value(cell_ref_to_relocatable(sqrt0, vm)?, sqrt0_val)?; + vm.insert_value(cell_ref_to_relocatable(sqrt1, vm)?, sqrt1_val)?; let (remainder_high_val, remainder_low_val) = remainder.div_rem(&pow_2_128); vm.insert_value( - cell_ref_to_relocatable(remainder_low, vm), + cell_ref_to_relocatable(remainder_low, vm)?, remainder_low_val, )?; vm.insert_value( - cell_ref_to_relocatable(remainder_high, vm), + cell_ref_to_relocatable(remainder_high, vm)?, remainder_high_val, )?; vm.insert_value( - cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm), + cell_ref_to_relocatable(sqrt_mul_2_minus_remainder_ge_u128, vm)?, usize::from(sqrt_mul_2_minus_remainder_ge_u128_val), )?; @@ -787,7 +791,7 @@ impl Cairo1HintProcessor { Felt252::from(1) }; - vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm), val)?; + vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm)?, val)?; Ok(()) } From 2649829719e33a6d4b4d4c31aa0adcc54984d626 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 14:51:50 -0300 Subject: [PATCH 25/84] Remove paniking macro from extract_buffer --- Cargo.lock | 203 ++---------------- Cargo.toml | 1 - .../cairo_1_hint_processor/hint_processor.rs | 21 +- 3 files changed, 28 insertions(+), 197 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0ba529f459..60ec0b9881 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,15 +33,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - [[package]] name = "anstream" version = "0.3.1" @@ -354,7 +345,7 @@ name = "cairo-lang-casm" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "indoc", "num-bigint", "num-traits 0.2.15", @@ -379,7 +370,7 @@ dependencies = [ "cairo-lang-sierra", "cairo-lang-sierra-generator", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "clap 4.2.5", "log", "salsa", @@ -402,7 +393,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-parser", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "indexmap", "itertools", "salsa", @@ -415,7 +406,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-filesystem", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "itertools", "salsa", ] @@ -425,7 +416,7 @@ name = "cairo-lang-eq-solver" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "good_lp", "indexmap", "itertools", @@ -437,7 +428,7 @@ version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ "cairo-lang-debug", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "path-clean", "salsa", "serde", @@ -457,7 +448,7 @@ dependencies = [ "cairo-lang-proc-macros", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "id-arena", "indexmap", "itertools", @@ -477,7 +468,7 @@ dependencies = [ "cairo-lang-filesystem", "cairo-lang-syntax", "cairo-lang-syntax-codegen", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "colored", "itertools", "log", @@ -499,7 +490,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-semantic", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "indoc", "itertools", "salsa", @@ -540,7 +531,7 @@ dependencies = [ "cairo-lang-parser", "cairo-lang-proc-macros", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "id-arena", "itertools", "log", @@ -555,7 +546,7 @@ name = "cairo-lang-sierra" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "const-fnv1a-hash", "convert_case", "derivative", @@ -579,7 +570,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "itertools", "thiserror", ] @@ -591,7 +582,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-eq-solver", "cairo-lang-sierra", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "itertools", "thiserror", ] @@ -612,7 +603,7 @@ dependencies = [ "cairo-lang-semantic", "cairo-lang-sierra", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "id-arena", "indexmap", "itertools", @@ -633,7 +624,7 @@ dependencies = [ "cairo-lang-sierra", "cairo-lang-sierra-ap-change", "cairo-lang-sierra-gas", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "clap 4.2.5", "indoc", "itertools", @@ -665,7 +656,7 @@ dependencies = [ "cairo-lang-sierra-generator", "cairo-lang-sierra-to-casm", "cairo-lang-syntax", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "clap 4.2.5", "convert_case", "genco", @@ -690,7 +681,7 @@ source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81c dependencies = [ "cairo-lang-debug", "cairo-lang-filesystem", - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "num-bigint", "num-traits 0.2.15", "salsa", @@ -704,25 +695,12 @@ name = "cairo-lang-syntax-codegen" version = "1.0.0-alpha.7" source = "git+https://github.com/starkware-libs/cairo.git?rev=a08bedbf26b711e81cec8b7660698a3788e67d46#a08bedbf26b711e81cec8b7660698a3788e67d46" dependencies = [ - "cairo-lang-utils 1.0.0-alpha.7", + "cairo-lang-utils", "genco", "log", "xshell", ] -[[package]] -name = "cairo-lang-utils" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7196aada3418cb09e7aa2b39f0a6bb660e09adccf565d340457bbcbeef439106" -dependencies = [ - "chrono", - "env_logger", - "indexmap", - "itertools", - "log", -] - [[package]] name = "cairo-lang-utils" version = "1.0.0-alpha.7" @@ -736,7 +714,7 @@ dependencies = [ "num-integer", "num-traits 0.2.15", "serde", - "time 0.3.20", + "time", ] [[package]] @@ -760,7 +738,6 @@ dependencies = [ "cairo-felt 0.3.0-rc1", "cairo-lang-casm", "cairo-lang-starknet", - "cairo-lang-utils 0.1.0", "cairo-take_until_unbalanced", "criterion", "generic-array", @@ -823,21 +800,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "chrono" -version = "0.4.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" -dependencies = [ - "iana-time-zone", - "js-sys", - "num-integer", - "num-traits 0.2.15", - "time 0.1.45", - "wasm-bindgen", - "winapi", -] - [[package]] name = "ci_info" version = "0.10.2" @@ -939,16 +901,6 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1" -[[package]] -name = "codespan-reporting" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" -dependencies = [ - "termcolor", - "unicode-width", -] - [[package]] name = "colorchoice" version = "1.0.0" @@ -991,12 +943,6 @@ dependencies = [ "unicode-segmentation", ] -[[package]] -name = "core-foundation-sys" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" - [[package]] name = "cpufeatures" version = "0.2.6" @@ -1133,50 +1079,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "cxx" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f61f1b6389c3fe1c316bf8a4dccc90a38208354b330925bce1f74a6c4756eb93" -dependencies = [ - "cc", - "cxxbridge-flags", - "cxxbridge-macro", - "link-cplusplus", -] - -[[package]] -name = "cxx-build" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12cee708e8962df2aeb38f594aae5d827c022b6460ac71a7a3e2c3c2aae5a07b" -dependencies = [ - "cc", - "codespan-reporting", - "once_cell", - "proc-macro2", - "quote", - "scratch", - "syn 2.0.13", -] - -[[package]] -name = "cxxbridge-flags" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7944172ae7e4068c533afbb984114a56c46e9ccddda550499caa222902c7f7bb" - -[[package]] -name = "cxxbridge-macro" -version = "1.0.94" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2345488264226bf682893e25de0769f3360aac9957980ec49361b083ddaa5bc5" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.13", -] - [[package]] name = "derivative" version = "2.2.0" @@ -1472,7 +1374,7 @@ dependencies = [ "cfg-if", "js-sys", "libc", - "wasi 0.11.0+wasi-snapshot-preview1", + "wasi", "wasm-bindgen", ] @@ -1586,30 +1488,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71a816c97c42258aa5834d07590b718b4c9a598944cd39a52dc25b351185d678" -[[package]] -name = "iana-time-zone" -version = "0.1.56" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" -dependencies = [ - "cxx", - "cxx-build", -] - [[package]] name = "id-arena" version = "2.2.1" @@ -1761,15 +1639,6 @@ dependencies = [ "libc", ] -[[package]] -name = "link-cplusplus" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" -dependencies = [ - "cc", -] - [[package]] name = "linux-raw-sys" version = "0.3.1" @@ -2512,12 +2381,6 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "scratch" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" - [[package]] name = "semver" version = "0.11.0" @@ -2853,17 +2716,6 @@ dependencies = [ "thiserror-impl-no-std", ] -[[package]] -name = "time" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" -dependencies = [ - "libc", - "wasi 0.10.0+wasi-snapshot-preview1", - "winapi", -] - [[package]] name = "time" version = "0.3.20" @@ -3018,12 +2870,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -3161,15 +3007,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" -[[package]] -name = "windows" -version = "0.48.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" -dependencies = [ - "windows-targets 0.48.0", -] - [[package]] name = "windows-sys" version = "0.45.0" diff --git a/Cargo.toml b/Cargo.toml index e397b3af9c..9861471fbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,6 @@ cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46" } # TODO: check these dependencies for wasm compatibility -cairo-lang-utils = "0.1.0" ark-ff = "0.4.0-alpha.7" ark-std = "0.3.0" diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index fcb7449f7a..669a2aa21f 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -17,9 +17,8 @@ use ark_ff::{Field, PrimeField}; use ark_std::UniformRand; use cairo_lang_casm::{ hints::{CoreHint, Hint}, - operand::{BinOpOperand, CellRef, DerefOrImmediate, Operation, Register, ResOperand}, + operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand}, }; -use cairo_lang_utils::extract_matches; use num_bigint::BigUint; use num_integer::Integer; use num_traits::cast::ToPrimitive; @@ -47,17 +46,13 @@ pub struct Cairo1HintProcessor { fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { let (cell, base_offset) = match buffer { ResOperand::Deref(cell) => (cell, 0.into()), - ResOperand::BinOp(BinOpOperand { - op: Operation::Add, - a, - b, - }) => ( - a, - extract_matches!(b, DerefOrImmediate::Immediate) - .clone() - .value - .into(), - ), + ResOperand::BinOp(bin_op) => { + if let DerefOrImmediate::Immediate(val) = &bin_op.b { + (&bin_op.a, val.clone().value.into()) + } else { + return Err(HintError::CustomHint("Failed to extract buffer, expected ResOperand of BinOp type to have Inmediate b value".to_owned())); + } + } _ => { return Err(HintError::CustomHint( "Illegal argument for a buffer.".to_string(), From ecb58d63a03d0835279ef62c6d1fc6f16e9de1ac Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 14:56:06 -0300 Subject: [PATCH 26/84] Misc improvements --- .../cairo_1_hint_processor/hint_processor.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 669a2aa21f..a4dc18db3b 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -289,17 +289,10 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let lhs_value = res_operand_get_val(vm, lhs)?; let rhs_value = res_operand_get_val(vm, rhs)?; - let result = if lhs_value < rhs_value { - Felt252::from(1) - } else { - Felt252::from(0) - }; + let result = Felt252::from((lhs_value < rhs_value) as u8); - vm.insert_value( - cell_ref_to_relocatable(dst, vm)?, - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) + vm.insert_value(cell_ref_to_relocatable(dst, vm)?, result) + .map_err(HintError::from) } fn square_root( From 2d9362d43fa2ce5550f4a22f97fa465f9732b77e Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 14:58:31 -0300 Subject: [PATCH 27/84] Misc improvements --- .../cairo_1_hint_processor/hint_processor.rs | 20 +++++-------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index a4dc18db3b..d68a913dc3 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -303,11 +303,8 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let value = res_operand_get_val(vm, value)?; let result = value.sqrt(); - vm.insert_value( - cell_ref_to_relocatable(dst, vm)?, - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) + vm.insert_value(cell_ref_to_relocatable(dst, vm)?, result) + .map_err(HintError::from) } fn test_less_than_or_equal( @@ -319,17 +316,10 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let lhs_value = res_operand_get_val(vm, lhs)?; let rhs_value = res_operand_get_val(vm, rhs)?; - let result = if lhs_value <= rhs_value { - Felt252::from(1) - } else { - Felt252::from(0) - }; + let result = Felt252::from((lhs_value <= rhs_value) as u8); - vm.insert_value( - cell_ref_to_relocatable(dst, vm)?, - MaybeRelocatable::from(result), - ) - .map_err(HintError::from) + vm.insert_value(cell_ref_to_relocatable(dst, vm)?, result) + .map_err(HintError::from) } fn assert_le_find_small_arcs( From 38be92cee9d08c199060e17429d7be6dd2359387 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:02:39 -0300 Subject: [PATCH 28/84] Misc improvements --- .../cairo_1_hint_processor/hint_processor.rs | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index d68a913dc3..f09a387f9b 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -7,7 +7,6 @@ use crate::types::errors::math_errors::MathError; use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, - types::relocatable::MaybeRelocatable, types::relocatable::Relocatable, vm::errors::vm_errors::VirtualMachineError, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, @@ -398,15 +397,9 @@ impl Cairo1HintProcessor { let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); let remainder_value = Felt252::new(lhs_value % rhs_value); - vm.insert_value( - cell_ref_to_relocatable(quotient, vm)?, - MaybeRelocatable::from(quotient_value), - )?; - vm.insert_value( - cell_ref_to_relocatable(remainder, vm)?, - MaybeRelocatable::from(remainder_value), - ) - .map_err(HintError::from) + vm.insert_value(cell_ref_to_relocatable(quotient, vm)?, quotient_value)?; + vm.insert_value(cell_ref_to_relocatable(remainder, vm)?, remainder_value) + .map_err(HintError::from) } fn get_segment_arena_index( @@ -422,11 +415,8 @@ impl Cairo1HintProcessor { exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); - vm.insert_value( - cell_ref_to_relocatable(dict_index, vm)?, - Felt252::from(dict_infos_index), - ) - .map_err(HintError::from) + vm.insert_value(cell_ref_to_relocatable(dict_index, vm)?, dict_infos_index) + .map_err(HintError::from) } #[allow(clippy::too_many_arguments)] @@ -453,8 +443,8 @@ impl Cairo1HintProcessor { let divisor_low = res_operand_get_val(vm, divisor_low)?; let divisor_high = res_operand_get_val(vm, divisor_high)?; let dividend = dividend_low + dividend_high.mul(pow_2_128.clone()); - let divisor = divisor_low + divisor_high.clone() * pow_2_128.clone(); - let quotient = dividend.clone() / divisor.clone(); + let divisor = divisor_low + (&divisor_high * &pow_2_128); + let quotient = ÷nd / &divisor; let remainder = dividend % divisor.clone(); // Guess quotient limbs. @@ -498,12 +488,7 @@ impl Cairo1HintProcessor { exec_scopes: &mut ExecutionScopes, ) -> Result<(), HintError> { let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; - let val = if excluded_arc != 0 { - Felt252::from(1) - } else { - Felt252::from(0) - }; - + let val = Felt252::from((excluded_arc != 0) as u8); vm.insert_value(cell_ref_to_relocatable(skip_exclude_a_flag, vm)?, val)?; Ok(()) } @@ -520,8 +505,8 @@ impl Cairo1HintProcessor { let value = res_operand_get_val(vm, value)?; let scalar = res_operand_get_val(vm, scalar)?; let max_x = res_operand_get_val(vm, max_x)?; - let x_value = (value.clone() / scalar.clone()).min(max_x); - let y_value = value - x_value.clone() * scalar; + let x_value = (&value / &scalar).min(max_x); + let y_value = value - &x_value * &scalar; vm.insert_value(cell_ref_to_relocatable(x, vm)?, x_value) .map_err(HintError::from)?; From a82308b0c960f0c914adf83c9ae6999e19af3463 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:08:43 -0300 Subject: [PATCH 29/84] Misc improvements --- .../cairo_1_hint_processor/hint_processor.rs | 31 +++++++------------ 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index f09a387f9b..18c94dfeaa 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -395,7 +395,7 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let lhs_value = res_operand_get_val(vm, lhs)?.to_biguint(); let rhs_value = res_operand_get_val(vm, rhs)?.to_biguint(); - let quotient_value = Felt252::new(lhs_value.clone() / rhs_value.clone()); + let quotient_value = Felt252::new(&lhs_value / &rhs_value); let remainder_value = Felt252::new(lhs_value % rhs_value); vm.insert_value(cell_ref_to_relocatable(quotient, vm)?, quotient_value)?; vm.insert_value(cell_ref_to_relocatable(remainder, vm)?, remainder_value) @@ -606,11 +606,7 @@ impl Cairo1HintProcessor { exec_scopes: &mut ExecutionScopes, ) -> Result<(), HintError> { let excluded_arc: i32 = exec_scopes.get("excluded_arc")?; - let val = if excluded_arc != 1 { - Felt252::from(1) - } else { - Felt252::from(0) - }; + let val = Felt252::from((excluded_arc != 1) as u8); vm.insert_value(cell_ref_to_relocatable(skip_exclude_b_minus_a, vm)?, val)?; @@ -657,11 +653,11 @@ impl Cairo1HintProcessor { let pow_2_64 = Felt252::from(u64::MAX) + 1u32; let value_low = res_operand_get_val(vm, value_low)?; let value_high = res_operand_get_val(vm, value_high)?; - let value = value_low + value_high * pow_2_128.clone(); + let value = value_low + value_high * &pow_2_128; let sqrt = value.sqrt(); - let remainder = value - sqrt.clone() * sqrt.clone(); + let remainder = value - &sqrt * &sqrt; let sqrt_mul_2_minus_remainder_ge_u128_val = - sqrt.clone() * Felt252::from(2u32) - remainder.clone() >= pow_2_128; + &sqrt * &Felt252::from(2u32) - &remainder >= pow_2_128; let (sqrt1_val, sqrt0_val) = sqrt.div_rem(&pow_2_64); vm.insert_value(cell_ref_to_relocatable(sqrt0, vm)?, sqrt0_val)?; @@ -743,16 +739,13 @@ impl Cairo1HintProcessor { let dict_squash_exec_scope: &mut DictSquashExecScope = exec_scopes.get_mut_ref("dict_squash_exec_scope")?; - let val = if dict_squash_exec_scope - .current_access_indices() - .ok_or(HintError::CustomHint("no indices accessed".to_string()))? - .len() - > 1 - { - Felt252::from(0) - } else { - Felt252::from(1) - }; + let val = Felt252::from( + (dict_squash_exec_scope + .current_access_indices() + .ok_or(HintError::CustomHint("no indices accessed".to_string()))? + .len() + > 1) as u8, + ); vm.insert_value(cell_ref_to_relocatable(should_skip_loop, vm)?, val)?; From 8d6c5cbd35b2160b65a1ae3b07bf09b0926076a2 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:25:33 -0300 Subject: [PATCH 30/84] Remove unwraps & asserts from DictSquashExecScope::pop_current_key --- .../cairo_1_hint_processor/dict_manager.rs | 32 ++++++++++++++----- .../cairo_1_hint_processor/hint_processor.rs | 2 +- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index 93e57b58f4..c2041c0ba0 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -1,6 +1,9 @@ use std::collections::HashMap; +use num_traits::One; + use crate::felt::Felt252; +use crate::vm::errors::hint_errors::HintError; use crate::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; /// Stores the data of a specific dictionary. @@ -94,15 +97,28 @@ impl DictSquashExecScope { self.keys.last().cloned() } - /// Returns and removes the current key, and its access indices. Should be called when only the + /// Removes the current key, and its access indices. Should be called when only the /// last key access is in the corresponding indices list. - pub fn pop_current_key(&mut self) -> Option { - let key_accesses = self.access_indices.remove(&self.current_key().unwrap()); - assert!( - key_accesses.unwrap().len() == 1, - "Key popped but not all accesses were processed." - ); - self.keys.pop() + pub fn pop_current_key(&mut self) -> Result<(), HintError> { + let current_key = self + .current_key() + .ok_or(HintError::CustomHint(String::from( + "Failed to get current key", + )))?; + let key_accesses = + self.access_indices + .remove(¤t_key) + .ok_or(HintError::CustomHint(format!( + "No key accesses for key {}", + current_key + )))?; + if !key_accesses.len().is_one() { + return Err(HintError::CustomHint(String::from( + "Key popped but not all accesses were processed.", + ))); + } + self.keys.pop(); + Ok(()) } /// Returns a reference to the access indices list of the current key. diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 18c94dfeaa..59814a227e 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -554,7 +554,7 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let dict_squash_exec_scope: &mut DictSquashExecScope = exec_scopes.get_mut_ref("dict_squash_exec_scope")?; - dict_squash_exec_scope.pop_current_key(); + dict_squash_exec_scope.pop_current_key()?; if let Some(current_key) = dict_squash_exec_scope.current_key() { return vm .insert_value(cell_ref_to_relocatable(next_key, vm)?, current_key) From 4e7f2e01609a416cceaa360466c980c37e75de40 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:29:33 -0300 Subject: [PATCH 31/84] Remove unwraps & asserts from DictManagerExecScope::new_default_dict --- .../cairo_1_hint_processor/dict_manager.rs | 26 +++++++++++-------- .../cairo_1_hint_processor/hint_processor.rs | 2 +- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index c2041c0ba0..bbd90819e3 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -36,18 +36,22 @@ impl DictManagerExecScope { pub const DICT_DEFAULT_VALUE: usize = 0; /// Allocates a new segment for a new dictionary and return the start of the segment. - pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Relocatable { + pub fn new_default_dict(&mut self, vm: &mut VirtualMachine) -> Result { let dict_segment = vm.add_memory_segment(); - assert!( - self.trackers - .insert( - dict_segment.segment_index, - DictTrackerExecScope::new(self.trackers.len()) - ) - .is_none(), - "Segment index already in use." - ); - dict_segment + if self + .trackers + .insert( + dict_segment.segment_index, + DictTrackerExecScope::new(self.trackers.len()), + ) + .is_some() + { + return Err(HintError::CustomHint(String::from( + "Segment index already in use.", + ))); + } + + Ok(dict_segment) } /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 59814a227e..88bfd05e7c 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -593,7 +593,7 @@ impl Cairo1HintProcessor { exec_scopes.get_mut_ref::("dict_manager_exec_scope")? } }; - let new_dict_segment = dict_manager_exec_scope.new_default_dict(vm); + let new_dict_segment = dict_manager_exec_scope.new_default_dict(vm)?; vm.insert_value((dict_infos_base + 3 * n_dicts)?, new_dict_segment)?; Ok(()) From 75218c8457639c3e615f65eaa953a97d9d2463f7 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:32:20 -0300 Subject: [PATCH 32/84] Remove expect from get_dict_tracker --- .../cairo_1_hint_processor/dict_manager.rs | 12 +++++++----- .../cairo_1_hint_processor/hint_processor.rs | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index bbd90819e3..a2eaf42c7d 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -55,10 +55,12 @@ impl DictManagerExecScope { } /// Returns a reference for a dict tracker corresponding to a given pointer to a dict segment. - fn get_dict_tracker(&self, dict_end: Relocatable) -> &DictTrackerExecScope { + fn get_dict_tracker(&self, dict_end: Relocatable) -> Result<&DictTrackerExecScope, HintError> { self.trackers .get(&dict_end.segment_index) - .expect("The given value does not point to a known dictionary.") + .ok_or(HintError::CustomHint(String::from( + "The given value does not point to a known dictionary.", + ))) } /// Returns a mut reference for a dict tracker corresponding to a given pointer to a dict @@ -70,8 +72,8 @@ impl DictManagerExecScope { } /// Returns the index of the dict tracker corresponding to a given pointer to a dict segment. - pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> usize { - self.get_dict_tracker(dict_end).idx + pub fn get_dict_infos_index(&self, dict_end: Relocatable) -> Result { + Ok(self.get_dict_tracker(dict_end)?.idx) } /// Inserts a value to the dict tracker corresponding to a given pointer to a dict segment. @@ -82,7 +84,7 @@ impl DictManagerExecScope { /// Gets a value from the dict tracker corresponding to a given pointer to a dict segment. /// None if the key does not exist in the tracker data. pub fn get_from_tracker(&self, dict_end: Relocatable, key: &Felt252) -> Option { - self.get_dict_tracker(dict_end).data.get(key).cloned() + self.get_dict_tracker(dict_end).ok()?.data.get(key).cloned() } } diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 88bfd05e7c..898afa944f 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -414,7 +414,7 @@ impl Cairo1HintProcessor { let dict_manager_exec_scope = exec_scopes.get_mut_ref::("dict_manager_exec_scope")?; - let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address); + let dict_infos_index = dict_manager_exec_scope.get_dict_infos_index(dict_address)?; vm.insert_value(cell_ref_to_relocatable(dict_index, vm)?, dict_infos_index) .map_err(HintError::from) } From b06f563e37c73adc72c840ac7db92a170424c1cb Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:39:59 -0300 Subject: [PATCH 33/84] Add constants for cairo 1 compiler binaries in makefile --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cdf40d8ada..9c7bc91ef1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ RELBIN:=target/release/cairo-rs-run DBGBIN:=target/debug/cairo-rs-run +STARKNET_COMPILE:=cairo/target/release/starknet-compile +STARKNET_SIERRA_COMPILE:=cairo/target/release/starknet-compile .PHONY: deps deps-macos cargo-deps build run check test clippy coverage benchmark flamegraph \ compare_benchmarks_deps compare_benchmarks docs clean \ @@ -99,10 +101,10 @@ COMPILED_SIERRA_CONTRACTS:=$(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo, $(C COMPILED_CASM_CONTRACTS:= $(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra, $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm, $(COMPILED_SIERRA_CONTRACTS)) $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo - cairo/target/debug/starknet-compile -- $< $@ + STARKNET_COMPILE -- $< $@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra - cairo/target/debug/starknet-sierra-compile -- $< $@ + STARKNET_SIERRA_COMPILE -- $< $@ cargo-deps: cargo install --version 1.1.0 cargo-criterion From 7d543aee953536430075230e29fb2a8b96faa6df Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:45:15 -0300 Subject: [PATCH 34/84] Add cairo 1 compiler to deps target in makefile --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9c7bc91ef1..9fc515a164 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ DBGBIN:=target/debug/cairo-rs-run STARKNET_COMPILE:=cairo/target/release/starknet-compile STARKNET_SIERRA_COMPILE:=cairo/target/release/starknet-compile -.PHONY: deps deps-macos cargo-deps build run check test clippy coverage benchmark flamegraph \ +.PHONY: build-cairo-1-compiler deps deps-macos cargo-deps build run check test clippy coverage benchmark flamegraph \ compare_benchmarks_deps compare_benchmarks docs clean \ compare_vm_output compare_trace_memory compare_trace compare_memory \ compare_trace_memory_proof compare_trace_proof compare_memory_proof \ @@ -106,6 +106,10 @@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra STARKNET_SIERRA_COMPILE -- $< $@ +build-cairo-1-compiler: + git clone https://github.com/starkware-libs/cairo.git + cd cairo; cargo build --release + cargo-deps: cargo install --version 1.1.0 cargo-criterion cargo install --version 0.6.1 flamegraph @@ -116,6 +120,7 @@ cargo-deps: deps: $(MAKE) cargo-deps + $(MAKE) build-cairo-1-compiler pyenv install -s pypy3.9-7.3.9 PYENV_VERSION=pypy3.9-7.3.9 python -m venv cairo-rs-pypy-env . cairo-rs-pypy-env/bin/activate ; \ @@ -127,6 +132,7 @@ deps: deps-macos: $(MAKE) cargo-deps + $(MAKE) build-cairo-1-compiler brew install gmp arch -x86_64 pyenv install -s pypy3.9-7.3.9 PYENV_VERSION=pypy3.9-7.3.9 python -m venv cairo-rs-pypy-env From 47ea449094a09fa4f88e08f726e0face87bad44b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:46:02 -0300 Subject: [PATCH 35/84] Add cairo folder to clean target --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 9fc515a164..c0bc0ea21d 100644 --- a/Makefile +++ b/Makefile @@ -231,3 +231,4 @@ clean: rm -f $(TEST_PROOF_DIR)/*.trace rm -rf cairo-rs-env rm -rf cairo-rs-pypy-env + rm -rf cairo From 6156c0ffdeda5cdcbb357faa52adbe3852dc6962 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 15:56:39 -0300 Subject: [PATCH 36/84] Remove todo from execute method --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 898afa944f..3d7b2f67b5 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -269,7 +269,7 @@ impl Cairo1HintProcessor { Hint::Core(CoreHint::ShouldSkipSquashLoop { should_skip_loop }) => { self.should_skip_squash_loop(vm, exec_scopes, should_skip_loop) } - _ => todo!(), + hint => Err(HintError::UnknownHint(hint.to_string())) } } From a0e28f580b330aa3dcd4247858b666a6ea8a7294 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 16:06:07 -0300 Subject: [PATCH 37/84] Separate helper functions from Cairo1HintProcessor implementation --- .../cairo_1_hint_processor/hint_processor.rs | 111 ++---------------- .../cairo_1_hint_processor/mod.rs | 1 + 2 files changed, 8 insertions(+), 104 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 3d7b2f67b5..7827ee028f 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -1,13 +1,13 @@ use super::dict_manager::DictManagerExecScope; +use super::hint_processor_utils::*; use crate::any_box; use crate::felt::{felt_str, Felt252}; use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope; use crate::hint_processor::hint_processor_definition::HintReference; -use crate::types::errors::math_errors::MathError; + use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, - types::relocatable::Relocatable, vm::errors::vm_errors::VirtualMachineError, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; @@ -16,13 +16,13 @@ use ark_ff::{Field, PrimeField}; use ark_std::UniformRand; use cairo_lang_casm::{ hints::{CoreHint, Hint}, - operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand}, + operand::{CellRef, ResOperand}, }; +use core::ops::Mul; use num_bigint::BigUint; use num_integer::Integer; -use num_traits::cast::ToPrimitive; -use num_traits::identities::Zero; -use std::{collections::HashMap, ops::Mul}; +use num_traits::{cast::ToPrimitive, Zero}; +use std::collections::HashMap; #[derive(MontConfig)] #[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"] @@ -41,103 +41,6 @@ pub struct Cairo1HintProcessor { hints: HashMap>, } -/// Extracts a parameter assumed to be a buffer. -fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { - let (cell, base_offset) = match buffer { - ResOperand::Deref(cell) => (cell, 0.into()), - ResOperand::BinOp(bin_op) => { - if let DerefOrImmediate::Immediate(val) = &bin_op.b { - (&bin_op.a, val.clone().value.into()) - } else { - return Err(HintError::CustomHint("Failed to extract buffer, expected ResOperand of BinOp type to have Inmediate b value".to_owned())); - } - } - _ => { - return Err(HintError::CustomHint( - "Illegal argument for a buffer.".to_string(), - )) - } - }; - Ok((cell, base_offset)) -} - -fn cell_ref_to_relocatable( - cell_ref: &CellRef, - vm: &VirtualMachine, -) -> Result { - let base = match cell_ref.register { - Register::AP => vm.get_ap(), - Register::FP => vm.get_fp(), - }; - base + (cell_ref.offset as i32) -} - -fn get_cell_val(vm: &VirtualMachine, cell: &CellRef) -> Result { - Ok(vm - .get_integer(cell_ref_to_relocatable(cell, vm)?)? - .as_ref() - .clone()) -} - -fn get_ptr( - vm: &VirtualMachine, - cell: &CellRef, - offset: &Felt252, -) -> Result { - Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm)?)? + offset)?) -} - -fn as_relocatable(vm: &mut VirtualMachine, value: &ResOperand) -> Result { - let (base, offset) = extract_buffer(value)?; - get_ptr(vm, base, &offset).map_err(HintError::from) -} - -fn get_double_deref_val( - vm: &VirtualMachine, - cell: &CellRef, - offset: &Felt252, -) -> Result { - Ok(vm.get_integer(get_ptr(vm, cell, offset)?)?.as_ref().clone()) -} - -/// Fetches the value of `res_operand` from the vm. -fn res_operand_get_val( - vm: &VirtualMachine, - res_operand: &ResOperand, -) -> Result { - match res_operand { - ResOperand::Deref(cell) => get_cell_val(vm, cell), - ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), - ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), - ResOperand::BinOp(op) => { - let a = get_cell_val(vm, &op.a)?; - let b = match &op.b { - DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, - DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()), - }; - match op.op { - Operation::Add => Ok(a + b), - Operation::Mul => Ok(a * b), - } - } - } -} - -fn as_cairo_short_string(value: &Felt252) -> Option { - let mut as_string = String::default(); - let mut is_end = false; - for byte in value.to_bytes_be() { - if byte == 0 { - is_end = true; - } else if is_end || !byte.is_ascii() { - return None; - } else { - as_string.push(byte as char); - } - } - Some(as_string) -} - impl Cairo1HintProcessor { pub fn new(hints: &[(usize, Vec)]) -> Self { Self { @@ -269,7 +172,7 @@ impl Cairo1HintProcessor { Hint::Core(CoreHint::ShouldSkipSquashLoop { should_skip_loop }) => { self.should_skip_squash_loop(vm, exec_scopes, should_skip_loop) } - hint => Err(HintError::UnknownHint(hint.to_string())) + hint => Err(HintError::UnknownHint(hint.to_string())), } } diff --git a/src/hint_processor/cairo_1_hint_processor/mod.rs b/src/hint_processor/cairo_1_hint_processor/mod.rs index e295b1a59a..29d5f47bd3 100644 --- a/src/hint_processor/cairo_1_hint_processor/mod.rs +++ b/src/hint_processor/cairo_1_hint_processor/mod.rs @@ -1,2 +1,3 @@ pub mod dict_manager; pub mod hint_processor; +pub mod hint_processor_utils; From 78e3c8a3b9a58fc05959fa1ae137f54a9e021954 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 16:41:13 -0300 Subject: [PATCH 38/84] Add untracked file --- .../hint_processor_utils.rs | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs new file mode 100644 index 0000000000..264a93f002 --- /dev/null +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs @@ -0,0 +1,108 @@ +use cairo_lang_casm::operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand}; + +use crate::types::{errors::math_errors::MathError, relocatable::Relocatable}; +use crate::vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError}; +use crate::vm::vm_core::VirtualMachine; +use felt::Felt252; +/// Extracts a parameter assumed to be a buffer. +pub(crate) fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { + let (cell, base_offset) = match buffer { + ResOperand::Deref(cell) => (cell, 0.into()), + ResOperand::BinOp(bin_op) => { + if let DerefOrImmediate::Immediate(val) = &bin_op.b { + (&bin_op.a, val.clone().value.into()) + } else { + return Err(HintError::CustomHint("Failed to extract buffer, expected ResOperand of BinOp type to have Inmediate b value".to_owned())); + } + } + _ => { + return Err(HintError::CustomHint( + "Illegal argument for a buffer.".to_string(), + )) + } + }; + Ok((cell, base_offset)) +} + +pub(crate) fn cell_ref_to_relocatable( + cell_ref: &CellRef, + vm: &VirtualMachine, +) -> Result { + let base = match cell_ref.register { + Register::AP => vm.get_ap(), + Register::FP => vm.get_fp(), + }; + base + (cell_ref.offset as i32) +} + +pub(crate) fn get_cell_val( + vm: &VirtualMachine, + cell: &CellRef, +) -> Result { + Ok(vm + .get_integer(cell_ref_to_relocatable(cell, vm)?)? + .as_ref() + .clone()) +} + +pub(crate) fn get_ptr( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok((vm.get_relocatable(cell_ref_to_relocatable(cell, vm)?)? + offset)?) +} + +pub(crate) fn as_relocatable( + vm: &mut VirtualMachine, + value: &ResOperand, +) -> Result { + let (base, offset) = extract_buffer(value)?; + get_ptr(vm, base, &offset).map_err(HintError::from) +} + +pub(crate) fn get_double_deref_val( + vm: &VirtualMachine, + cell: &CellRef, + offset: &Felt252, +) -> Result { + Ok(vm.get_integer(get_ptr(vm, cell, offset)?)?.as_ref().clone()) +} + +/// Fetches the value of `res_operand` from the vm. +pub(crate) fn res_operand_get_val( + vm: &VirtualMachine, + res_operand: &ResOperand, +) -> Result { + match res_operand { + ResOperand::Deref(cell) => get_cell_val(vm, cell).cloned(), + ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), + ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), + ResOperand::BinOp(op) => { + let a = get_cell_val(vm, &op.a)?; + let b = match &op.b { + DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, + DerefOrImmediate::Immediate(x) => &Felt252::from(x.value.clone()), + }; + match op.op { + Operation::Add => Ok(a + b), + Operation::Mul => Ok(a * b), + } + } + } +} + +pub(crate) fn as_cairo_short_string(value: &Felt252) -> Option { + let mut as_string = String::default(); + let mut is_end = false; + for byte in value.to_bytes_be() { + if byte == 0 { + is_end = true; + } else if is_end || !byte.is_ascii() { + return None; + } else { + as_string.push(byte as char); + } + } + Some(as_string) +} From 6a96be2a745dcb6ccdf0fd1d364bd3c5ea1c24ba Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 16:45:33 -0300 Subject: [PATCH 39/84] Fix --- .../cairo_1_hint_processor/hint_processor_utils.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs index 264a93f002..921cfc6fed 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs @@ -75,14 +75,14 @@ pub(crate) fn res_operand_get_val( res_operand: &ResOperand, ) -> Result { match res_operand { - ResOperand::Deref(cell) => get_cell_val(vm, cell).cloned(), + ResOperand::Deref(cell) => get_cell_val(vm, cell), ResOperand::DoubleDeref(cell, offset) => get_double_deref_val(vm, cell, &(*offset).into()), ResOperand::Immediate(x) => Ok(Felt252::from(x.value.clone())), ResOperand::BinOp(op) => { let a = get_cell_val(vm, &op.a)?; let b = match &op.b { DerefOrImmediate::Deref(cell) => get_cell_val(vm, cell)?, - DerefOrImmediate::Immediate(x) => &Felt252::from(x.value.clone()), + DerefOrImmediate::Immediate(x) => Felt252::from(x.value.clone()), }; match op.op { Operation::Add => Ok(a + b), From b902488ebfab067d53a0d9ed0c60c19d4e3dc79b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 16:58:41 -0300 Subject: [PATCH 40/84] Add changelog entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b18f115316..906cf6c1db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ #### Upcoming Changes +* Make the VM able to run `CasmContractClass` files [#1098](https://github.com/lambdaclass/cairo-rs/pull/1098) + + * Implement `TryFrom for Program` + * Add `Cairo1HintProcessor` + * Use to_signed_felt as function for felt252 as BigInt within [-P/2, P/2] range and use to_bigint as function for representation as BigInt. [#1100](https://github.com/lambdaclass/cairo-rs/pull/1100) * Implement hint on field_arithmetic lib [#1090](https://github.com/lambdaclass/cairo-rs/pull/1090) From 2b40d208356475baf2c3c3a00bb350e1658939cb Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:15:00 -0300 Subject: [PATCH 41/84] Add a job to compile cairo 1 contracts in ci --- .github/workflows/rust.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5c7885cad3..8912723d55 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,6 +11,37 @@ env: CARGO_TERM_COLOR: always jobs: + build-contracts: + name: Build Cairo 1 contracts + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Fetch from cache + uses: actions/cache@v3 + id: cache-programs + with: + path: cairo_programs/**/*.casm + key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.casm' ) }} + restore-keys: ${{ matrix.program-target }}-cache- + + # This is not pretty, but we need `make` to see the compiled programs are + # actually newer than the sources, otherwise it will try to rebuild them + - name: Restore timestamps + uses: chetan/git-restore-mtime-action@v1 + + - name: Install cairo 1 compiler + if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} + run: git clone https://github.com/starkware-libs/cairo.git \ + cd cairo; cargo build --release + + - name: Build programs + if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} + run: make -j ${{ matrix.program-target }} + build-programs: strategy: matrix: From 04e966ab4072af6cd8896d15c0ca3adba035c56d Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:17:37 -0300 Subject: [PATCH 42/84] Add job dependency --- .github/workflows/rust.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8912723d55..435cfcbf03 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -84,7 +84,7 @@ jobs: lint: - needs: build-programs + needs: build-programs, build-contracts name: Run Lints runs-on: ubuntu-22.04 steps: @@ -122,7 +122,7 @@ jobs: # NOTE: the term "smoke test" comes from electronics design: the minimal # expectations anyone has in their device is to not catch fire on boot. smoke: - needs: build-programs + needs: build-programs, build-contracts name: Make sure all builds work runs-on: ubuntu-22.04 steps: @@ -158,7 +158,7 @@ jobs: tests: - needs: build-programs + needs: build-programs, build-contracts strategy: fail-fast: false matrix: From e13bb43fcc5855d02a7afe847d8e12dab6c27630 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:26:47 -0300 Subject: [PATCH 43/84] Fix yml syntax --- .github/workflows/rust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 435cfcbf03..65de013d3c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -35,8 +35,8 @@ jobs: - name: Install cairo 1 compiler if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} - run: git clone https://github.com/starkware-libs/cairo.git \ - cd cairo; cargo build --release + run: git clone https://github.com/starkware-libs/cairo.git + run: cd cairo; cargo build --release - name: Build programs if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} From 0cb97905ffb3132e909f0573aea9a54da8f6900b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:30:27 -0300 Subject: [PATCH 44/84] Fix yml syntax --- .github/workflows/rust.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 65de013d3c..d279e93e5a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -35,8 +35,9 @@ jobs: - name: Install cairo 1 compiler if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} - run: git clone https://github.com/starkware-libs/cairo.git - run: cd cairo; cargo build --release + run: | + git clone https://github.com/starkware-libs/cairo.git + cd cairo; cargo build --release - name: Build programs if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} From 1461d56c4631100852b4472b3ffe20951a299a76 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:32:17 -0300 Subject: [PATCH 45/84] Temporarily extempt cairo-1-hint-processor from codecov --- .github/codecov.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/codecov.yml b/.github/codecov.yml index 50fe74a541..07b930c0a2 100644 --- a/.github/codecov.yml +++ b/.github/codecov.yml @@ -8,4 +8,5 @@ ignore: - src/vm/errors - src/types/errors - hint_accountant + - src/hint_processor/cairo-1-hint-processor # TODO: Remove this line - ./deps From 77d370eb881a865d2b25b8ea8364f0259fa299e5 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:33:48 -0300 Subject: [PATCH 46/84] Fix yml syntax --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index d279e93e5a..f3a9d5c330 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -37,7 +37,7 @@ jobs: if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} run: | git clone https://github.com/starkware-libs/cairo.git - cd cairo; cargo build --release + cd cairo; cargo build --release - name: Build programs if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} From 44d87e1a14676d4518c7b01619c1355ee556aa7b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:40:27 -0300 Subject: [PATCH 47/84] Fix workflow --- .github/workflows/rust.yml | 6 +++--- Makefile | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f3a9d5c330..fc64d84c72 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -25,8 +25,8 @@ jobs: id: cache-programs with: path: cairo_programs/**/*.casm - key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.casm' ) }} - restore-keys: ${{ matrix.program-target }}-cache- + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.casm' ) }} + restore-keys: cairo_1_test_contracts-cache- # This is not pretty, but we need `make` to see the compiled programs are # actually newer than the sources, otherwise it will try to rebuild them @@ -41,7 +41,7 @@ jobs: - name: Build programs if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} - run: make -j ${{ matrix.program-target }} + run: make -j cairo_1_test_contracts build-programs: strategy: diff --git a/Makefile b/Makefile index 46134c7ee7..6f4fd30f36 100644 --- a/Makefile +++ b/Makefile @@ -157,6 +157,7 @@ check: cairo_test_programs: $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) cairo_proof_programs: $(COMPILED_PROOF_TESTS) cairo_bench_programs: $(COMPILED_BENCHES) +cairo_1_test_contracts: $(COMPILED_CASM_CONTRACTS) cairo_proof_trace: $(CAIRO_TRACE_PROOF) $(CAIRO_MEM_PROOF) cairo-rs_proof_trace: $(CAIRO_RS_TRACE_PROOF) $(CAIRO_RS_MEM_PROOF) From 2e0701bea4843287c7d4eec63d99b9e384963737 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:44:49 -0300 Subject: [PATCH 48/84] Remove cache code from new job --- .github/workflows/rust.yml | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index fc64d84c72..6832345d9c 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -20,27 +20,12 @@ jobs: with: fetch-depth: 0 - - name: Fetch from cache - uses: actions/cache@v3 - id: cache-programs - with: - path: cairo_programs/**/*.casm - key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.casm' ) }} - restore-keys: cairo_1_test_contracts-cache- - - # This is not pretty, but we need `make` to see the compiled programs are - # actually newer than the sources, otherwise it will try to rebuild them - - name: Restore timestamps - uses: chetan/git-restore-mtime-action@v1 - - name: Install cairo 1 compiler - if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} run: | git clone https://github.com/starkware-libs/cairo.git cd cairo; cargo build --release - - name: Build programs - if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} + - name: Build contracts run: make -j cairo_1_test_contracts build-programs: From 85791c5740cdd889c1381f878ae6f8b2f80f3099 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:47:23 -0300 Subject: [PATCH 49/84] Fix yml syntax --- .github/workflows/rust.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6832345d9c..1533d2fd17 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -70,7 +70,7 @@ jobs: lint: - needs: build-programs, build-contracts + needs: [ build-programs, build-contracts ] name: Run Lints runs-on: ubuntu-22.04 steps: @@ -108,7 +108,7 @@ jobs: # NOTE: the term "smoke test" comes from electronics design: the minimal # expectations anyone has in their device is to not catch fire on boot. smoke: - needs: build-programs, build-contracts + needs: [ build-programs, build-contracts ] name: Make sure all builds work runs-on: ubuntu-22.04 steps: @@ -144,7 +144,7 @@ jobs: tests: - needs: build-programs, build-contracts + needs: [ build-programs, build-contracts ] strategy: fail-fast: false matrix: From 736b9737330184ecb94789616c286036cbc8faec Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:56:41 -0300 Subject: [PATCH 50/84] Fix wrong path --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6f4fd30f36..67e4e63018 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ RELBIN:=target/release/cairo-rs-run DBGBIN:=target/debug/cairo-rs-run STARKNET_COMPILE:=cairo/target/release/starknet-compile -STARKNET_SIERRA_COMPILE:=cairo/target/release/starknet-compile +STARKNET_SIERRA_COMPILE:=cairo/target/release/starknet-sierra-compile .PHONY: build-cairo-1-compiler deps deps-macos cargo-deps build run check test clippy coverage benchmark flamegraph \ compare_benchmarks_deps compare_benchmarks docs clean \ From 88f2cad50b669923458fb604476e4c559932a620 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 17:58:52 -0300 Subject: [PATCH 51/84] Fix makefile --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 67e4e63018..01e961865a 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ STARKNET_SIERRA_COMPILE:=cairo/target/release/starknet-sierra-compile compare_trace_memory_proof compare_trace_proof compare_memory_proof \ cairo_bench_programs cairo_proof_programs cairo_test_programs \ cairo_trace cairo-rs_trace cairo_proof_trace cairo-rs_proof_trace \ - $(RELBIN) $(DBGBIN) + $(RELBIN) $(DBGBIN) $(STARKNET_COMPILE) $(STARKNET_SIERRA_COMPILE) # Proof mode consumes too much memory with cairo-lang to execute # two instances at the same time in the CI without getting killed @@ -101,10 +101,10 @@ COMPILED_SIERRA_CONTRACTS:=$(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo, $(C COMPILED_CASM_CONTRACTS:= $(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra, $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm, $(COMPILED_SIERRA_CONTRACTS)) $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo - STARKNET_COMPILE -- $< $@ + $(STARKNET_COMPILE) -- $< $@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra - STARKNET_SIERRA_COMPILE -- $< $@ + $(STARKNET_SIERRA_COMPILE) -- $< $@ build-cairo-1-compiler: git clone https://github.com/starkware-libs/cairo.git From af264a5d7c494e0e8844bf487eaf39bf7683d88c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:00:14 -0300 Subject: [PATCH 52/84] Build only compiler binaries --- .github/workflows/rust.yml | 4 +--- Makefile | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1533d2fd17..f8f77cfaec 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -21,9 +21,7 @@ jobs: fetch-depth: 0 - name: Install cairo 1 compiler - run: | - git clone https://github.com/starkware-libs/cairo.git - cd cairo; cargo build --release + run: make build-cairo-1-compiler - name: Build contracts run: make -j cairo_1_test_contracts diff --git a/Makefile b/Makefile index 01e961865a..5325cf6524 100644 --- a/Makefile +++ b/Makefile @@ -108,7 +108,7 @@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra build-cairo-1-compiler: git clone https://github.com/starkware-libs/cairo.git - cd cairo; cargo build --release + cd cairo; cargo b --release --bin starknet-compile --bin starknet-sierra-compile cargo-deps: cargo install --version 1.1.0 cargo-criterion From 367515ffb644929815dddbc9f23dde08ce307fae Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:19:11 -0300 Subject: [PATCH 53/84] Add cairo-1-contracts-cache --- .github/workflows/rust.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f8f77cfaec..8cb035be3e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -19,6 +19,18 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 + - name: Fetch from cache + uses: actions/cache@v3 + id: cache-programs + with: + path: cairo_programs/cairo-1-contracts/*.casm + key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + restore-keys: cairo-1-contracts-cache- + + # This is not pretty, but we need `make` to see the compiled programs are + # actually newer than the sources, otherwise it will try to rebuild them + - name: Restore timestamps + uses: chetan/git-restore-mtime-action@v1 - name: Install cairo 1 compiler run: make build-cairo-1-compiler @@ -99,6 +111,11 @@ jobs: with: path: cairo_programs/**/*.json key: cairo_bench_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} + - name: Fetch test contracts + uses: actions/cache/restore@v3 + with: + path: cairo_programs/cairo-1-contracts/*.casm + key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Run clippy run: make clippy From e3635a4516aeb133d625f2f7a2904adc7c04a272 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:32:21 -0300 Subject: [PATCH 54/84] Fetch contracts cache in jobs that need them --- .github/workflows/rust.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8cb035be3e..2f628cc382 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -153,6 +153,11 @@ jobs: with: path: cairo_programs/**/*.json key: cairo_bench_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} + - name: Fetch test contracts + uses: actions/cache/restore@v3 + with: + path: cairo_programs/cairo-1-contracts/*.casm + key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Check run: cargo check-all-features --workspace --all-targets @@ -193,6 +198,11 @@ jobs: with: path: cairo_programs/**/*.json key: cairo_proof_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} + - name: Fetch test contracts + uses: actions/cache/restore@v3 + with: + path: cairo_programs/cairo-1-contracts/*.casm + key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Install testing tools uses: taiki-e/install-action@v2 From 04be04a6776eae7aa0a94f825cbafaede85e74d8 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:35:03 -0300 Subject: [PATCH 55/84] Use no-std version of HashMap --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 7827ee028f..a055bad3ed 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -5,6 +5,7 @@ use crate::felt::{felt_str, Felt252}; use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope; use crate::hint_processor::hint_processor_definition::HintReference; +use crate::stdlib::collections::HashMap; use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, @@ -22,7 +23,6 @@ use core::ops::Mul; use num_bigint::BigUint; use num_integer::Integer; use num_traits::{cast::ToPrimitive, Zero}; -use std::collections::HashMap; #[derive(MontConfig)] #[modulus = "3618502788666131213697322783095070105623107215331596699973092056135872020481"] From b944e9a0673abb62c948befc42a017a02183f5cb Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:36:45 -0300 Subject: [PATCH 56/84] Import stdlib::prelude::* --- src/hint_processor/cairo_1_hint_processor/dict_manager.rs | 4 ++-- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 1 + .../cairo_1_hint_processor/hint_processor_utils.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index a2eaf42c7d..0e15b06e98 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -1,6 +1,6 @@ -use std::collections::HashMap; - +use crate::stdlib::prelude::*; use num_traits::One; +use std::collections::HashMap; use crate::felt::Felt252; use crate::vm::errors::hint_errors::HintError; diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index a055bad3ed..c4f1d795aa 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -6,6 +6,7 @@ use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecS use crate::hint_processor::hint_processor_definition::HintReference; use crate::stdlib::collections::HashMap; +use crate::stdlib::prelude::*; use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs index 921cfc6fed..82d7942c43 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor_utils.rs @@ -1,8 +1,8 @@ -use cairo_lang_casm::operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand}; - +use crate::stdlib::prelude::*; use crate::types::{errors::math_errors::MathError, relocatable::Relocatable}; use crate::vm::errors::{hint_errors::HintError, vm_errors::VirtualMachineError}; use crate::vm::vm_core::VirtualMachine; +use cairo_lang_casm::operand::{CellRef, DerefOrImmediate, Operation, Register, ResOperand}; use felt::Felt252; /// Extracts a parameter assumed to be a buffer. pub(crate) fn extract_buffer(buffer: &ResOperand) -> Result<(&CellRef, Felt252), HintError> { From 8f78351c892665a73ab704444b1ae947fefc47f0 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 4 May 2023 18:53:51 -0300 Subject: [PATCH 57/84] Wrap print in not-wasm block --- .../cairo_1_hint_processor/dict_manager.rs | 2 +- .../cairo_1_hint_processor/hint_processor.rs | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index 0e15b06e98..068273e6a5 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -1,6 +1,6 @@ +use crate::stdlib::collections::HashMap; use crate::stdlib::prelude::*; use num_traits::One; -use std::collections::HashMap; use crate::felt::Felt252; use crate::vm::errors::hint_errors::HintError; diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index c4f1d795aa..5943b20fc6 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -20,6 +20,7 @@ use cairo_lang_casm::{ hints::{CoreHint, Hint}, operand::{CellRef, ResOperand}, }; +use core::any::Any; use core::ops::Mul; use num_bigint::BigUint; use num_integer::Integer; @@ -593,16 +594,20 @@ impl Cairo1HintProcessor { ) -> Result<(), HintError> { let mut curr = as_relocatable(vm, start)?; let end = as_relocatable(vm, end)?; - while curr != end { - let value = vm.get_integer(curr)?; - if let Some(shortstring) = as_cairo_short_string(&value) { - println!("[DEBUG]\t{shortstring: <31}\t(raw: {value: <31})"); - } else { - println!("[DEBUG]\t{0: <31}\t(raw: {value: <31}) ", ' '); + + #[cfg(not(target_arch = "wasm32"))] + { + while curr != end { + let value = vm.get_integer(curr)?; + if let Some(shortstring) = as_cairo_short_string(&value) { + println!("[DEBUG]\t{shortstring: <31}\t(raw: {value: <31})"); + } else { + println!("[DEBUG]\t{0: <31}\t(raw: {value: <31}) ", ' '); + } + curr += 1; } - curr += 1; + println!(); } - println!(); Ok(()) } @@ -670,7 +675,7 @@ impl HintProcessor for Cairo1HintProcessor { _reference_ids: &HashMap, //List of all references (key corresponds to element of the previous dictionary) _references: &HashMap, - ) -> Result, VirtualMachineError> { + ) -> Result, VirtualMachineError> { let data = hint_code.parse().ok().and_then(|x| self.hints.get(&x).cloned()).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; Ok(any_box!(data)) } From 806ee3924a4807b96dbe1571bb8c9a85301c2ff3 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 10:51:04 -0300 Subject: [PATCH 58/84] Remove std path --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 5943b20fc6..2ac4aaf001 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -690,7 +690,7 @@ impl HintProcessor for Cairo1HintProcessor { //access current scope variables exec_scopes: &mut ExecutionScopes, //Data structure that can be downcasted to the structure generated by compile_hint - hint_data: &Box, + hint_data: &Box, //Constant values extracted from the program specification. _constants: &HashMap, ) -> Result<(), HintError> { From 7195ffe1c3ddb596ed125af9fe75673e340a0c35 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 10:54:29 -0300 Subject: [PATCH 59/84] use slices instead of vec --- src/tests/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index cfb99ce711..d5d3668f96 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -92,8 +92,8 @@ pub(self) fn run_program( pub(self) fn run_cairo_1_entrypoint( program_content: &[u8], entrypoint_offset: usize, - args: &Vec, - expected_retdata: &Vec, + args: &[MaybeRelocatable], + expected_retdata: &[Felt252], ) { let contract_class: CasmContractClass = serde_json::from_slice(program_content).unwrap(); let mut hint_processor = Cairo1HintProcessor::new(&contract_class.hints); @@ -155,7 +155,7 @@ pub(self) fn run_cairo_1_entrypoint( // Load calldata let calldata_start = vm.add_memory_segment(); - let calldata_end = vm.load_data(calldata_start, args).unwrap(); + let calldata_end = vm.load_data(calldata_start, &args.to_vec()).unwrap(); // Create entrypoint_args From 6c8c334cab57f5ff8d1e6f42eeac2cb8ed6860c0 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 10:57:49 -0300 Subject: [PATCH 60/84] Make DictSquashExecScope fields private --- src/hint_processor/cairo_1_hint_processor/dict_manager.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs index 068273e6a5..123adc01f9 100644 --- a/src/hint_processor/cairo_1_hint_processor/dict_manager.rs +++ b/src/hint_processor/cairo_1_hint_processor/dict_manager.rs @@ -92,9 +92,9 @@ impl DictManagerExecScope { #[derive(Default, Debug)] pub struct DictSquashExecScope { /// A map from key to the list of indices accessing it, each list in reverse order. - pub access_indices: HashMap>, + pub(crate) access_indices: HashMap>, /// Descending list of keys. - pub keys: Vec, + keys: Vec, } impl DictSquashExecScope { From 917c9cb8af169c587360255668325401591ebc2a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:14:38 -0300 Subject: [PATCH 61/84] Import hint processor dependencies without default features --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1f4c18a6a8..b9e029f147 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,8 +83,8 @@ cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46" } # TODO: check these dependencies for wasm compatibility -ark-ff = "0.4.0-alpha.7" -ark-std = "0.3.0" +ark-ff = {version = "0.4.0-alpha.7", default-features = false} +ark-std = {version = "0.3.0", default-features = false} [dev-dependencies] assert_matches = "1.5.0" From a47b09c0ed7445a52d0e4b868f0444f608840551 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:23:54 -0300 Subject: [PATCH 62/84] -Clippy --- src/tests/cairo_1_run_from_entrypoint_tests.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index 2357d54b50..da6432e7d8 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -7,8 +7,8 @@ fn fibonacci_1() { run_cairo_1_entrypoint( program_data.as_slice(), 0, - &vec![1_usize.into(), 1_usize.into(), 1_usize.into()], - &vec![1_usize.into()], + &[1_usize.into(), 1_usize.into(), 1_usize.into()], + &[1_usize.into()], ); } @@ -19,7 +19,7 @@ fn fibonacci_3() { run_cairo_1_entrypoint( program_data.as_slice(), 0, - &vec![3_usize.into(), 3_usize.into(), 3_usize.into()], - &vec![9_usize.into()], + &[3_usize.into(), 3_usize.into(), 3_usize.into()], + &[9_usize.into()], ); } From b9417c5bd1b74ea9762794c4bc85d5d972203d0b Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:25:27 -0300 Subject: [PATCH 63/84] Add type --- src/hint_processor/cairo_1_hint_processor/hint_processor.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs index 2ac4aaf001..3763cff6e1 100644 --- a/src/hint_processor/cairo_1_hint_processor/hint_processor.rs +++ b/src/hint_processor/cairo_1_hint_processor/hint_processor.rs @@ -676,7 +676,7 @@ impl HintProcessor for Cairo1HintProcessor { //List of all references (key corresponds to element of the previous dictionary) _references: &HashMap, ) -> Result, VirtualMachineError> { - let data = hint_code.parse().ok().and_then(|x| self.hints.get(&x).cloned()).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; + let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned()).ok_or(VirtualMachineError::CompileHintFail(format!("No hint found for pc {}. Cairo1HintProccesor can only be used when running CasmContractClass", hint_code)))?; Ok(any_box!(data)) } From f96c5dfcd7c257f8ce03a0fd7e6dc016691e4c41 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:45:19 -0300 Subject: [PATCH 64/84] Compile cairo 1 contracts in build-programs job --- .github/workflows/rust.yml | 43 ++++++++++---------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2f628cc382..02118a0faa 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,38 +11,11 @@ env: CARGO_TERM_COLOR: always jobs: - build-contracts: - name: Build Cairo 1 contracts - runs-on: ubuntu-22.04 - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: Fetch from cache - uses: actions/cache@v3 - id: cache-programs - with: - path: cairo_programs/cairo-1-contracts/*.casm - key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - restore-keys: cairo-1-contracts-cache- - - # This is not pretty, but we need `make` to see the compiled programs are - # actually newer than the sources, otherwise it will try to rebuild them - - name: Restore timestamps - uses: chetan/git-restore-mtime-action@v1 - - - name: Install cairo 1 compiler - run: make build-cairo-1-compiler - - - name: Build contracts - run: make -j cairo_1_test_contracts - build-programs: strategy: matrix: # NOTE: we build cairo_proof_programs so clippy can check the benchmarks too - program-target: [ cairo_bench_programs, cairo_proof_programs, cairo_test_programs ] + program-target: [ cairo_bench_programs, cairo_proof_programs, cairo_test_programs, cairo_1_test_contracts ] name: Build Cairo programs runs-on: ubuntu-22.04 steps: @@ -55,7 +28,9 @@ jobs: uses: actions/cache@v3 id: cache-programs with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.json + cairo_programs/cairo-1-contracts/*.casm key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} restore-keys: ${{ matrix.program-target }}-cache- @@ -73,6 +48,10 @@ jobs: - name: Install cairo-lang and deps if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} run: pip install -r requirements.txt + + - name: Install cairo 1 compiler + if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} + run: make build-cairo-1-compiler - name: Build programs if: ${{ steps.cache-programs.outputs.cache-hit != 'true' }} @@ -80,7 +59,7 @@ jobs: lint: - needs: [ build-programs, build-contracts ] + needs: build-programs name: Run Lints runs-on: ubuntu-22.04 steps: @@ -123,7 +102,7 @@ jobs: # NOTE: the term "smoke test" comes from electronics design: the minimal # expectations anyone has in their device is to not catch fire on boot. smoke: - needs: [ build-programs, build-contracts ] + needs: build-programs name: Make sure all builds work runs-on: ubuntu-22.04 steps: @@ -164,7 +143,7 @@ jobs: tests: - needs: [ build-programs, build-contracts ] + needs: build-programs strategy: fail-fast: false matrix: From ddeefa17044ef3a2e4d45c0b113cb335205155d1 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:48:58 -0300 Subject: [PATCH 65/84] Rename cache --- .github/workflows/rust.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 02118a0faa..0254bc7164 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -94,7 +94,7 @@ jobs: uses: actions/cache/restore@v3 with: path: cairo_programs/cairo-1-contracts/*.casm - key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Run clippy run: make clippy @@ -136,7 +136,7 @@ jobs: uses: actions/cache/restore@v3 with: path: cairo_programs/cairo-1-contracts/*.casm - key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Check run: cargo check-all-features --workspace --all-targets @@ -181,7 +181,7 @@ jobs: uses: actions/cache/restore@v3 with: path: cairo_programs/cairo-1-contracts/*.casm - key: cairo-1-contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} - name: Install testing tools uses: taiki-e/install-action@v2 From a6b091319e57d392a339423cd568d79a377f5600 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 11:56:04 -0300 Subject: [PATCH 66/84] Use target dependency instead of explicit $(MAKE) --- Makefile | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 5325cf6524..b0acbde9c5 100644 --- a/Makefile +++ b/Makefile @@ -118,9 +118,7 @@ cargo-deps: cargo install --version 0.5.9 cargo-llvm-cov cargo install --version 0.11.0 wasm-pack -deps: - $(MAKE) cargo-deps - $(MAKE) build-cairo-1-compiler +deps: cargo-deps build-cairo-1-compiler pyenv install -s pypy3.9-7.3.9 PYENV_VERSION=pypy3.9-7.3.9 python -m venv cairo-rs-pypy-env . cairo-rs-pypy-env/bin/activate ; \ @@ -130,9 +128,7 @@ deps: . cairo-rs-env/bin/activate ; \ pip install -r requirements.txt ; \ -deps-macos: - $(MAKE) cargo-deps - $(MAKE) build-cairo-1-compiler +deps-macos: cargo-deps build-cairo-1-compiler brew install gmp arch -x86_64 pyenv install -s pypy3.9-7.3.9 PYENV_VERSION=pypy3.9-7.3.9 python -m venv cairo-rs-pypy-env From 329e17fd8669668bf86bf0a6a2699769ee3230dd Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 12:00:23 -0300 Subject: [PATCH 67/84] Fix yml syntax --- .github/workflows/rust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 0254bc7164..f29fa96fdc 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -29,8 +29,8 @@ jobs: id: cache-programs with: path: | - cairo_programs/**/*.json - cairo_programs/cairo-1-contracts/*.casm + cairo_programs/**/*.json + cairo_programs/cairo-1-contracts/*.casm key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} restore-keys: ${{ matrix.program-target }}-cache- From 2cd1d445a9ce34b9068e4336acd6037d36c327ce Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 12:16:36 -0300 Subject: [PATCH 68/84] Check for cairo folder before cloning cairo repo --- Makefile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b0acbde9c5..cb8aa2556f 100644 --- a/Makefile +++ b/Makefile @@ -106,7 +106,11 @@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra $(STARKNET_SIERRA_COMPILE) -- $< $@ -build-cairo-1-compiler: +cairo-repo-dir = cairo + +build-cairo-1-compiler: | $(cairo-repo-dir) + +$(cairo-repo-dir): git clone https://github.com/starkware-libs/cairo.git cd cairo; cargo b --release --bin starknet-compile --bin starknet-sierra-compile From bcdfa80ae175fbe42815c1bb6ef90f5dda10e1ff Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 12:32:09 -0300 Subject: [PATCH 69/84] Ommit folder name --- .github/workflows/rust.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f29fa96fdc..be5e777727 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -30,7 +30,7 @@ jobs: with: path: | cairo_programs/**/*.json - cairo_programs/cairo-1-contracts/*.casm + cairo_programs/**/*.casm key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} restore-keys: ${{ matrix.program-target }}-cache- @@ -93,8 +93,8 @@ jobs: - name: Fetch test contracts uses: actions/cache/restore@v3 with: - path: cairo_programs/cairo-1-contracts/*.casm - key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + path: cairo_programs/**/*.casm + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Run clippy run: make clippy @@ -135,8 +135,8 @@ jobs: - name: Fetch test contracts uses: actions/cache/restore@v3 with: - path: cairo_programs/cairo-1-contracts/*.casm - key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + path: cairo_programs/**/*.casm + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Check run: cargo check-all-features --workspace --all-targets @@ -180,8 +180,8 @@ jobs: - name: Fetch test contracts uses: actions/cache/restore@v3 with: - path: cairo_programs/cairo-1-contracts/*.casm - key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/cairo-1-contracts/*.cairo' ) }} + path: cairo_programs/**/*.casm + key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Install testing tools uses: taiki-e/install-action@v2 From 28bfe83a271ba9296340ef15272d2ff79686d54c Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 14:32:38 -0300 Subject: [PATCH 70/84] Swap paths --- .github/workflows/rust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index be5e777727..e6ccf98105 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -28,9 +28,9 @@ jobs: uses: actions/cache@v3 id: cache-programs with: - path: | - cairo_programs/**/*.json + path: | cairo_programs/**/*.casm + cairo_programs/**/*.json key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} restore-keys: ${{ matrix.program-target }}-cache- From dd05e21ff45eface04b2187017746ad6f856ac73 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 15:19:33 -0300 Subject: [PATCH 71/84] Add cairo-1-hints feature flag --- Cargo.toml | 1 + src/hint_processor/mod.rs | 1 + src/tests/mod.rs | 23 +++++++++++++++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9e029f147..6be9a0c262 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ std = [ "parse-hyperlinks/std", "felt/std", ] +cairo-1-hints = ["std", "with_mimalloc"] # Note that these features are not retro-compatible with the cairo Python VM. diff --git a/src/hint_processor/mod.rs b/src/hint_processor/mod.rs index 641409f7ae..0ecb72a1de 100644 --- a/src/hint_processor/mod.rs +++ b/src/hint_processor/mod.rs @@ -1,4 +1,5 @@ pub mod builtin_hint_processor; +#[cfg(features = "cairo-1-hints")] pub mod cairo_1_hint_processor; pub mod hint_processor_definition; pub mod hint_processor_utils; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index d5d3668f96..aa4febd021 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,21 +1,31 @@ -use crate::hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor; +#[cfg(features = "cairo-1-hints")] +use crate::{ + hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor, + types::relocatable::MaybeRelocatable, + vm::{ + runners::cairo_runner::{CairoArg, CairoRunner}, + vm::vm_core::VirtualMachine, + }, +}; +#[cfg(features = "cairo-1-hints")] +use cairo_lang_starknet::casm_contract_class::CasmContractClass; +#[cfg(features = "cairo-1-hints")] +use felt::Felt252; + use crate::stdlib::prelude::*; -use crate::types::relocatable::MaybeRelocatable; -use crate::vm::runners::cairo_runner::{CairoArg, CairoRunner}; -use crate::vm::vm_core::VirtualMachine; use crate::{ cairo_run::{cairo_run, CairoRunConfig}, hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, vm::trace::trace_entry::TraceEntry, }; -use cairo_lang_starknet::casm_contract_class::CasmContractClass; -use felt::Felt252; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; mod bitwise_test; + +#[cfg(features = "cairo-1-hints")] mod cairo_1_run_from_entrypoint_tests; mod cairo_run_test; mod pedersen_test; @@ -87,6 +97,7 @@ pub(self) fn run_program( } } +#[cfg(features = "cairo-1-hints")] // Runs a contract entrypoint with given arguments and checks its return values // Doesn't use a syscall_handler pub(self) fn run_cairo_1_entrypoint( From e91ca9811bca38f0016e5539d390feaca529a22d Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 15:51:56 -0300 Subject: [PATCH 72/84] Add compile-hint feature to tests run in workflow --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e6ccf98105..c4c40c265e 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -192,7 +192,7 @@ jobs: # FIXME: we need to update the Makefile to do this correctly case ${{ matrix.target }} in 'test') - cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils + cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils cairo-1-hints ;; 'test-no_std') cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils --no-default-features From 36a295740ce4b1437a7d07e7d8263316599eae0c Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 16:04:55 -0300 Subject: [PATCH 73/84] Add cairo-1-hints to test_utils --- Cargo.toml | 3 ++- Makefile | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6be9a0c262..2aebfa8325 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,13 +20,14 @@ std = [ "parse-hyperlinks/std", "felt/std", ] -cairo-1-hints = ["std", "with_mimalloc"] +cairo-1-hints = [] # Note that these features are not retro-compatible with the cairo Python VM. test_utils = [ "skip_next_instruction_hint", "hooks", + "cairo-1-hints" ] # This feature will reference every test-oriented feature skip_next_instruction_hint = [] hooks = [] diff --git a/Makefile b/Makefile index cb8aa2556f..2bb9b75a92 100644 --- a/Makefile +++ b/Makefile @@ -166,7 +166,7 @@ cairo_trace: $(CAIRO_TRACE) $(CAIRO_MEM) cairo-rs_trace: $(CAIRO_RS_TRACE) $(CAIRO_RS_MEM) test: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) $(COMPILED_CASM_CONTRACTS) - cargo llvm-cov nextest --no-report --workspace --features test_utils + cargo llvm-cov nextest --no-report --workspace --features test_utils cairo-1-hints test-no_std: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) cargo llvm-cov nextest --no-report --workspace --features test_utils --no-default-features test-wasm: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) From 76534a9ab08fdde5a499ee4ed73264166746decc Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 16:10:07 -0300 Subject: [PATCH 74/84] Add cairo-1-hints to test_utils --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index c4c40c265e..e6ccf98105 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -192,7 +192,7 @@ jobs: # FIXME: we need to update the Makefile to do this correctly case ${{ matrix.target }} in 'test') - cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils cairo-1-hints + cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils ;; 'test-no_std') cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils --no-default-features From 009b3480595aa1a63ba2db623ccc74547aff016a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 17:45:44 -0300 Subject: [PATCH 75/84] Use both paths when fetching compiled test data --- .github/workflows/rust.yml | 48 ++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index e6ccf98105..51774d8d66 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -78,22 +78,30 @@ jobs: - name: Fetch test programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_test_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch proof programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_proof_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch bench programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_bench_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch test contracts uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.casm + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Run clippy run: make clippy @@ -120,22 +128,30 @@ jobs: - name: Fetch test programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_test_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch proof programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_proof_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch bench programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_bench_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch test contracts uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.casm + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_1_test_contracts-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Check @@ -170,12 +186,16 @@ jobs: - name: Fetch test programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_test_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch proof programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: cairo_proof_programs-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} - name: Fetch test contracts uses: actions/cache/restore@v3 @@ -274,7 +294,9 @@ jobs: if: ${{ steps.trace-cache.outputs.cache-hit != 'true' }} uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} fail-on-cache-miss: true @@ -315,7 +337,9 @@ jobs: - name: Fetch programs uses: actions/cache/restore@v3 with: - path: cairo_programs/**/*.json + path: | + cairo_programs/**/*.casm + cairo_programs/**/*.json key: ${{ matrix.program-target }}-cache-${{ hashFiles( 'cairo_programs/**/*.cairo' ) }} fail-on-cache-miss: true From 058591976aefa1e14f4f2070cedbdea787c1571a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 18:00:56 -0300 Subject: [PATCH 76/84] Remove cairo-1-hints feature from test_utils feature --- Cargo.toml | 3 +-- Makefile | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2aebfa8325..249aa0c34b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,8 +26,7 @@ cairo-1-hints = [] # Note that these features are not retro-compatible with the cairo Python VM. test_utils = [ "skip_next_instruction_hint", - "hooks", - "cairo-1-hints" + "hooks" ] # This feature will reference every test-oriented feature skip_next_instruction_hint = [] hooks = [] diff --git a/Makefile b/Makefile index 2bb9b75a92..607657d696 100644 --- a/Makefile +++ b/Makefile @@ -166,7 +166,7 @@ cairo_trace: $(CAIRO_TRACE) $(CAIRO_MEM) cairo-rs_trace: $(CAIRO_RS_TRACE) $(CAIRO_RS_MEM) test: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) $(COMPILED_CASM_CONTRACTS) - cargo llvm-cov nextest --no-report --workspace --features test_utils cairo-1-hints + cargo llvm-cov nextest --no-report --workspace --features "test_utils, cairo-1-hints" test-no_std: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) cargo llvm-cov nextest --no-report --workspace --features test_utils --no-default-features test-wasm: $(COMPILED_PROOF_TESTS) $(COMPILED_TESTS) $(COMPILED_BAD_TESTS) $(COMPILED_NORETROCOMPAT_TESTS) From 02bf01366fc264227e4b095181b47c69c9b7c1d1 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 5 May 2023 18:11:24 -0300 Subject: [PATCH 77/84] Move dependencies to cairo-1-hints feature --- CHANGELOG.md | 2 +- Cargo.toml | 12 +++++++----- src/types/program.rs | 5 ++++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f525291c9..a37b5ce738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Upcoming Changes -* Make the VM able to run `CasmContractClass` files [#1098](https://github.com/lambdaclass/cairo-rs/pull/1098) +* Make the VM able to run `CasmContractClass` files under `cairo-1-hints` feature [#1098](https://github.com/lambdaclass/cairo-rs/pull/1098) * Implement `TryFrom for Program` * Add `Cairo1HintProcessor` diff --git a/Cargo.toml b/Cargo.toml index 249aa0c34b..016701a9ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ std = [ "parse-hyperlinks/std", "felt/std", ] -cairo-1-hints = [] +cairo-1-hints = ["cairo-lang-starknet", "cairo-lang-casm", "ark-ff", "ark-std"] # Note that these features are not retro-compatible with the cairo Python VM. @@ -80,12 +80,14 @@ felt = { package = "cairo-felt", path = "./felt", version = "0.3.0-rc1", default "alloc", ] } bitvec = { version = "1", default-features = false, features = ["alloc"] } -cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46"} -cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46" } + +# Dependencies for cairo-1-hints feature +cairo-lang-starknet = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46", optional = true} +cairo-lang-casm = { git = "https://github.com/starkware-libs/cairo.git", rev = "a08bedbf26b711e81cec8b7660698a3788e67d46", optional = true } # TODO: check these dependencies for wasm compatibility -ark-ff = {version = "0.4.0-alpha.7", default-features = false} -ark-std = {version = "0.3.0", default-features = false} +ark-ff = {version = "0.4.0-alpha.7", default-features = false, optional = true} +ark-std = {version = "0.3.0", default-features = false, optional = true} [dev-dependencies] assert_matches = "1.5.0" diff --git a/src/types/program.rs b/src/types/program.rs index c547bf553c..26750a179f 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -1,6 +1,7 @@ -use crate::serde::deserialize_program::{ApTracking, FlowTrackingData}; use crate::stdlib::{collections::HashMap, prelude::*, sync::Arc}; +#[cfg(feature = "cairo-1-hints")] +use crate::serde::deserialize_program::{ApTracking, FlowTrackingData}; use crate::{ serde::deserialize_program::{ deserialize_and_parse_program, Attribute, BuiltinName, HintParams, Identifier, @@ -8,6 +9,7 @@ use crate::{ }, types::{errors::program_errors::ProgramError, relocatable::MaybeRelocatable}, }; +#[cfg(feature = "cairo-1-hints")] use cairo_lang_starknet::casm_contract_class::CasmContractClass; use felt::{Felt252, PRIME_STR}; @@ -147,6 +149,7 @@ impl Default for Program { } } +#[cfg(feature = "cairo-1-hints")] // Note: This Program will only work when using run_from_entrypoint, and the Cairo1Hintprocesso impl TryFrom for Program { type Error = ProgramError; From 9ae9e8290f61b2f20b9560e9cb9775f344e9057f Mon Sep 17 00:00:00 2001 From: juanbono Date: Mon, 8 May 2023 00:01:10 -0300 Subject: [PATCH 78/84] add test --- cairo_programs/cairo-1-contracts/divmod.cairo | 8 ++++++++ src/tests/cairo_1_run_from_entrypoint_tests.rs | 12 ++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 cairo_programs/cairo-1-contracts/divmod.cairo diff --git a/cairo_programs/cairo-1-contracts/divmod.cairo b/cairo_programs/cairo-1-contracts/divmod.cairo new file mode 100644 index 0000000000..4e21be72cc --- /dev/null +++ b/cairo_programs/cairo-1-contracts/divmod.cairo @@ -0,0 +1,8 @@ +#[contract] +mod DivModTestContract { + #[external] + fn div_mod_test(x: u8, y: u8) -> u8 { + let (res, _) = integer::u8_safe_divmod(x, integer::u8_as_non_zero(y)); + res + } +} diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index da6432e7d8..3b8a3afe36 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -23,3 +23,15 @@ fn fibonacci_3() { &[9_usize.into()], ); } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn divmod_hint_test() { + let program_data = include_bytes!("../../cairo_programs/cairo-1-contracts/divmod.casm"); + run_cairo_1_entrypoint( + program_data.as_slice(), + 0, + &[16_usize.into(), 2_usize.into()], + &[8_usize.into()], + ); +} From f0dcbb611f81498a8e19f8ca9b9391b344763bf4 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Mon, 8 May 2023 10:30:19 -0300 Subject: [PATCH 79/84] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d4700a8fa..693fbd7f4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ * Add `Cairo1HintProcessor` * Add `CairoRunner::get_program method` [#1123](https://github.com/lambdaclass/cairo-rs/pull/1123): -======= + * perf: insert elements from the tail in `load_data` so reallocation happens only once [#1117](https://github.com/lambdaclass/cairo-rs/pull/1117) * Add `CairoRunner::get_program method` [#1123](https://github.com/lambdaclass/cairo-rs/pull/1123) From f4f97e9e00554b684723c00c4eb601381bcc57f6 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 May 2023 11:53:53 -0300 Subject: [PATCH 80/84] Fix cfg directive --- src/hint_processor/mod.rs | 2 +- src/tests/mod.rs | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hint_processor/mod.rs b/src/hint_processor/mod.rs index 0ecb72a1de..baba16f490 100644 --- a/src/hint_processor/mod.rs +++ b/src/hint_processor/mod.rs @@ -1,5 +1,5 @@ pub mod builtin_hint_processor; -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] pub mod cairo_1_hint_processor; pub mod hint_processor_definition; pub mod hint_processor_utils; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index aa4febd021..bb853ea15c 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,15 +1,15 @@ -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] use crate::{ hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor, types::relocatable::MaybeRelocatable, vm::{ runners::cairo_runner::{CairoArg, CairoRunner}, - vm::vm_core::VirtualMachine, + vm_core::VirtualMachine, }, }; -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] use cairo_lang_starknet::casm_contract_class::CasmContractClass; -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] use felt::Felt252; use crate::stdlib::prelude::*; @@ -25,7 +25,7 @@ use wasm_bindgen_test::*; mod bitwise_test; -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] mod cairo_1_run_from_entrypoint_tests; mod cairo_run_test; mod pedersen_test; @@ -97,7 +97,7 @@ pub(self) fn run_program( } } -#[cfg(features = "cairo-1-hints")] +#[cfg(feature = "cairo-1-hints")] // Runs a contract entrypoint with given arguments and checks its return values // Doesn't use a syscall_handler pub(self) fn run_cairo_1_entrypoint( From 8de54530285c27c471159c93fe286cb56a8c7f06 Mon Sep 17 00:00:00 2001 From: Federica Date: Mon, 8 May 2023 12:56:11 -0300 Subject: [PATCH 81/84] Add cairo-1-hints to test workflow --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 51774d8d66..2ed9e26371 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -212,7 +212,7 @@ jobs: # FIXME: we need to update the Makefile to do this correctly case ${{ matrix.target }} in 'test') - cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils + cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features "cairo-1-hints, test_utils" ;; 'test-no_std') cargo llvm-cov nextest --lcov --output-path lcov-${{ matrix.target }}.info --workspace --features test_utils --no-default-features From d1c9dda036438d4d329b9f7fdf2893bf1c5c7cb2 Mon Sep 17 00:00:00 2001 From: Roberto Catalan Date: Mon, 8 May 2023 13:01:15 -0300 Subject: [PATCH 82/84] Add Cairo 1 considerations for Gitignore and Makefile (#1144) * Add ignore for casm and sierra files * Add libfunc experimental for cairo 1 compilation * Add new enty to CHANGELOG --- .gitignore | 2 ++ CHANGELOG.md | 5 +++++ Makefile | 4 ++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 01c4357d9d..010070081c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ **/*.json !hint_accountant/whitelists/*.json !cairo_programs/manually_compiled/*.json +**/*.casm +**/*.sierra **/*.trace **/*.memory **/*.swp diff --git a/CHANGELOG.md b/CHANGELOG.md index 693fbd7f4d..013b93364d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ #### Upcoming Changes +* Add some small considerations regarding Cairo 1 programs [#1144](https://github.com/lambdaclass/cairo-rs/pull/1144): + + * Ignore Casm and Sierra files + * Add special flag to compile Cairo 1 programs + * Make the VM able to run `CasmContractClass` files under `cairo-1-hints` feature [#1098](https://github.com/lambdaclass/cairo-rs/pull/1098) * Implement `TryFrom for Program` diff --git a/Makefile b/Makefile index 607657d696..2db8e0d77e 100644 --- a/Makefile +++ b/Makefile @@ -101,10 +101,10 @@ COMPILED_SIERRA_CONTRACTS:=$(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo, $(C COMPILED_CASM_CONTRACTS:= $(patsubst $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra, $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm, $(COMPILED_SIERRA_CONTRACTS)) $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.cairo - $(STARKNET_COMPILE) -- $< $@ + $(STARKNET_COMPILE) --allowed-libfuncs-list-name experimental_v0.1.0 $< $@ $(CAIRO_1_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_1_CONTRACTS_TEST_DIR)/%.sierra - $(STARKNET_SIERRA_COMPILE) -- $< $@ + $(STARKNET_SIERRA_COMPILE) --allowed-libfuncs-list-name experimental_v0.1.0 $< $@ cairo-repo-dir = cairo From a1b5f276e87b2f639962392cb7bcbb7c3c1f7f58 Mon Sep 17 00:00:00 2001 From: juanbono Date: Mon, 8 May 2023 15:50:14 -0300 Subject: [PATCH 83/84] update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a37b5ce738..23108b8fbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* Add a test for the `DivMod` hint [#1138](https://github.com/lambdaclass/cairo-rs/pull/1138). + * Make the VM able to run `CasmContractClass` files under `cairo-1-hints` feature [#1098](https://github.com/lambdaclass/cairo-rs/pull/1098) * Implement `TryFrom for Program` From b3cc5e7c44f3954363d5f92435285bb2a275a1e9 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 8 May 2023 19:22:16 -0300 Subject: [PATCH 84/84] cargo fmt --- src/tests/cairo_1_run_from_entrypoint_tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests/cairo_1_run_from_entrypoint_tests.rs b/src/tests/cairo_1_run_from_entrypoint_tests.rs index ea7e18ddde..3b8a3afe36 100644 --- a/src/tests/cairo_1_run_from_entrypoint_tests.rs +++ b/src/tests/cairo_1_run_from_entrypoint_tests.rs @@ -35,4 +35,3 @@ fn divmod_hint_test() { &[8_usize.into()], ); } -