| 1 | #cxx . . $ `<file>/test/test_conversion_uniqueness.c`/include <stdbool.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include <sodium.h> |
| 6 | #include <xeddsa.h>cxx . . $ `<file>/include/xeddsa.h`/ |
| 7 | |
| 8 | #include <test.h>cxx . . $ `<file>/test/test.h`/ |
| 9 | |
| 10 | #define NUM_KEYScxx . . $ `test/test_conversion_uniqueness.c:10:9`! 8192 |
| 11 | |
| 12 | int test_conversion_uniquenesscxx . . $ test_conversion_uniqueness(d1b40a211a1a4e4c).() { |
| 13 | uint8_tcxx . . $ uint8_t# curve_priv[32], curve_pub_original[32], curve_pub_converted[32]; |
| 14 | uint8_tcxx . . $ uint8_t# ed_seed[32], ed_pub_original[32], ed_pub_converted[32]; |
| 15 | |
| 16 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! failed = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 17 | |
| 18 | // Test on a set of different keys |
| 19 | for (uint32_tcxx . . $ uint32_t# i = 0; i < NUM_KEYScxx . . $ `test/test_conversion_uniqueness.c:10:9`!; i++) { |
| 20 | // Generate a Curve25519 key pair (the private key is not used) |
| 21 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(curve_priv, 32); |
| 22 | priv_to_curve25519_pubcxx . . $ priv_to_curve25519_pub(225f8250db12d4ac).(curve_pub_original, curve_priv); |
| 23 | |
| 24 | // Convert the Curve25519 public key to an Ed25519 public key |
| 25 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub_converted, curve_pub_original, 0); |
| 26 | |
| 27 | // Convert the Ed25519 public key back into a Curve25519 public key |
| 28 | if (ed25519_pub_to_curve25519_pubcxx . . $ ed25519_pub_to_curve25519_pub(1ff552b5f61cf5ec).(curve_pub_converted, ed_pub_converted) != 0) { |
| 29 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Public key conversion from Ed25519 to Curve25519 failed.\n"); |
| 30 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 31 | } else { |
| 32 | // Check whether the converted Curve25519 public key is equal to the original |
| 33 | if (memcmpcxx . . $ memcmp(36f949851efa3055).(curve_pub_original, curve_pub_converted, 32) != 0) { |
| 34 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Converted Curve25519 public key does not match the original.\n"); |
| 35 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Generate an Ed25519 key pair (the private key is not used) |
| 40 | randombytes_bufcxx . . $ randombytes_buf(5500c4293b1bbef0).(ed_seed, 32); |
| 41 | seed_to_ed25519_pubcxx . . $ seed_to_ed25519_pub(225f8250db12d4ac).(ed_pub_original, ed_seed); |
| 42 | |
| 43 | // Convert the Ed25519 public key to a Curve25519 public key |
| 44 | if (ed25519_pub_to_curve25519_pubcxx . . $ ed25519_pub_to_curve25519_pub(1ff552b5f61cf5ec).(curve_pub_converted, ed_pub_original) != 0) { |
| 45 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Public key conversion from Ed25519 to Curve25519 failed.\n"); |
| 46 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 47 | } else { |
| 48 | // Convert the Curve25519 public key back into an Ed25519 public key. Set the sign accordingly. |
| 49 | curve25519_pub_to_ed25519_pubcxx . . $ curve25519_pub_to_ed25519_pub(1f7a79e8cc347955).(ed_pub_converted, curve_pub_converted, ed_pub_original[31] & 0x80); |
| 50 | |
| 51 | // Check whether the converted Ed25519 public key is equal to the original |
| 52 | if (memcmpcxx . . $ memcmp(36f949851efa3055).(ed_pub_original, ed_pub_converted, 32) != 0) { |
| 53 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Converted Ed25519 public key does not match the original.\n"); |
| 54 | failed = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return failed ? -1 : 0; |
| 60 | } |