2 This file is part of GNUnet.
3 Copyright (C) 2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file util/gnunet-scrypt.c
22 * @brief tool to manipulate SCRYPT proofs of work.
26 #include "gnunet_util_lib.h"
30 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
32 static unsigned long long nse_work_required;
35 * Interval between proof find runs.
37 static struct GNUNET_TIME_Relative proof_find_delay;
39 static struct GNUNET_CRYPTO_EddsaPublicKey pub;
41 static uint64_t proof;
43 static struct GNUNET_SCHEDULER_Task * proof_task;
45 static const struct GNUNET_CONFIGURATION_Handle *cfg;
53 * Write our current proof to disk.
59 GNUNET_DISK_fn_write (pwfn, &proof, sizeof (proof),
60 GNUNET_DISK_PERM_USER_READ |
61 GNUNET_DISK_PERM_USER_WRITE))
62 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof);
67 * Calculate the 'proof-of-work' hash (an expensive hash).
69 * @param buf data to hash
70 * @param buf_len number of bytes in @a buf
71 * @param result where to write the resulting hash
74 pow_hash (const void *buf,
76 struct GNUNET_HashCode *result)
79 gcry_kdf_derive (buf, buf_len,
82 "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
83 2 /* iterations; keep cost of individual op small */,
84 sizeof (struct GNUNET_HashCode), result));
89 * Count the leading zeroes in hash.
91 * @param hash to count leading zeros in
92 * @return the number of leading zero bits.
95 count_leading_zeroes (const struct GNUNET_HashCode *hash)
97 unsigned int hash_count;
100 while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
107 * Find our proof of work.
109 * @param cls closure (unused)
110 * @param tc task context
113 find_proof (void *cls,
114 const struct GNUNET_SCHEDULER_TaskContext *tc)
116 #define ROUND_SIZE 10
118 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
119 sizeof (uint64_t)] GNUNET_ALIGN;
120 struct GNUNET_HashCode result;
122 struct GNUNET_TIME_Absolute timestamp;
123 struct GNUNET_TIME_Relative elapsed;
125 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got Proof of Work %llu\n", proof);
132 memcpy (&buf[sizeof (uint64_t)], &pub,
133 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
136 timestamp = GNUNET_TIME_absolute_get ();
137 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
139 memcpy (buf, &counter, sizeof (uint64_t));
140 pow_hash (buf, sizeof (buf), &result);
141 if (nse_work_required <= count_leading_zeroes (&result))
144 FPRINTF (stdout, "Proof of work found: %llu!\n",
145 (unsigned long long) proof);
152 elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
153 elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
155 "Current: %llu [%s/proof]\n",
156 (unsigned long long) counter,
157 GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
158 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n",
161 (unsigned long long) counter);
162 /* remember progress every 100 rounds */
171 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
172 GNUNET_SCHEDULER_PRIORITY_IDLE,
178 * Main function that will be run by the scheduler.
181 * @param args remaining command-line arguments
182 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
183 * @param cfg configuration
189 const struct GNUNET_CONFIGURATION_Handle *config)
191 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
196 /* load proof of work */
200 GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE",
204 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
206 GNUNET_SCHEDULER_shutdown ();
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 "Proof of Work file: %s\n",
213 if ( (GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
215 GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof))))
218 /* load private key */
221 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER",
225 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
226 "PEER", "PRIVATE_KEY");
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
231 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
233 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), pkfn);
238 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
240 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
241 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
246 /* get target bit amount */
247 if (0 == nse_work_required)
250 GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
253 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
254 GNUNET_SCHEDULER_shutdown ();
257 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
259 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS",
260 _("Value is too large.\n"));
261 GNUNET_SCHEDULER_shutdown ();
263 } else if (0 == nse_work_required)
266 GNUNET_SCHEDULER_shutdown ();
270 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Delay between tries: %s\n",
276 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
277 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
283 * Program to manipulate ECC key files.
285 * @param argc number of arguments from the command line
286 * @param argv command line arguments
287 * @return 0 ok, 1 on error
290 main (int argc, char *const *argv)
292 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
293 { 'b', "bits", "BITS",
294 gettext_noop ("number of bits to require for the proof of work"),
295 1, &GNUNET_GETOPT_set_ulong, &nse_work_required },
296 { 'k', "keyfile", "FILE",
297 gettext_noop ("file with private key, otherwise default is used"),
298 1, &GNUNET_GETOPT_set_filename, &pkfn },
299 { 'o', "outfile", "FILE",
300 gettext_noop ("file with proof of work, otherwise default is used"),
301 1, &GNUNET_GETOPT_set_filename, &pwfn },
302 { 't', "timeout", "TIME",
303 gettext_noop ("time to wait between calculations"),
304 1, &GNUNET_GETOPT_set_relative_time, &proof_find_delay },
305 GNUNET_GETOPT_OPTION_END
309 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
313 GNUNET_PROGRAM_run (argc, argv,
314 "gnunet-scrypt [OPTIONS] prooffile",
315 gettext_noop ("Manipulate GNUnet proof of work files"),
316 options, &run, NULL)) ? 0 : 1;
317 GNUNET_free ((void*) argv);
318 GNUNET_free_non_null (pwfn);
322 /* end of gnunet-scrypt.c */