Skip to content

Commit 612e714

Browse files
rchinchastevvooe
andauthored
Blake3 (#1)
* digest: promote blake3 to first-class digest The dual module approach for blake3 was slightly awkward. Since it provides similar usability with a massive bump in performance, it's extremely likely to land as a registered algorithm in the image-spec. This PR removes the secondary module, which made it difficult to test as a unit. This may break users who are using HEAD versions of the package. For a new release, this will be backwards compatible. The other drawback is that the zeebo/blake3 will now be a dependency but this can be replaced transparently by the standard libary in the future. In addition to promoting blake3, this makes a few style adjustments to be in line with Go's style guidelines. Signed-off-by: Stephen Day <[email protected]> * fix: update stevvooe's blake3 PR opencontainers#66 opencontainers/image-spec#1240 Signed-off-by: Ramkumar Chinchani <[email protected]> * fix: add a length test for blake3 Signed-off-by: Ramkumar Chinchani <[email protected]> * fix: add a Makefile make make build make test Signed-off-by: Ramkumar Chinchani <[email protected]> * fix: merge conflicts Signed-off-by: Ramkumar Chinchani <[email protected]> * fix: blake3 pulls in golang 1.22.x dep 2025-04-25T19:33:20.2495501Z go: downloading github.com/zeebo/blake3 v0.2.4 2025-04-25T19:33:20.3154128Z go: downloading github.com/klauspost/cpuid/v2 v2.2.10 2025-04-25T19:33:20.3959610Z github.com/klauspost/cpuid/v2: cannot compile Go 1.22 code 2025-04-25T19:33:21.1059807Z FAIL github.com/opencontainers/go-digest [build failed] 2025-04-25T19:33:21.1060449Z FAIL github.com/opencontainers/go-digest/digestset [build failed] 2025-04-25T19:33:21.1219422Z ##[error]Process completed with exit code 1. Signed-off-by: Ramkumar Chinchani <[email protected]> --------- Signed-off-by: Stephen Day <[email protected]> Signed-off-by: Ramkumar Chinchani <[email protected]> Co-authored-by: Stephen Day <[email protected]>
1 parent 1e56c6d commit 612e714

File tree

11 files changed

+72
-91
lines changed

11 files changed

+72
-91
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x, 1.22.x]
7+
go-version: [1.22.x, 1.23.x, 1.24.x]
88
platform: [ubuntu-latest, windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
@@ -16,6 +16,3 @@ jobs:
1616
uses: actions/checkout@v4
1717
- name: Test
1818
run: go test -v ./...
19-
- name: Test Blake3
20-
run: go test -v ./...
21-
working-directory: blake3

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY: all build test
2+
3+
all: build test
4+
5+
build:
6+
go build -v ./...
7+
8+
test:
9+
go test -v -cover -race ./...

algorithm.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,16 @@ const (
103103

104104
var algorithmRegexp = regexp.MustCompile(`^[a-z0-9]+([+._-][a-z0-9]+)*$`)
105105

106-
// CryptoHash is the interface that any hash algorithm must implement
106+
// CryptoHash is the interface that any digest algorithm must implement
107107
type CryptoHash interface {
108-
// Available reports whether the given hash function is usable in the current binary.
108+
// Available reports whether the given hash function is usable in the
109+
// current binary.
109110
Available() bool
110-
// Size returns the length, in bytes, of a digest resulting from the given hash function.
111+
// Size returns the length, in bytes, of a digest resulting from the given
112+
// hash function.
111113
Size() int
112-
// New returns a new hash.Hash calculating the given hash function. If the hash function is not
113-
// available, it may panic.
114+
// New returns a new hash.Hash calculating the given hash function. If the
115+
// hash function is not available, it may panic.
114116
New() hash.Hash
115117
}
116118

@@ -129,14 +131,16 @@ var (
129131
algorithmsLock sync.RWMutex
130132
)
131133

132-
// RegisterAlgorithm may be called to dynamically register an algorithm. The implementation is a CryptoHash, and
133-
// the regex is meant to match the hash portion of the algorithm. If a duplicate algorithm is already registered,
134-
// the return value is false, otherwise if registration was successful the return value is true.
134+
// RegisterAlgorithm may be called to dynamically register an algorithm. The
135+
// implementation is a CryptoHash, and the regex is meant to match the hash
136+
// portion of the algorithm. If a duplicate algorithm is already registered, the
137+
// return value is false, otherwise if registration was successful the return
138+
// value is true.
135139
//
136140
// The algorithm encoding format must be based on hex.
137141
//
138-
// The algorithm name must be conformant to the BNF specification in the OCI image-spec, otherwise the function
139-
// will panic.
142+
// The algorithm name must be conformant to the BNF specification in the OCI
143+
// image-spec, otherwise the function will panic.
140144
func RegisterAlgorithm(algorithm Algorithm, implementation CryptoHash) bool {
141145
algorithmsLock.Lock()
142146
defer algorithmsLock.Unlock()
@@ -150,8 +154,10 @@ func RegisterAlgorithm(algorithm Algorithm, implementation CryptoHash) bool {
150154
}
151155

152156
algorithms[algorithm] = implementation
153-
// We can do this since the Digest function below only implements a hex digest. If we open this in the future
154-
// we need to allow for alternative digest algorithms to be implemented and for the user to pass their own
157+
158+
// We can do this since the Digest function below only implements a hex
159+
// digest. If we open this in the future we need to allow for alternative
160+
// digest algorithms to be implemented and for the user to pass their own
155161
// custom regexp.
156162
anchoredEncodedRegexps[algorithm] = hexDigestRegex(implementation)
157163
return true

blake3/blake3.go renamed to blake3.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14-
15-
package blake3
14+
package digest
1615

1716
import (
1817
"hash"
1918

20-
"github.com/opencontainers/go-digest"
2119
"github.com/zeebo/blake3"
2220
)
2321

22+
const (
23+
// Blake3 is the blake3 algorithm with the default 256-bit output size
24+
Blake3 Algorithm = "blake3"
25+
)
26+
2427
func init() {
25-
digest.RegisterAlgorithm(digest.BLAKE3, &blake3hash{})
28+
RegisterAlgorithm(Blake3, &blake3hash{})
2629
}
2730

2831
type blake3hash struct{}

blake3/blake3_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

blake3/go.mod

Lines changed: 0 additions & 15 deletions
This file was deleted.

blake3/go.sum

Lines changed: 0 additions & 16 deletions
This file was deleted.

digest_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ func TestParseDigest(t *testing.T) {
3838
Algorithm: "sha384",
3939
Encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
4040
},
41+
{
42+
Input: "blake3:af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
43+
Algorithm: "blake3",
44+
Encoded: "af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
45+
},
4146
{
4247
// empty
4348
Input: "",
@@ -83,6 +88,11 @@ func TestParseDigest(t *testing.T) {
8388
Input: "sha512:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
8489
Err: digest.ErrDigestInvalidLength,
8590
},
91+
{
92+
// too short (from different algorithm)
93+
Input: "blake3:abcdef0123456789abcdef0123456789abcdef01234",
94+
Err: digest.ErrDigestInvalidLength,
95+
},
8696
{
8797
Input: "foo:d41d8cd98f00b204e9800998ecf8427e",
8898
Err: digest.ErrDigestUnsupported,

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module github.com/opencontainers/go-digest
22

3-
go 1.18
3+
go 1.22
4+
5+
require github.com/zeebo/blake3 v0.2.4
6+
7+
require (
8+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
9+
golang.org/x/sys v0.32.0 // indirect
10+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
2+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
3+
github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY=
4+
github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
5+
github.com/zeebo/blake3 v0.2.4 h1:KYQPkhpRtcqh0ssGYcKLG1JYvddkEA8QwCM/yBqhaZI=
6+
github.com/zeebo/blake3 v0.2.4/go.mod h1:7eeQ6d2iXWRGF6npfaxl2CU+xy2Fjo2gxeyZGCRUjcE=
7+
github.com/zeebo/pcg v1.0.1 h1:lyqfGeWiv4ahac6ttHs+I5hwtH/+1mrhlCtVNQM2kHo=
8+
github.com/zeebo/pcg v1.0.1/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4=
9+
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
10+
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=

0 commit comments

Comments
 (0)