xor_unaligned.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build amd64 386 ppc64le
  5. // +build !appengine
  6. package sha3
  7. import "unsafe"
  8. // A storageBuf is an aligned array of maxRate bytes.
  9. type storageBuf [maxRate / 8]uint64
  10. func (b *storageBuf) asBytes() *[maxRate]byte {
  11. return (*[maxRate]byte)(unsafe.Pointer(b))
  12. }
  13. //go:nocheckptr
  14. //
  15. // xorInUnaligned intentionally reads the input buffer as an unaligned slice of
  16. // integers. The language spec is not clear on whether that is allowed.
  17. // See:
  18. // https://golang.org/issue/37644
  19. // https://golang.org/issue/37298
  20. // https://golang.org/issue/35381
  21. // xorInUnaligned uses unaligned reads and writes to update d.a to contain d.a
  22. // XOR buf.
  23. func xorInUnaligned(d *state, buf []byte) {
  24. n := len(buf)
  25. bw := (*[maxRate / 8]uint64)(unsafe.Pointer(&buf[0]))[: n/8 : n/8]
  26. if n >= 72 {
  27. d.a[0] ^= bw[0]
  28. d.a[1] ^= bw[1]
  29. d.a[2] ^= bw[2]
  30. d.a[3] ^= bw[3]
  31. d.a[4] ^= bw[4]
  32. d.a[5] ^= bw[5]
  33. d.a[6] ^= bw[6]
  34. d.a[7] ^= bw[7]
  35. d.a[8] ^= bw[8]
  36. }
  37. if n >= 104 {
  38. d.a[9] ^= bw[9]
  39. d.a[10] ^= bw[10]
  40. d.a[11] ^= bw[11]
  41. d.a[12] ^= bw[12]
  42. }
  43. if n >= 136 {
  44. d.a[13] ^= bw[13]
  45. d.a[14] ^= bw[14]
  46. d.a[15] ^= bw[15]
  47. d.a[16] ^= bw[16]
  48. }
  49. if n >= 144 {
  50. d.a[17] ^= bw[17]
  51. }
  52. if n >= 168 {
  53. d.a[18] ^= bw[18]
  54. d.a[19] ^= bw[19]
  55. d.a[20] ^= bw[20]
  56. }
  57. }
  58. func copyOutUnaligned(d *state, buf []byte) {
  59. ab := (*[maxRate]uint8)(unsafe.Pointer(&d.a[0]))
  60. copy(buf, ab[:])
  61. }
  62. var (
  63. xorIn = xorInUnaligned
  64. copyOut = copyOutUnaligned
  65. )
  66. const xorImplementationUnaligned = "unaligned"