|
| 1 | +/*********************************************************************** |
| 2 | + * Copyright (c) 2024 josibake * |
| 3 | + * Distributed under the MIT software license, see the accompanying * |
| 4 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php.* |
| 5 | + ***********************************************************************/ |
| 6 | + |
| 7 | +#ifndef SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H |
| 8 | +#define SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H |
| 9 | + |
| 10 | +#include "../../../include/secp256k1_silentpayments.h" |
| 11 | + |
| 12 | +typedef struct { |
| 13 | + secp256k1_context *ctx; |
| 14 | + secp256k1_pubkey spend_pubkey; |
| 15 | + unsigned char scan_key[32]; |
| 16 | + unsigned char input_pubkey33[33]; |
| 17 | + secp256k1_xonly_pubkey tx_outputs[2]; |
| 18 | + secp256k1_xonly_pubkey tx_inputs[2]; |
| 19 | + secp256k1_silentpayments_found_output found_outputs[2]; |
| 20 | + unsigned char scalar[32]; |
| 21 | + unsigned char smallest_outpoint[36]; |
| 22 | +} bench_silentpayments_data; |
| 23 | + |
| 24 | +/* we need a non-null pointer for the cache */ |
| 25 | +static int noop; |
| 26 | +void* label_cache = &noop; |
| 27 | +const unsigned char* label_lookup(const unsigned char* key, const void* cache_ptr) { |
| 28 | + (void)key; |
| 29 | + (void)cache_ptr; |
| 30 | + return NULL; |
| 31 | +} |
| 32 | + |
| 33 | +static void bench_silentpayments_scan_setup(void* arg) { |
| 34 | + int i; |
| 35 | + bench_silentpayments_data *data = (bench_silentpayments_data*)arg; |
| 36 | + const unsigned char tx_outputs[2][32] = { |
| 37 | + {0x84,0x17,0x92,0xc3,0x3c,0x9d,0xc6,0x19,0x3e,0x76,0x74,0x41,0x34,0x12,0x5d,0x40,0xad,0xd8,0xf2,0xf4,0xa9,0x64,0x75,0xf2,0x8b,0xa1,0x50,0xbe,0x03,0x2d,0x64,0xe8}, |
| 38 | + {0x2e,0x84,0x7b,0xb0,0x1d,0x1b,0x49,0x1d,0xa5,0x12,0xdd,0xd7,0x60,0xb8,0x50,0x96,0x17,0xee,0x38,0x05,0x70,0x03,0xd6,0x11,0x5d,0x00,0xba,0x56,0x24,0x51,0x32,0x3a}, |
| 39 | + }; |
| 40 | + const unsigned char static_tx_input[32] = { |
| 41 | + 0xf2,0x07,0x16,0x2b,0x1a,0x7a,0xbc,0x51, |
| 42 | + 0xc4,0x20,0x17,0xbe,0xf0,0x55,0xe9,0xec, |
| 43 | + 0x1e,0xfc,0x3d,0x35,0x67,0xcb,0x72,0x03, |
| 44 | + 0x57,0xe2,0xb8,0x43,0x25,0xdb,0x33,0xac |
| 45 | + }; |
| 46 | + const unsigned char smallest_outpoint[36] = { |
| 47 | + 0x16, 0x9e, 0x1e, 0x83, 0xe9, 0x30, 0x85, 0x33, 0x91, |
| 48 | + 0xbc, 0x6f, 0x35, 0xf6, 0x05, 0xc6, 0x75, 0x4c, 0xfe, |
| 49 | + 0xad, 0x57, 0xcf, 0x83, 0x87, 0x63, 0x9d, 0x3b, 0x40, |
| 50 | + 0x96, 0xc5, 0x4f, 0x18, 0xf4, 0x00, 0x00, 0x00, 0x00, |
| 51 | + }; |
| 52 | + const unsigned char spend_pubkey[33] = { |
| 53 | + 0x02,0xee,0x97,0xdf,0x83,0xb2,0x54,0x6a, |
| 54 | + 0xf5,0xa7,0xd0,0x62,0x15,0xd9,0x8b,0xcb, |
| 55 | + 0x63,0x7f,0xe0,0x5d,0xd0,0xfa,0x37,0x3b, |
| 56 | + 0xd8,0x20,0xe6,0x64,0xd3,0x72,0xde,0x9a,0x01 |
| 57 | + }; |
| 58 | + const unsigned char scan_key[32] = { |
| 59 | + 0xa8,0x90,0x54,0xc9,0x5b,0xe3,0xc3,0x01, |
| 60 | + 0x56,0x65,0x74,0xf2,0xaa,0x93,0xad,0xe0, |
| 61 | + 0x51,0x85,0x09,0x03,0xa6,0x9c,0xbd,0xd1, |
| 62 | + 0xd4,0x7e,0xae,0x26,0x3d,0x7b,0xc0,0x31 |
| 63 | + }; |
| 64 | + secp256k1_keypair input_keypair; |
| 65 | + secp256k1_pubkey input_pubkey; |
| 66 | + size_t pubkeylen = 33; |
| 67 | + |
| 68 | + for (i = 0; i < 32; i++) { |
| 69 | + data->scalar[i] = i + 1; |
| 70 | + } |
| 71 | + for (i = 0; i < 2; i++) { |
| 72 | + CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &data->tx_outputs[i], tx_outputs[i])); |
| 73 | + } |
| 74 | + /* Create the first input public key from the scalar. |
| 75 | + * This input is also used to create the serialized public data object for the light client |
| 76 | + */ |
| 77 | + CHECK(secp256k1_keypair_create(data->ctx, &input_keypair, data->scalar)); |
| 78 | + CHECK(secp256k1_keypair_pub(data->ctx, &input_pubkey, &input_keypair)); |
| 79 | + CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->input_pubkey33, &pubkeylen, &input_pubkey, SECP256K1_EC_COMPRESSED)); |
| 80 | + /* Create the input public keys for the full scan */ |
| 81 | + CHECK(secp256k1_keypair_xonly_pub(data->ctx, &data->tx_inputs[0], NULL, &input_keypair)); |
| 82 | + CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &data->tx_inputs[1], static_tx_input)); |
| 83 | + CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->spend_pubkey, spend_pubkey, pubkeylen)); |
| 84 | + memcpy(data->scan_key, scan_key, 32); |
| 85 | + memcpy(data->smallest_outpoint, smallest_outpoint, 36); |
| 86 | +} |
| 87 | + |
| 88 | +static void bench_silentpayments_output_scan(void* arg, int iters) { |
| 89 | + int i, k = 0; |
| 90 | + bench_silentpayments_data *data = (bench_silentpayments_data*)arg; |
| 91 | + secp256k1_silentpayments_recipient_public_data public_data; |
| 92 | + |
| 93 | + for (i = 0; i < iters; i++) { |
| 94 | + unsigned char shared_secret[33]; |
| 95 | + secp256k1_xonly_pubkey xonly_output; |
| 96 | + CHECK(secp256k1_silentpayments_recipient_public_data_parse(data->ctx, &public_data, data->input_pubkey33)); |
| 97 | + CHECK(secp256k1_silentpayments_recipient_create_shared_secret(data->ctx, |
| 98 | + shared_secret, |
| 99 | + data->scan_key, |
| 100 | + &public_data |
| 101 | + )); |
| 102 | + CHECK(secp256k1_silentpayments_recipient_create_output_pubkey(data->ctx, |
| 103 | + &xonly_output, |
| 104 | + shared_secret, |
| 105 | + &data->spend_pubkey, |
| 106 | + k |
| 107 | + )); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +static void bench_silentpayments_full_tx_scan(void* arg, int iters) { |
| 112 | + int i; |
| 113 | + size_t n_found = 0; |
| 114 | + secp256k1_silentpayments_found_output *found_output_ptrs[2]; |
| 115 | + const secp256k1_xonly_pubkey *tx_output_ptrs[2]; |
| 116 | + const secp256k1_xonly_pubkey *tx_input_ptrs[2]; |
| 117 | + bench_silentpayments_data *data = (bench_silentpayments_data*)arg; |
| 118 | + secp256k1_silentpayments_recipient_public_data public_data; |
| 119 | + |
| 120 | + for (i = 0; i < 2; i++) { |
| 121 | + found_output_ptrs[i] = &data->found_outputs[i]; |
| 122 | + tx_output_ptrs[i] = &data->tx_outputs[i]; |
| 123 | + tx_input_ptrs[i] = &data->tx_inputs[i]; |
| 124 | + } |
| 125 | + for (i = 0; i < iters; i++) { |
| 126 | + CHECK(secp256k1_silentpayments_recipient_public_data_create(data->ctx, |
| 127 | + &public_data, |
| 128 | + data->smallest_outpoint, |
| 129 | + tx_input_ptrs, 2, |
| 130 | + NULL, 0 |
| 131 | + )); |
| 132 | + CHECK(secp256k1_silentpayments_recipient_scan_outputs(data->ctx, |
| 133 | + found_output_ptrs, &n_found, |
| 134 | + tx_output_ptrs, 2, |
| 135 | + data->scan_key, |
| 136 | + &public_data, |
| 137 | + &data->spend_pubkey, |
| 138 | + label_lookup, label_cache) |
| 139 | + ); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +static void run_silentpayments_bench(int iters, int argc, char** argv) { |
| 144 | + bench_silentpayments_data data; |
| 145 | + int d = argc == 1; |
| 146 | + |
| 147 | + /* create a context with no capabilities */ |
| 148 | + data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT); |
| 149 | + |
| 150 | + if (d || have_flag(argc, argv, "silentpayments")) run_benchmark("silentpayments_full_tx_scan", bench_silentpayments_full_tx_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters); |
| 151 | + if (d || have_flag(argc, argv, "silentpayments")) run_benchmark("silentpayments_output_scan", bench_silentpayments_output_scan, bench_silentpayments_scan_setup, NULL, &data, 10, iters); |
| 152 | + |
| 153 | + secp256k1_context_destroy(data.ctx); |
| 154 | +} |
| 155 | + |
| 156 | +#endif /* SECP256K1_MODULE_SILENTPAYMENTS_BENCH_H */ |
0 commit comments