Fixed bug in nip44.c, was an error in ecdh_shared_secret. Added comments

This commit is contained in:
2025-08-17 11:29:07 -04:00
parent d8b342ca3f
commit 3ebfdc06c0
14 changed files with 15 additions and 69 deletions

View File

@@ -174,14 +174,12 @@ int ecdh_shared_secret(const unsigned char* private_key,
* nostr_cleanup();
*/
printf("[DEBUG] ecdh_shared_secret: Starting ECDH computation\n");
if (!private_key || !public_key_x || !shared_secret) {
printf("[DEBUG] ecdh_shared_secret: Invalid input - null pointer detected\n");
return -1;
}
printf("[DEBUG] ecdh_shared_secret: Input validation passed\n");
// NIP-04 ECDH: The key insight from the specification is that NOSTR requires
// "only the X coordinate of the shared point is used as the secret and it is NOT hashed"
@@ -196,21 +194,11 @@ int ecdh_shared_secret(const unsigned char* private_key,
compressed_pubkey[0] = 0x02;
memcpy(compressed_pubkey + 1, public_key_x, 32);
printf("[DEBUG] ecdh_shared_secret: Trying 0x02 prefix (even y)\n");
if (nostr_secp256k1_ec_pubkey_parse(&pubkey, compressed_pubkey, 33) == 1) {
printf("[DEBUG] ecdh_shared_secret: 0x02 prefix worked, public key parsed successfully\n");
// Perform ECDH with our custom hash function that copies the X coordinate
printf("[DEBUG] ecdh_shared_secret: Performing ECDH operation with 0x02 prefix\n");
if (nostr_secp256k1_ecdh(shared_secret, &pubkey, private_key, ecdh_hash_function_copy_x, NULL) == 1) {
printf("[DEBUG] ecdh_shared_secret: ECDH operation completed successfully\n");
return 0;
} else {
printf("[DEBUG] ecdh_shared_secret: ECDH operation failed with 0x02 prefix\n");
}
} else {
printf("[DEBUG] ecdh_shared_secret: 0x02 prefix failed, trying 0x03 prefix (odd y)\n");
}
// Try with 0x03 prefix (odd y-coordinate)
@@ -218,21 +206,12 @@ int ecdh_shared_secret(const unsigned char* private_key,
// public_key_x is already copied above
if (nostr_secp256k1_ec_pubkey_parse(&pubkey, compressed_pubkey, 33) == 1) {
printf("[DEBUG] ecdh_shared_secret: 0x03 prefix worked, public key parsed successfully\n");
// Perform ECDH with our custom hash function that copies the X coordinate
printf("[DEBUG] ecdh_shared_secret: Performing ECDH operation with 0x03 prefix\n");
if (nostr_secp256k1_ecdh(shared_secret, &pubkey, private_key, ecdh_hash_function_copy_x, NULL) == 1) {
printf("[DEBUG] ecdh_shared_secret: ECDH operation completed successfully\n");
return 0;
} else {
printf("[DEBUG] ecdh_shared_secret: ECDH operation failed with 0x03 prefix\n");
}
} else {
printf("[DEBUG] ecdh_shared_secret: Both 0x02 and 0x03 prefixes failed - invalid public key\n");
}
printf("[DEBUG] ecdh_shared_secret: All attempts failed\n");
return -1;
}