sha256_via.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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,
  17. uint32_t max_nonce, unsigned long *hashes_done)
  18. {
  19. unsigned char data[128] __attribute__((aligned(128)));
  20. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  21. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  22. uint32_t *data32 = (uint32_t *) data;
  23. uint32_t *hash32 = (uint32_t *) tmp_hash;
  24. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  25. uint32_t n = 0;
  26. unsigned long stat_ctr = 0;
  27. int i;
  28. /* bitcoin gives us big endian input, but via wants LE,
  29. * so we reverse the swapping bitcoin has already done (extra work)
  30. * in order to permit the hardware to swap everything
  31. * back to BE again (extra work).
  32. */
  33. for (i = 0; i < 128/4; i++)
  34. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  35. while (1) {
  36. n++;
  37. *nonce = n;
  38. /* first SHA256 transform */
  39. memcpy(tmp_hash1, sha256_init_state, 32);
  40. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  41. for (i = 0; i < 32/4; i++)
  42. ((uint32_t *)tmp_hash1)[i] =
  43. swab32(((uint32_t *)tmp_hash1)[i]);
  44. /* second SHA256 transform */
  45. memcpy(tmp_hash, sha256_init_state, 32);
  46. via_sha256(tmp_hash, tmp_hash1, 32);
  47. stat_ctr++;
  48. if (hash32[7] == 0) {
  49. print_pow(tmp_hash);
  50. /* swap nonce'd data back into original storage area;
  51. * TODO: only swap back the nonce, rather than all data
  52. */
  53. for (i = 0; i < 128/4; i++) {
  54. uint32_t *dout32 = (uint32_t *) data_inout;
  55. dout32[i] = swab32(data32[i]);
  56. }
  57. *hashes_done = stat_ctr;
  58. return true;
  59. }
  60. if (n >= max_nonce) {
  61. if (opt_debug)
  62. fprintf(stderr, "DBG: end of nonce range\n");
  63. *hashes_done = stat_ctr;
  64. return false;
  65. }
  66. }
  67. }
  68. #endif /* WANT_VIA_PADLOCK */