Skip to content

Commit

Permalink
Divide by 32 on HashByteSlice
Browse files Browse the repository at this point in the history
On HashByteSlice we multiply by 32 instead of dividing the length of the
slices.

Use also unsafe as reflect is deprecated
  • Loading branch information
potuz committed Jan 29, 2024
1 parent aafd8b3 commit f469183
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package gohashtree

import (
"fmt"
"reflect"
"unsafe"
)

Expand Down Expand Up @@ -69,18 +68,14 @@ func HashByteSlice(digests []byte, chunks []byte) error {
}
// We use an unsafe pointer to cast []byte to [][32]byte. The length and
// capacity of the slice need to be divided accordingly by 32.
header := *(*reflect.SliceHeader)(unsafe.Pointer(&chunks))
header.Len <<= 5
header.Cap <<= 5
chunkedChunks := *(*[][32]byte)(unsafe.Pointer(&header))
sizeChunks := (len(chunks) >> 5)
chunkedChunks := unsafe.Slice((*[32]byte)(unsafe.Pointer(&chunks[0])), sizeChunks)

Check failure on line 72 in hash.go

View workflow job for this annotation

GitHub Actions / Go 1.14 test

undefined: unsafe.Slice

sizeDigests := (len(digests) >> 5)
chunkedDigest := unsafe.Slice((*[32]byte)(unsafe.Pointer(&digests[0])), sizeDigests)

Check failure on line 75 in hash.go

View workflow job for this annotation

GitHub Actions / Go 1.14 test

undefined: unsafe.Slice
if supportedCPU {
_hash(&digests[0], chunkedChunks, uint32(len(chunks)/64))
Hash(chunkedDigest, chunkedChunks)
} else {
headerDigest := *(*reflect.SliceHeader)(unsafe.Pointer(&digests))
headerDigest.Len <<= 5
headerDigest.Cap <<= 5
chunkedDigest := *(*[][32]byte)(unsafe.Pointer(&headerDigest))
sha256_1_generic(chunkedDigest, chunkedChunks)
}
return nil
Expand Down

0 comments on commit f469183

Please sign in to comment.