BMR, Use your Brain, Semi/Semi2k.

This commit is contained in:
Marcel Keller
2019-04-30 17:24:11 +10:00
parent 9cde9548d2
commit b5d8123ae0
214 changed files with 4233 additions and 1550 deletions

View File

@@ -93,6 +93,7 @@ public:
ReceivedMsgStore() : start(0), mem_size(0), total_size(0) {}
~ReceivedMsgStore();
void push(ReceivedMsg& msg);
void push_and_clear(LocalBuffer& msg) { push(msg); msg.clear(); }
bool pop(ReceivedMsg& msg);
bool empty() { return mem_size == 0 and files.empty(); }
};

View File

@@ -146,3 +146,5 @@ void MMO::hashBlockWise<gfp1,128>(octet* output, octet* input)
template void MMO::hashBlocks<F,N>(void*, const void*);
#define Z(F) ZZ(F,1) ZZ(F,2) ZZ(F,8)
Z(gf2n_long) Z(Z2<64>) Z(Z2<112>) Z(Z2<128>) Z(Z2<160>) Z(Z2<114>) Z(Z2<130>)
Z(Z2<72>)
Z(SignedZ2<64>) Z(SignedZ2<72>)

33
Tools/NetworkOptions.cpp Normal file
View File

@@ -0,0 +1,33 @@
/*
* NetworkOptions.cpp
*
*/
#include "NetworkOptions.h"
NetworkOptions::NetworkOptions(ez::ezOptionParser& opt, int argc,
const char** argv)
{
opt.add(
"localhost", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Host where party 0 is running (default: localhost)", // Help description.
"-h", // Flag token.
"--hostname" // Flag token.
);
opt.add(
"5000", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Base port number (default: 5000).", // Help description.
"-pn", // Flag token.
"--portnum" // Flag token.
);
opt.parse(argc, argv);
opt.get("-pn")->getInt(portnum_base);
opt.get("-h")->getString(hostname);
opt.resetArgs();
}

22
Tools/NetworkOptions.h Normal file
View File

@@ -0,0 +1,22 @@
/*
* NetworkOptions.h
*
*/
#ifndef TOOLS_NETWORKOPTIONS_H_
#define TOOLS_NETWORKOPTIONS_H_
#include "ezOptionParser.h"
#include <string>
class NetworkOptions
{
public:
int portnum_base;
std::string hostname;
NetworkOptions(ez::ezOptionParser& opt, int argc, const char** argv);
};
#endif /* TOOLS_NETWORKOPTIONS_H_ */

View File

@@ -6,17 +6,6 @@
* M-Code Version *
**********************/
#define cpuid(func,ax,bx,cx,dx)\
__asm__ __volatile__ ("cpuid":\
"=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
int Check_CPU_support_AES()
{ unsigned int a,b,c,d;
cpuid(1, a,b,c,d);
return (c & 0x2000000);
}
inline __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2)
{ __m128i temp3; temp2 = _mm_shuffle_epi32 (temp2 ,0xff);
temp3 = _mm_slli_si128 (temp1, 0x4);
@@ -33,43 +22,46 @@ inline __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2)
void aes_128_schedule( octet* key, const octet* userkey )
{
#ifdef __AES__
__m128i temp1, temp2;
__m128i *Key_Schedule = (__m128i*)key;
temp1 = _mm_loadu_si128((__m128i*)userkey);
Key_Schedule[0] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1 ,0x1);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[1] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x2);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[2] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x4);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[3] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x8);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[4] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x10);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[5] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x20);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[6] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x40);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[7] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x80);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[8] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x1b);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[9] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x36);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[10] = temp1;
#else
aes_128_schedule((uint*) key, userkey);
if (cpu_has_aes())
{
__m128i temp1, temp2;
__m128i *Key_Schedule = (__m128i*)key;
temp1 = _mm_loadu_si128((__m128i*)userkey);
Key_Schedule[0] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1 ,0x1);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[1] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x2);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[2] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x4);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[3] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x8);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[4] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x10);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[5] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x20);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[6] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x40);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[7] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x80);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[8] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x1b);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[9] = temp1;
temp2 = _mm_aeskeygenassist_si128 (temp1,0x36);
temp1 = AES_128_ASSIST(temp1, temp2);
Key_Schedule[10] = temp1;
}
else
#endif
aes_128_schedule((uint*) key, userkey);
}
#ifdef __AES__

View File

@@ -4,6 +4,7 @@
#include <wmmintrin.h>
#include "Networking/data.h"
#include "cpu_support.h"
typedef unsigned int uint;
@@ -33,7 +34,7 @@ inline void aes_encrypt( octet* C, octet* M, uint* RK )
/*********** M-Code Version ***********/
// Check can support this
int Check_CPU_support_AES();
inline int Check_CPU_support_AES() { return cpu_has_aes(); }
// Key Schedule
void aes_128_schedule( octet* key, const octet* userkey );
void aes_192_schedule( octet* key, const octet* userkey );
@@ -52,17 +53,32 @@ void aes_256_encrypt( octet* C, const octet* M,const octet* RK );
__attribute__((optimize("unroll-loops")))
#endif
inline __m128i aes_128_encrypt(__m128i in, const octet* key)
{ __m128i& tmp = in;
tmp = _mm_xor_si128 (tmp,((__m128i*)key)[0]);
{
#ifdef __AES__
int j;
for(j=1; j <10; j++)
{ tmp = _mm_aesenc_si128 (tmp,((__m128i*)key)[j]); }
tmp = _mm_aesenclast_si128 (tmp,((__m128i*)key)[j]);
#else
throw runtime_error("need to compile with AES-NI support");
if (cpu_has_aes())
{
__m128i& tmp = in;
tmp = _mm_xor_si128 (tmp,((__m128i*)key)[0]);
int j;
for(j=1; j <10; j++)
tmp = _mm_aesenc_si128 (tmp,((__m128i*)key)[j]);
tmp = _mm_aesenclast_si128 (tmp,((__m128i*)key)[j]);
return tmp;
}
else
#endif
return tmp;
{
__m128i tmp;
aes_128_encrypt((octet*) &tmp, (octet*) &in, (uint*) key);
return tmp;
}
}
template <int N>
inline void software_ecb_aes_128_encrypt(__m128i* out, __m128i* in, uint* key)
{
for (int i = 0; i < N; i++)
aes_128_encrypt((octet*)&out[i], (octet*)&in[i], key);
}
template <int N>
@@ -72,19 +88,21 @@ __attribute__((optimize("unroll-loops")))
inline void ecb_aes_128_encrypt(__m128i* out, __m128i* in, const octet* key)
{
#ifdef __AES__
__m128i tmp[N];
for (int i = 0; i < N; i++)
tmp[i] = _mm_xor_si128 (in[i],((__m128i*)key)[0]);
int j;
for(j=1; j <10; j++)
if (cpu_has_aes())
{
__m128i tmp[N];
for (int i = 0; i < N; i++)
tmp[i] = _mm_aesenc_si128 (tmp[i],((__m128i*)key)[j]);
for (int i = 0; i < N; i++)
out[i] = _mm_aesenclast_si128 (tmp[i],((__m128i*)key)[j]);
#else
for (int i = 0; i < N; i++)
aes_128_encrypt((octet*)&out[i], (octet*)&in[i], (uint*)key);
tmp[i] = _mm_xor_si128 (in[i],((__m128i*)key)[0]);
int j;
for(j=1; j <10; j++)
for (int i = 0; i < N; i++)
tmp[i] = _mm_aesenc_si128 (tmp[i],((__m128i*)key)[j]);
for (int i = 0; i < N; i++)
out[i] = _mm_aesenclast_si128 (tmp[i],((__m128i*)key)[j]);
}
else
#endif
software_ecb_aes_128_encrypt<N>(out, in, (uint*) key);
}
template <int N>

View File

@@ -60,8 +60,14 @@ inline void avx_memzero(void* dest, size_t length)
length -= 32;
}
#endif
if (length)
switch (length)
{
case 8:
*(int64_t*)d = 0;
return;
default:
memset((void*)d, 0, length);
}
}
#endif /* TOOLS_AVX_MEMCPY_H_ */

71
Tools/cpu_support.h Normal file
View File

@@ -0,0 +1,71 @@
/*
* cpu_support.h
*
*/
#ifndef TOOLS_CPU_SUPPORT_H_
#define TOOLS_CPU_SUPPORT_H_
inline bool check_cpu(int func, bool ecx, int feature)
{
int ax = func, bx, cx = 0, dx;
__asm__ __volatile__ ("cpuid":
"+a" (ax), "=b" (bx), "+c" (cx), "=d" (dx));
return ((ecx ? cx : bx) >> feature) & 1;
}
inline bool cpu_has_adx()
{
#ifdef CHECK_ADX
return check_cpu(7, false, 19);
#else
return true;
#endif
}
inline bool cpu_has_bmi2()
{
#ifdef CHECK_BMI2
return check_cpu(7, false, 8);
#else
return true;
#endif
}
inline bool cpu_has_avx2()
{
#ifdef CHECK_AVX2
return check_cpu(7, false, 5);
#else
return true;
#endif
}
inline bool cpu_has_avx()
{
#ifdef CHECK_AVX
return check_cpu(1, true, 28);
#else
return true;
#endif
}
inline bool cpu_has_pclmul()
{
#ifdef CHECK_PCLMUL
return check_cpu(1, true, 1);
#else
return true;
#endif
}
inline bool cpu_has_aes()
{
#ifdef CHECK_AES
return check_cpu(1, true, 25);
#else
return true;
#endif
}
#endif /* TOOLS_CPU_SUPPORT_H_ */

View File

@@ -81,7 +81,7 @@ void PRNG::print_state() const
cout << hex << (int) random[i];
}
cout << "\t";
for (i=0; i<SEED_SIZE; i++)
for (i=0; i<RAND_SIZE; i++)
{ if (state[i]<10) { cout << "0"; }
cout << hex << (int) state[i];
}
@@ -99,7 +99,7 @@ void PRNG::hash()
blk_SHA1_Final(random,&ctx);
#else
if (useC)
{ aes_encrypt(random,state,KeyScheduleC); }
{ software_ecb_aes_128_encrypt<PIPELINES>((__m128i*)random,(__m128i*)state,KeyScheduleC); }
else
{ ecb_aes_128_encrypt<PIPELINES>((__m128i*)random,(__m128i*)state,KeySchedule); }
#endif