github.com/onflow/flow-go/crypto@v0.24.8/hash/xor_generic.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 purego 33 34 package hash 35 36 import "encoding/binary" 37 38 // A storageBuf is an aligned array of maxRate bytes. 39 type storageBuf [maxRate]byte 40 41 func (b *storageBuf) asBytes() *[maxRate]byte { 42 return (*[maxRate]byte)(b) 43 } 44 45 // xorIn xors the bytes in buf into the state; it 46 // makes no non-portable assumptions about memory layout 47 // or alignment. 48 func xorIn(d *spongeState, buf []byte) { 49 n := len(buf) / 8 50 51 for i := 0; i < n; i++ { 52 a := binary.LittleEndian.Uint64(buf) 53 d.a[i] ^= a 54 buf = buf[8:] 55 } 56 } 57 58 // copyOut copies ulint64s to a byte buffer. 59 func copyOut(b []byte, d *spongeState) { 60 for i := 0; len(b) >= 8; i++ { 61 binary.LittleEndian.PutUint64(b, d.a[i]) 62 b = b[8:] 63 } 64 }