Skip to content

Commit

Permalink
Merge pull request #303 from relic-toolkit/pornin
Browse files Browse the repository at this point in the history
Pornin
  • Loading branch information
dfaranha authored Aug 8, 2024
2 parents 0646e4f + 7f9f8d9 commit 6fde832
Show file tree
Hide file tree
Showing 18 changed files with 850 additions and 140 deletions.
5 changes: 5 additions & 0 deletions include/relic_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ ull_t arch_cycles(void);
*/
uint_t arch_lzcnt(dig_t);

/**
* Return the number of trailing zeros in an integer.
*/
uint_t arch_tzcnt(dig_t);

#if ARCH == AVR

/**
Expand Down
12 changes: 12 additions & 0 deletions include/relic_bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -1541,6 +1541,18 @@ void bn_rec_glv(bn_t k0, bn_t k1, const bn_t k, const bn_t n, const bn_t v1[],
void bn_rec_frb(bn_t *ki, int sub, const bn_t k, const bn_t x, const bn_t n,
int cof);

/**
* Recodes subscalars in the signed aligned column representation..
*
* @param[out] b - the recoded subscalars.
* @param[in] len - the length in bytes of the recoding.
* @param[in] k - the subscalars to recode.
* @param[in] m - the number of subscallars to recode.
* @param[in] n - the elliptic curve group order.
* @throw ERR_NO_BUFFER - if the buffer capacity is insufficient.
*/
void bn_rec_sac(int8_t *b, size_t *len, bn_t *k, size_t m, bn_t n);

/**
* Computes the coefficients of the polynomial representing the Lagrange
* interpolation for a modulus and a given set of roots.
Expand Down
2 changes: 2 additions & 0 deletions include/relic_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,10 @@ typedef struct _ctx_t {
/** Function pointer to underlying lznct implementation. */
#if ARCH == X86
unsigned int (*lzcnt_ptr)(dig_t);
unsigned int (*tzcnt_ptr)(dig_t);
#elif ARCH == X64 || ARCH == A64
unsigned int (*lzcnt_ptr)(ull_t);
unsigned int (*tzcnt_ptr)(ull_t);
#endif
} ctx_t;

Expand Down
8 changes: 8 additions & 0 deletions src/arch/relic_arch_a64.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand Down Expand Up @@ -177,6 +178,8 @@ void arch_init(void) {
if (ctx != NULL) {
core_get()->lzcnt_ptr =
(has_lzcnt_hard() ? lzcnt64_hard : lzcnt64_soft);
core_get()->tzcnt_ptr =
(has_lzcnt_hard() ? tzcnt64_hard : tzcnt64_soft);
}

#if TIMER == CYCLE
Expand All @@ -199,6 +202,7 @@ void arch_clean(void) {
ctx_t *ctx = core_get();
if (ctx != NULL) {
core_get()->lzcnt_ptr = NULL;
core_get()->tzcnt_ptr = NULL;
}
}

Expand Down Expand Up @@ -234,3 +238,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr(x);
}
9 changes: 9 additions & 0 deletions src/arch/relic_arch_arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "relic_types.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand Down Expand Up @@ -111,3 +112,11 @@ uint_t arch_lzcnt(uint_t x) {
return lzcnt64_gcc_arm(x);
#endif
}

uint_t arch_tzcnt(uint_t x) {
#ifdef WSIZE == 32
return tzcnt32_gcc_arm(x);
#elif WSIZE == 64
return tzcnt64_gcc_arm(x);
#endif
}
12 changes: 12 additions & 0 deletions src/arch/relic_arch_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ uint_t arch_lzcnt() {
}
return 0;
}

uint_t arch_tzcnt() {
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
}
29 changes: 29 additions & 0 deletions src/arch/relic_arch_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,32 @@ uint_t arch_lzcnt() {
return 0;
#endif
}

uint_t arch_tzcnt() {
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
#if WSIZE == 8
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
#elif WSIZE == 16
int offset;

if (a & 0xFF == 0) {
offset = 8;
} else {
offset = 0;
}
a = a >> offset;
if (a >> 4 != 0) {
return table[a & 0xF] + offset;
} else {
return table[a >> 4] + 4 + offset;
}
return 0;
#endif
}
45 changes: 44 additions & 1 deletion src/arch/relic_arch_none.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,50 @@ uint_t arch_lzcnt(dig_t a) {
#ifdef _MSC_VER
return __lzcnt64(a);
#else
return __builtin_clzll(a);
return __builtin_clzl(a);
#endif
#endif
}

uint_t arch_tzcnt(dig_t a) {
#if WSIZE == 8 || WSIZE == 16
static const uint8_t table[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};
#endif
#if WSIZE == 8
if (a >> 4 != 0) {
return table[a & 0xF];
} else {
return table[a >> 4] + 4;
}
return 0;
#elif WSIZE == 16
int offset;

if (a & 0xFF == 0) {
offset = 8;
} else {
offset = 0;
}
a = a >> offset;
if (a >> 4 != 0) {
return table[a & 0xF] + offset;
} else {
return table[a >> 4] + 4 + offset;
}
return 0;
#elif WSIZE == 32
#ifdef _MSC_VER
return __tzcnt(a);
#else
return __builtin_ctz(a);
#endif
#elif WSIZE == 64
#ifdef _MSC_VER
return __tzcnt64(a);
#else
return __builtin_ctzl(a);
#endif
#endif
}
8 changes: 8 additions & 0 deletions src/arch/relic_arch_x64.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/**
* Renames the inline assembly macro to a prettier name.
Expand All @@ -51,13 +52,16 @@ void arch_init(void) {
if (ctx != NULL) {
core_get()->lzcnt_ptr =
(has_lzcnt_hard() ? lzcnt64_hard : lzcnt64_soft);
core_get()->tzcnt_ptr =
(has_tzcnt_hard() ? tzcnt64_hard : tzcnt64_soft);
}
}

void arch_clean(void) {
ctx_t *ctx = core_get();
if (ctx != NULL) {
core_get()->lzcnt_ptr = NULL;
core_get()->tzcnt_ptr = NULL;
}
}

Expand Down Expand Up @@ -103,3 +107,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((ull_t)x) - (8 * sizeof(ull_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr(x);
}
7 changes: 7 additions & 0 deletions src/arch/relic_arch_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@
#include "relic_core.h"

#include "lzcnt.inc"
#include "tzcnt.inc"

/*============================================================================*/
/* Public definitions */
/*============================================================================*/

void arch_init(void) {
core_get()->lzcnt_ptr = (has_lzcnt_hard() ? lzcnt32_hard : lzcnt32_soft);
core_get()->tzcnt_ptr = (has_tzcnt_hard() ? tzcnt32_hard : tzcnt32_soft);
}

void arch_clean(void) {
core_get()->lzcnt_ptr = NULL;
core_get()->tzcnt_ptr = NULL;
}

ull_t arch_cycles(void) {
Expand All @@ -64,3 +67,7 @@ ull_t arch_cycles(void) {
uint_t arch_lzcnt(dig_t x) {
return core_get()->lzcnt_ptr((uint32_t)x) - (8 * sizeof(uint32_t) - WSIZE);
}

uint_t arch_tzcnt(dig_t x) {
return core_get()->tzcnt_ptr(x);
}
Loading

0 comments on commit 6fde832

Please sign in to comment.