2 This file is part of GNUnet.
3 (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.
22 * @file util/gnunet-scrypt.c
23 * @brief tool to manipulate SCRYPT proofs of work.
24 * largely stolen from gnunet-peerinfo.c and gnunet-service-nse.c
28 #include "gnunet_util_lib.h"
32 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
34 static unsigned long long nse_work_required;
37 * Interval between proof find runs.
39 static struct GNUNET_TIME_Relative proof_find_delay;
41 static struct GNUNET_CRYPTO_EddsaPublicKey pub;
43 GNUNET_SCHEDULER_TaskIdentifier proof_task;
44 const struct GNUNET_CONFIGURATION_Handle *cfg;
50 * Write our current proof to disk.
56 GNUNET_DISK_fn_write (pwfn, &proof, sizeof (proof),
57 GNUNET_DISK_PERM_USER_READ |
58 GNUNET_DISK_PERM_USER_WRITE))
59 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", proof);
64 * Calculate the 'proof-of-work' hash (an expensive hash).
66 * @param buf data to hash
67 * @param buf_len number of bytes in @a buf
68 * @param result where to write the resulting hash
71 pow_hash (const void *buf,
73 struct GNUNET_HashCode *result)
76 gcry_kdf_derive (buf, buf_len,
79 "gnunet-proof-of-work", strlen ("gnunet-proof-of-work"),
80 2 /* iterations; keep cost of individual op small */,
81 sizeof (struct GNUNET_HashCode), result));
87 * Count the leading zeroes in hash.
89 * @param hash to count leading zeros in
90 * @return the number of leading zero bits.
93 count_leading_zeroes (const struct GNUNET_HashCode *hash)
95 unsigned int hash_count;
98 while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))
105 * Find our proof of work.
107 * @param cls closure (unused)
108 * @param tc task context
111 find_proof (void *cls,
112 const struct GNUNET_SCHEDULER_TaskContext *tc)
114 #define ROUND_SIZE 10
116 char buf[sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
117 sizeof (uint64_t)] GNUNET_ALIGN;
118 struct GNUNET_HashCode result;
120 struct GNUNET_TIME_Absolute timestamp;
121 struct GNUNET_TIME_Relative elapsed;
123 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got Proof of Work %llu\n", proof);
129 proof_task = GNUNET_SCHEDULER_NO_TASK;
130 memcpy (&buf[sizeof (uint64_t)], &pub,
131 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
134 timestamp = GNUNET_TIME_absolute_get ();
135 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
137 memcpy (buf, &counter, sizeof (uint64_t));
138 pow_hash (buf, sizeof (buf), &result);
139 if (nse_work_required <= count_leading_zeroes (&result))
142 FPRINTF (stdout, "Proof of work found: %llu!\n",
143 (unsigned long long) proof);
150 elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
151 elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
152 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
153 "Current: %llu [%s/proof]\n",
154 (unsigned long long) counter,
155 GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
156 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n",
159 (unsigned long long) counter);
160 /* remember progress every 100 rounds */
169 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
170 GNUNET_SCHEDULER_PRIORITY_IDLE,
176 * Main function that will be run by the scheduler.
179 * @param args remaining command-line arguments
180 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
181 * @param cfg configuration
184 run (void *cls, char *const *args, const char *cfgfile,
185 const struct GNUNET_CONFIGURATION_Handle *config)
187 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
191 /* load proof of work */
194 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "NSE",
198 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
200 GNUNET_SCHEDULER_shutdown ();
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn);
205 if (GNUNET_YES != GNUNET_DISK_file_test (pwfn) ||
206 sizeof (proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof (proof)))
209 /* load private key */
212 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER",
216 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
217 "PEER", "PRIVATE_KEY");
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
222 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create_from_file (pkfn)))
224 FPRINTF (stderr, _("Loading hostkey from `%s' failed.\n"), pkfn);
229 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n",
232 GNUNET_CRYPTO_eddsa_public_key_to_string (&pub));
234 /* get target bit amount */
235 if (0 == nse_work_required)
238 GNUNET_CONFIGURATION_get_value_number (cfg, "NSE", "WORKBITS",
241 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
242 GNUNET_SCHEDULER_shutdown ();
245 if (nse_work_required >= sizeof (struct GNUNET_HashCode) * 8)
247 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS",
248 _("Value is too large.\n"));
249 GNUNET_SCHEDULER_shutdown ();
251 } else if (0 == nse_work_required)
254 GNUNET_SCHEDULER_shutdown ();
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Delay between tries: %s\n",
261 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
262 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
268 * Program to manipulate ECC key files.
270 * @param argc number of arguments from the command line
271 * @param argv command line arguments
272 * @return 0 ok, 1 on error
275 main (int argc, char *const *argv)
277 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
278 { 'b', "bits", "BITS",
279 gettext_noop ("number of bits to require for the proof of work"),
280 1, &GNUNET_GETOPT_set_ulong, &nse_work_required },
281 { 'k', "keyfile", "FILE",
282 gettext_noop ("file with private key, otherwise default is used"),
283 1, &GNUNET_GETOPT_set_filename, &pkfn },
284 { 'o', "outfile", "FILE",
285 gettext_noop ("file with proof of work, otherwise default is used"),
286 1, &GNUNET_GETOPT_set_filename, &pwfn },
287 { 't', "timeout", "TIME",
288 gettext_noop ("time to wait between calculations"),
289 1, &GNUNET_GETOPT_set_relative_time, &proof_find_delay },
290 GNUNET_GETOPT_OPTION_END
294 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
298 GNUNET_PROGRAM_run (argc, argv, "gnunet-scrypt [OPTIONS] prooffile",
299 gettext_noop ("Manipulate GNUnet proof of work files"),
300 options, &run, NULL)) ? 0 : 1;
301 GNUNET_free ((void*) argv);
302 GNUNET_free_non_null (pwfn);
306 /* end of gnunet-ecc.c */