1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00

remove test_{sign,verify}_speed from tests.c, rename tests to test_check.c, rework test_speed

This commit is contained in:
Pavol Rusnak 2017-06-03 23:43:58 +02:00
parent 27c7474856
commit edb31b89a8
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
8 changed files with 156 additions and 191 deletions

4
.gitignore vendored
View File

@ -2,9 +2,9 @@
*.d
*.exe
*~
test-openssl
test_openssl
test_speed
tests
test_check
*.os
*.so
*.pyc

View File

@ -21,9 +21,9 @@ install:
script:
- make
- ./tests
- CK_TIMEOUT_MULTIPLIER=20 valgrind -q --error-exitcode=1 ./tests
- ./test-openssl 1000
- ./test_check
- CK_TIMEOUT_MULTIPLIER=20 valgrind -q --error-exitcode=1 ./test_check
- ./test_openssl 1000
- ITERS=10 py.test
- mkdir _build && cd _build && cmake .. && make && cd ..

View File

@ -19,11 +19,11 @@ endif(MSVC)
# Build trezor-crypto tests (requires OpenSSL)
if (TREZOR_CRYPTO_TESTS)
add_executable(tests tests.c)
target_link_libraries(tests TrezorCrypto check rt pthread m crypto)
add_test(NAME trezor-crypto COMMAND tests)
add_executable(test_check test_check.c)
target_link_libraries(test_check TrezorCrypto check rt pthread m crypto)
add_test(NAME trezor-crypto COMMAND test_check)
add_executable(test-openssl test-openssl.c)
target_link_libraries(test-openssl TrezorCrypto check rt pthread m crypto)
add_test(NAME trezor-crypto-openssl COMMAND test-openssl 100)
add_executable(test_openssl test_openssl.c)
target_link_libraries(test_openssl TrezorCrypto check rt pthread m crypto)
add_test(NAME trezor-crypto-openssl COMMAND test_openssl 100)
endif()

View File

@ -53,19 +53,19 @@ OBJS = $(SRCS:.c=.o)
TESTLIBS = $(shell pkg-config --libs check) -lrt -lpthread -lm
TESTSSLLIBS = -lcrypto
all: tests test-openssl test_speed tools libtrezor-crypto.so
all: test_check test_openssl test_speed tools libtrezor-crypto.so
%.o: %.c %.h options.h
$(CC) $(CFLAGS) -o $@ -c $<
tests: tests.o $(OBJS)
$(CC) tests.o $(OBJS) $(TESTLIBS) -o tests
test_check: test_check.o $(OBJS)
$(CC) test_check.o $(OBJS) $(TESTLIBS) -o test_check
test_speed: test_speed.o $(OBJS)
$(CC) test_speed.o $(OBJS) -o test_speed
test-openssl: test-openssl.o $(OBJS)
$(CC) test-openssl.o $(OBJS) $(TESTSSLLIBS) -o test-openssl
test_openssl: test_openssl.o $(OBJS)
$(CC) test_openssl.o $(OBJS) $(TESTSSLLIBS) -o test_openssl
libtrezor-crypto.so: $(SRCS)
$(CC) $(CFLAGS) -fPIC -shared $(SRCS) -o libtrezor-crypto.so
@ -83,5 +83,5 @@ tools/bip39bruteforce: tools/bip39bruteforce.o $(OBJS)
clean:
rm -f *.o aes/*.o chacha20poly1305/*.o ed25519-donna/*.o
rm -f tests test_speed test-openssl libtrezor-crypto.so
rm -f test_check test_speed test_openssl libtrezor-crypto.so
rm -f tools/*.o tools/xpubaddrgen tools/mktable tools/bip39bruteforce

View File

@ -23,8 +23,8 @@ These include:
- SHA3/Keccak
- BLAKE2s/BLAKE2b
- Chacha20-Poly1305
- unit tests (using Check - check.sf.net; in tests.c)
- tests against OpenSSL (in test-openssl.c)
- unit tests (using Check - check.sf.net; in test_check.c)
- tests against OpenSSL (in test_openssl.c)
Distibuted under MIT License.

View File

@ -953,6 +953,32 @@ START_TEST(test_bip32_compare)
}
END_TEST
START_TEST(test_bip32_optimized)
{
HDNode root;
hdnode_from_seed((uint8_t *)"NothingToSeeHere", 16, SECP256K1_NAME, &root);
hdnode_fill_public_key(&root);
curve_point pub;
ecdsa_read_pubkey(&secp256k1, root.public_key, &pub);
HDNode node;
char addr1[MAX_ADDR_SIZE], addr2[MAX_ADDR_SIZE];
for (int i = 0; i < 40; i++) {
// unoptimized
memcpy(&node, &root, sizeof(HDNode));
hdnode_public_ckd(&node, i);
hdnode_fill_public_key(&node);
ecdsa_get_address(node.public_key, 0, addr1, sizeof(addr1));
// optimized
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, addr2, sizeof(addr2), false);
// check
ck_assert_str_eq(addr1, addr2);
}
}
END_TEST
START_TEST(test_bip32_cache_1)
{
HDNode node1, node2;
@ -1458,118 +1484,6 @@ START_TEST(test_rfc6979)
}
END_TEST
START_TEST(test_sign_speed)
{
uint8_t sig[64], priv_key[32], msg[256];
size_t i;
int res;
for (i = 0; i < sizeof(msg); i++) {
msg[i] = i * 1103515245;
}
const ecdsa_curve *curve = &secp256k1;
clock_t t = clock();
memcpy(priv_key, fromhex("c55ece858b0ddd5263f96810fe14437cd3b5e1fbd7c6a2ec1e031f05e86d8bd5"), 32);
for (i = 0 ; i < 250; i++) {
res = ecdsa_sign(curve, priv_key, msg, sizeof(msg), sig, NULL, NULL);
ck_assert_int_eq(res, 0);
}
memcpy(priv_key, fromhex("509a0382ff5da48e402967a671bdcde70046d07f0df52cff12e8e3883b426a0a"), 32);
for (i = 0 ; i < 250; i++) {
res = ecdsa_sign(curve, priv_key, msg, sizeof(msg), sig, NULL, NULL);
ck_assert_int_eq(res, 0);
}
printf("SECP256k1 signing speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
curve = &nist256p1;
t = clock();
memcpy(priv_key, fromhex("c55ece858b0ddd5263f96810fe14437cd3b5e1fbd7c6a2ec1e031f05e86d8bd5"), 32);
for (i = 0 ; i < 250; i++) {
res = ecdsa_sign(curve, priv_key, msg, sizeof(msg), sig, NULL, NULL);
ck_assert_int_eq(res, 0);
}
memcpy(priv_key, fromhex("509a0382ff5da48e402967a671bdcde70046d07f0df52cff12e8e3883b426a0a"), 32);
for (i = 0 ; i < 250; i++) {
res = ecdsa_sign(curve, priv_key, msg, sizeof(msg), sig, NULL, NULL);
ck_assert_int_eq(res, 0);
}
printf("NIST256p1 signing speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
END_TEST
START_TEST(test_verify_speed)
{
uint8_t sig[64], pub_key33[33], pub_key65[65], msg[256];
size_t i;
int res;
for (i = 0; i < sizeof(msg); i++) {
msg[i] = i * 1103515245;
}
const ecdsa_curve *curve = &secp256k1;
clock_t t = clock();
memcpy(sig, fromhex("88dc0db6bc5efa762e75fbcc802af69b9f1fcdbdffce748d403f687f855556e610ee8035414099ac7d89cff88a3fa246d332dfa3c78d82c801394112dda039c2"), 64);
memcpy(pub_key33, fromhex("024054fd18aeb277aeedea01d3f3986ff4e5be18092a04339dcf4e524e2c0a0974"), 33);
memcpy(pub_key65, fromhex("044054fd18aeb277aeedea01d3f3986ff4e5be18092a04339dcf4e524e2c0a09746c7083ed2097011b1223a17a644e81f59aa3de22dac119fd980b36a8ff29a244"), 65);
for (i = 0 ; i < 25; i++) {
res = ecdsa_verify(curve, pub_key65, sig, msg, sizeof(msg));
ck_assert_int_eq(res, 0);
res = ecdsa_verify(curve, pub_key33, sig, msg, sizeof(msg));
ck_assert_int_eq(res, 0);
}
memcpy(sig, fromhex("067040a2adb3d9deefeef95dae86f69671968a0b90ee72c2eab54369612fd524eb6756c5a1bb662f1175a5fa888763cddc3a07b8a045ef6ab358d8d5d1a9a745"), 64);
memcpy(pub_key33, fromhex("03ff45a5561a76be930358457d113f25fac790794ec70317eff3b97d7080d45719"), 33);
memcpy(pub_key65, fromhex("04ff45a5561a76be930358457d113f25fac790794ec70317eff3b97d7080d457196235193a15778062ddaa44aef7e6901b781763e52147f2504e268b2d572bf197"), 65);
for (i = 0 ; i < 25; i++) {
res = ecdsa_verify(curve, pub_key65, sig, msg, sizeof(msg));
ck_assert_int_eq(res, 0);
res = ecdsa_verify(curve, pub_key33, sig, msg, sizeof(msg));
ck_assert_int_eq(res, 0);
}
printf("SECP256k1 verifying speed: %0.2f sig/s\n", 100.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
curve = &nist256p1;
t = clock();
memcpy(sig, fromhex("7ed26b02c7e9664baaaa1e6ab64b3350c65ff685a7502863f357029c21c92a0dd7f2ae56d229262a8c7e97a0bfe1e7f9fd80c29df97355afa52ca07b507cecb9"), 64);
memcpy(pub_key33, fromhex("035662d692de519699000c7f5f04b2714e6a7f358c567fbcc001107ab86e4a7dea"), 33);
memcpy(pub_key65, fromhex("045662d692de519699000c7f5f04b2714e6a7f358c567fbcc001107ab86e4a7dea40523030e793724473d3cd60e436cbfc0a6e4ac5a5d0b340c5d637c6c870c00f"), 65);
for (i = 0 ; i < 25; i++) {
res = ecdsa_verify(curve, pub_key65, sig, msg, sizeof(msg));
//ck_assert_int_eq(res, 0);
res = ecdsa_verify(curve, pub_key33, sig, msg, sizeof(msg));
//ck_assert_int_eq(res, 0);
}
memcpy(sig, fromhex("6e13956f4227e66264dfcd92cf40b3b253622e96b1e1030392c0b06d9570f725dd1542164a481c0342ceb0b575f3516df536b7b90da57c381dfbd1aac6138c0b"), 64);
memcpy(pub_key33, fromhex("034b17f6b5c42b1be32c09f49056ee793e67f3ff42058123ce72fffc61d7236bc2"), 33);
memcpy(pub_key65, fromhex("044b17f6b5c42b1be32c09f49056ee793e67f3ff42058123ce72fffc61d7236bc29b6c29cbbbb2681d2b2e9c699cde8e591650d02bf4bb577ec53fd229442882e5"), 65);
for (i = 0 ; i < 25; i++) {
res = ecdsa_verify(curve, pub_key65, sig, msg, sizeof(msg));
//ck_assert_int_eq(res, 0);
res = ecdsa_verify(curve, pub_key33, sig, msg, sizeof(msg));
//ck_assert_int_eq(res, 0);
}
printf("NIST256p1 verifying speed: %0.2f sig/s\n", 100.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
END_TEST
// test vectors from http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors
START_TEST(test_aes)
{
@ -3111,6 +3025,7 @@ Suite *test_suite(void)
tcase_add_test(tc, test_bip32_vector_1);
tcase_add_test(tc, test_bip32_vector_2);
tcase_add_test(tc, test_bip32_compare);
tcase_add_test(tc, test_bip32_optimized);
tcase_add_test(tc, test_bip32_cache_1);
tcase_add_test(tc, test_bip32_cache_2);
suite_add_tcase(s, tc);
@ -3142,13 +3057,6 @@ Suite *test_suite(void)
tcase_add_test(tc, test_rfc6979);
suite_add_tcase(s, tc);
if (!RUNNING_ON_VALGRIND) {
tc = tcase_create("speed");
tcase_add_test(tc, test_sign_speed);
tcase_add_test(tc, test_verify_speed);
suite_add_tcase(s, tc);
}
tc = tcase_create("address");
tcase_add_test(tc, test_address);
suite_add_tcase(s, tc);

View File

@ -10,7 +10,7 @@
#include "nist256p1.h"
#include "ed25519.h"
static uint8_t msg[32];
static uint8_t msg[256];
void prepare_msg(void)
{
@ -19,7 +19,47 @@ void prepare_msg(void)
}
}
void bench_secp256k1(void)
void bench_sign_secp256k1(int iterations)
{
uint8_t sig[64], 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);
for (int i = 0 ; i < iterations; i++) {
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
}
}
void bench_sign_nist256p1(int iterations)
{
uint8_t sig[64], priv[32], pby;
const ecdsa_curve *curve = &nist256p1;
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);
for (int i = 0 ; i < iterations; i++) {
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
}
}
void bench_sign_ed25519(int iterations)
{
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);
for (int i = 0 ; i < iterations; i++) {
ed25519_sign(msg, sizeof(msg), sk, pk, sig);
}
}
void bench_verify_secp256k1_33(int iterations)
{
uint8_t sig[64], pub[33], priv[32], pby;
@ -29,15 +69,27 @@ void bench_secp256k1(void)
ecdsa_get_public_key33(curve, priv, pub);
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
clock_t t = clock();
for (int i = 0 ; i < 500; i++) {
int res = ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
assert(res == 0);
for (int i = 0 ; i < iterations; i++) {
ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
}
printf("SECP256k1 verifying speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void bench_nist256p1(void)
void bench_verify_secp256k1_65(int iterations)
{
uint8_t sig[64], pub[65], 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_key65(curve, priv, pub);
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
for (int i = 0 ; i < iterations; i++) {
ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
}
}
void bench_verify_nist256p1_33(int iterations)
{
uint8_t sig[64], pub[33], priv[32], pby;
@ -47,15 +99,27 @@ void bench_nist256p1(void)
ecdsa_get_public_key33(curve, priv, pub);
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
clock_t t = clock();
for (int i = 0 ; i < 500; i++) {
int res = ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
assert(res == 0);
for (int i = 0 ; i < iterations; i++) {
ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
}
printf("NIST256p1 verifying speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void bench_ed25519(void)
void bench_verify_nist256p1_65(int iterations)
{
uint8_t sig[64], pub[65], priv[32], pby;
const ecdsa_curve *curve = &nist256p1;
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_key65(curve, priv, pub);
ecdsa_sign(curve, priv, msg, sizeof(msg), sig, &pby, NULL);
for (int i = 0 ; i < iterations; i++) {
ecdsa_verify(curve, pub, sig, msg, sizeof(msg));
}
}
void bench_verify_ed25519(int iterations)
{
ed25519_public_key pk;
ed25519_secret_key sk;
@ -65,23 +129,12 @@ void bench_ed25519(void)
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);
for (int i = 0 ; i < iterations; i++) {
ed25519_sign_open(msg, sizeof(msg), pk, sig);
}
printf("Ed25519 verifying speed: %0.2f sig/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void test_verify_speed(void)
{
prepare_msg();
bench_secp256k1();
bench_nist256p1();
bench_ed25519();
}
void bench_curve25519(void)
void bench_multiply_curve25519(int iterations)
{
uint8_t result[32];
uint8_t secret[32];
@ -90,16 +143,9 @@ void bench_curve25519(void)
memcpy(secret, "\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);
memcpy(basepoint, "\x96\x47\xda\xbe\x1e\xea\xaf\x25\x47\x1e\x68\x0b\x4d\x7c\x6f\xd1\x14\x38\x76\xbb\x77\x59\xd8\x3d\x0f\xf7\xa2\x49\x08\xfd\xda\xbc", 32);
clock_t t = clock();
for (int i = 0 ; i < 500; i++) {
for (int i = 0 ; i < iterations; i++) {
curve25519_scalarmult(result, secret, basepoint);
}
printf("Curve25519 multiplying speed: %0.2f mul/s\n", 500.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void test_multiply_speed(void)
{
bench_curve25519();
}
static HDNode root;
@ -114,17 +160,12 @@ void bench_ckd_normal(int iterations)
{
char addr[MAX_ADDR_SIZE];
HDNode node;
clock_t t = clock();
for (int i = 0; i < iterations; i++) {
memcpy(&node, &root, sizeof(HDNode));
hdnode_public_ckd(&node, i);
hdnode_fill_public_key(&node);
ecdsa_get_address(node.public_key, 0, addr, sizeof(addr));
if (i == 0 || i == iterations - 1) {
printf("address = %s\n", addr);
}
}
printf("CKD normal speed: %0.2f iter/s\n", iterations / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void bench_ckd_optimized(int iterations)
@ -132,26 +173,42 @@ void bench_ckd_optimized(int iterations)
char addr[MAX_ADDR_SIZE];
curve_point pub;
ecdsa_read_pubkey(&secp256k1, root.public_key, &pub);
clock_t t = clock();
for (int i = 0; i < iterations; i++) {
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0, addr, sizeof(addr), false);
if (i == 0 || i == iterations -1) {
printf("address = %s\n", addr);
}
}
printf("CKD optim speed: %0.2f iter/s\n", iterations / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
void test_ckd_speed(int iterations)
void bench(void (*func)(int), const char *name, int iterations)
{
prepare_node();
bench_ckd_normal(iterations);
bench_ckd_optimized(iterations);
clock_t t = clock();
func(iterations);
float speed = iterations / ((float)(clock() - t) / CLOCKS_PER_SEC);
printf("%25s: %8.2f ops/s\n", name, speed);
}
#define BENCH(FUNC, ITER) bench(FUNC, #FUNC, ITER)
int main(void) {
test_verify_speed();
test_multiply_speed();
test_ckd_speed(1000);
prepare_msg();
BENCH(bench_sign_secp256k1, 500);
BENCH(bench_verify_secp256k1_33, 500);
BENCH(bench_verify_secp256k1_65, 500);
BENCH(bench_sign_nist256p1, 500);
BENCH(bench_verify_nist256p1_33, 500);
BENCH(bench_verify_nist256p1_65, 500);
BENCH(bench_sign_ed25519, 4000);
BENCH(bench_verify_ed25519, 4000);
BENCH(bench_multiply_curve25519, 4000);
prepare_node();
BENCH(bench_ckd_normal, 1000);
BENCH(bench_ckd_optimized, 1000);
return 0;
}