Skip to content

Commit

Permalink
Merge pull request #77 from cryspen/shake-benchmarks
Browse files Browse the repository at this point in the history
Added CIRCL and BoringSSL shake benchmarks.
  • Loading branch information
franziskuskiefer authored Sep 18, 2023
2 parents 388d6d2 + e29106e commit 37474c7
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 10 deletions.
5 changes: 5 additions & 0 deletions benches/boringssl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ FetchContent_MakeAvailable(boringssl)
add_executable(kyber768 kyber768.cxx)
target_compile_options(kyber768 PRIVATE -Wall -Wextra)
target_link_libraries(kyber768 crypto benchmark::benchmark)

add_executable(shake shake.cxx)
target_include_directories(shake PRIVATE ${boringssl_SOURCE_DIR})
target_compile_options(shake PRIVATE -Wall -Wextra)
target_link_libraries(shake crypto benchmark::benchmark)
43 changes: 43 additions & 0 deletions benches/boringssl/shake.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdlib.h>
#include <string>

#include "crypto/kyber/internal.h"

#include <benchmark/benchmark.h>

#define SHAKE128_BYTES_TO_OUTPUT 840

static void BM_SHAKE128(benchmark::State &state) {
uint8_t input[34] = {0};
for (uint8_t i = 0; i < sizeof(input); i++) {
input[i] = i;
}

uint8_t output[SHAKE128_BYTES_TO_OUTPUT];

for (auto _ : state) {
BORINGSSL_keccak(output, SHAKE128_BYTES_TO_OUTPUT, input, sizeof(input),
boringssl_shake128);
}
}

#define SHAKE256_BYTES_TO_OUTPUT 128

static void BM_SHAKE256(benchmark::State &state) {
uint8_t input[33] = {0};
for (uint8_t i = 0; i < sizeof(input); i++) {
input[i] = i;
}

uint8_t output[SHAKE256_BYTES_TO_OUTPUT];

for (auto _ : state) {
BORINGSSL_keccak(output, SHAKE256_BYTES_TO_OUTPUT, input, sizeof(input),
boringssl_shake256);
}
}

BENCHMARK(BM_SHAKE128);
BENCHMARK(BM_SHAKE256);

BENCHMARK_MAIN();
7 changes: 5 additions & 2 deletions benches/circl/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
bench-ref:
env GODEBUG=cpu.avx2=off go test -bench=.
bench-kyber-ref:
env GODEBUG=cpu.avx2=off go test -bench='Kyber768'

bench-shake-ref:
env GODEBUG=cpu.avx2=off go test -bench='SHAKE'
6 changes: 3 additions & 3 deletions benches/circl/kyber768_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

func BenchmarkKeyGeneration(b *testing.B) {
func BenchmarkKyber768KeyGeneration(b *testing.B) {
scheme := schemes.ByName("Kyber768")

for i := 0; i < b.N; i++ {
Expand All @@ -15,7 +15,7 @@ func BenchmarkKeyGeneration(b *testing.B) {
}
}

func BenchmarkEncapsulation(b *testing.B) {
func BenchmarkKyber768Encapsulation(b *testing.B) {
scheme := schemes.ByName("Kyber768")

pk, _, _ := scheme.GenerateKeyPair()
Expand All @@ -27,7 +27,7 @@ func BenchmarkEncapsulation(b *testing.B) {
}
}

func BenchmarkDecapsulation(b *testing.B) {
func BenchmarkKyber768Decapsulation(b *testing.B) {
scheme := schemes.ByName("Kyber768")

pk, sk, _ := scheme.GenerateKeyPair()
Expand Down
46 changes: 46 additions & 0 deletions benches/circl/shake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"github.com/cloudflare/circl/xof"
"testing"
)

func BenchmarkSHAKE128(b *testing.B) {
input := make([]byte, 0, 34)
for i := 0; i < len(input); i++ {
input[i] = byte(i)
}

bytesToOutput := 840
output := make([]byte, bytesToOutput)

xof := xof.SHAKE128.New()
_, err := xof.Write([]byte(input))
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
_, _ = xof.Read(output)
}
}

func BenchmarkSHAKE256(b *testing.B) {
input := make([]byte, 0, 33)
for i := 0; i < len(input); i++ {
input[i] = byte(i)
}

bytesToOutput := 128
output := make([]byte, bytesToOutput)

xof := xof.SHAKE256.New()
_, err := xof.Write([]byte(input))
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
_, _ = xof.Read(output)
}
}
2 changes: 1 addition & 1 deletion sys/hacl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ libc = { version = "0.2", default-features = false }
fs_extra = "1.3"
cc = { version = "1.0", features = ["parallel"] }
libcrux_platform = { path = "../platform" }
bindgen = "0.66"
bindgen = "0.68"

[dev-dependencies]
hex = "0.4.3"
Expand Down
2 changes: 1 addition & 1 deletion sys/hacl/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* automatically generated by rust-bindgen 0.66.1 */
/* automatically generated by rust-bindgen 0.68.1 */

pub const Spec_Hash_Definitions_SHA2_224: u32 = 0;
pub const Spec_Hash_Definitions_SHA2_256: u32 = 1;
Expand Down
2 changes: 1 addition & 1 deletion sys/libjade/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cc = { version = "1.0", features = ["parallel"] }
libcrux_platform = { version = "=0.0.1", path = "../platform" }

[target.'cfg(not(windows))'.build-dependencies]
bindgen = "0.66"
bindgen = "0.68"

[dev-dependencies]
pretty_env_logger = "0.5"
2 changes: 1 addition & 1 deletion sys/pqclean/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"

[build-dependencies]
cc = { version = "1.0", features = ["parallel"] }
bindgen = "0.66"
bindgen = "0.68"
fs_extra = "1.2"
2 changes: 1 addition & 1 deletion sys/pqclean/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* automatically generated by rust-bindgen 0.66.1 */
/* automatically generated by rust-bindgen 0.68.1 */

pub const SHAKE128_RATE: u32 = 168;
pub const SHAKE256_RATE: u32 = 136;
Expand Down

0 comments on commit 37474c7

Please sign in to comment.