diff --git a/Cargo.lock b/Cargo.lock index 4b4ebe391..e03df0170 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -931,6 +931,7 @@ dependencies = [ "mozak-circuits", "mozak-examples", "mozak-node", + "mozak-prover-sdk", "mozak-runner", "mozak-sdk", "plonky2", @@ -960,6 +961,19 @@ dependencies = [ "serde", ] +[[package]] +name = "mozak-prover-sdk" +version = "0.1.0" +dependencies = [ + "anyhow", + "itertools 0.12.1", + "mozak-circuits", + "mozak-runner", + "mozak-sdk", + "plonky2", + "starky", +] + [[package]] name = "mozak-recproofs" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index c8a0014ba..5d2623311 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ members = [ "examples-builder", "expr", "node", + "prover-sdk", "recproofs", "runner", "signatures", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ef5bda002..afcb17202 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -18,6 +18,7 @@ clap = { version = "4.5", features = [ ] } mozak-circuits = { path = "../circuits", features = ["test"] } mozak-node = { path = "../node", features = ["std"] } +mozak-prover-sdk = { path = "../prover-sdk" } mozak-runner = { path = "../runner", features = ["test"] } mozak-sdk = { path = "../sdk", features = ["std"] } # TODO(Matthias): implement shell completion for CLI via clap_complete diff --git a/cli/src/main.rs b/cli/src/main.rs index d8bd9c340..03d80e975 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -29,6 +29,7 @@ use mozak_circuits::test_utils::{prove_and_verify_mozak_stark, C, D, F, S}; use mozak_cli::cli_benches::benches::BenchArgs; use mozak_cli::runner::{deserialize_system_tape, load_program, raw_tapes_from_system_tape}; use mozak_node::types::{Attestation, Transaction}; +use mozak_prover_sdk::prog_id::ProgId; use mozak_runner::state::{RawTapes, State}; use mozak_runner::vm::step; use mozak_sdk::common::types::{CrossProgramCall, ProgramIdentifier, SystemTape}; @@ -78,7 +79,9 @@ pub struct ProveArgs { #[derive(Clone, Debug, Subcommand)] enum Command { /// Decode a given ELF and prints the program - Decode { elf: Input }, + Decode { + elf: Input, + }, /// Decode and execute a given ELF. Prints the final state of /// the registers Run(RunArgs), @@ -87,9 +90,14 @@ enum Command { /// Prove the execution of given ELF and write proof to file. Prove(ProveArgs), /// Verify the given proof from file. - Verify { proof: Input }, + Verify { + proof: Input, + }, /// Verify the given recursive proof from file. - VerifyRecursiveProof { proof: Input, verifier_key: Input }, + VerifyRecursiveProof { + proof: Input, + verifier_key: Input, + }, /// Builds a transaction bundle. BundleTransaction { /// System tape generated from native execution. @@ -100,11 +108,18 @@ enum Command { bundle: Output, }, /// Compute the Program Rom Hash of the given ELF. - ProgramRomHash { elf: Input }, + ProgramRomHash { + elf: Input, + }, /// Compute the Memory Init Hash of the given ELF. - MemoryInitHash { elf: Input }, + MemoryInitHash { + elf: Input, + }, /// Bench the function with given parameters Bench(BenchArgs), + SelfProgId { + elf: Input, + }, } /// Run me eg like `cargo run -- -vvv run vm/tests/testdata/rv32ui-p-addi @@ -435,6 +450,11 @@ fn main() -> Result<()> { let time_taken = bench.bench()?.as_secs_f64(); println!("{time_taken}"); } + Command::SelfProgId { elf } => { + let elf_path = elf.path().to_str().unwrap(); + let prog_id: ProgramIdentifier = ProgId::from_elf(elf_path)?.into(); + println!("{prog_id:?}"); + } } Ok(()) } diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 7302d4a65..78f0c686f 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -15,12 +15,70 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + [[package]] name = "allocator-api2" version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.83" @@ -36,12 +94,33 @@ dependencies = [ "nodrop", ] +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "bitfield" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c821a6e124197eb56d907ccc2188eab1038fb919c914f47976e64dd8dbc855d1" + +[[package]] +name = "bitmaps" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "031043d04099746d8db04daf1fa424b2bc8bd69d92b25962dcde24da39ab64a2" +dependencies = [ + "typenum", +] + [[package]] name = "bitvec" version = "1.0.1" @@ -76,12 +155,24 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "cc" +version = "1.0.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "099a5357d84c4c61eb35fc8eafa9a79a902c2f76911e5747ced4e032edd8d9b4" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + [[package]] name = "const-random" version = "0.1.18" @@ -143,6 +234,12 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" +[[package]] +name = "elf" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" + [[package]] name = "empty" version = "0.1.0" @@ -150,6 +247,37 @@ dependencies = [ "mozak-sdk", ] +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + +[[package]] +name = "expr" +version = "0.1.0" +dependencies = [ + "bumpalo", + "starky", +] + [[package]] name = "fibonacci" version = "0.1.0" @@ -209,16 +337,44 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "im" +version = "15.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0acd33ff0285af998aaf9b57342af478078f53492322fafc47450e09397e0e9" +dependencies = [ + "bitmaps", + "rand_core", + "rand_xoshiro", + "serde", + "sized-chunks", + "typenum", + "version_check", +] + [[package]] name = "inputtape" version = "0.1.0" dependencies = [ "hex", + "mozak-prover-sdk", "mozak-sdk", "rkyv", "rkyv_derive", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itertools" version = "0.12.1" @@ -249,6 +405,16 @@ version = "0.2.154" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +[[package]] +name = "libmimalloc-sys" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81eb4061c0582dedea1cbc7aff2240300dd6982e0239d1c99e65c1dbf4a30ba7" +dependencies = [ + "cc", + "libc", +] + [[package]] name = "log" version = "0.4.21" @@ -261,6 +427,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" +[[package]] +name = "memchr" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" + [[package]] name = "memory-access" version = "0.1.0" @@ -268,6 +440,15 @@ dependencies = [ "mozak-sdk", ] +[[package]] +name = "mimalloc" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f41a2280ded0da56c8cf898babb86e8f10651a34adcfff190ae9a1159c6908d" +dependencies = [ + "libmimalloc-sys", +] + [[package]] name = "min-max" version = "0.1.0" @@ -275,6 +456,69 @@ dependencies = [ "mozak-sdk", ] +[[package]] +name = "mozak-circuits" +version = "0.1.0" +dependencies = [ + "anyhow", + "arrayvec", + "bitfield", + "expr", + "itertools", + "log", + "mozak-circuits-derive", + "mozak-runner", + "mozak-sdk", + "plonky2", + "plonky2_maybe_rayon", + "serde", + "serde_json", + "starky", + "thiserror", + "tt-call", +] + +[[package]] +name = "mozak-circuits-derive" +version = "0.1.0" +dependencies = [ + "itertools", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "mozak-prover-sdk" +version = "0.1.0" +dependencies = [ + "anyhow", + "itertools", + "mozak-circuits", + "mozak-runner", + "mozak-sdk", + "plonky2", + "starky", +] + +[[package]] +name = "mozak-runner" +version = "0.1.0" +dependencies = [ + "anyhow", + "bitfield", + "elf", + "env_logger", + "im", + "itertools", + "log", + "mimalloc", + "mozak-sdk", + "plonky2", + "serde", +] + [[package]] name = "mozak-sdk" version = "0.2.0" @@ -448,6 +692,28 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro-error" +version = "2.0.0" +source = "git+https://github.com/staratlasmeta/proc-macro-error.git#32fb626a74daf1af81b28f9c41b296a19d92344f" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "git+https://github.com/staratlasmeta/proc-macro-error.git#32fb626a74daf1af81b28f9c41b296a19d92344f" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.82" @@ -531,6 +797,44 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rand_xoshiro" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" +dependencies = [ + "rand_core", +] + +[[package]] +name = "regex" +version = "1.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" + [[package]] name = "rend" version = "0.5.0-pre6" @@ -640,6 +944,16 @@ dependencies = [ "digest", ] +[[package]] +name = "sized-chunks" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16d69225bde7a69b235da73377861095455d298f2b970996eec25ddbb42b3d1e" +dependencies = [ + "bitmaps", + "typenum", +] + [[package]] name = "slice-group-by" version = "0.3.1" @@ -655,6 +969,22 @@ dependencies = [ "maybe-uninit", ] +[[package]] +name = "starky" +version = "0.4.0" +source = "git+https://github.com/0xmozak/plonky2.git#13cb36256626f53134758894897010c1506acd80" +dependencies = [ + "ahash", + "anyhow", + "hashbrown", + "itertools", + "log", + "num-bigint", + "plonky2", + "plonky2_maybe_rayon", + "plonky2_util", +] + [[package]] name = "static-mem-access" version = "0.1.0" @@ -685,6 +1015,26 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" +[[package]] +name = "thiserror" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -714,12 +1064,19 @@ name = "token" version = "0.1.0" dependencies = [ "hex", + "mozak-prover-sdk", "mozak-sdk", "rkyv", "rkyv_derive", "wallet", ] +[[package]] +name = "tt-call" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f195fd851901624eee5a58c4bb2b4f06399148fcd0ed336e6f1cb60a9881df" + [[package]] name = "typenum" version = "1.17.0" @@ -741,6 +1098,12 @@ dependencies = [ "syn", ] +[[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" @@ -752,6 +1115,7 @@ name = "wallet" version = "0.1.0" dependencies = [ "hex", + "mozak-prover-sdk", "mozak-sdk", "rand", "rand_chacha", @@ -819,6 +1183,79 @@ version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + [[package]] name = "wyz" version = "0.5.1" @@ -847,8 +1284,3 @@ dependencies = [ "quote", "syn", ] - -[[patch.unused]] -name = "starky" -version = "0.4.0" -source = "git+https://github.com/0xmozak/plonky2.git#13cb36256626f53134758894897010c1506acd80" diff --git a/examples/inputtape/Cargo.toml b/examples/inputtape/Cargo.toml index 7f7f0bee6..81ed9956b 100644 --- a/examples/inputtape/Cargo.toml +++ b/examples/inputtape/Cargo.toml @@ -12,6 +12,9 @@ mozak-sdk = { path = "../../sdk" } rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } rkyv_derive = "=0.8.0-alpha.1" +[target.'cfg(not(target_os="mozakvm"))'.dependencies] +mozak-prover-sdk = { path = "../../prover-sdk" } + [target.'cfg(not(target_os="mozakvm"))'.dependencies.hex] version = "0.4" @@ -30,4 +33,4 @@ path = "core_logic.rs" # The following is read by `run_examples.py` [package.metadata.mozak] -example_program_id = "MZK-5de9bc1bb4cb7a9f9cf991a32230d014b44274ea5464a352f089a8cccc486915" +example_program_id = "MZK-96d92ca8abef82f6c8091e76a38607d673fa6ce6f234fdfc8c12d75d04f12644" diff --git a/examples/inputtape/main_native.rs b/examples/inputtape/main_native.rs index 25600f7c2..000d75c42 100644 --- a/examples/inputtape/main_native.rs +++ b/examples/inputtape/main_native.rs @@ -2,12 +2,16 @@ #![allow(unused_attributes)] mod core_logic; +use mozak_prover_sdk::prog_id::{ProgId, ELF_DIR}; use mozak_sdk::common::types::{Poseidon2Hash, ProgramIdentifier}; use crate::core_logic::{dispatch, MethodArgs}; fn main() { - let token_program = ProgramIdentifier::new_from_rand_seed(3); + let token_program: ProgramIdentifier = + ProgId::from_elf(&format!("{}{}", ELF_DIR, "inputtapebin")) + .unwrap() + .into(); let buf1 = Poseidon2Hash::new_from_rand_seed(2).inner(); let buf2 = buf1.iter().map(|x| x.wrapping_add(1)).collect::>(); diff --git a/examples/programs_map.json b/examples/programs_map.json index e3f2e03b2..976b9fb0b 100644 --- a/examples/programs_map.json +++ b/examples/programs_map.json @@ -1,14 +1,14 @@ [ { - "name": "MZK-b10da48cea4c09676b8e0efcd806941465060736032bb898420d0863dca72538", + "name": "MZK-a530e8d822cea8fbb0f0a166c9058befb36af2ad3a17d1b9cbf409962e73a04f", "path": "examples/target/riscv32im-mozak-mozakvm-elf/release/tokenbin" }, { - "name": "MZK-c51b8a31c98b9fe13065b485c9f8658c194c430843570ccac2720a3b30b47adb", + "name": "MZK-8475b3990f7d99e8380c6597dcf445e42235b9fef6951a91cf546ac9a319a710", "path": "examples/target/riscv32im-mozak-mozakvm-elf/release/walletbin" }, { - "name": "MZK-5de9bc1bb4cb7a9f9cf991a32230d014b44274ea5464a352f089a8cccc486915", + "name": "MZK-96d92ca8abef82f6c8091e76a38607d673fa6ce6f234fdfc8c12d75d04f12644", "path": "examples/target/riscv32im-mozak-mozakvm-elf/release/inputtape" } ] diff --git a/examples/token/Cargo.toml b/examples/token/Cargo.toml index 9d1140c64..511db413c 100644 --- a/examples/token/Cargo.toml +++ b/examples/token/Cargo.toml @@ -13,6 +13,9 @@ rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["poin rkyv_derive = "=0.8.0-alpha.1" wallet = { path = "../wallet" } +[target.'cfg(not(target_os="mozakvm"))'.dependencies] +mozak-prover-sdk = { path = "../../prover-sdk" } + [target.'cfg(not(target_os="mozakvm"))'.dependencies.hex] version = "0.4" @@ -33,4 +36,4 @@ path = "core_logic.rs" # The following is read by `run_examples.py` [package.metadata.mozak] example_dependents = ["wallet"] -example_program_id = "MZK-b10da48cea4c09676b8e0efcd806941465060736032bb898420d0863dca72538" +example_program_id = "MZK-a530e8d822cea8fbb0f0a166c9058befb36af2ad3a17d1b9cbf409962e73a04f" diff --git a/examples/token/main_native.rs b/examples/token/main_native.rs index e597d0813..b2584b8e6 100644 --- a/examples/token/main_native.rs +++ b/examples/token/main_native.rs @@ -1,12 +1,15 @@ #![feature(restricted_std)] #![allow(unused_attributes)] mod core_logic; +use mozak_prover_sdk::prog_id::{ProgId, ELF_DIR}; use mozak_sdk::common::types::{ProgramIdentifier, StateAddress, StateObject}; use rkyv::rancor::Panic; use token::{dispatch, MethodArgs}; fn main() { - let token_program = ProgramIdentifier::new_from_rand_seed(1); + let token_program: ProgramIdentifier = ProgId::from_elf(&format!("{}{}", ELF_DIR, "tokenbin")) + .unwrap() + .into(); // We assume both wallet are the same program for now let remitter_program = ProgramIdentifier::new_from_rand_seed(2); diff --git a/examples/wallet/Cargo.toml b/examples/wallet/Cargo.toml index 811ffa730..844a0855c 100644 --- a/examples/wallet/Cargo.toml +++ b/examples/wallet/Cargo.toml @@ -12,8 +12,10 @@ mozak-sdk = { path = "../../sdk" } rkyv = { version = "=0.8.0-alpha.1", default-features = false, features = ["pointer_width_32", "alloc"] } rkyv_derive = "=0.8.0-alpha.1" + [target.'cfg(not(target_os="mozakvm"))'.dependencies] hex = "0.4" +mozak-prover-sdk = { path = "../../prover-sdk" } rand = "0.8" rand_chacha = "0.3" @@ -32,4 +34,4 @@ path = "core_logic.rs" # The following is read by `run_examples.py` [package.metadata.mozak] -example_program_id = "MZK-c51b8a31c98b9fe13065b485c9f8658c194c430843570ccac2720a3b30b47adb" +example_program_id = "MZK-8475b3990f7d99e8380c6597dcf445e42235b9fef6951a91cf546ac9a319a710" diff --git a/examples/wallet/main_native.rs b/examples/wallet/main_native.rs index febfb33da..3288303e4 100644 --- a/examples/wallet/main_native.rs +++ b/examples/wallet/main_native.rs @@ -2,13 +2,16 @@ #![allow(unused_attributes)] mod core_logic; - +use mozak_prover_sdk::prog_id::{ProgId, ELF_DIR}; use mozak_sdk::common::types::ProgramIdentifier; use crate::core_logic::{dispatch, BlackBox, MethodArgs, PublicKey, TokenObject}; fn main() { - let wallet_program = ProgramIdentifier::new_from_rand_seed(2); + let wallet_program: ProgramIdentifier = + ProgId::from_elf(&format!("{}{}", ELF_DIR, "walletbin")) + .unwrap() + .into(); let remitter_program = ProgramIdentifier::new_from_rand_seed(20); let remittee_program = ProgramIdentifier::new_from_rand_seed(21); let public_key = PublicKey::new_from_rand_seed(4); diff --git a/prover-sdk/Cargo.toml b/prover-sdk/Cargo.toml new file mode 100644 index 000000000..34ef15910 --- /dev/null +++ b/prover-sdk/Cargo.toml @@ -0,0 +1,19 @@ +[package] +categories = ["development-tools", "zk"] +description = "Provides tools for interacting with the proving system" +edition = "2021" +keywords = ["sdk", "prover"] +license = "Apache-2.0" +name = "mozak-prover-sdk" +readme = "README.md" +repository = "https://github.com/0xmozak/mozak-node/prover_sdk" +version = "0.1.0" + +[dependencies] +anyhow = "1.0.83" +itertools = "0.12.1" +mozak-circuits = { path = "../circuits" } +mozak-runner = { path = "../runner" } +mozak-sdk = { path = "../sdk" } +plonky2 = { workspace = true, default-features = false } +starky = { workspace = true, default-features = false } diff --git a/prover-sdk/src/lib.rs b/prover-sdk/src/lib.rs new file mode 100644 index 000000000..9c74bd57a --- /dev/null +++ b/prover-sdk/src/lib.rs @@ -0,0 +1 @@ +pub mod prog_id; diff --git a/prover-sdk/src/prog_id.rs b/prover-sdk/src/prog_id.rs new file mode 100644 index 000000000..7e25934bd --- /dev/null +++ b/prover-sdk/src/prog_id.rs @@ -0,0 +1,89 @@ +use itertools::{chain, Itertools}; +use mozak_circuits::memoryinit::generation::generate_elf_memory_init_trace; +use mozak_circuits::program::generation::generate_program_rom_trace; +use mozak_circuits::stark::utils::trace_rows_to_poly_values; +use mozak_runner::elf::Program; +use mozak_sdk::common::types::{Poseidon2Hash, ProgramIdentifier}; +use plonky2::field::extension::Extendable; +use plonky2::field::types::Field; +use plonky2::fri::oracle::PolynomialBatch; +use plonky2::hash::hash_types::RichField; +use plonky2::plonk::config::{ + AlgebraicHasher, GenericConfig, GenericHashOut, Hasher, Poseidon2GoldilocksConfig, +}; +use plonky2::util::timing::TimingTree; +use starky::config::StarkConfig; + +pub struct ProgId(Poseidon2Hash); + +// TODO: have a separate type for these in circuits +pub const PRODUCTION_STARK_CONFIG: StarkConfig = StarkConfig::standard_fast_config(); +pub type ProductionGenericConfig = Poseidon2GoldilocksConfig; +pub type F = >::F; +pub const D: usize = 2; +// TODO: remove this once we have a way to access ELF path inside native +pub const ELF_DIR: &str = "../target/riscv32im-mozak-mozakvm-elf/release/"; // relative to example in /examples + +impl ProgId { + pub fn inner(&self) -> Poseidon2Hash { self.0 } + + /// Create [ProgId] from `program` using production configs for stark + /// and recursive plonky2 prover + pub fn from_production_configs(program: &Program) -> Self { + let entry_point = F::from_canonical_u32(program.entry_point); + let elf_memory_init_trace = generate_elf_memory_init_trace::(program); + let program_rom_trace = generate_program_rom_trace::(program); + let elf_memory_init_hash = get_trace_commitment_hash::( + elf_memory_init_trace, + &PRODUCTION_STARK_CONFIG, + ); + let program_hash = get_trace_commitment_hash::( + program_rom_trace, + &PRODUCTION_STARK_CONFIG, + ); + let hashout = + <>::InnerHasher as Hasher>::hash_pad( + &chain!( + [entry_point], + program_hash.elements, + elf_memory_init_hash.elements + ) + .collect_vec(), + ); + Self(hashout.to_bytes().into()) + } + + pub fn from_elf(elf_path: &str) -> anyhow::Result { + let elf_bytes = std::fs::read(elf_path)?; + let program = Program::mozak_load_program(&elf_bytes)?; + + Ok(Self::from_production_configs(&program)) + } +} + +impl From for ProgramIdentifier { + fn from(val: ProgId) -> Self { ProgramIdentifier(val.0) } +} +/// Compute merkle cap of the trace, and return its hash. +fn get_trace_commitment_hash>( + trace: Vec, + config: &StarkConfig, +) -> <>::InnerHasher as Hasher>::Hash +where + F: RichField + Extendable, + C: GenericConfig, + C::Hasher: AlgebraicHasher, { + let trace_poly_values = trace_rows_to_poly_values(trace); + let rate_bits = config.fri_config.rate_bits; + let cap_height = config.fri_config.cap_height; + let trace_commitment = PolynomialBatch::::from_values( + trace_poly_values, + rate_bits, + false, + cap_height, + &mut TimingTree::default(), + None, + ); + let merkle_cap = trace_commitment.merkle_tree.cap; + <>::InnerHasher as Hasher>::hash_pad(&merkle_cap.flatten()) +} diff --git a/scripts/test_bundle_tx.sh b/scripts/test_bundle_tx.sh index 085b497c6..7b0f6b9f2 100755 --- a/scripts/test_bundle_tx.sh +++ b/scripts/test_bundle_tx.sh @@ -4,17 +4,21 @@ set -euo pipefail # Run native executions and build mozakvm binaries -cd examples/token && cargo run --release \ +cd examples/token && \ + cargo build --bin tokenbin --release && \ + cargo run --release \ --features="native" \ --bin token-native \ - --target "$(rustc --verbose --version | grep host | awk '{ print $2; }')" && - cargo build --bin tokenbin --release + --target "$(rustc --verbose --version | grep host | awk '{ print $2; }')" + -cd ../wallet && cargo run --release \ +cd ../wallet && \ + cargo build --bin walletbin --release && \ + cargo run --release \ --features="native" \ --bin wallet-native \ - --target "$(rustc --verbose --version | grep host | awk '{ print $2; }')" && - cargo build --bin walletbin --release + --target "$(rustc --verbose --version | grep host | awk '{ print $2; }')" + # Run CLI cd ../../