I make a small test code (test.cpp), try to show the problem I meet: to compile use

g++ -o test test.cpp -lsecp256k1

#include <stdio.h>
#include "secp256k1.h"

static secp256k1_context_t *ctx = NULL;

int main(int argc, char **argv) {

ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
printf("Context created!\n");
secp256k1_context_t *ctx_clone;
ctx_clone = secp256k1_context_clone(ctx);
printf("Context cloned!\n");

/* if enable below two lines of code the compliling error will happen */
// secp256k1_ecmult_context_t* clone_ecm_ctx;
// secp256k1_ecmult_context_clone(clone_ecm_ctx, &ctx->ecmult_ctx);

printf("Context destroyed!\n");

return 0;

Without the two comments out code I will not have issue. If with two comments out code, the complier will report that:

test.cpp:15:51: error: invalid use of incomplete type 'secp256k1_context_t {aka struct secp256k1_context_struct}'
 secp256k1_ecmult_context_clone(clone_ecm_ctx, &ctx->ecmult_ctx);
In file included from test.cpp:2:0:
/usr/local/include/secp256k1.h:50:16: note: forward declaration of 'secp256k1_context_t {aka struct secp256k1_context_struct}'
 typedef struct secp256k1_context_struct secp256k1_context_t;

That looks like I can not operate the context struct internal member (because of the opaque struct), however, my code indeed need to access the struct inside of the context struct, for exp, the below code will trig a similar compiling error:

secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r1, &s1, &q, &m)

is there any workaround there?

  • 1
    You use secp256k1_context_create () to initialize the context. Usually you will pass the flags SECP256K1_CONTEXT_SIGN or SECP256K1_CONTEXT_VERIFY – Mark H Feb 10 at 10:04
  • Can you add this as the answer @MarkH? I haven't verified it personally but I've been told it is correct. – Michael Folkson Feb 10 at 14:17
  • There's an open PR to libsecp256k1 that adds example usage code github.com/bitcoin-core/secp256k1/pull/748 Perhaps that's helpful for you. – nickler Feb 10 at 17:08
  • @MarkH Thank you so much for your suggestion, I add more into the questions. – LeonMSH Feb 18 at 3:59
  • @MarkH So far I still have the problem, using the posted small code can reproduce the issue I have, would you please help to check? Thanks so much... – LeonMSH Mar 2 at 1:45

The reason for this error is that secp256k1_context is an opaque type. This means that you cannot create it on the stack; you need to create a heap-allocated version (and destroy it) using the functions from the API (specifically secp256k1_context_create and secp256k1_context_destroy, as Mark H mentioned in a comment). There is no way to access the internals.

You're right not to try to #include <secp256k1.c> ... aside from being unlikely to build and requiring you to bundle a copy of the whole library, it would expose internals of the library to your code which are not safe to use directly.

For what you are doing (replacing internal signing functionality with OCL-enabled versions) you are probably better off forking the library and directly editing the source. This isn't much more invasive than what you're trying to do, and it would let you use the existing unit tests and valgrind ctime tests to check your work.

  • first of all, Thank you all for the above comments and suggestions. I will add more into the original post. – LeonMSH Feb 18 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.