20 {
21 unsigned char seckey1[32];
22 unsigned char seckey2[32];
23 unsigned char compressed_pubkey1[33];
24 unsigned char compressed_pubkey2[33];
25 unsigned char shared_secret1[32];
26 unsigned char shared_secret2[32];
27 unsigned char randomize[32];
28 int return_val;
32
33
34
35
36
37
39 if (!fill_random(randomize, sizeof(randomize))) {
40 printf(
"Failed to generate randomness\n");
41 return 1;
42 }
43
44
45
47 assert(return_val);
48
49
50
51
52
53
54 while (1) {
55 if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) {
56 printf(
"Failed to generate randomness\n");
57 return 1;
58 }
60 break;
61 }
62 }
63
64
66 assert(return_val);
68 assert(return_val);
69
70
71 len =
sizeof(compressed_pubkey1);
73 assert(return_val);
74
75 assert(
len ==
sizeof(compressed_pubkey1));
76
77
78 len =
sizeof(compressed_pubkey2);
80 assert(return_val);
81
82 assert(
len ==
sizeof(compressed_pubkey2));
83
84
85
86
87
88 return_val =
secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL);
89 assert(return_val);
90
91
92
93 return_val =
secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL);
94 assert(return_val);
95
96
97 return_val = memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1));
98 assert(return_val == 0);
99
101 print_hex(seckey1, sizeof(seckey1));
102 printf(
"Compressed Pubkey1: ");
103 print_hex(compressed_pubkey1, sizeof(compressed_pubkey1));
104 printf(
"\nSecret Key2: ");
105 print_hex(seckey2, sizeof(seckey2));
106 printf(
"Compressed Pubkey2: ");
107 print_hex(compressed_pubkey2, sizeof(compressed_pubkey2));
108 printf(
"\nShared Secret: ");
109 print_hex(shared_secret1, sizeof(shared_secret1));
110
111
113
114
115
116
117
118
119
120
121 memset(seckey1, 0,
sizeof(seckey1));
122 memset(seckey2, 0,
sizeof(seckey2));
123 memset(shared_secret1, 0,
sizeof(shared_secret1));
124 memset(shared_secret2, 0,
sizeof(shared_secret2));
125
126 return 0;
127}
LOGGING_API void printf(Category category, const char *format,...)
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
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)
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)
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
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)
#define SECP256K1_EC_COMPRESSED
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)
memset(pInfo->slotDescription, ' ', 64)