2016-04-26 00:14:47 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2019-03-29 14:44:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "bip32.h"
|
2016-04-26 00:14:47 +00:00
|
|
|
#include "curves.h"
|
|
|
|
#include "ecdsa.h"
|
2018-01-12 16:24:47 +00:00
|
|
|
#include "ed25519-donna/ed25519.h"
|
2017-12-09 23:22:41 +00:00
|
|
|
#include "hasher.h"
|
2019-03-29 14:44:55 +00:00
|
|
|
#include "nist256p1.h"
|
|
|
|
#include "secp256k1.h"
|
2016-04-26 00:14:47 +00:00
|
|
|
|
2017-06-03 21:43:58 +00:00
|
|
|
static uint8_t msg[256];
|
2016-04-26 00:14:47 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void prepare_msg(void) {
|
|
|
|
for (size_t i = 0; i < sizeof(msg); i++) {
|
|
|
|
msg[i] = i * 1103515245;
|
|
|
|
}
|
2016-04-26 00:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_sign_secp256k1(int iterations) {
|
|
|
|
uint8_t sig[64], priv[32], pby;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &secp256k1;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_sign(curve, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
|
|
|
}
|
2017-06-03 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_sign_nist256p1(int iterations) {
|
|
|
|
uint8_t sig[64], priv[32], pby;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &nist256p1;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_sign(curve, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
|
|
|
}
|
2017-06-03 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_sign_ed25519(int iterations) {
|
|
|
|
ed25519_secret_key sk;
|
|
|
|
ed25519_signature sig;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2021-12-03 15:42:26 +00:00
|
|
|
memcpy(sk,
|
2019-03-29 14:44:55 +00:00
|
|
|
"\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);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
2022-06-23 16:00:31 +00:00
|
|
|
ed25519_sign(msg, sizeof(msg), sk, sig);
|
2019-03-29 14:44:55 +00:00
|
|
|
}
|
2017-06-03 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_verify_secp256k1_33(int iterations) {
|
|
|
|
uint8_t sig[64], pub[33], priv[32], pby;
|
2016-04-26 00:14:47 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &secp256k1;
|
2016-04-26 00:14:47 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
2016-04-26 00:14:47 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_verify(curve, HASHER_SHA2, pub, sig, msg, sizeof(msg));
|
|
|
|
}
|
2016-04-26 00:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_verify_secp256k1_65(int iterations) {
|
|
|
|
uint8_t sig[64], pub[65], priv[32], pby;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &secp256k1;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_verify(curve, HASHER_SHA2, pub, sig, msg, sizeof(msg));
|
|
|
|
}
|
2017-06-03 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_verify_nist256p1_33(int iterations) {
|
|
|
|
uint8_t sig[64], pub[33], priv[32], pby;
|
2016-05-12 15:19:42 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &nist256p1;
|
2016-05-12 15:19:42 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
2016-05-12 15:19:42 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_verify(curve, HASHER_SHA2, pub, sig, msg, sizeof(msg));
|
|
|
|
}
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_verify_nist256p1_65(int iterations) {
|
|
|
|
uint8_t sig[64], pub[65], priv[32], pby;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
const ecdsa_curve *curve = &nist256p1;
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
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, HASHER_SHA2, priv, msg, sizeof(msg), sig, &pby, NULL);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ecdsa_verify(curve, HASHER_SHA2, pub, sig, msg, sizeof(msg));
|
|
|
|
}
|
2017-06-03 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_verify_ed25519(int iterations) {
|
|
|
|
ed25519_public_key pk;
|
|
|
|
ed25519_secret_key sk;
|
|
|
|
ed25519_signature sig;
|
|
|
|
|
2021-12-03 15:42:26 +00:00
|
|
|
memcpy(sk,
|
2019-03-29 14:44:55 +00:00
|
|
|
"\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);
|
2022-06-23 16:00:31 +00:00
|
|
|
ed25519_sign(msg, sizeof(msg), sk, sig);
|
2019-03-29 14:44:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
ed25519_sign_open(msg, sizeof(msg), pk, sig);
|
|
|
|
}
|
2016-04-26 00:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_multiply_curve25519(int iterations) {
|
|
|
|
uint8_t result[32];
|
|
|
|
uint8_t secret[32];
|
|
|
|
uint8_t basepoint[32];
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
curve25519_scalarmult(result, secret, basepoint);
|
|
|
|
}
|
2016-10-24 18:27:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 00:26:16 +00:00
|
|
|
static HDNode root;
|
2016-05-12 15:19:42 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void prepare_node(void) {
|
|
|
|
hdnode_from_seed((uint8_t *)"NothingToSeeHere", 16, SECP256K1_NAME, &root);
|
|
|
|
hdnode_fill_public_key(&root);
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_ckd_normal(int iterations) {
|
|
|
|
char addr[MAX_ADDR_SIZE];
|
|
|
|
HDNode node;
|
|
|
|
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, HASHER_SHA2, HASHER_SHA2D, 0, addr,
|
|
|
|
sizeof(addr));
|
|
|
|
}
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench_ckd_optimized(int iterations) {
|
|
|
|
char addr[MAX_ADDR_SIZE];
|
|
|
|
curve_point pub;
|
|
|
|
ecdsa_read_pubkey(&secp256k1, root.public_key, &pub);
|
|
|
|
for (int i = 0; i < iterations; i++) {
|
|
|
|
hdnode_public_ckd_address_optimized(&pub, root.chain_code, i, 0,
|
|
|
|
HASHER_SHA2, HASHER_SHA2D, addr,
|
|
|
|
sizeof(addr), false);
|
|
|
|
}
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
void bench(void (*func)(int), const char *name, int 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);
|
2016-05-12 15:19:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 21:43:58 +00:00
|
|
|
#define BENCH(FUNC, ITER) bench(FUNC, #FUNC, ITER)
|
|
|
|
|
2016-05-12 15:19:42 +00:00
|
|
|
int main(void) {
|
2019-03-29 14:44:55 +00:00
|
|
|
prepare_msg();
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
BENCH(bench_sign_secp256k1, 500);
|
|
|
|
BENCH(bench_verify_secp256k1_33, 500);
|
|
|
|
BENCH(bench_verify_secp256k1_65, 500);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
BENCH(bench_sign_nist256p1, 500);
|
|
|
|
BENCH(bench_verify_nist256p1_33, 500);
|
|
|
|
BENCH(bench_verify_nist256p1_65, 500);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
BENCH(bench_sign_ed25519, 4000);
|
|
|
|
BENCH(bench_verify_ed25519, 4000);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
BENCH(bench_multiply_curve25519, 4000);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
prepare_node();
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
BENCH(bench_ckd_normal, 1000);
|
|
|
|
BENCH(bench_ckd_optimized, 1000);
|
2017-06-03 21:43:58 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return 0;
|
2016-04-26 00:14:47 +00:00
|
|
|
}
|