diff options
Diffstat (limited to 'tests/unit/test_nip04.c')
| -rw-r--r-- | tests/unit/test_nip04.c | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/tests/unit/test_nip04.c b/tests/unit/test_nip04.c new file mode 100644 index 0000000..27eb13c --- /dev/null +++ b/tests/unit/test_nip04.c | |||
| @@ -0,0 +1,107 @@ | |||
| 1 | #include "test_framework.h" | ||
| 2 | #include "../../main/nip04.h" | ||
| 3 | #include <secp256k1.h> | ||
| 4 | #include <secp256k1_ecdh.h> | ||
| 5 | #include <secp256k1_extrakeys.h> | ||
| 6 | #include <string.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | #include <stdlib.h> | ||
| 9 | #include <stdbool.h> | ||
| 10 | |||
| 11 | static void test_nip04_roundtrip(void) | ||
| 12 | { | ||
| 13 | printf("\n=== NIP-04 encrypt/decrypt roundtrip ===\n"); | ||
| 14 | |||
| 15 | secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); | ||
| 16 | |||
| 17 | uint8_t alice_sec[32], bob_sec[32]; | ||
| 18 | memset(alice_sec, 0x01, 32); | ||
| 19 | memset(bob_sec, 0x02, 32); | ||
| 20 | |||
| 21 | secp256k1_pubkey alice_pk, bob_pk; | ||
| 22 | secp256k1_ec_pubkey_create(ctx, &alice_pk, alice_sec); | ||
| 23 | secp256k1_ec_pubkey_create(ctx, &bob_pk, bob_sec); | ||
| 24 | |||
| 25 | uint8_t alice_xonly[32], bob_xonly[32]; | ||
| 26 | secp256k1_xonly_pubkey alice_xpk, bob_xpk; | ||
| 27 | secp256k1_xonly_pubkey_from_pubkey(ctx, &alice_xpk, NULL, &alice_pk); | ||
| 28 | secp256k1_xonly_pubkey_from_pubkey(ctx, &bob_xpk, NULL, &bob_pk); | ||
| 29 | secp256k1_xonly_pubkey_serialize(ctx, alice_xonly, &alice_xpk); | ||
| 30 | secp256k1_xonly_pubkey_serialize(ctx, bob_xonly, &bob_xpk); | ||
| 31 | |||
| 32 | const char *message = "Hello, ContextVM! This is a test message."; | ||
| 33 | |||
| 34 | uint8_t ciphertext[4096]; | ||
| 35 | size_t ct_len = 0; | ||
| 36 | nip04_encrypt(alice_sec, bob_xonly, message, ciphertext, &ct_len); | ||
| 37 | ASSERT(ct_len > 0, "encryption produced output"); | ||
| 38 | ASSERT(ct_len > strlen(message), "ciphertext longer than plaintext"); | ||
| 39 | |||
| 40 | char plaintext[2048]; | ||
| 41 | int pt_len = nip04_decrypt(bob_sec, alice_xonly, (const char *)ciphertext, plaintext, sizeof(plaintext)); | ||
| 42 | ASSERT(pt_len > 0, "decryption succeeded"); | ||
| 43 | ASSERT_EQ_STR(message, plaintext, "decrypted message matches original"); | ||
| 44 | |||
| 45 | printf("\n--- Different key produces garbage ---\n"); | ||
| 46 | uint8_t eve_sec[32]; | ||
| 47 | memset(eve_sec, 0x03, 32); | ||
| 48 | pt_len = nip04_decrypt(eve_sec, alice_xonly, (const char *)ciphertext, plaintext, sizeof(plaintext)); | ||
| 49 | if (pt_len > 0) { | ||
| 50 | ASSERT(strcmp(message, plaintext) != 0, "wrong key produces different output"); | ||
| 51 | } else { | ||
| 52 | ASSERT(true, "wrong key fails to decrypt"); | ||
| 53 | } | ||
| 54 | |||
| 55 | printf("\n--- Second roundtrip (different message) ---\n"); | ||
| 56 | const char *msg2 = "Short"; | ||
| 57 | nip04_encrypt(alice_sec, bob_xonly, msg2, ciphertext, &ct_len); | ||
| 58 | pt_len = nip04_decrypt(bob_sec, alice_xonly, (const char *)ciphertext, plaintext, sizeof(plaintext)); | ||
| 59 | ASSERT(pt_len > 0, "short message decrypts"); | ||
| 60 | ASSERT_EQ_STR(msg2, plaintext, "short message matches"); | ||
| 61 | |||
| 62 | printf("\n--- Long message roundtrip ---\n"); | ||
| 63 | char long_msg[512]; | ||
| 64 | memset(long_msg, 'X', 511); | ||
| 65 | long_msg[511] = '\0'; | ||
| 66 | nip04_encrypt(alice_sec, bob_xonly, long_msg, ciphertext, &ct_len); | ||
| 67 | pt_len = nip04_decrypt(bob_sec, alice_xonly, (const char *)ciphertext, plaintext, sizeof(plaintext)); | ||
| 68 | ASSERT(pt_len > 0, "long message decrypts"); | ||
| 69 | ASSERT_EQ_INT(511, pt_len, "long message length matches"); | ||
| 70 | |||
| 71 | printf("\n--- Bob encrypts, Alice decrypts ---\n"); | ||
| 72 | nip04_encrypt(bob_sec, alice_xonly, message, ciphertext, &ct_len); | ||
| 73 | pt_len = nip04_decrypt(alice_sec, bob_xonly, (const char *)ciphertext, plaintext, sizeof(plaintext)); | ||
| 74 | ASSERT(pt_len > 0, "reverse direction decrypts"); | ||
| 75 | ASSERT_EQ_STR(message, plaintext, "reverse direction matches"); | ||
| 76 | |||
| 77 | secp256k1_context_destroy(ctx); | ||
| 78 | } | ||
| 79 | |||
| 80 | static void test_nip04_invalid_input(void) | ||
| 81 | { | ||
| 82 | printf("\n=== NIP-04 invalid inputs ===\n"); | ||
| 83 | char plaintext[256]; | ||
| 84 | int rc = nip04_decrypt(NULL, NULL, "AAAA", plaintext, sizeof(plaintext)); | ||
| 85 | ASSERT(rc < 0, "NULL keys fails"); | ||
| 86 | |||
| 87 | uint8_t dummy_sec[32]; | ||
| 88 | memset(dummy_sec, 0xAA, 32); | ||
| 89 | rc = nip04_decrypt(dummy_sec, NULL, "AAAA", plaintext, sizeof(plaintext)); | ||
| 90 | ASSERT(rc < 0, "NULL pubkey fails"); | ||
| 91 | |||
| 92 | uint8_t dummy_pub[32]; | ||
| 93 | memset(dummy_pub, 0xBB, 32); | ||
| 94 | rc = nip04_decrypt(dummy_sec, dummy_pub, "", plaintext, sizeof(plaintext)); | ||
| 95 | ASSERT(rc < 0, "empty ciphertext fails"); | ||
| 96 | |||
| 97 | rc = nip04_decrypt(dummy_sec, dummy_pub, "AAAA", plaintext, sizeof(plaintext)); | ||
| 98 | ASSERT(rc < 0, "garbage ciphertext fails"); | ||
| 99 | } | ||
| 100 | |||
| 101 | int main(void) | ||
| 102 | { | ||
| 103 | printf("=== test_nip04 ===\n"); | ||
| 104 | test_nip04_roundtrip(); | ||
| 105 | test_nip04_invalid_input(); | ||
| 106 | TEST_SUMMARY(); | ||
| 107 | } | ||