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

move speed tests to unit testing suite

This commit is contained in:
Pavol Rusnak 2013-09-22 12:42:35 +02:00
parent 2df62d4877
commit 58a65d9cd7
6 changed files with 57 additions and 73 deletions

4
.gitignore vendored
View File

@ -1,5 +1,3 @@
*.o
test-rfc6979
test-speed
test-verify
test-openssl
tests

View File

@ -2,7 +2,7 @@ CC = gcc
CFLAGS = -Wall -Os
OBJS = bignum.o ecdsa.o secp256k1.o sha2.o rand.o hmac.o bip32.o ripemd160.o
all: tests test-speed test-verify
all: tests test-openssl
%.o: %.c %.h
$(CC) $(CFLAGS) -o $@ -c $<
@ -10,11 +10,8 @@ all: tests test-speed test-verify
tests: tests.o $(OBJS)
gcc tests.o $(OBJS) -lcheck -o tests
test-speed: test-speed.o $(OBJS)
gcc test-speed.o $(OBJS) -o test-speed
test-verify: test-verify.o $(OBJS)
gcc test-verify.o $(OBJS) -o test-verify -lcrypto
test-openssl: test-openssl.o $(OBJS)
gcc test-openssl.o $(OBJS) -o test-openssl -lcrypto
clean:
rm -f *.o tests test-speed test-verify
rm -f *.o tests test-openssl

View File

@ -249,7 +249,7 @@ void generate_k_rfc6979(bignum256 *secret, const uint8_t *priv_key, const uint8_
// sig_len is the pointer to a uint that will contain resulting signature length. note that ((*sig_len) == sig[1]+2)
void ecdsa_sign(const uint8_t *priv_key, const uint8_t *msg, uint32_t msg_len, uint8_t *sig, uint32_t *sig_len)
{
int i;
uint32_t i;
uint8_t hash[32];
curve_point R;
bignum256 k, z;

View File

@ -1,61 +0,0 @@
/**
* Copyright (c) 2013 Tomas Dzetkulic
* Copyright (c) 2013 Pavol Rusnak
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <time.h>
#include "ecdsa.h"
#include "rand.h"
int main()
{
uint8_t sig[70], priv_key[32], msg[256];
uint32_t sig_len, i, msg_len;
int cnt = 0;
init_rand();
// random message len between 1 and 256
msg_len = (random32() & 0xFF) + 1;
// create random message
for (i = 0; i < msg_len; i++) {
msg[i] = random32() & 0xFF;
}
// create random privkey
for (i = 0; i < 8; i++) {
uint32_t r = random32();
priv_key[4 * i ] = r & 0xFF;
priv_key[4 * i + 1] = (r >> 8) & 0xFF;
priv_key[4 * i + 2] = (r >> 16) & 0xFF;
priv_key[4 * i + 3] = (r >> 24) & 0xFF;
}
clock_t t = clock();
for (;;) {
// use our ECDSA signer to sign the message with the key
ecdsa_sign(priv_key, msg, msg_len, sig, &sig_len);
cnt++;
if ((cnt % 100) == 0) printf("Speed: %f sig/s\n", 1.0f * cnt / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
return 0;
}

52
tests.c
View File

@ -1,4 +1,5 @@
/**
* Copyright (c) 2013 Tomas Dzetkulic
* Copyright (c) 2013 Pavol Rusnak
*
* Permission is hereby granted, free of charge, to any person obtaining
@ -22,6 +23,8 @@
#include <check.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#include "bignum.h"
#include "bip32.h"
@ -181,6 +184,48 @@ START_TEST(test_rfc6979)
}
END_TEST
START_TEST(test_sign_speed)
{
uint8_t sig[70], priv_key[32], msg[256];
uint32_t sig_len;
int i;
memcpy(priv_key, fromhex("c55ece858b0ddd5263f96810fe14437cd3b5e1fbd7c6a2ec1e031f05e86d8bd5"), 32);
for (i = 0; i < sizeof(msg); i++) {
msg[i] = i * 1103515245;
}
clock_t t = clock();
for (i = 0 ; i < 500; i++) {
// use our ECDSA signer to sign the message with the key
ecdsa_sign(priv_key, msg, sizeof(msg), sig, &sig_len);
}
printf("Signing speed: %0.2f sig/s\n", 1.0f * i / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
END_TEST
START_TEST(test_verify_speed)
{
uint8_t sig[70], pub_key[33], msg[256];
int i;
memcpy(sig, fromhex("3044022088dc0db6bc5efa762e75fbcc802af69b9f1fcdbdffce748d403f687f855556e6022010ee8035414099ac7d89cff88a3fa246d332dfa3c78d82c801394112dda039c2"), 70);
memcpy(pub_key, fromhex("024054fd18aeb277aeedea01d3f3986ff4e5be18092a04339dcf4e524e2c0a0974"), 33);
for (i = 0; i < sizeof(msg); i++) {
msg[i] = i * 1103515245;
}
clock_t t = clock();
for (i = 0 ; i < 150; i++) {
// use our ECDSA verifier to verify the message with the key
ecdsa_verify(pub_key, sig, msg, sizeof(msg));
}
printf("Verifying speed: %0.2f sig/s\n", 1.0f * i / ((float)(clock() - t) / CLOCKS_PER_SEC));
}
END_TEST
// define test suite and cases
Suite *test_suite(void)
{
@ -196,6 +241,11 @@ Suite *test_suite(void)
tcase_add_test(tc, test_rfc6979);
suite_add_tcase(s, tc);
tc = tcase_create("speed");
tcase_add_test(tc, test_sign_speed);
tcase_add_test(tc, test_verify_speed);
suite_add_tcase(s, tc);
return s;
}
@ -205,7 +255,7 @@ int main()
int number_failed;
Suite *s = test_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return number_failed;