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

crypto: Add BLAKE3 hashing algorithm #4366

Merged
merged 5 commits into from
Feb 3, 2020
Merged

Conversation

jayschwa
Copy link
Contributor

@jayschwa jayschwa commented Feb 2, 2020

This is a translation of the official reference implementation with few changes. The bad news is that the reference implementation is designed for simplicity and not speed, so there's a lot of room for performance improvement. The good news is that, according to the crypto benchmark, the implementation is still fast relative to the other hashing algorithms:

         md5: 442 MiB/s
        sha1: 387 MiB/s
      sha256: 192 MiB/s
      sha512: 277 MiB/s
    sha3-256: 217 MiB/s
    sha3-512: 127 MiB/s
     blake2s: 471 MiB/s
     blake2b: 537 MiB/s
      blake3: 588 MiB/s
    poly1305: 1492 MiB/s
    hmac-md5: 655 MiB/s
   hmac-sha1: 554 MiB/s
 hmac-sha256: 222 MiB/s
      x25519: 8753 exchanges/s

This is a translation of the [official reference implementation][1] with
few other changes. The bad news is that the reference implementation is
designed for simplicity and not speed, so there's a lot of room for
performance improvement. The good news is that, according to the crypto
benchmark, the implementation is still fast relative to the other
hashing algorithms:

```
         md5: 430 MiB/s
        sha1: 386 MiB/s
      sha256: 191 MiB/s
      sha512: 275 MiB/s
    sha3-256: 233 MiB/s
    sha3-512: 137 MiB/s
     blake2s: 464 MiB/s
     blake2b: 526 MiB/s
      blake3: 576 MiB/s
    poly1305: 1479 MiB/s
    hmac-md5: 653 MiB/s
   hmac-sha1: 553 MiB/s
 hmac-sha256: 222 MiB/s
      x25519: 8685 exchanges/s
```

[1]: https://github.com/BLAKE3-team/BLAKE3
@daurnimator daurnimator added the standard library This issue involves writing Zig code for the standard library. label Feb 2, 2020
I do not see many cases of constant pointers to arrays in the stdlib.
In fact, this makes the code run a little faster, probably because Zig
automatically converts to pointers where it makes sense.
@jayschwa
Copy link
Contributor Author

jayschwa commented Feb 2, 2020

Something about my test data is causing Zig to segfault in CI and local non-release builds. The test succeeds if I use a release build from my package manager (0.5.0+4f2652d50). The following is enough to cause a segfault; it doesn't even engage the Blake3 code.

test "BLAKE3 reference test cases" {
    for (reference_test.cases) |t| {
        _ = t;
    }
}

@LemonBoy
Copy link
Contributor

LemonBoy commented Feb 2, 2020

I've opened #4373 for that problem, you can side-step it by declaring reference_test initializer type explicitly.

@jayschwa
Copy link
Contributor Author

jayschwa commented Feb 3, 2020

Style question: When passing an array into a slice parameter, is it preferable to use &array or array[0..]?

@andrewrk
Copy link
Member

andrewrk commented Feb 3, 2020

Style question: When passing an array into a slice parameter, is it preferable to use &array or array[0..]?

I've been using &array.

@andrewrk andrewrk merged commit 5504f15 into ziglang:master Feb 3, 2020
@andrewrk
Copy link
Member

andrewrk commented Feb 3, 2020

Nice work!

@jayschwa
Copy link
Contributor Author

jayschwa commented Feb 3, 2020

I think this algorithm is a great candidate for use in #4311, especially with more potential performance wins. In addition to the simple reference implementation, the BLAKE3 repo also has a super-charged (but more complex) SIMD implementation that could be translated.

@jayschwa jayschwa deleted the blake3 branch February 3, 2020 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants