sha256_via.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2010-2011 Jeff Garzik
  3. * Copyright 2012-2013 Luke Dashjr
  4. * Copyright 2011 Con Kolivas
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include "driver-cpu.h"
  12. #include <stdint.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include "miner.h"
  18. #ifdef WANT_VIA_PADLOCK
  19. static void via_sha256(void *hash, void *buf, unsigned len)
  20. {
  21. unsigned stat = 0;
  22. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  23. :"+S"(buf), "+a"(stat)
  24. :"c"(len), "D" (hash)
  25. :"memory");
  26. }
  27. bool scanhash_via(struct thr_info*thr, const unsigned char __maybe_unused *pmidstate,
  28. unsigned char *data_inout,
  29. unsigned char __maybe_unused *phash1, unsigned char __maybe_unused *phash,
  30. const unsigned char *target,
  31. uint32_t max_nonce, uint32_t *last_nonce,
  32. uint32_t n)
  33. {
  34. unsigned char data[128] __attribute__((aligned(128)));
  35. unsigned char tmp_hash[32] __attribute__((aligned(128)));
  36. unsigned char tmp_hash1[32] __attribute__((aligned(128)));
  37. uint32_t *data32 = (uint32_t *) data;
  38. uint32_t *hash32 = (uint32_t *) tmp_hash;
  39. uint32_t *nonce = (uint32_t *)(data + 64 + 12);
  40. uint32_t *nonce_inout = (uint32_t *)(data_inout + 64 + 12);
  41. unsigned long stat_ctr = 0;
  42. /* bitcoin gives us big endian input, but via wants LE,
  43. * so we reverse the swapping bitcoin has already done (extra work)
  44. * in order to permit the hardware to swap everything
  45. * back to BE again (extra work).
  46. */
  47. swap32yes(data32, data_inout, 128/4);
  48. while (1) {
  49. *nonce = n;
  50. /* first SHA256 transform */
  51. memcpy(tmp_hash1, sha256_init_state, 32);
  52. via_sha256(tmp_hash1, data, 80); /* or maybe 128? */
  53. swap32yes(tmp_hash1, tmp_hash1, 32/4);
  54. /* second SHA256 transform */
  55. memcpy(tmp_hash, sha256_init_state, 32);
  56. via_sha256(tmp_hash, tmp_hash1, 32);
  57. stat_ctr++;
  58. if (unlikely((hash32[7] == 0) && fulltest(tmp_hash, target))) {
  59. /* swap nonce'd data back into original storage area;
  60. */
  61. *nonce_inout = bswap_32(n);
  62. *last_nonce = n;
  63. return true;
  64. }
  65. if ((n >= max_nonce) || thr->work_restart) {
  66. *last_nonce = n;
  67. return false;
  68. }
  69. n++;
  70. }
  71. }
  72. #endif /* WANT_VIA_PADLOCK */