Skip to content

Commit

Permalink
supercop-20081111
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel J. Bernstein authored and floodyberry committed Nov 10, 2008
1 parent c00c042 commit efc221e
Show file tree
Hide file tree
Showing 70 changed files with 2,118 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cpuid/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main()
putchar(c);
}

printf("-%08x-%08x\n",y[0],y[3]);
printf("-%08x-%08x\n",(unsigned int) y[0],(unsigned int) y[3]);

return 0;
}
2 changes: 1 addition & 1 deletion crypto_hash/cubehash81/emmintrin/api.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define crypto_hash_cubehash81_emmintrin_BYTES 64
#define CRYPTO_BYTES 64
8 changes: 4 additions & 4 deletions crypto_hash/cubehash81/emmintrin/cubehash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
20081106
20081110
D. J. Bernstein
Public domain.
*/
Expand Down Expand Up @@ -116,7 +116,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
/* so state->pos is a multiple of 8 */

while (databitlen >= 8) {
myuint32 u = *data;
crypto_uint32 u = *data;
u <<= 8 * ((state->pos / 8) % 4);
state->x[state->pos / 32] ^= u;
data += 1;
Expand All @@ -128,7 +128,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
}
}
if (databitlen > 0) {
myuint32 u = *data;
crypto_uint32 u = *data;
u <<= 8 * ((state->pos / 8) % 4);
state->x[state->pos / 32] ^= u;
state->pos += databitlen;
Expand All @@ -139,7 +139,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
HashReturn Final(hashState *state, BitSequence *hashval)
{
int i;
myuint32 u;
crypto_uint32 u;

u = (128 >> (state->pos % 8));
u <<= 8 * ((state->pos / 8) % 4);
Expand Down
4 changes: 2 additions & 2 deletions crypto_hash/cubehash81/emmintrin/cubehash.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ typedef unsigned char BitSequence;
typedef unsigned long long DataLength;
typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHBITLEN = 2 } HashReturn;

typedef unsigned int myuint32; /* must be exactly 32 bits */
#include "crypto_uint32.h"

typedef struct {
int hashbitlen;
int pos; /* number of bits read into x from current block */
myuint32 x[32];
crypto_uint32 x[32];
} hashState;

HashReturn Init(hashState *state, int hashbitlen);
Expand Down
2 changes: 1 addition & 1 deletion crypto_hash/cubehash81/emmintrin2/api.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define crypto_hash_cubehash81_emmintrin2_BYTES 64
#define CRYPTO_BYTES 64
2 changes: 1 addition & 1 deletion crypto_hash/cubehash81/emmintrin3/api.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define crypto_hash_cubehash81_emmintrin3_BYTES 64
#define CRYPTO_BYTES 64
2 changes: 1 addition & 1 deletion crypto_hash/cubehash81/simple/api.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define crypto_hash_cubehash81_simple_BYTES 64
#define CRYPTO_BYTES 64
10 changes: 5 additions & 5 deletions crypto_hash/cubehash81/simple/cubehash.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
20081106
20081110
D. J. Bernstein
Public domain.
*/
Expand All @@ -13,7 +13,7 @@ static void transform(hashState *state)
{
int i;
int r;
myuint32 y[16];
crypto_uint32 y[16];

for (r = 0;r < CUBEHASH_ROUNDS;++r) {
for (i = 0;i < 16;++i) state->x[i + 16] += state->x[i];
Expand Down Expand Up @@ -57,7 +57,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
/* so state->pos is a multiple of 8 */

while (databitlen >= 8) {
myuint32 u = *data;
crypto_uint32 u = *data;
u <<= 8 * ((state->pos / 8) % 4);
state->x[state->pos / 32] ^= u;
data += 1;
Expand All @@ -69,7 +69,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
}
}
if (databitlen > 0) {
myuint32 u = *data;
crypto_uint32 u = *data;
u <<= 8 * ((state->pos / 8) % 4);
state->x[state->pos / 32] ^= u;
state->pos += databitlen;
Expand All @@ -80,7 +80,7 @@ HashReturn Update(hashState *state, const BitSequence *data,
HashReturn Final(hashState *state, BitSequence *hashval)
{
int i;
myuint32 u;
crypto_uint32 u;

u = (128 >> (state->pos % 8));
u <<= 8 * ((state->pos / 8) % 4);
Expand Down
6 changes: 3 additions & 3 deletions crypto_hash/cubehash81/simple/cubehash.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
20081106
20081110
D. J. Bernstein
Public domain.
*/
Expand All @@ -11,12 +11,12 @@ typedef unsigned char BitSequence;
typedef unsigned long long DataLength;
typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHBITLEN = 2 } HashReturn;

typedef unsigned int myuint32; /* must be exactly 32 bits */
#include "crypto_uint32.h"

typedef struct {
int hashbitlen;
int pos; /* number of bits read into x from current block */
myuint32 x[32];
crypto_uint32 x[32];
} hashState;

HashReturn Init(hashState *state, int hashbitlen);
Expand Down
2 changes: 1 addition & 1 deletion crypto_hash/cubehash81/spec/api.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define crypto_hash_cubehash81_spec_BYTES 64
#define CRYPTO_BYTES 64
6 changes: 3 additions & 3 deletions crypto_hash/cubehash81/spec/cubehash.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*
20081106
20081110
D. J. Bernstein
Public domain.
*/

#include "parameters.h"
#include "cubehash.h"

typedef unsigned int myuint32; /* must be exactly 32 bits */
#include "crypto_uint32.h"
#define myuint32 crypto_uint32

#define ROTATEUPWARDS7(a) (((a) << 7) | ((a) >> 25))
#define ROTATEUPWARDS11(a) (((a) << 11) | ((a) >> 21))
Expand Down
1 change: 1 addition & 0 deletions crypto_hash/cubehash82/checksum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4bd726b2d467e75d743679abfee036b672941e34dd91af6a29348599bbc94e33108ebd1ff8e2988a71a7339572744aadc5525a2fa169493d6b30123cfce7ce0b
1 change: 1 addition & 0 deletions crypto_hash/cubehash82/emmintrin3/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define CRYPTO_BYTES 64
239 changes: 239 additions & 0 deletions crypto_hash/cubehash82/emmintrin3/cubehash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/*
20081110
D. J. Bernstein
Public domain.
Note: This code assumes that CUBEHASH_BLOCKBYTES is 2.
*/

#include "parameters.h"
#include "cubehash.h"
#include "crypto_uint32.h"
#include "crypto_uint16.h"

static void transform(hashState *state,int r)
{
__m128i x0;
__m128i x1;
__m128i x2;
__m128i x3;
__m128i x4;
__m128i x5;
__m128i x6;
__m128i x7;
__m128i y0;
__m128i y1;
__m128i y2;
__m128i y3;

x0 = state->x[0];
x1 = state->x[1];
x2 = state->x[2];
x3 = state->x[3];
x4 = state->x[4];
x5 = state->x[5];
x6 = state->x[6];
x7 = state->x[7];

for (;r > 0;--r) {
x4 = _mm_add_epi32(x0,x4);
x5 = _mm_add_epi32(x1,x5);
x6 = _mm_add_epi32(x2,x6);
x7 = _mm_add_epi32(x3,x7);
y0 = x2;
y1 = x3;
y2 = x0;
y3 = x1;
x0 = _mm_xor_si128(_mm_slli_epi32(y0,7),_mm_srli_epi32(y0,25));
x1 = _mm_xor_si128(_mm_slli_epi32(y1,7),_mm_srli_epi32(y1,25));
x2 = _mm_xor_si128(_mm_slli_epi32(y2,7),_mm_srli_epi32(y2,25));
x3 = _mm_xor_si128(_mm_slli_epi32(y3,7),_mm_srli_epi32(y3,25));
x0 = _mm_xor_si128(x0,x4);
x1 = _mm_xor_si128(x1,x5);
x2 = _mm_xor_si128(x2,x6);
x3 = _mm_xor_si128(x3,x7);
x4 = _mm_shuffle_epi32(x4,0x4e);
x5 = _mm_shuffle_epi32(x5,0x4e);
x6 = _mm_shuffle_epi32(x6,0x4e);
x7 = _mm_shuffle_epi32(x7,0x4e);
x4 = _mm_add_epi32(x0,x4);
x5 = _mm_add_epi32(x1,x5);
x6 = _mm_add_epi32(x2,x6);
x7 = _mm_add_epi32(x3,x7);
y0 = x1;
y1 = x0;
y2 = x3;
y3 = x2;
x0 = _mm_xor_si128(_mm_slli_epi32(y0,11),_mm_srli_epi32(y0,21));
x1 = _mm_xor_si128(_mm_slli_epi32(y1,11),_mm_srli_epi32(y1,21));
x2 = _mm_xor_si128(_mm_slli_epi32(y2,11),_mm_srli_epi32(y2,21));
x3 = _mm_xor_si128(_mm_slli_epi32(y3,11),_mm_srli_epi32(y3,21));
x0 = _mm_xor_si128(x0,x4);
x1 = _mm_xor_si128(x1,x5);
x2 = _mm_xor_si128(x2,x6);
x3 = _mm_xor_si128(x3,x7);
x4 = _mm_shuffle_epi32(x4,0xb1);
x5 = _mm_shuffle_epi32(x5,0xb1);
x6 = _mm_shuffle_epi32(x6,0xb1);
x7 = _mm_shuffle_epi32(x7,0xb1);
}

state->x[0] = x0;
state->x[1] = x1;
state->x[2] = x2;
state->x[3] = x3;
state->x[4] = x4;
state->x[5] = x5;
state->x[6] = x6;
state->x[7] = x7;
}

HashReturn Init(hashState *state, int hashbitlen)
{
int i;

if (hashbitlen < 8) return BAD_HASHBITLEN;
if (hashbitlen > 512) return BAD_HASHBITLEN;
if (hashbitlen != 8 * (hashbitlen / 8)) return BAD_HASHBITLEN;

state->hashbitlen = hashbitlen;
for (i = 0;i < 8;++i) state->x[i] = _mm_set_epi32(0,0,0,0);
state->x[0] = _mm_set_epi32(0,CUBEHASH_ROUNDS,CUBEHASH_BLOCKBYTES,hashbitlen / 8);
transform(state,10 * CUBEHASH_ROUNDS);
state->pos = 0;
return SUCCESS;
}

HashReturn Update(hashState *state, const BitSequence *data,
DataLength databitlen)
{
int r;
__m128i x0;
__m128i x1;
__m128i x2;
__m128i x3;
__m128i x4;
__m128i x5;
__m128i x6;
__m128i x7;
__m128i y0;
__m128i y1;
__m128i y2;
__m128i y3;

while (databitlen >= 8 && state->pos != 0) {
((unsigned char *) state->x)[state->pos / 8] ^= *data;
data += 1;
databitlen -= 8;
state->pos += 8;
if (state->pos == 8 * CUBEHASH_BLOCKBYTES) {
transform(state,CUBEHASH_ROUNDS);
state->pos = 0;
}
}

x0 = state->x[0];
x1 = state->x[1];
x2 = state->x[2];
x3 = state->x[3];
x4 = state->x[4];
x5 = state->x[5];
x6 = state->x[6];
x7 = state->x[7];

while (databitlen >= 8 * CUBEHASH_BLOCKBYTES) {
x0 = _mm_xor_si128(x0,_mm_set_epi32(0,0,0,(crypto_uint32) *(crypto_uint16 *) data));
data += CUBEHASH_BLOCKBYTES;
databitlen -= 8 * CUBEHASH_BLOCKBYTES;

for (r = 0;r < CUBEHASH_ROUNDS;++r) {
x4 = _mm_add_epi32(x0,x4);
x5 = _mm_add_epi32(x1,x5);
x6 = _mm_add_epi32(x2,x6);
x7 = _mm_add_epi32(x3,x7);
y0 = x2;
y1 = x3;
y2 = x0;
y3 = x1;
x0 = _mm_xor_si128(_mm_slli_epi32(y0,7),_mm_srli_epi32(y0,25));
x1 = _mm_xor_si128(_mm_slli_epi32(y1,7),_mm_srli_epi32(y1,25));
x2 = _mm_xor_si128(_mm_slli_epi32(y2,7),_mm_srli_epi32(y2,25));
x3 = _mm_xor_si128(_mm_slli_epi32(y3,7),_mm_srli_epi32(y3,25));
x0 = _mm_xor_si128(x0,x4);
x1 = _mm_xor_si128(x1,x5);
x2 = _mm_xor_si128(x2,x6);
x3 = _mm_xor_si128(x3,x7);
x4 = _mm_shuffle_epi32(x4,0x4e);
x5 = _mm_shuffle_epi32(x5,0x4e);
x6 = _mm_shuffle_epi32(x6,0x4e);
x7 = _mm_shuffle_epi32(x7,0x4e);
x4 = _mm_add_epi32(x0,x4);
x5 = _mm_add_epi32(x1,x5);
x6 = _mm_add_epi32(x2,x6);
x7 = _mm_add_epi32(x3,x7);
y0 = x1;
y1 = x0;
y2 = x3;
y3 = x2;
x0 = _mm_xor_si128(_mm_slli_epi32(y0,11),_mm_srli_epi32(y0,21));
x1 = _mm_xor_si128(_mm_slli_epi32(y1,11),_mm_srli_epi32(y1,21));
x2 = _mm_xor_si128(_mm_slli_epi32(y2,11),_mm_srli_epi32(y2,21));
x3 = _mm_xor_si128(_mm_slli_epi32(y3,11),_mm_srli_epi32(y3,21));
x0 = _mm_xor_si128(x0,x4);
x1 = _mm_xor_si128(x1,x5);
x2 = _mm_xor_si128(x2,x6);
x3 = _mm_xor_si128(x3,x7);
x4 = _mm_shuffle_epi32(x4,0xb1);
x5 = _mm_shuffle_epi32(x5,0xb1);
x6 = _mm_shuffle_epi32(x6,0xb1);
x7 = _mm_shuffle_epi32(x7,0xb1);
}
}

state->x[0] = x0;
state->x[1] = x1;
state->x[2] = x2;
state->x[3] = x3;
state->x[4] = x4;
state->x[5] = x5;
state->x[6] = x6;
state->x[7] = x7;

while (databitlen >= 8) {
((unsigned char *) state->x)[state->pos / 8] ^= *data;
data += 1;
databitlen -= 8;
state->pos += 8;
if (state->pos == 8 * CUBEHASH_BLOCKBYTES) {
transform(state,CUBEHASH_ROUNDS);
state->pos = 0;
}
}
if (databitlen > 0) {
((unsigned char *) state->x)[state->pos / 8] ^= *data;
state->pos += databitlen;
}
return SUCCESS;
}

HashReturn Final(hashState *state, BitSequence *hashval)
{
int i;

((unsigned char *) state->x)[state->pos / 8] ^= (128 >> (state->pos % 8));
transform(state,CUBEHASH_ROUNDS);
state->x[7] = _mm_xor_si128(state->x[7],_mm_set_epi32(1,0,0,0));
transform(state,10 * CUBEHASH_ROUNDS);
for (i = 0;i < state->hashbitlen / 8;++i)
hashval[i] = ((unsigned char *) state->x)[i];

return SUCCESS;
}

HashReturn Hash(int hashbitlen, const BitSequence *data,
DataLength databitlen, BitSequence *hashval)
{
hashState state;
if (Init(&state,hashbitlen) != SUCCESS) return BAD_HASHBITLEN;
Update(&state,data,databitlen);
return Final(&state,hashval);
}
Loading

0 comments on commit efc221e

Please sign in to comment.