Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Cuckoo Cycle, a memory-hard proof-of-work
- // Copyright (c) 2013-2016 John Tromp
- #include "cuckoo.h"
- #include <inttypes.h> // for SCNx64 macro
- #include <stdio.h> // printf/scanf
- #include <stdlib.h> // exit
- #include <unistd.h> // getopt
- #include <assert.h> // d'uh
- #include <string>
- // arbitrary length of header hashed into siphash key
- #define HEADERLEN 80
- std::string base64_encode(const unsigned char* pch, size_t len)
- {
- static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- std::string strRet="";
- strRet.reserve((len+2)/3*4);
- int mode=0, left=0;
- const unsigned char *pchEnd = pch+len;
- while (pch<pchEnd)
- {
- int enc = *(pch++);
- switch (mode)
- {
- case 0: // we have no bits
- strRet += pbase64[enc >> 2];
- left = (enc & 3) << 4;
- mode = 1;
- break;
- case 1: // we have two bits
- strRet += pbase64[left | (enc >> 4)];
- left = (enc & 15) << 2;
- mode = 2;
- break;
- case 2: // we have four bits
- strRet += pbase64[left | (enc >> 6)];
- strRet += pbase64[enc & 63];
- mode = 0;
- break;
- }
- }
- if (mode)
- {
- strRet += pbase64[left];
- strRet += '=';
- if (mode == 1)
- strRet += '=';
- }
- return strRet;
- }
- int hex2bin(char *p, const char *hexstr, size_t len)
- {
- char hex_byte[3];
- char *ep;
- hex_byte[2] = '\0';
- while (*hexstr && len) {
- if (!hexstr[1]) {
- //printf("hex2bin str truncated");
- return 0;
- }
- hex_byte[0] = hexstr[0];
- hex_byte[1] = hexstr[1];
- *p = (char)strtol(hex_byte, &ep, 16);
- if (*ep) {
- //printf("hex2bin failed on '%s'", hex_byte);
- return 0;
- }
- p++;
- hexstr += 2;
- len--;
- }
- return (len == 0 && *hexstr == 0) ? 1 : 0;
- }
- int main(int argc, char **argv) {
- const char *header = "";
- int nonce = 0;
- int c;
- while ((c = getopt (argc, argv, "h:n:")) != -1) {
- switch (c) {
- case 'h':
- header = optarg;
- break;
- case 'n':
- nonce = atoi(optarg);
- break;
- }
- }
- 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");
- auto header_arg = strdup("0000fd9ee9193623a9c331f42ec4f5305d16cdd31ec1e894e5591c79f6af8515");
- std::string xn1 = "0000fc7e";
- std::string xn2 = "000000f4";
- std::string nonce_hex = xn1+xn2;
- nonce = std::stoll(nonce_hex,0,16);
- char bin_header[32]{0};
- hex2bin(bin_header, header_arg, 64);
- char bin_nonce[8]{0};
- hex2bin(bin_nonce, nonce_hex.data(), nonce_hex.size());
- auto header64 = base64_encode((unsigned char*)bin_header,32);
- auto nonce64 = base64_encode((unsigned char*)&bin_nonce,8);
- auto header_nonce_64 = header64 + nonce64;
- printf("'%s'\n",header_nonce_64.c_str());
- char headernonce[HEADERLEN];
- memset(headernonce, 0, HEADERLEN);
- memcpy(headernonce, header_nonce_64.data(), header_nonce_64.size());
- siphash_keys keys;
- setheader(headernonce, sizeof(headernonce), &keys);
- printf("Verifying size %d proof for cuckoo%d(\"%s\",%d)\n",
- PROOFSIZE, EDGEBITS+1, header, nonce);
- word_t nonces[PROOFSIZE];
- char* token = strtok(sol_arg, ",");
- for (int n = 0; n < PROOFSIZE && token; n++) {
- word_t micro_nonce = strtoul(token,nullptr,16);
- nonces[n] = micro_nonce;
- token = strtok(NULL, ",");
- }
- int pow_rc = verify(nonces, &keys);
- if (pow_rc == POW_OK) {
- printf("Verified with cyclehash ");
- unsigned char cyclehash[32];
- blake2b((void *)cyclehash, sizeof(cyclehash), (const void *)nonces, sizeof(nonces), 0, 0);
- for (int i=0; i<32; i++)
- printf("%02x", cyclehash[i]);
- printf("\n");
- } else {
- printf("FAILED due to %s\n", errstr[pow_rc]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement