Advertisement
Guest User

Untitled

a guest
Apr 17th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. // Cuckoo Cycle, a memory-hard proof-of-work
  2. // Copyright (c) 2013-2016 John Tromp
  3.  
  4. #include "cuckoo.h"
  5. #include <inttypes.h> // for SCNx64 macro
  6. #include <stdio.h>    // printf/scanf
  7. #include <stdlib.h>   // exit
  8. #include <unistd.h>   // getopt
  9. #include <assert.h>   // d'uh
  10. #include <string>
  11.  
  12. // arbitrary length of header hashed into siphash key
  13. #define HEADERLEN 80
  14.  
  15. std::string base64_encode(const unsigned char* pch, size_t len)
  16. {
  17.     static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  18.  
  19.     std::string strRet="";
  20.     strRet.reserve((len+2)/3*4);
  21.  
  22.     int mode=0, left=0;
  23.     const unsigned char *pchEnd = pch+len;
  24.  
  25.     while (pch<pchEnd)
  26.     {
  27.         int enc = *(pch++);
  28.         switch (mode)
  29.         {
  30.             case 0: // we have no bits
  31.                 strRet += pbase64[enc >> 2];
  32.                 left = (enc & 3) << 4;
  33.                 mode = 1;
  34.                 break;
  35.  
  36.             case 1: // we have two bits
  37.                 strRet += pbase64[left | (enc >> 4)];
  38.                 left = (enc & 15) << 2;
  39.                 mode = 2;
  40.                 break;
  41.  
  42.             case 2: // we have four bits
  43.                 strRet += pbase64[left | (enc >> 6)];
  44.                 strRet += pbase64[enc & 63];
  45.                 mode = 0;
  46.                 break;
  47.         }
  48.     }
  49.  
  50.     if (mode)
  51.     {
  52.         strRet += pbase64[left];
  53.         strRet += '=';
  54.         if (mode == 1)
  55.             strRet += '=';
  56.     }
  57.  
  58.     return strRet;
  59. }
  60.  
  61. int hex2bin(char *p, const char *hexstr, size_t len)
  62. {
  63.     char hex_byte[3];
  64.     char *ep;
  65.  
  66.     hex_byte[2] = '\0';
  67.  
  68.     while (*hexstr && len) {
  69.         if (!hexstr[1]) {
  70.             //printf("hex2bin str truncated");
  71.             return 0;
  72.         }
  73.         hex_byte[0] = hexstr[0];
  74.         hex_byte[1] = hexstr[1];
  75.         *p = (char)strtol(hex_byte, &ep, 16);
  76.         if (*ep) {
  77.             //printf("hex2bin failed on '%s'", hex_byte);
  78.             return 0;
  79.         }
  80.         p++;
  81.         hexstr += 2;
  82.         len--;
  83.     }
  84.  
  85.     return (len == 0 && *hexstr == 0) ? 1 : 0;
  86. }
  87.  
  88. int main(int argc, char **argv) {
  89.   const char *header = "";
  90.   int nonce = 0;
  91.   int c;
  92.   while ((c = getopt (argc, argv, "h:n:")) != -1) {
  93.     switch (c) {
  94.       case 'h':
  95.         header = optarg;
  96.         break;
  97.       case 'n':
  98.         nonce = atoi(optarg);
  99.         break;
  100.     }
  101.   }
  102.  
  103.   auto sol_arg = strdup("000d26d1,015cdaad,01e72dee,028780c3,02ababc2,0313d14a,032f81b5,0332d3e9,053d5893,05bc994f,05d868ca,05e4aa52,06313303,079c2866,079d5642,09675888,09f8a001,0a19633c,0aacac1f,0ad35993,0c1493c5,0cbdf759,0d7b39aa,0e4e410b,10613f59,1086a03f,119717da,13679470,13e81f98,144a5670,1474b070,14888439,15b3c139,16e44de1,17d904d9,193f4851,19dd7dee,1b338966,1d5c9de1,1e725dab,1f8b5696,1fe826d5");
  104.   auto header_arg = strdup("0000fd9ee9193623a9c331f42ec4f5305d16cdd31ec1e894e5591c79f6af8515");
  105.   std::string xn1 = "0000fc7e";
  106.   std::string xn2 = "000000f4";
  107.   std::string nonce_hex = xn1+xn2;
  108.   nonce = std::stoll(nonce_hex,0,16);
  109.   char bin_header[32]{0};
  110.   hex2bin(bin_header, header_arg, 64);
  111.   char bin_nonce[8]{0};
  112.   hex2bin(bin_nonce, nonce_hex.data(), nonce_hex.size());
  113.   auto header64 = base64_encode((unsigned char*)bin_header,32);
  114.   auto nonce64 = base64_encode((unsigned char*)&bin_nonce,8);
  115.   auto header_nonce_64 = header64 + nonce64;
  116.  
  117.   printf("'%s'\n",header_nonce_64.c_str());
  118.  
  119.   char headernonce[HEADERLEN];
  120.   memset(headernonce, 0, HEADERLEN);
  121.   memcpy(headernonce, header_nonce_64.data(), header_nonce_64.size());
  122.  
  123.   siphash_keys keys;
  124.   setheader(headernonce, sizeof(headernonce), &keys);
  125.   printf("Verifying size %d proof for cuckoo%d(\"%s\",%d)\n",
  126.                PROOFSIZE, EDGEBITS+1, header, nonce);
  127.  
  128.   word_t nonces[PROOFSIZE];
  129.   char* token = strtok(sol_arg, ",");
  130.   for (int n = 0; n < PROOFSIZE && token; n++) {
  131.     word_t micro_nonce = strtoul(token,nullptr,16);
  132.     nonces[n] = micro_nonce;
  133.     token = strtok(NULL, ",");
  134.   }
  135.  
  136.   int pow_rc = verify(nonces, &keys);
  137.   if (pow_rc == POW_OK) {
  138.     printf("Verified with cyclehash ");
  139.     unsigned char cyclehash[32];
  140.     blake2b((void *)cyclehash, sizeof(cyclehash), (const void *)nonces, sizeof(nonces), 0, 0);
  141.     for (int i=0; i<32; i++)
  142.       printf("%02x", cyclehash[i]);
  143.     printf("\n");
  144.   } else {
  145.     printf("FAILED due to %s\n", errstr[pow_rc]);
  146.   }
  147.   return 0;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement