Skip to content

Commit

Permalink
Use AVX2 for SHA calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuktu committed May 12, 2016
1 parent 6480853 commit 7d4d666
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 64 deletions.
19 changes: 17 additions & 2 deletions Makefile.bsd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ ifneq ($(SSE), no)
SSE=yes
endif
endif
AVX2_FLAG = $(shell /usr/bin/grep -o AVX2 /var/run/dmesg.boot | /usr/bin/head -1)
ifneq ($(AVX2), no)
ifeq ($(AVX2_FLAG), AVX2)
AVX2=yes
endif
endif
ifeq ($(AVX2), yes)
SSE=yes
endif
ifeq ($(SSE), no)
AVX2=no
endif
ifeq ($(SSE), yes)
CFLAGS+=-mssse3
endif
ifeq ($(AVX2), yes)
CFLAGS+=-mavx2
endif

# use -march=native if we're compiling for x86
BENCH_ARCH_OPTION=
Expand Down Expand Up @@ -148,10 +163,10 @@ hybrid: static-lib
$(SRCDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c -fPIC $< -o $@

$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
$(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
$(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@

Expand Down
19 changes: 17 additions & 2 deletions Makefile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@ ifneq ($(SSE), no)
SSE=yes
endif
endif
AVX2_FLAG = $(shell /bin/grep -m 1 -o avx2 /proc/cpuinfo)
ifneq ($(AVX2), no)
ifeq ($(AVX2_FLAG), avx2)
AVX2=yes
endif
endif
ifeq ($(AVX2), yes)
SSE=yes
endif
ifeq ($(SSE), no)
AVX2=no
endif
ifeq ($(SSE), yes)
CFLAGS+=-mssse3
endif
ifeq ($(AVX2), yes)
CFLAGS+=-mavx2
endif

# use -march=native if we're compiling for x86
BENCH_ARCH_OPTION=
Expand Down Expand Up @@ -146,10 +161,10 @@ hybrid: static-lib
$(SRCDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c -fPIC $< -o $@

$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
$(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
$(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@

Expand Down
8 changes: 6 additions & 2 deletions Makefile.os2
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ OPTFLAGS=-O2
bench: OPTFLAGS=-O3 -march=native
CFLAGS?=-g $(OPTFLAGS)
CFLAGS+=-Wall -Wextra -Wno-unused-parameter
ifeq ($(AVX2), yes)
CFLAGS+=-mavx2
SSE=yes
endif
ifeq ($(SSE), yes)
CFLAGS+=-mssse3
endif
Expand Down Expand Up @@ -106,10 +110,10 @@ hybrid: lib
$(SRCDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
$(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
$(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@

Expand Down
19 changes: 17 additions & 2 deletions Makefile.osx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ ifneq ($(SSE), no)
SSE=yes
endif
endif
AVX2_FLAG = $(shell /usr/sbin/sysctl machdep.cpu.features | grep -m 1 -ow AVX2)
ifneq ($(AVX2), no)
ifeq ($(AVX2_FLAG), AVX2)
AVX2=yes
endif
endif
ifeq ($(AVX2), yes)
SSE=yes
endif
ifeq ($(SSE), no)
AVX2=no
endif
ifeq ($(SSE), yes)
CFLAGS+=-mssse3
endif
ifeq ($(SSE), no)
CFLAGS+=-march=x86-64
endif
ifeq ($(AVX2), yes)
CFLAGS+=-mavx2
endif
SRCDIR=src
TESTDIR=tests
LIB_OBJS=bitstring.o encparams.o hash.o idxgen.o key.o mgf.o ntru.o poly.o rand.o arith.o sha1.o sha2.o nist_ctr_drbg.o rijndael.o rijndael-alg-fst.o
Expand Down Expand Up @@ -104,10 +119,10 @@ hybrid: lib
$(SRCDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c -fPIC $< -o $@

$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
$(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
$(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@

Expand Down
8 changes: 6 additions & 2 deletions Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ OPTFLAGS=-O2
bench: OPTFLAGS=-O3 -march=native
CFLAGS?=-g $(OPTFLAGS)
CFLAGS+=-Wall -Wextra -Wno-unused-parameter
ifeq ($(AVX2), yes)
CFLAGS+=-mavx2
SSE=yes
endif
ifeq ($(SSE), yes)
CFLAGS+=-mssse3
endif
Expand Down Expand Up @@ -108,10 +112,10 @@ hybrid: lib
$(SRCDIR)/%.o: $(SRCDIR)/%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.s: $(SRCDIR)/sha1-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha1-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha1-mb-x86_64.o: $(SRCDIR)/sha1-mb-x86_64.s
$(AS) $(SRCDIR)/sha1-mb-x86_64.s -o $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.s: $(SRCDIR)/sha256-mb-x86_64.pl; CC=$(CC) ASM="$(AS)" $(PERL) $(SRCDIR)/sha256-mb-x86_64.pl $(PERLASM_SCHEME) > $@
$(SRCDIR)/sha256-mb-x86_64.o: $(SRCDIR)/sha256-mb-x86_64.s
$(AS) $(SRCDIR)/sha256-mb-x86_64.s -o $@

Expand Down
18 changes: 18 additions & 0 deletions src/encparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const NtruEncParams EES401EP1 = {\
{0, 2, 4}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
114 /* pklen */\
};
Expand All @@ -41,6 +42,7 @@ const NtruEncParams EES449EP1 = {\
{0, 3, 3}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
128 /* pklen */\
};
Expand All @@ -63,6 +65,7 @@ const NtruEncParams EES677EP1 = {\
{0, 5, 3}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
192 /* pklen */\
};
Expand All @@ -85,6 +88,7 @@ const NtruEncParams EES1087EP2 = {\
{0, 6, 3}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
256 /* pklen */\
};
Expand All @@ -107,6 +111,7 @@ const NtruEncParams EES541EP1 = {\
{0, 2, 5}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
112 /* pklen */\
};
Expand All @@ -129,6 +134,7 @@ const NtruEncParams EES613EP1 = {\
{0, 3, 4}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
128 /* pklen */\
};
Expand All @@ -151,6 +157,7 @@ const NtruEncParams EES887EP1 = {\
{0, 5, 4}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
192 /* pklen */\
};
Expand All @@ -173,6 +180,7 @@ const NtruEncParams EES1171EP1 = {\
{0, 6, 4}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
256 /* pklen */\
};
Expand All @@ -195,6 +203,7 @@ const NtruEncParams EES659EP1 = {\
{0, 2, 6}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
112 /* pklen */\
};
Expand All @@ -217,6 +226,7 @@ const NtruEncParams EES761EP1 = {\
{0, 3, 5}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
128 /* pklen */\
};
Expand All @@ -239,6 +249,7 @@ const NtruEncParams EES1087EP1 = {\
{0, 5, 5}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
192 /* pklen */\
};
Expand All @@ -261,6 +272,7 @@ const NtruEncParams EES1499EP1 = {\
{0, 6, 5}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
256 /* pklen */\
};
Expand All @@ -285,6 +297,7 @@ const NtruEncParams EES401EP2 = {\
{0, 2, 16}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
112 /* pklen */\
};
Expand All @@ -307,6 +320,7 @@ const NtruEncParams EES439EP1 = {\
{0, 3, 16}, /* oid */\
ntru_sha1, /* hash */\
ntru_sha1_4way, /* hash_4way */\
ntru_sha1_8way, /* hash_8way */\
20, /* hlen */\
128 /* pklen */\
};
Expand All @@ -329,6 +343,7 @@ const NtruEncParams EES443EP1 = {\
{0, 3, 17}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
128 /* pklen */\
};
Expand All @@ -351,6 +366,7 @@ const NtruEncParams EES593EP1 = {\
{0, 5, 16}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
192 /* pklen */\
};
Expand All @@ -373,6 +389,7 @@ const NtruEncParams EES587EP1 = {\
{0, 5, 17}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
192 /* pklen */\
};
Expand All @@ -395,6 +412,7 @@ const NtruEncParams EES743EP1 = {\
{0, 6, 16}, /* oid */\
ntru_sha256, /* hash */\
ntru_sha256_4way, /* hash_4way */\
ntru_sha256_8way, /* hash_8way */\
32, /* hlen */\
256 /* pklen */\
};
Expand Down
3 changes: 3 additions & 0 deletions src/encparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ typedef struct NtruEncParams {
/* hash function for 4 inputs, e.g. ntru_sha256_4way */
void (*hash_4way)(uint8_t *[4], uint16_t, uint8_t *[4]);

/* hash function for 8 inputs, e.g. ntru_sha256_8way */
void (*hash_8way)(uint8_t *[8], uint16_t, uint8_t *[8]);

/* output length of the hash function */
uint16_t hlen;

Expand Down
Loading

0 comments on commit 7d4d666

Please sign in to comment.