Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pure rust secp256k1 under WASM. #1176

Closed
artemii235 opened this issue Dec 24, 2021 · 3 comments
Closed

Use pure rust secp256k1 under WASM. #1176

artemii235 opened this issue Dec 24, 2021 · 3 comments
Assignees

Comments

@artemii235
Copy link
Member

We have some problems compiling for WASM on MacOS.

@artemii235 artemii235 self-assigned this Dec 24, 2021
@sergeyboyko0791
Copy link

sergeyboyko0791 commented Dec 24, 2021

I just found a solution 🥳

The summary:
To build mm2 in WASM on MacOS do the following:

brew install llvm
# M1
CC=/opt/homebrew/opt/llvm/bin/clang AR=/opt/homebrew/opt/llvm/bin/llvm-ar wasm-pack build --target web --out-dir wasm_build/deps/pkg/
# Intel
CC=/usr/local/opt/llvm/bin/clang AR=/usr/local/opt/llvm/bin/llvm-ar wasm-pack build --target web --out-dir wasm_build/deps/pkg/

The source: sapio-lang/sapio#146 (comment)

I'll add these steps to README.md

--------------

First of all, I faced a problem creating wasm32-unknown-unknown target when compile secp256k1-sys:

Command: wasm-pack build --target web --out-dir wasm_build/deps/pkg/
Error log:

   Compiling secp256k1-sys v0.4.0 (https://github.com/rust-bitcoin/rust-secp256k1#4ae0e7eb)
The following warnings were emitted during compilation:

warning: error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
warning: 1 error generated.

error: failed to run custom build command for `secp256k1-sys v0.4.0 (https://github.com/rust-bitcoin/rust-secp256k1#4ae0e7eb)`

Caused by:
  process didn't exit successfully: `/Users/bso/scm/tmp/rust-secp256k1/target/release/build/secp256k1-sys-c2a04f25427479bf/build-script-build` (exit status: 1)
  --- stdout
  TARGET = Some("wasm32-unknown-unknown")
  OPT_LEVEL = Some("3")
  HOST = Some("aarch64-apple-darwin")
  CC_wasm32-unknown-unknown = None
  CC_wasm32_unknown_unknown = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-unknown-unknown = None
  CFLAGS_wasm32_unknown_unknown = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("false")
  CC_wasm32-unknown-unknown = None
  CC_wasm32_unknown_unknown = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-unknown-unknown = None
  CFLAGS_wasm32_unknown_unknown = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_BUILD=1" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DECMULT_GEN_PREC_BITS=4" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/Users/bso/scm/tmp/rust-secp256k1/target/wasm32-unknown-unknown/release/build/secp256k1-sys-05a77631852afcad/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"
  cargo:warning=error: unable to create target: 'No available targets are compatible with triple "wasm32-unknown-unknown"'
  cargo:warning=1 error generated.
  exit status: 1

  --- stderr


  error occurred: Command "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_BUILD=1" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DECMULT_GEN_PREC_BITS=4" "-DUSE_NUM_NONE=1" "-DUSE_FIELD_INV_BUILTIN=1" "-DUSE_SCALAR_INV_BUILTIN=1" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-o" "/Users/bso/scm/tmp/rust-secp256k1/target/wasm32-unknown-unknown/release/build/secp256k1-sys-05a77631852afcad/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c" with args "clang" did not execute successfully (status code exit status: 1).


Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

Related issue: rustwasm/wasm-pack#621

--------------

After installing and configuring llvm accordingly to the comment, I got the following error:

Commands:

export CC=/opt/homebrew/opt/llvm/bin/clang
export AC=/opt/homebrew/opt/llvm/bin/llvm-ac
wasm-pack build --target web --out-dir wasm_build/deps/pkg/

Error log:

Compiling secp256k1-sys v0.4.0 (https://github.com/rust-bitcoin/rust-secp256k1#4ae0e7eb)
error: failed to build archive: section too large

error: aborting due to previous error

The following warnings were emitted during compilation:

warning: In file included from depend/secp256k1/src/secp256k1.c:10:
warning: In file included from depend/secp256k1/src/assumptions.h:12:
warning: In file included from depend/secp256k1/src/util.h:16:
warning: wasm-sysroot/stdio.h:4:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_INT_SIZE = sizeof(int);
warning:                            ^
warning: wasm-sysroot/stdio.h:5:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_INT_ALIGN = alignof(int);
warning:                            ^
warning: wasm-sysroot/stdio.h:7:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_UNSIGNED_INT_SIZE = sizeof(unsigned int);
warning:                            ^
warning: wasm-sysroot/stdio.h:8:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_UNSIGNED_INT_ALIGN = alignof(unsigned int);
warning:                            ^
warning: wasm-sysroot/stdio.h:10:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_SIZE_T_SIZE = sizeof(size_t);
warning:                            ^
warning: wasm-sysroot/stdio.h:11:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_SIZE_T_ALIGN = alignof(size_t);
warning:                            ^
warning: wasm-sysroot/stdio.h:13:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_UNSIGNED_CHAR_SIZE = sizeof(unsigned char);
warning:                            ^
warning: wasm-sysroot/stdio.h:14:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_UNSIGNED_CHAR_ALIGN = alignof(unsigned char);
warning:                            ^
warning: wasm-sysroot/stdio.h:16:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_PTR_SIZE = sizeof(void*);
warning:                            ^
warning: wasm-sysroot/stdio.h:17:28: warning: 'extern' variable has an initializer [-Wextern-initializer]
warning: extern const unsigned char WASM32_PTR_ALIGN = alignof(void*);
warning:                            ^
warning: In file included from depend/secp256k1/src/secp256k1.c:13:
warning: depend/secp256k1/src/field_impl.h:266:13: warning: unused function 'rustsecp256k1_v0_4_0_fe_inv_all_var' [-Wunused-function]
warning: static void rustsecp256k1_v0_4_0_fe_inv_all_var(rustsecp256k1_v0_4_0_fe *r, const rustsecp256k1_v0_4_0_fe *a, size_t len) {
warning:             ^
warning: In file included from depend/secp256k1/src/secp256k1.c:15:
warning: depend/secp256k1/src/group_impl.h:191:13: warning: unused function 'rustsecp256k1_v0_4_0_ge_set_infinity' [-Wunused-function]
warning: static void rustsecp256k1_v0_4_0_ge_set_infinity(rustsecp256k1_v0_4_0_ge *r) {
warning:             ^
warning: depend/secp256k1/src/group_impl.h:658:12: warning: unused function 'rustsecp256k1_v0_4_0_gej_has_quad_y_var' [-Wunused-function]
warning: static int rustsecp256k1_v0_4_0_gej_has_quad_y_var(const rustsecp256k1_v0_4_0_gej *a) {
warning:            ^
warning: In file included from depend/secp256k1/src/secp256k1.c:16:
warning: In file included from depend/secp256k1/src/ecmult_impl.h:16:
warning: In file included from depend/secp256k1/src/ecmult.h:13:
warning: depend/secp256k1/src/scratch.h:24:38: warning: unused function 'rustsecp256k1_v0_4_0_scratch_create' [-Wunused-function]
warning: static rustsecp256k1_v0_4_0_scratch* rustsecp256k1_v0_4_0_scratch_create(const rustsecp256k1_v0_4_0_callback* error_callback, size_t max_size);
warning:                                      ^
warning: depend/secp256k1/src/scratch.h:26:13: warning: unused function 'rustsecp256k1_v0_4_0_scratch_destroy' [-Wunused-function]
warning: static void rustsecp256k1_v0_4_0_scratch_destroy(const rustsecp256k1_v0_4_0_callback* error_callback, rustsecp256k1_v0_4_0_scratch* scratch);
warning:             ^
warning: In file included from depend/secp256k1/src/secp256k1.c:16:
warning: depend/secp256k1/src/ecmult_impl.h:1035:12: warning: unused function 'rustsecp256k1_v0_4_0_ecmult_multi_var' [-Wunused-function]
warning: static int rustsecp256k1_v0_4_0_ecmult_multi_var(const rustsecp256k1_v0_4_0_callback* error_callback, const rustsecp256k1_v0_4_0_ecmult_context *ctx, rustsecp256k1_v0_4_0_scratch *scratch, rustsecp256k1_v0_4_0_gej *r, const rustsecp256k1_v0_4_0_scalar *inp_g_sc, rustsecp256k1_v0_4_0_ecmult_multi_callback cb, void *cbdata, size_t n) {
warning:            ^
warning: depend/secp256k1/src/ecmult_impl.h:621:12: warning: unused function 'rustsecp256k1_v0_4_0_ecmult_strauss_batch_single' [-Wunused-function]
warning: static int rustsecp256k1_v0_4_0_ecmult_strauss_batch_single(const rustsecp256k1_v0_4_0_callback* error_callback, const rustsecp256k1_v0_4_0_ecmult_context *actx, rustsecp256k1_v0_4_0_scratch *scratch, rustsecp256k1_v0_4_0_gej *r, const rustsecp256k1_v0_4_0_scalar *inp_g_sc, rustsecp256k1_v0_4_0_ecmult_multi_callback cb, void *cbdata, size_t n) {
warning:            ^
warning: depend/secp256k1/src/ecmult_impl.h:863:15: warning: unused function 'rustsecp256k1_v0_4_0_pippenger_scratch_size' [-Wunused-function]
warning: static size_t rustsecp256k1_v0_4_0_pippenger_scratch_size(size_t n_points, int bucket_window) {
warning:               ^
warning: depend/secp256k1/src/ecmult_impl.h:944:12: warning: unused function 'rustsecp256k1_v0_4_0_ecmult_pippenger_batch_single' [-Wunused-function]
warning: static int rustsecp256k1_v0_4_0_ecmult_pippenger_batch_single(const rustsecp256k1_v0_4_0_callback* error_callback, const rustsecp256k1_v0_4_0_ecmult_context *actx, rustsecp256k1_v0_4_0_scratch *scratch, rustsecp256k1_v0_4_0_gej *r, const rustsecp256k1_v0_4_0_scalar *inp_g_sc, rustsecp256k1_v0_4_0_ecmult_multi_callback cb, void *cbdata, size_t n) {
warning:            ^
warning: 19 warnings generated.
warning: warning: /Library/Developer/CommandLineTools/usr/bin/ranlib: archive library: /Users/bso/scm/tmp/rust-secp256k1/target/wasm32-unknown-unknown/release/build/secp256k1-sys-05a77631852afcad/out/libsecp256k1.a the table of contents is empty (no object file members in the library define global symbols)

error: could not compile `secp256k1-sys`

To learn more, run the command again with --verbose.
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"

--------------

But... Unexpectedly for me, the following command works 🧐

Command: CC=/opt/homebrew/opt/llvm/bin/clang AR=/opt/homebrew/opt/llvm/bin/llvm-ar wasm-pack build --target web --out-dir wasm_build/deps/pkg/

@artemii235
Copy link
Member Author

@sergeyboyko0791 Could you please update the docs and add an instruction on how the WASM should be built on M1?

@artemii235 artemii235 reopened this Feb 1, 2022
@artemii235
Copy link
Member Author

Instruction is merged to dev.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants