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

Replace all uses of SHA-256 with BLAKE2b. #37439

Merged
merged 3 commits into from
Oct 31, 2016

Conversation

michaelwoerister
Copy link
Member

Removes the SHA-256 implementation and replaces all uses of it with BLAKE2b, which we already use for debuginfo type guids and incremental compilation hashes. It doesn't make much sense to have two different cryptographic hash implementations in the compiler and Blake has a few advantages over SHA-2 (computationally less expensive, hashes of up to 512 bits).

@rust-highfive
Copy link
Collaborator

r? @Aatch

(rust_highfive has picked a reviewer for you, use r? to override)

@michaelwoerister
Copy link
Member Author

r? @alexcrichton

@rust-highfive rust-highfive assigned alexcrichton and unassigned Aatch Oct 28, 2016
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me! r=me with just a few nits

debug_assert!(mem::size_of_val(&self.0.h) >= self.0.outlen as usize);
let raw_ptr = (&self.0.h[..]).as_ptr() as * const u8;
unsafe {
slice::from_raw_parts(raw_ptr, self.0.outlen as usize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm, we don't have to worry about endianness here, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct, blake2b_finish makes sure that Blake2bCtx::h contains the bytes in little endian order, which is what blake2b defines.

let mut hasher = Sha256::new();
use std::hash::Hasher;

let mut hasher = ArchIndependentHasher::new(Blake2bHasher::new(256 / 8, &[]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment why 256 is used here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no real reason other than that we had a 256 bit hash here. 128 bits would probably be more than enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I thought it doesn't how with this hash was as long as it would avoid collisions. We are only storing it once per crate, so I thought there is no harm in making it as wide as possible. But since it has been introduced a few things have been added and now we are feeding this hash into many other hashes, often multiple times (symbol hashes, type id hashes, incr. comp. hashes). So, making it smaller should actually have a positive impact on runtime.

I figure, 128 bits should be enough to make collisions unlikely enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me! r=me with a rebase

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shared this patch with one of the blake2 designers and he was concerned about the use of 128 bits. See this titter thread: https://twitter.com/zooko/status/794972598853627904?s=09

@@ -18,6 +18,7 @@ rustc = { path = "../librustc" }
rustc_back = { path = "../librustc_back" }
rustc_borrowck = { path = "../librustc_borrowck" }
rustc_const_eval = { path = "../librustc_const_eval" }
rustc_data_structures = { path = "../rustc_data_structures" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should start with "librustc" (travis is complaining)

@bors
Copy link
Contributor

bors commented Oct 30, 2016

☔ The latest upstream changes (presumably #37400) made this pull request unmergeable. Please resolve the merge conflicts.

@michaelwoerister
Copy link
Member Author

@bors r=alexcrichton

@bors
Copy link
Contributor

bors commented Oct 31, 2016

📌 Commit 9ef9194 has been approved by alexcrichton

@bors
Copy link
Contributor

bors commented Oct 31, 2016

⌛ Testing commit 9ef9194 with merge 4497196...

bors added a commit that referenced this pull request Oct 31, 2016
Replace all uses of SHA-256 with BLAKE2b.

Removes the SHA-256 implementation and replaces all uses of it with BLAKE2b, which we already use for debuginfo type guids and incremental compilation hashes. It doesn't make much sense to have two different cryptographic hash implementations in the compiler and Blake has a few advantages over SHA-2 (computationally less expensive, hashes of up to 512 bits).
@bors bors merged commit 9ef9194 into rust-lang:master Oct 31, 2016
@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Oct 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants