Advertisement
aidanozo

Untitled

May 10th, 2024
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "../include/utils.cuh"
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <inttypes.h>
  7.  
  8. // TODO: Implement function to search for all nonces from 1 through MAX_NONCE (inclusive) using CUDA Threads
  9.  
  10. //extern BYTE difficulty_5_zeros[SHA256_HASH_SIZE];
  11. //extern void printResult(BYTE *block_hash, uint64_t nonce, float seconds);
  12.  
  13. BYTE *d_block_content, *d_block_hash;
  14. uint64_t *d_nonce;
  15.  
  16. __global__ void findNonce(BYTE* block_content, size_t current_length, BYTE* block_hash, uint64_t* nonce) {
  17.  
  18.         BYTE difficulty[SHA256_HASH_SIZE] = "0000000000000000000000000000000000000000000000000000000000000000";
  19.         int index = blockIdx.x * blockDim.x + threadIdx.x;
  20.  
  21.         BYTE c_block_content[BLOCK_SIZE + 30], c_block_hash[SHA256_HASH_SIZE];
  22.  
  23.         d_strcpy((char *)c_block_content, (const char *)block_content);
  24.  
  25.  
  26.         if (index <= MAX_NONCE) {
  27.                 char nonce_string[NONCE_SIZE];
  28.  
  29.                 intToString(index, nonce_string);
  30.                 d_strcpy((char*)c_block_content + current_length, nonce_string);
  31.  
  32.                 apply_sha256(c_block_content, current_length + d_strlen(nonce_string), c_block_hash, 1);
  33.  
  34.                 if (compare_hashes(c_block_hash, difficulty) <= 0){
  35.                         //printf("Block hash found: %s, below difficulty %s, for nonce %" PRIu64 "\n", block_hash, difficulty_5_zeros, *nonce);
  36.                         //printResult(block_hash, *nonce, 0.0);
  37.                         //(*nonce)++;
  38.                         *nonce = index;
  39.                         d_strcpy((char *)block_hash, (const char *)c_block_hash);
  40.                         return;
  41.                 }
  42.         }
  43. }
  44.  
  45. int main(int argc, char **argv) {
  46.         BYTE hashed_tx1[SHA256_HASH_SIZE], hashed_tx2[SHA256_HASH_SIZE], hashed_tx3[SHA256_HASH_SIZE], hashed_tx4[SHA256_HASH_SIZE],
  47.                         tx12[SHA256_HASH_SIZE * 2], tx34[SHA256_HASH_SIZE * 2], hashed_tx12[SHA256_HASH_SIZE], hashed_tx34[SHA256_HASH_SIZE],
  48.                         tx1234[SHA256_HASH_SIZE * 2], top_hash[SHA256_HASH_SIZE], block_content[BLOCK_SIZE];
  49.         BYTE block_hash[SHA256_HASH_SIZE]; // TODO: Update
  50.         uint64_t nonce = 0; // TODO: Update
  51.         size_t current_length;
  52.  
  53.         cudaMalloc((void**)&d_block_content, BLOCK_SIZE);
  54.         cudaMalloc((void**)&d_block_hash, SHA256_HASH_SIZE);
  55.         cudaMalloc((void**)&d_nonce, sizeof(uint64_t));
  56.  
  57.         // Top hash
  58.         apply_sha256(tx1, strlen((const char*)tx1), hashed_tx1, 1);
  59.         apply_sha256(tx2, strlen((const char*)tx2), hashed_tx2, 1);
  60.         apply_sha256(tx3, strlen((const char*)tx3), hashed_tx3, 1);
  61.         apply_sha256(tx4, strlen((const char*)tx4), hashed_tx4, 1);
  62.         strcpy((char *)tx12, (const char *)hashed_tx1);
  63.         strcat((char *)tx12, (const char *)hashed_tx2);
  64.         apply_sha256(tx12, strlen((const char*)tx12), hashed_tx12, 1);
  65.         strcpy((char *)tx34, (const char *)hashed_tx3);
  66.         strcat((char *)tx34, (const char *)hashed_tx4);
  67.         apply_sha256(tx34, strlen((const char*)tx34), hashed_tx34, 1);
  68.         strcpy((char *)tx1234, (const char *)hashed_tx12);
  69.         strcat((char *)tx1234, (const char *)hashed_tx34);
  70.         apply_sha256(tx1234, strlen((const char*)tx34), top_hash, 1);
  71.  
  72.         // prev_block_hash + top_hash
  73.         strcpy((char*)block_content, (const char*)prev_block_hash);
  74.         strcat((char*)block_content, (const char*)top_hash);
  75.         current_length = strlen((char*) block_content);
  76.  
  77.         cudaEvent_t start, stop;
  78.         startTiming(&start, &stop);
  79.  
  80.         cudaMemcpy(d_block_hash, block_hash, SHA256_HASH_SIZE, cudaMemcpyHostToDevice);
  81.  
  82.         cudaMemcpy(d_block_content, block_content, BLOCK_SIZE, cudaMemcpyHostToDevice);
  83.         cudaMemcpy(d_nonce, &nonce, sizeof(uint64_t), cudaMemcpyHostToDevice);
  84.  
  85.         const size_t block_size = 300;
  86.         size_t blocks_no = 100000000 / block_size;
  87.  
  88.         if (100000000 % block_size)
  89.                 ++blocks_no;
  90.  
  91.         findNonce<<<blocks_no, block_size>>>(d_block_content, current_length, d_block_hash, d_nonce);
  92.  
  93.         cudaDeviceSynchronize();
  94.  
  95.         float seconds = stopTiming(&start, &stop);
  96.  
  97.         cudaMemcpy(block_hash, d_block_hash, SHA256_HASH_SIZE, cudaMemcpyDeviceToHost);
  98.         cudaMemcpy(&nonce, d_nonce, sizeof(uint64_t), cudaMemcpyDeviceToHost);
  99.  
  100.         cudaFree(d_block_content);
  101.         cudaFree(d_block_hash);
  102.         cudaFree(d_nonce);
  103.  
  104.         //float seconds = stopTiming(&start, &stop);
  105.         printResult(block_hash, nonce, seconds);
  106.  
  107.         return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement