<commit_before>// zig run benchmark.zig --release-fast --override-lib-dir ..

const builtin = @import("builtin");
const std = @import("../std.zig");
const time = std.time;
const Timer = time.Timer;
const crypto = std.crypto;

const KiB = 1024;
const MiB = 1024 * KiB;

var prng = std.rand.DefaultPrng.init(0);

const Crypto = struct {
    ty: type,
    name: []const u8,
};

const hashes = [_]Crypto{
    Crypto{ .ty = crypto.Md5, .name = "md5" },
    Crypto{ .ty = crypto.Sha1, .name = "sha1" },
    Crypto{ .ty = crypto.Sha256, .name = "sha256" },
    Crypto{ .ty = crypto.Sha512, .name = "sha512" },
    Crypto{ .ty = crypto.Sha3_256, .name = "sha3-256" },
    Crypto{ .ty = crypto.Sha3_512, .name = "sha3-512" },
    Crypto{ .ty = crypto.Blake2s256, .name = "blake2s" },
    Crypto{ .ty = crypto.Blake2b512, .name = "blake2b" },
};

pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 {
    var h = Hash.init();

    var block: [Hash.digest_length]u8 = undefined;
    prng.random.bytes(block[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    while (offset < bytes) : (offset += block.len) {
        h.update(block[0..]);
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, bytes / elapsed_s);

    return throughput;
}

const macs = [_]Crypto{
    Crypto{ .ty = crypto.Poly1305, .name = "poly1305" },
    Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" },
    Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" },
    Crypto{ .ty = crypto.HmacSha256, .name = "hmac-sha256" },
};

pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 {
    std.debug.assert(32 >= Mac.mac_length and 32 >= Mac.minimum_key_length);

    var in: [1 * MiB]u8 = undefined;
    prng.random.bytes(in[0..]);

    var key: [32]u8 = undefined;
    prng.random.bytes(key[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    while (offset < bytes) : (offset += in.len) {
        Mac.create(key[0..], in[0..], key);
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, bytes / elapsed_s);

    return throughput;
}

const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};

pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 {
    std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length);

    var in: [DhKeyExchange.minimum_key_length]u8 = undefined;
    prng.random.bytes(in[0..]);

    var out: [DhKeyExchange.minimum_key_length]u8 = undefined;
    prng.random.bytes(out[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    {
        var i: usize = 0;
        while (i < exchange_count) : (i += 1) {
            _ = DhKeyExchange.create(out[0..], out, in);
        }
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, exchange_count / elapsed_s);

    return throughput;
}

fn usage() void {
    std.debug.warn(
        \\throughput_test [options]
        \\
        \\Options:
        \\  --filter [test-name]
        \\  --seed   [int]
        \\  --help
        \\
    );<commit_msg>Crypto benchmark fixes<commit_after>// zig run benchmark.zig --release-fast --override-lib-dir ..

const builtin = @import("builtin");
const std = @import("std");
const time = std.time;
const Timer = time.Timer;
const crypto = std.crypto;

const KiB = 1024;
const MiB = 1024 * KiB;

var prng = std.rand.DefaultPrng.init(0);

const Crypto = struct {
    ty: type,
    name: []const u8,
};

const hashes = [_]Crypto{
    Crypto{ .ty = crypto.Md5, .name = "md5" },
    Crypto{ .ty = crypto.Sha1, .name = "sha1" },
    Crypto{ .ty = crypto.Sha256, .name = "sha256" },
    Crypto{ .ty = crypto.Sha512, .name = "sha512" },
    Crypto{ .ty = crypto.Sha3_256, .name = "sha3-256" },
    Crypto{ .ty = crypto.Sha3_512, .name = "sha3-512" },
    Crypto{ .ty = crypto.Blake2s256, .name = "blake2s" },
    Crypto{ .ty = crypto.Blake2b512, .name = "blake2b" },
};

pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 {
    var h = Hash.init();

    var block: [Hash.digest_length]u8 = undefined;
    prng.random.bytes(block[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    while (offset < bytes) : (offset += block.len) {
        h.update(block[0..]);
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, bytes / elapsed_s);

    return throughput;
}

const macs = [_]Crypto{
    Crypto{ .ty = crypto.Poly1305, .name = "poly1305" },
    Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" },
    Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" },
    Crypto{ .ty = crypto.HmacSha256, .name = "hmac-sha256" },
};

pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 {
    std.debug.assert(32 >= Mac.mac_length and 32 >= Mac.minimum_key_length);

    var in: [1 * MiB]u8 = undefined;
    prng.random.bytes(in[0..]);

    var key: [32]u8 = undefined;
    prng.random.bytes(key[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    while (offset < bytes) : (offset += in.len) {
        Mac.create(key[0..], in[0..], key[0..]);
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, bytes / elapsed_s);

    return throughput;
}

const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};

pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 {
    std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length);

    var in: [DhKeyExchange.minimum_key_length]u8 = undefined;
    prng.random.bytes(in[0..]);

    var out: [DhKeyExchange.minimum_key_length]u8 = undefined;
    prng.random.bytes(out[0..]);

    var offset: usize = 0;
    var timer = try Timer.start();
    const start = timer.lap();
    {
        var i: usize = 0;
        while (i < exchange_count) : (i += 1) {
            _ = DhKeyExchange.create(out[0..], out[0..], in[0..]);
        }
    }
    const end = timer.read();

    const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
    const throughput = @floatToInt(u64, exchange_count / elapsed_s);

    return throughput;
}

fn usage() void {
    std.debug.warn(
        \\throughput_test [options]
        \\
        \\Options:
        \\  --filter [test-name]
        \\  --seed   [int]
        \\  --help
        \\
    );
{'fraction_non_alphanumeric': 0.16418126538294484, 'fraction_numerical': 0.03633994498335023, 'mean_word_length': 2.6764236295902077, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5189250', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2547, 'n_words': 590}