320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(result, result_length));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_validate_work(const unsigned char *blockhash, const unsigned char *work, uint64_t workMin) {
unsigned char workReversed[NANO_WORK_VALUE_LENGTH], workCheck[NANO_WORK_HASH_LENGTH];
unsigned int idxIn, idxOut;
blake2b_state workhash_state;
uint64_t workValue;
int blake2_ret;
idxIn = sizeof(workReversed) - 1;
idxOut = 0;
while (idxOut < sizeof(workReversed)) {
workReversed[idxOut] = work[idxIn];
idxOut++;
idxIn--;
}
blake2_ret = blake2b_init(&workhash_state, sizeof(workCheck));
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_update(&workhash_state, workReversed, sizeof(workReversed));
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_update(&workhash_state, blockhash, NANO_BLOCK_HASH_LENGTH);
if (blake2_ret != 0) {
return(0);
}
blake2_ret = blake2b_final(&workhash_state, workCheck, sizeof(workCheck));
if (blake2_ret != 0) {
return(0);
}
workValue = 0;
for (idxIn = sizeof(workCheck); idxIn > 0; idxIn--) {
workValue <<= 8;
workValue |= workCheck[idxIn - 1];
}
if (workValue < workMin) {
/* Fails to meet the requirements */
return(0);
}
return(1);
}
static void nano_generate_work(const unsigned char *blockhash, unsigned char *workOut, uint64_t workMin) {
unsigned char work[NANO_WORK_VALUE_LENGTH];
unsigned int offset;
int work_valid;
memcpy(work, blockhash, sizeof(work));
while (1) {
work_valid = nano_validate_work(blockhash, work, workMin);
if (work_valid) {
break;
}
offset = 0;
while (work[offset] == 0xff) {
work[offset] = 0;
offset++;
offset %= sizeof(work);
}
work[offset] = (((int) work[offset]) + 1) & 0xff;
}
memcpy(workOut, work, sizeof(work));
return;
}
static int nano_tcl_validate_work(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *blockhash, *work;
uint64_t workMin = NANO_WORK_DEFAULT_MIN;
int blockhash_length, work_length;
if (objc < 3 || objc > 4) {
Tcl_WrongNumArgs(interp, 1, objv, "workBlockhash work ?workMin?");
return(TCL_ERROR);
}
blockhash = Tcl_GetByteArrayFromObj(objv[1], &blockhash_length);
if (blockhash_length != NANO_BLOCK_HASH_LENGTH) {
Tcl_SetResult(interp, "Block hash size is wrong", NULL);
return(TCL_ERROR);
}
work = Tcl_GetByteArrayFromObj(objv[2], &work_length);
if (work_length != NANO_WORK_VALUE_LENGTH) {
Tcl_SetResult(interp, "Work size is wrong", NULL);
return(TCL_ERROR);
}
if (objc == 4) {
/* XXX:TODO: Implement getting a uint64_t from Tcl */
Tcl_SetResult(interp, "User-supplied workMin is not implemented", NULL);
return(TCL_ERROR);
}
int valid, result;
valid = nano_validate_work(blockhash, work, workMin);
if (valid) {
result = 1;
} else {
result = 0;
}
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(result));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_generate_work(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
unsigned char *blockhash;
unsigned char work[NANO_WORK_VALUE_LENGTH];
uint64_t workMin = NANO_WORK_DEFAULT_MIN;
int blockhash_length;
if (objc < 2 || objc > 3) {
Tcl_WrongNumArgs(interp, 1, objv, "workBlockhash ?workMin?");
return(TCL_ERROR);
}
blockhash = Tcl_GetByteArrayFromObj(objv[1], &blockhash_length);
if (blockhash_length != NANO_BLOCK_HASH_LENGTH) {
Tcl_SetResult(interp, "Block hash size is wrong", NULL);
return(TCL_ERROR);
}
if (objc == 3) {
/* XXX:TODO: Implement getting a uint64_t from Tcl */
Tcl_SetResult(interp, "User-supplied workMin is not implemented", NULL);
return(TCL_ERROR);
}
nano_generate_work(blockhash, work, workMin);
Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(work, sizeof(work)));
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
static int nano_tcl_self_test(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) {
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, "");
return(TCL_ERROR);
}
|
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
+
+
|
Tcl_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work, NULL, NULL);
Tcl_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work, NULL, NULL);
if (interp) {
te_ret = Tcl_Eval(interp, nanoInitScript);
if (te_ret != TCL_OK) {
return(te_ret);
}
}
|