|
| 1 | +#include <stdio.h> |
| 2 | +#include <assert.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +#include "random.h" |
| 6 | +#include "secp256k1.h" |
| 7 | +#include "secp256k1_ecdh.h" |
| 8 | + |
| 9 | + |
| 10 | +void print_hex(unsigned char* data, size_t size) { |
| 11 | + size_t i; |
| 12 | + printf("0x"); |
| 13 | + for (i = 0; i < size; i++) { |
| 14 | + printf("%02x", data[i]); |
| 15 | + } |
| 16 | + printf("\n"); |
| 17 | +} |
| 18 | + |
| 19 | +int main(void) { |
| 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 | + size_t len; |
| 27 | + secp256k1_pubkey pubkey1; |
| 28 | + secp256k1_pubkey pubkey2; |
| 29 | + secp256k1_context* ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 30 | + while (1) { |
| 31 | + if (!fill_random(seckey1, sizeof(seckey1)) || !fill_random(seckey2, sizeof(seckey2))) { |
| 32 | + printf("Failed to generate randomness\n"); |
| 33 | + return 1; |
| 34 | + } |
| 35 | + if (secp256k1_ec_seckey_verify(ctx, seckey1) && secp256k1_ec_seckey_verify(ctx, seckey2)) { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /* Pubkey creation on a valid Context with a verified seckey should never fail */ |
| 41 | + assert(secp256k1_ec_pubkey_create(ctx, &pubkey1, seckey1)); |
| 42 | + assert(secp256k1_ec_pubkey_create(ctx, &pubkey2, seckey2)); |
| 43 | + |
| 44 | + /* Serialize the pubkey in a compressed form */ |
| 45 | + len = sizeof(compressed_pubkey1); |
| 46 | + secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey1, &len, &pubkey1, SECP256K1_EC_COMPRESSED); |
| 47 | + /* Should be the same size as the size of the output */ |
| 48 | + assert(len == sizeof(compressed_pubkey1)); |
| 49 | + |
| 50 | + /* Serialize the pubkey in a compressed form */ |
| 51 | + len = sizeof(compressed_pubkey2); |
| 52 | + secp256k1_ec_pubkey_serialize(ctx, compressed_pubkey2, &len, &pubkey2, SECP256K1_EC_COMPRESSED); |
| 53 | + assert(len == sizeof(compressed_pubkey2)); |
| 54 | + |
| 55 | + /* Perform ECDH with seckey1 and pubkey2, should never fail with a verified seckey and valid pubkey */ |
| 56 | + assert(secp256k1_ecdh(ctx, shared_secret1, &pubkey2, seckey1, NULL, NULL)); |
| 57 | + |
| 58 | + /* Perform ECDH with seckey2 and pubkey1, should never fail with a verified seckey and valid pubkey */ |
| 59 | + assert(secp256k1_ecdh(ctx, shared_secret2, &pubkey1, seckey2, NULL, NULL)); |
| 60 | + |
| 61 | + /* Both parties should end up with the same shared secret */ |
| 62 | + assert(memcmp(shared_secret1, shared_secret2, sizeof(shared_secret1)) == 0); |
| 63 | + |
| 64 | + printf("Secret Key1: "); |
| 65 | + print_hex(seckey1, sizeof(seckey1)); |
| 66 | + printf("Compressed Pubkey1: "); |
| 67 | + print_hex(compressed_pubkey1, sizeof(compressed_pubkey1)); |
| 68 | + printf("\nSecret Key2: "); |
| 69 | + print_hex(seckey2, sizeof(seckey2)); |
| 70 | + printf("Compressed Pubkey2: "); |
| 71 | + print_hex(compressed_pubkey2, sizeof(compressed_pubkey2)); |
| 72 | + printf("\nShared Secret: "); |
| 73 | + print_hex(shared_secret1, sizeof(shared_secret1)); |
| 74 | + |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
0 commit comments