Crypt-PQClean-Sign
view release on metacpan or search on metacpan
pqclean/crypto_sign/falcon-1024/aarch64/codec.c view on Meta::CPAN
* Extra bits in the last byte must be zero.
*/
return 0;
}
return in_len;
}
/* see inner.h */
size_t
PQCLEAN_FALCON1024_AARCH64_comp_encode(void *out, size_t max_out_len, const int16_t *x) {
uint8_t *buf;
size_t u, v;
uint32_t acc;
unsigned acc_len;
buf = out;
/*
* Make sure that all values are within the -2047..+2047 range.
*/
if (PQCLEAN_FALCON1024_AARCH64_poly_check_bound_int16(x, -2047, 2047)) {
return 0;
}
acc = 0;
acc_len = 0;
v = 0;
for (u = 0; u < FALCON_N; u ++) {
int t;
unsigned w;
/*
* Get sign and absolute value of next integer; push the
* sign bit.
*/
acc <<= 1;
t = x[u];
if (t < 0) {
t = -t;
acc |= 1;
}
w = (unsigned)t;
/*
* Push the low 7 bits of the absolute value.
*/
acc <<= 7;
acc |= w & 127u;
w >>= 7;
/*
* We pushed exactly 8 bits.
*/
acc_len += 8;
/*
* Push as many zeros as necessary, then a one. Since the
* absolute value is at most 2047, w can only range up to
* 15 at this point, thus we will add at most 16 bits
* here. With the 8 bits above and possibly up to 7 bits
* from previous iterations, we may go up to 31 bits, which
* will fit in the accumulator, which is an uint32_t.
*/
acc <<= (w + 1);
acc |= 1;
acc_len += w + 1;
/*
* Produce all full bytes.
*/
while (acc_len >= 8) {
acc_len -= 8;
if (buf != NULL) {
if (v >= max_out_len) {
return 0;
}
buf[v] = (uint8_t)(acc >> acc_len);
}
v ++;
}
}
/*
* Flush remaining bits (if any).
*/
if (acc_len > 0) {
if (buf != NULL) {
if (v >= max_out_len) {
return 0;
}
buf[v] = (uint8_t)(acc << (8 - acc_len));
}
v ++;
}
return v;
}
/* see inner.h */
size_t
PQCLEAN_FALCON1024_AARCH64_comp_decode(int16_t *x, const void *in, size_t max_in_len) {
const uint8_t *buf;
size_t u, v;
uint32_t acc;
unsigned acc_len;
buf = in;
acc = 0;
acc_len = 0;
v = 0;
for (u = 0; u < FALCON_N; u ++) {
unsigned b, s, m;
/*
* Get next eight bits: sign and low seven bits of the
* absolute value.
*/
if (v >= max_in_len) {
return 0;
}
acc = (acc << 8) | (uint32_t)buf[v ++];
( run in 1.697 second using v1.01-cache-2.11-cpan-96521ef73a4 )