1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <time.h>
5
6 /* #define ED25519_DLL */
7 #include "src/ed25519.h"
8
9 #include "src/ge.h"
10 #include "src/sc.h"
11
12
main()13 int main() {
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];
18
19 clock_t start;
20 clock_t end;
21 int i;
22
23 const unsigned char message[] = "Hello, world!";
24 const int message_len = strlen((char*) message);
25
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);
29
30 /* create signature on the message with the keypair */
31 ed25519_sign(signature, message, message_len, public_key, private_key);
32
33 /* verify the signature */
34 if (ed25519_verify(signature, message, message_len, public_key)) {
35 printf("valid signature\n");
36 } else {
37 printf("invalid signature\n");
38 }
39
40 /* create scalar and add it to the keypair */
41 ed25519_create_seed(scalar);
42 ed25519_add_scalar(public_key, private_key, scalar);
43
44 /* create signature with the new keypair */
45 ed25519_sign(signature, message, message_len, public_key, private_key);
46
47 /* verify the signature with the new keypair */
48 if (ed25519_verify(signature, message, message_len, public_key)) {
49 printf("valid signature\n");
50 } else {
51 printf("invalid signature\n");
52 }
53
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");
58 } else {
59 printf("correctly detected signature change\n");
60 }
61
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);
67
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);
71
72 for (i = 0; i < 32; ++i) {
73 if (shared_secret[i] != other_shared_secret[i]) {
74 printf("key exchange was incorrect\n");
75 break;
76 }
77 }
78
79 if (i == 32) {
80 printf("key exchange was correct\n");
81 }
82
83 /* test performance */
84 printf("testing seed generation performance: ");
85 start = clock();
86 for (i = 0; i < 10000; ++i) {
87 ed25519_create_seed(seed);
88 }
89 end = clock();
90
91 printf("%fus per seed\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
92
93
94 printf("testing key generation performance: ");
95 start = clock();
96 for (i = 0; i < 10000; ++i) {
97 ed25519_create_keypair(public_key, private_key, seed);
98 }
99 end = clock();
100
101 printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
102
103 printf("testing sign performance: ");
104 start = clock();
105 for (i = 0; i < 10000; ++i) {
106 ed25519_sign(signature, message, message_len, public_key, private_key);
107 }
108 end = clock();
109
110 printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
111
112 printf("testing verify performance: ");
113 start = clock();
114 for (i = 0; i < 10000; ++i) {
115 ed25519_verify(signature, message, message_len, public_key);
116 }
117 end = clock();
118
119 printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
120
121
122 printf("testing keypair scalar addition performance: ");
123 start = clock();
124 for (i = 0; i < 10000; ++i) {
125 ed25519_add_scalar(public_key, private_key, scalar);
126 }
127 end = clock();
128
129 printf("%fus per keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
130
131 printf("testing public key scalar addition performance: ");
132 start = clock();
133 for (i = 0; i < 10000; ++i) {
134 ed25519_add_scalar(public_key, NULL, scalar);
135 }
136 end = clock();
137
138 printf("%fus per key\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
139
140 printf("testing key exchange performance: ");
141 start = clock();
142 for (i = 0; i < 10000; ++i) {
143 ed25519_key_exchange(shared_secret, other_public_key, private_key);
144 }
145 end = clock();
146
147 printf("%fus per shared secret\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
148
149 return 0;
150 }
151