Skip to content

Commit d35124f

Browse files
committed
Make SSE2 configurable via CRYPTOPP_SSE2_INTRIN_AVAILABLE
Benchmarking shows things run a little slower with SSE2 on modern Core i5's. Also update comments and links
1 parent 67af746 commit d35124f

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

bench1.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ void OutputResultOperations(const char *name, const char *provider, const char *
122122

123123
oss << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : "");
124124
//oss << "<TD>" << provider;
125-
oss << "<TD>" << std::setprecision(4) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
125+
oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
126126

127127
// Coverity finding
128128
if (g_hertz > 1.0f)
129129
{
130130
const double t = timeTaken * g_hertz / iterations / 1000000;
131-
oss << "<TD>" << std::setprecision(4) << std::setiosflags(std::ios::fixed) << t;
131+
oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << t;
132132
}
133133

134134
g_logTotal += log(iterations/timeTaken);

donna.h

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// donna.h - written and placed in public domain by Jeffrey Walton
2-
// This is a port of Adam Langley's curve25519-donna
3-
// located at https://github.com/agl/curve25519-donna
2+
// This is a integration of Andrew Moon's public domain code.
3+
// Also see https://github.com/floodyberry/curve25519-donna.
4+
5+
// Benchmarking on a modern Core i5-6400 shows SSE2 on Linux is not
6+
// profitable. You can enable it with CRYPTOPP_CURVE25519_SSE2.
7+
8+
// If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
9+
// https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
410

511
#ifndef CRYPTOPP_DONNA_H
612
#define CRYPTOPP_DONNA_H
@@ -39,7 +45,19 @@ int curve25519(byte sharedKey[32], const byte secretKey[32], const byte othersKe
3945
# define CRYPTOPP_CURVE25519_64BIT 1
4046
#endif
4147

42-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
48+
// Benchmarking on a modern Core i5-6400 shows SSE2 on Linux is
49+
// not profitable. Here are the numbers in milliseconds/operation:
50+
//
51+
// * Langley, C++, 0.050
52+
// * Moon, C++: 0.040
53+
// * Moon, SSE2: 0.061
54+
// * Moon, native: 0.045
55+
56+
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE) && 0
57+
# define CRYPTOPP_CURVE25519_SSE2 1
58+
#endif
59+
60+
#if (CRYPTOPP_CURVE25519_SSE2)
4361
extern int curve25519_SSE2(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32]);
4462
#endif
4563

donna_32.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// donna_32.cpp - written and placed in public domain by Jeffrey Walton
22
// This is a integration of Andrew Moon's public domain code.
3-
// Also see curve25519-donna-32bit.h.
3+
// Also see https://github.com/floodyberry/curve25519-donna.
44

55
// If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
66
// https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
@@ -448,10 +448,7 @@ int curve25519_CXX(byte sharedKey[32], const byte secretKey[32], const byte othe
448448
FixedSizeSecBlock<byte, 32> e;
449449
for (size_t i = 0;i < 32;++i)
450450
e[i] = secretKey[i];
451-
452-
e[ 0] &= 0xf8;
453-
e[31] &= 0x7f;
454-
e[31] |= 0x40;
451+
e[0] &= 0xf8; e[31] &= 0x7f; e[31] |= 0x40;
455452

456453
bignum25519 nqpqx = {1}, nqpqz = {0}, nqz = {1}, nqx;
457454
bignum25519 q, qx, qpqx, qqx, zzz, zmone;
@@ -513,7 +510,7 @@ int curve25519_CXX(byte sharedKey[32], const byte secretKey[32], const byte othe
513510

514511
int curve25519(byte publicKey[32], const byte secretKey[32])
515512
{
516-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
513+
#if (CRYPTOPP_CURVE25519_SSE2)
517514
if (HasSSE2())
518515
return curve25519_SSE2(publicKey, secretKey, basePoint);
519516
else
@@ -524,7 +521,7 @@ int curve25519(byte publicKey[32], const byte secretKey[32])
524521

525522
int curve25519(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
526523
{
527-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
524+
#if (CRYPTOPP_CURVE25519_SSE2)
528525
if (HasSSE2())
529526
return curve25519_SSE2(sharedKey, secretKey, othersKey);
530527
else

donna_64.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// donna_64.cpp - written and placed in public domain by Jeffrey Walton
22
// This is a integration of Andrew Moon's public domain code.
3-
// Also see curve25519-donna-64bit.h.
3+
// Also see https://github.com/floodyberry/curve25519-donna.
44

55
// If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
66
// https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
@@ -442,7 +442,7 @@ int curve25519_CXX(byte sharedKey[32], const byte secretKey[32], const byte othe
442442

443443
int curve25519(byte publicKey[32], const byte secretKey[32])
444444
{
445-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
445+
#if (CRYPTOPP_CURVE25519_SSE2)
446446
if (HasSSE2())
447447
return curve25519_SSE2(publicKey, secretKey, basePoint);
448448
else
@@ -453,7 +453,7 @@ int curve25519(byte publicKey[32], const byte secretKey[32])
453453

454454
int curve25519(byte sharedKey[32], const byte secretKey[32], const byte othersKey[32])
455455
{
456-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
456+
#if (CRYPTOPP_CURVE25519_SSE2)
457457
if (HasSSE2())
458458
return curve25519_SSE2(sharedKey, secretKey, othersKey);
459459
else

donna_sse.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// donna_sse.cpp - written and placed in public domain by Jeffrey Walton
2-
// This is an integration of Andrew Moon's public domain code.
3-
// Also see curve25519-donna-sse2.h.
2+
// This is a integration of Andrew Moon's public domain code.
3+
// Also see https://github.com/floodyberry/curve25519-donna.
44

55
// This is a integration of Andrew Moon's public domain code. The port was
66
// clean, but it has one potential problem. The original code is C and relies
77
// upon unions. Accessing the inactive union member is undefined behavior in
88
// C++. That means copying the array into packedelem8.u is OK; but then using
9-
// packedelem8.v in a calcualtion is undefined behavior. We will have to
10-
// keep an eye on things or rewrite significant portions of this code.
9+
// packedelem8.v in a calcualtion is UB. Fortunately most (all?) compilers
10+
// take pity on C++ developers and compile the code. We will have to keep an
11+
// eye on things or rewrite significant portions of this code.
1112

1213
// If needed, see Moon's commit "Go back to ignoring 256th bit [sic]",
1314
// https://github.com/floodyberry/curve25519-donna/commit/57a683d18721a658
@@ -19,14 +20,20 @@
1920
#include "secblock.h"
2021
#include "misc.h"
2122

22-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
23+
#if (CRYPTOPP_CURVE25519_SSE2)
2324
# include <emmintrin.h>
2425
#endif
2526

27+
// The data is aligned, but Clang issues warning based on type
28+
// and not the actual alignment of the variable and data.
29+
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
30+
# pragma GCC diagnostic ignored "-Wcast-align"
31+
#endif
32+
2633
// Squash MS LNK4221 and libtool warnings
2734
extern const char DONNA_SSE_FNAME[] = __FILE__;
2835

29-
#if (CRYPTOPP_SSE2_INTRIN_AVAILABLE)
36+
#if (CRYPTOPP_CURVE25519_SSE2)
3037

3138
typedef __m128i xmmi;
3239
#define ALIGN(n) CRYPTOPP_ALIGN_DATA(n)
@@ -1164,4 +1171,4 @@ int curve25519_SSE2(byte sharedKey[32], const byte secretKey[32], const byte oth
11641171
NAMESPACE_END // Donna
11651172
NAMESPACE_END // CryptoPP
11661173

1167-
#endif // CRYPTOPP_SSE2_INTRIN_AVAILABLE
1174+
#endif // CRYPTOPP_CURVE25519_SSE2

0 commit comments

Comments
 (0)