diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e054be87..e232165b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,11 +11,8 @@ jobs: uses: actions/checkout@v2 - name: Set up Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@1.66.0 with: - profile: minimal - toolchain: 1.67.0 - override: true target: x86_64-unknown-linux-gnu - name: Run tests @@ -36,11 +33,8 @@ jobs: uses: actions/checkout@v2 - name: Set up Rust - uses: actions-rs/toolchain@v1 + uses: dtolnay/rust-toolchain@1.66.0 with: - profile: minimal - toolchain: 1.67.0 - override: true target: ${{ matrix.target }} - name: Run tests diff --git a/Cargo.lock b/Cargo.lock index 81e39a88..9c1c8a90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "acir" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86577747c44f23e2e8e6d972287d01341c0eea42a78ce15c5efd212a39d0fc27" +checksum = "2dab15381940b2eb3ecca70e78adcfb547df5b49123b2872c2333b0ee965beae" dependencies = [ "acir_field", "bincode", @@ -18,9 +18,9 @@ dependencies = [ [[package]] name = "acir_field" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4239156a8eddd55b2ae8bd25aa169d012bae70e0fd7c635f08f68ada54a8cb6c" +checksum = "16ae84a2d0f6e5087a499c56d41ca8227fd26abea01b527694c601b230b27111" dependencies = [ "ark-bn254", "ark-ff", @@ -32,14 +32,13 @@ dependencies = [ [[package]] name = "acvm" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74351bab6e0fd2ec1bd631abc73260f374cc28d2baf85c0e11300c0c989d5e53" +checksum = "1cc5bd62182c728ec5d3a080319765b0c86f294fc74f90fd6657008ae789d797" dependencies = [ "acir", "acvm_blackbox_solver", "acvm_stdlib", - "async-trait", "brillig_vm", "indexmap", "num-bigint", @@ -57,36 +56,41 @@ dependencies = [ "const_format", "dirs 5.0.1", "flate2", - "getrandom", - "pkg-config", "reqwest", - "rust-embed", "tar", "tempfile", "thiserror", - "wasmer", ] [[package]] name = "acvm_blackbox_solver" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a362499180c6498acc0ebf77bd919be8ccd9adabc84a695d4af44ca180ba0709" +checksum = "b79e031466b5075260257a61359eb9775c5ef92dce27621af1c36bfd0a6eb511" dependencies = [ "acir", "blake2", + "flate2", + "getrandom", + "js-sys", "k256", "p256", + "pkg-config", + "reqwest", + "rust-embed", "sha2", "sha3", + "tar", "thiserror", + "wasm-bindgen-futures", + "wasmer", ] [[package]] name = "acvm_stdlib" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e485b3bc3331eaa10bc92fb092ca14275936c8935c3ae7ec89fb0bd48246ab42" +checksum = "1389b884c07b5eb0c15e5f5395317de060bcf1a6a5f476893c4d65f50de5de1f" dependencies = [ "acir", ] @@ -130,18 +134,9 @@ dependencies = [ [[package]] name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - -[[package]] -name = "aho-corasick" -version = "1.0.2" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43f6cb1bf222025340178f382c426f13757b2960e89779dfcb319c32542a5a41" +checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" dependencies = [ "memchr", ] @@ -269,17 +264,6 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" -[[package]] -name = "async-trait" -version = "0.1.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a564d521dd56509c4c47480d00b80ee55f7e385ae48db5744c67ad50c92d2ebf" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.23", -] - [[package]] name = "autocfg" version = "1.1.0" @@ -372,9 +356,9 @@ dependencies = [ [[package]] name = "brillig" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d64df3df7d2d96fc2519e4dd64bc6bc23eee2949ee86725d9041ef7703c283ab" +checksum = "dd5fe44d05264a0d9a6d5c5dca8a6c976f3fcdfdfb0244d75e458bf13fcb8f21" dependencies = [ "acir_field", "serde", @@ -382,9 +366,9 @@ dependencies = [ [[package]] name = "brillig_vm" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b306b3d79b6da192fd2ed68b94ab07712496f39bb5d50fedce44dac3f4953065" +checksum = "733be2c7fea117e0b0be985b9b99c15aeca01fb342528fac9c3f70a7f844ec50" dependencies = [ "acir", "acvm_blackbox_solver", @@ -488,9 +472,9 @@ dependencies = [ [[package]] name = "corosensei" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9847f90f32a50b0dcbd68bc23ff242798b13080b97b0569f6ed96a45ce4cf2cd" +checksum = "80128832c58ea9cbd041d2a759ec449224487b2c1e400453d99d244eead87a8e" dependencies = [ "autocfg", "cfg-if", @@ -667,9 +651,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0558d22a7b463ed0241e993f76f09f30b126687447751a8638587b864e4b3944" +checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" dependencies = [ "darling_core", "darling_macro", @@ -677,9 +661,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfa2e259f8ee1ce5e97824a3c55ec4404a0d772ca7fa96bf19f0752a046eb" +checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" dependencies = [ "fnv", "ident_case", @@ -690,9 +674,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29a358ff9f12ec09c3e61fef9b5a9902623a695a46a917b07f269bff1445611a" +checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core", "quote", @@ -701,9 +685,9 @@ dependencies = [ [[package]] name = "dashmap" -version = "5.5.0" +version = "5.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6943ae99c34386c84a470c499d3414f66502a41340aa895406e0d2e4a207b91d" +checksum = "9b101bb8960ab42ada6ae98eb82afcea4452294294c45b681295af26610d6d28" dependencies = [ "cfg-if", "hashbrown 0.14.0", @@ -785,32 +769,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "dynasm" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "add9a102807b524ec050363f09e06f1504214b0e1c7797f64261c891022dce8b" -dependencies = [ - "bitflags 1.3.2", - "byteorder", - "lazy_static", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "dynasmrt" -version = "1.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64fba5a42bd76a17cad4bfa00de168ee1cbfa06a5e8ce992ae880218c05641a9" -dependencies = [ - "byteorder", - "dynasm", - "memmap2", -] - [[package]] name = "ecdsa" version = "0.14.8" @@ -1071,8 +1029,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -1094,11 +1054,11 @@ checksum = "b6c80984affa11d98d1b88b66ac8853f143217b399d3c74116778ff8fdb4ed2e" [[package]] name = "globset" -version = "0.4.10" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "029d74589adefde59de1a0c4f4732695c32805624aec7b68d91503d4dba79afc" +checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" dependencies = [ - "aho-corasick 0.7.20", + "aho-corasick", "bstr", "fnv", "log", @@ -1736,11 +1696,11 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.0" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89089e897c013b3deb627116ae56a6955a72b8bed395c9526af31c9fe528b484" +checksum = "12de2eff854e5fa4b1295edd650e227e9d8fb0c9e90b12e7f36d6a6811791a29" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick", "memchr", "regex-automata", "regex-syntax", @@ -1748,20 +1708,20 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.0" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa250384981ea14565685dea16a9ccc4d1c541a13f82b9c168572264d1df8c56" +checksum = "49530408a136e16e5b486e883fbb6ba058e8e4e8ae6621a77b048b314336e629" dependencies = [ - "aho-corasick 1.0.2", + "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" -version = "0.7.3" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ab07dc67230e4a4718e70fd5c20055a4334b121f1f9db8fe63ef39ce9b8c846" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "region" @@ -1990,9 +1950,9 @@ dependencies = [ [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "sct" @@ -2240,9 +2200,9 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.12.8" +version = "0.12.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b1c7f239eb94671427157bd93b3694320f3668d4e1eff08c7285366fd777fac" +checksum = "9d0e916b1148c8e263850e1ebcbd046f333e0683c724876bb0da63ea4373dc8a" [[package]] name = "tempfile" @@ -2434,9 +2394,9 @@ dependencies = [ [[package]] name = "uuid" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d023da39d1fde5a8a3fe1f3e01ca9632ada0a63e9797de55a879d6e2236277be" +checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" [[package]] name = "version_check" @@ -2560,18 +2520,18 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "wasm-encoder" -version = "0.29.0" +version = "0.32.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18c41dbd92eaebf3612a39be316540b8377c871cb9bde6b064af962984912881" +checksum = "1ba64e81215916eaeb48fee292f29401d69235d62d8b8fd92a7b2844ec5ae5f7" dependencies = [ "leb128", ] [[package]] name = "wasmer" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7142dbb91ede83cc0aef2301fa75fcc7e0c9e5a7d5358e3c4f3a7249fe9ce8" +checksum = "78caedecd8cb71ed47ccca03b68d69414a3d278bb031e6f93f15759344efdd52" dependencies = [ "bytes", "cfg-if", @@ -2588,19 +2548,20 @@ dependencies = [ "wasm-bindgen-downcast", "wasmer-compiler", "wasmer-compiler-cranelift", - "wasmer-compiler-singlepass", "wasmer-derive", "wasmer-types", "wasmer-vm", + "wasmparser 0.83.0", + "wasmparser 0.95.0", "wat", "winapi", ] [[package]] name = "wasmer-compiler" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5b99c70711ec7631b602a9fc95577c40df21e8f3916159c9d80c3fb4f77abdc" +checksum = "726a8450541af4a57c34af7b6973fdbfc79f896cc7e733429577dfd1d1687180" dependencies = [ "backtrace", "cfg-if", @@ -2615,15 +2576,15 @@ dependencies = [ "thiserror", "wasmer-types", "wasmer-vm", - "wasmparser", + "wasmparser 0.95.0", "winapi", ] [[package]] name = "wasmer-compiler-cranelift" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52aef2ef35513a04fed54de9a7dc9c469d4742a5c2e378a5f7e2a79b1327e3bd" +checksum = "a1e5633f90f372563ebbdf3f9799c7b29ba11c90e56cf9b54017112d2e656c95" dependencies = [ "cranelift-codegen", "cranelift-entity", @@ -2638,30 +2599,11 @@ dependencies = [ "wasmer-types", ] -[[package]] -name = "wasmer-compiler-singlepass" -version = "4.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebfd019aa98b19fea0fb1d8db9b539145c9416d183ce4cda4e8e024b2c890aac" -dependencies = [ - "byteorder", - "dynasm", - "dynasmrt", - "enumset", - "gimli 0.26.2", - "lazy_static", - "more-asserts", - "rayon", - "smallvec", - "wasmer-compiler", - "wasmer-types", -] - [[package]] name = "wasmer-derive" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25bb1425c9e4dc3e2d3aacd6e82e22e27a3127379e0d09bcbdf25ff376229162" +checksum = "97901fdbaae383dbb90ea162cc3a76a9fa58ac39aec7948b4c0b9bbef9307738" dependencies = [ "proc-macro-error", "proc-macro2", @@ -2671,9 +2613,9 @@ dependencies = [ [[package]] name = "wasmer-types" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7e32ed799fa8c0d96ca9615d9ea8006857a0f0c18e7c2ed8082bd5c63a9ea70" +checksum = "67f1f2839f4f61509550e4ddcd0e658e19f3af862b51c79fda15549d735d659b" dependencies = [ "bytecheck", "enum-iterator", @@ -2687,9 +2629,9 @@ dependencies = [ [[package]] name = "wasmer-vm" -version = "4.1.2" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0847513cb176b5d62a6f65d6ae474594935e726a10e9e3387177d9cbf8b8cda0" +checksum = "043118ec4f16d1714fed3aab758b502b864bd865e1d5188626c9ad290100563f" dependencies = [ "backtrace", "cc", @@ -2712,6 +2654,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "wasmparser" +version = "0.83.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718ed7c55c2add6548cca3ddd6383d738cd73b892df400e96b9aa876f0141d7a" + [[package]] name = "wasmparser" version = "0.95.0" @@ -2724,9 +2672,9 @@ dependencies = [ [[package]] name = "wast" -version = "60.0.0" +version = "64.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd06cc744b536e30387e72a48fdd492105b9c938bb4f415c39c616a7a0a697ad" +checksum = "a259b226fd6910225aa7baeba82f9d9933b6d00f2ce1b49b80fa4214328237cc" dependencies = [ "leb128", "memchr", @@ -2736,9 +2684,9 @@ dependencies = [ [[package]] name = "wat" -version = "1.0.66" +version = "1.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5abe520f0ab205366e9ac7d3e6b2fc71de44e32a2b58f2ec871b6b575bdcea3b" +checksum = "53253d920ab413fca1c7dc2161d601c79b4fdf631d0ba51dd4343bf9b556c3f6" dependencies = [ "wast", ] diff --git a/Cargo.toml b/Cargo.toml index 411e9cc6..0821641e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT OR Apache-2.0" crate-type = ["cdylib", "lib"] [dependencies] -acvm = { version = "0.22.0", features = ["bn254"] } +acvm = { version = "0.23.0", features = ["bn254"] } thiserror = "1.0.21" base64 = "0.21.2" @@ -29,16 +29,5 @@ reqwest = { version = "0.11.16", default-features = false, features = [ "blocking", ] } - -# Wasm -getrandom = { version = "0.2" } -rust-embed = { version = "6.6.0", features = [ - "debug-embed", - "interpolate-folder-path", - "include-exclude", -] } -wasmer = { version = "4.1.2", features = ["wasmer-compiler-singlepass"] } - [build-dependencies] build-target = "0.4.0" -pkg-config = "0.3" diff --git a/flake.lock b/flake.lock index 659daec5..23a701d4 100644 --- a/flake.lock +++ b/flake.lock @@ -10,15 +10,16 @@ ] }, "locked": { - "lastModified": 1688820427, - "narHash": "sha256-w7yMeYp50KrlTn23TTKfYmLOQL4uIgw0wSX67v2tvvc=", + "lastModified": 1693320399, + "narHash": "sha256-P6xsi4OzfVBWQF/gHt4u2dMyKOPbCMoJ7n9URzMvkik=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "fdd46f77531a6fcc9d9b24a698c56590d54d487e", + "rev": "051449fe181bf53e2cfebc15c6b9bd01cc44fc50", "type": "github" }, "original": { "owner": "AztecProtocol", + "ref": "barretenberg-v0.4.6", "repo": "barretenberg", "type": "github" } diff --git a/flake.nix b/flake.nix index e7d8dc3a..597345b3 100644 --- a/flake.nix +++ b/flake.nix @@ -38,7 +38,7 @@ }; barretenberg = { - url = "github:AztecProtocol/barretenberg"; + url = "github:AztecProtocol/barretenberg?ref=barretenberg-v0.4.6"; # All of these inputs (a.k.a. dependencies) need to align with inputs we # use so they use the `inputs.*.follows` syntax to reference our inputs inputs = { @@ -60,7 +60,7 @@ ]; }; - rustToolchain = pkgs.rust-bin.stable."1.67.0".default.override { + rustToolchain = pkgs.rust-bin.stable."1.66.0".default.override { # We include rust-src to ensure rust-analyzer works. # See https://discourse.nixos.org/t/rust-src-not-found-and-other-misadventures-of-developing-rust-on-nixos/11570/4 extensions = [ "rust-src" ]; @@ -70,17 +70,6 @@ sharedEnvironment = { }; - nativeEnvironment = sharedEnvironment // { - # rust-bindgen needs to know the location of libclang - LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; - }; - - wasmEnvironment = sharedEnvironment // { - # We set the environment variable because barretenberg must be compiled in a special way for wasm - BARRETENBERG_BIN_DIR = "${pkgs.barretenberg-wasm}/bin"; - }; - - wasmFilter = path: _type: builtins.match ".*wasm$" path != null; # We use `include_str!` macro to embed the solidity verifier template so we need to create a special # source filter to include .sol files in addition to usual rust/cargo source files. solidityFilter = path: _type: builtins.match ".*sol$" path != null; @@ -88,7 +77,7 @@ bytecodeFilter = path: _type: builtins.match ".*bytecode$" path != null; witnessFilter = path: _type: builtins.match ".*tr$" path != null; sourceFilter = path: type: - (solidityFilter path type) || (bytecodeFilter path type)|| (wasmFilter path type) || (witnessFilter path type) || (craneLib.filterCargoSources path type); + (solidityFilter path type) || (bytecodeFilter path type)|| (witnessFilter path type) || (craneLib.filterCargoSources path type); # As per https://discourse.nixos.org/t/gcc11stdenv-and-clang/17734/7 since it seems that aarch64-linux uses # gcc9 instead of gcc11 for the C++ stdlib, while all other targets we support provide the correct libstdc++ @@ -127,47 +116,33 @@ }; # Combine the environment and other configuration needed for crane to build with the native feature - nativeArgs = nativeEnvironment // commonArgs // { + nativeArgs = sharedEnvironment // commonArgs // { # Use our custom stdenv to build and test our Rust project inherit stdenv; - nativeBuildInputs = [ - # This provides the pkg-config tool to find barretenberg & other native libraries - pkgs.pkg-config - # This provides the `lld` linker to cargo - pkgs.llvmPackages.bintools - ] ++ pkgs.lib.optionals stdenv.isLinux [ + nativeBuildInputs = pkgs.lib.optionals stdenv.isLinux [ # This is linux specific and used to patch the rpath and interpreter of the bb binary pkgs.patchelf ]; buildInputs = [ pkgs.llvmPackages.openmp - pkgs.barretenberg ] ++ extraBuildInputs; }; - - # Combine the environment and other configuration needed for crane to build with the wasm feature - wasmArgs = wasmEnvironment // commonArgs // { - # We disable the default "native" feature and enable the "wasm" feature - cargoExtraArgs = "--no-default-features --features='wasm'"; - - buildInputs = [ ] ++ extraBuildInputs; - }; # Conditionally download the binary based on whether it is linux or mac bb_binary = let platformSpecificUrl = if stdenv.hostPlatform.isLinux then - "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.3/bb-ubuntu.tar.gz" + "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.6/barretenberg-x86_64-linux-gnu.tar.gz" else if stdenv.hostPlatform.isDarwin then - "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.3/barretenberg-x86_64-apple-darwin.tar.gz" + "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.6/barretenberg-x86_64-apple-darwin.tar.gz" else throw "Unsupported platform"; platformSpecificHash = if stdenv.hostPlatform.isLinux then - "sha256:0rcsjws87f4v28cw9734c10pg7c49apigf4lg3m0ji5vbhhmfnhr" + "sha256:1p15v6rvf9195047pfbw8dns3z6s54q4c9hgbxan72lhjjgjz800" else if stdenv.hostPlatform.isDarwin then - "sha256:0pnsd56z0vkai7m0advawfgcvq9jbnpqm7lk98n5flqj583x3w35" + "sha256:03d9rsmhvzaz7ni8ac9hf3adc2dw57nv6q406v4l49g1rd354dm0" else throw "Unsupported platform"; in builtins.fetchurl { @@ -188,17 +163,14 @@ echo "Extracting bb binary" mkdir extracted tar -xf ${bb_binary} -C extracted + cp extracted/bb ./backend_binary # Conditionally patch the binary for Linux ${if stdenv.hostPlatform.isLinux then '' - - cp extracted/cpp/build/bin/bb ./backend_binary - echo "Patching bb binary for Linux" patchelf --set-rpath "${stdenv.cc.cc.lib}/lib:${pkgs.gcc.cc.lib}/lib" ./backend_binary patchelf --set-interpreter ${stdenv.cc.libc}/lib/ld-linux-x86-64.so.2 ./backend_binary '' else if stdenv.hostPlatform.isDarwin then '' - cp extracted/bb ./backend_binary '' else throw "Unsupported platform" } @@ -214,17 +186,12 @@ ''; }; - # Build *just* the cargo dependencies, so we can reuse all of that work between runs native-cargo-artifacts = craneLib.buildDepsOnly nativeArgs; - wasm-cargo-artifacts = craneLib.buildDepsOnly wasmArgs; - + acvm-backend-barretenberg-native = craneLib.buildPackage (nativeArgs // { cargoArtifacts = native-cargo-artifacts; }); - acvm-backend-barretenberg-wasm = craneLib.buildPackage (wasmArgs // { - cargoArtifacts = native-cargo-artifacts; - }); in rec { checks = { @@ -246,42 +213,21 @@ # It's unclear why doCheck needs to be enabled for tests to run but not clippy doCheck = true; }); - - cargo-clippy-wasm = craneLib.cargoClippy (wasmArgs // { - # Crane appends "clippy" - pname = "wasm"; - - cargoArtifacts = wasm-cargo-artifacts; - - cargoClippyExtraArgs = "--all-targets -- -D warnings"; - }); - - cargo-test-wasm = craneLib.cargoTest (wasmArgs // (networkTestArgs 8001) // { - # Crane appends "test" - pname = "wasm"; - - cargoArtifacts = wasm-cargo-artifacts; - - # It's unclear why doCheck needs to be enabled for tests to run but not clippy - doCheck = true; - }); }; packages = { inherit acvm-backend-barretenberg-native; - inherit acvm-backend-barretenberg-wasm; default = acvm-backend-barretenberg-native; # We expose the `*-cargo-artifacts` derivations so we can cache our cargo dependencies in CI inherit native-cargo-artifacts; - inherit wasm-cargo-artifacts; }; # Setup the environment to match the stdenv from `nix build` & `nix flake check`, and # combine it with the environment settings, the inputs from our checks derivations, # and extra tooling via `nativeBuildInputs` - devShells.default = pkgs.mkShell.override { inherit stdenv; } (nativeEnvironment // wasmEnvironment // { + devShells.default = pkgs.mkShell.override { inherit stdenv; } (sharedEnvironment // { inputsFrom = builtins.attrValues checks; nativeBuildInputs = with pkgs; [ diff --git a/src/barretenberg/barretenberg.wasm b/src/barretenberg/barretenberg.wasm deleted file mode 100755 index 3670e0e6..00000000 Binary files a/src/barretenberg/barretenberg.wasm and /dev/null differ diff --git a/src/barretenberg/barretenberg_structures.rs b/src/barretenberg/barretenberg_structures.rs deleted file mode 100644 index 9a937a36..00000000 --- a/src/barretenberg/barretenberg_structures.rs +++ /dev/null @@ -1,42 +0,0 @@ -use acvm::FieldElement; - -#[derive(Debug, Default, Clone)] -pub(crate) struct Assignments(Vec<FieldElement>); - -// This is a separate impl so the constructor can get the wasm_bindgen macro in the future -impl Assignments { - #[allow(dead_code)] - pub(crate) fn new() -> Assignments { - Assignments::default() - } -} - -impl Assignments { - pub(crate) fn to_bytes(&self) -> Vec<u8> { - let mut buffer = Vec::new(); - - let witness_len = self.0.len() as u32; - buffer.extend_from_slice(&witness_len.to_be_bytes()); - - for assignment in self.0.iter() { - buffer.extend_from_slice(&assignment.to_be_bytes()); - } - - buffer - } -} - -impl IntoIterator for Assignments { - type Item = FieldElement; - type IntoIter = std::vec::IntoIter<Self::Item>; - - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } -} - -impl From<Vec<FieldElement>> for Assignments { - fn from(w: Vec<FieldElement>) -> Assignments { - Assignments(w) - } -} diff --git a/src/barretenberg/mod.rs b/src/barretenberg/mod.rs deleted file mode 100644 index 269cebee..00000000 --- a/src/barretenberg/mod.rs +++ /dev/null @@ -1,349 +0,0 @@ -// `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. -#[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 barretenberg_structures; -pub(crate) mod pedersen; -pub(crate) mod scalar_mul; -pub(crate) mod schnorr; - -use thiserror::Error; - -#[derive(Debug, Error)] -pub(super) enum FeatureError { - #[error("Trying to call {name} resulted in an error")] - FunctionCallFailed { - name: String, - source: wasmer::RuntimeError, - }, - #[error("Could not find function export named {name}")] - InvalidExport { - name: String, - source: wasmer::ExportError, - }, - #[error("No value available when value was expected")] - NoValue, - #[error("Value expected to be i32")] - InvalidI32, - #[error("Could not convert value {value} from i32 to u32")] - InvalidU32 { - value: i32, - source: std::num::TryFromIntError, - }, - #[error("Could not convert value {value} from i32 to usize")] - InvalidUsize { - value: i32, - source: std::num::TryFromIntError, - }, - #[error("Value expected to be 0 or 1 representing a boolean")] - InvalidBool, -} - -#[allow(clippy::upper_case_acronyms)] -#[derive(Debug, Error)] -pub(super) enum Error { - #[error(transparent)] - FromFeature(#[from] FeatureError), -} - -/// The number of bytes necessary to store a `FieldElement`. -pub(crate) const FIELD_BYTES: usize = 32; - -#[derive(Debug)] -pub struct Barretenberg { - store: std::cell::RefCell<wasmer::Store>, - - memory: wasmer::Memory, - - instance: wasmer::Instance, -} - -impl Default for Barretenberg { - fn default() -> Barretenberg { - Barretenberg::new() - } -} - -#[test] -fn smoke() -> Result<(), Error> { - use pedersen::Pedersen; - - let b = Barretenberg::new(); - let (x, y) = b.encrypt( - vec![acvm::FieldElement::zero(), acvm::FieldElement::one()], - 0, - )?; - dbg!(x.to_hex(), y.to_hex()); - Ok(()) -} - -mod wasm { - use std::cell::RefCell; - - use wasmer::{ - imports, Function, FunctionEnv, FunctionEnvMut, Instance, Memory, MemoryType, Module, - Store, Value, WasmPtr, - }; - - use super::{Barretenberg, Error, FeatureError}; - - /// The Barretenberg WASM gives us 1024 bytes of scratch space which we can use without - /// needing to allocate/free it ourselves. This can be useful for when we need to pass in several small variables - /// when calling functions on the wasm, however it's important to not overrun this scratch space as otherwise - /// the written data will begin to corrupt the stack. - /// - /// Using this scratch space isn't particularly safe if we have multiple threads interacting with the wasm however, - /// each thread could write to the same pointer address simultaneously. - pub(super) const WASM_SCRATCH_BYTES: usize = 1024; - - /// Embed the Barretenberg WASM file - #[derive(rust_embed::RustEmbed)] - #[folder = "src/barretenberg"] - #[include = "barretenberg.wasm"] - struct Wasm; - - impl Barretenberg { - pub(crate) fn new() -> Barretenberg { - let (instance, memory, store) = instance_load(); - Barretenberg { - memory, - instance, - store: RefCell::new(store), - } - } - } - - /// A wrapper around the arguments or return value from a WASM call. - /// Notice, `Option<Value>` is used because not every call returns a value, - /// some calls are simply made to free a pointer or manipulate the heap. - #[derive(Debug, Clone)] - pub(crate) struct WASMValue(Option<Value>); - - impl From<usize> for WASMValue { - fn from(value: usize) -> Self { - WASMValue(Some(Value::I32(value as i32))) - } - } - - impl From<u32> for WASMValue { - fn from(value: u32) -> Self { - WASMValue(Some(Value::I32(value as i32))) - } - } - - impl From<i32> for WASMValue { - fn from(value: i32) -> Self { - WASMValue(Some(Value::I32(value))) - } - } - - impl From<Value> for WASMValue { - fn from(value: Value) -> Self { - WASMValue(Some(value)) - } - } - - impl TryFrom<WASMValue> for bool { - type Error = FeatureError; - - fn try_from(value: WASMValue) -> Result<Self, Self::Error> { - match value.try_into()? { - 0 => Ok(false), - 1 => Ok(true), - _ => Err(FeatureError::InvalidBool), - } - } - } - - impl TryFrom<WASMValue> for usize { - type Error = FeatureError; - - fn try_from(value: WASMValue) -> Result<Self, Self::Error> { - let value: i32 = value.try_into()?; - value - .try_into() - .map_err(|source| FeatureError::InvalidUsize { value, source }) - } - } - - impl TryFrom<WASMValue> for u32 { - type Error = FeatureError; - - fn try_from(value: WASMValue) -> Result<Self, Self::Error> { - let value = value.try_into()?; - u32::try_from(value).map_err(|source| FeatureError::InvalidU32 { value, source }) - } - } - - impl TryFrom<WASMValue> for i32 { - type Error = FeatureError; - - fn try_from(value: WASMValue) -> Result<Self, Self::Error> { - value.0.map_or(Err(FeatureError::NoValue), |val| { - val.i32().ok_or(FeatureError::InvalidI32) - }) - } - } - - impl TryFrom<WASMValue> for Value { - type Error = FeatureError; - - fn try_from(value: WASMValue) -> Result<Self, Self::Error> { - value.0.ok_or(FeatureError::NoValue) - } - } - - impl Barretenberg { - /// Transfer bytes to WASM heap - 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); - - memory_view.write(offset as u64, data).unwrap() - } - - // TODO: Consider making this Result-returning - pub(super) fn read_memory<const SIZE: usize>(&self, start: usize) -> [u8; SIZE] { - self.read_memory_variable_length(start, SIZE) - .try_into() - .expect("Read memory should be of the specified length") - } - - // TODO: Consider making this Result-returning - pub(crate) fn read_memory_variable_length(&self, offset: usize, length: usize) -> Vec<u8> { - let memory = &self.memory; - 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 call(&self, name: &str, param: &WASMValue) -> Result<WASMValue, Error> { - self.call_multiple(name, vec![param]) - } - - pub(crate) fn call_multiple( - &self, - name: &str, - params: Vec<&WASMValue>, - ) -> Result<WASMValue, Error> { - // We take in a reference to values, since they do not implement Copy. - // We then clone them inside of this function, so that the API does not have a bunch of Clones everywhere - - let mut args: Vec<Value> = vec![]; - for param in params.into_iter().cloned() { - args.push(param.try_into()?) - } - let func = self.instance.exports.get_function(name).map_err(|source| { - FeatureError::InvalidExport { - 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)) - } - - /// Creates a pointer and allocates the bytes that the pointer references to, to the heap - pub(crate) fn allocate(&self, bytes: &[u8]) -> Result<WASMValue, Error> { - let ptr: i32 = self.call("bbmalloc", &bytes.len().into())?.try_into()?; - - let i32_bytes = ptr.to_be_bytes(); - let u32_bytes = u32::from_be_bytes(i32_bytes); - - self.transfer_to_heap(bytes, u32_bytes as usize); - Ok(ptr.into()) - } - } - - fn instance_load() -> (Instance, Memory, Store) { - let mut store = Store::default(); - - let mem_type = MemoryType::new(18, Some(65536), false); - let memory = Memory::new(&mut store, mem_type).unwrap(); - - let function_env = FunctionEnv::new(&mut store, memory.clone()); - let custom_imports = imports! { - "env" => { - "logstr" => Function::new_typed_with_env( - &mut store, - &function_env, - logstr, - ), - "memory" => memory.clone(), - }, - "wasi_snapshot_preview1" => { - "proc_exit" => Function::new_typed(&mut store, proc_exit), - "random_get" => Function::new_typed_with_env( - &mut store, - &function_env, - random_get - ), - }, - }; - - let module = Module::new(&store, Wasm::get("barretenberg.wasm").unwrap().data).unwrap(); - - ( - Instance::new(&mut store, &module, &custom_imports).unwrap(), - memory, - store, - ) - } - - fn logstr(mut env: FunctionEnvMut<Memory>, ptr: i32) { - let (memory, store) = env.data_and_store_mut(); - let memory_view = memory.view(&store); - - let log_str_wasm_ptr: WasmPtr<u8, wasmer::Memory32> = WasmPtr::new(ptr as u32); - - 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(mut env: FunctionEnvMut<Memory>, 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(()) => { - 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 - } - } - } - Err(_) => { - 29_i32 // __WASI_EIO - } - } - } - - fn proc_exit(_: i32) {} -} diff --git a/src/barretenberg/pedersen.rs b/src/barretenberg/pedersen.rs deleted file mode 100644 index a8389d3b..00000000 --- a/src/barretenberg/pedersen.rs +++ /dev/null @@ -1,144 +0,0 @@ -use super::{Barretenberg, Error}; - -use acvm::FieldElement; - -pub(crate) trait Pedersen { - fn compress_native( - &self, - left: &FieldElement, - right: &FieldElement, - ) -> Result<FieldElement, Error>; - fn compress_many(&self, inputs: Vec<FieldElement>) -> Result<FieldElement, Error>; - fn encrypt( - &self, - inputs: Vec<FieldElement>, - hash_index: u32, - ) -> Result<(FieldElement, FieldElement), Error>; -} - -impl Pedersen for Barretenberg { - fn compress_native( - &self, - left: &FieldElement, - right: &FieldElement, - ) -> Result<FieldElement, Error> { - use super::FIELD_BYTES; - - let lhs_ptr: usize = 0; - let rhs_ptr: usize = lhs_ptr + FIELD_BYTES; - let result_ptr: usize = rhs_ptr + FIELD_BYTES; - - self.transfer_to_heap(&left.to_be_bytes(), lhs_ptr); - self.transfer_to_heap(&right.to_be_bytes(), rhs_ptr); - - self.call_multiple( - "pedersen_plookup_compress_fields", - vec![&lhs_ptr.into(), &rhs_ptr.into(), &result_ptr.into()], - )?; - - let result_bytes: [u8; FIELD_BYTES] = self.read_memory(result_ptr); - Ok(FieldElement::from_be_bytes_reduce(&result_bytes)) - } - - #[allow(dead_code)] - fn compress_many(&self, inputs: Vec<FieldElement>) -> Result<FieldElement, Error> { - use super::barretenberg_structures::Assignments; - use super::FIELD_BYTES; - - let input_buf = Assignments::from(inputs).to_bytes(); - let input_ptr = self.allocate(&input_buf)?; - let result_ptr: usize = 0; - - self.call_multiple( - "pedersen_plookup_compress", - vec![&input_ptr, &result_ptr.into()], - )?; - - let result_bytes: [u8; FIELD_BYTES] = self.read_memory(result_ptr); - Ok(FieldElement::from_be_bytes_reduce(&result_bytes)) - } - - fn encrypt( - &self, - inputs: Vec<FieldElement>, - hash_index: u32, - ) -> Result<(FieldElement, FieldElement), Error> { - use super::barretenberg_structures::Assignments; - use super::FIELD_BYTES; - - let input_buf = Assignments::from(inputs).to_bytes(); - let input_ptr = self.allocate(&input_buf)?; - let result_ptr: usize = 0; - - self.call_multiple( - "pedersen_plookup_commit_with_hash_index", - vec![&input_ptr, &result_ptr.into(), &hash_index.into()], - )?; - - let result_bytes: [u8; 2 * FIELD_BYTES] = self.read_memory(result_ptr); - let (point_x_bytes, point_y_bytes) = result_bytes.split_at(FIELD_BYTES); - - let point_x = FieldElement::from_be_bytes_reduce(point_x_bytes); - let point_y = FieldElement::from_be_bytes_reduce(point_y_bytes); - - Ok((point_x, point_y)) - } -} - -#[test] -fn basic_interop() -> Result<(), Error> { - // Expected values were taken from Barretenberg by running `crypto::pedersen::compress_native` - // printing the result in hex to `std::cout` and copying - struct Test<'a> { - input_left: FieldElement, - input_right: FieldElement, - expected_hex: &'a str, - } - - let tests = vec![ - Test { - input_left: FieldElement::zero(), - input_right: FieldElement::one(), - expected_hex: "0x0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af", - }, - Test { - input_left: FieldElement::one(), - input_right: FieldElement::one(), - expected_hex: "0x0e1793a0c122887bcb53c84776f4704c26bc093b25eaa9c7847a672c65e314ae", - }, - Test { - input_left: FieldElement::one(), - input_right: FieldElement::zero(), - expected_hex: "0x0c93b3f27730b2e331e634af15bc9d5a769688921f30b36ca926b35a96b3306c", - }, - ]; - - let barretenberg = Barretenberg::new(); - for test in tests { - let expected = FieldElement::from_hex(test.expected_hex).unwrap(); - - let got = barretenberg.compress_native(&test.input_left, &test.input_right)?; - let got_many = barretenberg.compress_many(vec![test.input_left, test.input_right])?; - assert_eq!(got, expected); - assert_eq!(got, got_many); - } - Ok(()) -} - -#[test] -fn pedersen_hash_to_point() -> Result<(), Error> { - let barretenberg = Barretenberg::new(); - let (x, y) = barretenberg.encrypt(vec![FieldElement::zero(), FieldElement::one()], 0)?; - let expected_x = FieldElement::from_hex( - "0x0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af", - ) - .unwrap(); - let expected_y = FieldElement::from_hex( - "0x230294a041e26fe80b827c2ef5cb8784642bbaa83842da2714d62b1f3c4f9752", - ) - .unwrap(); - - assert_eq!(expected_x.to_hex(), x.to_hex()); - assert_eq!(expected_y.to_hex(), y.to_hex()); - Ok(()) -} diff --git a/src/barretenberg/scalar_mul.rs b/src/barretenberg/scalar_mul.rs deleted file mode 100644 index 45204365..00000000 --- a/src/barretenberg/scalar_mul.rs +++ /dev/null @@ -1,48 +0,0 @@ -use acvm::FieldElement; - -use super::{Barretenberg, Error, FIELD_BYTES}; - -pub(crate) trait ScalarMul { - fn fixed_base(&self, input: &FieldElement) -> Result<(FieldElement, FieldElement), Error>; -} - -impl ScalarMul for Barretenberg { - fn fixed_base(&self, input: &FieldElement) -> Result<(FieldElement, FieldElement), Error> { - let lhs_ptr: usize = 0; - let result_ptr: usize = lhs_ptr + FIELD_BYTES; - self.transfer_to_heap(&input.to_be_bytes(), lhs_ptr); - - self.call_multiple( - "compute_public_key", - vec![&lhs_ptr.into(), &result_ptr.into()], - )?; - - let result_bytes: [u8; 2 * FIELD_BYTES] = self.read_memory(result_ptr); - let (pubkey_x_bytes, pubkey_y_bytes) = result_bytes.split_at(FIELD_BYTES); - - assert!(pubkey_x_bytes.len() == FIELD_BYTES); - assert!(pubkey_y_bytes.len() == FIELD_BYTES); - - let pubkey_x = FieldElement::from_be_bytes_reduce(pubkey_x_bytes); - let pubkey_y = FieldElement::from_be_bytes_reduce(pubkey_y_bytes); - Ok((pubkey_x, pubkey_y)) - } -} - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn smoke_test() -> Result<(), Error> { - let barretenberg = Barretenberg::new(); - let input = FieldElement::one(); - - let res = barretenberg.fixed_base(&input)?; - let x = "0000000000000000000000000000000000000000000000000000000000000001"; - let y = "0000000000000002cf135e7506a45d632d270d45f1181294833fc48d823f272c"; - - assert_eq!(x, res.0.to_hex()); - assert_eq!(y, res.1.to_hex()); - Ok(()) - } -} diff --git a/src/barretenberg/schnorr.rs b/src/barretenberg/schnorr.rs deleted file mode 100644 index 0ef8b7a2..00000000 --- a/src/barretenberg/schnorr.rs +++ /dev/null @@ -1,163 +0,0 @@ -use super::{Barretenberg, Error}; - -pub(crate) trait SchnorrSig { - fn construct_signature( - &self, - message: &[u8], - private_key: [u8; 32], - ) -> Result<([u8; 32], [u8; 32]), Error>; - fn construct_public_key(&self, private_key: [u8; 32]) -> Result<[u8; 64], Error>; - fn verify_signature( - &self, - pub_key: [u8; 64], - sig_s: [u8; 32], - sig_e: [u8; 32], - message: &[u8], - ) -> Result<bool, Error>; -} - -impl SchnorrSig for Barretenberg { - fn construct_signature( - &self, - message: &[u8], - private_key: [u8; 32], - ) -> Result<([u8; 32], [u8; 32]), Error> { - use super::{wasm::WASM_SCRATCH_BYTES, FIELD_BYTES}; - - let sig_s_ptr: usize = 0; - let sig_e_ptr: usize = sig_s_ptr + FIELD_BYTES; - let private_key_ptr: usize = sig_e_ptr + FIELD_BYTES; - let message_ptr: usize = private_key_ptr + private_key.len(); - assert!( - message_ptr + message.len() < WASM_SCRATCH_BYTES, - "Message overran wasm scratch space" - ); - - self.transfer_to_heap(&private_key, private_key_ptr); - self.transfer_to_heap(message, message_ptr); - self.call_multiple( - "construct_signature", - vec![ - &message_ptr.into(), - &message.len().into(), - &private_key_ptr.into(), - &sig_s_ptr.into(), - &sig_e_ptr.into(), - ], - )?; - - let sig_s: [u8; FIELD_BYTES] = self.read_memory(sig_s_ptr); - let sig_e: [u8; FIELD_BYTES] = self.read_memory(sig_e_ptr); - - Ok((sig_s, sig_e)) - } - - #[allow(dead_code)] - fn construct_public_key(&self, private_key: [u8; 32]) -> Result<[u8; 64], Error> { - use super::FIELD_BYTES; - - let private_key_ptr: usize = 0; - let result_ptr: usize = private_key_ptr + FIELD_BYTES; - - self.transfer_to_heap(&private_key, private_key_ptr); - - self.call_multiple( - "compute_public_key", - vec![&private_key_ptr.into(), &result_ptr.into()], - )?; - - Ok(self.read_memory(result_ptr)) - } - - fn verify_signature( - &self, - pub_key: [u8; 64], - sig_s: [u8; 32], - sig_e: [u8; 32], - message: &[u8], - ) -> Result<bool, Error> { - use super::wasm::WASM_SCRATCH_BYTES; - - let public_key_ptr: usize = 0; - let sig_s_ptr: usize = public_key_ptr + pub_key.len(); - let sig_e_ptr: usize = sig_s_ptr + sig_s.len(); - let message_ptr: usize = sig_e_ptr + sig_e.len(); - assert!( - message_ptr + message.len() < WASM_SCRATCH_BYTES, - "Message overran wasm scratch space" - ); - - self.transfer_to_heap(&pub_key, public_key_ptr); - self.transfer_to_heap(&sig_s, sig_s_ptr); - self.transfer_to_heap(&sig_e, sig_e_ptr); - self.transfer_to_heap(message, message_ptr); - - let verified = self.call_multiple( - "verify_signature", - vec![ - &message_ptr.into(), - &message.len().into(), - &public_key_ptr.into(), - &sig_s_ptr.into(), - &sig_e_ptr.into(), - ], - )?; - - // Note, currently for Barretenberg plonk, if the signature fails - // then the whole circuit fails. - Ok(verified.try_into()?) - } -} - -#[test] -fn basic_interop() -> Result<(), Error> { - let barretenberg = Barretenberg::new(); - - // First case should pass, standard procedure for Schnorr - let private_key = [2; 32]; - let message = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - - let public_key = barretenberg.construct_public_key(private_key)?; - let (sig_s, sig_e) = barretenberg.construct_signature(&message, private_key)?; - let valid_signature = barretenberg.verify_signature(public_key, sig_s, sig_e, &message)?; - assert!(valid_signature); - - // Should fail, since the messages are different - let private_key = [2; 32]; - let message = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - - let public_key = barretenberg.construct_public_key(private_key)?; - let (sig_s, sig_e) = barretenberg.construct_signature(&message, private_key)?; - let valid_signature = barretenberg.verify_signature(public_key, sig_s, sig_e, &[0, 2])?; - assert!(!valid_signature); - - // Should fail, since the signature is not valid - let private_key = [2; 32]; - let message = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - let sig_s = [1; 32]; - let sig_e = [1; 32]; - - let public_key = barretenberg.construct_public_key(private_key)?; - let valid_signature = barretenberg.verify_signature(public_key, sig_s, sig_e, &message)?; - assert!(!valid_signature); - - // Should fail, since the public key does not match - let private_key_a = [1; 32]; - let private_key_b = [2; 32]; - let message = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - - let public_key_b = barretenberg.construct_public_key(private_key_b)?; - let (sig_s, sig_e) = barretenberg.construct_signature(&message, private_key_a)?; - let valid_signature = barretenberg.verify_signature(public_key_b, sig_s, sig_e, &message)?; - assert!(!valid_signature); - - // Test the first case again, to check if memory is being freed and overwritten properly - let private_key = [2; 32]; - let message = vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - - let public_key = barretenberg.construct_public_key(private_key)?; - let (sig_s, sig_e) = barretenberg.construct_signature(&message, private_key)?; - let valid_signature = barretenberg.verify_signature(public_key, sig_s, sig_e, &message)?; - assert!(valid_signature); - Ok(()) -} diff --git a/src/common_reference_string.rs b/src/common_reference_string.rs deleted file mode 100644 index 799de7c3..00000000 --- a/src/common_reference_string.rs +++ /dev/null @@ -1,24 +0,0 @@ -use acvm::{acir::circuit::Circuit, async_trait, CommonReferenceString}; - -use crate::{BackendError, Barretenberg}; - -// TODO(#185): Ensure CRS download works in JS -#[async_trait(?Send)] -impl CommonReferenceString for Barretenberg { - type Error = BackendError; - - async fn generate_common_reference_string( - &self, - _circuit: &Circuit, - ) -> Result<Vec<u8>, Self::Error> { - unimplemented!("CRS handling is now left to the backend") - } - - async fn update_common_reference_string( - &self, - _common_reference_string: Vec<u8>, - _circuit: &Circuit, - ) -> Result<Vec<u8>, Self::Error> { - unimplemented!("CRS handling is now left to the backend") - } -} diff --git a/src/lib.rs b/src/lib.rs index 941a6fd2..46867a6a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,15 +13,21 @@ 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 barretenberg; mod bb; -mod common_reference_string; mod proof_system; -mod pwg; mod smart_contract; -pub use barretenberg::Barretenberg; -use barretenberg::{Error, FeatureError}; +/// The number of bytes necessary to store a `FieldElement`. +const FIELD_BYTES: usize = 32; + +#[derive(Debug, Default)] +pub struct Barretenberg; + +impl Barretenberg { + pub fn new() -> Barretenberg { + Barretenberg + } +} impl acvm::Backend for Barretenberg {} @@ -29,8 +35,6 @@ impl acvm::Backend for Barretenberg {} #[error(transparent)] pub struct BackendError(#[from] Error); -impl From<FeatureError> for BackendError { - fn from(value: FeatureError) -> Self { - value.into() - } -} +#[allow(clippy::upper_case_acronyms)] +#[derive(Debug, thiserror::Error)] +enum Error {} diff --git a/src/proof_system.rs b/src/proof_system.rs index 1ea9b8ba..33ec8457 100644 --- a/src/proof_system.rs +++ b/src/proof_system.rs @@ -8,9 +8,8 @@ use acvm::FieldElement; use acvm::{Language, ProofSystemCompiler}; use tempfile::tempdir; -use crate::barretenberg::FIELD_BYTES; use crate::bb::{GatesCommand, ProveCommand, VerifyCommand, WriteVkCommand}; -use crate::{BackendError, Barretenberg}; +use crate::{BackendError, Barretenberg, FIELD_BYTES}; impl ProofSystemCompiler for Barretenberg { type Error = BackendError; @@ -64,14 +63,6 @@ impl ProofSystemCompiler for Barretenberg { } } - fn preprocess( - &self, - _common_reference_string: &[u8], - _circuit: &Circuit, - ) -> Result<(Vec<u8>, Vec<u8>), Self::Error> { - unimplemented!("Key generation is now left to the backend.") - } - fn prove_with_pk( &self, _common_reference_string: &[u8], diff --git a/src/pwg.rs b/src/pwg.rs deleted file mode 100644 index d10737d0..00000000 --- a/src/pwg.rs +++ /dev/null @@ -1,58 +0,0 @@ -use acvm::acir::BlackBoxFunc; -use acvm::{BlackBoxFunctionSolver, BlackBoxResolutionError, FieldElement}; - -use crate::barretenberg::pedersen::Pedersen; -use crate::barretenberg::scalar_mul::ScalarMul; -use crate::barretenberg::schnorr::SchnorrSig; -use crate::Barretenberg; - -impl BlackBoxFunctionSolver for Barretenberg { - fn schnorr_verify( - &self, - public_key_x: &FieldElement, - public_key_y: &FieldElement, - signature: &[u8], - message: &[u8], - ) -> Result<bool, BlackBoxResolutionError> { - // In barretenberg, if the signature fails, then the whole thing fails. - - let pub_key: Vec<u8> = public_key_x - .to_be_bytes() - .into_iter() - .chain(public_key_y.to_be_bytes()) - .collect(); - let pub_key: [u8; 64] = pub_key.try_into().unwrap(); - - let sig_s: [u8; 32] = signature[0..32].try_into().unwrap(); - let sig_e: [u8; 32] = signature[32..64].try_into().unwrap(); - - let valid_signature = self - .verify_signature(pub_key, sig_s, sig_e, message) - .map_err(|err| { - BlackBoxResolutionError::Failed(BlackBoxFunc::SchnorrVerify, err.to_string()) - })?; - if !valid_signature { - dbg!("signature has failed to verify"); - } - - Ok(valid_signature) - } - - fn pedersen( - &self, - inputs: &[FieldElement], - domain_separator: u32, - ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { - self.encrypt(inputs.to_vec(), domain_separator) - .map_err(|err| BlackBoxResolutionError::Failed(BlackBoxFunc::Pedersen, err.to_string())) - } - - fn fixed_base_scalar_mul( - &self, - input: &FieldElement, - ) -> Result<(FieldElement, FieldElement), BlackBoxResolutionError> { - self.fixed_base(input).map_err(|err| { - BlackBoxResolutionError::Failed(BlackBoxFunc::FixedBaseScalarMul, err.to_string()) - }) - } -} diff --git a/src/smart_contract.rs b/src/smart_contract.rs index 73c236bc..b1a028f1 100644 --- a/src/smart_contract.rs +++ b/src/smart_contract.rs @@ -93,7 +93,7 @@ mod tests { return_values: PublicInputs::default(), }; - let bb = Barretenberg::new(); + let bb = Barretenberg; let common_reference_string = Vec::new(); let verification_key = Vec::new();