Debian Codegraph @

test/test_conversion_uniqueness.c

raw symbols SCIP
1 #include <stdbool.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <sodium.h>
6 #include <xeddsa.h>
7
8 #include <test.h>
9
10 #define NUM_KEYS 8192
11
12 int test_conversion_uniqueness() {
13 uint8_t curve_priv[32], curve_pub_original[32], curve_pub_converted[32];
14 uint8_t ed_seed[32], ed_pub_original[32], ed_pub_converted[32];
15
16 bool failed = false;
17
18 // Test on a set of different keys
19 for (uint32_t i = 0; i < NUM_KEYS; i++) {
20 // Generate a Curve25519 key pair (the private key is not used)
21 randombytes_buf(curve_priv, 32);
22 priv_to_curve25519_pub(curve_pub_original, curve_priv);
23
24 // Convert the Curve25519 public key to an Ed25519 public key
25 curve25519_pub_to_ed25519_pub(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_pub(curve_pub_converted, ed_pub_converted) != 0) {
29 fprintf(stderr, "Public key conversion from Ed25519 to Curve25519 failed.\n");
30 failed = true;
31 } else {
32 // Check whether the converted Curve25519 public key is equal to the original
33 if (memcmp(curve_pub_original, curve_pub_converted, 32) != 0) {
34 fprintf(stderr, "Converted Curve25519 public key does not match the original.\n");
35 failed = true;
36 }
37 }
38
39 // Generate an Ed25519 key pair (the private key is not used)
40 randombytes_buf(ed_seed, 32);
41 seed_to_ed25519_pub(ed_pub_original, ed_seed);
42
43 // Convert the Ed25519 public key to a Curve25519 public key
44 if (ed25519_pub_to_curve25519_pub(curve_pub_converted, ed_pub_original) != 0) {
45 fprintf(stderr, "Public key conversion from Ed25519 to Curve25519 failed.\n");
46 failed = true;
47 } else {
48 // Convert the Curve25519 public key back into an Ed25519 public key. Set the sign accordingly.
49 curve25519_pub_to_ed25519_pub(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 (memcmp(ed_pub_original, ed_pub_converted, 32) != 0) {
53 fprintf(stderr, "Converted Ed25519 public key does not match the original.\n");
54 failed = true;
55 }
56 }
57 }
58
59 return failed ? -1 : 0;
60 }