From 4c53350bd562147e9bf1f816a51b9d7fb3864799 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Sun, 18 Feb 2024 00:46:21 +0100 Subject: [PATCH] refacto: faster --- rsa/Makefile | 3 +++ rsa/bigint.c | 47 +++++++++++++++++++++-------------------------- rsa/rsa.c | 19 ++++++++++++++----- rsa/rsa.h | 4 +++- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/rsa/Makefile b/rsa/Makefile index dec020e..08a0c23 100644 --- a/rsa/Makefile +++ b/rsa/Makefile @@ -18,6 +18,9 @@ fast: profile: gcc -Wall -Wextra -Werror -Wunused-function -pg $(SRC) -o $(NAME) +profile-fast: + gcc -Wall -Wextra -Werror -Wunused-function -O3 -pg $(SRC) -o $(NAME) + fclean: rm -rf $(NAME) diff --git a/rsa/bigint.c b/rsa/bigint.c index 1f78f25..e27ed6a 100644 --- a/rsa/bigint.c +++ b/rsa/bigint.c @@ -55,12 +55,12 @@ int bigint_cmp(bigint_t a, bigint_t b) { uint32_t size = sizeof(uint32_t) * 8; uint32_t acursor = size * a.len - 1; uint32_t bcursor = size * b.len - 1; - while (acursor >= bcursor + size && a.data[acursor / size] == 0) { + while (acursor >= bcursor + size && a.data[acursor >> 5] == 0) { acursor -= size; } while (acursor > bcursor) { - if (a.data[acursor / size] & (1 << acursor % size)) { + if (a.data[acursor >> 5] & (1 << acursor % size)) { return 1; } acursor -= 1; @@ -70,26 +70,22 @@ int bigint_cmp(bigint_t a, bigint_t b) { } while (bcursor > acursor) { - if (b.data[bcursor / size] & (1 << bcursor % size)) { + if (b.data[bcursor >> 5] & (1 << bcursor % size)) { return -1; } bcursor -= 1; } int cursor = acursor; - while (cursor >= 0 && a.data[cursor / size] == b.data[cursor / size]) { - cursor -= size; - } while (cursor >= 0) { - uint32_t abit = a.data[cursor / size] & (1 << (cursor % size)); - uint32_t bbit = b.data[cursor / size] & (1 << (cursor % size)); - if (abit > bbit) { + if (a.data[cursor >> 5] > b.data[cursor >> 5]) { return 1; } - if (bbit > abit) { + if (b.data[cursor >> 5] > a.data[cursor >> 5]) { return -1; } - cursor -= 1; + cursor -= size; } + return 0; } @@ -189,10 +185,7 @@ void bigint_destroy(bigint_t n) { } void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { - //bigint_t result = bigint_zero(a.len); bigint_set_zeros(result); - //size_t size = sizeof(uint32_t) * 8; - //size_t width = a.len * size; uint32_t carriage = 0; for (size_t cursor = 0; cursor < a.len; cursor++) { @@ -201,7 +194,6 @@ void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result) { carriage = tmp >> 32; } memcpy(a.data, result.data, a.len * sizeof(uint32_t)); - //bigint_destroy(result); } void bigint_set_zeros(bigint_t n) { @@ -213,15 +205,18 @@ void bigint_set_zeros(bigint_t n) { void custom_bigint_mul(bigint_t a, bigint_t b, bigint_t result, bigint_t custom) { //bigint_t b_tool = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); bigint_t b_tool = bigint_zero(a.len + b.len); + //memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); size_t size = sizeof(uint32_t) * 8; - size_t width = a.len * size; + int width = a.len * size; bigint_set_zeros(result); - for (size_t cursor = 0; cursor < width; cursor++) { - if (a.data[cursor / 32] >> (cursor % 32) & 1) { + for (int cursor = 0; cursor < width; cursor++) { + if (a.data[cursor >> 5] >> (cursor % 32) & 1) { bigint_set_zeros(b_tool); - memcpy(b_tool.data, b.data, b.len * sizeof(uint32_t)); - for (size_t i = 0; i < cursor; i++) { + memcpy(b_tool.data + (cursor >> 5), b.data, b.len * sizeof(uint32_t)); + int i = cursor - cursor % 32; + while (i < cursor) { bigint_bitwise_left_shift(b_tool); + i++; } custom_bigint_add(result, b_tool, custom); } @@ -277,7 +272,7 @@ void bulk_destroy(bigint_t x, bigint_t y, bigint_t n, bigint_t d, bigint_t two, bigint_t bigint_random_range(bigint_t low, bigint_t high) { } - */ + */ bigint_t bigint_prime(size_t len) { bigint_t n = bigint_zero(len); @@ -292,11 +287,11 @@ bigint_t bigint_prime(size_t len) { s += 1; } - bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); - bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 4); + bigint_t x = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t y = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom2 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); + bigint_t custom3 = bigint_zero(RSA_BLOCK_SIZE / 8 / sizeof(uint32_t) * 2); bigint_t two = bigint_zero(len); two.data[0] = 2; diff --git a/rsa/rsa.c b/rsa/rsa.c index 8a07142..94b47eb 100644 --- a/rsa/rsa.c +++ b/rsa/rsa.c @@ -4,12 +4,14 @@ rsa_t rsa_init(size_t len) { rsa_t rsa; printf("Generating two primes of length %d bits\n", RSA_BLOCK_SIZE / 2); - printf("Generating p...\n"); + //printf("Generating p...\n"); rsa.p = bigint_prime(len / 2); - printf("p = %u\n", rsa.p.data[0]); - printf("Generating q...\n"); + printf("p = %lu\n", ((uint64_t)rsa.p.data[1] << 32) + (uint64_t)rsa.p.data[0]); + //printf("p = %u\n", rsa.p.data[0]); + //printf("Generating q...\n"); rsa.q = bigint_prime(len / 2); - printf("q = %u\n", rsa.q.data[0]); + printf("q = %lu\n", ((uint64_t)rsa.q.data[1] << 32) + (uint64_t)rsa.q.data[0]); + //printf("q = %u\n", rsa.q.data[0]); return rsa; @@ -18,6 +20,13 @@ rsa_t rsa_init(size_t len) { rsa_t rsa_generate_keys(size_t block_size) { size_t len = block_size / sizeof(uint32_t) / 8; rsa_t rsa = rsa_init(len); - + bigint_destroy(rsa.p); + bigint_destroy(rsa.q); + for (int i = 0; i < 18; i++) { + bigint_t p = bigint_prime(len / 2); + printf("%lu\n", ((uint64_t)p.data[1] << 32) + (uint64_t)p.data[0]); + bigint_destroy(p); + } return rsa; } + diff --git a/rsa/rsa.h b/rsa/rsa.h index fc29fca..b4b4485 100644 --- a/rsa/rsa.h +++ b/rsa/rsa.h @@ -9,7 +9,7 @@ #include #include -#define RSA_BLOCK_SIZE 64 +#define RSA_BLOCK_SIZE 128 typedef struct bigint_s { uint32_t *data; @@ -39,6 +39,7 @@ bigint_t bigint_new(size_t len); bigint_t bigint_zero(size_t len); bigint_t bigint_clone(bigint_t src); void bigint_add(bigint_t a, bigint_t b); +void custom_bigint_add(bigint_t a, bigint_t b, bigint_t result); bigint_t assignable_bigint_mul(bigint_t a, bigint_t b); bigint_t assignable_bigint_modulo(bigint_t a, bigint_t b); bigint_t assignable_bigint_pow_mod(bigint_t a, bigint_t e, bigint_t n); @@ -53,3 +54,4 @@ void array_bitwise_right_shift(uint32_t *a, size_t len); void array_decrement(uint32_t *a, size_t len); #endif +