20 unsigned char seckey1[32];
21 unsigned char seckey2[32];
22 unsigned char compressed_pubkey1[33];
23 unsigned char compressed_pubkey2[33];
24 unsigned char shared_secret1[32];
25 unsigned char shared_secret2[32];
26 unsigned char randomize[32];
35 printf(
"Failed to generate randomness\n");
46 printf(
"Failed to generate randomness\n");
53 printf(
"Generated secret key is invalid. This indicates an issue with the random number generator.\n");
64 len =
sizeof(compressed_pubkey1);
68 assert(len ==
sizeof(compressed_pubkey1));
71 len =
sizeof(compressed_pubkey2);
75 assert(len ==
sizeof(compressed_pubkey2));
81 return_val =
secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL);
86 return_val =
secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL);
90 return_val = memcmp(shared_secret1, shared_secret2,
sizeof(shared_secret1));
95 printf(
"Compressed Pubkey1: ");
96 print_hex(compressed_pubkey1,
sizeof(compressed_pubkey1));
99 printf(
"Compressed Pubkey2: ");
100 print_hex(compressed_pubkey2,
sizeof(compressed_pubkey2));
101 printf(
"\nShared Secret: ");
102 print_hex(shared_secret1,
sizeof(shared_secret1));
static int fill_random(unsigned char *data, size_t size)
static void secure_erase(void *ptr, size_t len)
static void print_hex(unsigned char *data, size_t size)
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an elliptic curve secret key.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Opaque data structure that holds a parsed and valid public key.