|
62 | 62 |
|
63 | 63 | #include "btc_helpers.h"
|
64 | 64 |
|
| 65 | +#include <stdint.h> |
| 66 | +#include <string.h> |
| 67 | + |
| 68 | +#include "bignum.h" |
65 | 69 | #include "btc_priv.h"
|
66 | 70 | #include "coin_utils.h"
|
| 71 | +#include "ecdsa.h" |
67 | 72 | #include "flash_config.h"
|
| 73 | +#include "secp256k1.h" |
68 | 74 | #include "segwit_addr.h"
|
| 75 | +#include "sha2.h" |
| 76 | +#include "utils.h" |
69 | 77 |
|
70 | 78 | /*****************************************************************************
|
71 | 79 | * EXTERN VARIABLES
|
|
86 | 94 | /*****************************************************************************
|
87 | 95 | * STATIC VARIABLES
|
88 | 96 | *****************************************************************************/
|
| 97 | +// sha256("TapTweak") used in BIP-340 |
| 98 | +// This is used to derive the taproot address |
| 99 | +static const uint8_t tap_tweak_hash[32] = { |
| 100 | + 232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, |
| 101 | + 57, 193, 67, 198, 62, 66, 156, 188, 235, 21, 217, |
| 102 | + 64, 251, 181, 197, 161, 244, 175, 87, 197, 233}; |
89 | 103 |
|
90 | 104 | /*****************************************************************************
|
91 | 105 | * GLOBAL VARIABLES
|
|
95 | 109 | * STATIC FUNCTIONS
|
96 | 110 | *****************************************************************************/
|
97 | 111 |
|
| 112 | +// Computes sha256(tap_tweak_hash + tap_tweak_hash + x_only_public_key + |
| 113 | +// root_hash) and stores the result in tweak_key_hash. |
| 114 | +static bool bip340_tweak_key_hash(const uint8_t *x_only_public_key, |
| 115 | + const uint8_t *root_hash, |
| 116 | + uint8_t tweak_key_hash[32]) { |
| 117 | + if (NULL == x_only_public_key || NULL == tweak_key_hash) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
| 121 | + size_t payload_size = |
| 122 | + 32 * 3; // tap_tweak_hash + tap_tweak_hash + x_only_public_key |
| 123 | + if (root_hash != NULL) { |
| 124 | + payload_size += 32; |
| 125 | + } |
| 126 | + |
| 127 | + uint8_t payload[payload_size]; |
| 128 | + memzero(payload, payload_size); |
| 129 | + // Prepare the data for hashing |
| 130 | + memcpy(payload, tap_tweak_hash, 32); |
| 131 | + memcpy(payload + 32, tap_tweak_hash, 32); |
| 132 | + memcpy(payload + 64, x_only_public_key, 32); |
| 133 | + if (root_hash != NULL) { |
| 134 | + memcpy(payload + 32 * 3, root_hash, 32); |
| 135 | + } |
| 136 | + |
| 137 | + sha256_Raw(payload, payload_size, tweak_key_hash); |
| 138 | + return true; |
| 139 | +} |
| 140 | + |
| 141 | +// Calculates result = (public_key_point + tweak_key_hash * G).x |
| 142 | +static bool bip340_point_add_tweak(const ecdsa_curve *curve, |
| 143 | + const uint8_t *public_key, |
| 144 | + const uint8_t *tweak_key_hash, |
| 145 | + uint8_t result[32]) { |
| 146 | + if (NULL == public_key || NULL == tweak_key_hash || NULL == result) { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + curve_point public_key_point = {0}; |
| 151 | + if (!ecdsa_read_pubkey(curve, public_key, &public_key_point)) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + // Negate y-coordinate if it's odd |
| 156 | + if (bn_is_odd(&public_key_point.y)) { |
| 157 | + bn_subtract(&curve->prime, &public_key_point.y, &public_key_point.y); |
| 158 | + bn_mod(&public_key_point.y, &curve->prime); |
| 159 | + } |
| 160 | + |
| 161 | + bignum256 tweak_hash_bn; |
| 162 | + bn_read_be(tweak_key_hash, &tweak_hash_bn); |
| 163 | + bn_mod(&tweak_hash_bn, &curve->order); |
| 164 | + |
| 165 | + if (bn_is_zero(&tweak_hash_bn)) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + curve_point result_point = {0}; |
| 170 | + point_multiply(curve, |
| 171 | + &tweak_hash_bn, |
| 172 | + &curve->G, |
| 173 | + &result_point); // result_point = tweak_hash_bn * G |
| 174 | + point_add(curve, |
| 175 | + &public_key_point, |
| 176 | + &result_point); // result_point = public_key_point + result_point |
| 177 | + |
| 178 | + // take the x-coordinate of the result point |
| 179 | + bn_write_be(&result_point.x, result); |
| 180 | + return true; |
| 181 | +} |
| 182 | + |
| 183 | +// implementation of BIP-340 tweak public key for taproot without using |
| 184 | +// secp256k1 library |
| 185 | +static bool bip340_tweak_public_key(const uint8_t *public_key, |
| 186 | + const uint8_t *root_hash, |
| 187 | + uint8_t *tweaked_public_key) { |
| 188 | + if (NULL == public_key || NULL == tweaked_public_key) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + uint8_t tweak_key_hash[32] = {0}; |
| 193 | + if (!bip340_tweak_key_hash(public_key + 1, root_hash, tweak_key_hash)) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + if (!bip340_point_add_tweak( |
| 198 | + &secp256k1, public_key, tweak_key_hash, tweaked_public_key)) { |
| 199 | + return false; |
| 200 | + } |
| 201 | + |
| 202 | + return true; |
| 203 | +} |
| 204 | + |
98 | 205 | /*****************************************************************************
|
99 | 206 | * GLOBAL FUNCTIONS
|
100 | 207 | *****************************************************************************/
|
@@ -208,3 +315,18 @@ void format_value(const uint64_t value_in_sat,
|
208 | 315 | snprintf(
|
209 | 316 | msg, msg_len, "%0.*f %s", precision, fee_in_btc, g_btc_app->lunit_name);
|
210 | 317 | }
|
| 318 | + |
| 319 | +int btc_get_taproot_address(uint8_t *public_key, |
| 320 | + const char *hrp, |
| 321 | + char *address) { |
| 322 | + if (NULL == public_key || NULL == hrp || NULL == address) { |
| 323 | + return 0; |
| 324 | + } |
| 325 | + |
| 326 | + uint8_t tweaked_public_key[32] = {0}; |
| 327 | + if (!bip340_tweak_public_key(public_key, NULL, tweaked_public_key)) { |
| 328 | + return 0; |
| 329 | + } |
| 330 | + |
| 331 | + return segwit_addr_encode(address, hrp, 1, tweaked_public_key, 32); |
| 332 | +} |
0 commit comments