comparison test for debugging

This commit is contained in:
2025-08-16 17:48:02 -04:00
parent 19452f45c2
commit df23fd618a
12 changed files with 277 additions and 50 deletions

View File

@@ -8,22 +8,7 @@
#include <string.h>
#include "../nostr_core/nip004.h"
#include "../nostr_core/nostr_common.h"
void print_hex(const char* label, const unsigned char* data, size_t len) {
printf("%s: ", label);
for (size_t i = 0; i < len; i++) {
printf("%02x", data[i]);
}
printf("\n");
}
void hex_to_bytes(const char* hex_str, unsigned char* bytes) {
size_t len = strlen(hex_str);
for (size_t i = 0; i < len; i += 2) {
sscanf(hex_str + i, "%2hhx", &bytes[i / 2]);
}
}
#include "../nostr_core/utils.h"
// Simple replacement for strndup which isn't available in C99
char* safe_strndup(const char* s, size_t n) {
@@ -50,10 +35,10 @@ int test_vector_1(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -157,10 +142,10 @@ int test_vector_2(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -255,10 +240,10 @@ int test_vector_3_bidirectional(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -364,10 +349,10 @@ int test_vector_4_random_keys(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -452,10 +437,10 @@ int test_vector_5_long_message(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -540,10 +525,10 @@ int test_vector_6_short_message(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -644,10 +629,10 @@ int test_vector_7_10kb_payload(void) {
// Convert hex keys to bytes
unsigned char sk1[32], sk2[32], pk1[32], pk2[32];
hex_to_bytes(sk1_hex, sk1);
hex_to_bytes(sk2_hex, sk2);
hex_to_bytes(pk1_hex, pk1);
hex_to_bytes(pk2_hex, pk2);
nostr_hex_to_bytes(sk1_hex, sk1, 32);
nostr_hex_to_bytes(sk2_hex, sk2, 32);
nostr_hex_to_bytes(pk1_hex, pk1, 32);
nostr_hex_to_bytes(pk2_hex, pk2, 32);
printf("Input Test Vector:\n");
printf("SK1 (Alice): %s\n", sk1_hex);
@@ -757,11 +742,11 @@ int test_vector_7_10kb_payload(void) {
int main(void) {
printf("=== NIP-04 Encryption Test with Reference Test Vectors ===\n\n");
// Initialize the library
// if (nostr_init() != NOSTR_SUCCESS) {
// printf("ERROR: Failed to initialize NOSTR library\n");
// return 1;
// }
// Initialize the library - REQUIRED for secp256k1 operations
if (nostr_crypto_init() != 0) {
printf("ERROR: Failed to initialize NOSTR crypto library\n");
return 1;
}
int all_passed = 1;
@@ -813,6 +798,7 @@ int main(void) {
printf("❌ SOME TESTS FAILED. Please review the output above.\n");
}
// nostr_cleanup();
// Cleanup crypto resources
nostr_crypto_cleanup();
return all_passed ? 0 : 1;
}