| 1 |
/* { dg-do run { target { lp64 && avx2_runtime } } } */ |
| 2 |
/* { dg-options "-O3 -mavx2" } */ |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
#include <string.h> |
| 6 |
#include <immintrin.h> |
| 7 |
|
| 8 |
#define SPX_N 16 |
| 9 |
|
| 10 |
typedef struct { |
| 11 |
uint8_t pub_seed[SPX_N]; |
| 12 |
} spx_ctx; |
| 13 |
|
| 14 |
typedef float __m256_u __attribute__ ((__vector_size__ (32), __may_alias__, __aligned__ (1))); |
| 15 |
typedef double __m256d_u __attribute__ ((__vector_size__ (32), __may_alias__, __aligned__ (1))); |
| 16 |
typedef long long __m256i_u __attribute__ ((__vector_size__ (32), __may_alias__, __aligned__ (1))); |
| 17 |
|
| 18 |
__attribute__((noinline)) |
| 19 |
void Keccak(__m256i *states) { } |
| 20 |
|
| 21 |
__attribute__((noipa)) |
| 22 |
void capture(uint8_t *v) { } |
| 23 |
|
| 24 |
static void thashx4(uint64_t *out0, |
| 25 |
uint64_t *out1, |
| 26 |
uint64_t *out2, |
| 27 |
uint64_t *out3, |
| 28 |
unsigned int inblocks, |
| 29 |
const uint64_t* pub_seed) { |
| 30 |
if (inblocks == 1 || inblocks == 2) { |
| 31 |
__m256i state[25]; |
| 32 |
for (int i = 0; i < 25; i++) { |
| 33 |
state[i] = _mm256_set1_epi64x(0); |
| 34 |
} |
| 35 |
for (int i = 0; i < 2; i++) { |
| 36 |
state[i] = _mm256_set1_epi64x(pub_seed[i]); |
| 37 |
} |
| 38 |
|
| 39 |
/* Domain separator and padding. */ |
| 40 |
for (size_t i = 4 * inblocks + 4; i < 16; i++) { |
| 41 |
state[i] = _mm256_set1_epi64x(0); |
| 42 |
} |
| 43 |
|
| 44 |
Keccak(&state[0]); |
| 45 |
|
| 46 |
for (int i = 0; i < SPX_N / 8; i++) { |
| 47 |
out0[i] = _mm256_extract_epi64(state[i], 0); |
| 48 |
out1[i] = _mm256_extract_epi64(state[i], 1); |
| 49 |
out2[i] = _mm256_extract_epi64(state[i], 2); |
| 50 |
out3[i] = _mm256_extract_epi64(state[i], 3); |
| 51 |
} |
| 52 |
} else { |
| 53 |
unsigned char buf0[inblocks * SPX_N]; |
| 54 |
unsigned char buf1[inblocks * SPX_N]; |
| 55 |
unsigned char buf2[inblocks * SPX_N]; |
| 56 |
|
| 57 |
memcpy(buf0, pub_seed, SPX_N); |
| 58 |
memcpy(buf1, pub_seed, SPX_N); |
| 59 |
memcpy(buf2, pub_seed, SPX_N); |
| 60 |
|
| 61 |
capture(buf0); |
| 62 |
capture(buf1); |
| 63 |
capture(buf2); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static |
| 68 |
void wots_gen_leafx4(const spx_ctx *ctx) { |
| 69 |
uint64_t dest[4]; |
| 70 |
|
| 71 |
thashx4(dest, dest, dest, dest, 1, (const uint64_t *) ctx->pub_seed); |
| 72 |
thashx4(dest, dest, dest, dest, 3, (const uint64_t *) ctx->pub_seed); |
| 73 |
} |
| 74 |
|
| 75 |
void treehashx4_v2(const spx_ctx *ctx, |
| 76 |
void (*gen_leafx4)( const spx_ctx *), |
| 77 |
uint32_t* tree_addrx4 |
| 78 |
) { |
| 79 |
for (int i = 0; i < 2; i++) { |
| 80 |
gen_leafx4( ctx ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
__attribute__((noipa)) |
| 85 |
void crypto_sign_signature(uint64_t *sg, uint8_t enable, const uint8_t *sk) |
| 86 |
{ |
| 87 |
spx_ctx ctx; |
| 88 |
|
| 89 |
memcpy(ctx.pub_seed, sk, SPX_N); |
| 90 |
|
| 91 |
const uint64_t* ptr = (const uint64_t *)&ctx.pub_seed[0]; |
| 92 |
thashx4(sg, sg, sg, sg, 1, ptr); |
| 93 |
|
| 94 |
if (!enable) |
| 95 |
return; |
| 96 |
|
| 97 |
uint32_t tree_addrx4[32] = { 0 }; |
| 98 |
|
| 99 |
treehashx4_v2(&ctx, wots_gen_leafx4, tree_addrx4); |
| 100 |
} |
| 101 |
|
| 102 |
#define length_secret_key 64 |
| 103 |
#define length_signature 64 |
| 104 |
|
| 105 |
int main() { |
| 106 |
uint8_t secret_key[length_secret_key]; |
| 107 |
uint64_t signature[length_signature]; |
| 108 |
memset(secret_key, 0, length_secret_key); |
| 109 |
memset(signature, 0, length_signature * sizeof(uint64_t)); |
| 110 |
|
| 111 |
crypto_sign_signature(signature, 0, secret_key); |
| 112 |
|
| 113 |
for (int i = 0; i < length_signature; ++i) |
| 114 |
if (signature[i] != 0) |
| 115 |
__builtin_abort (); |
| 116 |
return 0; |
| 117 |
} |