From becad2190d5d7fd4e6d5776f0ffdc9ec30eba212 Mon Sep 17 00:00:00 2001 From: xvzcf Date: Wed, 13 Sep 2023 17:17:27 -0400 Subject: [PATCH 1/6] Added CIRCL SHAKE benchmarks. --- benches/circl/Makefile | 7 ++++-- benches/circl/kyber768_test.go | 6 ++--- benches/circl/shake_test.go | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 benches/circl/shake_test.go diff --git a/benches/circl/Makefile b/benches/circl/Makefile index 48dddf9b6..9b0ad56e5 100644 --- a/benches/circl/Makefile +++ b/benches/circl/Makefile @@ -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' diff --git a/benches/circl/kyber768_test.go b/benches/circl/kyber768_test.go index 79ebada74..3b6c3c35b 100644 --- a/benches/circl/kyber768_test.go +++ b/benches/circl/kyber768_test.go @@ -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++ { @@ -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() @@ -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() diff --git a/benches/circl/shake_test.go b/benches/circl/shake_test.go new file mode 100644 index 000000000..1129ffc17 --- /dev/null +++ b/benches/circl/shake_test.go @@ -0,0 +1,44 @@ +package main + +import ( + "github.com/cloudflare/circl/xof" + "testing" +) + +const bytesToOutput = 10000; + +func BenchmarkSHAKE128(b *testing.B) { + input := make([]byte, 0, 34); + 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++ { + n, err := xof.Read(output) + if n != bytesToOutput || err != nil { + b.Fatal() + } + } +} + +func BenchmarkSHAKE256(b *testing.B) { + input := make([]byte, 0, 34); + 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++ { + n, err := xof.Read(output) + if n != bytesToOutput || err != nil { + b.Fatal() + } + } +} From 60532416ce377c703fe61907d6bc1220c557c819 Mon Sep 17 00:00:00 2001 From: xvzcf Date: Wed, 13 Sep 2023 17:20:24 -0400 Subject: [PATCH 2/6] Added BoringSSL SHAKE benchmarks. --- benches/boringssl/CMakeLists.txt | 5 +++++ benches/boringssl/shake.cxx | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 benches/boringssl/shake.cxx diff --git a/benches/boringssl/CMakeLists.txt b/benches/boringssl/CMakeLists.txt index 5e51a98c3..69f98dbef 100644 --- a/benches/boringssl/CMakeLists.txt +++ b/benches/boringssl/CMakeLists.txt @@ -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) diff --git a/benches/boringssl/shake.cxx b/benches/boringssl/shake.cxx new file mode 100644 index 000000000..dd33d55a9 --- /dev/null +++ b/benches/boringssl/shake.cxx @@ -0,0 +1,33 @@ +#include +#include + +#include "crypto/kyber/internal.h" + +#include + +#define BYTES_TO_OUTPUT 10000 + +static void BM_SHAKE128(benchmark::State &state) { + uint8_t input[34] = {0}; + uint8_t output[BYTES_TO_OUTPUT]; + + for (auto _ : state) { + BORINGSSL_keccak(output, BYTES_TO_OUTPUT, input, sizeof(input), + boringssl_shake128); + } +} + +static void BM_SHAKE256(benchmark::State &state) { + uint8_t input[34] = {0}; + uint8_t output[BYTES_TO_OUTPUT]; + + for (auto _ : state) { + BORINGSSL_keccak(output, BYTES_TO_OUTPUT, input, sizeof(input), + boringssl_shake256); + } +} + +BENCHMARK(BM_SHAKE128); +BENCHMARK(BM_SHAKE256); + +BENCHMARK_MAIN(); From 60b3fd1e81b3ded7f7ff39f3ec0d382080dae3ae Mon Sep 17 00:00:00 2001 From: xvzcf Date: Wed, 13 Sep 2023 17:22:57 -0400 Subject: [PATCH 3/6] Ran go fmt. --- benches/circl/shake_test.go | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/benches/circl/shake_test.go b/benches/circl/shake_test.go index 1129ffc17..5b16d5b67 100644 --- a/benches/circl/shake_test.go +++ b/benches/circl/shake_test.go @@ -5,40 +5,40 @@ import ( "testing" ) -const bytesToOutput = 10000; +const bytesToOutput = 10000 func BenchmarkSHAKE128(b *testing.B) { - input := make([]byte, 0, 34); - output := make([]byte, bytesToOutput); + input := make([]byte, 0, 34) + output := make([]byte, bytesToOutput) - xof := xof.SHAKE128.New() - _, err := xof.Write([]byte(input)) - if err != nil { - b.Fatal(err) - } + xof := xof.SHAKE128.New() + _, err := xof.Write([]byte(input)) + if err != nil { + b.Fatal(err) + } for i := 0; i < b.N; i++ { - n, err := xof.Read(output) - if n != bytesToOutput || err != nil { - b.Fatal() - } + n, err := xof.Read(output) + if n != bytesToOutput || err != nil { + b.Fatal() + } } } func BenchmarkSHAKE256(b *testing.B) { - input := make([]byte, 0, 34); - output := make([]byte, bytesToOutput) + input := make([]byte, 0, 34) + output := make([]byte, bytesToOutput) - xof := xof.SHAKE256.New() - _, err := xof.Write([]byte(input)) - if err != nil { - b.Fatal(err) - } + xof := xof.SHAKE256.New() + _, err := xof.Write([]byte(input)) + if err != nil { + b.Fatal(err) + } for i := 0; i < b.N; i++ { - n, err := xof.Read(output) - if n != bytesToOutput || err != nil { - b.Fatal() - } + n, err := xof.Read(output) + if n != bytesToOutput || err != nil { + b.Fatal() + } } } From f5e95b63bf57385643d1e7e20703ee9409a558d3 Mon Sep 17 00:00:00 2001 From: xvzcf Date: Wed, 13 Sep 2023 17:25:21 -0400 Subject: [PATCH 4/6] Remove success check in CIRCL SHAKE benchmark. --- benches/circl/shake_test.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/benches/circl/shake_test.go b/benches/circl/shake_test.go index 5b16d5b67..b95a8a310 100644 --- a/benches/circl/shake_test.go +++ b/benches/circl/shake_test.go @@ -18,10 +18,7 @@ func BenchmarkSHAKE128(b *testing.B) { } for i := 0; i < b.N; i++ { - n, err := xof.Read(output) - if n != bytesToOutput || err != nil { - b.Fatal() - } + _, _ = xof.Read(output) } } @@ -36,9 +33,6 @@ func BenchmarkSHAKE256(b *testing.B) { } for i := 0; i < b.N; i++ { - n, err := xof.Read(output) - if n != bytesToOutput || err != nil { - b.Fatal() - } + _, _ = xof.Read(output) } } From 1aabe707815450a8ca328b8b764df3ece8f6d5dd Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Thu, 14 Sep 2023 11:17:29 +0200 Subject: [PATCH 5/6] bump bindgen version --- sys/hacl/Cargo.toml | 2 +- sys/hacl/src/bindings.rs | 2 +- sys/libjade/Cargo.toml | 2 +- sys/pqclean/Cargo.toml | 2 +- sys/pqclean/src/bindings.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sys/hacl/Cargo.toml b/sys/hacl/Cargo.toml index 221d73c6a..e94808bd7 100644 --- a/sys/hacl/Cargo.toml +++ b/sys/hacl/Cargo.toml @@ -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" diff --git a/sys/hacl/src/bindings.rs b/sys/hacl/src/bindings.rs index 676945045..2b919deea 100644 --- a/sys/hacl/src/bindings.rs +++ b/sys/hacl/src/bindings.rs @@ -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; diff --git a/sys/libjade/Cargo.toml b/sys/libjade/Cargo.toml index 3fe4ee634..9a4423e32 100644 --- a/sys/libjade/Cargo.toml +++ b/sys/libjade/Cargo.toml @@ -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" diff --git a/sys/pqclean/Cargo.toml b/sys/pqclean/Cargo.toml index c10ec849a..ca6fc08f5 100644 --- a/sys/pqclean/Cargo.toml +++ b/sys/pqclean/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" [build-dependencies] cc = { version = "1.0", features = ["parallel"] } -bindgen = "0.66" +bindgen = "0.68" fs_extra = "1.2" diff --git a/sys/pqclean/src/bindings.rs b/sys/pqclean/src/bindings.rs index 636875539..67a4e7b1b 100644 --- a/sys/pqclean/src/bindings.rs +++ b/sys/pqclean/src/bindings.rs @@ -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; From e29106ed318f1f0753eff392113d24e603dc7fe6 Mon Sep 17 00:00:00 2001 From: xvzcf Date: Thu, 14 Sep 2023 09:59:51 -0400 Subject: [PATCH 6/6] Update BoringSSL and CIRCL SHAKE benchmarks. --- benches/boringssl/shake.cxx | 22 ++++++++++++++++------ benches/circl/shake_test.go | 14 +++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/benches/boringssl/shake.cxx b/benches/boringssl/shake.cxx index dd33d55a9..4021431dc 100644 --- a/benches/boringssl/shake.cxx +++ b/benches/boringssl/shake.cxx @@ -5,24 +5,34 @@ #include -#define BYTES_TO_OUTPUT 10000 +#define SHAKE128_BYTES_TO_OUTPUT 840 static void BM_SHAKE128(benchmark::State &state) { uint8_t input[34] = {0}; - uint8_t output[BYTES_TO_OUTPUT]; + 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, BYTES_TO_OUTPUT, input, sizeof(input), + 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[34] = {0}; - uint8_t output[BYTES_TO_OUTPUT]; + 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, BYTES_TO_OUTPUT, input, sizeof(input), + BORINGSSL_keccak(output, SHAKE256_BYTES_TO_OUTPUT, input, sizeof(input), boringssl_shake256); } } diff --git a/benches/circl/shake_test.go b/benches/circl/shake_test.go index b95a8a310..a988a48df 100644 --- a/benches/circl/shake_test.go +++ b/benches/circl/shake_test.go @@ -5,10 +5,13 @@ import ( "testing" ) -const bytesToOutput = 10000 - 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() @@ -23,7 +26,12 @@ func BenchmarkSHAKE128(b *testing.B) { } func BenchmarkSHAKE256(b *testing.B) { - input := make([]byte, 0, 34) + 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()