compress_noasm.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // +build !amd64
  2. package blake3
  3. import "encoding/binary"
  4. func compressNode(n node) (out [16]uint32) {
  5. compressNodeGeneric(&out, n)
  6. return
  7. }
  8. func compressBuffer(buf *[maxSIMD * chunkSize]byte, buflen int, key *[8]uint32, counter uint64, flags uint32) node {
  9. return compressBufferGeneric(buf, buflen, key, counter, flags)
  10. }
  11. func compressChunk(chunk []byte, key *[8]uint32, counter uint64, flags uint32) node {
  12. n := node{
  13. cv: *key,
  14. counter: counter,
  15. blockLen: blockSize,
  16. flags: flags | flagChunkStart,
  17. }
  18. var block [blockSize]byte
  19. for len(chunk) > blockSize {
  20. copy(block[:], chunk)
  21. chunk = chunk[blockSize:]
  22. bytesToWords(block, &n.block)
  23. n.cv = chainingValue(n)
  24. n.flags &^= flagChunkStart
  25. }
  26. // pad last block with zeros
  27. block = [blockSize]byte{}
  28. n.blockLen = uint32(len(chunk))
  29. copy(block[:], chunk)
  30. bytesToWords(block, &n.block)
  31. n.flags |= flagChunkEnd
  32. return n
  33. }
  34. func hashBlock(out *[64]byte, buf []byte) {
  35. var block [64]byte
  36. var words [16]uint32
  37. copy(block[:], buf)
  38. bytesToWords(block, &words)
  39. compressNodeGeneric(&words, node{
  40. cv: iv,
  41. block: words,
  42. blockLen: uint32(len(buf)),
  43. flags: flagChunkStart | flagChunkEnd | flagRoot,
  44. })
  45. wordsToBytes(words, out)
  46. }
  47. func compressBlocks(out *[maxSIMD * blockSize]byte, n node) {
  48. var outs [maxSIMD][64]byte
  49. compressBlocksGeneric(&outs, n)
  50. for i := range outs {
  51. copy(out[i*64:], outs[i][:])
  52. }
  53. }
  54. func mergeSubtrees(cvs *[maxSIMD][8]uint32, numCVs uint64, key *[8]uint32, flags uint32) node {
  55. return mergeSubtreesGeneric(cvs, numCVs, key, flags)
  56. }
  57. func bytesToWords(bytes [64]byte, words *[16]uint32) {
  58. for i := range words {
  59. words[i] = binary.LittleEndian.Uint32(bytes[4*i:])
  60. }
  61. }
  62. func wordsToBytes(words [16]uint32, block *[64]byte) {
  63. for i, w := range words {
  64. binary.LittleEndian.PutUint32(block[4*i:], w)
  65. }
  66. }