aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-scrypt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-scrypt.c')
-rw-r--r--src/util/gnunet-scrypt.c325
1 files changed, 0 insertions, 325 deletions
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c
deleted file mode 100644
index ad46e3f39..000000000
--- a/src/util/gnunet-scrypt.c
+++ /dev/null
@@ -1,325 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2014 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file util/gnunet-scrypt.c
22 * @brief tool to manipulate SCRYPT proofs of work.
23 * @author Bart Polot
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include <gcrypt.h>
28
29
30/**
31 * Salt for PoW calcualations.
32 */
33static struct GNUNET_CRYPTO_PowSalt salt = { "gnunet-nse-proof" };
34
35
36/**
37 * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
38 */
39static unsigned long long nse_work_required;
40
41/**
42 * Interval between proof find runs.
43 */
44static struct GNUNET_TIME_Relative proof_find_delay;
45
46static struct GNUNET_CRYPTO_EddsaPublicKey pub;
47
48static uint64_t proof;
49
50static struct GNUNET_SCHEDULER_Task *proof_task;
51
52static const struct GNUNET_CONFIGURATION_Handle *cfg;
53
54static char *pkfn;
55
56static char *pwfn;
57
58
59/**
60 * Write our current proof to disk.
61 *
62 * @param cls closure
63 */
64static void
65shutdown_task (void *cls)
66{
67 (void) cls;
68 if (GNUNET_OK !=
69 GNUNET_DISK_fn_write (pwfn,
70 &proof,
71 sizeof(proof),
72 GNUNET_DISK_PERM_USER_READ
73 | GNUNET_DISK_PERM_USER_WRITE))
74 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
75 "write",
76 pwfn);
77}
78
79
80/**
81 * Find our proof of work.
82 *
83 * @param cls closure (unused)
84 * @param tc task context
85 */
86static void
87find_proof (void *cls)
88{
89#define ROUND_SIZE 10
90 uint64_t counter;
91 char buf[sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)
92 + sizeof(uint64_t)] GNUNET_ALIGN;
93 struct GNUNET_HashCode result;
94 unsigned int i;
95 struct GNUNET_TIME_Absolute timestamp;
96 struct GNUNET_TIME_Relative elapsed;
97
98 (void) cls;
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100 "Got Proof of Work %llu\n",
101 (unsigned long long) proof);
102 proof_task = NULL;
103 GNUNET_memcpy (&buf[sizeof(uint64_t)],
104 &pub,
105 sizeof(struct GNUNET_CRYPTO_EddsaPublicKey));
106 i = 0;
107 counter = proof;
108 timestamp = GNUNET_TIME_absolute_get ();
109 while ((counter != UINT64_MAX) && (i < ROUND_SIZE))
110 {
111 GNUNET_memcpy (buf, &counter, sizeof(uint64_t));
112 GNUNET_CRYPTO_pow_hash (&salt,
113 buf,
114 sizeof(buf),
115 &result);
116 if (nse_work_required <=
117 GNUNET_CRYPTO_hash_count_leading_zeros (&result))
118 {
119 proof = counter;
120 fprintf (stdout,
121 "Proof of work found: %llu!\n",
122 (unsigned long long) proof);
123 GNUNET_SCHEDULER_shutdown ();
124 return;
125 }
126 counter++;
127 i++;
128 }
129 elapsed = GNUNET_TIME_absolute_get_duration (timestamp);
130 elapsed = GNUNET_TIME_relative_divide (elapsed, ROUND_SIZE);
131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
132 "Current: %llu [%s/proof]\n",
133 (unsigned long long) counter,
134 GNUNET_STRINGS_relative_time_to_string (elapsed, 0));
135 if (proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
136 {
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Testing proofs currently at %llu\n",
139 (unsigned long long) counter);
140 /* remember progress every 100 rounds */
141 proof = counter;
142 shutdown_task (NULL);
143 }
144 else
145 {
146 proof = counter;
147 }
148 proof_task =
149 GNUNET_SCHEDULER_add_delayed_with_priority (proof_find_delay,
150 GNUNET_SCHEDULER_PRIORITY_IDLE,
151 &find_proof,
152 NULL);
153}
154
155
156/**
157 * Main function that will be run by the scheduler.
158 *
159 * @param cls closure
160 * @param args remaining command-line arguments
161 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
162 * @param cfg configuration
163 */
164static void
165run (void *cls,
166 char *const *args,
167 const char *cfgfile,
168 const struct GNUNET_CONFIGURATION_Handle *config)
169{
170 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
171 char *pids;
172
173 (void) cls;
174 (void) args;
175 (void) cfgfile;
176 cfg = config;
177 /* load proof of work */
178 if (NULL == pwfn)
179 {
180 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
181 "NSE",
182 "PROOFFILE",
183 &pwfn))
184 {
185 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "PROOFFILE");
186 GNUNET_SCHEDULER_shutdown ();
187 return;
188 }
189 }
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Proof of Work file: %s\n", pwfn);
191 if ((GNUNET_YES != GNUNET_DISK_file_test (pwfn)) ||
192 (sizeof(proof) != GNUNET_DISK_fn_read (pwfn, &proof, sizeof(proof))))
193 proof = 0;
194
195 /* load private key */
196 if (NULL == pkfn)
197 {
198 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
199 "PEER",
200 "PRIVATE_KEY",
201 &pkfn))
202 {
203 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
204 "PEER",
205 "PRIVATE_KEY");
206 return;
207 }
208 }
209 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Private Key file: %s\n", pkfn);
210 if (GNUNET_SYSERR ==
211 GNUNET_CRYPTO_eddsa_key_from_file (pkfn,
212 GNUNET_YES,
213 &pk))
214 {
215 fprintf (stderr, _ ("Loading hostkey from `%s' failed.\n"), pkfn);
216 GNUNET_free (pkfn);
217 return;
218 }
219 GNUNET_free (pkfn);
220 GNUNET_CRYPTO_eddsa_key_get_public (&pk,
221 &pub);
222 pids = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer ID: %s\n", pids);
224 GNUNET_free (pids);
225
226 /* get target bit amount */
227 if (0 == nse_work_required)
228 {
229 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg,
230 "NSE",
231 "WORKBITS",
232 &nse_work_required))
233 {
234 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "NSE", "WORKBITS");
235 GNUNET_SCHEDULER_shutdown ();
236 return;
237 }
238 if (nse_work_required >= sizeof(struct GNUNET_HashCode) * 8)
239 {
240 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
241 "NSE",
242 "WORKBITS",
243 _ ("Value is too large.\n"));
244 GNUNET_SCHEDULER_shutdown ();
245 return;
246 }
247 else if (0 == nse_work_required)
248 {
249 GNUNET_SCHEDULER_shutdown ();
250 return;
251 }
252 }
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Bits: %llu\n", nse_work_required);
254
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Delay between tries: %s\n",
257 GNUNET_STRINGS_relative_time_to_string (proof_find_delay, 1));
258 proof_task =
259 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
260 &find_proof,
261 NULL);
262 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
263}
264
265
266/**
267 * Program to manipulate ECC key files.
268 *
269 * @param argc number of arguments from the command line
270 * @param argv command line arguments
271 * @return 0 ok, 1 on error
272 */
273int
274main (int argc, char *const *argv)
275{
276 struct GNUNET_GETOPT_CommandLineOption options[] = {
277 GNUNET_GETOPT_option_ulong (
278 'b',
279 "bits",
280 "BITS",
281 gettext_noop ("number of bits to require for the proof of work"),
282 &nse_work_required),
283 GNUNET_GETOPT_option_filename (
284 'k',
285 "keyfile",
286 "FILE",
287 gettext_noop ("file with private key, otherwise default is used"),
288 &pkfn),
289 GNUNET_GETOPT_option_filename (
290 'o',
291 "outfile",
292 "FILE",
293 gettext_noop ("file with proof of work, otherwise default is used"),
294 &pwfn),
295 GNUNET_GETOPT_option_relative_time ('t',
296 "timeout",
297 "TIME",
298 gettext_noop (
299 "time to wait between calculations"),
300 &proof_find_delay),
301 GNUNET_GETOPT_OPTION_END
302 };
303 int ret;
304
305 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
306 return 2;
307
308 ret =
309 (GNUNET_OK ==
310 GNUNET_PROGRAM_run (argc,
311 argv,
312 "gnunet-scrypt [OPTIONS] prooffile",
313 gettext_noop ("Manipulate GNUnet proof of work files"),
314 options,
315 &run,
316 NULL))
317 ? 0
318 : 1;
319 GNUNET_free_nz ((void *) argv);
320 GNUNET_free (pwfn);
321 return ret;
322}
323
324
325/* end of gnunet-scrypt.c */