sha256_via.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "config.h"
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <sys/time.h>
  7. #include "miner.h"
  8. #ifdef WANT_VIA_PADLOCK
  9. static void via_sha256(void *hash, void *buf, unsigned len)
  10. {
  11. unsigned stat = 0;
  12. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  13. :"+S"(buf), "+a"(stat)
  14. :"c"(len), "D" (hash)
  15. :"memory");
  16. }
  17. bool scanhash_via(int thr_id, const unsigned char *pmidstate,
  18. unsigned char *data_inout,
  19. unsigned char *phash1, unsigned char *phash,
  20. const unsigned char *target,
  21. uint32_t max_nonce, uint32_t *last_nonce,
  22. uint32_t n)
  23. {
  24. unsigned char data[128] __attribute__((aligned(128)));
  25. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  26. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  27. uint32_t *data32 = (uint32_t *) data;
  28. uint32_t *hash32 = (uint32_t *) tmp_hash;
  29. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  30. unsigned long stat_ctr = 0;
  31. int i;
  32. work_restart[thr_id].restart = 0;
  33. /* bitcoin gives us big endian input, but via wants LE,
  34. * so we reverse the swapping bitcoin has already done (extra work)
  35. * in order to permit the hardware to swap everything
  36. * back to BE again (extra work).
  37. */
  38. for (i = 0; i < 128/4; i++)
  39. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  40. while (1) {
  41. n++;
  42. *nonce = n;
  43. /* first SHA256 transform */
  44. memcpy(tmp_hash1, sha256_init_state, 32);
  45. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  46. for (i = 0; i < 32/4; i++)
  47. ((uint32_t *)tmp_hash1)[i] =
  48. swab32(((uint32_t *)tmp_hash1)[i]);
  49. /* second SHA256 transform */
  50. memcpy(tmp_hash, sha256_init_state, 32);
  51. via_sha256(tmp_hash, tmp_hash1, 32);
  52. stat_ctr++;
  53. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  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. *last_nonce = n;
  62. return true;
  63. }
  64. if ((n >= max_nonce) || work_restart[thr_id].restart) {
  65. *last_nonce = n;
  66. return false;
  67. }
  68. }
  69. }
  70. #endif /* WANT_VIA_PADLOCK */