Skip to content

Commit 21ba115

Browse files
committed
Adding guards to compile w/o AX512.
1 parent c5b081c commit 21ba115

File tree

13 files changed

+298
-268
lines changed

13 files changed

+298
-268
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ set(CMAKE_C_STANDARD 99)
88
set(EXECUTABLE_OUTPUT_PATH ../bin)
99
set(LIBRARY_OUTPUT_PATH ../lib)
1010

11-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wextra -O3 -pedantic -std=c99 -m64 -mavx2 -mbmi2 -mavx512f -mavx512bw -march=native -mtune=native")
1211
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
12+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Wall -Wextra -O3 -pedantic -std=c99 -m64 -mavx2 -mbmi2 -march=native -mtune=native")
13+
14+
option(ENABLE_AVX512 "Enables AVX512 code" OFF)
15+
if(ENABLE_AVX512)
16+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx512f -mavx512bw")
17+
add_compile_definitions(ENABLED_AVX512)
18+
endif(ENABLE_AVX512)
1319

1420
add_subdirectory(third_party)
1521
add_subdirectory(src)

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# FLD ECC AVX2
22

3-
This is a mathematical software library for computing the Edwards Digital Signature Algorithm (EdDSA) and the Diffie-Hellman functions X25519 and X448. The library is optimized with the Intel Advanced Vector eXtensions version 2 (AVX2) and is derived from a research project.
3+
This is a mathematical software library for computing the Edwards Digital Signature Algorithm (EdDSA) and the Diffie-Hellman functions X25519 and X448. The library is optimized with the Intel Advanced Vector eXtensions AVX2 and AVX512 product of this research project.
44

55
```bibtex
66
@article{fld_ecc_avx2,
@@ -31,6 +31,14 @@ To compile use:
3131
$ make all
3232
```
3333

34+
#### Enabling AVX512
35+
36+
Use AVX512 extensions to perform two X25519 operations simultaneously.
37+
```bash
38+
$ cmake .. -DENABLE_AVX512=ON
39+
$ make all
40+
```
41+
3442
### Benchmarks
3543

3644
For running the benchmark program use:

bench/bench_ecdh.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static void bench_ecdh(const X_ECDH* ecdh)
2020
ecdh->freeKey(shared_secret);
2121
}
2222

23+
#if defined(ENABLED_AVX512)
2324
static void bench_x25519_avx512()
2425
{
2526
struct X25519_KEY_x2 ss, sk, pk;
@@ -37,6 +38,7 @@ static void bench_x25519_avx512()
3738
X25519_AVX512.randKey(pk.k1);
3839
, shared, X25519_AVX512.shared(&ss, &sk, &pk));
3940
}
41+
#endif /* defined(ENABLED_AVX512) */
4042

4143
void bench_x25519(void)
4244
{
@@ -45,8 +47,10 @@ void bench_x25519(void)
4547
bench_ecdh(&X25519_x64);
4648
printf("====== X25519 AVX2 ======\n");
4749
bench_ecdh(&X25519_AVX2);
50+
#if defined(ENABLED_AVX512)
4851
printf("====== X25519 AVX512 ====\n");
4952
bench_x25519_avx512();
53+
#endif /* defined(ENABLED_AVX512) */
5054
}
5155

5256
void bench_x448(void)

include/faz_ecdh_avx2.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ typedef ALIGN uint8_t X25519_KEY[ECDH25519_KEY_SIZE_BYTES];
9797
#define ECDH448_KEY_SIZE_BYTES 56
9898
typedef ALIGN uint8_t X448_KEY[ECDH448_KEY_SIZE_BYTES];
9999

100-
ALIGN struct X25519_KEY_x2 {
100+
#if defined(ENABLED_AVX512)
101+
struct ALIGN X25519_KEY_x2 {
101102
X25519_KEY k0, k1;
102103
};
103104

@@ -124,6 +125,7 @@ typedef struct _struct_DiffieHellmanXFunction_x2 {
124125
} X_ECDH_x2;
125126

126127
extern const X_ECDH_x2 X25519_AVX512;
128+
#endif /* defined(ENABLED_AVX512) */
127129

128130
#ifdef __cplusplus
129131
} /* namespace ecdh */

include/simd_avx2.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/**
2-
* Copyright 2017 Armando Faz Hernández
3-
* This file is part of faz_ecc_avx2.
4-
*
5-
* faz_ecc_avx2 is free software: you can redistribute it and/or modify
6-
* it under the terms of the GNU Lesser General Public License as published by
7-
* the Free Software Foundation, either version 3 of the License, or
8-
* (at your option) any later version.
9-
*
10-
* faz_ecc_avx2 is distributed in the hope that it will be useful,
11-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13-
* GNU Lesser General Public License for more details.
14-
*
15-
* You should have received a copy of the GNU Lesser General Public License
16-
* along with faz_ecc_avx2. If not, see <http://www.gnu.org/licenses/>.
17-
*/
18-
191
/**
202
* Copyright 2017 Armando Faz Hernández
213
* This file is part of faz_ecc_avx2.
@@ -112,6 +94,7 @@
11294
: /* in */ "r"(Y) \
11395
: /* regs */);
11496

97+
#if defined(ENABLED_AVX512)
11598
#define ZERO_x2 _mm512_setzero_si512()
11699
#define LOAD_x2(X) _mm512_load_si512((__m512i*) X)
117100
#define STORE_x2(X, Y) _mm512_store_si512((__m512i*) X, Y)
@@ -134,5 +117,6 @@
134117
#define SET64_x2(X7,X6,X5,X4,X3,X2,X1,X0) _mm512_set_epi64(X7,X6,X5,X4,X3,X2,X1,X0)
135118
#define EXTR_x2(X,Y) _mm512_extracti64x4_epi64(X,Y)
136119
#define INSR_x2(X,Y,Z) _mm512_inserti64x4(X,Y,Z)
120+
#endif /* defined(ENABLED_AVX512) */
137121

138122
#endif /* SIMD_AVX2_H */

src/dh255.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* along with faz_ecc_avx2. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#if defined(DH255) || 1
19+
#if defined(DH255)
2020

2121
#include "faz_ecdh_avx2.h"
2222

@@ -30,6 +30,7 @@ extern "C" {
3030

3131
#include "ladder255.c"
3232

33+
#if defined(ENABLED_AVX512)
3334
const X_ECDH_x2 X25519_AVX512 = {
3435
.keygen = x25519_keygen_avx512,
3536
.shared = x25519_shared_avx512,
@@ -39,6 +40,7 @@ const X_ECDH_x2 X25519_AVX512 = {
3940
.randKey = randX25519_Key,
4041
.printKey = printX25519_Key,
4142
};
43+
#endif /* defined(ENABLED_AVX512) */
4244

4345
const X_ECDH X25519_AVX2 = {
4446
.keygen = x25519_keygen_avx2,

src/eltfp25519_2w_redradix_x2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017 Armando Faz Hernández
2+
* Copyright 2025 Armando Faz Hernández
33
* This file is part of faz_ecc_avx2.
44
*
55
* faz_ecc_avx2 is free software: you can redistribute it and/or modify
@@ -16,7 +16,7 @@
1616
* along with faz_ecc_avx2. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#if defined(FP255) || 1
19+
#if defined(FP255) && defined(ENABLED_AVX512)
2020

2121
#define FN(NAME) SUFFIX(NAME, 2, redradix_x2)
2222
#define DECL(RETURN, NAME) SCOPE RETURN FN(NAME)

src/fp255.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* along with faz_ecc_avx2. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
#if defined(FP255) || 1
19+
#if defined(FP255)
2020

2121
#include "faz_fp_avx2.h"
2222
#include "simd_avx2.h"
@@ -35,7 +35,9 @@ extern "C" {
3535
#include "eltfp25519_1w_redradix.c"
3636
#include "eltfp25519_2w_redradix.c"
3737
#include "eltfp25519_4w_redradix.c"
38+
#if defined(ENABLED_AVX512)
3839
#include "eltfp25519_2w_redradix_x2.c"
40+
#endif /* defined(ENABLED_AVX512) */
3941

4042
const PrimeField Fp25519 = {
4143
._1w_full = {
@@ -136,6 +138,7 @@ const PrimeField Fp25519 = {
136138
.compressfast = SUFFIX(compressfast,2,redradix),
137139
},
138140
},
141+
#if defined(ENABLED_AVX512)
139142
._2w_red_x2 = {
140143
.arith = {
141144
.add = SUFFIX(add,2,redradix_x2),
@@ -169,6 +172,7 @@ const PrimeField Fp25519 = {
169172
.compressfast = SUFFIX(compressfast,2,redradix_x2),
170173
},
171174
},
175+
#endif /* defined(ENABLED_AVX512) */
172176
._4w_red = {
173177
.arith = {
174178
.add = SUFFIX(add,4,redradix),

0 commit comments

Comments
 (0)