From 35824d8b08bb11da3e16e6d04a27946d5b67e2b8 Mon Sep 17 00:00:00 2001 From: Evan Date: Fri, 24 Feb 2023 01:30:41 -0800 Subject: [PATCH] Add wasm proof generation with inclusion proof --- Cargo.lock | 182 +++++------- Cargo.toml | 98 +++---- rust/Cargo.toml | 21 +- rust/src/account/encryptor.rs | 5 +- wasm/Cargo.toml | 40 ++- wasm/src/lib.rs | 3 + wasm/src/program/intermediate_transaction.rs | 28 ++ wasm/src/program/mod.rs | 30 ++ wasm/src/program/program.rs | 81 ++++++ wasm/src/program/proving_key.rs | 72 +++++ wasm/src/program/transaction.rs | 262 ++++++++++++++++++ wasm/src/program/verifying_key.rs | 72 +++++ wasm/src/types.rs | 41 ++- website/package.json | 10 +- website/pkg/index.js | 0 website/public/index.html | 2 +- website/src/App.js | 12 +- website/src/db/index.js | 124 +++++++++ website/src/tabs/transaction/SendCredits.js | 214 ++++++++++++++ website/src/tabs/transaction/getChainState.js | 27 ++ website/src/workers/worker.js | 103 +++++++ website/webpack.config.js | 34 ++- website/yarn.lock | 44 ++- 23 files changed, 1310 insertions(+), 195 deletions(-) create mode 100644 wasm/src/program/intermediate_transaction.rs create mode 100644 wasm/src/program/mod.rs create mode 100644 wasm/src/program/program.rs create mode 100644 wasm/src/program/proving_key.rs create mode 100644 wasm/src/program/transaction.rs create mode 100644 wasm/src/program/verifying_key.rs create mode 100644 website/pkg/index.js create mode 100644 website/src/db/index.js create mode 100644 website/src/tabs/transaction/SendCredits.js create mode 100644 website/src/tabs/transaction/getChainState.js create mode 100644 website/src/workers/worker.js diff --git a/Cargo.lock b/Cargo.lock index 44d469769..1824f9583 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,7 @@ dependencies = [ "snarkvm-console", "snarkvm-synthesizer", "snarkvm-utilities", + "snarkvm-wasm", "ureq", ] @@ -123,7 +124,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6118baab6285accf088b31d5ea5029c37bbf9d98e62b4d8720a0a5a66bc2e427" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -143,11 +144,18 @@ dependencies = [ "console_error_panic_hook", "getrandom", "hex", + "js-sys", "rand", + "rand_chacha", + "serde", + "serde-wasm-bindgen", "serde_json", + "snarkvm-console", + "snarkvm-synthesizer", "snarkvm-wasm", "wasm-bindgen", "wasm-bindgen-test", + "web-sys", ] [[package]] @@ -235,9 +243,9 @@ dependencies = [ [[package]] name = "blake2s_simd" -version = "1.0.1" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6637f448b9e61dfadbdcbae9a885fadee1f3eaffb1f8d3c1965d3ade8bdfd44f" +checksum = "db539cc2b5f6003621f1cd9ef92d7ded8ea5232c7de0f9faa2de251cd98730d4" dependencies = [ "arrayref", "arrayvec", @@ -333,7 +341,7 @@ checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65" dependencies = [ "heck", "proc-macro-error", - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -383,9 +391,9 @@ dependencies = [ [[package]] name = "constant_time_eq" -version = "0.2.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3ad85c1f65dc7b37604eb0e89748faf0b9653065f2a8ef69f96a687ec1e9279" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "core-foundation" @@ -510,7 +518,7 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -595,9 +603,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "1.9.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" dependencies = [ "instant", ] @@ -1063,14 +1071,14 @@ dependencies = [ [[package]] name = "mio" -version = "0.8.6" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b9d9a46eff5b4ff64b45a9e316a6d1e0bc719ef429cbec4dc630684212bfdf9" +checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de" dependencies = [ "libc", "log", "wasi", - "windows-sys 0.45.0", + "windows-sys 0.42.0", ] [[package]] @@ -1142,7 +1150,7 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "876a53fff98e03a936a674b29568b0e605f06b29372c2489ff4de23f1949743d" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -1194,9 +1202,9 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "once_cell" -version = "1.17.1" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" [[package]] name = "openssl" @@ -1219,7 +1227,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -1299,7 +1307,7 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -1341,7 +1349,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", "version_check", @@ -1353,7 +1361,7 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "version_check", ] @@ -1369,9 +1377,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.50" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" dependencies = [ "unicode-ident", ] @@ -1421,7 +1429,7 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", ] [[package]] @@ -1770,22 +1778,33 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-wasm-bindgen" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" +dependencies = [ + "js-sys", + "serde", + "wasm-bindgen", +] + [[package]] name = "serde_derive" version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] [[package]] name = "serde_json" -version = "1.0.93" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cad406b69c91885b5107daf2c29572f6c8cdb3c66826821e286c533490c0bc76" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" dependencies = [ "indexmap", "itoa", @@ -1856,8 +1875,6 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "snarkvm" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59bc826fb94e0240a58cc627d1d4795cf02856e87ea47e315c983d1c5e3e0f89" dependencies = [ "anyhow", "clap", @@ -1884,8 +1901,6 @@ dependencies = [ [[package]] name = "snarkvm-algorithms" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f6bb298e5150e95e85c4658bd0f6d55c733adc5021e2f255fb90c05839cfe0d" dependencies = [ "aleo-std", "anyhow", @@ -1906,13 +1921,12 @@ dependencies = [ "snarkvm-r1cs", "snarkvm-utilities", "thiserror", + "web-sys", ] [[package]] name = "snarkvm-circuit" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7624cbc7e21e73cf83311ad7618bd2fdfe11224ef6c125efb274c9ecfe2e06e0" dependencies = [ "snarkvm-circuit-account", "snarkvm-circuit-algorithms", @@ -1926,8 +1940,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-account" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "327bb0b8d52b13619fb3c5e2cec87d12a2891d56d38b5f415cfcde18cea80118" dependencies = [ "snarkvm-circuit-algorithms", "snarkvm-circuit-network", @@ -1938,8 +1950,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-algorithms" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "347c010c8128840984c5f5f6a513eaa960258bee87c9ca94d577689e522c8bda" dependencies = [ "snarkvm-circuit-types", "snarkvm-console-algorithms", @@ -1949,8 +1959,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-collections" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85f15032bab5ebc07b04685d757d84add90f073b3a7c629f57311f9715ecd2b8" dependencies = [ "snarkvm-circuit-algorithms", "snarkvm-circuit-types", @@ -1960,8 +1968,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-environment" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfdaee135140f27c6321c2ee32b159522331b1f0f57a55c61d85a8eef0261250" dependencies = [ "indexmap", "itertools", @@ -1979,14 +1985,10 @@ dependencies = [ [[package]] name = "snarkvm-circuit-environment-witness" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84154491d3f29f739b9254cd23e6ab20868e1ea8a0c50bf70a66215195a49f7" [[package]] name = "snarkvm-circuit-network" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb53bf539d4e7a3d45b84b1a066d69b5fa211145ace42943fc55b33ed04c6886" dependencies = [ "snarkvm-circuit-algorithms", "snarkvm-circuit-collections", @@ -1997,8 +1999,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-program" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b491fde294e2df38560c77f47b436eb84cda9179d1384289d48d2f0f381b75f" dependencies = [ "snarkvm-circuit-account", "snarkvm-circuit-collections", @@ -2011,8 +2011,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb99c920473e96d7036761c270077da9600535f632d382a11e99ad4b67c04e0f" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-address", @@ -2027,8 +2025,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-address" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35c4c29afe50f82decf8436d9b876b595fff2e3fe69cd8e2ac57091a1936761c" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2041,8 +2037,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-boolean" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "571825663ac3ea5aa057aa2945600a646534ff9e74b4ae4f4d6fa555ac0f5897" dependencies = [ "snarkvm-circuit-environment", "snarkvm-console-types-boolean", @@ -2051,8 +2045,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-field" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79b07ae8fefb380325f3eec538457cfd386a79a9ef998a62c935081b81126b6c" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2062,8 +2054,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-group" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48b04d694962e0eff5c6f1847202e9a90969c2148a4292c37aaabd68bc5ca85c" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2075,8 +2065,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-integers" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c06f6ad40106e2310b27ea368993ea29a20e1c21e1eeb6b14103848ce5f67fd" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2087,8 +2075,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-scalar" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc8d800fdc9deb77e1837aaca4b8295cb48cecbd7a888949c5196f0e828a045d" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2099,8 +2085,6 @@ dependencies = [ [[package]] name = "snarkvm-circuit-types-string" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77f462665cad0282d51572e9250aa3165214053afbb5a1d36f59c4be46cba3d4" dependencies = [ "snarkvm-circuit-environment", "snarkvm-circuit-types-boolean", @@ -2112,8 +2096,6 @@ dependencies = [ [[package]] name = "snarkvm-console" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4873c8da377319091e6aa7f7e3ff85d82be0da1d76f48f9eee4a1c5d2d5f358" dependencies = [ "snarkvm-console-account", "snarkvm-console-algorithms", @@ -2126,8 +2108,6 @@ dependencies = [ [[package]] name = "snarkvm-console-account" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2f5e3ec09b3153e35e45d126aeb43484a6502ebaee9c1ff70483b77dc7d717b" dependencies = [ "bs58", "snarkvm-console-network", @@ -2137,8 +2117,6 @@ dependencies = [ [[package]] name = "snarkvm-console-algorithms" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd4c34eed9d7be1fe81d698cfb1f82807195f91dd0983bbb50e5d0b6861cc783" dependencies = [ "blake2s_simd", "smallvec", @@ -2150,8 +2128,6 @@ dependencies = [ [[package]] name = "snarkvm-console-collections" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab9949b61fb8ce05c78885803287c426d5852eefa0012d9c35b62e8e8041a008" dependencies = [ "aleo-std", "rayon", @@ -2162,8 +2138,6 @@ dependencies = [ [[package]] name = "snarkvm-console-network" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac46f0cf67ea542855c7da5684d0c64c29bcf6c13dfd26cd74d170244859e54" dependencies = [ "anyhow", "indexmap", @@ -2186,8 +2160,6 @@ dependencies = [ [[package]] name = "snarkvm-console-network-environment" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "949790255a3ce43484d2d64e4019e83d377cf6e3378065e3a4dfbe95bada2d2a" dependencies = [ "anyhow", "bech32", @@ -2204,8 +2176,6 @@ dependencies = [ [[package]] name = "snarkvm-console-program" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed97fd6b0bc5271e2db014da2f1b6d729941545e2a8579bbc1cfad8929321683" dependencies = [ "enum_index", "enum_index_derive", @@ -2224,8 +2194,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e6a2eb8fcd2311769e8c4178d33d2d98860afb7bcf57095305d3577fc59536" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-address", @@ -2240,8 +2208,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-address" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ba29808292f9aa51e64e889729632e0c08f631296812b3af21ccf9dc1eac22d" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2252,8 +2218,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-boolean" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bea412df3e71f3f2ae5df39caff188807ae57769dac9202ed3cb2614952113f7" dependencies = [ "snarkvm-console-network-environment", ] @@ -2261,8 +2225,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-field" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00306032a99673b0aba8b8913533a4d6846c44020d593b44ffa84088bdd853dd" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2271,8 +2233,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-group" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "416e30243bd359ed354d8594e1f9836a6f0be8bc958778527a04c585cf28f1d5" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2283,8 +2243,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-integers" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1db3e47a68a26d13410306e919efe2442610c1f5fc6ba259c846e39c33da3aea" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2294,8 +2252,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-scalar" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e204373be0ebfa1a17b166851a8f80ba4cb760799f2ad1823daefaea4b739113" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2305,8 +2261,6 @@ dependencies = [ [[package]] name = "snarkvm-console-types-string" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89189d253ff3d96f646becfc15eec1314224290a1bc7dc215b71f3d38cfca0a1" dependencies = [ "snarkvm-console-network-environment", "snarkvm-console-types-boolean", @@ -2317,8 +2271,6 @@ dependencies = [ [[package]] name = "snarkvm-curves" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "594d431ffc5b5811d3ae126d625f4542c010bc9e3017f882815b8ff639e69d65" dependencies = [ "rand", "rustc_version", @@ -2331,8 +2283,6 @@ dependencies = [ [[package]] name = "snarkvm-fields" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e27cc43b63eec46506b8f49067413f90b6577e7b3811d659884e841898b1c306" dependencies = [ "aleo-std", "anyhow", @@ -2349,8 +2299,6 @@ dependencies = [ [[package]] name = "snarkvm-parameters" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27eab7d7b81615de7743ba615e784f851711146923caf137e7963c04da13df7e" dependencies = [ "aleo-std", "anyhow", @@ -2377,8 +2325,6 @@ dependencies = [ [[package]] name = "snarkvm-r1cs" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20d349dd0c725a8b75189898d40ed67228889b0a418ccc5ea5ec68885d25f87e" dependencies = [ "anyhow", "cfg-if", @@ -2394,14 +2340,13 @@ dependencies = [ [[package]] name = "snarkvm-synthesizer" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "013d1cec9d64aa452f05a48ba3af0f1bc9598c2f220a61830c34160e4402920a" dependencies = [ "aleo-std", "anyhow", "blake2", "colored", "indexmap", + "itertools", "once_cell", "parking_lot", "paste", @@ -2422,8 +2367,6 @@ dependencies = [ [[package]] name = "snarkvm-utilities" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34724eac8768fbc1c39b09f925b1fca52c264280c28b40137a91c09a3148b4de" dependencies = [ "aleo-std", "anyhow", @@ -2442,10 +2385,8 @@ dependencies = [ [[package]] name = "snarkvm-utilities-derives" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a06c52a5ceba9b9159f1b8cc3ca29bd5da16f339e4eb077cfc9024c813f2b86" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -2453,14 +2394,15 @@ dependencies = [ [[package]] name = "snarkvm-wasm" version = "0.9.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eab1c2fc59610557d3f4e8c215d43bdb90748aec810ba0b04e6fa7d1b6543ece" dependencies = [ "getrandom", "rand", "serde", + "snarkvm-circuit", "snarkvm-console", + "snarkvm-curves", "snarkvm-fields", + "snarkvm-synthesizer", "snarkvm-utilities", "wasm-bindgen", ] @@ -2521,7 +2463,7 @@ version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "unicode-ident", ] @@ -2579,7 +2521,7 @@ version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -2595,9 +2537,9 @@ dependencies = [ [[package]] name = "tinyvec_macros" -version = "0.1.1" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" @@ -2618,9 +2560,9 @@ dependencies = [ [[package]] name = "tokio-native-tls" -version = "0.3.1" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" dependencies = [ "native-tls", "tokio", @@ -2651,9 +2593,9 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.7" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5427d89453009325de0d8f342c9490009f76e999cb7672d77e46267448f7e6b2" +checksum = "0bb2e075f03b3d66d8d8785356224ba688d2906a371015e225beeb65ca92c740" dependencies = [ "bytes", "futures-core", @@ -2697,7 +2639,7 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", ] @@ -2931,7 +2873,7 @@ dependencies = [ "bumpalo", "log", "once_cell", - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", "wasm-bindgen-shared", @@ -2965,7 +2907,7 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", "syn 1.0.107", "wasm-bindgen-backend", @@ -2998,7 +2940,7 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0734759ae6b3b1717d661fe4f016efcfb9828f5edb4520c18eaee05af3b43be9" dependencies = [ - "proc-macro2 1.0.51", + "proc-macro2 1.0.50", "quote 1.0.23", ] diff --git a/Cargo.toml b/Cargo.toml index 9569383c5..4ebf30f5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,10 +22,20 @@ members = [ "rust", "wasm" ] [workspace.dependencies.snarkvm] version = "0.9.13" +path="../snarkVM" [workspace.dependencies.snarkvm-wasm] version = "0.9.13" default-features = false +path="../snarkVM/wasm" + +[workspace.dependencies.snarkvm-synthesizer] +path="../snarkVM/synthesizer" +version = "0.9.13" + +[workspace.dependencies.snarkvm-console] +path="../snarkVM/console" +version = "0.9.13" [lib] path = "cli/lib.rs" @@ -41,68 +51,62 @@ wasm = [ "aleo-wasm" ] [dependencies] num-format = "0.4.4" - [dependencies.aleo-rust] - path = "rust" - version = "0.3.5" - optional = true +[dependencies.aleo-rust] +path = "rust" +version = "0.3.5" +optional = true - [dependencies.aleo-wasm] - path = "wasm" - version = "0.3.5" - optional = true +[dependencies.aleo-wasm] +path = "wasm" +version = "0.3.5" +optional = true - [dependencies.anyhow] - version = "1.0" +[dependencies.anyhow] +version = "1.0" - [dependencies.clap] - version = "3.2" - features = [ "derive" ] +[dependencies.clap] +version = "3.2" +features = [ "derive" ] - [dependencies.colored] - version = "2" +[dependencies.colored] +version = "2" - [dependencies.parking_lot] - version = "0.12" +[dependencies.parking_lot] +version = "0.12" - [dependencies.rand] - version = "0.8" - default-features = false +[dependencies.rand] +version = "0.8" +default-features = false - [dependencies.rand_chacha] - version = "0.3.0" - default-features = false +[dependencies.rand_chacha] +version = "0.3.0" +default-features = false - [dependencies.rpassword] - version = "7.2.0" +[dependencies.rpassword] +version = "7.2.0" - [dependencies.self_update] - version = "0.32" +[dependencies.self_update] +version = "0.32" - [dependencies.serde] - version = "1" +[dependencies.serde] +version = "1" - [dependencies.serde_json] - version = "1" +[dependencies.serde_json] +version = "1" - [dependencies.snarkvm] - workspace = true - features = [ - "aleo-cli", - "circuit", - "console", - "parallel", - "snarkvm-synthesizer" -] +[dependencies.snarkvm] +workspace = true +features = [ "aleo-cli", "circuit", "console", "snarkvm-synthesizer"] - [dependencies.thiserror] - version = "1.0" +[dependencies.thiserror] +version = "1.0" - [dependencies.tokio] - version = "1.22" - features = [ "rt" ] +[dependencies.tokio] +version = "1.22" +features = [ "rt" ] - [dependencies.warp] - version = "0.3" +[dependencies.warp] +version = "0.3" [dev-dependencies.rusty-hook] version = "0.11.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index fbb5e82be..22bf58477 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -52,19 +52,26 @@ optional = true [dependencies.serde_json] version = "1.0.91" -[dependencies.snarkvm-console] -features = [ "parallel" ] -optional = true +[dependencies.snarkvm-wasm] +version = "0.9.13" +path="../../snarkVM/wasm" + +[dependencies.snarkvm-utilities] version = "0.9.13" +path="../../snarkVM/utilities" [dependencies.snarkvm-synthesizer] -features = [ "parallel" ] -optional = true version = "0.9.13" +path = "../../snarkVM/synthesizer" +default-features = false +features = [ "wasm" ] +optional = true -[dependencies.snarkvm-utilities] -features = [ "parallel" ] +[dependencies.snarkvm-console] +path = "../../snarkVM/console" +default-features = false version = "0.9.13" +optional = true [dev-dependencies.bencher] version = "0.1.5" diff --git a/rust/src/account/encryptor.rs b/rust/src/account/encryptor.rs index f8681d0cd..7a844b4b2 100644 --- a/rust/src/account/encryptor.rs +++ b/rust/src/account/encryptor.rs @@ -17,11 +17,14 @@ use snarkvm_console::{ account::PrivateKey, network::Network, - program::{Ciphertext, Identifier, Literal, Plaintext}, types::Field, }; use snarkvm_utilities::Uniform; +use snarkvm_console::{ + program::{Ciphertext, Identifier, Literal, Plaintext} +}; + use anyhow::{anyhow, Result}; use once_cell::sync::OnceCell; use std::{iter::FromIterator, str::FromStr}; diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 546f18ddb..85b2f621b 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -41,17 +41,53 @@ version = "0.8" [dependencies.serde_json] version = "1.0" +features = [ "preserve_order" ] + +[dependencies.snarkvm-synthesizer] +path="../../snarkVM/synthesizer" +version = "0.9.13" +default-features = false +features = [ "wasm" ] + +[dependencies.snarkvm-console] +path="../../snarkVM/console" +version = "0.9.13" +default-features = false [dependencies.snarkvm-wasm] -workspace = true -features = [ "console", "fields", "utilities" ] +path="../../snarkVM/wasm" +version = "0.9.13" [dependencies.wasm-bindgen] version = "0.2" features = [ "serde-serialize" ] +[dependencies.js-sys] +version = "0.3" + +[dependencies.web-sys] +version = "0.3" +features = ["console"] + +[dependencies.serde] +version = "1.0" +features = ["derive"] + +[dependencies.serde-wasm-bindgen] +version = "0.4" + [dependencies.console_error_panic_hook] version = "0.1.7" +[dependencies.rand_chacha] +version = "0.3.1" + [dev-dependencies.wasm-bindgen-test] version = "0.3.33" + +[profile.release] +opt-level = 3 +lto = true + +[package.metadata.wasm-pack.profile.release] +wasm-opt = ["-O4", "--fast-math"] diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 762d731eb..b5c18e7af 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -20,4 +20,7 @@ pub use account::*; pub mod record; pub use record::*; +pub mod program; +pub use program::*; + pub(crate) mod types; diff --git a/wasm/src/program/intermediate_transaction.rs b/wasm/src/program/intermediate_transaction.rs new file mode 100644 index 000000000..90924d48a --- /dev/null +++ b/wasm/src/program/intermediate_transaction.rs @@ -0,0 +1,28 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +use crate::types::{ + InputIDNative, + TransitionNative, +}; + +use serde::{Serialize, Deserialize}; + +#[derive(Clone, Serialize, Deserialize)] +pub struct IntermediateTransaction { + pub transition: TransitionNative, + pub input_ids: Vec +} \ No newline at end of file diff --git a/wasm/src/program/mod.rs b/wasm/src/program/mod.rs new file mode 100644 index 000000000..6afc6b637 --- /dev/null +++ b/wasm/src/program/mod.rs @@ -0,0 +1,30 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +pub mod proving_key; +pub use proving_key::*; + +pub mod verifying_key; +pub use verifying_key::*; + +pub mod program; +pub use program::*; + +pub mod intermediate_transaction; +pub use intermediate_transaction::*; + +pub mod transaction; +pub use transaction::*; \ No newline at end of file diff --git a/wasm/src/program/program.rs b/wasm/src/program/program.rs new file mode 100644 index 000000000..31f3dcf25 --- /dev/null +++ b/wasm/src/program/program.rs @@ -0,0 +1,81 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +use crate::types::ProgramNative; + +use core::{ops::Deref, str::FromStr}; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Clone, Debug)] +pub struct Program(ProgramNative); + +#[wasm_bindgen] +impl Program { + /// Get the id of the program + pub fn id(&self) -> String { + self.0.id().to_string() + } + + /// Create a program from a string representation + /// + /// This function will fail if the text is not a valid program + pub fn from_string(program: &str) -> Result { + Self::from_str(program).map_err(|_| "Invalid program".to_string()) + } + + /// Get a string representation of the program + #[allow(clippy::inherent_to_string_shadow_display)] + pub fn to_string(&self) -> String { + self.0.to_string() + } + + /// Get the default credits program + /// + /// This function shouldn't fail as the credits program is always defined + pub fn credits() -> Result { + let credits_program = ProgramNative::credits().map_err(|_| "Could not load credits program".to_string())?; + Ok(Self(credits_program)) + } +} + +impl From for Program { + fn from(program: ProgramNative) -> Self { + Self(program) + } +} + +impl From for ProgramNative { + fn from(program: Program) -> Self { + program.0 + } +} + +impl FromStr for Program { + type Err = anyhow::Error; + + fn from_str(program: &str) -> Result { + Ok(Self(ProgramNative::from_str(program)?)) + } +} + +impl Deref for Program { + type Target = ProgramNative; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} \ No newline at end of file diff --git a/wasm/src/program/proving_key.rs b/wasm/src/program/proving_key.rs new file mode 100644 index 000000000..c7c5d33bf --- /dev/null +++ b/wasm/src/program/proving_key.rs @@ -0,0 +1,72 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +use crate::{ + types::{ProvingKeyNative, ToBytes, FromBytes}, +}; + +use std::{ops::Deref}; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Clone, Debug)] +pub struct ProvingKey(ProvingKeyNative); + +#[wasm_bindgen] +impl ProvingKey { + #[wasm_bindgen] + pub fn from_bytes(bytes: Vec) -> ProvingKey { + console_error_panic_hook::set_once(); + let proving_key = ProvingKeyNative::from_bytes_le(&bytes); + Self(proving_key.unwrap()) + } + + #[wasm_bindgen] + pub fn to_bytes(&self) -> Result, String> { + console_error_panic_hook::set_once(); + self.0.to_bytes_le().map_err(|_| "Failed to serialize prover key".to_string()) + } +} + +impl Deref for ProvingKey { + type Target = ProvingKeyNative; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From for ProvingKeyNative { + fn from(proving_key: ProvingKey) -> ProvingKeyNative { + proving_key.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn test_deserialize_serialize_key_wasm() { + let transfer_bytes = include_bytes!(concat!(env!("HOME"), "/.aleo/resources/transfer.prover.837ad21")).to_vec(); + let proving_key = ProvingKey::from_bytes(transfer_bytes.clone()); + let bytes = proving_key.to_bytes().unwrap(); + assert!(transfer_bytes.eq(&bytes)); + } +} \ No newline at end of file diff --git a/wasm/src/program/transaction.rs b/wasm/src/program/transaction.rs new file mode 100644 index 000000000..9b80337d8 --- /dev/null +++ b/wasm/src/program/transaction.rs @@ -0,0 +1,262 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +use crate::{ + Program, + ProvingKey, + VerifyingKey, + IntermediateTransaction, + account::PrivateKey, + types::{ + Aleo, + ExecutionNative, + InclusionNative, + ProcessNative, + ProgramNative, + ProvingKeyNative, + VerifyingKeyNative, + StatePathNative, + StateRootNative, + TransactionNative, IdentifierNative + } +}; +use std::collections::HashMap; + +use std::str::FromStr; +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub struct TransactionBuilder {} + +#[wasm_bindgen] +impl TransactionBuilder { + pub fn build_transition( + program: Program, + function_name: &str, + inputs: &str, // https://github.com/rustwasm/wasm-bindgen/issues/111 + private_key: PrivateKey, + proving_key: ProvingKey + ) -> Result { + console_error_panic_hook::set_once(); + let inputs: Vec = serde_json::from_str(inputs) + .map_err(|_| "Could not deserialize inputs".to_string())?; + + // Get the function identifier + let program_native = ProgramNative::from(program); + let function_identifier = IdentifierNative::from_str(function_name) + .map_err(|_| "Could not get function identifer".to_string())?; + if !program_native.contains_function(&function_identifier) { + return Err(format!("Function: {:?} not found", function_identifier)) + } + + // Create the process with only the credits program by default + let mut process = ProcessNative::load_wasm().map_err(|_| "Could not initialize wasm".to_string())?; + + // Check if the process contains the program, if not, add it + if !process.contains_program(program_native.id()) { + process.add_program(&program_native).map_err(|_| "Could not add program".to_string())?; + } + + // Insert the proving key + process.insert_proving_key(program_native.id(), &function_identifier, ProvingKeyNative::from(proving_key)) + .map_err(|_| "Could not insert proving key".to_string())?; + + // Generate the authorization + let rng = &mut rand::thread_rng(); + let authorization = + process.authorize::(&private_key, program_native.id(), function_name, inputs.iter(), rng) + .map_err(|_| "Could generate authorization".to_string())?; + + // Get the input_ids, necessary to reconstruct the inclusion proof + let next = authorization.peek_next().unwrap(); + let input_ids = next.input_ids().to_vec(); + + // Generate the execution + let (_, execution, _, _) = process.execute::(authorization, rng) + .map_err(|_| "Could not complete program execution".to_string())?; + + // Get the transition from the execution + let mut transitions = execution.transitions(); + let transition = transitions.next().unwrap().to_owned(); + let intermediate_transaction = IntermediateTransaction { + transition, + input_ids + }; + let intermediate_transaction = serde_json::to_string(&intermediate_transaction) + .map_err(|_| "Could not serialize intermediate transaction".to_string())?; + + Ok(intermediate_transaction) + + } + + pub fn build_transaction( + inclusion_key: ProvingKey, + intermediate_transactions: &str, + state_root: &str, + commitment_map: &str + ) -> Result { + console_error_panic_hook::set_once(); + + // Create Inclusion and Execution from all IntermediateTransactions + let mut inclusion = InclusionNative::new(); + let mut execution = ExecutionNative::new(); + + let intermediate_transactions: Vec = serde_json::from_str(intermediate_transactions) + .map_err(|_| "Could not deserialize intermediate transactions".to_string())?; + + for IntermediateTransaction { transition, input_ids } in &intermediate_transactions { + let transition = transition.to_owned(); + inclusion.insert_transition(&input_ids, &transition).unwrap(); + execution.push(transition); + } + + // Parse state root and state paths + let global_state_root = StateRootNative::from_str(state_root).unwrap(); + let default_map: HashMap = HashMap::new(); + let commitment_to_state_path: HashMap = serde_json::from_str(commitment_map).unwrap_or(default_map); + + // Prepare the assignments. + let (assignments, _) = inclusion + .prepare_execution_stateless(&execution, global_state_root, commitment_to_state_path).unwrap(); + + // Compute the inclusion proof and update the execution. + let rng = &mut rand::thread_rng(); + let execution = inclusion + .prove_execution_stateless::(inclusion_key.into(), execution, &assignments, rng).unwrap(); + + let tx = TransactionNative::from_execution(execution, None).unwrap(); + Ok(tx.to_string()) + } + + pub fn verify_transaction( + transaction_string: &str, + program: Program, + function_name: &str, + function_verifying_key: VerifyingKey, + inclusion_key: VerifyingKey, + verify_inclusion: bool, + verify_execution: bool + ) -> Result { + console_error_panic_hook::set_once(); + // Parse Transaction + let transaction = TransactionNative::from_str(transaction_string).unwrap(); + + // Get the function identifier + let program_native = ProgramNative::from(program); + let function_identifier = IdentifierNative::from_str(function_name) + .map_err(|_| "Could not get function identifer".to_string())?; + if !program_native.contains_function(&function_identifier) { + return Err(format!("Function: {:?} not found", function_identifier)) + } + + // Create the process with only the credits program by default + let mut process = ProcessNative::load_wasm().map_err(|_| "Could not initialize wasm".to_string())?; + + // Check if the process contains the program, if not, add it + if !process.contains_program(program_native.id()) { + process.add_program(&program_native).map_err(|_| "Could not add program".to_string())?; + } + + // Insert the verifying key + process.insert_verifying_key(program_native.id(), &function_identifier, VerifyingKeyNative::from(function_verifying_key)) + .map_err(|_| "Could not insert transfer verifying key".to_string())?; + + match transaction { + TransactionNative::Deploy(_, _, _) => { + panic!("Cannot verify Deploy Transactions"); + } + TransactionNative::Execute(_, execution, _) => { + if verify_execution { + process.verify_execution::(&execution) + .map_err(|_| "Failed to verify execution".to_string())?; + } + if verify_inclusion { + InclusionNative::verify_execution_stateless(&execution, VerifyingKeyNative::from(inclusion_key)) + .map_err(|_| "Failed to verify inclusion".to_string())?; + } + } + } + + Ok("Transaction verified".to_string()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use wasm_bindgen_test::*; + use web_sys::console; + + const PRIVATE_KEY: &str = "APrivateKey1zkpDKDJELtg3GN29R2UPihgsucvYSkb7BzEYDRkJtZcwhj8"; + const OWNER_PLAINTEXT1: &str = r"{ owner: aleo1mc6h9k9g03hfkrh93wm6vlew6q2ehzwalqutzjx6hp5mdcv9rsyq2w3alz.private, gates: 9973u64.private, _nonce: 1157538218493794217455834413151916695596644156738232294925137030650794330428group.public}"; + const OWNER_PLAINTEXT1_COMMITMENT: &str = r"605538192287731854742364189642613370746132375889525872894501892977418029640field"; + const OWNER_PLAINTEXT1_PATH: &str = r"path1qqqd4vjjgfd05ekjmd02r74pxd7rqjsu2n3a9rnutkc6x5c3yhj6szklquqqqqqqqqqr4hq4tv6w8qp64680gfnq69tvdr0unup88k5sgum5ynru8f7zqpx6z4t3pxwm2zh2c9n8jhm9f9nat86kdx2yf5lfdl8kr0p9rh3fq347xs7al64hvdjuetejauufsfhvrvzz429kcmcavjs9229ctedssw8ucpfq88q4jkf083fwp64nwf6eq06wj0vlqa2s87c4vudemsqy3facnk8c6xwskfg23h6xctfca2r28echq935pzjt8n6upgv9ls9zm4ll46tsszsxkdg3hw0vl6ah8vmvrh7g2eejfjeju6c6dlt7gy9jsvfa60yjq5tft7zt6gnsz4flegx50dgsluqwwv6w7hwpct99z2y8ftxn4pupqlvupt4jtjkv2zxkye0333ktmjfgz03f99xy03aqjzx4zmzemg6kyxugy97m6p9mlfkmp5xwz5udre5rh4rk40ezsrsftcghmjqf793nuq482m5uyznnegaq8v6tu4zxr78pyh23hug8jvqmut52jefmeqd8dnzsugx65fwhr3qmtfw8nw9am5kwwtnjyk4jsy5yzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qns27zmwr8z5y604wad5pcrjyx0xw0jsev8zmp92hetyf8nvepgqlrrj4qwaueef7jukyclk7xy5djeg3jqan4cpdnv69aa7az6tulssu4qm9lrx3fzgdqlpd7zfct94fyw63a2gzyglj72cx7rlyfmrlgrszqqqqqqqqqqqkrg383d3ctrsv3dn365v0hg3wmuj7frqzmuvldsj9mjcgllaqyqx8ce6fm5jhhfup3as6eg7m95h7hhd38f2m2e8w582lwzj253rkpqplxpvafj73f7kuymxn2lnhajfymjp7tg4r8xc8ltq70zgdwtcpvqcmhna74rpc8h4yt4f6g9uxdls83qr6cpvyymespsaahtpn0c9spsqqqqqqqqqqqqqhcewf768vg6pldnetgxf448sm9ecxd9vlzcp72d4nxqxlsx2vr5yzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzgg9v7559gaxswfjytkkhslq44kvj8ld2pj29xvvqdlsn6zez45pzumn3nfqsdylxcesax8zwvks7de3v0gw5ndttpd4l80ucku7mvp5qqqqqqqqqqqqyyzk022z5wng8yez9mttc0s26mxfrlk4qe9znxxqxlcfapv326qjzpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqfpq4n6js4r56pexg3w667ruzkkejgla4gxfg5e3sph7z0gty2ksyss2eafg236dqunyghdd0p7pttvey0765ry52vccqmlp859j9tgzqzqqqhgl59s8lcdezyqg4ylcvajkjwqkcv9dygxm23njmd9fvvmk30c9sxqqqqqqqqqqqe332uur0a63lncg7gw5nz5vqqkz3njajl9v4henfgjuuh3hjksr879j0kkkteaehcn9yfmmh5ny4j9vt8gqelnygdue682j97ka4yq099ryhtp3hk6f0mvcr5watm4ca7wg42sze6skdmsxt8jx8t72hq6zpt849p28f5rjv3za44u8c9ddny3lm2svj3fnrqr0uy7skg4dqgqqrqvqy3jnmdu3va9w68y6rcnlmvh26mhud86nqqs0v0p84nq7l7wu9vqg4sqmk7"; + + const RECIPIENT: &str = "aleo1nhdttzqjjj68qqjge68m7ugpz6d9dq30qxxngyg2qjr9f8hx4cgs42t7wp"; + const AMOUNT: &str = "97u64"; + const GLOBAL_STATE_ROOT: &str = "ar13manm2epykqtk0zxzcqa7nn3zc5z064lap6r2ys0t0l5t3uy9crsufrdj2"; + + #[wasm_bindgen_test] + fn test_build_transition() { + // Prepare inputs to build transition + let program = Program::credits().unwrap(); + let function_name = "transfer"; + let inputs = serde_json::to_string(&[ + OWNER_PLAINTEXT1, + RECIPIENT, + AMOUNT + ]).unwrap(); + let private_key = PrivateKey::from_str(PRIVATE_KEY).unwrap(); + let transfer_bytes = include_bytes!(concat!(env!("HOME"), "/.aleo/resources/transfer.prover.837ad21")).to_vec(); + let transfer_proving_key = ProvingKey::from_bytes(transfer_bytes); + + // Build transition + let intermediate_transaction1 = TransactionBuilder::build_transition( + program, + function_name, + &inputs, + private_key, + transfer_proving_key + ).unwrap(); + + console::log_1(&intermediate_transaction1.into()); + } + + #[wasm_bindgen_test] + fn test_build_transaction() { + // Prepare inputs to build transaction + let intermediate_transaction1 = r#"{"transition":{"id":"as100gs7rlucq4zu7u3dlwys9vnep80ftwws2u2ausvzcpf2fesrgzsfuf9jv","program":"credits.aleo","function":"transfer","inputs":[{"type":"record","id":"5014771957006340580317363981373591084465734774693302641777058302177738009042field","tag":"1592215853668322267062146193964960404834934970523747812466009574754114740452field"},{"type":"private","id":"322815084863476371220771878109567628405323161045197678148136586030747914889field","value":"ciphertext1qgq9ya29q50jnxwspkwgr6g4cgvf87sw5j2hvk6x2zmkarg9xxauxqhyj3fh8xs0gjqfezfl689krqxj5th76uz075465mfjdvfh72tgpc3tan76"},{"type":"private","id":"2145739655038782315766524255894847900603699299809703897648490882673572222470field","value":"ciphertext1qyqrh2twkdal03adakvv4cl4lzrwu5wljxyms84js9s5h6l62ftevqcj2rk79"}],"outputs":[{"type":"record","id":"4069439388021451078269049650605705726761983682392111434935933144215765309236field","checksum":"1406934742874233776553884855777823800086998498181550189152055420777952320965field","value":"record1qyqspyrgflcgucsr59gqzqzegxf395wxhw0eq3kv63zsfa7y9sqd4tqsqyqspuv6ewd4qhz6p9r2nfakxmn2l2zfyqgsh6vh6p2d5q2650vxtjq8qzajnka4q5u50vfwdavv3qqwe2wnpsuymfxvzyazmejnjnuqgurswsfx9eh"},{"type":"record","id":"5420469059357958525128014207249608754218509899266503418642860591860771984738field","checksum":"980276437940144926956762218081372377044893877273476995664467211172445965935field","value":"record1qyqsp0lyytf9eym2xccvpcm45nyuymmksnvmlyvd6furxqx7rv8wfkssqyqspevvj0qamqf3rsz4sf67k7qppslfj9e3fwfzrfhmxuwz6rw8myc2qzz8gdp55x534a0e2haqg9jd960yyuhkcn09jgqddlu0fyumxmzsvrj4due"}],"proof":"proof1qqqqzqqqqqqqqqqqkvuv2w8r0npu43mp8s7p9sh48cjssqq5q6ya4x9ryg4fu7dl0x6xsmqqdnjd7s9e3yu0n6yzvv5czgvaghn8vfurfcm6dlfgwzp3lk8un8wzshhleqpdxm8l04e86p3n8ux8kwn42vaz6kgz7ngnahvssq7er0szz4ku9q2xdz2pz58fhcuka5k4ff9lsmjax273maz9ph2tf0mvfzdh8zm0snh6v4xj7pls2qgptdaxeyqg8x57n7rehkywucu38mup6gnm6f700j7x8wak7evtsw8juqqxswxdh525ml6spr5smsncp03emkk85pt52jtdt8qqcmttg3rxj2uvjv2km4r7acxuyqca4f632fksax6ed287c4khjwucqvvjq8ks5k5atpdftwu82vxmu65pxsn494p9gsthhg0j4kpqrk30cd4pxkjnwrjewltuvp7jztfd4dppyqwss3yd26vecdad3rt6ez4gqhx9tn776m4grugc3ztfg2r2facr5mcw8uxwq963vc6pxngenzqp55q2j0y0j8rz62ry40ygxr72s6zk6hxdltfvmuxmlujuzkf5a09p8nre7slgxff4rwqelmfqd4jved5q4j542ddwlgp20qhsd8mnkdcckgvwrt6rv6a20rhutj72f5ns44tsxlztrdffj4lqycfy6qr7jaesr6zch4v9keullzuem4nz7rgs9rps8d3aywq8lu9pz7nprgkjjau6p3qh87gceqe6wtevss8wl0ppsx24nhppj8z4274wegs9te8eeekr9pl8ue2hy7war4smlksmdngsva9g5kjvm4h3046070enwemsduztlwwd08gxmaymwc8hrd485gqsp2nakpzjlaqdmv6q3w6f9usju4l760zgmzny7l9chfcfy8xekcgyysv4a3yvcnm84t0c7c8l4ef0afkp37409gctx6cg79z88h86wpce4tan5lecxaa4aqde8gay4wmdu4sywhdw32cnses8pdka9xhqp09uxrfndxj35ng0r0zj625t3ny2gp6990wwe5dsk2d8ng2j4ajsk097d5gjh0m3nzkng39sfrrrf6v3rjdc6mr2agn6clp09jmgzlqjy37y8p2rymn2nuw3aktznpy4kra58cdkfgfuvehpwg5hgq0g9c8syqqqqqqqqqqq6cac9vgjv8433sg7e522nz0dg379t955ft035ce8z7xe4h228dqajv9g6qlkwn8zjt6kgy0c7ragqq2matv09cpdyswjsknfd9g7srqm8dpfq5wjqxee6yavqlw08jc3qwwtahh9nnwav6cl4lhfstp3yezyu82m2068drjqqzkuy705mt8fwwdwdgpx2z2pljnal63zppm9vqqqqq2pftw6","tpk":"7232922488843750299609714637897998954390062332698850825353778535377930212519group","tcm":"2576677602366284087862363339085985775279049642524480921123773832178280700244field","fee":0},"input_ids":[{"type":"record","commitment":"605538192287731854742364189642613370746132375889525872894501892977418029640field","gamma":"5356502370233901355799069746156088185496903661954521847638241903524144982783group","serial_number":"5014771957006340580317363981373591084465734774693302641777058302177738009042field","tag":"1592215853668322267062146193964960404834934970523747812466009574754114740452field"},{"type":"private","id":"322815084863476371220771878109567628405323161045197678148136586030747914889field"},{"type":"private","id":"2145739655038782315766524255894847900603699299809703897648490882673572222470field"}]}"#; + let intermediate_transaction1: IntermediateTransaction = serde_json::from_str(intermediate_transaction1).unwrap(); + let inclusion_bytes = include_bytes!(concat!(env!("HOME"), "/.aleo/resources/inclusion.prover.b9921c5")).to_vec(); + let inclusion_proving_key = ProvingKey::from_bytes(inclusion_bytes); + let intermediate_transactions = serde_json::to_string(&[ + intermediate_transaction1 + ]).unwrap(); + + let mut commitment_map: HashMap = HashMap::new(); + commitment_map.insert(OWNER_PLAINTEXT1_COMMITMENT.to_string(), StatePathNative::from_str(&OWNER_PLAINTEXT1_PATH).unwrap()); + let commitment_map_serialized = serde_json::to_string(&commitment_map).unwrap(); + + let transaction = TransactionBuilder::build_transaction( + inclusion_proving_key, + intermediate_transactions.as_str(), + GLOBAL_STATE_ROOT, + &commitment_map_serialized.as_str() + ).unwrap(); + + console::log_1(&transaction.into()); + } +} \ No newline at end of file diff --git a/wasm/src/program/verifying_key.rs b/wasm/src/program/verifying_key.rs new file mode 100644 index 000000000..d72e16096 --- /dev/null +++ b/wasm/src/program/verifying_key.rs @@ -0,0 +1,72 @@ +// Copyright (C) 2019-2023 Aleo Systems Inc. +// This file is part of the Aleo library. + +// The Aleo library is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// The Aleo library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with the Aleo library. If not, see . + +use crate::{ + types::{VerifyingKeyNative, ToBytes, FromBytes}, +}; + +use std::{ops::Deref}; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +#[derive(Clone, Debug)] +pub struct VerifyingKey(VerifyingKeyNative); + +#[wasm_bindgen] +impl VerifyingKey { + #[wasm_bindgen] + pub fn from_bytes(bytes: Vec) -> VerifyingKey { + console_error_panic_hook::set_once(); + let verifying_key = VerifyingKeyNative::from_bytes_le(&bytes); + Self(verifying_key.unwrap()) + } + + #[wasm_bindgen] + pub fn to_bytes(&self) -> Result, String> { + console_error_panic_hook::set_once(); + self.0.to_bytes_le().map_err(|_| "Failed to serialize prover key".to_string()) + } +} + +impl Deref for VerifyingKey { + type Target = VerifyingKeyNative; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From for VerifyingKeyNative { + fn from(verifying_key: VerifyingKey) -> VerifyingKeyNative { + verifying_key.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn test_deserialize_serialize_key_wasm() { + let transfer_bytes = include_bytes!(concat!(env!("HOME"), "/.aleo/resources/transfer.verifier.db46e4c")).to_vec(); + let verifying_key = VerifyingKey::from_bytes(transfer_bytes.clone()); + let bytes = verifying_key.to_bytes().unwrap(); + assert!(transfer_bytes.eq(&bytes)); + } +} \ No newline at end of file diff --git a/wasm/src/types.rs b/wasm/src/types.rs index 57aa443a4..1e4bf723c 100644 --- a/wasm/src/types.rs +++ b/wasm/src/types.rs @@ -14,10 +14,31 @@ // You should have received a copy of the GNU General Public License // along with the Aleo library. If not, see . +use snarkvm_synthesizer::{ + Process as AleoProcess, + Program as AleoProgram, + Transaction as AleoTransaction, + Transition as AleoTransition, + ProvingKey as AleoProvingKey, + VerifyingKey as AleoVerifyingKey, + Inclusion as AleoInclusion, + Execution as AleoExecution +}; use snarkvm_wasm::{ account::{Address, PrivateKey, Signature, ViewKey}, - network::Testnet3, - program::{Ciphertext, Identifier, Plaintext, ProgramID, Record}, + network::Testnet3 +}; +use snarkvm_console::{ + program::{ + Ciphertext, + Identifier as AleoIdentifier, + InputID as AleoInputID, + Plaintext, + ProgramID, + Record, + StatePath as AleoStatePath, + Network + } }; pub use aleo_rust::Encryptor; @@ -30,7 +51,21 @@ pub type SignatureNative = Signature; pub type ViewKeyNative = ViewKey; // Network types +pub type Aleo = snarkvm_wasm::AleoV0; pub type CurrentNetwork = Testnet3; +pub type ProvingKeyNative = AleoProvingKey; +pub type VerifyingKeyNative = AleoVerifyingKey; + +// Program types +pub type InputIDNative = AleoInputID; +pub type ProcessNative = AleoProcess; +pub type ProgramNative = AleoProgram; +pub type TransactionNative = AleoTransaction; +pub type TransitionNative = AleoTransition; +pub type StateRootNative = ::StateRoot; +pub type StatePathNative = AleoStatePath; +pub type InclusionNative = AleoInclusion; +pub type ExecutionNative = AleoExecution; // Record types pub type CiphertextNative = Ciphertext; @@ -39,5 +74,5 @@ pub type RecordCiphertextNative = Record; pub type RecordPlaintextNative = Record; // Utility types -pub type IdentifierNative = Identifier; +pub type IdentifierNative = AleoIdentifier; pub type ProgramIDNative = ProgramID; diff --git a/website/package.json b/website/package.json index 6ba2a57a8..f0662ed84 100644 --- a/website/package.json +++ b/website/package.json @@ -4,11 +4,13 @@ "private": true, "dependencies": { "@aleohq/ui": "^0.0.9", - "@ant-design/icons": "^4.4.0", "@aleohq/wasm": "file:../wasm/pkg", + "@ant-design/icons": "^4.4.0", "antd": "^4.11.2", "axios": "^1.1.3", "babel-loader": "^8.2.3", + "babel-polyfill": "^6.26.0", + "comlink": "^4.4.1", "copy-to-clipboard": "^3.3.1", "gh-pages": "^3.1.0", "react": "^17.0.1", @@ -21,6 +23,7 @@ "@babel/preset-react": "^7.16.7", "@wasm-tool/wasm-pack-plugin": "^1.3.1", "css-loader": "^6.6.0", + "dexie": "^3.2.3", "html-webpack-plugin": "^5.5.0", "mini-css-extract-plugin": "^2.5.3", "style-loader": "^3.3.1", @@ -29,10 +32,7 @@ "webpack-dev-server": "^4.7.4" }, "scripts": { - "preinstall": "cd ../wasm && wasm-pack build", - "prestart": "yarn preinstall", - "prerestart": "yarn preinstall", - "start": "webpack-dev-server", + "start": "yarn upgrade @aleohq/wasm && webpack-dev-server", "build": "webpack --config webpack.config.js", "predeploy": "yarn build", "deploy": "gh-pages -d build", diff --git a/website/pkg/index.js b/website/pkg/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/website/public/index.html b/website/public/index.html index e59bfa852..96b1a0634 100644 --- a/website/public/index.html +++ b/website/public/index.html @@ -14,7 +14,7 @@ manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --> - +