sha256_via.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const unsigned char *target,
  18. uint32_t max_nonce, unsigned long *hashes_done)
  19. {
  20. unsigned char data[128] __attribute__((aligned(128)));
  21. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  22. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  23. uint32_t *data32 = (uint32_t *) data;
  24. uint32_t *hash32 = (uint32_t *) tmp_hash;
  25. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  26. uint32_t n = 0;
  27. unsigned long stat_ctr = 0;
  28. int i;
  29. /* bitcoin gives us big endian input, but via wants LE,
  30. * so we reverse the swapping bitcoin has already done (extra work)
  31. * in order to permit the hardware to swap everything
  32. * back to BE again (extra work).
  33. */
  34. for (i = 0; i < 128/4; i++)
  35. data32[i] = swab32(((uint32_t *)data_inout)[i]);
  36. while (1) {
  37. n++;
  38. *nonce = n;
  39. /* first SHA256 transform */
  40. memcpy(tmp_hash1, sha256_init_state, 32);
  41. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  42. for (i = 0; i < 32/4; i++)
  43. ((uint32_t *)tmp_hash1)[i] =
  44. swab32(((uint32_t *)tmp_hash1)[i]);
  45. /* second SHA256 transform */
  46. memcpy(tmp_hash, sha256_init_state, 32);
  47. via_sha256(tmp_hash, tmp_hash1, 32);
  48. stat_ctr++;
  49. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  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. *hashes_done = stat_ctr;
  62. return false;
  63. }
  64. }
  65. }
  66. #endif /* WANT_VIA_PADLOCK */