13 Commits

Author SHA1 Message Date
Frank Denis
4417206230 Now that they support vectors, use math.rot{l,r} 2020-11-05 17:19:48 -05:00
Frank Denis
72064eba23 std/crypto: vectorize BLAKE3
Gives a ~40% speedup on x86_64.

However, the generic code remains faster on aarch64.

This is still processing only one block at a time for now.

I'm pretty confident that processing more blocks per round
will eventually give a substantial performance improvement on
all platforms with vector units.
2020-10-25 21:13:14 -04:00
Frank Denis
fa17447090 std/crypto: make the whole APIs more consistent
- use `PascalCase` for all types. So, AES256GCM is now Aes256Gcm.
- consistently use `_length` instead of mixing `_size` and `_length` for the
constants we expose
- Use `minimum_key_length` when it represents an actual minimum length.
Otherwise, use `key_length`.
- Require output buffers (for ciphertexts, macs, hashes) to be of the right
size, not at least of that size in some functions, and the exact size elsewhere.
- Use a `_bits` suffix instead of `_length` when a size is represented as a
number of bits to avoid confusion.
- Functions returning a constant-sized slice are now defined as a slice instead
of a pointer + a runtime assertion. This is the case for most hash functions.
- Use `camelCase` for all functions instead of `snake_case`.

No functional changes, but these are breaking API changes.
2020-10-17 18:53:08 -04:00
Frank Denis
fc55cd458a Hash functions now accept an option set
- This avoids having multiple `init()` functions for every combination
of optional parameters
- The API is consistent across all hash functions
- New options can be added later without breaking existing applications.
  For example, this is going to come in handy if we implement parallelization
  for BLAKE2 and BLAKE3.
- We don't have a mix of snake_case and camelCase functions any more, at
least in the public crypto API

Support for BLAKE2 salt and personalization (more commonly called context)
parameters have been implemented by the way to illustrate this.
2020-08-21 00:51:14 +02:00
Frank Denis
446597bd3c Remove the reset() function from hash functions
Justification:
- reset() is unnecessary; states that have to be reused can be copied
- reset() is error-prone. Copying a previous state prevents forgetting
  struct members.
- reset() forces implementation to store sensitive data (key, initial state)
  in memory even when they are not needed.
- reset() is confusing as it has a different meaning elsewhere in Zig.
2020-08-20 23:02:10 +02:00
Frank Denis
f92a5d7944 Repair crypto/benchmark; add BLAKE2b256
Some MACs have a 64-bit output
2020-08-20 23:02:10 +02:00
Andrew Kelley
4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00
Vexu
85fd484f07
std: fix blake3 assignment to constant 2020-05-04 14:45:36 +03:00
Jay Petacat
cb2c14e03f blake3: Workaround issue #4373 with named types 2020-02-02 18:44:50 -05:00
Jay Petacat
923e567c6d blake3: Replace &arr with arr[0..] for slice args 2020-02-02 14:59:36 -05:00
Jay Petacat
b143fc0d32 blake3: Name and const pointer refinements 2020-02-02 14:42:57 -05:00
Jay Petacat
d098e212ad blake3: Convert *const [n]u8 types to [n]u8
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.
2020-02-02 14:08:10 -05:00
Jay Petacat
4b86c1e3bb crypto: Add BLAKE3 hashing algorithm
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
2020-02-01 23:03:23 -05:00