diff --git a/Cargo.lock b/Cargo.lock index 3dc28d62..cf29f3f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,8 +5,7 @@ version = 3 [[package]] name = "acir" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c831802bad8d6810bfdb57cd00913157dfe710a185dc360c9a94eecd043bbf12" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir_field", "flate2", @@ -18,8 +17,7 @@ dependencies = [ [[package]] name = "acir_field" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d455746f51b4421548e42d34e2ce46b6db637cb048095d78766612d26bf526db" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "ark-bn254", "ark-ff", @@ -32,8 +30,7 @@ dependencies = [ [[package]] name = "acvm" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd8e75cf737b52195a8c109e0ee776e468e1c2ec42f0f88aa4465cd76a09b4ed" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir", "acvm_stdlib", @@ -55,7 +52,6 @@ dependencies = [ "acvm", "barretenberg-sys", "bincode", - "blake2", "bytesize", "getrandom", "pkg-config", @@ -71,8 +67,7 @@ dependencies = [ [[package]] name = "acvm_stdlib" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2e87549b987593eeb1902990006443f2fe5817db927035f081008e4b8a46a49" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir", ] @@ -240,6 +235,12 @@ dependencies = [ "rand", ] +[[package]] +name = "arrayvec" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" + [[package]] name = "async-trait" version = "0.1.68" @@ -268,7 +269,7 @@ dependencies = [ "cfg-if", "libc", "miniz_oxide 0.6.2", - "object 0.30.3", + "object", "rustc-demangle", ] @@ -511,56 +512,74 @@ dependencies = [ [[package]] name = "cranelift-bforest" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38faa2a16616c8e78a18d37b4726b98bfd2de192f2fdc8a39ddf568a408a0f75" +checksum = "2a2ab4512dfd3a6f4be184403a195f76e81a8a9f9e6c898e19d2dc3ce20e0115" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26f192472a3ba23860afd07d2b0217dc628f21fcc72617aa1336d98e1671f33b" +checksum = "98b022ed2a5913a38839dfbafe6cf135342661293b08049843362df4301261dc" dependencies = [ + "arrayvec", + "bumpalo", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", + "cranelift-egraph", "cranelift-entity", + "cranelift-isle", "gimli 0.26.2", "log", - "regalloc", + "regalloc2", "smallvec", "target-lexicon", ] [[package]] name = "cranelift-codegen-meta" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f32ddb89e9b89d3d9b36a5b7d7ea3261c98235a76ac95ba46826b8ec40b1a24" +checksum = "639307b45434ad112a98f8300c0f0ab085cbefcd767efcdef9ef19d4c0756e74" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01fd0d9f288cc1b42d9333b7a776b17e278fc888c28e6a0f09b5573d45a150bc" +checksum = "278e52e29c53fcf32431ef08406c295699a70306d05a0715c5b1bf50e33a9ab7" + +[[package]] +name = "cranelift-egraph" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624b54323b06e675293939311943ba82d323bb340468ce1889be5da7932c8d73" +dependencies = [ + "cranelift-entity", + "fxhash", + "hashbrown 0.12.3", + "indexmap", + "log", + "smallvec", +] [[package]] name = "cranelift-entity" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3bfe172b83167604601faf9dc60453e0d0a93415b57a9c4d1a7ae6849185cf" +checksum = "9a59bcbca89c3f1b70b93ab3cbba5e5e0cbf3e63dadb23c7525cb142e21a9d4c" [[package]] name = "cranelift-frontend" -version = "0.82.3" +version = "0.91.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a006e3e32d80ce0e4ba7f1f9ddf66066d052a8c884a110b91d05404d6ce26dce" +checksum = "0d70abacb8cfef3dc8ff7e8836e9c1d70f7967dfdac824a4cd5e30223415aca6" dependencies = [ "cranelift-codegen", "log", @@ -568,6 +587,12 @@ dependencies = [ "target-lexicon", ] +[[package]] +name = "cranelift-isle" +version = "0.91.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "393bc73c451830ff8dbb3a07f61843d6cb41a084f9996319917c0b291ed785bb" + [[package]] name = "crc32fast" version = "1.3.2" @@ -674,6 +699,19 @@ dependencies = [ "syn 1.0.107", ] +[[package]] +name = "dashmap" +version = "5.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc" +dependencies = [ + "cfg-if", + "hashbrown 0.12.3", + "lock_api", + "once_cell", + "parking_lot_core", +] + [[package]] name = "der" version = "0.1.0" @@ -835,15 +873,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" -[[package]] -name = "fastrand" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" -dependencies = [ - "instant", -] - [[package]] name = "ff" version = "0.8.0" @@ -925,6 +954,15 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "generic-array" version = "0.14.6" @@ -942,8 +980,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -1012,15 +1052,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "hashbrown" -version = "0.11.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" -dependencies = [ - "ahash 0.7.6", -] - [[package]] name = "hashbrown" version = "0.12.3" @@ -1165,16 +1196,6 @@ checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown 0.12.3", - "serde", -] - -[[package]] -name = "instant" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" -dependencies = [ - "cfg-if", ] [[package]] @@ -1272,33 +1293,22 @@ dependencies = [ ] [[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "loupe" -version = "0.1.3" +name = "lock_api" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6a72dfa44fe15b5e76b94307eeb2ff995a8c5b283b55008940c02e0c5b634d" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" dependencies = [ - "indexmap", - "loupe-derive", - "rustversion", + "autocfg", + "scopeguard", ] [[package]] -name = "loupe-derive" -version = "0.1.3" +name = "log" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0fbfc88337168279f2e9ae06e157cfed4efd3316e14dc96ed074d4f2e6c5952" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "quote", - "syn 1.0.107", + "cfg-if", ] [[package]] @@ -1327,18 +1337,18 @@ dependencies = [ [[package]] name = "memoffset" -version = "0.6.5" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" dependencies = [ "autocfg", ] [[package]] name = "memoffset" -version = "0.7.1" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" +checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1" dependencies = [ "autocfg", ] @@ -1441,18 +1451,6 @@ dependencies = [ "libc", ] -[[package]] -name = "object" -version = "0.28.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e42c982f2d955fac81dd7e1d0e1426a7d702acd9c98d19ab01083a6a0328c424" -dependencies = [ - "crc32fast", - "hashbrown 0.11.2", - "indexmap", - "memchr", -] - [[package]] name = "object" version = "0.30.3" @@ -1480,6 +1478,19 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "parking_lot_core" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys 0.45.0", +] + [[package]] name = "paste" version = "1.0.12" @@ -1674,13 +1685,14 @@ dependencies = [ ] [[package]] -name = "regalloc" -version = "0.0.34" +name = "regalloc2" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62446b1d3ebf980bdc68837700af1d77b37bc430e524bf95319c6eada2a4cc02" +checksum = "300d4fbfb40c1c66a78ba3ddd41c1110247cf52f97b87d0f2fc9209bd49b030c" dependencies = [ + "fxhash", "log", - "rustc-hash", + "slice-group-by", "smallvec", ] @@ -1713,15 +1725,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "remove_dir_all" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" -dependencies = [ - "winapi", -] - [[package]] name = "rend" version = "0.4.0" @@ -1793,6 +1796,7 @@ checksum = "c30f1d45d9aa61cbc8cd1eb87705470892289bb2d01943e7803b873a57404dc3" dependencies = [ "bytecheck", "hashbrown 0.12.3", + "indexmap", "ptr_meta", "rend", "rkyv_derive", @@ -1910,12 +1914,6 @@ dependencies = [ "base64", ] -[[package]] -name = "rustversion" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5583e89e108996506031660fe09baa5011b9dd0341b89029313006d1fb508d70" - [[package]] name = "ryu" version = "1.0.12" @@ -1978,12 +1976,14 @@ dependencies = [ ] [[package]] -name = "serde_bytes" -version = "0.11.9" +name = "serde-wasm-bindgen" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "416bda436f9aab92e02c8e10d49a15ddd339cea90b6e340fe51ed97abb548294" +checksum = "e3b4c031cd0d9014307d82b8abf653c0290fbdaeb4c02d00c63cf52f728628bf" dependencies = [ + "js-sys", "serde", + "wasm-bindgen", ] [[package]] @@ -2097,6 +2097,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "slice-group-by" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826167069c09b99d56f31e9ae5c99049e932a98c9dc2dac47645b08dbbf76ba7" + [[package]] name = "smallvec" version = "1.10.0" @@ -2171,20 +2177,6 @@ version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ae9980cab1db3fceee2f6c6f643d5d8de2997c58ee8d25fb0cc8a9e9e7348e5" -[[package]] -name = "tempfile" -version = "3.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" -dependencies = [ - "cfg-if", - "fastrand", - "libc", - "redox_syscall", - "remove_dir_all", - "winapi", -] - [[package]] name = "thiserror" version = "1.0.38" @@ -2295,7 +2287,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ "cfg-if", - "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2469,6 +2460,29 @@ dependencies = [ "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-downcast" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dac026d43bcca6e7ce1c0956ba68f59edf6403e8e930a5d891be72c31a44340" +dependencies = [ + "js-sys", + "once_cell", + "wasm-bindgen", + "wasm-bindgen-downcast-macros", +] + +[[package]] +name = "wasm-bindgen-downcast-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5020cfa87c7cecefef118055d44e3c1fc122c7ec25701d528ee458a0b45f38f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.107", +] + [[package]] name = "wasm-bindgen-futures" version = "0.4.34" @@ -2521,74 +2535,67 @@ dependencies = [ [[package]] name = "wasmer" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea8d8361c9d006ea3d7797de7bd6b1492ffd0f91a22430cfda6c1658ad57bedf" +checksum = "78caedecd8cb71ed47ccca03b68d69414a3d278bb031e6f93f15759344efdd52" dependencies = [ + "bytes", "cfg-if", + "derivative", "indexmap", "js-sys", - "loupe", "more-asserts", + "rustc-demangle", + "serde", + "serde-wasm-bindgen", "target-lexicon", "thiserror", "wasm-bindgen", - "wasmer-artifact", + "wasm-bindgen-downcast", "wasmer-compiler", "wasmer-compiler-cranelift", "wasmer-derive", - "wasmer-engine", - "wasmer-engine-dylib", - "wasmer-engine-universal", "wasmer-types", "wasmer-vm", - "wasmparser", + "wasmparser 0.83.0", + "wasmparser 0.95.0", "wat", "winapi", ] -[[package]] -name = "wasmer-artifact" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7aaf9428c29c1d8ad2ac0e45889ba8a568a835e33fd058964e5e500f2f7ce325" -dependencies = [ - "enumset", - "loupe", - "thiserror", - "wasmer-compiler", - "wasmer-types", -] - [[package]] name = "wasmer-compiler" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e67a6cd866aed456656db2cfea96c18baabbd33f676578482b85c51e1ee19d2c" +checksum = "726a8450541af4a57c34af7b6973fdbfc79f896cc7e733429577dfd1d1687180" dependencies = [ + "backtrace", + "cfg-if", + "enum-iterator", "enumset", - "loupe", - "rkyv", - "serde", - "serde_bytes", + "lazy_static", + "leb128", + "memmap2", + "more-asserts", + "region", "smallvec", - "target-lexicon", "thiserror", "wasmer-types", - "wasmparser", + "wasmer-vm", + "wasmparser 0.95.0", + "winapi", ] [[package]] name = "wasmer-compiler-cranelift" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48be2f9f6495f08649e4f8b946a2cbbe119faf5a654aa1457f9504a99d23dae0" +checksum = "a1e5633f90f372563ebbdf3f9799c7b29ba11c90e56cf9b54017112d2e656c95" dependencies = [ "cranelift-codegen", "cranelift-entity", "cranelift-frontend", "gimli 0.26.2", - "loupe", "more-asserts", "rayon", "smallvec", @@ -2600,9 +2607,9 @@ dependencies = [ [[package]] name = "wasmer-derive" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e50405cc2a2f74ff574584710a5f2c1d5c93744acce2ca0866084739284b51" +checksum = "97901fdbaae383dbb90ea162cc3a76a9fa58ac39aec7948b4c0b9bbef9307738" dependencies = [ "proc-macro-error", "proc-macro2", @@ -2610,143 +2617,45 @@ dependencies = [ "syn 1.0.107", ] -[[package]] -name = "wasmer-engine" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f98f010978c244db431b392aeab0661df7ea0822343334f8f2a920763548e45" -dependencies = [ - "backtrace", - "enumset", - "lazy_static", - "loupe", - "memmap2", - "more-asserts", - "rustc-demangle", - "serde", - "serde_bytes", - "target-lexicon", - "thiserror", - "wasmer-artifact", - "wasmer-compiler", - "wasmer-types", - "wasmer-vm", -] - -[[package]] -name = "wasmer-engine-dylib" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0358af9c154724587731175553805648d9acb8f6657880d165e378672b7e53" -dependencies = [ - "cfg-if", - "enum-iterator", - "enumset", - "leb128", - "libloading", - "loupe", - "object 0.28.4", - "rkyv", - "serde", - "tempfile", - "tracing", - "wasmer-artifact", - "wasmer-compiler", - "wasmer-engine", - "wasmer-object", - "wasmer-types", - "wasmer-vm", - "which", -] - -[[package]] -name = "wasmer-engine-universal" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "440dc3d93c9ca47865a4f4edd037ea81bf983b5796b59b3d712d844b32dbef15" -dependencies = [ - "cfg-if", - "enumset", - "leb128", - "loupe", - "region", - "rkyv", - "wasmer-compiler", - "wasmer-engine", - "wasmer-engine-universal-artifact", - "wasmer-types", - "wasmer-vm", - "winapi", -] - -[[package]] -name = "wasmer-engine-universal-artifact" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68f1db3f54152657eb6e86c44b66525ff7801dad8328fe677da48dd06af9ad41" -dependencies = [ - "enum-iterator", - "enumset", - "loupe", - "rkyv", - "thiserror", - "wasmer-artifact", - "wasmer-compiler", - "wasmer-types", -] - -[[package]] -name = "wasmer-object" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d831335ff3a44ecf451303f6f891175c642488036b92ceceb24ac8623a8fa8b" -dependencies = [ - "object 0.28.4", - "thiserror", - "wasmer-compiler", - "wasmer-types", -] - [[package]] name = "wasmer-types" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39df01ea05dc0a9bab67e054c7cb01521e53b35a7bb90bd02eca564ed0b2667f" +checksum = "67f1f2839f4f61509550e4ddcd0e658e19f3af862b51c79fda15549d735d659b" dependencies = [ - "backtrace", + "bytecheck", "enum-iterator", + "enumset", "indexmap", - "loupe", "more-asserts", "rkyv", - "serde", + "target-lexicon", "thiserror", ] [[package]] name = "wasmer-vm" -version = "2.3.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30d965fa61f4dc4cdb35a54daaf7ecec3563fbb94154a6c35433f879466247dd" +checksum = "043118ec4f16d1714fed3aab758b502b864bd865e1d5188626c9ad290100563f" dependencies = [ "backtrace", "cc", "cfg-if", "corosensei", + "dashmap", + "derivative", "enum-iterator", + "fnv", "indexmap", "lazy_static", "libc", - "loupe", "mach", - "memoffset 0.6.5", + "memoffset 0.8.0", "more-asserts", "region", - "rkyv", "scopeguard", - "serde", "thiserror", - "wasmer-artifact", "wasmer-types", "winapi", ] @@ -2757,6 +2666,16 @@ version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "718ed7c55c2add6548cca3ddd6383d738cd73b892df400e96b9aa876f0141d7a" +[[package]] +name = "wasmparser" +version = "0.95.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ea896273ea99b15132414be1da01ab0d8836415083298ecaffbe308eaac87a" +dependencies = [ + "indexmap", + "url", +] + [[package]] name = "wast" version = "54.0.0" diff --git a/Cargo.toml b/Cargo.toml index 141577b6..879175b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] + [dependencies] acvm = { version = "0.12.0", features = ["bn254"] } bincode = "1.3.3" @@ -18,23 +21,35 @@ serde = { version = "1.0.136", features = ["derive"] } serde-big-array = "0.5.1" thiserror = "1.0.21" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Native barretenberg-sys = { version = "0.1.2", optional = true } # Wasm -wasmer = { version = "2.3", optional = true, default-features = false } +getrandom = { version = "0.2", optional = true } rust-embed = { version = "6.6.0", optional = true, features = [ "debug-embed", "interpolate-folder-path", "include-exclude", ] } -getrandom = { version = "0.2", optional = true } +wasmer = { version = "3.3", optional = true, default-features = false, features = [ + "sys-default", + "cranelift" +] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = [ "js" ] } +rust-embed = { version = "6.6.0", features = [ + "debug-embed", + "interpolate-folder-path", + "include-exclude", +] } +wasmer = { version = "3.3", default-features = false, features = [ "js-default" ] } [build-dependencies] pkg-config = "0.3" -[dev-dependencies] -blake2 = "0.10.6" +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tokio = { version = "1.0", features = [ "macros" ] } [features] @@ -43,18 +58,10 @@ native = [ "dep:barretenberg-sys" ] wasm = [ - "wasmer", + "dep:wasmer", "dep:rust-embed", - "dep:getrandom", - "wasmer/sys-default", - "wasmer/cranelift", - "wasmer/default-compiler", - "wasmer/default-cranelift", - "wasmer/default-universal" -] -js = [ - "wasmer", - "dep:rust-embed", - "dep:getrandom", - "wasmer/js-default" + "dep:getrandom" ] + +[patch.crates-io] +acvm = { git = "https://github.com/noir-lang/acvm", rev = "9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" } diff --git a/src/acvm_interop/common_reference_string.rs b/src/acvm_interop/common_reference_string.rs index 2122ce00..a52c239a 100644 --- a/src/acvm_interop/common_reference_string.rs +++ b/src/acvm_interop/common_reference_string.rs @@ -3,7 +3,7 @@ use acvm::{acir::circuit::Circuit, async_trait, CommonReferenceString}; use crate::{composer::Composer, BackendError, Barretenberg}; // TODO(#185): Ensure CRS download works in JS -#[async_trait] +#[async_trait(?Send)] impl CommonReferenceString for Barretenberg { type Error = BackendError; diff --git a/src/acvm_interop/proof_system.rs b/src/acvm_interop/proof_system.rs index 74d670ea..fa9b559c 100644 --- a/src/acvm_interop/proof_system.rs +++ b/src/acvm_interop/proof_system.rs @@ -34,7 +34,6 @@ impl ProofSystemCompiler for Barretenberg { | BlackBoxFunc::SHA256 | BlackBoxFunc::Blake2s | BlackBoxFunc::Keccak256 - | BlackBoxFunc::ComputeMerkleRoot | BlackBoxFunc::SchnorrVerify | BlackBoxFunc::Pedersen | BlackBoxFunc::HashToField128Security diff --git a/src/acvm_interop/pwg.rs b/src/acvm_interop/pwg.rs index 3d7c2928..816252c1 100644 --- a/src/acvm_interop/pwg.rs +++ b/src/acvm_interop/pwg.rs @@ -1,17 +1,14 @@ use acvm::acir::circuit::opcodes::FunctionInput; use acvm::acir::native_types::{Witness, WitnessMap}; use acvm::acir::BlackBoxFunc; -use acvm::pwg::{hash, logic, range, signature, witness_to_value}; -use acvm::{pwg::OpcodeResolution, FieldElement}; -use acvm::{OpcodeResolutionError, PartialWitnessGenerator}; +use acvm::pwg::{witness_to_value, OpcodeResolution, OpcodeResolutionError}; +use acvm::{FieldElement, PartialWitnessGenerator}; use crate::pedersen::Pedersen; use crate::scalar_mul::ScalarMul; use crate::schnorr::SchnorrSig; use crate::Barretenberg; -mod merkle; - impl PartialWitnessGenerator for Barretenberg { fn aes( &self, @@ -24,86 +21,6 @@ impl PartialWitnessGenerator for Barretenberg { )) } - fn and( - &self, - initial_witness: &mut WitnessMap, - lhs: &FunctionInput, - rhs: &FunctionInput, - output: &Witness, - ) -> Result { - logic::and(initial_witness, lhs, rhs, output) - } - - fn xor( - &self, - initial_witness: &mut WitnessMap, - lhs: &FunctionInput, - rhs: &FunctionInput, - output: &Witness, - ) -> Result { - logic::xor(initial_witness, lhs, rhs, output) - } - - fn range( - &self, - initial_witness: &mut WitnessMap, - input: &FunctionInput, - ) -> Result { - range::solve_range_opcode(initial_witness, input) - } - - fn sha256( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::sha256(initial_witness, inputs, outputs) - } - - fn blake2s( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::blake2s256(initial_witness, inputs, outputs) - } - - fn compute_merkle_root( - &self, - initial_witness: &mut WitnessMap, - leaf: &FunctionInput, - index: &FunctionInput, - hash_path: &[FunctionInput], - output: &Witness, - ) -> Result { - let leaf = witness_to_value(initial_witness, leaf.witness)?; - - let index = witness_to_value(initial_witness, index.witness)?; - - let hash_path: Result, _> = hash_path - .iter() - .map(|input| witness_to_value(initial_witness, input.witness)) - .collect(); - - let computed_merkle_root = merkle::compute_merkle_root( - |left, right| self.compress_native(left, right), - hash_path?, - index, - leaf, - ) - .map_err(|err| { - OpcodeResolutionError::BlackBoxFunctionFailed( - BlackBoxFunc::ComputeMerkleRoot, - err.to_string(), - ) - })?; - - initial_witness.insert(*output, computed_merkle_root); - Ok(OpcodeResolution::Solved) - } - fn schnorr_verify( &self, initial_witness: &mut WitnessMap, @@ -212,34 +129,6 @@ impl PartialWitnessGenerator for Barretenberg { Ok(OpcodeResolution::Solved) } - fn hash_to_field_128_security( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - output: &Witness, - ) -> Result { - hash::hash_to_field_128_security(initial_witness, inputs, output) - } - - fn ecdsa_secp256k1( - &self, - initial_witness: &mut WitnessMap, - public_key_x: &[FunctionInput], - public_key_y: &[FunctionInput], - signature: &[FunctionInput], - message: &[FunctionInput], - outputs: &Witness, - ) -> Result { - signature::ecdsa::secp256k1_prehashed( - initial_witness, - public_key_x, - public_key_y, - signature, - message, - *outputs, - ) - } - fn fixed_base_scalar_mul( &self, initial_witness: &mut WitnessMap, @@ -259,13 +148,4 @@ impl PartialWitnessGenerator for Barretenberg { initial_witness.insert(outputs[1], pub_y); Ok(OpcodeResolution::Solved) } - - fn keccak256( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::keccak256(initial_witness, inputs, outputs) - } } diff --git a/src/acvm_interop/pwg/merkle.rs b/src/acvm_interop/pwg/merkle.rs deleted file mode 100644 index d194ccb3..00000000 --- a/src/acvm_interop/pwg/merkle.rs +++ /dev/null @@ -1,185 +0,0 @@ -use acvm::FieldElement; - -use crate::Error; - -pub(super) fn compute_merkle_root( - hash_func: impl Fn(&FieldElement, &FieldElement) -> Result, - hash_path: Vec<&FieldElement>, - index: &FieldElement, - leaf: &FieldElement, -) -> Result { - let mut index_bits: Vec = index.bits(); - index_bits.reverse(); - - assert!( - hash_path.len() <= index_bits.len(), - "hash path exceeds max depth of tree" - ); - index_bits.into_iter().zip(hash_path.into_iter()).fold( - Ok(*leaf), - |current_node, (path_bit, path_elem)| match current_node { - Ok(current_node) => { - let (left, right) = if !path_bit { - (¤t_node, path_elem) - } else { - (path_elem, ¤t_node) - }; - hash_func(left, right) - } - Err(_) => current_node, - }, - ) -} - -#[cfg(test)] -mod tests { - use crate::merkle::{MerkleTree, MessageHasher}; - use crate::Error; - use crate::{pedersen::Pedersen, Barretenberg}; - use acvm::FieldElement; - - #[test] - fn test_check_membership() -> Result<(), Error> { - struct Test<'a> { - // Index of the leaf in the MerkleTree - index: &'a str, - // Returns true if the leaf is indeed a part of the MerkleTree at the specified index - result: bool, - // The message is used to derive the leaf at `index` by using the specified hash - message: Vec, - // If this is true, then before checking for membership - // we update the tree with the message at that index - should_update_tree: bool, - error_msg: &'a str, - } - // Note these test cases are not independent. - // i.e. If you update index 0, then this will be saved for the next test - let tests = vec![ - Test { - index : "0", - result : true, - message : vec![0;64], - should_update_tree: false, - error_msg : "this should always be true, since the tree is initialized with 64 zeroes" - }, - Test { - index : "0", - result : false, - message : vec![10;64], - should_update_tree: false, - error_msg : "this should be false, since the tree was not updated, however the message which derives the leaf has changed" - }, - Test { - index : "0", - result : true, - message : vec![1;64], - should_update_tree: true, - error_msg : "this should be true, since we are updating the tree" - }, - Test { - index : "0", - result : true, - message : vec![1;64], - should_update_tree: false, - error_msg : "this should be true since the index at 4 has not been changed yet, so it would be [0;64]" - }, - Test { - index : "4", - result : true, - message : vec![0;64], - should_update_tree: false, - error_msg : "this should be true since the index at 4 has not been changed yet, so it would be [0;64]" - }, - ]; - - let mut msg_hasher: blake2::Blake2s256 = MessageHasher::new(); - - let mut tree: MerkleTree = MerkleTree::new(3); - - for test_vector in tests { - let index = FieldElement::try_from_str(test_vector.index).unwrap(); - let index_as_usize: usize = test_vector.index.parse().unwrap(); - let mut index_bits = index.bits(); - index_bits.reverse(); - - let leaf = msg_hasher.hash(&test_vector.message); - - let mut root = tree.root(); - if test_vector.should_update_tree { - root = tree.update_message(index_as_usize, &test_vector.message)?; - } - - let hash_path = tree.get_hash_path(index_as_usize); - let mut hash_path_ref = Vec::new(); - for (i, path_pair) in hash_path.into_iter().enumerate() { - let path_bit = index_bits[i]; - let hash = if !path_bit { path_pair.1 } else { path_pair.0 }; - hash_path_ref.push(hash); - } - let hash_path_ref = hash_path_ref.iter().collect(); - - let bb = Barretenberg::new(); - let computed_merkle_root = super::compute_merkle_root( - |left, right| bb.compress_native(left, right), - hash_path_ref, - &index, - &leaf, - )?; - let is_leaf_in_tree = root == computed_merkle_root; - - assert_eq!( - is_leaf_in_tree, test_vector.result, - "{}", - test_vector.error_msg - ); - } - - Ok(()) - } - - // This test uses `update_leaf` directly rather than `update_message` - #[test] - fn simple_shield() -> Result<(), Error> { - let mut tree: MerkleTree = MerkleTree::new(3); - - let barretenberg = Barretenberg::new(); - let pubkey_x = FieldElement::from_hex( - "0x0bff8247aa94b08d1c680d7a3e10831bd8c8cf2ea2c756b0d1d89acdcad877ad", - ) - .unwrap(); - let pubkey_y = FieldElement::from_hex( - "0x2a5d7253a6ed48462fedb2d350cc768d13956310f54e73a8a47914f34a34c5c4", - ) - .unwrap(); - let (note_commitment_x, _) = barretenberg.encrypt(vec![pubkey_x, pubkey_y])?; - dbg!(note_commitment_x.to_hex()); - let leaf = note_commitment_x; - - let index = FieldElement::try_from_str("0").unwrap(); - let index_as_usize: usize = 0_usize; - let mut index_bits = index.bits(); - index_bits.reverse(); - - let root = tree.update_leaf(index_as_usize, leaf)?; - - let hash_path = tree.get_hash_path(index_as_usize); - let mut hash_path_ref = Vec::new(); - for (i, path_pair) in hash_path.into_iter().enumerate() { - let path_bit = index_bits[i]; - let hash = if !path_bit { path_pair.1 } else { path_pair.0 }; - hash_path_ref.push(hash); - } - let hash_path_ref = hash_path_ref.iter().collect(); - let bb = Barretenberg::new(); - let computed_merkle_root = super::compute_merkle_root( - |left, right| bb.compress_native(left, right), - hash_path_ref, - &index, - &leaf, - )?; - - assert_eq!(root, computed_merkle_root); - - Ok(()) - } -} diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 55ca562a..e1321855 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -750,7 +750,9 @@ impl TryFrom<&Circuit> for ConstraintSystem { let mut block_constraints: Vec = Vec::new(); let mut keccak_constraints: Vec = Vec::new(); let mut pedersen_constraints: Vec = Vec::new(); - let mut compute_merkle_root_constraints: Vec = Vec::new(); + // ACVM doesn't generate `ComputeMerkleRootConstraint`s anymore. + // We maintain this to maintain the serialization format. + let compute_merkle_root_constraints: Vec = Vec::new(); let mut schnorr_constraints: Vec = Vec::new(); let mut ecdsa_secp256k1_constraints: Vec = Vec::new(); let mut fixed_base_scalar_mul_constraints: Vec = Vec::new(); @@ -865,36 +867,6 @@ impl TryFrom<&Circuit> for ConstraintSystem { blake2s_constraints.push(blake2s_constraint); } - BlackBoxFuncCall::ComputeMerkleRoot { - leaf, - index, - hash_path: hash_path_inputs, - output, - } => { - // leaf - let leaf = leaf.witness.witness_index() as i32; - // index - let index = index.witness.witness_index() as i32; - - let mut hash_path = Vec::new(); - for path_elem in hash_path_inputs.iter() { - let path_elem_index = path_elem.witness.witness_index() as i32; - - hash_path.push(path_elem_index); - } - - // computed root - let result = output.witness_index() as i32; - - let constraint = ComputeMerkleRootConstraint { - hash_path, - leaf, - index, - result, - }; - - compute_merkle_root_constraints.push(constraint); - } BlackBoxFuncCall::SchnorrVerify { public_key_x, public_key_y, diff --git a/src/composer.rs b/src/composer.rs index dcc772ef..494ccf39 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -6,7 +6,7 @@ use crate::{crs::CRS, Barretenberg, Error, FIELD_BYTES}; const NUM_RESERVED_GATES: u32 = 4; // this must be >= num_roots_cut_out_of_vanishing_polynomial (found under prover settings in barretenberg) -#[async_trait] +#[async_trait(?Send)] pub(crate) trait Composer { fn get_circuit_size(&self, constraint_system: &ConstraintSystem) -> Result; @@ -410,13 +410,9 @@ mod test { use tokio::test; use super::*; - use crate::{ - barretenberg_structures::{ - BlockConstraint, ComputeMerkleRootConstraint, Constraint, Keccak256Constraint, - LogicConstraint, MemOpBarretenberg, PedersenConstraint, RangeConstraint, - SchnorrConstraint, - }, - merkle::{MerkleTree, MessageHasher}, + use crate::barretenberg_structures::{ + BlockConstraint, Constraint, Keccak256Constraint, LogicConstraint, MemOpBarretenberg, + PedersenConstraint, RangeConstraint, SchnorrConstraint, }; #[test] @@ -892,55 +888,6 @@ mod test { test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]).await } - #[test] - async fn test_compute_merkle_root_constraint() -> Result<(), Error> { - let mut msg_hasher: blake2::Blake2s256 = MessageHasher::new(); - - let tree: MerkleTree = MerkleTree::new(3); - - let empty_leaf = vec![0; 64]; - - let index = FieldElement::zero(); - let index_as_usize: usize = 0; - let mut index_bits = index.bits(); - index_bits.reverse(); - - let leaf = msg_hasher.hash(&empty_leaf); - - let root = tree.root(); - - let hash_path = tree.get_hash_path(index_as_usize); - let mut hash_path_ref = Vec::new(); - for (i, path_pair) in hash_path.into_iter().enumerate() { - let path_bit = index_bits[i]; - let hash = if !path_bit { path_pair.1 } else { path_pair.0 }; - hash_path_ref.push(hash); - } - let hash_path_ref: Vec = hash_path_ref.into_iter().collect(); - - let constraint = ComputeMerkleRootConstraint { - hash_path: (3..3 + hash_path_ref.len() as i32).collect(), - leaf: 0, - index: 1, - result: 2, - }; - - let constraint_system = ConstraintSystem::new() - .var_num(500) - .compute_merkle_root_constraints(vec![constraint]); - - let mut witness_values = vec![leaf, index, root]; - witness_values.extend(hash_path_ref); - - let case_1 = WitnessResult { - witness: witness_values.into(), - public_inputs: vec![].into(), - result: true, - }; - - test_composer_with_pk_vk(constraint_system, vec![case_1]).await - } - #[test] async fn test_logic_constraints() -> Result<(), Error> { /* diff --git a/src/lib.rs b/src/lib.rs index b4f4bbaf..551d53b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,17 +4,19 @@ // `acvm-backend-barretenberg` can either interact with the Barretenberg backend through a static library // or through an embedded wasm binary. It does not make sense to include both of these backends at the same time. // We then throw a compilation error if both flags are set. -// TODO: handle JS target. #[cfg(all(feature = "native", feature = "wasm"))] compile_error!("feature \"native\" and feature \"wasm\" cannot be enabled at the same time"); +#[cfg(all(feature = "native", target_arch = "wasm32"))] +compile_error!("feature \"native\" cannot be enabled for a \"wasm32\" target"); + +#[cfg(all(feature = "wasm", target_arch = "wasm32"))] +compile_error!("feature \"wasm\" cannot be enabled for a \"wasm32\" target"); + mod acvm_interop; mod barretenberg_structures; mod composer; -#[cfg(any(feature = "native", feature = "wasm"))] mod crs; -#[cfg(test)] -mod merkle; mod pedersen; mod pippenger; mod scalar_mul; @@ -122,9 +124,11 @@ const FIELD_BYTES: usize = 32; #[derive(Debug)] pub struct Barretenberg { - #[cfg(feature = "wasm")] + #[cfg(not(feature = "native"))] + store: std::cell::RefCell, + #[cfg(not(feature = "native"))] memory: wasmer::Memory, - #[cfg(feature = "wasm")] + #[cfg(not(feature = "native"))] instance: wasmer::Instance, } @@ -165,7 +169,11 @@ mod native { #[cfg(not(feature = "native"))] mod wasm { - use wasmer::{imports, Function, Instance, Memory, MemoryType, Module, Store, Value}; + use std::cell::RefCell; + use wasmer::{ + imports, Function, FunctionEnv, FunctionEnvMut, Instance, Memory, MemoryType, Module, + Store, Value, WasmPtr, + }; use super::{Barretenberg, Error, FeatureError}; @@ -189,16 +197,15 @@ mod wasm { impl Barretenberg { pub(crate) fn new() -> Barretenberg { - let (instance, memory) = instance_load(); - Barretenberg { memory, instance } + let (instance, memory, store) = instance_load(); + Barretenberg { + memory, + instance, + store: RefCell::new(store), + } } } - #[derive(wasmer::WasmerEnv, Clone)] - struct Env { - memory: Memory, - } - /// A wrapper around the arguments or return value from a WASM call. /// Notice, `Option` is used because not every call returns a value, /// some calls are simply made to free a pointer or manipulate the heap. @@ -275,26 +282,13 @@ mod wasm { impl Barretenberg { /// Transfer bytes to WASM heap - pub(super) fn transfer_to_heap(&self, arr: &[u8], offset: usize) { + // TODO: Consider making this Result-returning + pub(super) fn transfer_to_heap(&self, data: &[u8], offset: usize) { let memory = &self.memory; + let store = self.store.borrow(); + let memory_view = memory.view(&store); - #[cfg(feature = "js")] - { - let view = memory.uint8view(); - for (byte_id, cell_id) in (offset..(offset + arr.len())).enumerate() { - view.set_index(cell_id as u32, arr[byte_id]) - } - } - - #[cfg(not(feature = "js"))] - { - for (byte_id, cell) in memory.uint8view()[offset..(offset + arr.len())] - .iter() - .enumerate() - { - cell.set(arr[byte_id]); - } - } + memory_view.write(offset as u64, data).unwrap() } // TODO: Consider making this Result-returning @@ -304,21 +298,16 @@ mod wasm { .expect("Read memory should be of the specified length") } - pub(super) fn read_memory_variable_length(&self, start: usize, length: usize) -> Vec { + // TODO: Consider making this Result-returning + pub(super) fn read_memory_variable_length(&self, offset: usize, length: usize) -> Vec { let memory = &self.memory; - let end = start + length; - - #[cfg(feature = "js")] - return memory - .uint8view() - .subarray(start as u32, end as u32) - .to_vec(); - - #[cfg(not(feature = "js"))] - return memory.view()[start..end] - .iter() - .map(|cell| cell.get()) - .collect(); + let store = &self.store.borrow(); + let memory_view = memory.view(&store); + + let mut buf = vec![0; length]; + + memory_view.read(offset as u64, &mut buf).unwrap(); + buf } pub(super) fn get_pointer(&self, ptr_ptr: usize) -> usize { @@ -348,12 +337,12 @@ mod wasm { source, } })?; - let boxed_value = - func.call(&args) - .map_err(|source| FeatureError::FunctionCallFailed { - name: name.to_string(), - source, - })?; + let boxed_value = func + .call(&mut self.store.borrow_mut(), &args) + .map_err(|source| FeatureError::FunctionCallFailed { + name: name.to_string(), + source, + })?; let option_value = boxed_value.first().cloned(); Ok(WASMValue(option_value)) @@ -379,114 +368,93 @@ mod wasm { } } - fn load_module() -> (Module, Store) { - let store = Store::default(); + fn instance_load() -> (Instance, Memory, Store) { + let mut store = Store::default(); let module = Module::new(&store, Wasm::get("barretenberg.wasm").unwrap().data).unwrap(); - (module, store) - } - fn instance_load() -> (Instance, Memory) { - let (module, store) = load_module(); + let mem_type = MemoryType::new(22, None, false); + let memory = Memory::new(&mut store, mem_type).unwrap(); - let mem_type = MemoryType::new(130, None, false); - let memory = Memory::new(&store, mem_type).unwrap(); + let function_env = FunctionEnv::new(&mut store, memory.clone()); let custom_imports = imports! { "env" => { - "logstr" => Function::new_native_with_env( - &store, - Env { - memory: memory.clone(), - }, + "logstr" => Function::new_typed_with_env( + &mut store, + &function_env, logstr, ), - "set_data" => Function::new_native(&store, set_data), - "get_data" => Function::new_native(&store, get_data), - "env_load_verifier_crs" => Function::new_native(&store, env_load_verifier_crs), - "env_load_prover_crs" => Function::new_native(&store, env_load_prover_crs), + "set_data" => Function::new_typed(&mut store, set_data), + "get_data" => Function::new_typed(&mut store, get_data), + "env_load_verifier_crs" => Function::new_typed(&mut store, env_load_verifier_crs), + "env_load_prover_crs" => Function::new_typed(&mut store, env_load_prover_crs), "memory" => memory.clone(), }, "wasi_snapshot_preview1" => { - "fd_read" => Function::new_native(&store, fd_read), - "fd_close" => Function::new_native(&store, fd_close), - "proc_exit" => Function::new_native(&store, proc_exit), - "fd_fdstat_get" => Function::new_native(&store, fd_fdstat_get), - "random_get" => Function::new_native_with_env( - &store, - Env { - memory: memory.clone(), - }, + "fd_read" => Function::new_typed(&mut store, fd_read), + "fd_close" => Function::new_typed(&mut store, fd_close), + "proc_exit" => Function::new_typed(&mut store, proc_exit), + "fd_fdstat_get" => Function::new_typed(&mut store, fd_fdstat_get), + "random_get" => Function::new_typed_with_env( + &mut store, + &function_env, random_get ), - "fd_seek" => Function::new_native(&store, fd_seek), - "fd_write" => Function::new_native(&store, fd_write), - "environ_sizes_get" => Function::new_native(&store, environ_sizes_get), - "environ_get" => Function::new_native(&store, environ_get), + "fd_seek" => Function::new_typed(&mut store, fd_seek), + "fd_write" => Function::new_typed(&mut store, fd_write), + "environ_sizes_get" => Function::new_typed(&mut store, environ_sizes_get), + "environ_get" => Function::new_typed(&mut store, environ_get), + "clock_time_get" => Function::new_typed(&mut store, clock_time_get), }, }; - (Instance::new(&module, &custom_imports).unwrap(), memory) + ( + Instance::new(&mut store, &module, &custom_imports).unwrap(), + memory, + store, + ) } - fn logstr(env: &Env, ptr: i32) { - let mut ptr_end = 0; - let byte_view = env.memory.uint8view(); - - #[cfg(feature = "js")] - for (i, cell) in byte_view.to_vec()[ptr as usize..].iter().enumerate() { - if cell != &0_u8 { - ptr_end = i; - } else { - break; - } - } - - #[cfg(not(feature = "js"))] - for (i, cell) in byte_view[ptr as usize..].iter().enumerate() { - if cell.get() != 0 { - ptr_end = i; - } else { - break; - } - } - - #[cfg(feature = "js")] - let str_vec: Vec<_> = - byte_view.to_vec()[ptr as usize..=(ptr + ptr_end as i32) as usize].to_vec(); + fn logstr(mut env: FunctionEnvMut, ptr: i32) { + let (memory, store) = env.data_and_store_mut(); + let memory_view = memory.view(&store); - #[cfg(not(feature = "js"))] - let str_vec: Vec<_> = byte_view[ptr as usize..=(ptr + ptr_end as i32) as usize] - .iter() - .cloned() - .map(|chr| chr.get()) - .collect(); + let log_str_wasm_ptr: WasmPtr = WasmPtr::new(ptr as u32); - // Convert the subslice to a `&str`. - let string = std::str::from_utf8(&str_vec).unwrap(); - - // Print it! - println!("{string}"); + match log_str_wasm_ptr.read_utf8_string_with_nul(&memory_view) { + Ok(log_string) => println!("{log_string}"), + Err(err) => println!("Error while reading log string from memory: {err}"), + }; } // Based on https://github.com/wasmerio/wasmer/blob/2.3.0/lib/wasi/src/syscalls/mod.rs#L2537 - fn random_get(env: &Env, buf: i32, buf_len: i32) -> i32 { + fn random_get(mut env: FunctionEnvMut, buf_ptr: i32, buf_len: i32) -> i32 { let mut u8_buffer = vec![0; buf_len as usize]; let res = getrandom::getrandom(&mut u8_buffer); match res { Ok(()) => { - unsafe { - env.memory - .uint8view() - .subarray(buf as u32, buf as u32 + buf_len as u32) - .copy_from(&u8_buffer); + let (memory, store) = env.data_and_store_mut(); + let memory_view = memory.view(&store); + match memory_view.write(buf_ptr as u64, u8_buffer.as_mut_slice()) { + Ok(_) => { + 0_i32 // __WASI_ESUCCESS + } + Err(_) => { + 29_i32 // __WASI_EIO + } } - 0_i32 // __WASI_ESUCCESS } - Err(_) => 29_i32, // __WASI_EIO + Err(_) => { + 29_i32 // __WASI_EIO + } } } + fn clock_time_get(_: i32, _: i64, _: i32) -> i32 { + unimplemented!("proc_exit: clock_time_get is not implemented") + } + fn proc_exit(_: i32) { unimplemented!("proc_exit is not implemented") } diff --git a/src/merkle.rs b/src/merkle.rs deleted file mode 100644 index 60edc29b..00000000 --- a/src/merkle.rs +++ /dev/null @@ -1,394 +0,0 @@ -// TODO(#166): Rework this module to return results -use acvm::FieldElement; -use std::{collections::BTreeMap, convert::TryInto}; - -use crate::{pedersen::Pedersen, Barretenberg, Error}; - -// Hashes the leaves up the path, on the way to the root -pub(crate) trait PathHasher { - fn new() -> Self; - fn hash(&self, left: &FieldElement, right: &FieldElement) -> Result; -} - -impl PathHasher for Barretenberg { - fn hash(&self, left: &FieldElement, right: &FieldElement) -> Result { - self.compress_native(left, right) - } - - fn new() -> Self { - Barretenberg::new() - } -} - -// Hashes the message into a leaf -pub(crate) trait MessageHasher { - fn new() -> Self; - fn hash(&mut self, msg: &[u8]) -> FieldElement; -} - -impl MessageHasher for blake2::Blake2s256 { - fn new() -> Self { - use blake2::Digest; - ::new() - } - - fn hash(&mut self, msg: &[u8]) -> FieldElement { - use blake2::Digest; - - self.update(msg); - - let res = self.clone().finalize(); - self.reset(); - FieldElement::from_be_bytes_reduce(&res[..]) - } -} - -// This impl should be redone in a more efficient and readable way. -// We should have a separate impl for SparseMerkle and regular merkle -// With Regular merkle we need to ensure that updates are done sequentially -// -// With sparse merkle, one can update at any index - -type HashPath = Vec<(FieldElement, FieldElement)>; - -#[allow(dead_code)] -fn flatten_path(path: Vec<(FieldElement, FieldElement)>) -> Vec { - path.into_iter() - .flat_map(|(left, right)| std::iter::once(left).chain(std::iter::once(right))) - .collect() -} - -pub(crate) struct MerkleTree { - depth: u32, - total_size: u32, - db: BTreeMap<&'static [u8], Vec>, - preimages_tree: BTreeMap<[u8; 16], Vec>, - hashes_tree: BTreeMap<[u8; 16], Vec>, - barretenberg: PH, - msg_hasher: MH, -} - -impl MerkleTree { - pub(crate) fn new(depth: u32) -> Self { - let barretenberg = PH::new(); - let mut msg_hasher = MH::new(); - - assert!((1..=20).contains(&depth)); // Why can depth != 0 and depth not more than 20? - - let db = BTreeMap::new(); - let preimages_tree = BTreeMap::new(); - let hashes_tree = BTreeMap::new(); - - let total_size = 1u32 << depth; - - let mut hashes: Vec<_> = (0..total_size * 2 - 2) - .map(|_| FieldElement::zero()) - .collect(); - - let zero_message = [0u8; 64]; - let pre_images = (0..total_size).map(|_| zero_message.to_vec()); - - let mut current = msg_hasher.hash(&zero_message); - - let mut offset = 0usize; - let mut layer_size = total_size as usize; // XXX: On 32 bit architectures, this `as` cast may silently truncate, when total_size > 2^32? - while offset < hashes.len() { - for i in 0..layer_size { - hashes[offset + i] = current; - } - current = barretenberg.hash(¤t, ¤t).unwrap(); - - offset += layer_size; - layer_size /= 2; - } - - let mut merkle_tree = MerkleTree { - depth, - total_size, - barretenberg, - db, - preimages_tree, - hashes_tree, - msg_hasher, - }; - - let root = current; - merkle_tree.insert_root(root); - - for (index, hash) in hashes.into_iter().enumerate() { - merkle_tree.insert_hash(index as u32, hash) - } - - for (index, image) in pre_images.into_iter().enumerate() { - merkle_tree.insert_preimage(index as u32, image) - } - - merkle_tree.insert_depth(depth); - merkle_tree.insert_empty_index(0); - - merkle_tree - } - - fn insert_root(&mut self, value: FieldElement) { - self.db.insert("ROOT".as_bytes(), value.to_be_bytes()); - } - - fn fetch_root(&self) -> FieldElement { - let value = self - .db - .get("ROOT".as_bytes()) - .expect("merkle root should always be present"); - FieldElement::from_be_bytes_reduce(value) - } - - fn insert_depth(&mut self, value: u32) { - self.db - .insert("DEPTH".as_bytes(), value.to_be_bytes().into()); - } - - fn fetch_depth(&self) -> u32 { - let value = self - .db - .get("DEPTH".as_bytes()) - .expect("depth should always be present"); - u32::from_be_bytes(value.to_vec().try_into().unwrap()) - } - - fn insert_empty_index(&mut self, index: u32) { - // First fetch the depth to see that this is less than - let depth = self.fetch_depth(); - let total_size = 1 << depth; - if index > total_size { - panic!("trying to insert at index {index}, but total width is {total_size}") - } - self.db - .insert("EMPTY".as_bytes(), index.to_be_bytes().into()); - } - - fn fetch_empty_index(&self) -> u32 { - let value = self - .db - .get("EMPTY".as_bytes()) - .expect("empty index should always be present"); - u32::from_be_bytes(value.to_vec().try_into().unwrap()) - } - - fn insert_preimage(&mut self, index: u32, value: Vec) { - let index = index as u128; - self.preimages_tree.insert(index.to_be_bytes(), value); - } - - #[allow(dead_code)] - fn fetch_preimage(&self, index: usize) -> Vec { - let index = index as u128; - self.preimages_tree - .get(&index.to_be_bytes()) - .unwrap() - .to_vec() - } - - fn fetch_hash(&self, index: usize) -> FieldElement { - let index = index as u128; - - let i_vec = self.hashes_tree.get(&index.to_be_bytes()).unwrap(); - FieldElement::from_be_bytes_reduce(i_vec) - } - - fn insert_hash(&mut self, index: u32, hash: FieldElement) { - let index = index as u128; - - self.hashes_tree - .insert(index.to_be_bytes(), hash.to_be_bytes()); - } - - fn find_hash_from_value(&self, leaf_value: &FieldElement) -> Option { - for index_db_lef_hash in self.hashes_tree.iter() { - let (key, db_leaf_hash) = index_db_lef_hash; - let index = u128::from_be_bytes(key.to_vec().try_into().unwrap()); - - if db_leaf_hash.to_vec() == leaf_value.to_be_bytes() { - return Some(index); - } - } - None - } - - pub(crate) fn get_hash_path(&self, mut index: usize) -> HashPath { - let mut path = HashPath::with_capacity(self.depth as usize); - - let mut offset = 0usize; - let mut layer_size = self.total_size; - for _ in 0..self.depth { - index &= (!0) - 1; - path.push(( - self.fetch_hash(offset + index), - self.fetch_hash(offset + index + 1), - )); - offset += layer_size as usize; - layer_size /= 2; - index /= 2; - } - path - } - /// Updates the message at index and computes the new tree root - pub(crate) fn update_message( - &mut self, - index: usize, - new_message: &[u8], - ) -> Result { - let current = self.msg_hasher.hash(new_message); - self.insert_preimage(index as u32, new_message.to_vec()); - self.update_leaf(index, current) - } - - fn check_if_index_valid_and_increment(&mut self, mut index: usize) { - // Fetch the empty index - let empty_index = self.fetch_empty_index() as usize; - if empty_index == index { - // increment the empty index - index += 1; - self.insert_empty_index(index as u32); - } else { - panic!("this is an regular append-only merkle tree. Tried to insert at {index}, but next empty is at {empty_index}"); - } - } - - #[allow(dead_code)] - pub(crate) fn find_index_from_leaf(&self, leaf_value: &FieldElement) -> Option { - let index = self.find_hash_from_value(leaf_value); - index.map(|val| val as usize) - } - - #[allow(dead_code)] - // TODO: this gets updated to be -1 on the latest barretenberg branch - pub(crate) fn find_index_for_empty_leaf(&self) -> usize { - let index = self.fetch_empty_index(); - index as usize - } - - /// Update the element at index and compute the new tree root - pub(crate) fn update_leaf( - &mut self, - mut index: usize, - mut current: FieldElement, - ) -> Result { - // Note that this method does not update the list of messages [preimages]| - // use `update_message` to do this - self.check_if_index_valid_and_increment(index); - - let mut offset = 0usize; - let mut layer_size = self.total_size; - for _ in 0..self.depth { - self.insert_hash((offset + index) as u32, current); - - index &= (!0) - 1; - current = self.barretenberg.hash( - &self.fetch_hash(offset + index), - &self.fetch_hash(offset + index + 1), - )?; - - offset += layer_size as usize; - layer_size /= 2; - index /= 2; - } - - self.insert_root(current); - Ok(current) - } - - #[allow(dead_code)] - /// Gets a message at `index`. This is not the leaf - pub(crate) fn get_message_at_index(&self, index: usize) -> Vec { - self.fetch_preimage(index) - } - - pub(crate) fn root(&self) -> FieldElement { - self.fetch_root() - } - - #[allow(dead_code)] - pub(crate) fn depth(&self) -> u32 { - self.depth - } -} - -#[test] -fn basic_interop_initial_root() { - // Test that the initial root is computed correctly - let tree: MerkleTree = MerkleTree::new(3); - // Copied from barretenberg by copying the stdout from MemoryTree - let expected_hex = "04ccfbbb859b8605546e03dcaf41393476642859ff7f99446c054b841f0e05c8"; - assert_eq!(tree.root().to_hex(), expected_hex) -} - -#[test] -fn basic_interop_hashpath() { - // Test that the hashpath is correct - let tree: MerkleTree = MerkleTree::new(3); - - let path = tree.get_hash_path(0); - - let expected_hash_path = vec![ - ( - "1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", - "1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", - ), - ( - "0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", - "0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", - ), - ( - "22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", - "22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", - ), - ]; - - for (got, expected_segment) in path.into_iter().zip(expected_hash_path) { - assert_eq!(got.0.to_hex().as_str(), expected_segment.0); - assert_eq!(got.1.to_hex().as_str(), expected_segment.1) - } -} - -#[test] -fn basic_interop_update() -> Result<(), Error> { - // Test that computing the HashPath is correct - let mut tree: MerkleTree = MerkleTree::new(3); - - tree.update_message(0, &[0; 64])?; - tree.update_message(1, &[1; 64])?; - tree.update_message(2, &[2; 64])?; - tree.update_message(3, &[3; 64])?; - tree.update_message(4, &[4; 64])?; - tree.update_message(5, &[5; 64])?; - tree.update_message(6, &[6; 64])?; - let root = tree.update_message(7, &[7; 64])?; - - assert_eq!( - "0ef8e14db4762ebddadb23b2225f93ca200a4c9bd37130b4d028c971bbad16b5", - root.to_hex() - ); - - let path = tree.get_hash_path(2); - - let expected_hash_path = vec![ - ( - "06c2335d6f7acb84bbc7d0892cefebb7ca31169a89024f24814d5785e0d05324", - "12dc36b01cbd8a6248b04e08f0ec91aa6d11a91f030b4a7b1460281859942185", - ), - ( - "1f399ea0d6aaf602c7cbcb6ae8cda0e6b6487836c017163888ed4fd38b548389", - "220dd1b310caa4a6af755b4c893d956c48f31642b487164b258f2973aac2c28f", - ), - ( - "25cbb3084647221ffcb535945bb65bd70e0809834dc7a6d865a3f2bb046cdc29", - "2cc463fc8c9a4eda416f3e490876672f644708dd0330a915f6835d8396fa8f20", - ), - ]; - - for (got, expected_segment) in path.into_iter().zip(expected_hash_path) { - assert_eq!(got.0.to_hex().as_str(), expected_segment.0); - assert_eq!(got.1.to_hex().as_str(), expected_segment.1) - } - - Ok(()) -}