From 364bd9ec43a8d01f32079c9005518253da4954df Mon Sep 17 00:00:00 2001 From: itsdevbear Date: Thu, 20 Jun 2024 12:33:32 -0400 Subject: [PATCH 1/2] bet --- errors.go | 14 ++++++++++++++ hash.go | 13 ++++++++----- hash_test.go | 3 ++- 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 errors.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..cecd9ac --- /dev/null +++ b/errors.go @@ -0,0 +1,14 @@ +package gohashtree + +import "errors" + +var ( + // ErrOddChunks is returned when the number of chunks is odd. + ErrOddChunks = errors.New("odd number of chunks") + // ErrNotEnoughDigests is returned when the number of digests is not enough. + ErrNotEnoughDigests = errors.New("not enough digest length") + // ErrNotMultipleOf64 is returned when the chunks are not multiple of 64 bytes. + ErrNotMultipleOf64 = errors.New("chunks not multiple of 64 bytes") + // ErrNotMultipleOf32 is returned when the digests are not multiple of 32 bytes. + ErrNotMultipleOf32 = errors.New("digests not multiple of 32 bytes") +) diff --git a/hash.go b/hash.go index b9675e4..cb91597 100644 --- a/hash.go +++ b/hash.go @@ -38,10 +38,10 @@ func Hash(digests [][32]byte, chunks [][32]byte) error { } if len(chunks)%2 == 1 { - return fmt.Errorf("odd number of chunks") + return ErrOddChunks } if len(digests) < len(chunks)/2 { - return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests)) + return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests)) } if supportedCPU { _hash(&digests[0][0], chunks, uint32(len(chunks)/2)) @@ -64,14 +64,17 @@ func HashByteSlice(digests []byte, chunks []byte) error { if len(chunks) == 0 { return nil } + if len(chunks)%64 != 0 { - return fmt.Errorf("chunks not multiple of 64 bytes") + return ErrNotMultipleOf64 } + if len(digests)%32 != 0 { - return fmt.Errorf("digests not multiple of 32 bytes") + return ErrNotMultipleOf32 } + if len(digests) < len(chunks)/2 { - return fmt.Errorf("not enough digest length, need at least %d, got %d", len(chunks)/2, len(digests)) + return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests)) } // 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. diff --git a/hash_test.go b/hash_test.go index 53dbb69..6f4beac 100644 --- a/hash_test.go +++ b/hash_test.go @@ -24,6 +24,7 @@ SOFTWARE. package gohashtree_test import ( + "errors" "reflect" "testing" @@ -283,7 +284,7 @@ func TestNotAllocatedDigest(t *testing.T) { chunks := make([][32]byte, 4) err := gohashtree.Hash(digests, chunks) expected := "not enough digest length, need at least 2, got 1" - if err.Error() != expected { + if !errors.Is(err, gohashtree.ErrNotEnoughDigests) { t.Logf("expected error: \"%s\", got: \"%s\"", expected, err) t.Fail() } From 676b6c76facf59f5483372b4128c6fc89bed741f Mon Sep 17 00:00:00 2001 From: itsdevbear Date: Thu, 20 Jun 2024 12:37:00 -0400 Subject: [PATCH 2/2] bet --- errors.go | 8 ++++---- hash.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/errors.go b/errors.go index cecd9ac..7a8a56a 100644 --- a/errors.go +++ b/errors.go @@ -7,8 +7,8 @@ var ( ErrOddChunks = errors.New("odd number of chunks") // ErrNotEnoughDigests is returned when the number of digests is not enough. ErrNotEnoughDigests = errors.New("not enough digest length") - // ErrNotMultipleOf64 is returned when the chunks are not multiple of 64 bytes. - ErrNotMultipleOf64 = errors.New("chunks not multiple of 64 bytes") - // ErrNotMultipleOf32 is returned when the digests are not multiple of 32 bytes. - ErrNotMultipleOf32 = errors.New("digests not multiple of 32 bytes") + // ErrChunksNotMultipleOf64 is returned when the chunks are not multiple of 64 bytes. + ErrChunksNotMultipleOf64 = errors.New("chunks not multiple of 64 bytes") + // ErrDigestsNotMultipleOf32 is returned when the digests are not multiple of 32 bytes. + ErrDigestsNotMultipleOf32 = errors.New("digests not multiple of 32 bytes") ) diff --git a/hash.go b/hash.go index cb91597..e009335 100644 --- a/hash.go +++ b/hash.go @@ -66,11 +66,11 @@ func HashByteSlice(digests []byte, chunks []byte) error { } if len(chunks)%64 != 0 { - return ErrNotMultipleOf64 + return ErrChunksNotMultipleOf64 } if len(digests)%32 != 0 { - return ErrNotMultipleOf32 + return ErrDigestsNotMultipleOf32 } if len(digests) < len(chunks)/2 {