~sircmpwn/public-inbox

4 2

[PATCH mkproof v4 1/4] Split mkproof logic into own function

Details
Message ID
<20201224011907.32919-1-me@tomlebreux.com>
DKIM signature
pass
Download raw message
Patch: +50 -41
---
 src/mkproof.c | 91 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 50 insertions(+), 41 deletions(-)

diff --git a/src/mkproof.c b/src/mkproof.c
index c5bf90bea9d5..cb79383b9fc4 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -16,6 +16,49 @@ die(int check, char *why)
	}
}

static void
run_mkproof(argon2_context context, int digits)
{
	unsigned char password[16];
	unsigned char hash[32];

	context.out = hash;
	context.outlen = sizeof(hash);
	context.pwd = password;
	context.pwdlen = sizeof(password);

	unsigned long nattempts = 0;
	int valid = 0;
	while (!valid) {
		if (isatty(STDERR_FILENO)) {
			fprintf(stderr, "\rAttempt %lu", ++nattempts);
		}

		ssize_t b = get_random_bytes(password, sizeof(password));
		assert(b == sizeof(password));

		int r = argon2id_ctx(&context);
		die(r != 0, "argon2id failed\n");

		valid = 1;
		for (int i = 0; i < digits; ++i) {
			unsigned char n = hash[i / 2] & (i % 2 ? 0x0F : 0xF0);
			if (n != 0) {
				valid = 0;
				break;
			}
		}
	}

	if (isatty(STDERR_FILENO)) {
		fprintf(stderr, ": found.\nHere is your proof:\n");
	}

	char proof[33];
	enchex(password, sizeof(password), proof, sizeof(proof));
	printf("%s\n", proof);
}

int
main(int argc, char *argv[])
{
@@ -52,15 +95,15 @@ main(int argc, char *argv[])
	int r = dechex(saltstr, strlen(saltstr), salt, sizeof(salt));
	die(r == -1, "Invalid challenge");

	unsigned char password[16];
	unsigned char hash[32];
	if (isatty(STDERR_FILENO)) {
		// TODO: Provide a better estimate based on the difficulty
		fprintf(stderr, "Generating a proof of work.\n");
		fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
	}

	argon2_context context = {
		.out = hash,
		.outlen = sizeof(hash),
		.salt = salt,
		.saltlen = sizeof(salt),
		.pwd = password,
		.pwdlen = sizeof(password),
		.t_cost = iters,
		.m_cost = memory,
		.lanes = 1,
@@ -69,41 +112,7 @@ main(int argc, char *argv[])
		.version = ARGON2_VERSION_NUMBER,
	};

	if (isatty(STDERR_FILENO)) {
		// TODO: Provide a better estimate based on the difficulty
		fprintf(stderr, "Generating a proof of work.\n");
		fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
	}

	unsigned long nattempts = 0;
	int valid = 0;
	while (!valid) {
		if (isatty(STDERR_FILENO)) {
			fprintf(stderr, "\rAttempt %lu", ++nattempts);
		}

		ssize_t b = get_random_bytes(password, sizeof(password));
		assert(b == sizeof(password));
	run_mkproof(context, digits);

		r = argon2id_ctx(&context);
		die(r != 0, "argon2id failed\n");

		valid = 1;
		for (int i = 0; i < digits; ++i) {
			unsigned char n = hash[i / 2] & (i % 2 ? 0x0F : 0xF0);
			if (n != 0) {
				valid = 0;
				break;
			}
		}
	}

	if (isatty(STDERR_FILENO)) {
		fprintf(stderr, ": found.\nHere is your proof:\n");
	}

	char proof[33];
	enchex(password, sizeof(password), proof, sizeof(proof));
	printf("%s\n", proof);
	return 0;
}
-- 
2.29.2

[PATCH mkproof v4 4/4] Add -j option to specify number of processes

Details
Message ID
<20201224011907.32919-4-me@tomlebreux.com>
In-Reply-To
<20201224011907.32919-1-me@tomlebreux.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +26 -10
---

Notes:
    Changes from v3:
    - Check nprocs >0

 src/mkproof.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/mkproof.c b/src/mkproof.c
index 72e7f26ff1a2..394835e74e15 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -77,21 +77,43 @@ run_mkproof(argon2_context context, int digits)
int
main(int argc, char *argv[])
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s <challenge>\n", argv[0]);
#ifdef _SC_NPROCESSORS_ONLN
	long nprocs = sysconf(_SC_NPROCESSORS_ONLN) - 1;
#else
	long nprocs = 2;
#endif

	char *endptr;
	int c;
	while ((c = getopt(argc, argv, "j:")) != -1) {
		switch (c) {
		case 'j':
			nprocs = strtol(optarg, &endptr, 10);
			die(*endptr, "error parsing nprocs");
			break;
		}
	}

	if ((argc - optind) != 1) {
		fprintf(stderr, "Usage: %s [-j nprocs] <challenge>\n", argv[0]);
		return 1;
	}

	if (nprocs <= 0 || nprocs > MAX_PIDS) {
		fprintf(stderr, "please specify nprocs value between 1 and "
				"%d\n", MAX_PIDS);
		return 1;
	}

	int iters, memory, digits;
	unsigned char salt[16];

	char *challenge = argv[1];
	char *challenge = argv[optind];
	char *algo = strtok(challenge, ":");
	if (strcmp(algo, "argon2id") != 0) {
		fprintf(stderr, "Error: unknown challenge type %s\n", algo);
		return 1;
	}
	char *endptr;

	char *iterstr = strtok(NULL, ":");
	iters = strtoul(iterstr, &endptr, 10);
@@ -116,12 +138,6 @@ main(int argc, char *argv[])
		fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
	}

#ifdef _SC_NPROCESSORS_ONLN
	long nprocs = sysconf(_SC_NPROCESSORS_ONLN) - 1;
#else
	long nprocs = 2;
#endif

	pid_t pids[MAX_PIDS];
	assert(nprocs <= MAX_PIDS);

-- 
2.29.2

[PATCH mkproof v4 2/4] Use multiple processes to find proof

Details
Message ID
<20201224011907.32919-2-me@tomlebreux.com>
In-Reply-To
<20201224011907.32919-1-me@tomlebreux.com> (view parent)
DKIM signature
pass
Download raw message
Patch: +52 -18
---

Notes:
    Changes from v1:
    - Use sysconf with fallback to get number of processors
    - Statically allocate list of pids
    Changes from v2:
    - Set default value to nprocs - 1
    - Set default parallelism to 2 for unknown nprocs

 src/mkproof.c | 70 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 52 insertions(+), 18 deletions(-)

diff --git a/src/mkproof.c b/src/mkproof.c
index cb79383b9fc4..aa0bbed3c6ec 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -1,12 +1,19 @@
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "argon2.h"
#include "random.h"
#include "util.h"


#define MAX_PIDS 512

static void
die(int check, char *why)
{
@@ -27,13 +34,8 @@ run_mkproof(argon2_context context, int digits)
	context.pwd = password;
	context.pwdlen = sizeof(password);

	unsigned long nattempts = 0;
	int valid = 0;
	while (!valid) {
		if (isatty(STDERR_FILENO)) {
			fprintf(stderr, "\rAttempt %lu", ++nattempts);
		}

		ssize_t b = get_random_bytes(password, sizeof(password));
		assert(b == sizeof(password));

@@ -51,12 +53,13 @@ run_mkproof(argon2_context context, int digits)
	}

	if (isatty(STDERR_FILENO)) {
		fprintf(stderr, ": found.\nHere is your proof:\n");
		fprintf(stderr, "Here is your proof:\n");
	}

	char proof[33];
	enchex(password, sizeof(password), proof, sizeof(proof));
	printf("%s\n", proof);
	_exit(0);
}

int
@@ -101,18 +104,49 @@ main(int argc, char *argv[])
		fprintf(stderr, "This may take anywhere from several minutes to a few hours on slow computers.\n");
	}

	argon2_context context = {
		.salt = salt,
		.saltlen = sizeof(salt),
		.t_cost = iters,
		.m_cost = memory,
		.lanes = 1,
		.threads = 1,
		.flags = ARGON2_DEFAULT_FLAGS,
		.version = ARGON2_VERSION_NUMBER,
	};

	run_mkproof(context, digits);
#ifdef _SC_NPROCESSORS_ONLN
	long nprocs = sysconf(_SC_NPROCESSORS_ONLN) - 1;
#else
	long nprocs = 2;
#endif

	pid_t pids[MAX_PIDS];
	assert(nprocs <= MAX_PIDS);

	for (size_t i = 0; i < (size_t)nprocs; i++) {
		pid_t pid = fork();
		die(pid < 0, "fork()");

		if (pid == 0) {
			argon2_context context = {
				.salt = salt,
				.saltlen = sizeof(salt),
				.t_cost = iters,
				.m_cost = memory,
				.lanes = 1,
				.threads = 1,
				.flags = ARGON2_DEFAULT_FLAGS,
				.version = ARGON2_VERSION_NUMBER,
			};
			run_mkproof(context, digits);
			// unreachable
			assert(0);
		}

		pids[i] = pid;
	}

	int wstatus;
	pid_t pid = wait(&wstatus);

	for (size_t i = 0; i < (size_t)nprocs; i++) {
		if (pids[i] == pid) {
			continue;
		}

		r = kill(pids[i], SIGTERM);
		die(r == -1, strerror(errno));
	}

	return 0;
}
-- 
2.29.2
Details
Message ID
<C812MQLU4ZNX.C74XXZ69CJ1Y@taiga>
In-Reply-To
<20201224011907.32919-1-me@tomlebreux.com> (view parent)
DKIM signature
pass
Download raw message
Can you rebase this following the recent changes to proof logic?
Details
Message ID
<8d1acfd8-9922-c687-7da3-b2cf08b39631@tomlebreux.com>
In-Reply-To
<C812MQLU4ZNX.C74XXZ69CJ1Y@taiga> (view parent)
DKIM signature
pass
Download raw message
On 2020-12-24 9:05 a.m., Drew DeVault wrote:
> Can you rebase this following the recent changes to proof logic?
> 

Yes,

rebased in v5.
Reply to thread Export thread (mbox)