diff --git a/export_test.go b/export_test.go index 59f2edf..7b44c93 100644 --- a/export_test.go +++ b/export_test.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2021 Prysmatic Labs +# Copyright (c) 2021 Prysmatic Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -25,4 +25,4 @@ package gohashtree // Export internal functions for testing. -var Sha256_1_generic = sha256_1_generic +var Sha256_1_generic = sha256_1_generic[[32]byte, [32]byte] diff --git a/hash.go b/hash.go index b9675e4..e9fd3b6 100644 --- a/hash.go +++ b/hash.go @@ -32,7 +32,7 @@ func _hash(digests *byte, p [][32]byte, count uint32) // Hash hashes the chunks two at the time and outputs the digests on the first // argument. It does check for lengths on the inputs. -func Hash(digests [][32]byte, chunks [][32]byte) error { +func Hash[D, C ~[32]byte](digests []D, chunks []C) error { if len(chunks) == 0 { return nil } @@ -44,7 +44,7 @@ func Hash(digests [][32]byte, chunks [][32]byte) error { return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests)) } if supportedCPU { - _hash(&digests[0][0], chunks, uint32(len(chunks)/2)) + _hash(&digests[0][0], *(*[][32]byte)(unsafe.Pointer(&chunks)), uint32(len(chunks)/2)) } else { sha256_1_generic(digests, chunks) } @@ -52,9 +52,9 @@ func Hash(digests [][32]byte, chunks [][32]byte) error { } // HashChunks is the same as Hash, but does not do error checking on the lengths of the slices -func HashChunks(digests [][32]byte, chunks [][32]byte) { +func HashChunks[D, C ~[32]byte](digests []D, chunks []C) { if supportedCPU { - _hash(&digests[0][0], chunks, uint32(len(chunks)/2)) + _hash(&digests[0][0], *(*[][32]byte)(unsafe.Pointer(&chunks[0])), uint32(len(chunks)/2)) } else { sha256_1_generic(digests, chunks) } diff --git a/sha256_1_generic.go b/sha256_1_generic.go index 89ed2d5..66b2fa7 100644 --- a/sha256_1_generic.go +++ b/sha256_1_generic.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2021-2022 Prysmatic Labs +# Copyright (c) 2021-2022 Prysmatic Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -125,7 +125,7 @@ var _K = []uint32{ 0xc67178f2, } -func sha256_1_generic(digests [][32]byte, p [][32]byte) { +func sha256_1_generic[D, P ~[32]byte](digests []D, p []P) { var w [16]uint32 for k := 0; k < len(p)/2; k++ { // First 16 rounds @@ -217,7 +217,7 @@ func sha256_1_generic(digests [][32]byte, p [][32]byte) { h6 += g h7 += h - var dig [32]byte + var dig D binary.BigEndian.PutUint32(dig[0:4], h0) binary.BigEndian.PutUint32(dig[4:8], h1) binary.BigEndian.PutUint32(dig[8:12], h2)