Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions c-1/Makefile
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions c-gpu-1/Makefile
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
41 changes: 41 additions & 0 deletions c-php-sourced/gmp_add.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gmp.h>

int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <num_a> <num_b> <iterations>\\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;
}
41 changes: 41 additions & 0 deletions c-php-sourced/gmp_div.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <gmp.h>

int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "Usage: %s <num_a> <num_b> <iterations>\\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;
}
43 changes: 43 additions & 0 deletions c-php-sourced/password_hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Source: https://github.com/php/php-src/blob/master/ext/standard/password.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "argon2.h"

#define HASHLEN 32
#define SALTLEN 16

int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <password> <iterations>\\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;
}
33 changes: 33 additions & 0 deletions c-php-sourced/sha256.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <openssl/sha.h>

int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <string> <iterations>\\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;
}