6 /* #define ED25519_DLL */
7 #include "src/ed25519.h"
14 unsigned char public_key[32], private_key[64], seed[32], scalar[32];
15 unsigned char other_public_key[32], other_private_key[64];
16 unsigned char shared_secret[32], other_shared_secret[32];
17 unsigned char signature[64];
23 const unsigned char message[] = "Hello, world!";
24 const int message_len = strlen((char*) message);
26 /* create a random seed, and a keypair out of that seed */
27 ed25519_create_seed(seed);
28 ed25519_create_keypair(public_key, private_key, seed);
30 /* create signature on the message with the keypair */
31 ed25519_sign(signature, message, message_len, public_key, private_key);
33 /* verify the signature */
34 if (ed25519_verify(signature, message, message_len, public_key)) {
35 printf("valid signature\n");
37 printf("invalid signature\n");
40 /* create scalar and add it to the keypair */
41 ed25519_create_seed(scalar);
42 ed25519_add_scalar(public_key, private_key, scalar);
44 /* create signature with the new keypair */
45 ed25519_sign(signature, message, message_len, public_key, private_key);
47 /* verify the signature with the new keypair */
48 if (ed25519_verify(signature, message, message_len, public_key)) {
49 printf("valid signature\n");
51 printf("invalid signature\n");
54 /* make a slight adjustment and verify again */
55 signature[44] ^= 0x10;
56 if (ed25519_verify(signature, message, message_len, public_key)) {
57 printf("did not detect signature change\n");
59 printf("correctly detected signature change\n");
62 /* generate two keypairs for testing key exchange */
63 ed25519_create_seed(seed);
64 ed25519_create_keypair(public_key, private_key, seed);
65 ed25519_create_seed(seed);
66 ed25519_create_keypair(other_public_key, other_private_key, seed);
68 /* create two shared secrets - from both perspectives - and check if they're equal */
69 ed25519_key_exchange(shared_secret, other_public_key, private_key);
70 ed25519_key_exchange(other_shared_secret, public_key, other_private_key);
72 for (i = 0; i < 32; ++i) {
73 if (shared_secret[i] != other_shared_secret[i]) {
74 printf("key exchange was incorrect\n");
80 printf("key exchange was correct\n");
83 /* test performance */
84 printf("testing seed generation performance: ");
86 for (i = 0; i < 10000; ++i) {
87 ed25519_create_seed(seed);
91 printf("%fus per seed\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
94 printf("testing key generation performance: ");
96 for (i = 0; i < 10000; ++i) {
97 ed25519_create_keypair(public_key, private_key, seed);
101 printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
103 printf("testing sign performance: ");
105 for (i = 0; i < 10000; ++i) {
106 ed25519_sign(signature, message, message_len, public_key, private_key);
110 printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
112 printf("testing verify performance: ");
114 for (i = 0; i < 10000; ++i) {
115 ed25519_verify(signature, message, message_len, public_key);
119 printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
122 printf("testing keypair scalar addition performance: ");
124 for (i = 0; i < 10000; ++i) {
125 ed25519_add_scalar(public_key, private_key, scalar);
129 printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
131 printf("testing public key scalar addition performance: ");
133 for (i = 0; i < 10000; ++i) {
134 ed25519_add_scalar(public_key, NULL, scalar);
138 printf("%fus per key\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
140 printf("testing key exchange performance: ");
142 for (i = 0; i < 10000; ++i) {
143 ed25519_key_exchange(shared_secret, other_public_key, private_key);
147 printf("%fus per shared secret\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);