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

How to use the library? #224

Closed
ShaliDaliHaliDa opened this issue Feb 25, 2015 · 16 comments
Closed

How to use the library? #224

ShaliDaliHaliDa opened this issue Feb 25, 2015 · 16 comments

Comments

@ShaliDaliHaliDa
Copy link

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
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link

joshmg commented Jan 22, 2018

@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
Copy link

mrx23dot commented Apr 3, 2022

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
Copy link
Contributor

elichai commented Apr 3, 2022

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
Copy link

mrx23dot commented Apr 3, 2022

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

@elichai
Copy link
Contributor

elichai commented Apr 3, 2022

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
Copy link

mrx23dot commented Apr 3, 2022

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

@elichai
Copy link
Contributor

elichai commented Apr 3, 2022

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 #929

@mrx23dot
Copy link

mrx23dot commented Apr 3, 2022

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
Copy link
Contributor

sipa commented Apr 3, 2022

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
Copy link

mrx23dot commented Apr 4, 2022

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
Copy link
Contributor

sipa commented Apr 4, 2022

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
Copy link

mrx23dot commented Apr 5, 2022

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
Copy link
Contributor

sipa commented Apr 5, 2022

@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).

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

7 participants