| 1 |
// 20240812 djb: more cryptoint usage |
| 2 |
#include "crypto_core.h" |
| 3 |
#include "params.h" |
| 4 |
|
| 5 |
#include "crypto_int8.h" |
| 6 |
#include "crypto_int16.h" |
| 7 |
#include "crypto_int32.h" |
| 8 |
#define int8 crypto_int8 |
| 9 |
#define int16 crypto_int16 |
| 10 |
#define int32 crypto_int32 |
| 11 |
typedef int8 small; |
| 12 |
|
| 13 |
typedef int16 Fq; |
| 14 |
/* always represented as -(q-1)/2...(q-1)/2 */ |
| 15 |
|
| 16 |
/* works for -7000000 < x < 7000000 if q in 4591, 4621, 5167, 6343, 7177, 7879 */ |
| 17 |
static Fq Fq_freeze(int32 x) |
| 18 |
{ |
| 19 |
x -= q*((q18*x)>>18); |
| 20 |
x -= q*((q27*x+67108864)>>27); |
| 21 |
return x; |
| 22 |
} |
| 23 |
|
| 24 |
void crypto_core(unsigned char *outbytes,const unsigned char *inbytes,const unsigned char *kbytes,const unsigned char *cbytes) |
| 25 |
{ |
| 26 |
Fq f[p]; |
| 27 |
int32 f32[p]; |
| 28 |
int32 fg[p+p-1]; |
| 29 |
Fq h[p]; |
| 30 |
int i,j; |
| 31 |
|
| 32 |
crypto_decode_pxint16(f,inbytes); |
| 33 |
for (i = 0;i < p;++i) |
| 34 |
f32[i] = Fq_freeze(f[i]); |
| 35 |
|
| 36 |
for (i = 0; i < p + p - 1; ++i) fg[i] = 0; |
| 37 |
for (j = 0; j < p; ++j) { |
| 38 |
small gjx = kbytes[j]; |
| 39 |
small gj0 = crypto_int8_bottombit_01(gjx); |
| 40 |
int32 gj = gj0-(gjx&(gj0<<1)); |
| 41 |
for (i = 0; i < p; ++i) fg[i + j] += f32[i] * gj; |
| 42 |
} |
| 43 |
for (i = p; i < p + p - 1; ++i) fg[i - p] += fg[i]; |
| 44 |
for (i = p; i < p + p - 1; ++i) fg[i - p + 1] += fg[i]; |
| 45 |
for (i = 0; i < p; ++i) h[i] = Fq_freeze(fg[i]); |
| 46 |
|
| 47 |
crypto_encode_pxint16(outbytes,h); |
| 48 |
} |