|
|
@@ -1,6 +1,8 @@ |
|
|
|
#include "params.h" |
|
|
|
#include "rounding.h" |
|
|
|
#include <stdint.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <assert.h> |
|
|
|
|
|
|
|
/************************************************* |
|
|
|
* Name: PQCLEAN_DILITHIUM2_CLEAN_power2round |
|
|
@@ -37,14 +39,46 @@ int32_t PQCLEAN_DILITHIUM2_CLEAN_power2round(int32_t *a0, int32_t a) { |
|
|
|
* Returns a1. |
|
|
|
**************************************************/ |
|
|
|
int32_t PQCLEAN_DILITHIUM2_CLEAN_decompose(int32_t *a0, int32_t a) { |
|
|
|
int32_t a1; |
|
|
|
int32_t a1 = 0; |
|
|
|
uint64_t r; |
|
|
|
|
|
|
|
int32_t r0, r1; |
|
|
|
|
|
|
|
assert(a>0); assert(a<Q); |
|
|
|
|
|
|
|
// mod ALPHA |
|
|
|
static const uint32_t u = 360800; |
|
|
|
r = ((uint64_t)a)*u; |
|
|
|
r >>= 36; |
|
|
|
r *= 2 * GAMMA2; |
|
|
|
r = a - r; |
|
|
|
|
|
|
|
if (r>(2*GAMMA2)) { |
|
|
|
r -= 2*GAMMA2; |
|
|
|
} |
|
|
|
|
|
|
|
r1 = ((int32_t)r)*2*GAMMA2; |
|
|
|
|
|
|
|
// centrize |
|
|
|
if (r > GAMMA2) { |
|
|
|
*a0 = (int32_t)r - 2*GAMMA2; |
|
|
|
} else { |
|
|
|
*a0 = r; |
|
|
|
} |
|
|
|
|
|
|
|
// CASE: r-r0 = q-1 => r1=0, r0 = r0-1 |
|
|
|
|
|
|
|
// OLD |
|
|
|
a1 = (a + 127) >> 7; |
|
|
|
a1 = (a1 * 11275 + (1 << 23)) >> 24; |
|
|
|
a1 ^= ((43 - a1) >> 31) & a1; |
|
|
|
|
|
|
|
*a0 = a - a1 * 2 * GAMMA2; |
|
|
|
*a0 -= (((Q - 1) / 2 - *a0) >> 31) & Q; |
|
|
|
// TODO: ten sam trick co w barrett |
|
|
|
int32_t a2 = ((uint64_t)a-*a0)/(2*GAMMA2); |
|
|
|
|
|
|
|
//*a0 = a - a1 * 2 * GAMMA2; |
|
|
|
//*a0 -= (((Q - 1) / 2 - *a0) >> 31) & Q; |
|
|
|
printf("(%d, %d,\n)", a1, a2); |
|
|
|
return a1; |
|
|
|
} |
|
|
|
|
|
|
|