xor_unaligned.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. //go:build (amd64 || 386 || ppc64le) && !purego
  5. // +build amd64 386 ppc64le
  6. // +build !purego
  7. package sha3
  8. import "unsafe"
  9. // A storageBuf is an aligned array of maxRate bytes.
  10. type storageBuf [maxRate / 8]uint64
  11. func (b *storageBuf) asBytes() *[maxRate]byte {
  12. return (*[maxRate]byte)(unsafe.Pointer(b))
  13. }
  14. // xorInUnaligned uses unaligned reads and writes to update d.a to contain d.a
  15. // XOR buf.
  16. func xorInUnaligned(d *state, buf []byte) {
  17. n := len(buf)
  18. bw := (*[maxRate / 8]uint64)(unsafe.Pointer(&buf[0]))[: n/8 : n/8]
  19. if n >= 72 {
  20. d.a[0] ^= bw[0]
  21. d.a[1] ^= bw[1]
  22. d.a[2] ^= bw[2]
  23. d.a[3] ^= bw[3]
  24. d.a[4] ^= bw[4]
  25. d.a[5] ^= bw[5]
  26. d.a[6] ^= bw[6]
  27. d.a[7] ^= bw[7]
  28. d.a[8] ^= bw[8]
  29. }
  30. if n >= 104 {
  31. d.a[9] ^= bw[9]
  32. d.a[10] ^= bw[10]
  33. d.a[11] ^= bw[11]
  34. d.a[12] ^= bw[12]
  35. }
  36. if n >= 136 {
  37. d.a[13] ^= bw[13]
  38. d.a[14] ^= bw[14]
  39. d.a[15] ^= bw[15]
  40. d.a[16] ^= bw[16]
  41. }
  42. if n >= 144 {
  43. d.a[17] ^= bw[17]
  44. }
  45. if n >= 168 {
  46. d.a[18] ^= bw[18]
  47. d.a[19] ^= bw[19]
  48. d.a[20] ^= bw[20]
  49. }
  50. }
  51. func copyOutUnaligned(d *state, buf []byte) {
  52. ab := (*[maxRate]uint8)(unsafe.Pointer(&d.a[0]))
  53. copy(buf, ab[:])
  54. }
  55. var (
  56. xorIn = xorInUnaligned
  57. copyOut = copyOutUnaligned
  58. )
  59. const xorImplementationUnaligned = "unaligned"