From 26429eaca2e09cee7cf304fd0dddb4439a44a775 Mon Sep 17 00:00:00 2001 From: Mike Voronov Date: Thu, 21 Dec 2023 16:51:15 +0300 Subject: [PATCH] feat(air-cli)!: introduce risc-0 extension (#681) --- .github/workflows/run-tests.yml | 7 + .github/workflows/tests.yml | 6 + Cargo.lock | 445 ++++++++++++++++-- .../air-lib/interpreter-signatures/src/lib.rs | 10 +- crates/risc-0/.gitignore | 4 + crates/risc-0/Cargo.toml | 19 + crates/risc-0/LICENSE | 201 ++++++++ crates/risc-0/core/Cargo.toml | 11 + crates/risc-0/core/src/lib.rs | 29 ++ crates/risc-0/methods/Cargo.toml | 17 + crates/risc-0/methods/build.rs | 20 + crates/risc-0/methods/guest/Cargo.toml | 18 + crates/risc-0/methods/guest/src/main.rs | 36 ++ crates/risc-0/methods/src/lib.rs | 18 + tools/cli/air/Cargo.toml | 6 + tools/cli/air/src/trace/run.rs | 53 ++- tools/cli/air/src/trace/run/risc0.rs | 113 +++++ 17 files changed, 946 insertions(+), 67 deletions(-) create mode 100644 crates/risc-0/.gitignore create mode 100644 crates/risc-0/Cargo.toml create mode 100644 crates/risc-0/LICENSE create mode 100644 crates/risc-0/core/Cargo.toml create mode 100644 crates/risc-0/core/src/lib.rs create mode 100644 crates/risc-0/methods/Cargo.toml create mode 100644 crates/risc-0/methods/build.rs create mode 100644 crates/risc-0/methods/guest/Cargo.toml create mode 100644 crates/risc-0/methods/guest/src/main.rs create mode 100644 crates/risc-0/methods/src/lib.rs create mode 100644 tools/cli/air/src/trace/run/risc0.rs diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 0e2a119f..d4d692e2 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -38,6 +38,13 @@ jobs: - name: Setup Rust toolchain uses: dsherret/rust-toolchain-file@v1 + - name: Setup risczero + uses: brndnmtthws/rust-action-cargo-binstall@v1 + with: + packages: cargo-risczero + + - run: cargo risczero install + - name: Setup cache uses: Swatinem/rust-cache@v2 with: diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index f8a2d78d..2d5f55ca 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -49,7 +49,13 @@ jobs: - uses: taiki-e/install-action@cargo-llvm-cov - uses: taiki-e/install-action@nextest + - name: Setup risczero + uses: brndnmtthws/rust-action-cargo-binstall@v1 + with: + packages: cargo-risczero + - run: cargo clean + - run: cargo risczero install - run: cargo build - name: Run cargo nextest diff --git a/Cargo.lock b/Cargo.lock index 3e83b3b5..f8030728 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,7 +125,7 @@ name = "air-beautifier" version = "0.4.0" dependencies = [ "aquavm-air-parser", - "itertools", + "itertools 0.10.5", "thiserror", ] @@ -239,7 +239,7 @@ dependencies = [ name = "air-lambda-ast" version = "0.1.0" dependencies = [ - "itertools", + "itertools 0.10.5", "non-empty-vec", "serde", ] @@ -249,7 +249,7 @@ name = "air-lambda-parser" version = "0.1.0" dependencies = [ "air-lambda-ast", - "itertools", + "itertools 0.10.5", "lalrpop", "lalrpop-util", "regex", @@ -294,7 +294,7 @@ dependencies = [ "air-test-utils", "aquavm-air-parser", "fluence-keypair", - "itertools", + "itertools 0.10.5", "maplit", "nom", "nom_locate", @@ -475,8 +475,9 @@ dependencies = [ "bs58 0.5.0", "clap 4.4.11", "fluence-keypair", - "itertools", + "itertools 0.10.5", "near-sdk", + "risc0-zkvm", "serde", "serde_json", "termcolor", @@ -485,6 +486,8 @@ dependencies = [ "tracing-subscriber", "workspaces", "zeroize", + "zk-aquavm-interface", + "zk-aquavm-methods", ] [[package]] @@ -496,7 +499,7 @@ dependencies = [ "codespan", "codespan-reporting", "criterion 0.4.0", - "itertools", + "itertools 0.10.5", "lalrpop", "lalrpop-util", "multimap 0.9.1", @@ -598,7 +601,7 @@ dependencies = [ "polling 2.8.0", "rustix 0.37.27", "slab", - "socket2", + "socket2 0.4.10", "waker-fn", ] @@ -700,9 +703,9 @@ dependencies = [ [[package]] name = "async-task" -version = "4.5.0" +version = "4.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1" +checksum = "e1d90cd0b264dfdd8eb5bad0a2c217c1f88fa96a8573f40e7b12de23fb468f46" [[package]] name = "async-trait" @@ -738,6 +741,15 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "autotools" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aef8da1805e028a172334c3b680f93e71126f2327622faef2ec3d893c0a4ad77" +dependencies = [ + "cc", +] + [[package]] name = "avm-data-store" version = "0.7.3" @@ -895,6 +907,15 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "blake2" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" +dependencies = [ + "digest 0.10.7", +] + [[package]] name = "blake2b_simd" version = "0.5.11" @@ -976,6 +997,17 @@ dependencies = [ "tracing", ] +[[package]] +name = "bonsai-sdk" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "441d1092e11977985946b6564251df91d80ae36982128e53be52a32548ad8762" +dependencies = [ + "reqwest", + "serde", + "thiserror", +] + [[package]] name = "borsh" version = "0.9.3" @@ -1117,6 +1149,26 @@ version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1e5f035d16fc623ae5f74981db80a439803888314e3a555fd6f04acd51a3205" +[[package]] +name = "bytemuck" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "374d28ec25809ee0e23827c2ab573d729e293f281dfe393500e7ad618baa61c6" +dependencies = [ + "bytemuck_derive", +] + +[[package]] +name = "bytemuck_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.41", +] + [[package]] name = "byteorder" version = "1.5.0" @@ -1128,6 +1180,9 @@ name = "bytes" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +dependencies = [ + "serde", +] [[package]] name = "bytesize" @@ -1169,6 +1224,15 @@ dependencies = [ "ppv-lite86", ] +[[package]] +name = "camino" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" +dependencies = [ + "serde", +] + [[package]] name = "cap-fs-ext" version = "1.0.15" @@ -1232,6 +1296,29 @@ dependencies = [ "winx 0.35.1", ] +[[package]] +name = "cargo-platform" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e34637b3140142bdf929fb439e8aa4ebad7651ebf7b1080b3930aa16ac1459ff" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7daec1a2a2129eeba1644b220b4647ec537b0b5d4bfd6876fcc5a540056b592" +dependencies = [ + "camino", + "cargo-platform", + "semver 1.0.20", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "cast" version = "0.3.0" @@ -1604,7 +1691,7 @@ dependencies = [ "cranelift-codegen", "cranelift-entity", "cranelift-frontend", - "itertools", + "itertools 0.10.5", "log", "smallvec", "wasmparser 0.100.0", @@ -1631,7 +1718,7 @@ dependencies = [ "clap 2.34.0", "criterion-plot 0.4.5", "csv", - "itertools", + "itertools 0.10.5", "lazy_static", "num-traits", "oorandom", @@ -1658,7 +1745,7 @@ dependencies = [ "ciborium", "clap 3.2.25", "criterion-plot 0.5.0", - "itertools", + "itertools 0.10.5", "lazy_static", "num-traits", "oorandom", @@ -1679,7 +1766,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876" dependencies = [ "cast", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -1689,7 +1776,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", - "itertools", + "itertools 0.10.5", ] [[package]] @@ -1802,7 +1889,7 @@ dependencies = [ "openssl-probe", "openssl-sys", "schannel", - "socket2", + "socket2 0.4.10", "winapi", ] @@ -2006,6 +2093,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer 0.10.4", + "const-oid", "crypto-common", "subtle", ] @@ -2081,6 +2169,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "docker-generate" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf673e0848ef09fa4aeeba78e681cf651c0c7d35f76ee38cec8e55bc32fa111" + +[[package]] +name = "downcast-rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" + [[package]] name = "dyn-clone" version = "1.0.16" @@ -2148,6 +2248,12 @@ version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" +[[package]] +name = "elf" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" + [[package]] name = "ena" version = "0.14.2" @@ -2860,9 +2966,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.27" +version = "0.14.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffb1cfd654a8219eaef89881fdb3bb3b1cdc5fa75ded05d6933b2b382e395468" +checksum = "bf96e135eb83a2a8ddf766e426a841d8ddd7449d5f00d34ea02b41d2f19eef80" dependencies = [ "bytes", "futures-channel", @@ -2875,7 +2981,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2", + "socket2 0.5.5", "tokio", "tower-service", "tracing", @@ -3092,6 +3198,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" @@ -3162,7 +3277,7 @@ dependencies = [ "diff", "ena", "is-terminal", - "itertools", + "itertools 0.10.5", "lalrpop-util", "petgraph", "pico-args", @@ -3204,6 +3319,12 @@ version = "0.2.151" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +[[package]] +name = "libm" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058" + [[package]] name = "libp2p-identity" version = "0.2.8" @@ -3419,7 +3540,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52907d7e7b9487a029dd3d99e647307c8808d7fba327e27113b0807d4868b315" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "marine-it-interfaces", "marine-module-interface", "marine-wasm-backend-traits", @@ -3501,7 +3622,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49d8f21255b94f1c8b2731a2b49c2ed34f955ed2e11dae3fb6c580a346d10bdd" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "marine-it-interfaces", "nom", "semver 1.0.20", @@ -3543,7 +3664,7 @@ dependencies = [ "bytesize", "it-json-serde", "it-memory-traits", - "itertools", + "itertools 0.10.5", "log", "marine-core", "marine-module-interface", @@ -3910,7 +4031,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7754612b47737d277fb818e9fdbb1406e90f9e57151c55c3584d714421976cb6" dependencies = [ "arrayref", - "blake2", + "blake2 0.9.2", "borsh 0.9.3", "bs58 0.4.0", "c2-chacha", @@ -3934,7 +4055,7 @@ version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff6b382b626e7e0cd372d027c6672ac97b4b6ee6114288c9e58d8180b935d315" dependencies = [ - "blake2", + "blake2 0.9.2", "borsh 0.10.3", "bs58 0.4.0", "c2-chacha", @@ -4385,6 +4506,17 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-derive" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfb77679af88f8b125209d354a202862602672222e7f2313fdd6dc349bad4712" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.41", +] + [[package]] name = "num-integer" version = "0.1.45" @@ -4551,7 +4683,7 @@ dependencies = [ "futures-util", "http", "opentelemetry", - "prost", + "prost 0.9.0", "thiserror", "tokio", "tonic", @@ -4859,6 +4991,16 @@ dependencies = [ "yansi", ] +[[package]] +name = "prettyplease" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae005bd773ab59b4725093fd7df83fd7892f7d8eafb48dbd7de6e024e4215f9d" +dependencies = [ + "proc-macro2", + "syn 2.0.41", +] + [[package]] name = "primitive-types" version = "0.10.1" @@ -4953,7 +5095,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "444879275cb4fd84958b1a1d5420d15e6fcf7c235fe47f053c9c2a80aceb6001" dependencies = [ "bytes", - "prost-derive", + "prost-derive 0.9.0", +] + +[[package]] +name = "prost" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +dependencies = [ + "bytes", + "prost-derive 0.12.3", ] [[package]] @@ -4964,18 +5116,40 @@ checksum = "62941722fb675d463659e49c4f3fe1fe792ff24fe5bbaa9c08cd3b98a1c354f5" dependencies = [ "bytes", "heck 0.3.3", - "itertools", + "itertools 0.10.5", "lazy_static", "log", "multimap 0.8.3", "petgraph", - "prost", - "prost-types", + "prost 0.9.0", + "prost-types 0.9.0", "regex", "tempfile", "which", ] +[[package]] +name = "prost-build" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c55e02e35260070b6f716a2423c2ff1c3bb1642ddca6f99e1f26d06268a0e2d2" +dependencies = [ + "bytes", + "heck 0.4.1", + "itertools 0.11.0", + "log", + "multimap 0.8.3", + "once_cell", + "petgraph", + "prettyplease", + "prost 0.12.3", + "prost-types 0.12.3", + "regex", + "syn 2.0.41", + "tempfile", + "which", +] + [[package]] name = "prost-derive" version = "0.9.0" @@ -4983,12 +5157,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9cc1a3263e07e0bf68e96268f37665207b49560d98739662cdfaae215c720fe" dependencies = [ "anyhow", - "itertools", + "itertools 0.10.5", "proc-macro2", "quote", "syn 1.0.109", ] +[[package]] +name = "prost-derive" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +dependencies = [ + "anyhow", + "itertools 0.11.0", + "proc-macro2", + "quote", + "syn 2.0.41", +] + [[package]] name = "prost-types" version = "0.9.0" @@ -4996,7 +5183,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "534b7a0e836e3c482d2693070f982e39e7611da9695d4d1f5a4b186b51faef0a" dependencies = [ "bytes", - "prost", + "prost 0.9.0", +] + +[[package]] +name = "prost-types" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e" +dependencies = [ + "prost 0.12.3", ] [[package]] @@ -5005,6 +5201,15 @@ version = "2.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94" +[[package]] +name = "protobuf-src" +version = "1.1.0+21.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7ac8852baeb3cc6fb83b93646fb93c0ffe5d14bf138c945ceb4b9948ee0e3c1" +dependencies = [ + "autotools", +] + [[package]] name = "psm" version = "0.1.21" @@ -5322,6 +5527,139 @@ dependencies = [ "digest 0.10.7", ] +[[package]] +name = "risc0-binfmt" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "923c85a23cb9a9475b8cd4479ad3a06252604a361626e9ae7dc0dc635af22c22" +dependencies = [ + "anyhow", + "elf", + "log", + "risc0-zkp", + "risc0-zkvm-platform", + "serde", +] + +[[package]] +name = "risc0-build" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e58d4cc25e243e52d1ccd75d357b0aa55081736bf3052c65a823fdf169586843" +dependencies = [ + "anyhow", + "cargo_metadata", + "docker-generate", + "risc0-binfmt", + "risc0-zkp", + "risc0-zkvm-platform", + "serde", + "serde_json", + "tempfile", +] + +[[package]] +name = "risc0-circuit-recursion" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97547e10e9fdaaab8b64ffb45dc158b31f023b1a68015c6ce9f12fe3e403012a" +dependencies = [ + "anyhow", + "bytemuck", + "log", + "risc0-core", + "risc0-zkp", + "tracing", +] + +[[package]] +name = "risc0-circuit-rv32im" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a269d01b18cba24ee1a08f68726fc3623e8705ed79d158377d12e9129dcde2e" +dependencies = [ + "anyhow", + "log", + "risc0-core", + "risc0-zkp", + "risc0-zkvm-platform", + "tracing", +] + +[[package]] +name = "risc0-core" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "477e0bb8d2ec0b7955088b521eb596901e652d0faa2ea73bda0b77e05af5c07d" +dependencies = [ + "bytemuck", + "rand_core 0.6.4", +] + +[[package]] +name = "risc0-zkp" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5abb1a0cf847d3f9aed1e563b76c358107e7ba66dbfab28f7144252c990bd82" +dependencies = [ + "anyhow", + "blake2 0.10.6", + "bytemuck", + "digest 0.10.7", + "hex 0.4.3", + "log", + "paste", + "rand_core 0.6.4", + "risc0-core", + "risc0-zkvm-platform", + "serde", + "sha2 0.10.8", + "tracing", +] + +[[package]] +name = "risc0-zkvm" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cf80df202c038efc2199be34fda8114b38bfc5b2b51c60cbbdf1f425b07b384" +dependencies = [ + "anyhow", + "bincode", + "bonsai-sdk", + "bytemuck", + "bytes", + "cfg-if 1.0.0", + "getrandom 0.2.11", + "hex 0.4.3", + "log", + "num-derive", + "num-traits", + "prost 0.12.3", + "prost-build 0.12.3", + "protobuf-src", + "risc0-binfmt", + "risc0-circuit-recursion", + "risc0-circuit-rv32im", + "risc0-core", + "risc0-zkp", + "risc0-zkvm-platform", + "rrs-lib", + "semver 1.0.20", + "serde", + "tracing", +] + +[[package]] +name = "risc0-zkvm-platform" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dcd6b66f7a4972001db0acf3f06d99b7851c8d9f0de1f7e0fb4496c66c5cd02" +dependencies = [ + "bytemuck", + "getrandom 0.2.11", + "libm", +] + [[package]] name = "rmp" version = "0.8.12" @@ -5344,6 +5682,16 @@ dependencies = [ "serde", ] +[[package]] +name = "rrs-lib" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4382d3af3a4ebdae7f64ba6edd9114fff92c89808004c4943b393377a25d001" +dependencies = [ + "downcast-rs", + "paste", +] + [[package]] name = "rust-argon2" version = "0.8.3" @@ -5866,6 +6214,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "socket2" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "spin" version = "0.5.2" @@ -6226,7 +6584,7 @@ dependencies = [ "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", - "socket2", + "socket2 0.4.10", "tokio-macros", "windows-sys 0.48.0", ] @@ -6368,8 +6726,8 @@ dependencies = [ "hyper-timeout", "percent-encoding", "pin-project", - "prost", - "prost-derive", + "prost 0.9.0", + "prost-derive 0.9.0", "tokio", "tokio-stream", "tokio-util 0.6.10", @@ -6387,7 +6745,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9403f1bafde247186684b230dc6f38b5cd514584e8bec1dd32514be4745fa757" dependencies = [ "proc-macro2", - "prost-build", + "prost-build 0.9.0", "quote", "syn 1.0.109", ] @@ -6925,7 +7283,7 @@ dependencies = [ "it-lilo", "it-memory-traits", "it-to-bytes", - "itertools", + "itertools 0.10.5", "log", "nom", "safe-transmute", @@ -7700,6 +8058,21 @@ dependencies = [ "time 0.1.45", ] +[[package]] +name = "zk-aquavm-interface" +version = "0.1.0" +dependencies = [ + "aquavm-air", + "serde", +] + +[[package]] +name = "zk-aquavm-methods" +version = "0.1.0" +dependencies = [ + "risc0-build", +] + [[package]] name = "zstd" version = "0.11.2+zstd.1.5.2" diff --git a/crates/air-lib/interpreter-signatures/src/lib.rs b/crates/air-lib/interpreter-signatures/src/lib.rs index 12ee6c18..1c83240c 100644 --- a/crates/air-lib/interpreter-signatures/src/lib.rs +++ b/crates/air-lib/interpreter-signatures/src/lib.rs @@ -143,13 +143,6 @@ impl KeyPair { pub fn as_inner(&self) -> &fluence_keypair::KeyPair { &self.0 } - - #[cfg(feature = "rand")] - pub fn generate(key_format: KeyFormat) -> Result { - validate_with_key_format((), key_format)?; - - Ok(Self(fluence_keypair::KeyPair::generate(key_format))) - } } impl TryFrom for KeyPair { @@ -167,6 +160,9 @@ impl From for fluence_keypair::KeyPair { } pub(crate) fn validate_with_key_format(inner: V, key_format: KeyFormat) -> Result { + // this allow is needed in order to support old versions of the fluence_keypair + // repos which is used to build it for RISC-0 + #[allow(unreachable_patterns)] match key_format { fluence_keypair::KeyFormat::Ed25519 => Ok(inner), _ => Err(KeyError::AlgorithmNotWhitelisted(key_format)), diff --git a/crates/risc-0/.gitignore b/crates/risc-0/.gitignore new file mode 100644 index 00000000..f4247e18 --- /dev/null +++ b/crates/risc-0/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +Cargo.lock +methods/guest/Cargo.lock +target/ diff --git a/crates/risc-0/Cargo.toml b/crates/risc-0/Cargo.toml new file mode 100644 index 00000000..f727548f --- /dev/null +++ b/crates/risc-0/Cargo.toml @@ -0,0 +1,19 @@ +[workspace] +members = [ + "core", + "methods", +] + +# Always optimize; building and running the guest takes much longer without optimization. +[profile.dev] +opt-level = 3 + +[profile.dev.build-override] +opt-level = 3 + +[profile.release] +debug = 1 +lto = true + +[profile.release.build-override] +opt-level = 3 diff --git a/crates/risc-0/LICENSE b/crates/risc-0/LICENSE new file mode 100644 index 00000000..261eeb9e --- /dev/null +++ b/crates/risc-0/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/crates/risc-0/core/Cargo.toml b/crates/risc-0/core/Cargo.toml new file mode 100644 index 00000000..5ca6cd38 --- /dev/null +++ b/crates/risc-0/core/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "zk-aquavm-interface" +version = "0.1.0" +description = "Interface for RISC-0 AquaVM wrapper" +authors = ["Fluence Labs"] +edition = "2021" +license = "Apache-2.0" + +[dependencies] +aquavm-air = { path = "../../../air", version = "0.55.0", default-features = false } +serde = "1.0" diff --git a/crates/risc-0/core/src/lib.rs b/crates/risc-0/core/src/lib.rs new file mode 100644 index 00000000..1e1bbf6f --- /dev/null +++ b/crates/risc-0/core/src/lib.rs @@ -0,0 +1,29 @@ +/* + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use air::RunParameters; + +use serde::Deserialize; +use serde::Serialize; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct AquaVMProvingParameters { + pub air: String, + pub prev_data: Vec, + pub current_data: Vec, + pub run_params: RunParameters, + pub call_results: Vec, +} diff --git a/crates/risc-0/methods/Cargo.toml b/crates/risc-0/methods/Cargo.toml new file mode 100644 index 00000000..2c5f6a91 --- /dev/null +++ b/crates/risc-0/methods/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "zk-aquavm-methods" +version = "0.1.0" +description = "RISC-0 methods wrapper for AquaVM" +authors = ["Fluence Labs"] +edition = "2021" +license = "Apache-2.0" + +[build-dependencies] +risc0-build = { version = "0.19.1" } + +[package.metadata.risc0] +methods = ["guest"] + +[features] +# this features is needed to allow AquaVM compilation without installed risczero CLI tool +enable_risc_0_compilation = [] diff --git a/crates/risc-0/methods/build.rs b/crates/risc-0/methods/build.rs new file mode 100644 index 00000000..6f3d7b9d --- /dev/null +++ b/crates/risc-0/methods/build.rs @@ -0,0 +1,20 @@ +/* + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +fn main() { + #[cfg(feature = "enable_risc_0_compilation")] + risc0_build::embed_methods(); +} diff --git a/crates/risc-0/methods/guest/Cargo.toml b/crates/risc-0/methods/guest/Cargo.toml new file mode 100644 index 00000000..e4c91dc5 --- /dev/null +++ b/crates/risc-0/methods/guest/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "zk-aquavm" +version = "0.1.0" +description = "RISC-0 guest wrapper for AquaVM" +authors = ["Fluence Labs"] +edition = "2021" +license = "Apache-2.0" + +[workspace] + +[dependencies] +risc0-zkvm = { version = "0.19.1", default-features = false, features = [ "std" ] } +zk-aquavm-interface = { path = "../../core", version = "0.1.0" } +aquavm-air = { path = "../../../../air", version = "0.55.0" } + +[patch.crates-io] +fluence-keypair = { git = "https://github.com/fluencelabs/trust-graph.git", branch = "lean-keypair-2" } +libp2p-identity = { git = "https://github.com/fluencelabs/rust-libp2p.git", branch = "rand-feature" } diff --git a/crates/risc-0/methods/guest/src/main.rs b/crates/risc-0/methods/guest/src/main.rs new file mode 100644 index 00000000..4dcec57f --- /dev/null +++ b/crates/risc-0/methods/guest/src/main.rs @@ -0,0 +1,36 @@ +/* + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#![no_main] + +use air::execute_air; +use risc0_zkvm::guest::env; +use zk_aquavm_interface::AquaVMProvingParameters; + +risc0_zkvm::guest::entry!(main); + +pub fn main() { + let parameters: AquaVMProvingParameters = env::read(); + + let result = execute_air( + parameters.air, + parameters.prev_data, + parameters.current_data, + parameters.run_params, + parameters.call_results.into(), + ); + env::commit(&result); +} diff --git a/crates/risc-0/methods/src/lib.rs b/crates/risc-0/methods/src/lib.rs new file mode 100644 index 00000000..ef244f99 --- /dev/null +++ b/crates/risc-0/methods/src/lib.rs @@ -0,0 +1,18 @@ +/* + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#[cfg(feature = "enable_risc_0_compilation")] +include!(concat!(env!("OUT_DIR"), "/methods.rs")); diff --git a/tools/cli/air/Cargo.toml b/tools/cli/air/Cargo.toml index fed52033..9afc9661 100644 --- a/tools/cli/air/Cargo.toml +++ b/tools/cli/air/Cargo.toml @@ -38,12 +38,18 @@ near-sdk = { version = "5.0.0-alpha.1", optional = true } tokio = { version = "1", features = ["rt"], optional = true } workspaces = { version = "0.7.0", optional = true } +# risc-0 +risc0-zkvm = { version = "0.19.1", optional = true } +zk-aquavm-methods = { path = "../../../crates/risc-0/methods", version = "0.1.0", features = ["enable_risc_0_compilation"], optional = true } +zk-aquavm-interface = { path = "../../../crates/risc-0/core", version = "0.1.0", optional = true } + [features] default = ["wasm"] wasm = ["air-test-utils"] gen_signatures = ["aquavm-air/gen_signatures"] check_signatures = ["aquavm-air/check_signatures"] near = [ "dep:near-sdk", "dep:tokio", "dep:workspaces" ] +risc0 = [ "dep:risc0-zkvm", "dep:zk-aquavm-methods", "dep:zk-aquavm-interface" ] [[bin]] name = "air" diff --git a/tools/cli/air/src/trace/run.rs b/tools/cli/air/src/trace/run.rs index 68e8b60f..0a792b75 100644 --- a/tools/cli/air/src/trace/run.rs +++ b/tools/cli/air/src/trace/run.rs @@ -18,6 +18,8 @@ mod data; mod native; #[cfg(feature = "near")] mod near; +#[cfg(feature = "risc0")] +mod risc0; mod runner; #[cfg(feature = "wasm")] mod wasm; @@ -121,6 +123,10 @@ struct ModeArgs { #[cfg(feature = "near")] #[arg(long)] near: bool, + + #[cfg(feature = "risc0")] + #[arg(long)] + risc0: bool, } impl From for Option { @@ -139,6 +145,11 @@ impl From for Option { return Some(Mode::Near); } + #[cfg(feature = "risc0")] + if value.risc0 { + return Some(Mode::Risc0); + } + None } } @@ -151,6 +162,9 @@ enum Mode { #[cfg(feature = "near")] Near, + + #[cfg(feature = "risc0")] + Risc0, } pub(crate) fn run(args: Args) -> anyhow::Result<()> { @@ -166,7 +180,7 @@ pub(crate) fn run(args: Args) -> anyhow::Result<()> { let global_tracing_params = args.tracing_params.clone(); init_tracing(global_tracing_params, tracing_json); - let mut runner = get_runner( + let mut runner = create_runner( args.mode.into(), &args.air_interpreter_path, &args.air_near_contract_path, @@ -215,41 +229,32 @@ pub(crate) fn run(args: Args) -> anyhow::Result<()> { Ok(()) } -#[cfg(feature = "wasm")] -fn get_runner( - mode: Option, - air_interpreter_wasm_path: &Path, - _air_contract_wasm_path: &Path, - max_heap_size: Option, -) -> anyhow::Result> { - let mode = mode.unwrap_or(Mode::Wasm); - match mode { - Mode::Native => { - self::native::create_native_avm_runner().context("Failed to instantiate a native AVM") - } - Mode::Wasm => self::wasm::create_wasm_avm_runner(air_interpreter_wasm_path, max_heap_size) - .context("Failed to instantiate WASM AVM"), - #[cfg(feature = "near")] - Mode::Near => self::near::create_near_runner(_air_contract_wasm_path) - .context("Failed to instantiate NEAR AVM"), - } -} - -#[cfg(not(feature = "wasm"))] -fn get_runner( +fn create_runner( mode: Option, _air_interpreter_wasm_path: &Path, _air_contract_wasm_path: &Path, _max_heap_size: Option, ) -> anyhow::Result> { - let mode = mode.unwrap_or(Mode::Native); + #[cfg(not(feature = "wasm"))] + let default_mode = Mode::Native; + #[cfg(feature = "wasm")] + let default_mode = Mode::Wasm; + + let mode = mode.unwrap_or(default_mode); match mode { Mode::Native => { self::native::create_native_avm_runner().context("Failed to instantiate a native AVM") } + #[cfg(feature = "wasm")] + Mode::Wasm => { + self::wasm::create_wasm_avm_runner(_air_interpreter_wasm_path, _max_heap_size) + .context("Failed to instantiate WASM AVM") + } #[cfg(feature = "near")] Mode::Near => self::near::create_near_runner(_air_contract_wasm_path) .context("Failed to instantiate NEAR AVM"), + #[cfg(feature = "risc0")] + Mode::Risc0 => Ok(Box::new(self::risc0::Risc0Runner::new())), } } diff --git a/tools/cli/air/src/trace/run/risc0.rs b/tools/cli/air/src/trace/run/risc0.rs new file mode 100644 index 00000000..82f06405 --- /dev/null +++ b/tools/cli/air/src/trace/run/risc0.rs @@ -0,0 +1,113 @@ +/* + * Copyright 2023 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use super::runner::AirRunner; + +use air_interpreter_interface::InterpreterOutcome; +use air_interpreter_interface::RunParameters; +use avm_interface::into_raw_result; +use avm_interface::raw_outcome::RawAVMOutcome; +use fluence_keypair::KeyPair; +use zk_aquavm_interface::AquaVMProvingParameters; + +use risc0_zkvm::default_prover; +use risc0_zkvm::ExecutorEnv; +use zk_aquavm_methods::ZK_AQUAVM_ELF; +use zk_aquavm_methods::ZK_AQUAVM_ID; + +use std::time::Instant; + +pub struct Risc0Runner {} + +impl Risc0Runner { + pub(crate) fn new() -> Self { + Self {} + } +} + +impl AirRunner for Risc0Runner { + fn call_tracing( + &mut self, + air: String, + prev_data: Vec, + current_data: Vec, + init_peer_id: String, + timestamp: u64, + ttl: u32, + current_peer_id: String, + call_results: avm_interface::CallResults, + _tracing_params: String, + _tracing_output_mode: u8, + keypair: &KeyPair, + particle_id: String, + ) -> anyhow::Result { + let key_format = keypair.key_format().into(); + let secret_key_bytes = keypair.secret().expect("Failed to get secret key"); + + let run_params = RunParameters { + init_peer_id, + current_peer_id, + timestamp, + ttl, + key_format, + secret_key_bytes, + particle_id, + }; + + let call_results = into_raw_result(call_results); + let call_results = serde_json::to_vec(&call_results)?; + + let arguments = AquaVMProvingParameters { + air, + prev_data, + current_data, + run_params, + call_results, + }; + + execute_on_risc0(arguments) + } +} + +fn execute_on_risc0(arguments: AquaVMProvingParameters) -> anyhow::Result { + use risc0_zkvm::serde::from_slice; + + let env = ExecutorEnv::builder().write(&arguments)?.build()?; + + let prover = default_prover(); + + eprintln!("default prover created, proving is started"); + + let session_timer = Instant::now(); + let receipt = prover.prove_elf(env, ZK_AQUAVM_ELF)?; + let session_duration = session_timer.elapsed(); + + eprintln!("proving finished:"); + eprintln!(" elapsed time {:?}", session_duration); + eprintln!(" journal size {}", receipt.journal.bytes.len()); + + let verification_timer = Instant::now(); + receipt.verify(ZK_AQUAVM_ID)?; + let verification_duration = verification_timer.elapsed(); + + eprintln!( + "verification successfully finished in {:?}", + verification_duration + ); + + let outcome: InterpreterOutcome = from_slice(&receipt.journal.bytes)?; + Ok(RawAVMOutcome::from_interpreter_outcome(outcome)?) +}