1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 23:38:09 +00:00

add speed benchmark (secp256k1 vs ed25519)

This commit is contained in:
Pavol Rusnak 2016-04-26 02:14:47 +02:00
parent 91c64858d0
commit 5c4e131ada
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 70 additions and 6 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
*.exe
*~
test-openssl
test_speed
tests
build-*/
build/

View File

@ -26,6 +26,8 @@ CFLAGS += $(OPTFLAGS) \
CFLAGS += -Wno-sequence-point
CFLAGS += -DED25519_CUSTOMRANDOM=1
CFLAGS += -DED25519_CUSTOMHASH=1
CFLAGS += -DED25519_NO_INLINE_ASM
CFLAGS += -DED25519_FORCE_32BIT=1
CFLAGS += -Ied25519-donna -I.
# disable certain optimizations and features when small footprint is required
@ -44,7 +46,7 @@ OBJS = $(SRCS:.c=.o)
TESTLIBS = -lcheck -lrt -lpthread -lm
TESTSSLLIBS = -lcrypto
all: tests test-openssl libtrezor-crypto.so
all: tests test-openssl libtrezor-crypto.so test_speed
%.o: %.c %.h options.h
$(CC) $(CFLAGS) -o $@ -c $<
@ -52,6 +54,9 @@ all: tests test-openssl libtrezor-crypto.so
tests: tests.o $(OBJS)
$(CC) tests.o $(OBJS) $(TESTLIBS) -o tests
test_speed: test_speed.o $(OBJS)
$(CC) test_speed.o $(OBJS) $(TESTLIBS) -o test_speed
test-openssl: test-openssl.o $(OBJS)
$(CC) test-openssl.o $(OBJS) $(TESTSSLLIBS) -o test-openssl
@ -59,4 +64,4 @@ libtrezor-crypto.so: $(SRCS)
$(CC) $(CFLAGS) -fPIC -shared $(SRCS) -o libtrezor-crypto.so
clean:
rm -f *.o ed25519-donna/*.o tests test-openssl libtrezor-crypto.so
rm -f *.o ed25519-donna/*.o tests test_speed test-openssl libtrezor-crypto.so

60
test_speed.c Normal file
View File

@ -0,0 +1,60 @@
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdint.h>
#include "curves.h"
#include "ecdsa.h"
#include "secp256k1.h"
#include "ed25519.h"
uint8_t msg[32];
void prepare_msg(void)
{
for (size_t i = 0; i < sizeof(msg); i++) {
msg[i] = i * 1103515245;
}
}
void bench_secp256k1(void) {
uint8_t sig[64], pub[33], priv[32], pby;
const ecdsa_curve *curve = &secp256k1;
memcpy(priv, "\xc5\x5e\xce\x85\x8b\x0d\xdd\x52\x63\xf9\x68\x10\xfe\x14\x43\x7c\xd3\xb5\xe1\xfb\xd7\xc6\xa2\xec\x1e\x03\x1f\x05\xe8\x6d\x8b\xd5", 32);
ecdsa_get_public_key33(curve, priv, pub);
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby);
clock_t t = clock();
for (int i = 0 ; i < 500; i++) {
int res = ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
assert(res == 0);
}
printf("SECP256k1 verifying speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void bench_ed25519(void) {
ed25519_public_key pk;
ed25519_secret_key sk;
ed25519_signature sig;
memcpy(pk, "\xc5\x5e\xce\x85\x8b\x0d\xdd\x52\x63\xf9\x68\x10\xfe\x14\x43\x7c\xd3\xb5\xe1\xfb\xd7\xc6\xa2\xec\x1e\x03\x1f\x05\xe8\x6d\x8b\xd5", 32);
ed25519_publickey(sk, pk);
ed25519_sign(msg, sizeof(msg), sk, pk, sig);
clock_t t = clock();
for (int i = 0 ; i < 500; i++) {
int res = ed25519_sign_open(msg, sizeof(msg), pk, sig);
assert(res == 0);
}
printf("Ed25519 verifying speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
int main(void) {
prepare_msg();
bench_secp256k1();
bench_ed25519();
return 0;
}

View File

@ -47,8 +47,7 @@ uint8_t *fromhex(const char *str)
{
static uint8_t buf[256];
uint8_t c;
size_t i;
for (i = 0; i < strlen(str) / 2; i++) {
for (size_t i = 0; i < strlen(str) / 2; i++) {
c = 0;
if (str[i*2] >= '0' && str[i*2] <= '9') c += (str[i*2] - '0') << 4;
if (str[i*2] >= 'a' && str[i*2] <= 'f') c += (10 + str[i*2] - 'a') << 4;
@ -65,8 +64,7 @@ char *tohex(const uint8_t *bin, size_t l)
{
char *buf = (char *)malloc(l * 2 + 1);
static char digits[] = "0123456789abcdef";
size_t i;
for (i = 0; i < l; i++) {
for (size_t i = 0; i < l; i++) {
buf[i*2 ] = digits[(bin[i] >> 4) & 0xF];
buf[i*2+1] = digits[bin[i] & 0xF];
}