starkware-libs / crypto-cpp

Apache License 2.0
32 stars 32 forks source link

linking issue in cpp project #17

Closed Ziemien closed 2 years ago

Ziemien commented 2 years ago

I am trying to integrate the ecdsa & pedersen_hash function in custom c++ project. I cloned and built the project just fine.

After the built I have a few files in build/src/starkware/crypto/ffi, 2 headers ecdsa.h, pedersen_hash.h and libcrypto_c_exports.dylib

so I copied manually these files into a new directory and try to create a custom target in my cmake project

set(_STARK_CRYPTO_BASE_DIR /storage/developer/tools/stark)

set(STARK_CRYPTO_INCLUDE_DIR "${_STARK_CRYPTO_BASE_DIR}/include")
set(STARK_CRYPTO_LIBRARY "${_STARK_CRYPTO_BASE_DIR}/lib/libcrypto_c_exports.dylib")

message(${STARK_CRYPTO_LIBRARY})

add_library(stark::stark SHARED IMPORTED)

set_target_properties(
        stark::stark
        PROPERTIES
        IMPORTED_LOCATION "${STARK_CRYPTO_LIBRARY}"
        INTERFACE_INCLUDE_DIRECTORIES "${STARK_CRYPTO_INCLUDE_DIR}"
)

but then when trying to build a sample cxx file

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

#include <ecdsa.h>

TEST_CASE("test stark-crypto", "[single-file]") {
  const auto private_key = "aaa";
  const auto message_key = "bbb";
  const auto k = "cc";
  char out[251];

  Sign(private_key, message_key, k, out);
}

I am getting error with undefined symbol.

Undefined symbols for architecture arm64:
  "Sign(char const*, char const*, char const*, char*)", referenced from:
      C_A_T_C_H_T_E_S_T_0() in demo.cxx.o

I tried to build the library with static/shared mode (with and without -fPIC) but no luck.

➜  ffi git:(master) ✗ nm -gU libcrypto_c_exports.dylib
0000000000003a14 T _GetPublicKey
0000000000002f00 T _Hash
0000000000003cc4 T _Sign
0000000000003b2c T _Verify
0000000000003eec T __ZN9starkware11DeserializeEN3gsl4spanIKNS0_4byteEEE
0000000000003e8c T __ZN9starkware11HandleErrorEPKcN3gsl4spanINS2_4byteEEE
00000000000121a8 T __ZN9starkware11VerifyEcdsaERKNS_7EcPointINS_17PrimeFieldElementEEERKS1_RKNSt3__14pairIS1_S1_EE
0000000000011380 T __ZN9starkware12GetPublicKeyERKNS_6BigIntILm4EEE
0000000000010dac T __ZN9starkware12PedersenHashERKNS_17PrimeFieldElementES2_
000000000000411c T __ZN9starkware14GetEcConstantsEv
0000000000016248 T __ZN9starkware17PrimeFieldElement13RandomElementEPNS_4PrngE
0000000000017b70 S __ZN9starkware17PrimeFieldElement19kMontgomeryRSquaredE
000000000002b8b0 S __ZN9starkware17PrimeFieldElement28kHalfMultiplicativeGroupSizeE
0000000000017b90 S __ZN9starkware17PrimeFieldElement8kModulusE
0000000000012d54 T __ZN9starkware21VerifyEcdsaPartialKeyERKNS_17PrimeFieldElementES2_RKNSt3__14pairIS0_S0_EE
0000000000003fe8 T __ZN9starkware9SerializeERKNS_6BigIntILm4EEEN3gsl4spanINS4_4byteEEE
0000000000011698 T __ZN9starkware9SignEcdsaERKNS_6BigIntILm4EEERKNS_17PrimeFieldElementES3_
0000000000016334 T __ZNK9starkware17PrimeFieldElement3PowERKNSt3__16vectorIbNS1_9allocatorIbEEEE
0000000000016460 T __ZNK9starkware17PrimeFieldElement3PowEy
0000000000016850 T __ZNK9starkware17PrimeFieldElement4SqrtEv
0000000000016678 T __ZNK9starkware17PrimeFieldElement8IsSquareEv
Ziemien commented 2 years ago

exported headers are missing extern "C"