github.com/deso-protocol/core@v1.2.9/desohash/sha3m/xor.go (about)

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