git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/internal/blake3/compress_noasm.go (about) 1 //go:build !amd64 2 // +build !amd64 3 4 package blake3 5 6 import "encoding/binary" 7 8 func compressNode(n node) (out [16]uint32) { 9 compressNodeGeneric(&out, n) 10 return 11 } 12 13 func compressBuffer(buf *[maxSIMD * chunkSize]byte, buflen int, key *[8]uint32, counter uint64, flags uint32) node { 14 return compressBufferGeneric(buf, buflen, key, counter, flags) 15 } 16 17 func compressChunk(chunk []byte, key *[8]uint32, counter uint64, flags uint32) node { 18 n := node{ 19 cv: *key, 20 counter: counter, 21 blockLen: blockSize, 22 flags: flags | flagChunkStart, 23 } 24 var block [blockSize]byte 25 for len(chunk) > blockSize { 26 copy(block[:], chunk) 27 chunk = chunk[blockSize:] 28 bytesToWords(block, &n.block) 29 n.cv = chainingValue(n) 30 n.flags &^= flagChunkStart 31 } 32 // pad last block with zeros 33 block = [blockSize]byte{} 34 n.blockLen = uint32(len(chunk)) 35 copy(block[:], chunk) 36 bytesToWords(block, &n.block) 37 n.flags |= flagChunkEnd 38 return n 39 } 40 41 func hashBlock(out *[64]byte, buf []byte) { 42 var block [64]byte 43 var words [16]uint32 44 copy(block[:], buf) 45 bytesToWords(block, &words) 46 compressNodeGeneric(&words, node{ 47 cv: iv, 48 block: words, 49 blockLen: uint32(len(buf)), 50 flags: flagChunkStart | flagChunkEnd | flagRoot, 51 }) 52 wordsToBytes(words, out) 53 } 54 55 func compressBlocks(out *[maxSIMD * blockSize]byte, n node) { 56 var outs [maxSIMD][64]byte 57 compressBlocksGeneric(&outs, n) 58 for i := range outs { 59 copy(out[i*64:], outs[i][:]) 60 } 61 } 62 63 func mergeSubtrees(cvs *[maxSIMD][8]uint32, numCVs uint64, key *[8]uint32, flags uint32) node { 64 return mergeSubtreesGeneric(cvs, numCVs, key, flags) 65 } 66 67 func bytesToWords(bytes [64]byte, words *[16]uint32) { 68 for i := range words { 69 words[i] = binary.LittleEndian.Uint32(bytes[4*i:]) 70 } 71 } 72 73 func wordsToBytes(words [16]uint32, block *[64]byte) { 74 for i, w := range words { 75 binary.LittleEndian.PutUint32(block[4*i:], w) 76 } 77 } 78 79 func bytesToCV(b []byte) [8]uint32 { 80 var cv [8]uint32 81 for i := range cv { 82 cv[i] = binary.LittleEndian.Uint32(b[4*i:]) 83 } 84 return cv 85 } 86 87 func cvToBytes(cv *[8]uint32) *[32]byte { 88 var b [32]byte 89 for i, w := range cv { 90 binary.LittleEndian.PutUint32(b[4*i:], w) 91 } 92 return &b 93 }