sha256_via.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2010-2011 Jeff Garzik
  3. * Copyright 2012-2013 Luke Dashjr
  4. * Copyright 2011-2012 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 "config.h"
  12. #include "driver-cpu.h"
  13. #include <stdbool.h>
  14. #include <stdint.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <sys/time.h>
  19. #include "miner.h"
  20. #ifdef WANT_VIA_PADLOCK
  21. static void via_sha256(void *hash, void *buf, unsigned len)
  22. {
  23. unsigned stat = 0;
  24. asm volatile(".byte 0xf3, 0x0f, 0xa6, 0xd0"
  25. :"+S"(buf), "+a"(stat)
  26. :"c"(len), "D" (hash)
  27. :"memory");
  28. }
  29. bool scanhash_via(struct thr_info * const thr, struct work * const work,
  30. uint32_t max_nonce, uint32_t *last_nonce,
  31. uint32_t n)
  32. {
  33. uint8_t * const data_inout = work->data;
  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)))
  59. {
  60. /* swap nonce'd data back into original storage area;
  61. */
  62. *nonce_inout = bswap_32(n);
  63. *last_nonce = n;
  64. return true;
  65. }
  66. if ((n >= max_nonce) || thr->work_restart) {
  67. *last_nonce = n;
  68. return false;
  69. }
  70. n++;
  71. }
  72. }
  73. #endif /* WANT_VIA_PADLOCK */