Skip to content

Commit

Permalink
Merge pull request #183 from relic-toolkit/stackless
Browse files Browse the repository at this point in the history
Remove ALLOC = STACK to simplify memory allocation.
  • Loading branch information
dfaranha authored Feb 23, 2021
2 parents 2557793 + 631f8fd commit 1f366d6
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 406 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ env:
- CONFIG="-DSEED= -DBENCH=0" TEST=""
# Dynamic allocation
- CONFIG="-DALLOC=DYNAMIC -DSEED= -DBENCH=0" TEST=""
# Stack allocation
- CONFIG="-DALLOC=STACK -DSEED= -DBENCH=0" TEST=""
# Jacobian coordinates
- CONFIG="-DSEED= -DBENCH=0 -DEP_METHD='JACOB;LWNAF;COMBS;INTER'" TEST=""
# Build for Edwards curves
Expand Down
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ message(" ARITH=gmp-sec Same as above, but using constant-time code.\n")
message(STATUS "Available memory-allocation policies (default = AUTO):\n")

message(" ALLOC=AUTO All memory is automatically allocated.")
message(" ALLOC=STATIC All memory is allocated statically once.")
message(" ALLOC=DYNAMIC All memory is allocated dynamically on demand.")
message(" ALLOC=STACK All memory is allocated from the stack.\n")
message(" ALLOC=DYNAMIC All memory is allocated dynamically on demand.\n")

message(STATUS "Supported operating systems (default = LINUX):\n")

Expand Down
2 changes: 1 addition & 1 deletion bench/bench_fbx.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static void util2(void) {
BENCH_BEGIN("fb2_cmp") {
fb2_rand(a);
fb2_rand(b);
BENCH_ADD(fb2_cmp(b, a));
BENCH_ADD((void)fb2_cmp(b, a));
}
BENCH_END;

Expand Down
20 changes: 3 additions & 17 deletions include/relic_bn.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ typedef struct {
#if ALLOC == DYNAMIC
/** The sequence of contiguous digits that forms this integer. */
dig_t *dp;
#elif ALLOC == STACK || ALLOC == AUTO
#elif ALLOC == AUTO
/** The sequence of contiguous digits that forms this integer. */
rlc_align dig_t dp[RLC_BN_SIZE];
#endif
Expand All @@ -113,7 +113,7 @@ typedef struct {
*/
#if ALLOC == AUTO
typedef bn_st bn_t[1];
#else
#elif ALLOC == DYNAMIC
#ifdef CHECK
typedef bn_st *volatile bn_t;
#else
Expand All @@ -132,7 +132,7 @@ typedef bn_st *bn_t;
*/
#if ALLOC == AUTO
#define bn_null(A) /* empty */
#else
#elif ALLOC == DYNAMIC
#define bn_null(A) A = NULL;
#endif

Expand All @@ -154,11 +154,6 @@ typedef bn_st *bn_t;
#define bn_new(A) \
bn_init(A, RLC_BN_SIZE); \

#elif ALLOC == STACK
#define bn_new(A) \
A = (bn_t)alloca(sizeof(bn_st)); \
bn_init(A, RLC_BN_SIZE); \

#endif

/**
Expand All @@ -183,11 +178,6 @@ typedef bn_st *bn_t;
#define bn_new_size(A, D) \
bn_init(A, D); \

#elif ALLOC == STACK
#define bn_new_size(A, D) \
A = (bn_t)alloca(sizeof(bn_st)); \
bn_init(A, D); \

#endif

/**
Expand All @@ -206,10 +196,6 @@ typedef bn_st *bn_t;
#elif ALLOC == AUTO
#define bn_free(A) /* empty */ \

#elif ALLOC == STACK
#define bn_free(A) \
A = NULL; \

#endif

/**
Expand Down
2 changes: 0 additions & 2 deletions include/relic_conf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,6 @@
#define AUTO 1
/** Dynamic memory allocation. */
#define DYNAMIC 2
/** Stack memory allocation. */
#define STACK 3
/** Chosen memory allocation policy. */
#define ALLOC @ALLOC@

Expand Down
8 changes: 0 additions & 8 deletions include/relic_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,6 @@ typedef struct _ctx_t {
/** Array of pointers to the precomputation table. */
ep2_st *ep2_ptr[RLC_EP_TABLE];
#endif /* EP_PRECO */
#if ALLOC == STACK
/** In case of stack allocation, we need to get global memory for the table. */
fp2_st _ep2_pre[3 * RLC_EP_TABLE];
/** In case of stack allocation, storage for the EPX constants. */
ep2_st _ep2_g;
/* 3 for ep2_g, plus ep2_a, ep2_b, ep2_map_u, and ep2_map_c[4] */
fp2_st _ep2_storage[10];
#endif /* ALLOC == STACK */
#ifdef EP_CTMAP
/** The isogeny map coefficients for the SSWU mapping. */
iso2_st ep2_iso;
Expand Down
89 changes: 0 additions & 89 deletions include/relic_cp.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,6 @@ typedef bgn_st *bgn_t;
bn_new((A)->q); \
bn_new((A)->qi); \

#elif ALLOC == STACK
#define crt_new(A) \
A = (crt_t)alloca(sizeof(crt_st)); \
bn_new((A)->n); \
bn_new((A)->dp); \
bn_new((A)->dq); \
bn_new((A)->p); \
bn_new((A)->q); \
bn_new((A)->qi); \

#endif

/**
Expand All @@ -262,16 +252,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define crt_free(A) /* empty */

#elif ALLOC == STACK
#define crt_free(A) \
bn_free((A)->n); \
bn_free((A)->dp); \
bn_free((A)->dq); \
bn_free((A)->p); \
bn_free((A)->q); \
bn_free((A)->qi); \
A = NULL; \

#endif

/**
Expand Down Expand Up @@ -308,13 +288,6 @@ typedef bgn_st *bgn_t;
bn_new((A)->e); \
crt_new((A)->crt); \

#elif ALLOC == STACK
#define rsa_new(A) \
A = (rsa_t)alloca(sizeof(_rsa_st)); \
bn_new((A)->e); \
bn_new((A)->d); \
crt_new((A)->crt); \

#endif

/**
Expand All @@ -335,13 +308,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define rsa_free(A) /* empty */

#elif ALLOC == STACK
#define rsa_free(A) \
bn_free((A)->d); \
bn_free((A)->e); \
crt_free((A)->crt); \
A = NULL; \

#endif

/**
Expand Down Expand Up @@ -429,14 +395,6 @@ typedef bgn_st *bgn_t;
bn_new((A)->q); \
(A)->t = 0; \

#elif ALLOC == STACK
#define bdpe_new(A) \
A = (bdpe_t)alloca(sizeof(bdpe_st)); \
bn_new((A)->n); \
bn_new((A)->y); \
bn_new((A)->p); \
bn_new((A)->q); \

#endif

/**
Expand All @@ -459,15 +417,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define bdpe_free(A) /* empty */

#elif ALLOC == STACK
#define bdpe_free(A) \
bn_free((A)->n); \
bn_free((A)->y); \
bn_free((A)->p); \
bn_free((A)->q); \
(A)->t = 0; \
A = NULL; \

#endif

/**
Expand Down Expand Up @@ -498,12 +447,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define sokaka_new(A) /* empty */

#elif ALLOC == STACK
#define sokaka_new(A) \
A = (sokaka_t)alloca(sizeof(sokaka_st)); \
g1_new((A)->s1); \
g2_new((A)->s2); \

#endif

/**
Expand All @@ -523,12 +466,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define sokaka_free(A) /* empty */

#elif ALLOC == STACK
#define sokaka_free(A) \
g1_free((A)->s1); \
g2_free((A)->s2); \
A = NULL; \

#endif

/**
Expand Down Expand Up @@ -566,19 +503,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define bgn_new(A) /* empty */

#elif ALLOC == STACK
#define bgn_new(A) \
A = (bgn_t)alloca(sizeof(bgn_st)); \
bn_new((A)->x); \
bn_new((A)->y); \
bn_new((A)->z); \
g1_new((A)->gx); \
g1_new((A)->gy); \
g1_new((A)->gz); \
g2_new((A)->hx); \
g2_new((A)->hy); \
g2_new((A)->hz); \

#endif

/**
Expand All @@ -605,19 +529,6 @@ typedef bgn_st *bgn_t;
#elif ALLOC == AUTO
#define bgn_free(A) /* empty */

#elif ALLOC == STACK
#define bgn_free(A) \
bn_free((A)->x); \
bn_free((A)->y); \
bn_free((A)->z); \
g1_free((A)->gx); \
g1_free((A)->gy); \
g1_free((A)->gz); \
g2_free((A)->hx); \
g2_free((A)->hy); \
g2_free((A)->hz); \
A = NULL; \

#endif

/*============================================================================*/
Expand Down
7 changes: 0 additions & 7 deletions include/relic_dv.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ typedef dig_t *dv_t;
#define dv_new(A) dv_new_dynam(&(A), RLC_DV_DIGS)
#elif ALLOC == AUTO
#define dv_new(A) /* empty */
#elif ALLOC == STACK
#define dv_new(A) \
A = (dig_t *)alloca(RLC_DV_BYTES + RLC_PAD(RLC_DV_BYTES)); \
A = (dig_t *)RLC_ALIGN(A); \

#endif

/**
Expand All @@ -134,8 +129,6 @@ typedef dig_t *dv_t;
#define dv_free(A) dv_free_dynam(&(A))
#elif ALLOC == AUTO
#define dv_free(A) (void)A
#elif ALLOC == STACK
#define dv_free(A) (void)A
#endif

/*============================================================================*/
Expand Down
32 changes: 12 additions & 20 deletions include/relic_eb.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ typedef eb_st *eb_t;
* @param[out] A - the point to initialize.
*/
#if ALLOC == AUTO
#define eb_null(A) /* empty */
#define eb_null(A) /* empty */
#else
#define eb_null(A) A = NULL;
#define eb_null(A) A = NULL;
#endif

/**
Expand All @@ -176,11 +176,7 @@ typedef eb_st *eb_t;
} \

#elif ALLOC == AUTO
#define eb_new(A) /* empty */

#elif ALLOC == STACK
#define eb_new(A) \
A = (eb_t)alloca(sizeof(eb_st)); \
#define eb_new(A) /* empty */

#endif

Expand All @@ -197,11 +193,7 @@ typedef eb_st *eb_t;
} \

#elif ALLOC == AUTO
#define eb_free(A) /* empty */

#elif ALLOC == STACK
#define eb_free(A) \
A = NULL; \
#define eb_free(A) /* empty */

#endif

Expand Down Expand Up @@ -295,13 +287,13 @@ typedef eb_st *eb_t;
* @param[in] P - the point to multiply.
*/
#if EB_FIX == BASIC
#define eb_mul_pre(T, P) eb_mul_pre_basic(T, P)
#define eb_mul_pre(T, P) eb_mul_pre_basic(T, P)
#elif EB_FIX == COMBS
#define eb_mul_pre(T, P) eb_mul_pre_combs(T, P)
#define eb_mul_pre(T, P) eb_mul_pre_combs(T, P)
#elif EB_FIX == COMBD
#define eb_mul_pre(T, P) eb_mul_pre_combd(T, P)
#define eb_mul_pre(T, P) eb_mul_pre_combd(T, P)
#elif EB_FIX == LWNAF
#define eb_mul_pre(T, P) eb_mul_pre_lwnaf(T, P)
#define eb_mul_pre(T, P) eb_mul_pre_lwnaf(T, P)
#endif

/**
Expand All @@ -313,13 +305,13 @@ typedef eb_st *eb_t;
* @param[in] K - the integer.
*/
#if EB_FIX == BASIC
#define eb_mul_fix(R, T, K) eb_mul_fix_basic(R, T, K)
#define eb_mul_fix(R, T, K) eb_mul_fix_basic(R, T, K)
#elif EB_FIX == COMBS
#define eb_mul_fix(R, T, K) eb_mul_fix_combs(R, T, K)
#define eb_mul_fix(R, T, K) eb_mul_fix_combs(R, T, K)
#elif EB_FIX == COMBD
#define eb_mul_fix(R, T, K) eb_mul_fix_combd(R, T, K)
#define eb_mul_fix(R, T, K) eb_mul_fix_combd(R, T, K)
#elif EB_FIX == LWNAF
#define eb_mul_fix(R, T, K) eb_mul_fix_lwnaf(R, T, K)
#define eb_mul_fix(R, T, K) eb_mul_fix_lwnaf(R, T, K)
#endif

/**
Expand Down
Loading

0 comments on commit 1f366d6

Please sign in to comment.