~sircmpwn/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
4 2

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

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

diff --git a/src/mkproof.c b/src/mkproof.c
index 3ec703ceb5e3..04e549ccc673 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -17,6 +17,42 @@ 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 = hash_msb(hash) >= digits;
	}

	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[])
{
@@ -53,15 +89,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,
@@ -70,34 +106,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));

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

		valid = hash_msb(hash) >= digits;
	}

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

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

[PATCH mkproof v5 3/4] Draw spinner while waiting for a proof

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

Notes:
    Changes from v2:
    - Make spinners const
    - Increment i forever
    - int i -> size_t i

 src/mkproof.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/src/mkproof.c b/src/mkproof.c
index 8aeb51f3de7e..11927e7c1cfe 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -24,6 +24,18 @@ die(int check, char *why)
	}
}

static void
draw_spinner(void)
{
	static size_t i = 0;
	const char *spinners[] = {"|", "/", "-", "\\"};
	size_t nspinners = sizeof(spinners) / sizeof(*spinners);

	fprintf(stderr, "\rRunning... %s", spinners[i % nspinners]);

	i++;
}

static void
run_mkproof(argon2_context context, int digits)
{
@@ -130,8 +142,17 @@ main(int argc, char *argv[])
		pids[i] = pid;
	}

	int wstatus;
	pid_t pid = wait(&wstatus);
	pid_t pid;
	while (1) {
		int wstatus;
		pid = waitpid(-1, &wstatus, WNOHANG);
		if (pid > 0) {
			break;
		}

		draw_spinner();
		sleep(1);
	}

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

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

Details
Message ID
<20201224192804.1830661-4-me@tomlebreux.com>
In-Reply-To
<20201224192804.1830661-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 11927e7c1cfe..962421c7c919 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -71,21 +71,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);
@@ -110,12 +132,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 v5 2/4] Use multiple processes to find proof

Details
Message ID
<20201224192804.1830661-2-me@tomlebreux.com>
In-Reply-To
<20201224192804.1830661-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 04e549ccc673..8aeb51f3de7e 100644
--- a/src/mkproof.c
+++ b/src/mkproof.c
@@ -1,13 +1,20 @@
#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"
#include "proof.h"


#define MAX_PIDS 512

static void
die(int check, char *why)
{
@@ -28,13 +35,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));

@@ -45,12 +47,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
@@ -95,18 +98,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

Re: [PATCH mkproof v5 4/4] Add -j option to specify number of processes

Details
Message ID
<C8352KQ49OBS.3G5CJK18TNS4W@taiga>
In-Reply-To
<20201224192804.1830661-4-me@tomlebreux.com> (view parent)
DKIM signature
fail
Download raw message
DKIM signature: fail
Thanks!

To git@git.sr.ht:~sircmpwn/mkproof
   6f9ea7d..4a8e70d  master -> master
Reply to thread Export thread (mbox)