You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xor_unaligned.go 1.3KB

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