diff --git a/c-1/Makefile b/c-1/Makefile new file mode 100644 index 0000000..61a79b1 --- /dev/null +++ b/c-1/Makefile @@ -0,0 +1,37 @@ +CC=gcc +CFLAGS=-O3 -march=native -flto -Wall + +# Get all .c files in the current directory +SOURCES=$(wildcard *.c) +# Create a list of executable names from the .c files +TARGETS=$(SOURCES:.c=) + +# Default target: build all executables +all: $(TARGETS) + +# General rule for programs with no special libs +%: %.c + $(CC) $(CFLAGS) -o $@ $< + +# Specific rules for programs with special libs +password_hash: password_hash.c + $(CC) $(CFLAGS) -o $@ $< -largon2 + +sha256: sha256.c + $(CC) $(CFLAGS) -o $@ $< -lcrypto + +gmp_init: gmp_init.c + $(CC) $(CFLAGS) -o $@ $< -lgmp + +gmp_mul: gmp_mul.c + $(CC) $(CFLAGS) -o $@ $< -lgmp + +gmp_add: gmp_add.c + $(CC) $(CFLAGS) -o $@ $< -lgmp + +gmp_div: gmp_div.c + $(CC) $(CFLAGS) -o $@ $< -lgmp + +# Target to clean up all compiled binaries +clean: + rm -f $(TARGETS) diff --git a/c/date_math_add.c b/c-1/date_math_add.c similarity index 100% rename from c/date_math_add.c rename to c-1/date_math_add.c diff --git a/c/date_math_subtract.c b/c-1/date_math_subtract.c similarity index 100% rename from c/date_math_subtract.c rename to c-1/date_math_subtract.c diff --git a/c/get_time.c b/c-1/get_time.c similarity index 100% rename from c/get_time.c rename to c-1/get_time.c diff --git a/c/gmp_add.c b/c-1/gmp_add.c similarity index 100% rename from c/gmp_add.c rename to c-1/gmp_add.c diff --git a/c/gmp_div.c b/c-1/gmp_div.c similarity index 100% rename from c/gmp_div.c rename to c-1/gmp_div.c diff --git a/c/gmp_init.c b/c-1/gmp_init.c similarity index 100% rename from c/gmp_init.c rename to c-1/gmp_init.c diff --git a/c/gmp_mul.c b/c-1/gmp_mul.c similarity index 100% rename from c/gmp_mul.c rename to c-1/gmp_mul.c diff --git a/c/hexdec.c b/c-1/hexdec.c similarity index 100% rename from c/hexdec.c rename to c-1/hexdec.c diff --git a/c/microtime.c b/c-1/microtime.c similarity index 100% rename from c/microtime.c rename to c-1/microtime.c diff --git a/c/password_hash.c b/c-1/password_hash.c similarity index 100% rename from c/password_hash.c rename to c-1/password_hash.c diff --git a/c/sha256.c b/c-1/sha256.c similarity index 100% rename from c/sha256.c rename to c-1/sha256.c diff --git a/c/substr.c b/c-1/substr.c similarity index 100% rename from c/substr.c rename to c-1/substr.c diff --git a/c-gpu-1/Makefile b/c-gpu-1/Makefile new file mode 100644 index 0000000..04bf0f3 --- /dev/null +++ b/c-gpu-1/Makefile @@ -0,0 +1,27 @@ +# --- CUDA Configuration --- +# Check if nvcc is available +NVCC := $(shell command -v nvcc 2> /dev/null) +NVCCFLAGS=-O3 --std=c++11 + +# --- argon2-gpu Library --- +ARGON2_GPU_DIR=../argon2-gpu +ARGON2_GPU_INCLUDE_DIR=$(ARGON2_GPU_DIR)/include +ARGON2_GPU_LIB_DIR=$(ARGON2_GPU_DIR) +ARGON2_GPU_LIBS=-L$(ARGON2_GPU_LIB_DIR) -largon2-cuda -largon2-gpu-common + +TARGETS = +ifdef NVCC + TARGETS += password_hash_gpu +endif + +# Default target: build all executables +all: $(TARGETS) + +ifdef NVCC +password_hash_gpu: password_hash_gpu.cu + $(NVCC) $(NVCCFLAGS) -I$(ARGON2_GPU_INCLUDE_DIR) -o $@ $< $(ARGON2_GPU_LIBS) -largon2 +endif + +# Target to clean up all compiled binaries +clean: + rm -f $(TARGETS) diff --git a/c/password_hash_gpu.cu b/c-gpu-1/password_hash_gpu.cu similarity index 100% rename from c/password_hash_gpu.cu rename to c-gpu-1/password_hash_gpu.cu diff --git a/c-php-sourced/gmp_add.c b/c-php-sourced/gmp_add.c new file mode 100644 index 0000000..2d2b121 --- /dev/null +++ b/c-php-sourced/gmp_add.c @@ -0,0 +1,41 @@ +// C implementation to mirror the functionality of php/gmp_add.php +// Based on the existing C implementation in c-1/gmp_add.c for consistency. + +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 4) { + fprintf(stderr, "Usage: %s \\n", argv[0]); + return 1; + } + + char *a_str = argv[1]; + char *b_str = argv[2]; + int iterations = atoi(argv[3]); + + mpz_t a, b, result; + mpz_init_set_str(a, a_str, 10); + mpz_init_set_str(b, b_str, 10); + mpz_init(result); + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < iterations; i++) { + mpz_add(result, a, b); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + + double time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("%.12f\\n", time_spent); + + mpz_clear(a); + mpz_clear(b); + mpz_clear(result); + + return 0; +} diff --git a/c-php-sourced/gmp_div.c b/c-php-sourced/gmp_div.c new file mode 100644 index 0000000..67f44f0 --- /dev/null +++ b/c-php-sourced/gmp_div.c @@ -0,0 +1,41 @@ +// C implementation to mirror the functionality of php/gmp_div.php +// Based on the existing C implementation in c-1/gmp_div.c for consistency. + +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 4) { + fprintf(stderr, "Usage: %s \\n", argv[0]); + return 1; + } + + char *a_str = argv[1]; + char *b_str = argv[2]; + int iterations = atoi(argv[3]); + + mpz_t a, b, result; + mpz_init_set_str(a, a_str, 10); + mpz_init_set_str(b, b_str, 10); + mpz_init(result); + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < iterations; i++) { + mpz_div(result, a, b); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + + double time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("%.12f\\n", time_spent); + + mpz_clear(a); + mpz_clear(b); + mpz_clear(result); + + return 0; +} diff --git a/c-php-sourced/password_hash.c b/c-php-sourced/password_hash.c new file mode 100644 index 0000000..401bf20 --- /dev/null +++ b/c-php-sourced/password_hash.c @@ -0,0 +1,43 @@ +// Source: https://github.com/php/php-src/blob/master/ext/standard/password.c + +#include +#include +#include +#include +#include "argon2.h" + +#define HASHLEN 32 +#define SALTLEN 16 + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, "Usage: %s \\n", argv[0]); + return 1; + } + + char *pwd = argv[1]; + int iterations = atoi(argv[2]); + + uint8_t salt[SALTLEN]; + memset(salt, 0x00, SALTLEN); // For simplicity, using a fixed salt for the benchmark. + + uint32_t t_cost = 2; // time_cost + uint32_t m_cost = 32768; // memory_cost in KiB + uint32_t parallelism = 1; // threads + + char hash[HASHLEN]; + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < iterations; i++) { + argon2i_hash_raw(t_cost, m_cost, parallelism, pwd, strlen(pwd), salt, SALTLEN, hash, HASHLEN); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + + double time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("%.12f\\n", time_spent); + + return 0; +} diff --git a/c-php-sourced/sha256.c b/c-php-sourced/sha256.c new file mode 100644 index 0000000..e61e8b8 --- /dev/null +++ b/c-php-sourced/sha256.c @@ -0,0 +1,33 @@ +// Implementation based on OpenSSL's libcrypto, mirroring the original c/sha256.c test. +// Source for PHP's hash function is complex to isolate, so this provides a standard C equivalent. + +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, "Usage: %s \\n", argv[0]); + return 1; + } + + char *string = argv[1]; + int iterations = atoi(argv[2]); + unsigned char hash[SHA256_DIGEST_LENGTH]; + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < iterations; i++) { + SHA256((unsigned char*)string, strlen(string), hash); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + + double time_spent = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000.0; + printf("%.12f\\n", time_spent); + + return 0; +}