bitcoin-core / secp256k1

Optimized C library for EC operations on curve secp256k1
MIT License
2.05k stars 993 forks source link

How to use the library? #224

Closed ShaliDaliHaliDa closed 9 years ago

ShaliDaliHaliDa commented 9 years ago

Sorry if this is a silly question but after compilation how do I use it? I found the static lib in /usr/local/lib/libsecp256k1.a and I am able to reference it, are there any examples on how to use it from Xcode?

DavidEGrayson commented 9 years ago

Did you already find the documentation in the header file? If you are familiar with C and with some of the binary formats used for ECDSA, it should probably be enough:

https://github.com/bitcoin/secp256k1/blob/master/include/secp256k1.h

You might also want to look at my usage examples here, though they are written in Ruby and they use a future version of the library's API (the one from pull request #208):

https://github.com/DavidEGrayson/secp256k1-rb

gmaxwell commented 9 years ago

Keep in mind that this is currently an unreleased experimental library with a changing interfac. When it reaches a full release there will be better documentation. (DavidEGrayson's comments about the headers are the right advice now currently.)

ShaliDaliHaliDa commented 9 years ago

Thanks for the info.Unfortunately getting a bunch of errors in Xcode when I try and use any of the mehtods. I have added reference to libsecp256k1.a , the include paths, and GMP is installed via brew yet looks like it can't find GMP ? ___gmpn_gcdext?

Is there a better compiler / IDE I should be using instead of Xcode? QT? Or Eclipse perhaps?

 "___gmpn_gcdext", referenced from:

  _secp256k1_num_mod_inverse in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

  "___gmpn_get_str", referenced from:

  _secp256k1_num_get_bin in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

 "___gmpn_set_str", referenced from:

  _secp256k1_ecdsa_recover_compact in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

  _secp256k1_scalar_inverse_var in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

  _secp256k1_ge_set_all_gej_var in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

  "___gmpn_sub_n", referenced from:

  _secp256k1_num_mod_inverse in libsecp256k1.a(libsecp256k1_la-secp256k1.o)

ld: symbol(s) not found for architecture x86_64

joshmg commented 6 years ago

@ShaliDaliHaliDa : You needed to add the GNU Multiple Precision Arithmetic Library to your build command. (via -lgmp)

Ex: g++ -lgmp ../secp256k1/.libs/libsecp256k1.a main.cpp -o main

mrx23dot commented 2 years ago

And how do I compile in the library as part of my project? (not as shared library) The library has many main() functions. Is there a simplified version? It seems to be huge.

elichai commented 2 years ago

And how do I compile in the library as part of my project? (not as shared library) The library has many main() functions. Is there a simplified version? It seems to be huge.

Depending on the build system of your project, but the best way would probably be to invoke the autogen.sh then run ./configure with whatever flags you need (schnorr/ecdh etc.) and then run the makefile via make and link to the resulting libsecp256k1.a

mrx23dot commented 2 years ago

I mean compile secp256k1.c into my exe. I tried it but it gives seg fault on context creation.

elichai commented 2 years ago

I mean compile secp256k1.c into my exe. I tried it but it gives seg fault on context creation.

That's exactly what I've talked about, you should compile libsecp256k1 as a library and then link to it. Sounds like it is windows which I know nothing about, but maybe whatever bitcoin core is doing can help you: https://github.com/bitcoin/bitcoin/blob/master/build_msvc/libsecp256k1/libsecp256k1.vcxproj

mrx23dot commented 2 years ago

Ok, so it wasn't designed to be used without linking in as a static library (.a) aka not as a black-box.

elichai commented 2 years ago

Ok, so it wasn't designed to be used without linking in as a static library (.a) aka not as a black-box.

I don't know what a "black box" means in the context of a library. But this is not a "header only library" and as such it needs to be compiled and linked (shared/static), We are trying to make this library compile more easily with a one line compiler call, but it's still ongoing, See https://github.com/bitcoin-core/secp256k1/issues/929

mrx23dot commented 2 years ago

Ok so how can I use it as a "header only" library? Linking it in two steps doesn't change a thing. gcc mymain.c secp256k1.c

Does it require some init code before I can call context init? Because it gave me segfault. As I saw the lib itself has main function.

sipa commented 2 years ago

This is not a headers only library, you can't use it as such.

You'll need to build the library, and link it into your application (either statically or dynamically). How to do that depends on the platform. I'm afraid I can't help you with usage on Windows platforms.

mrx23dot commented 2 years ago

Could you explain how is it possible that all of these files have main() entry points and compile into .a object? precompute_ecmult.c precompute_ecmult_gen.c bench.c

I would assume linker would complain for anything anything more than one main, which would come from post linking with user app. I'm on linux.

sipa commented 2 years ago

Those are separate executables that are part of the project, but are not part of the library.

The library itself is compiled from secp256k1.c, plus a few other files with assembly and precomputed tables.

mrx23dot commented 2 years ago

I manage to build it as intended, copied over generated files, it compiles, give correct result. Weirdly secp256k1_ec_pubkey_create returns the public key (generated from private key) x_mirrored + y_mirrored byte array, which won't work for the next sha256+RIPE160 step.

How do you develop/debug this lib with dbg? Is there an entry point for it? Or a script that prepares it for running by it's own?

sipa commented 2 years ago

@mrx23dot You need to call secp256k1_ec_pubkey_serialize to convert it to a stable format, as explained in the API documentation in secp256k1.h. You cannot do anything with secp256k1_pubkey objects other than passing them back to other libsecp256k1 functions, as there is no guarantee about its internal format (neither across platforms, or across versions of the library).