GNUnet  0.11.x
Macros | Functions | Variables
gnunet-scrypt.c File Reference

tool to manipulate SCRYPT proofs of work. More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include <gcrypt.h>
Include dependency graph for gnunet-scrypt.c:

Go to the source code of this file.

Macros

#define ROUND_SIZE   10
 

Functions

static void shutdown_task (void *cls)
 Write our current proof to disk. More...
 
static void pow_hash (const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
 Calculate the 'proof-of-work' hash (an expensive hash). More...
 
static unsigned int count_leading_zeroes (const struct GNUNET_HashCode *hash)
 Count the leading zeroes in hash. More...
 
static void find_proof (void *cls)
 Find our proof of work. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config)
 Main function that will be run by the scheduler. More...
 
int main (int argc, char *const *argv)
 Program to manipulate ECC key files. More...
 

Variables

static unsigned long long nse_work_required
 Amount of work required (W-bit collisions) for NSE proofs, in collision-bits. More...
 
static struct GNUNET_TIME_Relative proof_find_delay
 Interval between proof find runs. More...
 
static struct GNUNET_CRYPTO_EddsaPublicKey pub
 
static uint64_t proof
 
static struct GNUNET_SCHEDULER_Taskproof_task
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 
static char * pkfn
 
static char * pwfn
 

Detailed Description

tool to manipulate SCRYPT proofs of work.

Author
Bart Polot

Definition in file gnunet-scrypt.c.

Macro Definition Documentation

◆ ROUND_SIZE

#define ROUND_SIZE   10

Referenced by find_proof().

Function Documentation

◆ shutdown_task()

static void shutdown_task ( void *  cls)
static

Write our current proof to disk.

Parameters
clsclosure

Definition at line 58 of file gnunet-scrypt.c.

References GNUNET_DISK_fn_write(), GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_strerror_file, proof, and pwfn.

Referenced by find_proof(), and run().

59 {
60  (void) cls;
61  if (sizeof(proof) != GNUNET_DISK_fn_write (pwfn,
62  &proof,
63  sizeof(proof),
67 }
ssize_t GNUNET_DISK_fn_write(const char *fn, const void *buffer, size_t n, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file.
Definition: disk.c:880
static char * pwfn
Definition: gnunet-scrypt.c:49
static uint64_t proof
Definition: gnunet-scrypt.c:41
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pow_hash()

static void pow_hash ( const void *  buf,
size_t  buf_len,
struct GNUNET_HashCode result 
)
static

Calculate the 'proof-of-work' hash (an expensive hash).

Parameters
bufdata to hash
buf_lennumber of bytes in buf
resultwhere to write the resulting hash

Definition at line 78 of file gnunet-scrypt.c.

References GNUNET_break.

Referenced by find_proof().

79 {
80  GNUNET_break (
81  0 == gcry_kdf_derive (buf,
82  buf_len,
83  GCRY_KDF_SCRYPT,
84  1 /* subalgo */,
85  "gnunet-proof-of-work",
86  strlen ("gnunet-proof-of-work"),
87  2 /* iterations; keep cost of individual op small */,
88  sizeof(struct GNUNET_HashCode),
89  result));
90 }
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char buf[2048]
A 512-bit hashcode.
Here is the caller graph for this function:

◆ count_leading_zeroes()

static unsigned int count_leading_zeroes ( const struct GNUNET_HashCode hash)
static

Count the leading zeroes in hash.

Parameters
hashto count leading zeros in
Returns
the number of leading zero bits.

Definition at line 100 of file gnunet-scrypt.c.

References GNUNET_CRYPTO_hash_get_bit().

Referenced by find_proof().

101 {
102  unsigned int hash_count;
103 
104  hash_count = 0;
105  while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
106  hash_count++;
107  return hash_count;
108 }
int GNUNET_CRYPTO_hash_get_bit(const struct GNUNET_HashCode *code, unsigned int bit)
Obtain a bit from a hashcode.
Definition: crypto_hash.c:251
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_proof()

static void find_proof ( void *  cls)
static

Find our proof of work.

Parameters
clsclosure (unused)
tctask context

Definition at line 118 of file gnunet-scrypt.c.

References buf, count_leading_zeroes(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_log, GNUNET_memcpy, GNUNET_SCHEDULER_add_delayed_with_priority(), GNUNET_SCHEDULER_PRIORITY_IDLE, GNUNET_SCHEDULER_shutdown(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_get_duration(), GNUNET_TIME_relative_divide(), nse_work_required, pow_hash(), proof, proof_find_delay, pub, ROUND_SIZE, and shutdown_task().

Referenced by run().

119 {
120 #define ROUND_SIZE 10
121  uint64_t counter;
122  char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
123  + sizeof(uint64_t)] GNUNET_ALIGN;
124  struct GNUNET_HashCode result;
125  unsigned int i;
126  struct GNUNET_TIME_Absolute timestamp;
127  struct GNUNET_TIME_Relative elapsed;
128 
129  (void) cls;
131  "Got Proof of Work %llu\n",
132  (unsigned long long) proof);
133  proof_task = NULL;
134  GNUNET_memcpy (&buf[sizeof(uint64_t)],
135  &pub,
136  sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
137  i = 0;
138  counter = proof;
139  timestamp = GNUNET_TIME_absolute_get ();
140  while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
141  {
142  GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
143  pow_hash (buf, sizeof(buf), &result);
145  {
146  proof = counter;
147  fprintf (stdout,
148  "Proof of work found: %llu!\n",
149  (unsigned long long) proof);
151  return;
152  }
153  counter++;
154  i++;
155  }
156  elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
157  elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
159  "Current: %llu [%s/proof]\n",
160  (unsigned long long) counter,
162  if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
163  {
165  "Testing proofs currently at %llu\n",
166  (unsigned long long) counter);
167  /* remember progress every 100 rounds */
168  proof = counter;
169  shutdown_task (NULL);
170  }
171  else
172  {
173  proof = counter;
174  }
175  proof_task =
178  &find_proof,
179  NULL);
180 }
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed_with_priority(struct GNUNET_TIME_Relative delay, enum GNUNET_SCHEDULER_Priority priority, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1183
static void find_proof(void *cls)
Find our proof of work.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
static struct GNUNET_SCHEDULER_Task * proof_task
Definition: gnunet-scrypt.c:43
static unsigned int count_leading_zeroes(const struct GNUNET_HashCode *hash)
Count the leading zeroes in hash.
static void pow_hash(const void *buf, size_t buf_len, struct GNUNET_HashCode *result)
Calculate the &#39;proof-of-work&#39; hash (an expensive hash).
Definition: gnunet-scrypt.c:78
static unsigned long long nse_work_required
Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
Definition: gnunet-scrypt.c:32
static uint64_t proof
Definition: gnunet-scrypt.c:41
static struct GNUNET_TIME_Relative proof_find_delay
Interval between proof find runs.
Definition: gnunet-scrypt.c:37
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:687
static char buf[2048]
static int result
Global testing status.
A 512-bit hashcode.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
Run when otherwise idle.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
static void shutdown_task(void *cls)
Write our current proof to disk.
Definition: gnunet-scrypt.c:58
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
Definition: time.c:375
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:39
#define GNUNET_log(kind,...)
struct GNUNET_TIME_Relative GNUNET_TIME_relative_divide(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Divide relative time by a given factor.
Definition: time.c:527
Time for absolute times used by GNUnet, in microseconds.
#define ROUND_SIZE
Time for relative time used by GNUnet, in microseconds.
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle config 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgconfiguration

Definition at line 192 of file gnunet-scrypt.c.

References _, config, find_proof(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CRYPTO_eddsa_key_create_from_file(), GNUNET_CRYPTO_eddsa_key_get_public(), GNUNET_CRYPTO_eddsa_public_key_to_string(), GNUNET_DISK_file_test(), GNUNET_DISK_fn_read(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log, GNUNET_log_config_invalid(), GNUNET_log_config_missing(), GNUNET_OK, GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_add_with_priority(), GNUNET_SCHEDULER_PRIORITY_IDLE, GNUNET_SCHEDULER_shutdown(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_YES, nse_work_required, pk, pkfn, proof, proof_find_delay, pub, pwfn, and shutdown_task().

Referenced by main().

196 {
198  char *pids;
199 
200  (void) cls;
201  (void) args;
202  (void) cfgfile;
203  cfg = config;
204  /* load proof of work */
205  if (NULL == pwfn)
206  {
208  "NSE",
209  "PROOFFILE",
210  &pwfn))
211  {
214  return;
215  }
216  }
217  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn);
219  (sizeof(proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof(proof))))
220  proof = 0;
221 
222  /* load private key */
223  if (NULL == pkfn)
224  {
226  "PEER",
227  "PRIVATE_KEY",
228  &pkfn))
229  {
231  "PEER",
232  "PRIVATE_KEY");
233  return;
234  }
235  }
236  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
237  if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
238  {
239  fprintf (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn);
240  GNUNET_free (pkfn);
241  return;
242  }
243  GNUNET_free (pkfn);
245  GNUNET_free (pk);
247  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids);
248  GNUNET_free (pids);
249 
250  /* get target bit amount */
251  if (0 == nse_work_required)
252  {
254  "NSE",
255  "WORKBITS",
257  {
260  return;
261  }
262  if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
263  {
265  "NSE",
266  "WORKBITS",
267  _ ("Value is too large.\n"));
269  return;
270  }
271  else if (0 == nse_work_required)
272  {
274  return;
275  }
276  }
278 
280  "Delay between tries: %s\n",
282  proof_task =
284  &find_proof,
285  NULL);
287 }
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1300
static void find_proof(void *cls)
Find our proof of work.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
void GNUNET_log_config_invalid(enum GNUNET_ErrorType kind, const char *section, const char *option, const char *required)
Log error message about invalid configuration option value.
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:526
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static char * pkfn
Definition: gnunet-scrypt.c:47
static char * pwfn
Definition: gnunet-scrypt.c:49
static struct GNUNET_SCHEDULER_Task * proof_task
Definition: gnunet-scrypt.c:43
static const struct GNUNET_CONFIGURATION_Handle * cfg
Definition: gnunet-scrypt.c:45
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority.
Definition: scheduler.c:1207
static unsigned long long nse_work_required
Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
Definition: gnunet-scrypt.c:32
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
static uint64_t proof
Definition: gnunet-scrypt.c:41
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:365
static struct GNUNET_TIME_Relative proof_find_delay
Interval between proof find runs.
Definition: gnunet-scrypt.c:37
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:687
static const struct GNUNET_CONFIGURATION_Handle * config
A 512-bit hashcode.
Run when otherwise idle.
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:270
Private ECC key encoded for transmission.
static void shutdown_task(void *cls)
Write our current proof to disk.
Definition: gnunet-scrypt.c:58
static struct GNUNET_CRYPTO_EddsaPublicKey pub
Definition: gnunet-scrypt.c:39
#define GNUNET_log(kind,...)
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename)
Create a new private key by reading it from a file.
int GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
#define GNUNET_YES
Definition: gnunet_common.h:77
ssize_t GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:794
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

Program to manipulate ECC key files.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, 1 on error

Definition at line 298 of file gnunet-scrypt.c.

References gettext_noop, GNUNET_free, GNUNET_free_non_null, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_filename(), GNUNET_GETOPT_option_relative_time(), GNUNET_GETOPT_option_ulong(), GNUNET_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), nse_work_required, pkfn, proof_find_delay, pwfn, ret, and run().

299 {
302  'b',
303  "bits",
304  "BITS",
305  gettext_noop ("number of bits to require for the proof of work"),
308  'k',
309  "keyfile",
310  "FILE",
311  gettext_noop ("file with private key, otherwise default is used"),
312  &pkfn),
314  'o',
315  "outfile",
316  "FILE",
317  gettext_noop ("file with proof of work, otherwise default is used"),
318  &pwfn),
320  "timeout",
321  "TIME",
322  gettext_noop (
323  "time to wait between calculations"),
326  int ret;
327 
328  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
329  return 2;
330 
331  ret =
332  (GNUNET_OK ==
333  GNUNET_PROGRAM_run (argc,
334  argv,
335  "gnunet-scrypt [OPTIONS] prooffile",
336  gettext_noop ("Manipulate GNUnet proof of work files"),
337  options,
338  &run,
339  NULL))
340  ? 0
341  : 1;
342  GNUNET_free ((void *) argv);
344  return ret;
345 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time)...
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1438
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
Definition of a command line option.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
static char * pkfn
Definition: gnunet-scrypt.c:47
static char * pwfn
Definition: gnunet-scrypt.c:49
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_filename(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a filename (automatically path expanded).
static unsigned long long nse_work_required
Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
Definition: gnunet-scrypt.c:32
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config)
Main function that will be run by the scheduler.
static struct GNUNET_TIME_Relative proof_find_delay
Interval between proof find runs.
Definition: gnunet-scrypt.c:37
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_ulong(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned long long *val)
Allow user to specify an unsigned long long.
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:367
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
Here is the call graph for this function:

Variable Documentation

◆ nse_work_required

unsigned long long nse_work_required
static

Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.

Definition at line 32 of file gnunet-scrypt.c.

Referenced by find_proof(), main(), and run().

◆ proof_find_delay

struct GNUNET_TIME_Relative proof_find_delay
static

Interval between proof find runs.

Definition at line 37 of file gnunet-scrypt.c.

Referenced by find_proof(), main(), and run().

◆ pub

struct GNUNET_CRYPTO_EddsaPublicKey pub
static

◆ proof

uint64_t proof
static

Definition at line 41 of file gnunet-scrypt.c.

Referenced by automaton_create_proofs(), find_proof(), run(), shutdown_task(), and write_proof().

◆ proof_task

struct GNUNET_SCHEDULER_Task* proof_task
static

Definition at line 43 of file gnunet-scrypt.c.

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Definition at line 45 of file gnunet-scrypt.c.

◆ pkfn

char* pkfn
static

Definition at line 47 of file gnunet-scrypt.c.

Referenced by main(), and run().

◆ pwfn

char* pwfn
static

Definition at line 49 of file gnunet-scrypt.c.

Referenced by main(), run(), and shutdown_task().