Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#include <stdio.h>2#include <math.h>3#include "sys/time.h"5#include "ctaes.h"7static double gettimedouble(void) {8struct timeval tv;9gettimeofday(&tv, NULL);10return tv.tv_usec * 0.000001 + tv.tv_sec;11}13static void print_number(double x) {14double y = x;15int c = 0;16if (y < 0.0) {17y = -y;18}19while (y < 100.0) {20y *= 10.0;21c++;22}23printf("%.*f", c, x);24}26static void run_benchmark(char *name, void (*benchmark)(void*), void (*setup)(void*), void (*teardown)(void*), void* data, int count, int iter) {27int i;28double min = HUGE_VAL;29double sum = 0.0;30double max = 0.0;31for (i = 0; i < count; i++) {32double begin, total;33if (setup != NULL) {34setup(data);35}36begin = gettimedouble();37benchmark(data);38total = gettimedouble() - begin;39if (teardown != NULL) {40teardown(data);41}42if (total < min) {43min = total;44}45if (total > max) {46max = total;47}48sum += total;49}50printf("%s: min ", name);51print_number(min * 1000000000.0 / iter);52printf("ns / avg ");53print_number((sum / count) * 1000000000.0 / iter);54printf("ns / max ");55print_number(max * 1000000000.0 / iter);56printf("ns\n");57}59static void bench_AES128_init(void* data) {60AES128_ctx* ctx = (AES128_ctx*)data;61int i;62for (i = 0; i < 50000; i++) {63AES128_init(ctx, (unsigned char*)ctx);64}65}67static void bench_AES128_encrypt_setup(void* data) {68AES128_ctx* ctx = (AES128_ctx*)data;69static const unsigned char key[16] = {0};70AES128_init(ctx, key);71}73static void bench_AES128_encrypt(void* data) {74const AES128_ctx* ctx = (const AES128_ctx*)data;75unsigned char scratch[16] = {0};76int i;77for (i = 0; i < 4000000 / 16; i++) {78AES128_encrypt(ctx, 1, scratch, scratch);79}80}82static void bench_AES128_decrypt(void* data) {83const AES128_ctx* ctx = (const AES128_ctx*)data;84unsigned char scratch[16] = {0};85int i;86for (i = 0; i < 4000000 / 16; i++) {87AES128_decrypt(ctx, 1, scratch, scratch);88}89}91static void bench_AES192_init(void* data) {92AES192_ctx* ctx = (AES192_ctx*)data;93int i;94for (i = 0; i < 50000; i++) {95AES192_init(ctx, (unsigned char*)ctx);96}97}99static void bench_AES192_encrypt_setup(void* data) {100AES192_ctx* ctx = (AES192_ctx*)data;101static const unsigned char key[16] = {0};102AES192_init(ctx, key);103}105static void bench_AES192_encrypt(void* data) {106const AES192_ctx* ctx = (const AES192_ctx*)data;107unsigned char scratch[16] = {0};108int i;109for (i = 0; i < 4000000 / 16; i++) {110AES192_encrypt(ctx, 1, scratch, scratch);111}112}114static void bench_AES192_decrypt(void* data) {115const AES192_ctx* ctx = (const AES192_ctx*)data;116unsigned char scratch[16] = {0};117int i;118for (i = 0; i < 4000000 / 16; i++) {119AES192_decrypt(ctx, 1, scratch, scratch);120}121}123static void bench_AES256_init(void* data) {124AES256_ctx* ctx = (AES256_ctx*)data;125int i;126for (i = 0; i < 50000; i++) {127AES256_init(ctx, (unsigned char*)ctx);128}129}132static void bench_AES256_encrypt_setup(void* data) {133AES256_ctx* ctx = (AES256_ctx*)data;134static const unsigned char key[16] = {0};135AES256_init(ctx, key);136}138static void bench_AES256_encrypt(void* data) {139const AES256_ctx* ctx = (const AES256_ctx*)data;140unsigned char scratch[16] = {0};141int i;142for (i = 0; i < 4000000 / 16; i++) {143AES256_encrypt(ctx, 1, scratch, scratch);144}145}147static void bench_AES256_decrypt(void* data) {148const AES256_ctx* ctx = (const AES256_ctx*)data;149unsigned char scratch[16] = {0};150int i;151for (i = 0; i < 4000000 / 16; i++) {152AES256_decrypt(ctx, 1, scratch, scratch);153}154}156int main(void) {157AES128_ctx ctx128;158AES192_ctx ctx192;159AES256_ctx ctx256;160run_benchmark("aes128_init", bench_AES128_init, NULL, NULL, &ctx128, 20, 50000);161run_benchmark("aes128_encrypt_byte", bench_AES128_encrypt, bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000);162run_benchmark("aes128_decrypt_byte", bench_AES128_decrypt, bench_AES128_encrypt_setup, NULL, &ctx128, 20, 4000000);163run_benchmark("aes192_init", bench_AES192_init, NULL, NULL, &ctx192, 20, 50000);164run_benchmark("aes192_encrypt_byte", bench_AES192_encrypt, bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000);165run_benchmark("aes192_decrypt_byte", bench_AES192_decrypt, bench_AES192_encrypt_setup, NULL, &ctx192, 20, 4000000);166run_benchmark("aes256_init", bench_AES256_init, NULL, NULL, &ctx256, 20, 50000);167run_benchmark("aes256_encrypt_byte", bench_AES256_encrypt, bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000);168run_benchmark("aes256_decrypt_byte", bench_AES256_decrypt, bench_AES256_encrypt_setup, NULL, &ctx256, 20, 4000000);169return 0;170}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.