sha256_via.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "driver-cpu.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(struct thr_info*thr, const unsigned char __maybe_unused *pmidstate,
  18. unsigned char *data_inout,
  19. unsigned char __maybe_unused *phash1, unsigned char __maybe_unused *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. /* bitcoin gives us big endian input, but via wants LE,
  32. * so we reverse the swapping bitcoin has already done (extra work)
  33. * in order to permit the hardware to swap everything
  34. * back to BE again (extra work).
  35. */
  36. swap32yes(data32, data_inout, 128/4);
  37. while (1) {
  38. n++;
  39. *nonce = n;
  40. /* first SHA256 transform */
  41. memcpy(tmp_hash1, sha256_init_state, 32);
  42. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  43. swap32yes(tmp_hash1, tmp_hash1, 32/4);
  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 (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  49. /* swap nonce'd data back into original storage area;
  50. * TODO: only swap back the nonce, rather than all data
  51. */
  52. swap32yes(data_inout, data32, 128/4);
  53. *last_nonce = n;
  54. return true;
  55. }
  56. if ((n >= max_nonce) || thr->work_restart) {
  57. *last_nonce = n;
  58. return false;
  59. }
  60. }
  61. }
  62. #endif /* WANT_VIA_PADLOCK */