Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BLAKE3 hashing algorithm via Rust interop #12416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ indent_style = space
indent_size = 2

# Match C++/C/shell/Perl, set indent to spaces with width of four
[*.{hpp,cc,hh,c,h,sh,pl,xs}]
[*.{hpp,cc,hh,c,h,sh,pl,xs,rs}]
indent_style = space
indent_size = 4

Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Default meson build dir
/build

# Rust
/src/rust/.advisory-db
/src/rust/target

# /tests/functional/
/tests/functional/common/subst-vars.sh
/tests/functional/restricted-innocent
Expand Down
37 changes: 36 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
flake = false;
};

inputs.rust-overlay = {
url = "github:oxalica/rust-overlay";
};

# dev tooling
inputs.flake-parts.url = "github:hercules-ci/flake-parts";
inputs.git-hooks-nix.url = "github:cachix/git-hooks.nix";
Expand All @@ -27,6 +31,7 @@
self,
nixpkgs,
nixpkgs-regression,
rust-overlay,
...
}:

Expand Down Expand Up @@ -118,6 +123,7 @@
useLLVM = true;
};
overlays = [
rust-overlay.overlays.default
(overlayFor (pkgs: pkgs.${stdenv}))
];
}
Expand Down Expand Up @@ -195,7 +201,11 @@
{
# A Nixpkgs overlay that overrides the 'nix' and
# 'nix-perl-bindings' packages.
overlays.default = overlayFor (p: p.stdenv);
overlays.default = nixpkgs.lib.composeManyExtensions [
rust-overlay.overlays.default
overlayFor
(p: p.stdenv)
];

hydraJobs = import ./packaging/hydra.nix {
inherit
Expand Down Expand Up @@ -289,6 +299,7 @@
# Components we'll iterate over in the upcoming lambda
"nix-util" = { };
"nix-util-c" = { };
"nix-util-rust" = { };
"nix-util-test-support" = { };
"nix-util-tests" = { };

Expand Down
1 change: 1 addition & 0 deletions packaging/components.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in

nix-util = callPackage ../src/libutil/package.nix { };
nix-util-c = callPackage ../src/libutil-c/package.nix { };
nix-util-rust = callPackage ../src/rust/crates/nix-util-rust/package.nix { };
nix-util-test-support = callPackage ../src/libutil-test-support/package.nix { };
nix-util-tests = callPackage ../src/libutil-tests/package.nix { };

Expand Down
2 changes: 2 additions & 0 deletions packaging/dev-shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pkgs.nixComponents.nix-util.overrideAttrs (
nativeBuildInputs =
attrs.nativeBuildInputs or [ ]
++ pkgs.nixComponents.nix-util.nativeBuildInputs
++ pkgs.nixComponents.nix-util-rust.nativeBuildInputs
++ pkgs.nixComponents.nix-store.nativeBuildInputs
++ pkgs.nixComponents.nix-fetchers.nativeBuildInputs
++ pkgs.nixComponents.nix-expr.nativeBuildInputs
Expand Down Expand Up @@ -129,6 +130,7 @@ pkgs.nixComponents.nix-util.overrideAttrs (
buildInputs =
attrs.buildInputs or [ ]
++ pkgs.nixComponents.nix-util.buildInputs
++ pkgs.nixComponents.nix-util-rust.buildInputs
++ pkgs.nixComponents.nix-store.buildInputs
++ pkgs.nixComponents.nix-store-tests.externalBuildInputs
++ pkgs.nixComponents.nix-fetchers.buildInputs
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr-tests/error_traces.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ namespace nix {

ASSERT_TRACE1("hashString \"foo\" \"content\"",
UsageError,
HintFmt("unknown hash algorithm '%s', expect 'md5', 'sha1', 'sha256', or 'sha512'", "foo"));
HintFmt("unknown hash algorithm '%s', expect 'blake3', 'md5', 'sha1', 'sha256', or 'sha512'", "foo"));

ASSERT_TRACE2("hashString \"sha256\" {}",
TypeError,
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/local-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct LocalBinaryCacheStore : virtual LocalBinaryCacheStoreConfig, virtual Bina
void getFile(const std::string & path, Sink & sink) override
{
try {
readFile(binaryCacheDir + "/" + path, sink);
sink.readFile(binaryCacheDir + "/" + path);
} catch (SysError & e) {
if (e.errNo == ENOENT)
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path);
Expand Down
28 changes: 28 additions & 0 deletions src/libutil-tests/hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ namespace nix {
* hashString
* --------------------------------------------------------------------------*/

TEST(hashString, testKnownBLAKE3Hashes1Base16) {
auto s = "abc";
auto hash = hashString(HashAlgorithm::BLAKE3, s);
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
"blake3:6437b3ac38465133ffb63b75273a8db548c558465d79db03fd359c6cd5bd9d85");
}

TEST(hashString, testKnownBLAKE3Hashes1SRI) {
auto s = "abc";
auto hash = hashString(HashAlgorithm::BLAKE3, s);
ASSERT_EQ(hash.to_string(HashFormat::SRI, true),
"blake3-ZDezrDhGUTP/tjt1JzqNtUjFWEZdedsD/TWcbNW9nYU=");
}

TEST(hashString, testKnownBLAKE3Hashes2Base16) {
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashAlgorithm::BLAKE3, s);
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
"blake3:c19012cc2aaf0dc3d8e5c45a1b79114d2df42abb2a410bf54be09e891af06ff8");
}

TEST(hashString, testKnownBLAKE3Hashes2SRI) {
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashAlgorithm::BLAKE3, s);
ASSERT_EQ(hash.to_string(HashFormat::SRI, true),
"blake3-wZASzCqvDcPY5cRaG3kRTS30KrsqQQv1S+CeiRrwb/g=");
}

TEST(hashString, testKnownMD5Hashes1) {
// values taken from: https://tools.ietf.org/html/rfc1321
auto s1 = "";
Expand Down
14 changes: 0 additions & 14 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,6 @@ std::string readFile(const std::filesystem::path & path)
}


void readFile(const Path & path, Sink & sink)
{
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_RDONLY
// TODO
#ifndef _WIN32
| O_CLOEXEC
#endif
));
if (!fd)
throw SysError("opening file '%s'", path);
drainFD(fd.get(), sink);
}


void writeFile(const Path & path, std::string_view s, mode_t mode, bool sync)
{
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_WRONLY | O_TRUNC | O_CREAT
Expand Down
1 change: 0 additions & 1 deletion src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ Descriptor openDirectory(const std::filesystem::path & path);
*/
std::string readFile(const Path & path);
std::string readFile(const std::filesystem::path & path);
void readFile(const Path & path, Sink & sink);

/**
* Write a string to a file.
Expand Down
Loading
Loading