diff --git a/bt_secp256k1_i128.h b/bt_secp256k1_i128.h index 1b06588..1af984c 100644 --- a/bt_secp256k1_i128.h +++ b/bt_secp256k1_i128.h @@ -36753,7 +36753,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 * /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 * by using the correct tagged hash function. */ -static const unsigned char bip340_algo[13] = "BIP0340/nonce"; +static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce"; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; @@ -36789,7 +36789,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms /* Tag the hash with algo which is important to avoid nonce reuse across * algorithms. If this nonce function is used in BIP-340 signing as defined * in the spec, an optimized tagging implementation is used. */ - if (algolen == sizeof(bip340_algo) + if (algolen == (sizeof(bip340_algo) - 1) && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { secp256k1_nonce_function_bip340_sha256_tagged(&sha); } else { @@ -36869,7 +36869,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi secp256k1_scalar_get_b32(seckey, &sk); secp256k1_fe_get_b32(pk_buf, &pk.x); - ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); + ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata); secp256k1_scalar_set_b32(&k, buf, NULL); ret &= !secp256k1_scalar_is_zero(&k); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); diff --git a/bt_secp256k1_i128_x86_64_asm.h b/bt_secp256k1_i128_x86_64_asm.h index cf474d9..d338b94 100644 --- a/bt_secp256k1_i128_x86_64_asm.h +++ b/bt_secp256k1_i128_x86_64_asm.h @@ -36981,7 +36981,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 * /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 * by using the correct tagged hash function. */ -static const unsigned char bip340_algo[13] = "BIP0340/nonce"; +static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce"; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; @@ -37017,7 +37017,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms /* Tag the hash with algo which is important to avoid nonce reuse across * algorithms. If this nonce function is used in BIP-340 signing as defined * in the spec, an optimized tagging implementation is used. */ - if (algolen == sizeof(bip340_algo) + if (algolen == (sizeof(bip340_algo) - 1) && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { secp256k1_nonce_function_bip340_sha256_tagged(&sha); } else { @@ -37097,7 +37097,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi secp256k1_scalar_get_b32(seckey, &sk); secp256k1_fe_get_b32(pk_buf, &pk.x); - ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); + ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata); secp256k1_scalar_set_b32(&k, buf, NULL); ret &= !secp256k1_scalar_is_zero(&k); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); diff --git a/bt_secp256k1_i64.h b/bt_secp256k1_i64.h index 3386ba9..d6083fa 100644 --- a/bt_secp256k1_i64.h +++ b/bt_secp256k1_i64.h @@ -36370,7 +36370,7 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 * /* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340 * by using the correct tagged hash function. */ -static const unsigned char bip340_algo[13] = "BIP0340/nonce"; +static const unsigned char bip340_algo[13 + 1] = "BIP0340/nonce"; static const unsigned char schnorrsig_extraparams_magic[4] = SECP256K1_SCHNORRSIG_EXTRAPARAMS_MAGIC; @@ -36406,7 +36406,7 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms /* Tag the hash with algo which is important to avoid nonce reuse across * algorithms. If this nonce function is used in BIP-340 signing as defined * in the spec, an optimized tagging implementation is used. */ - if (algolen == sizeof(bip340_algo) + if (algolen == (sizeof(bip340_algo) - 1) && secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) { secp256k1_nonce_function_bip340_sha256_tagged(&sha); } else { @@ -36486,7 +36486,7 @@ static int secp256k1_schnorrsig_sign_internal(const secp256k1_context* ctx, unsi secp256k1_scalar_get_b32(seckey, &sk); secp256k1_fe_get_b32(pk_buf, &pk.x); - ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata); + ret &= !!noncefp(buf, msg, msglen, seckey, pk_buf, bip340_algo, (sizeof(bip340_algo) - 1), ndata); secp256k1_scalar_set_b32(&k, buf, NULL); ret &= !secp256k1_scalar_is_zero(&k); secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret); diff --git a/bt_secp256k1_metaprogram.cpp b/bt_secp256k1_metaprogram.cpp index bc612c2..311a892 100644 --- a/bt_secp256k1_metaprogram.cpp +++ b/bt_secp256k1_metaprogram.cpp @@ -369,6 +369,16 @@ int main(int argc, char *argv[]) file.buffer = StringReplace(file.buffer, STRING("secp256k1_sha256_write(&sha, data, 32)"), STRING("secp256k1_sha256_write(&sha, (const unsigned char *)data, 32)")); + + // NOTE: C++ requires that a string array initialised by a literal has a space for the null-terminator + file.buffer = StringReplace(file.buffer, + STRING("unsigned char bip340_algo[13] ="), + STRING("unsigned char bip340_algo[13 + 1] =")); + + // NOTE: Code that relied on sizeof(bip340_algo) must be adjusted to account for the null-terminator now + file.buffer = StringReplace(file.buffer, + STRING("sizeof(bip340_algo)"), + STRING("(sizeof(bip340_algo) - 1)")); } } } @@ -759,7 +769,7 @@ int main(int argc, char *argv[]) // NOTE: Misc patches to source code { // NOTE: Delete any Windows style new-lines if there were any - // buffer = StringReplace(buffer, STRING("\r"), STRING("")); + buffer = StringReplace(buffer, STRING("\r"), STRING("")); } // NOTE: Output file