From 4f0484fca22ab4a7c93087cd2a6b74b4735cdde3 Mon Sep 17 00:00:00 2001 From: Santiago Palladino Date: Wed, 5 Apr 2023 10:12:19 +0100 Subject: [PATCH] Build a3crypto.wasm (#311) * Build a3crypto.wasm Builds a smaller wasm than full barretenberg with crypto primitives to be used from aztec3-packages. * Rename to primitives and remove asyncify * Add missing modules for tests --- cpp/dockerfiles/Dockerfile.wasm-linux-clang | 5 ++-- cpp/src/CMakeLists.txt | 32 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cpp/dockerfiles/Dockerfile.wasm-linux-clang b/cpp/dockerfiles/Dockerfile.wasm-linux-clang index f4a19bba53..d1c41f0ce7 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/src/CMakeLists.txt b/cpp/src/CMakeLists.txt index f82e5d0d72..6ccdebdf0e 100644 --- a/cpp/src/CMakeLists.txt +++ b/cpp/src/CMakeLists.txt @@ -112,12 +112,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) @@ -131,6 +154,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()