8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
#include "randombytes.h"
#include "tweetnacl.h"
#include "blake2.h"
#define NANO_SECRET_KEY_LENGTH (crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES)
#define NANO_PUBLIC_KEY_LENGTH (crypto_sign_PUBLICKEYBYTES)
#define NANO_BLOCK_HASH_LENGTH 32
#define NANO_BLOCK_SIGNATURE_LENGTH crypto_sign_BYTES
#define NANO_WORK_VALUE_LENGTH 8
#define NANO_WORK_HASH_LENGTH 8
#define NANO_WORK_DEFAULT_MIN 0xffffffc000000000LLU
#define TclNano_AttemptAlloc(x) ((void *) Tcl_AttemptAlloc(x))
#define TclNano_Free(x) Tcl_Free((char *) x)
#define TclNano_SetIntVar(interp, name, intValue) \
tclobj_ret = Tcl_SetVar2Ex(interp, name, NULL, Tcl_NewIntObj(intValue), TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
}
#define TclNano_CreateNamespace(interp, name) \
tclobj_ret = Tcl_CreateNamespace(interp, name, NULL, NULL); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
}
#define TclNano_CreateObjCommand(interp, name, functionName) \
tclobj_ret = Tcl_CreateObjCommand(interp, name, functionName, NULL, NULL); \
if (!tclobj_ret) { \
return(TCL_ERROR); \
}
#define TclNano_Eval(interp, script) \
tclcmd_ret = Tcl_Eval(interp, script); \
if (tclcmd_ret != TCL_OK) { \
return(tclcmd_ret); \
}
#define TclNano_PkgProvide(interp, name, version) \
tclcmd_ret = Tcl_PkgProvide(interp, name, version); \
if (tclcmd_ret != TCL_OK) { \
return(tclcmd_ret); \
}
static unsigned char *nano_parse_secret_key(Tcl_Obj *secret_key_only_obj, int *out_key_length) {
unsigned char *secret_key, *public_key, *secret_key_only;
int secret_key_length, secret_key_only_length;
secret_key_only = Tcl_GetByteArrayFromObj(secret_key_only_obj, &secret_key_only_length);
if (secret_key_only_length != NANO_SECRET_KEY_LENGTH) {
|
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
+
-
-
-
+
-
-
-
+
-
-
-
+
-
|
return(TCL_OK);
/* NOTREACH */
clientData = clientData;
}
int Nano_Init(Tcl_Interp *interp) {
void *tclobj_ret;
int te_ret, tpp_ret;
int tclcmd_ret;
const char nanoInitScript[] = {
#include "nano.tcl.h"
0x00
};
#ifdef USE_TCL_STUBS
const char *tclInitStubs_ret;
/* Initialize Stubs */
tclInitStubs_ret = Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0);
if (!tclInitStubs_ret) {
return(TCL_ERROR);
}
#endif
if (!interp) {
return(TCL_OK);
}
TclNano_CreateNamespace(interp, "::nano");
TclNano_CreateNamespace(interp, "::nano::block");
TclNano_CreateNamespace(interp, "::nano::key");
TclNano_CreateNamespace(interp, "::nano::work");
TclNano_SetIntVar(interp, "::nano::block::hashLength", NANO_BLOCK_HASH_LENGTH);
TclNano_SetIntVar(interp, "::nano::block::signatureLength", NANO_BLOCK_SIGNATURE_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::publicKeyLength", NANO_PUBLIC_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::privateKeyLength", NANO_SECRET_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::key::seedLength", NANO_SECRET_KEY_LENGTH);
TclNano_SetIntVar(interp, "::nano::work::workValueLength", NANO_WORK_VALUE_LENGTH);
TclNano_SetIntVar(interp, "::nano::work::workHashLength", NANO_WORK_HASH_LENGTH);
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);
TclNano_CreateObjCommand(interp, "::nano::internal::selfTest", nano_tcl_self_test);
TclNano_CreateObjCommand(interp, "::nano::internal::generateKey", nano_tcl_generate_keypair);
TclNano_CreateObjCommand(interp, "::nano::internal::generateSeed", nano_tcl_generate_seed);
TclNano_CreateObjCommand(interp, "::nano::internal::publicKey", nano_tcl_secret_key_to_public_key);
TclNano_CreateObjCommand(interp, "::nano::internal::signDetached", nano_tcl_sign_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::verifyDetached", nano_tcl_verify_detached);
TclNano_CreateObjCommand(interp, "::nano::internal::hashData", nano_tcl_hash_data);
TclNano_CreateObjCommand(interp, "::nano::internal::validateWork", nano_tcl_validate_work);
TclNano_CreateObjCommand(interp, "::nano::internal::generateWork", nano_tcl_generate_work);
if (interp) {
te_ret = Tcl_Eval(interp, nanoInitScript);
TclNano_Eval(interp, nanoInitScript);
if (te_ret != TCL_OK) {
return(te_ret);
}
}
tpp_ret = Tcl_PkgProvide(interp, "nano", PACKAGE_VERSION);
TclNano_PkgProvide(interp, "nano", PACKAGE_VERSION);
if (tpp_ret != TCL_OK) {
return(tpp_ret);
}
return(TCL_OK);
}
|