diff --git a/README.md b/README.md index 802392f5fc3..ef207aa92b8 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,17 @@ - libomp (if multithreading is required. Multithreading can be disabled using the compiler flag `-DMULTITHREADING 0`) - wasm-opt (part of the [Binaryen](https://github.com/WebAssembly/binaryen) toolkit) +To install on Ubuntu, run: +``` +sudo apt-get install cmake clang clang-format ninja-build binaryen +``` + ### Installing openMP (Linux) +Install from source: + ``` -RUN git clone -b release/10.x --depth 1 https://github.com/llvm/llvm-project.git \ +git clone -b release/10.x --depth 1 https://github.com/llvm/llvm-project.git \ && cd llvm-project && mkdir build-openmp && cd build-openmp \ && cmake ../openmp -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DLIBOMP_ENABLE_SHARED=OFF \ && cmake --build . --parallel \ @@ -22,6 +29,14 @@ RUN git clone -b release/10.x --depth 1 https://github.com/llvm/llvm-project.git && cd ../.. && rm -rf llvm-project ``` +Or install from a package manager, on Ubuntu: + +``` +sudo apt-get install libomp-dev +``` + +> Note: on a fresh Ubuntu Kinetic installation, installing OpenMP from source yields a `Could NOT find OpenMP_C (missing: OpenMP_omp_LIBRARY) (found version "5.0")` error when trying to build Barretenberg. Installing from apt worked fine. + ### Getting started Run the bootstrap script. (The bootstrap script will build both the native and wasm versions of barretenberg) diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index b978b04d53f..582d7285374 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -18,6 +18,8 @@ option(BENCHMARKS "Build benchmarks" ON) option(FUZZING "Build fuzzing harnesses" OFF) option(DISABLE_TBB "Intel Thread Building Blocks" ON) option(COVERAGE "Enable collecting coverage from tests" OFF) +option(SERIALIZE_CANARY "Build with serialize canary" OFF) +option(ENABLE_ASAN "Address sanitizer for debugging tricky memory corruption" OFF) option(ENABLE_HEAVY_TESTS "Enable heavy tests when collecting coverage" OFF) option(INSTALL_BARRETENBERG "Enable installation of barretenberg. (Projects embedding barretenberg may want to turn this OFF.)" ON) option(USE_TURBO "Enable the use of TurboPlonk in barretenberg." OFF) @@ -39,6 +41,15 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64" OR CMAKE_SYSTEM_PROCESSOR MATCHES "a set(DISABLE_TBB 0) endif() +if(ENABLE_ASAN) + add_compile_options(-fsanitize=address) + add_link_options(-fsanitize=address) +endif() + +if(SERIALIZE_CANARY) + add_definitions(-DENABLE_SERIALIZE_CANARY) +endif() + if(FUZZING) add_definitions(-DFUZZING=1) diff --git a/cpp/dockerfiles/Dockerfile.wasm-linux-clang b/cpp/dockerfiles/Dockerfile.wasm-linux-clang index f4a19bba534..d1c41f0ce7a 100644 --- a/cpp/dockerfiles/Dockerfile.wasm-linux-clang +++ b/cpp/dockerfiles/Dockerfile.wasm-linux-clang @@ -5,10 +5,11 @@ WORKDIR /usr/src/barretenberg/cpp/src RUN curl -s -L https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-12/wasi-sdk-12.0-linux.tar.gz | tar zxfv - WORKDIR /usr/src/barretenberg/cpp COPY . . -# Build both honk_tests barretenberg.wasm +# Build both honk_tests barretenberg.wasm primitives.wasm # This ensures that we aren't using features that would be incompatible with WASM for Honk -RUN cmake --preset wasm && cmake --build --preset wasm --target honk_tests --target barretenberg.wasm +RUN cmake --preset wasm && cmake --build --preset wasm --target honk_tests --target barretenberg.wasm --target primitives.wasm FROM alpine:3.17 COPY --from=builder /usr/src/barretenberg/cpp/build-wasm/bin/barretenberg.wasm /usr/src/barretenberg/cpp/build/bin/barretenberg.wasm +COPY --from=builder /usr/src/barretenberg/cpp/build-wasm/bin/primitives.wasm /usr/src/barretenberg/cpp/build/bin/primitives.wasm COPY --from=builder /usr/src/barretenberg/cpp/build-wasm/bin/*_tests /usr/src/barretenberg/cpp/build/bin/ diff --git a/cpp/scripts/bb-tests b/cpp/scripts/bb-tests index b8df6e338fb..ba012981ead 100644 --- a/cpp/scripts/bb-tests +++ b/cpp/scripts/bb-tests @@ -2,7 +2,7 @@ crypto_aes128_tests crypto_blake2s_tests crypto_blake3s_tests crypto_ecdsa_tests -crypto_pedersen_tests +crypto_pedersen_commitment_tests crypto_schnorr_tests crypto_sha256_tests ecc_tests @@ -18,7 +18,7 @@ stdlib_blake2s_tests stdlib_blake3s_tests stdlib_ecdsa_tests stdlib_merkle_tree_tests -stdlib_pedersen_tests +stdlib_pedersen_commitment_tests stdlib_schnorr_tests stdlib_sha256_tests transcript_tests diff --git a/cpp/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index 3a40b2a908a..a90789f9c64 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -76,7 +76,9 @@ if(WASM) $ $ $ - $ + $ + $ + $ $ $ $ @@ -84,7 +86,8 @@ if(WASM) $ $ $ - $ + $ + $ $ $ $ @@ -92,6 +95,9 @@ if(WASM) $ $ $ + $ + $ + $ ) # With binaryen installed, it seems its wasm backend optimiser gets invoked automatically. @@ -105,12 +111,35 @@ if(WASM) -nostartfiles -O2 -Wl,--no-entry -Wl,--export-dynamic -Wl,--import-memory -Wl,--allow-undefined -Wl,--stack-first -Wl,-z,stack-size=1048576 ) + # Repeat the above but for the smaller primitives.wasm + # Used in packages where we don't need the full contents of barretenberg + add_executable( + primitives.wasm + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + $ + ) + + target_link_options( + primitives.wasm + PRIVATE + -nostartfiles -O2 -Wl,--no-entry -Wl,--export-dynamic -Wl,--import-memory -Wl,--allow-undefined -Wl,--stack-first -Wl,-z,stack-size=1048576 + ) + # TODO(blaine): Figure out how to Asyncify the wasm output. # Binaryen's Asyncify transform produces wasm that has too many local variables to run in a WebAssembly # instance. This likely would be "solved" by enabling the optimizations to reduce the number of locals, # but using any optimization level results in a wasm file that takes an unusable amount of time to solve the # most simple prood. - # find_program(WASM_OPT wasm-opt) # if(NOT WASM_OPT) @@ -124,6 +153,13 @@ if(WASM) # VERBATIM # ) + add_custom_command( + TARGET primitives.wasm + POST_BUILD + COMMAND wasm-opt "$" -O2 -o "$" + VERBATIM + ) + if(INSTALL_BARRETENBERG) install(TARGETS barretenberg.wasm DESTINATION ${CMAKE_INSTALL_BINDIR}) endif() @@ -142,7 +178,9 @@ if(WASM) $ $ $ - $ + $ + $ + $ $ $ $ @@ -150,7 +188,8 @@ if(WASM) $ $ $ - $ + $ + $ $ $ $ @@ -174,7 +213,9 @@ else() $ $ $ - $ + $ + $ + $ $ $ $ @@ -182,7 +223,8 @@ else() $ $ $ - $ + $ + $ $ $ $ diff --git a/cpp/src/barretenberg/common/container.hpp b/cpp/src/barretenberg/common/container.hpp index 6418be1461d..1c87bb1224a 100644 --- a/cpp/src/barretenberg/common/container.hpp +++ b/cpp/src/barretenberg/common/container.hpp @@ -48,4 +48,16 @@ InnerCont flatten(Cont const& in) result.insert(result.end(), e.begin(), e.end()); } return result; +} + +// Return the first index at which a given item can be found in the vector. +// Only safe for vectors with length less than the size_t overflow size. +template long index_of(std::vector const& vec, T const& item) +{ + auto const& begin = vec.begin(); + auto const& end = vec.end(); + + auto const& itr = std::find(begin, end, item); + + return itr == end ? -1 : std::distance(begin, itr); } \ No newline at end of file diff --git a/cpp/src/barretenberg/common/map.hpp b/cpp/src/barretenberg/common/map.hpp index a78c81ca628..79205f2bda5 100644 --- a/cpp/src/barretenberg/common/map.hpp +++ b/cpp/src/barretenberg/common/map.hpp @@ -10,13 +10,29 @@ template