ed25519/test.c

100 lines
2.9 KiB
C
Raw Normal View History

2013-01-11 18:54:40 -05:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
2013-01-19 18:08:14 -05:00
2013-02-04 11:56:19 -05:00
//#define ED25519_DLL
2013-01-22 04:58:57 -05:00
#include "src/ed25519.h"
2013-01-21 16:28:34 -05:00
2013-02-04 11:56:19 -05:00
#include "src/ge.h"
#include "src/sc.h"
const char message[] = "Hello, world!";
2013-01-11 18:54:40 -05:00
2013-02-04 11:56:19 -05:00
2013-01-11 18:54:40 -05:00
int main(int argc, char *argv[]) {
2013-02-04 11:56:19 -05:00
unsigned char public_key[32], private_key[64], seed[32], scalar[32];
unsigned char signature[64];
2013-01-19 18:08:14 -05:00
clock_t start;
clock_t end;
int i;
/* create a random seed, and a keypair out of that seed */
2013-01-11 18:54:40 -05:00
ed25519_create_seed(seed);
ed25519_create_keypair(public_key, private_key, seed);
2013-01-11 20:38:34 -05:00
/* create signature on the message with the keypair */
ed25519_sign(signature, message, strlen(message), public_key, private_key);
2013-01-11 20:38:34 -05:00
/* verify the signature */
if (ed25519_verify(signature, message, strlen(message), public_key)) {
printf("valid signature\n");
} else {
printf("invalid signature\n");
2013-01-11 20:38:34 -05:00
}
2013-02-04 11:56:19 -05:00
/* create scalar and add it to the keypair */
ed25519_create_seed(scalar);
ed25519_add_scalar(public_key, private_key, scalar);
/* create signature with the new keypair */
ed25519_sign(signature, message, strlen(message), public_key, private_key);
/* verify the signature with the new keypair */
if (ed25519_verify(signature, message, strlen(message), public_key)) {
printf("valid signature\n");
} else {
printf("invalid signature\n");
}
/* make a slight adjustment and verify again */
signature[44] ^= 0x10;
if (ed25519_verify(signature, message, strlen(message), public_key)) {
printf("did not detect signature change\n");
2013-01-11 20:38:34 -05:00
} else {
printf("correctly detected signature change\n");
2013-01-11 20:38:34 -05:00
}
2013-01-11 18:54:40 -05:00
/* test performance */
printf("testing key generation performance: ");
start = clock();
for (i = 0; i < 10000; ++i) {
ed25519_create_seed(seed);
ed25519_create_keypair(public_key, private_key, seed);
}
end = clock();
printf("%fus per seed and keypair\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
printf("testing sign performance: ");
2013-01-19 18:08:14 -05:00
start = clock();
for (i = 0; i < 10000; ++i) {
ed25519_sign(signature, message, strlen(message), public_key, private_key);
2013-01-19 18:08:14 -05:00
}
end = clock();
printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
2013-01-19 18:08:14 -05:00
printf("testing verify performance: ");
2013-01-19 18:08:14 -05:00
start = clock();
for (i = 0; i < 10000; ++i) {
ed25519_verify(signature, message, strlen(message), public_key);
2013-01-19 18:08:14 -05:00
}
end = clock();
printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
2013-02-04 11:56:19 -05:00
printf("testing scalar addition performance: ");
start = clock();
for (i = 0; i < 10000; ++i) {
ed25519_add_scalar(public_key, private_key, scalar);
}
end = clock();
printf("%fus per signature\n", ((double) ((end - start) * 1000)) / CLOCKS_PER_SEC / i * 1000);
2013-01-19 18:08:14 -05:00
2013-01-11 18:54:40 -05:00
return 0;
}