aboutsummaryrefslogtreecommitdiff
path: root/tests/benchmark/crypto/benchmark_stream.odin
blob: 38c5a87c63b3c88a819c83af71453abf80914f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package benchmark_core_crypto

import "base:runtime"
import "core:crypto"
import "core:testing"
import "core:text/table"
import "core:time"

import "core:crypto/aes"
import "core:crypto/chacha20"

@(private = "file")
ITERS :: 10000
@(private = "file")
SIZES := []int{64, 1024, 65536}
@(private = "file")
AES_CTR_KEY_SIZES := []int{128, 192, 256}

@(test)
benchmark_crypto_stream :: proc(t: ^testing.T) {
	runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()

	tbl: table.Table
	table.init(&tbl)
	defer table.destroy(&tbl)

	table.caption(&tbl, "Stream Cipher")
	table.aligned_header_of_values(&tbl, .Right, "Algorithm", "Size", "Time", "Throughput")

	for key_sz, i in AES_CTR_KEY_SIZES {
		if i > 0 {
			table.row(&tbl)
		}

		key := make([]byte, key_sz/8, context.temp_allocator)
		iv := make([]byte, aes.CTR_IV_SIZE, context.temp_allocator)
		crypto.rand_bytes(key)
		crypto.rand_bytes(iv)

		ctx: aes.Context_CTR
		aes.init_ctr(&ctx, key, iv)

		for sz, _ in SIZES {
			options := &time.Benchmark_Options{
				rounds = ITERS,
				bytes = sz,
				setup = setup_sized_buf,
				bench = do_bench_aes_ctr,
				teardown = teardown_sized_buf,
			}
			context.user_ptr = &ctx

			err := time.benchmark(options, context.allocator)
			testing.expect(t, err == nil)

			time_per_iter := options.duration / ITERS
			table.aligned_row_of_values(
				&tbl,
				.Right,
				table.format(&tbl, "AES%d-CTR", key_sz),
				table.format(&tbl, "%d", sz),
				table.format(&tbl, "%8M", time_per_iter),
				table.format(&tbl, "%5.3f MiB/s", options.megabytes_per_second),
			)
		}
	}

	table.row(&tbl)

	{
		key := make([]byte, chacha20.KEY_SIZE, context.temp_allocator)
		iv := make([]byte, chacha20.IV_SIZE, context.temp_allocator)
		crypto.rand_bytes(key)
		crypto.rand_bytes(iv)

		ctx: chacha20.Context
		chacha20.init(&ctx, key, iv)

		for sz, _ in SIZES {
			options := &time.Benchmark_Options{
				rounds = ITERS,
				bytes = sz,
				setup = setup_sized_buf,
				bench = do_bench_chacha20,
				teardown = teardown_sized_buf,
			}
			context.user_ptr = &ctx

			err := time.benchmark(options, context.allocator)
			testing.expect(t, err == nil)

			time_per_iter := options.duration / ITERS
			table.aligned_row_of_values(
				&tbl,
				.Right,
				"chacha20",
				table.format(&tbl, "%d", sz),
				table.format(&tbl, "%8M", time_per_iter),
				table.format(&tbl, "%5.3f MiB/s", options.megabytes_per_second),
			)
		}
	}

	log_table(&tbl)
}

@(private = "file")
do_bench_aes_ctr :: proc(
	options: ^time.Benchmark_Options,
	allocator := context.allocator,
) -> (
	err: time.Benchmark_Error,
) {
	ctx := (^aes.Context_CTR)(context.user_ptr)

	buf := options.input

	for _ in 0 ..= options.rounds {
		aes.xor_bytes_ctr(ctx, buf, buf)
	}
	options.count = options.rounds
	options.processed = options.rounds * options.bytes

	return
}

@(private = "file")
do_bench_chacha20 :: proc(
	options: ^time.Benchmark_Options,
	allocator := context.allocator,
) -> (
	err: time.Benchmark_Error,
) {
	ctx := (^chacha20.Context)(context.user_ptr)

	buf := options.input

	for _ in 0 ..= options.rounds {
		chacha20.xor_bytes(ctx, buf, buf)
	}
	options.count = options.rounds
	options.processed = options.rounds * options.bytes

	return
}