8 g "github.com/anacrolix/generics"
11 // The leaf block size for BitTorrent v2 Merkle trees.
12 const BlockSize = 1 << 14 // 16KiB
14 func Root(hashes [][sha256.Size]byte) [sha256.Size]byte {
17 return sha256.Sum256(nil)
21 numHashes := uint(len(hashes))
22 if numHashes != RoundUpToPowerOfTwo(uint(len(hashes))) {
23 panic(fmt.Sprintf("expected power of two number of hashes, got %d", numHashes))
25 var next [][sha256.Size]byte
26 for i := 0; i < len(hashes); i += 2 {
29 h := sha256.Sum256(append(left[:], right[:]...))
30 next = append(next, h)
35 func CompactLayerToSliceHashes(compactLayer string) (hashes [][sha256.Size]byte, err error) {
36 g.MakeSliceWithLength(&hashes, len(compactLayer)/sha256.Size)
37 for i := range hashes {
38 n := copy(hashes[i][:], compactLayer[i*sha256.Size:])
40 err = fmt.Errorf("compact layer has incomplete hash at index %d", i)
47 func RoundUpToPowerOfTwo(n uint) (ret uint) {
48 return 1 << bits.Len(n-1)