mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-12 18:49:07 +00:00
fix broken test_speed
This commit is contained in:
parent
54cc18c493
commit
ed755120af
6
bip32.c
6
bip32.c
@ -132,7 +132,7 @@ int hdnode_from_seed(const uint8_t *seed, int seed_len, const char* curve, HDNod
|
||||
}
|
||||
memcpy(out->private_key, I, 32);
|
||||
memcpy(out->chain_code, I + 32, 32);
|
||||
|
||||
MEMSET_BZERO(out->public_key, sizeof(out->public_key));
|
||||
MEMSET_BZERO(I, sizeof(I));
|
||||
return 1;
|
||||
}
|
||||
@ -215,7 +215,6 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
||||
{
|
||||
uint8_t data[1 + 32 + 4];
|
||||
uint8_t I[32 + 32];
|
||||
uint8_t fingerprint[32];
|
||||
curve_point a, b;
|
||||
bignum256 c;
|
||||
|
||||
@ -265,7 +264,6 @@ int hdnode_public_ckd(HDNode *inout, uint32_t i)
|
||||
// Wipe all stack data.
|
||||
MEMSET_BZERO(data, sizeof(data));
|
||||
MEMSET_BZERO(I, sizeof(I));
|
||||
MEMSET_BZERO(fingerprint, sizeof(fingerprint));
|
||||
MEMSET_BZERO(&a, sizeof(a));
|
||||
MEMSET_BZERO(&b, sizeof(b));
|
||||
MEMSET_BZERO(&c, sizeof(c));
|
||||
@ -295,7 +293,7 @@ int hdnode_public_ckd_address_optimized(const curve_point *pub, const uint8_t *p
|
||||
failed = true;
|
||||
} else {
|
||||
scalar_multiply(&secp256k1, &c, &b); // b = c * G
|
||||
point_add(&secp256k1, pub, &b); // b = a + b
|
||||
point_add(&secp256k1, pub, &b); // b = a + b
|
||||
if (point_is_infinity(&b)) {
|
||||
failed = true;
|
||||
}
|
||||
|
2
ecdsa.h
2
ecdsa.h
@ -50,6 +50,8 @@ typedef struct {
|
||||
|
||||
#define MAX_ADDR_RAW_SIZE (4 + 20)
|
||||
#define MAX_WIF_RAW_SIZE (4 + 32 + 1)
|
||||
#define MAX_ADDR_SIZE (40)
|
||||
#define MAX_WIF_SIZE (58)
|
||||
|
||||
// rfc6979 pseudo random number generator state
|
||||
typedef struct {
|
||||
|
43
test_speed.c
43
test_speed.c
@ -9,7 +9,7 @@
|
||||
#include "nist256p1.h"
|
||||
#include "ed25519.h"
|
||||
|
||||
uint8_t msg[32];
|
||||
static uint8_t msg[32];
|
||||
|
||||
void prepare_msg(void)
|
||||
{
|
||||
@ -76,49 +76,52 @@ void test_verify_speed(void) {
|
||||
bench_ed25519();
|
||||
}
|
||||
|
||||
HDNode root;
|
||||
static HDNode root;
|
||||
|
||||
void prepare_node(void)
|
||||
{
|
||||
hdnode_from_seed((uint8_t *)"NothingToSeeHere", 16, SECP256K1_NAME, &root);
|
||||
hdnode_fill_public_key(&root);
|
||||
}
|
||||
|
||||
void bench_ckd_normal(void) {
|
||||
char addr[40];
|
||||
void bench_ckd_normal(int iterations) {
|
||||
char addr[MAX_ADDR_SIZE];
|
||||
HDNode node;
|
||||
clock_t t = clock();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
HDNode node = root;
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
memcpy(&node, &root, sizeof(HDNode));
|
||||
hdnode_public_ckd(&node, i);
|
||||
ecdsa_get_address(node.public_key, 0, addr, 40);
|
||||
if (i == 0) {
|
||||
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", 1000.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
|
||||
printf("CKD normal speed: %0.2f iter/s\n", iterations / ((float)(clock() - t) / CLOCKS_PER_SEC));
|
||||
}
|
||||
|
||||
void bench_ckd_optimized(void) {
|
||||
char addr[40];
|
||||
void bench_ckd_optimized(int iterations) {
|
||||
char addr[MAX_ADDR_SIZE];
|
||||
curve_point pub;
|
||||
ecdsa_read_pubkey(0, root.public_key, &pub);
|
||||
ecdsa_read_pubkey(&secp256k1, root.public_key, &pub);
|
||||
clock_t t = clock();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
hdnode_public_ckd_address_optimized(&pub, root.public_key, root.chain_code, i, 0, addr, 40);
|
||||
if (i == 0) {
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
hdnode_public_ckd_address_optimized(&pub, root.public_key, root.chain_code, i, 0, addr, sizeof(addr));
|
||||
if (i == 0 || i == iterations -1) {
|
||||
printf("address = %s\n", addr);
|
||||
}
|
||||
}
|
||||
printf("CKD optim speed: %0.2f iter/s\n", 1000.0f / ((float)(clock() - t) / CLOCKS_PER_SEC));
|
||||
printf("CKD optim speed: %0.2f iter/s\n", iterations / ((float)(clock() - t) / CLOCKS_PER_SEC));
|
||||
}
|
||||
|
||||
void test_ckd_speed(void) {
|
||||
void test_ckd_speed(int iterations) {
|
||||
prepare_node();
|
||||
bench_ckd_normal();
|
||||
bench_ckd_optimized();
|
||||
bench_ckd_normal(iterations);
|
||||
bench_ckd_optimized(iterations);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_verify_speed();
|
||||
test_ckd_speed();
|
||||
test_ckd_speed(1000);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user