Skip to content

Commit 539a1e7

Browse files
stevvooerchincha
authored andcommitted
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]>
1 parent 1e56c6d commit 539a1e7

File tree

10 files changed

+42
-145
lines changed

10 files changed

+42
-145
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
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+
// BLAKE3 is deprecated. Use the symbol "Blake3" instead.
27+
BLAKE3 = Blake3
28+
)
29+
2430
func init() {
25-
digest.RegisterAlgorithm(digest.BLAKE3, &blake3hash{})
31+
RegisterAlgorithm(Blake3, &blake3hash{})
2632
}
2733

2834
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.

digest_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func TestParseDigest(t *testing.T) {
3838
Algorithm: "sha384",
3939
Encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
4040
},
41+
{
42+
Input: "blake3:af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262",
43+
Algorithm: "blake3",
4144
{
4245
// empty
4346
Input: "",

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module github.com/opencontainers/go-digest
22

33
go 1.18
4+
5+
require github.com/zeebo/blake3 v0.2.0
File renamed without changes.

testdigest/testdigest.go

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

verifiers_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ func TestDigestVerifier(t *testing.T) {
3737
}
3838
}
3939

40+
func TestDigestBlakeVector(t *testing.T) {
41+
// From the BLAKE3 test vectors.
42+
testvector := Blake3.FromBytes([]byte{0, 1, 2, 3, 4})
43+
expected := "blake3:b40b44dfd97e7a84a996a91af8b85188c66c126940ba7aad2e7ae6b385402aa2"
44+
if string(testvector) != expected {
45+
t.Fatalf("Expected: %s; Got: %s", expected, testvector)
46+
}
47+
}
48+
4049
// TestVerifierUnsupportedDigest ensures that unsupported digest validation is
4150
// flowing through verifier creation.
4251
func TestVerifierUnsupportedDigest(t *testing.T) {

0 commit comments

Comments
 (0)