diff --git a/Cargo.lock b/Cargo.lock index e2978810..7eecc64a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -210,6 +210,25 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + +[[package]] +name = "common" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "log", + "pilout", +] + [[package]] name = "console" version = "0.15.8" @@ -224,10 +243,35 @@ dependencies = [ ] [[package]] -name = "crunchy" -version = "0.2.2" +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "deranged" @@ -271,6 +315,29 @@ version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[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 = "equivalent" version = "1.0.1" @@ -294,14 +361,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" [[package]] -name = "fib" +name = "fibonacci" version = "0.1.0" dependencies = [ - "bytes", - "prost", - "prost-build", - "prost-types", - "tiny-keccak", + "ziskos", ] [[package]] @@ -414,6 +477,12 @@ dependencies = [ "slab", ] +[[package]] +name = "gcd" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a" + [[package]] name = "getrandom" version = "0.2.15" @@ -502,6 +571,12 @@ version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.4.0" @@ -577,6 +652,7 @@ checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ "equivalent", "hashbrown", + "serde", ] [[package]] @@ -622,6 +698,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -670,6 +755,16 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "libloading" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e310b3a6b5907f99202fcdb4960ff45b93735d7c7d96b760fcff8db2dc0e103d" +dependencies = [ + "cfg-if", + "windows-targets 0.52.5", +] + [[package]] name = "libredox" version = "0.1.3" @@ -752,12 +847,50 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "defc4c55412d89136f966bbb339008b474350e5e6e78d2714439c386b3137a03" +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", + "rand", +] + [[package]] name = "num-conv" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "num_cpus" version = "1.16.0" @@ -783,6 +916,18 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" +[[package]] +name = "nums" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3c74f925fb8cfc49a8022f2afce48a0683b70f9e439885594e84c5edbf5b01" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", + "rand", +] + [[package]] name = "object" version = "0.36.1" @@ -804,6 +949,113 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "p3-dft" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "p3-field", + "p3-matrix", + "p3-maybe-rayon", + "p3-util", + "tracing", +] + +[[package]] +name = "p3-field" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "itertools 0.13.0", + "num-bigint", + "num-integer", + "num-traits", + "nums", + "p3-util", + "rand", + "serde", +] + +[[package]] +name = "p3-goldilocks" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "num-bigint", + "p3-dft", + "p3-field", + "p3-mds", + "p3-poseidon2", + "p3-symmetric", + "p3-util", + "rand", + "serde", +] + +[[package]] +name = "p3-matrix" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "itertools 0.13.0", + "p3-field", + "p3-maybe-rayon", + "p3-util", + "rand", + "serde", + "tracing", + "transpose", +] + +[[package]] +name = "p3-maybe-rayon" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" + +[[package]] +name = "p3-mds" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "itertools 0.12.1", + "p3-dft", + "p3-field", + "p3-matrix", + "p3-symmetric", + "p3-util", + "rand", +] + +[[package]] +name = "p3-poseidon2" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "gcd", + "p3-field", + "p3-mds", + "p3-symmetric", + "rand", +] + +[[package]] +name = "p3-symmetric" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "itertools 0.13.0", + "p3-field", + "serde", +] + +[[package]] +name = "p3-util" +version = "0.1.0" +source = "git+https://github.com/Plonky3/Plonky3.git?rev=c3d754ef77b9fce585b46b972af751fe6e7a9803#c3d754ef77b9fce585b46b972af751fe6e7a9803" +dependencies = [ + "serde", +] + [[package]] name = "parking_lot" version = "0.12.3" @@ -843,6 +1095,18 @@ dependencies = [ "indexmap", ] +[[package]] +name = "pilout" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "bytes", + "log", + "prost", + "prost-build", + "util", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -918,11 +1182,27 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proofman" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "common", + "env_logger", + "libloading", + "log", + "p3-field", + "p3-goldilocks", + "pilout", + "stark", + "wchelpers", +] + [[package]] name = "prost" -version = "0.12.6" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deb1435c188b76130da55f17a466d252ff7b1418b2ad3e037d127b94e3411f29" +checksum = "e13db3d3fde688c61e2446b4d843bc27a7e8af269a69440c0308021dc92333cc" dependencies = [ "bytes", "prost-derive", @@ -930,13 +1210,13 @@ dependencies = [ [[package]] name = "prost-build" -version = "0.12.6" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22505a5c94da8e3b7c2996394d1c933236c4d743e81a410bcca4e6989fc066a4" +checksum = "5bb182580f71dd070f88d01ce3de9f4da5021db7115d2e1c3605a754153b77c1" dependencies = [ "bytes", "heck", - "itertools", + "itertools 0.13.0", "log", "multimap", "once_cell", @@ -951,12 +1231,12 @@ dependencies = [ [[package]] name = "prost-derive" -version = "0.12.6" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bddcdb20abf9501610992b6759a4c888aef7d1a7247ef75e2404275ac24af1" +checksum = "18bec9b0adc4eba778b33684b7ba3e7137789434769ee3ce3930463ef904cfca" dependencies = [ "anyhow", - "itertools", + "itertools 0.13.0", "proc-macro2", "quote", "syn", @@ -964,9 +1244,9 @@ dependencies = [ [[package]] name = "prost-types" -version = "0.12.6" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9091c90b0a32608e984ff2fa4091273cbdd755d54935c51d520887f4a1dbd5b0" +checksum = "cee5168b05f49d4b0ca581206eb14a7b22fafd963efe729ac48eb03266e25cc2" dependencies = [ "prost", ] @@ -1057,6 +1337,26 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.5.2" @@ -1165,6 +1465,10 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "riscv2zisk" +version = "0.1.0" + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1251,18 +1555,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", @@ -1275,6 +1579,7 @@ version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ + "indexmap", "itoa", "ryu", "serde", @@ -1310,6 +1615,32 @@ dependencies = [ "autocfg", ] +[[package]] +name = "sm-main" +version = "0.1.0" +dependencies = [ + "common", + "log", + "p3-field", + "p3-goldilocks", + "proofman", + "wchelpers", + "zisk-common", +] + +[[package]] +name = "sm-mem" +version = "0.1.0" +dependencies = [ + "common", + "log", + "p3-field", + "p3-goldilocks", + "proofman", + "wchelpers", + "zisk-common", +] + [[package]] name = "smallvec" version = "1.13.2" @@ -1332,6 +1663,37 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "stark" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "common", + "log", + "p3-field", + "p3-goldilocks", + "serde", + "serde_derive", + "serde_json", + "starks-lib-c", + "transcript", + "util", +] + +[[package]] +name = "starks-lib-c" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "log", +] + +[[package]] +name = "strength_reduce" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" + [[package]] name = "strsim" version = "0.11.1" @@ -1361,6 +1723,21 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +[[package]] +name = "sysinfo" +version = "0.30.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5b4ddaee55fb2bea2bf0e5000747e5f5c0de765e5a5ff87f4cd106439f4bb3" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "windows", +] + [[package]] name = "target-lexicon" version = "0.12.14" @@ -1432,15 +1809,6 @@ 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 = "tinyvec" version = "1.6.1" @@ -1568,6 +1936,24 @@ dependencies = [ "once_cell", ] +[[package]] +name = "transcript" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "starks-lib-c", +] + +[[package]] +name = "transpose" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "try-lock" version = "0.2.5" @@ -1624,6 +2010,15 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "util" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "colored", + "sysinfo", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -1737,6 +2132,14 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wchelpers" +version = "0.1.0" +source = "git+https://github.com/0xPolygonHermez/pil2-proofman.git?branch=develop#e8356ce685a1eca358fb8abbaaa1f9086f4fbd55" +dependencies = [ + "common", +] + [[package]] name = "web-sys" version = "0.3.69" @@ -1756,6 +2159,47 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -1916,3 +2360,35 @@ name = "zeroize" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + +[[package]] +name = "zisk-common" +version = "0.1.0" +dependencies = [ + "indexmap", + "serde", + "serde_json", +] + +[[package]] +name = "zisk-simulator" +version = "0.1.0" + +[[package]] +name = "zisk-wc" +version = "0.1.0" +dependencies = [ + "common", + "env_logger", + "log", + "p3-field", + "p3-goldilocks", + "proofman", + "sm-main", + "sm-mem", + "wchelpers", +] + +[[package]] +name = "ziskos" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index fbed84a3..234e962a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,11 +2,13 @@ members = [ "cli", "common", + "examples/fibonacci", "riscv/riscv2zisk", "simulator", "state-machines/main", "state-machines/mem", - "witness-computation" + "witness-computation", + "ziskos/entrypoint", ] resolver = "2" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ef5ffc78..a4b0174f 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -3,6 +3,10 @@ name = "cargo-zisk" version = "0.1.0" edition = "2021" +[[bin]] +name = "cargo-zisk" +path = "src/bin/cargo-zisk.rs" + [build-dependencies] vergen = { version = "8", default-features = false, features = [ "build", diff --git a/cli/src/bin/cargo-zisk.rs b/cli/src/bin/cargo-zisk.rs index 84c55d1e..b2a38ea1 100644 --- a/cli/src/bin/cargo-zisk.rs +++ b/cli/src/bin/cargo-zisk.rs @@ -1,15 +1,22 @@ -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use cargo_zisk::commands::build_toolchain::BuildToolchainCmd; use cargo_zisk::commands::install_toolchain::InstallToolchainCmd; use cargo_zisk::commands::new::NewCmd; use cargo_zisk::ZISK_VERSION_MESSAGE; use clap::{Parser, Subcommand}; +use std::env; +use std::process::{Command, Stdio}; + +use std::fs::File; +use std::io::{self, Read, Write}; +use std::path::Path; // Main enum defining cargo subcommands. #[derive(Parser)] -#[command(name = "cargo", bin_name = "cargo")] +#[command(name = "cargo-zisk", bin_name = "cargo-zisk", version = ZISK_VERSION_MESSAGE)] pub enum Cargo { Sdk(ZiskSdk), + Run(ZiskRun), } // Structure representing the 'sdk' subcommand of cargo. @@ -20,6 +27,28 @@ pub struct ZiskSdk { pub command: Option, } +// Structure representing the 'run' subcommand of cargo. +#[derive(clap::Args)] +#[command(author, about, long_about = None, version = ZISK_VERSION_MESSAGE)] +pub struct ZiskRun { + #[clap(long, short = 'F')] + features: Option, + #[clap(long)] + all_features: bool, + #[clap(long)] + release: bool, + #[clap(long)] + no_default_features: bool, + #[clap(long)] + sim: bool, + #[clap(long)] + stats: bool, + #[clap(long)] + gdb: bool, + #[clap(last = true)] + args: Vec, +} + // Enum defining the available subcommands for `ZiskSdk`. #[derive(Subcommand)] pub enum ZiskSdkCommands { @@ -28,28 +57,132 @@ pub enum ZiskSdkCommands { New(NewCmd), } -fn main() -> Result<()> { - // Parse command-line arguments and handle errors if they occur. - let Cargo::Sdk(args) = Cargo::parse(); - - // Check if a command was provided and execute the corresponding command. - if let Some(command) = args.command { - match command { - ZiskSdkCommands::BuildToolchain(cmd) => { - cmd.run() - .context("Error executing BuildToolchain command")?; +// Implement the run functionality for ZiskRun +impl ZiskRun { + fn run(&self) -> Result<()> { + let mut runner_command: String = String::new(); + // Construct the cargo run command + let mut command = Command::new("cargo"); + command.args(["+zisk", "run"]); + + // Add the feature selection flags + if let Some(features) = &self.features { + command.arg("--features").arg(features); + } + if self.all_features { + command.arg("--all-features"); + } + if self.no_default_features { + command.arg("--no-default-features"); + } + if self.release { + command.arg("--release"); + } + if self.sim { + let mut stats_command = ""; + if self.stats { + stats_command = "-s" } - ZiskSdkCommands::InstallToolchain(cmd) => { - cmd.run() - .context("Error executing BuildToolchain command")?; + runner_command = format!( + "node /home/edu/ziskjs/src/sim/main.js -n 100000000000 -i output/input.bin {} -e", + stats_command + ); + } else { + let mut gdb_command = ""; + if self.gdb { + gdb_command = "-S"; } - ZiskSdkCommands::New(cmd) => { - cmd.run() - .context("Error executing BuildToolchain command")?; + + let input_filename = "output/input.bin"; + let output_filename = "output/input_size.bin"; + + let input_path = Path::new(input_filename); + let metadata = std::fs::metadata(input_path)?; + + let file_size = metadata.len(); + + let size_bytes = file_size.to_le_bytes(); + let mut output_file = File::create(output_filename)?; + output_file.write_all(&size_bytes)?; + + runner_command = format!( + " + qemu-system-riscv64 \ + -cpu rv64 \ + -machine virt \ + -device loader,file=./output/input_size.bin,addr=0x90000000 \ + -device loader,file=./output/input.bin,addr=0x90000008 \ + -m 1G \ + -s \ + {} \ + -nographic \ + -serial mon:stdio \ + -bios none \ + -kernel", + gdb_command + ); + } + + + env::set_var("CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER", runner_command.to_string()); + // Verify the environment variable is set + println!( + "CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER: {}", + env::var("CARGO_TARGET_RISCV64IMA_POLYGON_ZISKOS_ELF_RUNNER").unwrap() + ); + + command.args(["--target", "riscv64ima-polygon-ziskos-elf"]); + + // Add any additional arguments passed to the run command + command.args(&self.args); + + println!("running {:?}", command); + // Set up the command to inherit the parent's stdout and stderr + command.stdout(Stdio::inherit()); + command.stderr(Stdio::inherit()); + + // Execute the command + let status = command.status().context("Failed to execute cargo run command")?; + if !status.success() { + return Err(anyhow!("Cargo run command failed with status {}", status)); + } + + Ok(()) + } +} + +fn main() -> Result<()> { + // Parse command-line arguments and handle errors if they occur. + let cargo_args = Cargo::parse(); + + match cargo_args { + Cargo::Sdk(args) => { + if let Some(command) = args.command { + execute_sdk_command(command)?; + } else { + println!("No SDK command provided"); } } - } else { - println!("No command provided"); + Cargo::Run(args) => { + args.run().context("Error executing Run command")?; + } + } + + Ok(()) +} + +// Function to handle SDK commands execution +fn execute_sdk_command(command: ZiskSdkCommands) -> Result<()> { + match command { + ZiskSdkCommands::BuildToolchain(cmd) => { + cmd.run().context("Error executing BuildToolchain command")?; + } + ZiskSdkCommands::InstallToolchain(cmd) => { + cmd.run().context("Error executing InstallToolchain command")?; + } + ZiskSdkCommands::New(cmd) => { + cmd.run().context("Error executing New command")?; + } } Ok(()) } diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs index b3b52edb..0b4774e3 100644 --- a/cli/src/commands/new.rs +++ b/cli/src/commands/new.rs @@ -9,7 +9,7 @@ pub struct NewCmd { name: String, } -const TEMPLATE_REPOSITORY_URL: &str = "https://github.com/0xPolygonHermez/hellozisk_rust"; +const TEMPLATE_REPOSITORY_URL: &str = "https://github.com/0xPolygonHermez/zisk_template"; impl NewCmd { pub fn run(&self) -> Result<()> { diff --git a/examples/fibonacci b/examples/fibonacci new file mode 160000 index 00000000..4221afef --- /dev/null +++ b/examples/fibonacci @@ -0,0 +1 @@ +Subproject commit 4221afefee9645749742b7335972d1d9a8c003b7 diff --git a/ziskos/entrypoint/Cargo.toml b/ziskos/entrypoint/Cargo.toml new file mode 100644 index 00000000..1b1dbcaa --- /dev/null +++ b/ziskos/entrypoint/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ziskos" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/ziskos/entrypoint/src/lib.rs b/ziskos/entrypoint/src/lib.rs new file mode 100644 index 00000000..9eed5298 --- /dev/null +++ b/ziskos/entrypoint/src/lib.rs @@ -0,0 +1,232 @@ +#![feature(asm_const)] + +pub mod ziskos_definitions; + +#[macro_export] +macro_rules! entrypoint { + ($path:path) => { + const ZISK_ENTRY: fn() = $path; + + mod zkvm_generated_main { + #[no_mangle] + fn main() { + super::ZISK_ENTRY() + } + } + }; +} + +#[allow(unused_imports)] +use crate::ziskos_definitions::ziskos_config::*; + +#[cfg(target_os = "ziskos")] +pub fn read_input() -> Vec { + // Create a slice of the first 8 bytes to get the size + let bytes = unsafe { core::slice::from_raw_parts(INPUT_ADDR as *const u8, 8) }; + // Convert the slice to a u64 (little-endian) + let size: u64 = u64::from_le_bytes(bytes.try_into().unwrap()); + + let input = unsafe { core::slice::from_raw_parts((INPUT_ADDR as *const u8).add(8), size as usize) }; + input.to_vec() +} + +#[cfg(not(target_os = "ziskos"))] +pub fn read_input() -> Vec { + use std::fs::File; + use std::io::Read; + + let mut file = File::open("output/input.bin").unwrap(); + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer).unwrap(); + buffer +} + +#[cfg(target_os = "ziskos")] +pub fn write_output(write_ptr: &[u8], nbytes: usize) { + use std::arch::asm; + let arch_id_zisk: usize; + let mut addr: *mut u8 = 0x1000_0000 as *mut u8; + + unsafe { + asm!( + "csrr {0}, marchid", + out(reg) arch_id_zisk, + ) + }; + if arch_id_zisk == ARCH_ID_ZISK as usize { + addr = 0xa000_0200 as *mut u8; + } + + for i in 0..nbytes { + unsafe { + core::ptr::write_volatile(addr, write_ptr[i]); + } + } +} + +#[cfg(not(target_os = "ziskos"))] +pub fn write_output(write_ptr: &[u8], _nbytes: usize) { + // Convert write_ptr to string + let write_str = match std::str::from_utf8(write_ptr) { + Ok(v) => v, + Err(e) => { + println!("Error converting write_ptr to string: {}", e); + return; + } + }; + + // Create the output string + let output = format!("{}", write_str); + + // Print the output string + print!("{}", output); +} + +#[cfg(target_os = "ziskos")] +mod ziskos { + use core::arch::asm; + use crate::ziskos_definitions::ziskos_config::*; + + #[no_mangle] + #[link_section = ".text.init"] + unsafe extern "C" fn _start() -> ! { + asm!( + // before we use the `la` pseudo-instruction for the first time, + // we need to set `gp` (google linker relaxation) + ".option push", + ".option norelax", + "la gp, _global_pointer", + ".option pop", + + // set the stack pointer + "la sp, _init_stack_top", + + // "tail-call" to {entry} + "call {_zisk_main}", + "csrr t0, marchid", + "li t1, {_ARCH_ID_ZISK}", + "beq t0, t1, 1f", + + // QEmuu exit + "li t0, {_QEMU_EXIT_ADDR}", + "li t1, {_QEMU_EXIT_CODE}", + "sw t1, 0(t0)", + "j 2f", + + // Zisk exit + "1: li a7, 93", + "ecall", + + "2: j 2b", + + _zisk_main = sym _zisk_main, // {entry} refers to the function [entry] below + _QEMU_EXIT_ADDR = const QEMU_EXIT_ADDR, + _QEMU_EXIT_CODE = const QEMU_EXIT_CODE, + _ARCH_ID_ZISK = const ARCH_ID_ZISK, + options(noreturn) // we must handle "returning" from assembly + ); + } + + #[no_mangle] + unsafe extern "C" fn _zisk_main() { + { + extern "C" { + fn main(); + } + main() + } + } + + #[no_mangle] + extern "C" fn sys_write(_fd: u32, write_ptr: *const u8, nbytes: usize) { + let arch_id_zisk: usize; + let mut addr: *mut u8 = 0x1000_0000 as *mut u8; + + unsafe { + asm!( + "csrr {0}, marchid", + out(reg) arch_id_zisk, + ) + }; + if arch_id_zisk == ARCH_ID_ZISK as usize { + addr = 0xa000_0200 as *mut u8; + } + + for i in 0..nbytes { + unsafe { + core::ptr::write_volatile(addr, *write_ptr.add(i)); + } + } + } + + #[no_mangle] + extern "C" fn sys_getenv() { + //unimplemented!("sys_getenv") + } + + #[no_mangle] + extern "C" fn sys_alloc_words() { + //unimplemented!("sys_alloc_words") + } + + #[no_mangle] + extern "C" fn sys_argc() { + unimplemented!("sys_argc"); + } + + #[no_mangle] + extern "C" fn sys_argv() { + unimplemented!("sys_argv"); + } + + #[no_mangle] + pub unsafe extern "C" fn sys_alloc_aligned(bytes: usize, align: usize) -> *mut u8 { + use core::arch::asm; + let heap_bottom: usize; + // UNSAFE: This is fine, just loading some constants. + unsafe { + // using inline assembly is easier to access linker constants + asm!( + "la {heap_bottom}, _kernel_heap_bottom", + heap_bottom = out(reg) heap_bottom, + options(nomem) + ) + }; + + // Pointer to next heap address to use, or 0 if the heap has not yet been + // initialized. + static mut HEAP_POS: usize = 0; + + // SAFETY: Single threaded, so nothing else can touch this while we're working. + let mut heap_pos = unsafe { HEAP_POS }; + + if heap_pos == 0 { + heap_pos = heap_bottom; + } + + let offset = heap_pos & (align - 1); + if offset != 0 { + heap_pos += align - offset; + } + + let ptr = heap_pos as *mut u8; + heap_pos += bytes; + + // Check to make sure heap doesn't collide with SYSTEM memory. + //if SYSTEM_START < heap_pos { + // panic!(); + // } + + unsafe { HEAP_POS = heap_pos }; + + ptr + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() {} +} diff --git a/ziskos/entrypoint/src/ziskos_definitions.rs b/ziskos/entrypoint/src/ziskos_definitions.rs new file mode 100644 index 00000000..5f494102 --- /dev/null +++ b/ziskos/entrypoint/src/ziskos_definitions.rs @@ -0,0 +1,12 @@ +pub mod ziskos_config { + pub static mut SV: u64 = 0xBBBB; + + pub const QEMU_EXIT_ADDR: u64 = 0x100000; + pub const QEMU_EXIT_CODE: u64 = 0x5555; + pub const INPUT_ADDR: u64 = 0x9000_0000; + pub const OUTPUT_ADDR: u64 = 0xa001_0000; + pub const ARCH_ID_ZISK: u64 = 0xFFFEEEE; // TEMPORARY // TODO register one + + pub const MAX_INPUT: usize = 0x2000; + pub const MAX_OUTPUT: usize = 0x1_0000; +}