sha256_via.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <sys/time.h>
  6. #include "miner.h"
  7. #ifdef WANT_VIA_PADLOCK
  8. static void via_sha256(void *hash, void *buf, unsigned len)
  9. {
  10. unsigned stat = 0;
  11. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  12. :"+S"(buf), "+a"(stat)
  13. :"c"(len), "D" (hash)
  14. :"memory");
  15. }
  16. bool scanhash_via(unsigned char *data_inout, unsigned long *hashes_done)
  17. {
  18. unsigned char data[128] __attribute__((aligned(128)));
  19. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  20. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  21. uint32_t *data32 = (uint32_t *) data;
  22. uint32_t *hash32 = (uint32_t *) tmp_hash;
  23. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  24. uint32_t n = 0;
  25. unsigned long stat_ctr = 0;
  26. int i;
  27. /* bitcoin gives us big endian input, but via wants LE,
  28. * so we reverse the swapping bitcoin has already done (extra work)
  29. * in order to permit the hardware to swap everything
  30. * back to BE again (extra work).
  31. */
  32. for (i = 0; i < 128/4; i++)
  33. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  34. while (1) {
  35. n++;
  36. *nonce = n;
  37. /* first SHA256 transform */
  38. memcpy(tmp_hash1, sha256_init_state, 32);
  39. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  40. for (i = 0; i < 32/4; i++)
  41. ((uint32_t *)tmp_hash1)[i] =
  42. swab32(((uint32_t *)tmp_hash1)[i]);
  43. /* second SHA256 transform */
  44. memcpy(tmp_hash, sha256_init_state, 32);
  45. via_sha256(tmp_hash, tmp_hash1, 32);
  46. stat_ctr++;
  47. if (hash32[7] == 0) {
  48. char *hexstr;
  49. hexstr = bin2hex(tmp_hash, 32);
  50. fprintf(stderr,
  51. "DBG: found zeroes in hash:\n%s\n",
  52. hexstr);
  53. free(hexstr);
  54. /* swap nonce'd data back into original storage area;
  55. * TODO: only swap back the nonce, rather than all data
  56. */
  57. for (i = 0; i < 128/4; i++) {
  58. uint32_t *dout32 = (uint32_t *) data_inout;
  59. dout32[i] = swab32(data32[i]);
  60. }
  61. *hashes_done = stat_ctr;
  62. return true;
  63. }
  64. if ((n & 0xffffff) == 0) {
  65. if (opt_debug)
  66. fprintf(stderr, "DBG: end of nonce range\n");
  67. *hashes_done = stat_ctr;
  68. return false;
  69. }
  70. }
  71. }
  72. #endif /* WANT_VIA_PADLOCK */