Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bench): Add libblas to compilation #1

Open
wants to merge 14 commits into
base: experimental/xwing-checksums
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.ci
*.tar.gz
libjade-*
.vscode
.vscode
*.so
*.csv
12 changes: 11 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@
url = https://github.com/tyhicks/ssbd-tools.git
[submodule "submodules/crypto-specs"]
path = submodules/crypto-specs
url = git@github.com:formosa-crypto/crypto-specs.git
url = https://github.com/formosa-crypto/crypto-specs.git
[submodule "test/external/crypto_kem/xwing/common/JoaoDiogoDuarte_xwing"]
path = test/external/crypto_kem/xwing/common/JoaoDiogoDuarte_xwing
url = https://github.com/JoaoDiogoDuarte/xwing.git
[submodule "test/external/crypto_hash/sha3-256/common/Kyber_FIPS202"]
path = test/external/crypto_hash/sha3-256/common/Kyber_FIPS202
url = https://github.com/JoaoDiogoDuarte/kyber-mlkem-static/
branch = justhash
[submodule "test/external/crypto_kem/xwing_naive/common/JoaoDDuarte_xwing_naive"]
path = test/external/crypto_kem/xwing_naive/common/JoaoDDuarte_xwing_naive
url = https://github.com/JoaoDiogoDuarte/xwing.git
[submodule "test/external/crypto_kem/xwing_naive/common/JoaoDiogoDuarte_xwing_naive"]
path = test/external/crypto_kem/xwing_naive/common/JoaoDiogoDuarte_xwing_naive
url = https://github.com/JoaoDiogoDuarte/xwing.git
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Libjade

Libjade is a formally verified cryptographic library written in
[the jasmin programming language](https://github.com/jasmin-lang/jasmin)
with computer-verified proofs in [EasyCrypt](https://github.com/EasyCrypt/easycrypt).
Expand Down
2 changes: 1 addition & 1 deletion bench/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ MAIN = $(COMMON)/crypto_$(OPERATION).c
DEFINE ?=
DNAMESPACES = -DJADE_NAMESPACE=$(NAMESPACE1) -DJADE_NAMESPACE_LC=$(NAMESPACE)
INCLUDES = -I$(IDIR)/include/ -I$(COMMON)/ -I$(RANDINC)
COMPILE = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(ASM) $(RANDLIB) -lgsl $(CIL)
COMPILE = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(ASM) $(RANDLIB) -lblas -lgsl $(CIL)

# --------------------------------------------------------------------
.PHONY: __phony benchs
Expand Down
33 changes: 31 additions & 2 deletions bench/Makefile.partial_implementations
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
EXTERNAL_PATH = $(subst $(BIN),external,$(@D))
EXTERNAL_LIB_STATIC = ../test/$(EXTERNAL_PATH)/$(subst -,_,$(subst /,_,$(EXTERNAL_PATH))).a
EXTERNAL_LIB_SHARED = $(abspath ../test/$(EXTERNAL_PATH)/$(subst -,_,$(subst /,_,$(EXTERNAL_PATH))).so)
EXTERNAL_COMPILE1 = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(PRINT) $(ASM) $(RANDLIB) $(EXTERNAL_LIB) -lgsl $(CIL)
EXTERNAL_COMPILE2 = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(PRINT) $(ASM) $(RANDLIB) -lgsl $(EXTERNAL_LIB_SHARED)
EXTERNAL_COMPILE1 = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(PRINT) $(ASM) $(RANDLIB) $(EXTERNAL_LIB) -lblas -lgsl $(CIL)
EXTERNAL_COMPILE2 = $(CC) $(CFLAGS) -o $@ $(DEFINE) $(DNAMESPACES) $(INCLUDES) $(MAIN) $(PRINT) $(ASM) $(RANDLIB) -lblas -lgsl $(EXTERNAL_LIB_SHARED)


#-- falcon512
Expand Down Expand Up @@ -44,8 +44,37 @@ $(XWING_C_AVX2_BINARIES): __phony | $(XWING_C_AVX2_DIR) $(XWING_C_AVX2_DIR)/$(CI
$(MAKE) -C ../test/$(EXTERNAL_PATH)
$(EXTERNAL_COMPILE2) -l25519 $(CIL) || true

#-- ---------------------------------------------------------------------------
#-- xwing_naive

#--
XWING_NAIVE_C_AVX2_DIR:=bin/crypto_kem/xwing_naive/amd64/c-avx2/
XWING_NAIVE_C_AVX2_BINARIES:=$(foreach bin,$(BENCH_BINARIES),$(XWING_NAIVE_C_AVX2_DIR)$(bin))

$(XWING_NAIVE_C_AVX2_BINARIES): __phony | $(XWING_NAIVE_C_AVX2_DIR) $(XWING_NAIVE_C_AVX2_DIR)/$(CID)
$(MAKE) -C $(IDIR) || true
$(CIC)
$(MAKE) -C ../test/$(EXTERNAL_PATH)
$(EXTERNAL_COMPILE2) -l25519 $(CIL) || true

#-- ---------------------------------------------------------------------------
#-- sha3fips

SHA3_256_C_AVX2_DIR:=bin/crypto_hash/sha3-256/amd64/c-avx2/
SHA3_256_C_AVX2_BINARIES:=$(foreach bin,$(BENCH_BINARIES),$(SHA3_256_C_AVX2_DIR)$(bin))

$(SHA3_256_C_AVX2_BINARIES): __phony | $(SHA3_256_C_AVX2_DIR) $(SHA3_256_C_AVX2_DIR)/$(CID)
$(MAKE) -C $(IDIR) || true
$(CIC)
$(MAKE) -C ../test/$(EXTERNAL_PATH)
$(EXTERNAL_COMPILE2) $(CIL) || true



#-- clean
clean-external:
$(MAKE) -C ../test/external/crypto_sign/falcon/falcon512/amd64/avx2 clean
$(MAKE) -C ../test/external/crypto_kem/xwing/common/JoaoDiogoDuarte_xwing/src/crypto_kem/xwing/ref/ clean
$(MAKE) -C ../test/external/crypto_kem/xwing/common/JoaoDiogoDuarte_xwing/src/crypto_kem/xwing/avx2/ clean
$(MAKE) -C ../test/external/crypto_kem/xwing/common/JoaoDiogoDuarte_xwing_naive/src/crypto_kem/xwing_naive/avx2/ clean
$(MAKE) -C ../test/external/crypto_sign/sha3-256/common/Kyber_FIPS202/avx2/ clean
9 changes: 7 additions & 2 deletions bench/common/increment.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ static size_t inc_2(size_t len)
return len << 1;
}

static size_t size_inc_2(size_t start, size_t end)
static size_t inc_1088(size_t len)
{
return len + 1088;
}

static size_t size_inc_1088(size_t start, size_t end)
{
size_t i, r=0;
for(i = start; i <= end; i = inc_2(i))
for(i = start; i <= end; i = inc_1088(i))
{ r += 1; }
return r;
}
Expand Down
1 change: 1 addition & 0 deletions run_hash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cd bench/ && make DEFINE='-DRUNS=100 -DLOOPS=100 -DST_ON -DMININBYTES=134 -DMAXINBYTES=1222 -DINC_INBYTES=1088' FILTER=../src/crypto_hash/sha3-256/% && ./bin/crypto_hash/sha3-256/amd64/c-avx2/bench
2 changes: 2 additions & 0 deletions run_xwing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
cd bench/ && make DEFINE='-DRUNS=100 -DST_ON' FILTER=../src/crypto_kem/xwing/% && ./bin/crypto_kem/xwing/amd64/c-avx2/bench
1 change: 1 addition & 0 deletions run_xwing_naive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cd bench/ && make DEFINE='-DRUNS=100 -DST_ON' FILTER=../src/crypto_kem/xwing_naive/% && ./bin/crypto_kem/xwing_naive/amd64/c-avx2/bench
97 changes: 97 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
librandombytes = stdenv.mkDerivation rec {
pname = "librandombytes";
version = "20230919";

src = fetchzip {
url = "https://randombytes.cr.yp.to/librandombytes-${version}.tar.gz";
hash = "sha256-wr44x45AwEU1v4kvbmG37npUJGmRprnUtAzQvJJuPyw=";
};

nativeBuildInputs = [ python3 ];

buildInputs = [ openssl ];

configurePhase = ''
patchShebangs configure
patchShebangs scripts-build
./configure --prefix=${placeholder "out"}
'';
};

libcpucycles = stdenv.mkDerivation rec {
pname = "libcpucycles";
version = "20240114";

src = fetchzip {
url = "https://cpucycles.cr.yp.to/libcpucycles-${version}.tar.gz";
hash = "sha256-EWUmQjsoHZfSC6xPxKaNW0M5X1AIHerWC9HbC84CLtY=";
};

nativeBuildInputs = [ python3 ];

configurePhase = ''
patchShebangs configure
patchShebangs scripts-build
./configure --prefix=${placeholder "out"}
'';
};

lib25519 = stdenv.mkDerivation rec {
pname = "lib25519";
version = "20230630";

src = fetchzip {
url = "https://lib25519.cr.yp.to/lib25519-${version}.tar.gz";
hash = "sha256-mZ6U3ZbcCW+frnf8/TB8x/5LgxW/96lmSB+oX4tvCpQ=";
};

nativeBuildInputs = [ python3 ];

buildInputs = [ librandombytes libcpucycles ];

configurePhase = ''
patchShebangs configure
patchShebangs scripts-build
./configure --prefix=${placeholder "out"}
'';
};

jasmin-src = pkgs.fetchFromGitHub {
owner = "Rixxc";
repo = "jasmin";
rev = "c523a9e20bae85e028c6cb9b0b55bfc3f8f3734b";
hash = "sha256-6cDmfj55mYwTQE0Fka7s3/EuBts9hj+65xgWQ7OTez8=";
};

jasmin = pkgs.callPackage "${jasmin-src}/default.nix" { inherit pkgs; };

gls = stdenv.mkDerivation rec {
pname = "libgls";
version = "2.7";

src = fetchzip {
url = "https://mirror.ibcp.fr/pub/gnu/gsl/gsl-${version}.tar.gz";
hash = "sha256-/5yCVBUHRlqnG3bmkI80S41t0UNRiVda5X8/itNBLSw=";
};

buildInputs = [
blas
];
};
in
pkgs.mkShell {
nativeBuildInputs = [
jasmin
];

buildInputs = [
blas
gls
libsodium
lib25519
];
}

7 changes: 7 additions & 0 deletions src/crypto_hash/sha3-256/amd64/c-avx2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
hash.s: hash.c
$(CC) -Wall -Werror -S hash.c

clean:
rm -f hash.s

distclean: clean
9 changes: 9 additions & 0 deletions src/crypto_hash/sha3-256/amd64/c-avx2/hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "include/api.h"
#include <stddef.h>

int sha3_256(uint8_t *h, const uint8_t *in, size_t inlen);

int jade_hash_sha3_256_amd64_c_avx2(uint8_t *hash, const uint8_t *input,
uint64_t input_length) {
return sha3_256(hash, input, input_length);
}
2 changes: 2 additions & 0 deletions src/crypto_hash/sha3-256/amd64/c-avx2/hash.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

// This file has no code on purpose: its presence here simplifies the setup for testing and benchmarking for non-Jasmin implementations (such as this one)
18 changes: 18 additions & 0 deletions src/crypto_hash/sha3-256/amd64/c-avx2/include/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef JADE_HASH_sha3_256_amd64_c_avx2_API_H
#define JADE_HASH_sha3_256_amd64_c_avx2_API_H

#define JADE_HASH_sha3_256_amd64_c_avx2_BYTES 32

#define JADE_HASH_sha3_256_amd64_c_avx2_ALGNAME "SHA3-256"
#define JADE_HASH_sha3_256_amd64_c_avx2_ARCH "amd64"
#define JADE_HASH_sha3_256_amd64_c_avx2_IMPL "avx2"

#include <stdint.h>

int jade_hash_sha3_256_amd64_c_avx2(
uint8_t *hash,
const uint8_t *input,
uint64_t input_length
);

#endif
26 changes: 26 additions & 0 deletions src/crypto_kem/xwing_naive/META.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: X-Wing-Naive
type: kem
checksumsmall:
checksumbig:
claimed-nist-level: 3
claimed-security: IND-CCA2
length-public-key: 1216
length-ciphertext: 1120
length-secret-key: 2464
length-shared-secret: 32
principal-submitters:
- TODO
auxiliary-submitters:
- TODO
implementations:
- name: amd64/ref
version: TODO
supported_platforms:
- architecture: x86_64
operating_systems:
- Linux
- Darwin
required_flags: # FIXME
- avx2
- bmi2
- popcnt
8 changes: 8 additions & 0 deletions src/crypto_kem/xwing_naive/amd64/c-avx2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

kem.s: kem.c
$(CC) -Wall -Werror -S kem.c

clean:
rm -f kem.s

distclean: clean
2 changes: 2 additions & 0 deletions src/crypto_kem/xwing_naive/amd64/c-avx2/dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib25519 (-> libcpucycles; librandombytes (-> libssl-dev) )
libsodium
47 changes: 47 additions & 0 deletions src/crypto_kem/xwing_naive/amd64/c-avx2/include/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef JADE_KEM_xwing_naive_amd64_c_avx2_API_H
#define JADE_KEM_xwing_naive_amd64_c_avx2_API_H

#include <stdint.h>

#define JADE_KEM_xwing_naive_amd64_c_avx2_PUBLICKEYBYTES 1216
#define JADE_KEM_xwing_naive_amd64_c_avx2_SECRETKEYBYTES 2464
#define JADE_KEM_xwing_naive_amd64_c_avx2_CIPHERTEXTBYTES 1120
#define JADE_KEM_xwing_naive_amd64_c_avx2_KEYPAIRCOINBYTES 96
#define JADE_KEM_xwing_naive_amd64_c_avx2_ENCCOINBYTES 64
#define JADE_KEM_xwing_naive_amd64_c_avx2_BYTES 32

#define JADE_KEM_xwing_naive_amd64_c_avx2_ALGNAME "X-Wing-Naive"
#define JADE_KEM_xwing_naive_amd64_c_avx2_ARCH "amd64"
#define JADE_KEM_xwing_naive_amd64_c_avx2_IMPL "c-avx2"

int jade_kem_xwing_naive_amd64_c_avx2_keypair_derand(
uint8_t *public_key,
uint8_t *secret_key,
const uint8_t *coins
);

int jade_kem_xwing_naive_amd64_c_avx2_keypair(
uint8_t *public_key,
uint8_t *secret_key
);

int jade_kem_xwing_naive_amd64_c_avx2_enc_derand(
uint8_t *ciphertext,
uint8_t *shared_secret,
const uint8_t *public_key,
const uint8_t *coins
);

int jade_kem_xwing_naive_amd64_c_avx2_enc(
uint8_t *ciphertext,
uint8_t *shared_secret,
const uint8_t *public_key
);

int jade_kem_xwing_naive_amd64_c_avx2_dec(
uint8_t *shared_secret,
const uint8_t *ciphertext,
const uint8_t *secret_key
);

#endif
Loading