github.com/onflow/flow-go/crypto@v0.24.8/hash/xor_unaligned.go (about)

     1  /// The functions below were copied and modified from golang.org/x/crypto/sha3.
     2  //
     3  // Copyright (c) 2009 The Go Authors. All rights reserved.
     4  
     5  // Redistribution and use in source and binary forms, with or without
     6  // modification, are permitted provided that the following conditions are
     7  // met:
     8  
     9  //    * Redistributions of source code must retain the above copyright
    10  // notice, this list of conditions and the following disclaimer.
    11  //    * Redistributions in binary form must reproduce the above
    12  // copyright notice, this list of conditions and the following disclaimer
    13  // in the documentation and/or other materials provided with the
    14  // distribution.
    15  //    * Neither the name of Google Inc. nor the names of its
    16  // contributors may be used to endorse or promote products derived from
    17  // this software without specific prior written permission.
    18  
    19  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    20  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    21  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    22  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    23  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    24  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    25  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    26  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    27  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    28  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    29  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30  
    31  //go:build (amd64 || 386 || ppc64le) && !purego
    32  // +build amd64 386 ppc64le
    33  // +build !purego
    34  
    35  package hash
    36  
    37  import "unsafe"
    38  
    39  // A storageBuf is an aligned array of maxRate bytes.
    40  type storageBuf [maxRate / 8]uint64
    41  
    42  //go:nocheckptr ignore "pointer arithmetic result points to invalid allocation"
    43  func (b *storageBuf) asBytes() *[maxRate]byte {
    44  	// re-using a trick from https://github.com/golang/go/blob/master/src/runtime/stubs.go#L178:
    45  	// to hide the input pointer from escape analysis and avoid
    46  	// an escape to to the heap. The 0 xor tricks the escape analysis tool
    47  	// to think "ptr" and "b" are not related.
    48  	ptr := uintptr(unsafe.Pointer(b)) ^ 0 // nolint:staticcheck
    49  	return (*[maxRate]byte)(unsafe.Pointer(ptr))
    50  }
    51  
    52  // xorIn uses unaligned reads and writes to update d.a to contain d.a
    53  // XOR buf.
    54  func xorIn(d *spongeState, buf []byte) {
    55  	n := len(buf)
    56  	bw := (*[maxRate / 8]uint64)(unsafe.Pointer(&buf[0]))[: n/8 : n/8]
    57  
    58  	d.a[0] ^= bw[0]
    59  	d.a[1] ^= bw[1]
    60  	d.a[2] ^= bw[2]
    61  	d.a[3] ^= bw[3]
    62  	d.a[4] ^= bw[4]
    63  	d.a[5] ^= bw[5]
    64  	d.a[6] ^= bw[6]
    65  	d.a[7] ^= bw[7]
    66  	d.a[8] ^= bw[8]
    67  	d.a[9] ^= bw[9]
    68  	d.a[10] ^= bw[10]
    69  	d.a[11] ^= bw[11]
    70  	d.a[12] ^= bw[12]
    71  	if n >= 136 {
    72  		d.a[13] ^= bw[13]
    73  		d.a[14] ^= bw[14]
    74  		d.a[15] ^= bw[15]
    75  		d.a[16] ^= bw[16]
    76  	}
    77  }
    78  
    79  func copyOut(buf []byte, d *spongeState) {
    80  	ab := (*[maxRate]uint8)(unsafe.Pointer(&d.a[0]))
    81  	copy(buf, ab[:])
    82  }