CVE-2023-36326: Integer Overflow or Wraparound (CWE-190)
value: Integer Overflow vulnerability in RELIC before commit 34580d840469361ba9b5f001361cad659687b9ab, allows attackers to execute arbitrary code, cause a denial of service, and escalate privileges when calling realloc function in bn_grow function.
CWE description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
CVSS v3 base score: 9.8
Published: 2023-09-01T16:15Z
Repository: https://github.com/relic-toolkit/relic
Language: C
Files changed: bench/bench_bn.c, bench/bench_cp.c, bench/bench_epx.c, include/relic_bn.h, include/relic_cp.h, include/relic_eb.h, include/relic_ed.h, include/relic_ep.h, include/relic_epx.h, include/relic_fb.h, include/relic_fp.h, include/relic_fpx.h, include/relic_md.h, include/relic_rand.h, include/relic_util.h, src/bn/relic_bn_gcd.c, src/bn/relic_bn_mem.c, src/bn/relic_bn_mxp.c, src/bn/relic_bn_prime.c, src/bn/relic_bn_rec.c, src/bn/relic_bn_shift.c, src/bn/relic_bn_smb.c, src/bn/relic_bn_srt.c, src/bn/relic_bn_util.c, src/cp/relic_cp_bls.c, src/cp/relic_cp_cmlhs.c, src/cp/relic_cp_pbpsi.c, src/cp/relic_cp_rsa.c, src/cp/relic_cp_sokaka.c, src/cp/relic_cp_vbnn.c, src/eb/relic_eb_map.c, src/eb/relic_eb_mul.c, src/eb/relic_eb_mul_sim.c, src/eb/relic_eb_util.c, src/ed/relic_ed_map.c, src/ed/relic_ed_mul.c, src/ed/relic_ed_mul_fix.c, src/ed/relic_ed_mul_sim.c, src/ed/relic_ed_util.c, src/ep/relic_ep_map.c, src/ep/relic_ep_mul.c, src/ep/relic_ep_mul_fix.c, src/ep/relic_ep_mul_sim.c, src/ep/relic_ep_util.c, src/epx/relic_ep2_map.c, src/epx/relic_ep2_mul.c, src/epx/relic_ep2_mul_fix.c, src/epx/relic_ep2_mul_sim.c, src/epx/relic_ep2_util.c, src/epx/relic_ep4_map.c, src/epx/relic_ep4_mul.c, src/epx/relic_ep4_mul_fix.c, src/epx/relic_ep4_mul_sim.c, src/epx/relic_ep4_util.c, src/fb/relic_fb_exp.c, src/fb/relic_fb_util.c, src/fp/relic_fp_exp.c, src/fp/relic_fp_prime.c, src/fp/relic_fp_util.c, src/fpx/relic_fpx_cyc.c, src/fpx/relic_fpx_exp.c, src/fpx/relic_fpx_util.c, src/md/relic_md_blake2s.c, src/md/relic_md_hmac.c, src/md/relic_md_kdf.c, src/md/relic_md_mgf.c, src/md/relic_md_sha224.c, src/md/relic_md_sha256.c, src/md/relic_md_sha384.c, src/md/relic_md_sha512.c, src/pp/relic_pp_map_k12.c, src/pp/relic_pp_map_k24.c, src/pp/relic_pp_map_k48.c, src/pp/relic_pp_map_k54.c, src/pp/relic_pp_map_k8.c, src/rand/relic_rand_core.c, src/rand/relic_rand_hashd.c, src/relic_util.c, test/test_bn.c, test/test_core.c, test/test_pc.c, test/test_rand.c
Commit date: 2022-11-14 21:49:05 +0100
Commit message: Huge commit improving the API to use size_t instead of int.

Diff: diff --git a/bench/bench_bn.c b/bench/bench_bn.c
index 64c97703..e4a0f75b 100644
--- a/bench/bench_bn.c
+++ b/bench/bench_bn.c
@@ -282,11 +282,11 @@ static void util(void) {
 
 static void arith(void) {
 	bn_t a, b, c, d[3], e[3];
 	crt_t crt;
 	dig_t f;
-	int len;
+	size_t len;
 
 	bn_null(a);
 	bn_null(b);
 	bn_null(c);
 	crt_null(crt);
@@ -889,21 +889,21 @@ static void arith(void) {
 	}
 	BENCH_END;
 
 	BENCH_RUN("bn_rec_naf") {
 		int8_t naf[RLC_BN_BITS + 1];
-		int len;
+		size_t len;
 		bn_rand(a, RLC_POS, RLC_BN_BITS);
 		BENCH_ADD((len = RLC_BN_BITS + 1, bn_rec_naf(naf, &len, a, 4)));
 	}
 	BENCH_END;
 
 #if defined(WITH_EB) && defined(EB_KBLTZ) && (EB_MUL == LWNAF || EB_MUL == RWNAF || EB_FIX == LWNAF || EB_SIM == INTER || !defined(STRIP))
 	if (eb_param_set_any_kbltz() == RLC_OK) {
 		BENCH_RUN("bn_rec_tnaf") {
 			int8_t tnaf[RLC_FB_BITS + 8];
-			int len = RLC_BN_BITS + 1;
+			size_t len = RLC_BN_BITS + 1;
 			eb_curve_get_ord(b);
 			bn_rand_mod(a, b);
 			if (eb_curve_opt_a() == RLC_ZERO) {
 				BENCH_ADD((len = RLC_FB_BITS + 8, bn_rec_tnaf(tnaf, &len, a, -1, RLC_FB_BITS, 4)));
 			} else {
@@ -926,11 +926,11 @@ static void arith(void) {
 	}
 #endif
 
 	BENCH_RUN("bn_rec_reg") {
 		int8_t naf[RLC_BN_BITS + 1];
-		int len = RLC_BN_BITS + 1;
+		size_t len = RLC_BN_BITS + 1;
 		bn_rand(a, RLC_POS, RLC_BN_BITS);
 		BENCH_ADD((len = RLC_BN_BITS + 1, bn_rec_reg(naf, &len, a, RLC_BN_BITS, 4)));
 	}
 	BENCH_END;
 
diff --git a/bench/bench_cp.c b/bench/bench_cp.c
index d34d3108..de0d843d 100644
--- a/bench/bench_cp.c
+++ b/bench/bench_cp.c
@@ -139,32 +139,32 @@ static void rabin(void) {
 static void benaloh(void) {
 	bdpe_t pub, prv;
 	dig_t in, new;
 	uint8_t out[RLC_BN_BITS / 8 + 1];
 	size_t out_len;
+	dig_t prime = 0xFB;
 
 	bdpe_null(pub);
 	bdpe_null(prv);
 
 	bdpe_new(pub);
 	bdpe_new(prv);
 
-	BENCH_ONE("cp_bdpe_gen", cp_bdpe_gen(pub, prv, bn_get_prime(47),
-		RLC_BN_BITS), 1);
+	BENCH_ONE("cp_bdpe_gen", cp_bdpe_gen(pub, prv, prime, RLC_BN_BITS), 1);
 
 	BENCH_RUN("cp_bdpe_enc") {
 		out_len = RLC_BN_BITS / 8 + 1;
 		rand_bytes(out, 1);
-		in = out[0] % bn_get_prime(47);
+		in = out[0] % prime;
 		BENCH_ADD(cp_bdpe_enc(out, &out_len, in, pub));
 		cp_bdpe_dec(&new, out, out_len, prv);
 	} BENCH_END;
 
 	BENCH_RUN("cp_bdpe_dec") {
 		out_len = RLC_BN_BITS / 8 + 1;
 		rand_bytes(out, 1);
-		in = out[0] % bn_get_prime(47);
+		in = out[0] % prime;
 		cp_bdpe_enc(out, &out_len, in, pub);
 		BENCH_ADD(cp_bdpe_dec(&new, out, out_len, prv));
 	} BENCH_END;
 
 	bdpe_free(pub);
diff --git a/bench/bench_epx.c b/bench/bench_epx.c
index 923f7b3f..9df560f2 100644
--- a/bench/bench_epx.c
+++ b/bench/bench_epx.c
@@ -35,21 +35,21 @@
 #include "relic_bench.h"
 
 static void memory2(void) {
 	ep2_t a[BENCH];
 
-	BENCH_FEW("ep2_null", ep4_null(a[i]), 1);
+	BENCH_FEW("ep2_null", ep2_null(a[i]), 1);
 
-	BENCH_FEW("ep2_new", ep4_new(a[i]), 1);
+	BENCH_FEW("ep2_new", ep2_new(a[i]), 1);
 	for (int i = 0; i < BENCH; i++) {
 		ep2_free(a[i]);
 	}
 
 	for (int i = 0; i < BENCH; i++) {
 		ep2_new(a[i]);
 	}
-	BENCH_FEW("ep2_free", ep4_free(a[i]), 1);
+	BENCH_FEW("ep2_free", ep2_free(a[i]), 1);
 
 	(void)a;
 }
 
 static void util2(void) {
diff --git a/include/relic_bn.h b/include/relic_bn.h
index 1b5b3244..a68e8243 100644
--- a/include/relic_bn.h
+++ b/include/relic_bn.h
@@ -51,26 +51,26 @@
  * If the library is built with support for dynamic allocation, this constant
  * represents the size in bits of the memory block allocated each time a
  * multiple precision integer must grow. Otherwise, it represents the fixed
  * fixed precision.
  */
-#define RLC_BN_BITS 	((int)BN_PRECI)
+#define RLC_BN_BITS 	((size_t)BN_PRECI)
 
 /**
  * Size in digits of a block sufficient to store the required precision.
  */
-#define RLC_BN_DIGS		((int)RLC_CEIL(BN_PRECI, RLC_DIG))
+#define RLC_BN_DIGS		((size_t)RLC_CEIL(BN_PRECI, RLC_DIG))
 
 /**
  * Size in digits of a block sufficient to store a multiple precision integer.
  */
 #if BN_MAGNI == DOUBLE
-#define RLC_BN_SIZE		((int)(2 * RLC_BN_DIGS + 2))
+#define RLC_BN_SIZE		((size_t)(2 * RLC_BN_DIGS + 2))
 #elif BN_MAGNI == CARRY
-#define RLC_BN_SIZE		((int)(RLC_BN_DIGS + 1))
+#define RLC_BN_SIZE		((size_t)(RLC_BN_DIGS + 1))
 #elif BN_MAGNI == SINGLE
-#define RLC_BN_SIZE		((int)RLC_BN_DIGS)
+#define RLC_BN_SIZE		((size_t)RLC_BN_DIGS)
 #endif
 
 /**
  * Positive sign of a multiple precision integer.
  */
@@ -92,13 +92,13 @@
  * in little-endian format, that is, the least significant digits are
  * stored in the first positions of the vector.
  */
 typedef struct {
 	/** The number of digits allocated to this multiple precision integer. */
-	int alloc;
+	size_t alloc;
 	/** The number of digits actually used. */
-	int used;
+	size_t used;
 	/** The sign of this multiple precision integer. */
 	int sign;
 #if ALLOC == DYNAMIC
 	/** The sequence of contiguous digits that forms this integer. */
 	dig_t *dp;
@@ -455,11 +455,11 @@ typedef crt_st *crt_t;
  * @param[in] digits		- the required precision in digits.
  * @throw ERR_NO_MEMORY		- if there is no available memory.
  * @throw ERR_PRECISION		- if the required precision cannot be represented
  * 							by the library.
  */
-void bn_make(bn_t a, int digits);
+void bn_make(bn_t a, size_t digits);
 
 /**
  * Cleans a multiple precision integer.
  *
  * @param[out] a			- the multiple precision integer to free.
@@ -474,11 +474,11 @@ void bn_clean(bn_t a);
  * @param[in] digits		- the number of digits to expand.
  * @throw ERR_NO_MEMORY		- if there is no available memory.
  * @throw ERR_PRECISION		- if the required precision cannot be represented
  * 							by the library.
  */
-void bn_grow(bn_t a, int digits);
+void bn_grow(bn_t a, size_t digits);
 
 /**
  * Adjust the number of valid digits of a multiple precision integer.
  *
  * @param[out] a			- the multiple precision integer to adjust.
@@ -544,37 +544,37 @@ int bn_is_even(const bn_t a);
  * Returns the number of bits of a multiple precision integer.
  *
  * @param[in] a				- the multiple precision integer.
  * @return number of bits.
  */
-int bn_bits(const bn_t a);
+size_t bn_bits(const bn_t a);
 
 /**
  * Returns the bit stored in the given position on a multiple precision integer.
  *
  * @param[in] a				- the multiple precision integer.
  * @param[in] bit			- the bit position to read.
  * @return the bit value.
  */
-int bn_get_bit(const bn_t a, int bit);
+int bn_get_bit(const bn_t a, size_t bit);
 
 /**
  * Stores a bit in a given position on a multiple precision integer.
  *
  * @param[out] a			- the multiple precision integer.
  * @param[in] bit			- the bit position to store.
  * @param[in] value			- the bit value.
  */
-void bn_set_bit(bn_t a, int bit, int value);
+void bn_set_bit(bn_t a, size_t bit, int value);
 
 /**
  * Returns the Hamming weight of a multiple precision integer.
  *
  * @param[in] a				- the multiple precision integer.
  * @return the number of non-zero bits.
  */
-int bn_ham(const bn_t a);
+size_t bn_ham(const bn_t a);
 
 /**
  * Reads the first digit in a multiple precision integer.
  *
  * @param[out] digit		- the result.
@@ -597,20 +597,20 @@ void bn_set_dig(bn_t a, dig_t digit);
  * Assigns a multiple precision integer to 2^b.
  *
  * @param[out] a			- the result.
  * @param[in] b				- the power of 2 to assign.
  */
-void bn_set_2b(bn_t a, int b);
+void bn_set_2b(bn_t a, size_t b);
 
 /**
  * Assigns a random value to a multiple precision integer.
  *
  * @param[out] a			- the multiple precision integer to assign.
  * @param[in] sign			- the sign to be assigned (RLC_NEG or RLC_POS).
  * @param[in] bits			- the number of bits.
  */
-void bn_rand(bn_t a, int sign, int bits);
+void bn_rand(bn_t a, int sign, size_t bits);
 
 /**
  * Assigns a non-zero random value to a multiple precision integer with absolute
  * value smaller than a given modulus.
  *
@@ -633,11 +633,11 @@ void bn_print(const bn_t a);
  * @param[in] a				- the multiple precision integer.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  * @return the number of digits in the given radix.
  */
-int bn_size_str(const bn_t a, int radix);
+size_t bn_size_str(const bn_t a, unsigned int radix);
 
 /**
  * Reads a multiple precision integer from a string in a given radix. The radix
  * must be included in the interval [2, 64].
  *
@@ -645,11 +645,11 @@ int bn_size_str(const bn_t a, int radix);
  * @param[in] str			- the string.
  * @param[in] len			- the size of the string.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  */
-void bn_read_str(bn_t a, const char *str, int len, int radix);
+void bn_read_str(bn_t a, const char *str, size_t len, unsigned int radix);
 
 /**
  * Writes a multiple precision integer to a string in a given radix. The radix
  * must be included in the interval [2, 64].
  *
@@ -658,67 +658,67 @@ void bn_read_str(bn_t a, const char *str, int len, int radix);
  * @param[in] a				- the multiple integer to write.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  */
-void bn_write_str(char *str, int len, const bn_t a, int radix);
+void bn_write_str(char *str, size_t len, const bn_t a, unsigned radix);
 
 /**
  * Returns the number of bytes necessary to store a multiple precision integer.
  *
  * @param[in] a				- the multiple precision integer.
  * @return the number of bytes.
  */
-int bn_size_bin(const bn_t a);
+size_t bn_size_bin(const bn_t a);
 
 /**
  * Reads a positive multiple precision integer from a byte vector in big-endian
  * format.
  *
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  */
-void bn_read_bin(bn_t a, const uint8_t *bin, int len);
+void bn_read_bin(bn_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a positive multiple precision integer to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the multiple integer to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_write_bin(uint8_t *bin, int len, const bn_t a);
+void bn_write_bin(uint8_t *bin, size_t len, const bn_t a);
 
 /**
  * Returns the number of digits necessary to store a multiple precision integer.
  *
  * @param[in] a				- the multiple precision integer.
  * @return the number of digits.
  */
-int bn_size_raw(const bn_t a);
+size_t bn_size_raw(const bn_t a);
 
 /**
  * Reads a positive multiple precision integer from a digit vector.
  *
  * @param[out] a			- the result.
  * @param[in] raw			- the digit vector.
  * @param[in] len			- the size of the string.
  */
-void bn_read_raw(bn_t a, const dig_t *raw, int len);
+void bn_read_raw(bn_t a, const dig_t *raw, size_t len);
 
 /**
  * Writes a positive multiple precision integer to a byte vector.
  *
  * @param[out] raw			- the digit vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the multiple integer to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_write_raw(dig_t *raw, int len, const bn_t a);
+void bn_write_raw(dig_t *raw, size_t len, const bn_t a);
 
 /**
  * Returns the result of an unsigned comparison between two multiple precision
  * integers.
  *
@@ -868,21 +868,21 @@ void bn_hlv(bn_t c, const bn_t a);
  *
  * @param[out] c			- the result.
  * @param[in] a				- the multiple precision integer to shift.
  * @param[in] bits			- the number of bits to shift.
  */
-void bn_lsh(bn_t c, const bn_t a, int bits);
+void bn_lsh(bn_t c, const bn_t a, unsigned int bits);
 
 /**
  * Shifts a multiple precision number to the right. Computes
  * c = floor(a / 2^bits).
  *
  * @param[out] c			- the result.
  * @param[in] a				- the multiple precision integer to shift.
  * @param[in] bits			- the number of bits to shift.
  */
-void bn_rsh(bn_t c, const bn_t a, int bits);
+void bn_rsh(bn_t c, const bn_t a, unsigned int bits);
 
 /**
  * Divides a multiple precision integer by another multiple precision integer
  * without producing the positive remainder. Computes c = floor(a / b).
  *
@@ -1308,39 +1308,39 @@ int bn_is_prime_solov(const bn_t a);
  * Generates a probable prime number.
  *
  * @param[out] a			- the result.
  * @param[in] bits			- the length of the number in bits.
  */
-void bn_gen_prime_basic(bn_t a, int bits);
+void bn_gen_prime_basic(bn_t a, size_t bits);
 
 /**
  * Generates a probable prime number a with (a - 1)/2 also prime.
  *
  * @param[out] a			- the result.
  * @param[in] bits			- the length of the number in bits.
  */
-void bn_gen_prime_safep(bn_t a, int bits);
+void bn_gen_prime_safep(bn_t a, size_t bits);
 
 /**
  * Generates a probable prime number with (a - 1)/2, (a + 1)/2 and
  * ((a - 1)/2 - 1)/2 also prime.
  *
  * @param[out] a			- the result.
  * @param[in] bits			- the length of the number in bits.
  */
-void bn_gen_prime_stron(bn_t a, int bits);
+void bn_gen_prime_stron(bn_t a, size_t bits);
 
 /**
  * Generates a probable prime number b, with (b-1) divisible by a probable large
  * prime a.
  *
  * @param[out] a			- the prime factor of (b-1).
  * @param[out] b			- the prime result b.
  * @param[in] abits			- the length of the factor a in bits.
  * @param[in] bbits			- the length of the result in bits.
  */
-int bn_gen_prime_factor(bn_t a, bn_t b, int abits, int bbits);
+int bn_gen_prime_factor(bn_t a, bn_t b, size_t abits, size_t bbits);
 
 
 /**
  * Tries to factorize an integer using Pollard (p - 1) factoring algorithm.
  * The maximum length of the returned factor is 16 bits.
@@ -1368,11 +1368,11 @@ int bn_is_factor(bn_t c, const bn_t a);
  * @param[out] len			- the number of bytes written.
  * @param[in] k				- the integer to recode.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w);
+void bn_rec_win(uint8_t *win, size_t *len, const bn_t k, size_t w);
 
 /**
  * Recodes a positive integer in sliding window form. If a negative integer is
  * given instead, its absolute value is taken.
  *
@@ -1380,11 +1380,11 @@ void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w);
  * @param[out] len			- the number of bytes written.
  * @param[in] k				- the integer to recode.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w);
+void bn_rec_slw(uint8_t *win, size_t *len, const bn_t k, size_t w);
 
 /**
  * Recodes a positive integer in width-w Non-Adjacent Form. If a negative
  * integer is given instead, its absolute value is taken.
  *
@@ -1392,11 +1392,11 @@ void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w);
  * @param[out] len			- the number of bytes written.
  * @param[in] k				- the integer to recode.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w);
+void bn_rec_naf(int8_t *naf, size_t *len, const bn_t k, size_t w);
 
 /**
  * Recodes a positive integer in width-w \tau-NAF. If a negative integer is
  * given instead, its absolute value is taken.
  *
@@ -1406,11 +1406,12 @@ void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w);
  * @param[in] u				- the u curve parameter.
  * @param[in] m				- the extension degree of the binary field.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
+void bn_rec_tnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
+		size_t w);
 
 /**
  * Recodes a positive integer in regular fixed-length width-w \tau-NAF.
  * If a negative integer is given instead, its absolute value is taken.
  *
@@ -1420,11 +1421,12 @@ void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
  * @param[in] u				- the u curve parameter.
  * @param[in] m				- the extension degree of the binary field.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
+void bn_rec_rtnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
+		size_t w);
 
 /**
  * Write the constants needed for \tau-NAF recoding as a set of \alpha_u =
  * \beta_u + \gamma_u * \tau elements.
  *
@@ -1432,11 +1434,12 @@ void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
  * @param[out] beta		- the first coefficients of the constants.
  * @param[out] gama		- the second coefficients of the constants.
  * @param[in] u 		- the u curve parameter.
  * @param[in] w 		- the window size in bits.
  */
-void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w);
+void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u,
+		size_t w);
 
 /**
  * Computes the partial reduction k partmod d = r0 + r1 * t, where
  * d = (t^m - 1)/(t - 1).
  *
@@ -1444,11 +1447,11 @@ void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w);
  * @param[out] r1		- the second half of the result.
  * @param[in] k			- the number to reduce.
  * @param[in] u			- the u curve parameter.
  * @param[in] m			- the extension degree of the binary field.
  */
-void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m);
+void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, size_t m);
 
 /**
  * Recodes a positive integer in regular fixed-length width-w NAF. If a negative
  * integer is given instead, its absolute value is taken.
  *
@@ -1457,11 +1460,11 @@ void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m);
  * @param[in] k				- the integer to recode.
  * @param[in] n				- the length of the recoding.
  * @param[in] w				- the window size in bits.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w);
+void bn_rec_reg(int8_t *naf, size_t *len, const bn_t k, size_t n, size_t w);
 
 /**
  * Recodes of a pair of positive integers in Joint Sparse Form. If negative
  * integers are given instead, takes their absolute value.
  *
@@ -1469,11 +1472,11 @@ void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w);
  * @param[out] len			- the number of bytes written.
  * @param[in] k				- the first integer to recode.
  * @param[in] l				- the second integer to recode.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void bn_rec_jsf(int8_t *jsf, int *len, const bn_t k, const bn_t l);
+void bn_rec_jsf(int8_t *jsf, size_t *len, const bn_t k, const bn_t l);
 
 /**
  * Recodes a positive integer into two parts k0,k1 such that k = k0 + phi(k1),
  * where phi is an efficient curve endomorphism. If a negative integer is
  * given instead, its absolute value is taken.
diff --git a/include/relic_cp.h b/include/relic_cp.h
index 7872f19e..1200dbb9 100644
--- a/include/relic_cp.h
+++ b/include/relic_cp.h
@@ -738,11 +738,11 @@ typedef etrs_st *etrs_t;
  * @param[out] pub			- the public key.
  * @param[out] prv			- the private key.
  * @param[in] bits			- the key length in bits.
  * @return RLC_OK if no errors occurred, RLC_ERR otherwise.
  */
-int cp_rsa_gen(rsa_t pub, rsa_t prv, int bits);
+int cp_rsa_gen(rsa_t pub, rsa_t prv, size_t bits);
 
 /**
  * Encrypts using the RSA cryptosystem.
  *
  * @param[out] out			- the output buffer.
@@ -1538,11 +1538,11 @@ int cp_bls_gen(bn_t d, g2_t q);
  * @param[in] msg			- the message to sign.
  * @param[in] len			- the message length in bytes.
  * @param[in] d				- the private key.
  * @return RLC_OK if no errors occurred, RLC_ERR otherwise.
  */
-int cp_bls_sig(g1_t s, const uint8_t *msg, int len, const bn_t d);
+int cp_bls_sig(g1_t s, const uint8_t *msg, size_t len, const bn_t d);
 
 /**
  * Verifies a message signed with BLS protocol.
  *
  * @param[out] s			- the signature.
diff --git a/include/relic_eb.h b/include/relic_eb.h
index b43650f1..68e47eee 100644
--- a/include/relic_eb.h
+++ b/include/relic_eb.h
@@ -558,11 +558,11 @@ int eb_size_bin(const eb_t a, int pack);
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_VALID		- if the encoded point is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void eb_read_bin(eb_t a, const uint8_t *bin, int len);
+void eb_read_bin(eb_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a binary field element to a byte vector in big-endian format with
  * optional point compression.
  *
@@ -570,11 +570,11 @@ void eb_read_bin(eb_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the binary elliptic curve point to write.
  * @param[in] pack			- the flag to indicate point compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void eb_write_bin(uint8_t *bin, int len, const eb_t a, int pack);
+void eb_write_bin(uint8_t *bin, size_t len, const eb_t a, int pack);
 
 /**
  * Negates a binary elliptic curve point represented by affine coordinates.
  *
  * @param[out] r			- the result.
@@ -926,11 +926,11 @@ void eb_norm_sim(eb_t *r, const eb_t *t, int n);
  *
  * @param[out] p			- the result.
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  */
-void eb_map(eb_t p, const uint8_t *msg, int len);
+void eb_map(eb_t p, const uint8_t *msg, size_t len);
 
 /**
  * Compresses a point.
  *
  * @param[out] r			- the result.
diff --git a/include/relic_ed.h b/include/relic_ed.h
index 4d8cbc71..a3997b7d 100644
--- a/include/relic_ed.h
+++ b/include/relic_ed.h
@@ -480,11 +480,11 @@ void ed_norm_sim(ed_t *r, const ed_t *t, int n);
  *
  * @param[out] p			- the result.
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  */
-void ed_map(ed_t p, const uint8_t *msg, int len);
+void ed_map(ed_t p, const uint8_t *msg, size_t len);
 
 /**
  * Maps a byte array to a point in an Edwards elliptic curve using
  * an explicit domain separation tag.
  *
@@ -492,11 +492,12 @@ void ed_map(ed_t p, const uint8_t *msg, int len);
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  * @param[in] dst			- the domain separation tag.
  * @param[in] dst_len		- the domain separation tag length in bytes.
  */
-void ed_map_dst(ed_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
+void ed_map_dst(ed_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len);
 
 /**
  * Multiplies an Edwards elliptic curve point by an integer. Computes R = [k]P.
  *
  * @param[out] R			- the result.
@@ -838,11 +839,11 @@ int ed_size_bin(const ed_t a, int pack);
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_VALID		- if the encoded point is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ed_read_bin(ed_t a, const uint8_t *bin, int len);
+void ed_read_bin(ed_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes an Edwards elliptic curve point to a byte vector in big-endian format
  * with optional point compression.
  *
@@ -850,11 +851,11 @@ void ed_read_bin(ed_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the Edwards elliptic curve point to write.
  * @param[in] pack			- the flag to indicate point compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ed_write_bin(uint8_t *bin, int len, const ed_t a, int pack);
+void ed_write_bin(uint8_t *bin, size_t len, const ed_t a, int pack);
 
 /**
  * Multiplies an Edwards elliptic point by an integer using the binary method.
  *
  * @param[out] r			- the result.
diff --git a/include/relic_ep.h b/include/relic_ep.h
index 506288b2..0e833462 100644
--- a/include/relic_ep.h
+++ b/include/relic_ep.h
@@ -809,11 +809,11 @@ int ep_size_bin(const ep_t a, int pack);
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_VALID		- if the encoded point is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep_read_bin(ep_t a, const uint8_t *bin, int len);
+void ep_read_bin(ep_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a prime elliptic curve point to a byte vector in big-endian format
  * with optional point compression.
  *
@@ -821,11 +821,11 @@ void ep_read_bin(ep_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the prime elliptic curve point to write.
  * @param[in] pack			- the flag to indicate point compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep_write_bin(uint8_t *bin, int len, const ep_t a, int pack);
+void ep_write_bin(uint8_t *bin, size_t len, const ep_t a, int pack);
 
 /**
  * Negates a prime elliptic curve point.
  *
  * @param[out] r			- the result.
@@ -1213,20 +1213,20 @@ void ep_norm_sim(ep_t *r, const ep_t *t, int n);
   *
  * @param[out] p			- the result.
  * @param[in] uniform_bytes		- the array of uniform bytes to map.
  * @param[in] len			- the array length in bytes.
  */
-void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len);
+void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, size_t len);
 
 /**
  * Maps a byte array to a point in a prime elliptic curve.
  *
  * @param[out] p			- the result.
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  */
-void ep_map(ep_t p, const uint8_t *msg, int len);
+void ep_map(ep_t p, const uint8_t *msg, size_t len);
 
 /**
  * Maps a byte array to a point in a prime elliptic curve with specified
  * domain separation tag (aka personalization string).
  *
@@ -1234,12 +1234,12 @@ void ep_map(ep_t p, const uint8_t *msg, int len);
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  * @param[in] dst			- the domain separation tag.
  * @param[in] dst_len		- the domain separation tag length in bytes.
  */
-void ep_map_dst(ep_t p, const uint8_t *msg, int len, const uint8_t *dst,
-		int dst_len);
+void ep_map_dst(ep_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len);
 
 /**
  * Compresses a point.
  *
  * @param[out] r			- the result.
diff --git a/include/relic_epx.h b/include/relic_epx.h
index fb2c821c..2b17e50a 100644
--- a/include/relic_epx.h
+++ b/include/relic_epx.h
@@ -743,11 +743,11 @@ int ep2_size_bin(const ep2_t a, int pack);
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_VALID		- if the encoded point is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep2_read_bin(ep2_t a, const uint8_t *bin, int len);
+void ep2_read_bin(ep2_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a prime elliptic curve pointer over a quadratic extension to a byte
  * vector in big-endian format with optional point compression.
  *
@@ -755,11 +755,11 @@ void ep2_read_bin(ep2_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the prime elliptic curve point to write.
  * @param[in] pack			- the flag to indicate point compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep2_write_bin(uint8_t *bin, int len, const ep2_t a, int pack);
+void ep2_write_bin(uint8_t *bin, size_t len, const ep2_t a, int pack);
 
 /**
  * Negates a point represented in affine coordinates in an elliptic curve over
  * a quadratic extension.
  *
@@ -1058,11 +1058,12 @@ void ep2_mul_sim_trick(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, cons
  * @param[in] p				- the first point to multiply.
  * @param[in] k				- the first integer.
  * @param[in] q				- the second point to multiply.
  * @param[in] m				- the second integer,
  */
-void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, const bn_t m);
+void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
+			const bn_t m);
 
 /**
  * Multiplies and adds two prime elliptic curve points simultaneously using
  * Solinas' Joint Sparse Form.
  *
@@ -1070,22 +1071,23 @@ void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, cons
  * @param[in] p				- the first point to multiply.
  * @param[in] k				- the first integer.
  * @param[in] q				- the second point to multiply.
  * @param[in] m				- the second integer,
  */
-void ep2_mul_sim_joint(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, const bn_t m);
+void ep2_mul_sim_joint(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
+			const bn_t m);
 
 /**
  * Multiplies simultaneously elements from a prime elliptic curve.
  * Computes R = \Sum_i=0..n k_iP_i.
  *
  * @param[out] r			- the result.
  * @param[out] p			- the points to multiply.
  * @param[out] k			- the integer scalars.
  * @param[out] n			- the number of elements to multiply.
  */
-void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], int n);
+void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], size_t n);
 
 /**
  * Multiplies and adds the generator and a prime elliptic curve point
  * simultaneously. Computes R = [k]G + [l]Q.
  *
@@ -1103,11 +1105,11 @@ void ep2_mul_sim_gen(ep2_t r, const bn_t k, const ep2_t q, const bn_t m);
  * @param[out] r			- the result.
  * @param[in] p				- the points to multiply.
  * @param[in] k				- the small scalars.
  * @param[in] len			- the number of points to multiply.
  */
-void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], int len);
+void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], size_t len);
 
 /**
  * Converts a point to affine coordinates.
  *
  * @param[out] r			- the result.
@@ -1132,20 +1134,20 @@ void ep2_norm_sim(ep2_t *r, const ep2_t *t, int n);
   *
  * @param[out] p			- the result.
  * @param[in] uniform_bytes		- the array of uniform bytes to map.
  * @param[in] len			- the array length in bytes.
  */
-void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, int len);
+void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, size_t len);
 
 /**
  * Maps a byte array to a point in an elliptic curve over a quadratic extension.
  *
  * @param[out] p			- the result.
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  */
-void ep2_map(ep2_t p, const uint8_t *msg, int len);
+void ep2_map(ep2_t p, const uint8_t *msg, size_t len);
 
 /**
  * Maps a byte array to a point in an elliptic curve over a quadratic extension
  * using an explicit domain separation tag.
  *
@@ -1153,11 +1155,12 @@ void ep2_map(ep2_t p, const uint8_t *msg, int len);
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  * @param[in] dst			- the domain separatoin tag.
  * @param[in] dst_len		- the domain separation tag length in bytes.
  */
-void ep2_map_dst(ep2_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
+void ep2_map_dst(ep2_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len);
 
 /**
  * Computes a power of the Gailbraith-Lin-Scott homomorphism of a point
  * represented in affine coordinates on a twisted elliptic curve over a
  * quadratic exension. That is, Psi^i(P) = Twist(P)(Frob^i(unTwist(P)).
@@ -1383,11 +1386,11 @@ int ep4_size_bin(const ep4_t a, int pack);
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_VALID		- if the encoded point is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep4_read_bin(ep4_t a, const uint8_t *bin, int len);
+void ep4_read_bin(ep4_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a prime elliptic curve pointer over a quartic extension to a byte
  * vector in big-endian format with optional point compression.
  *
@@ -1395,11 +1398,11 @@ void ep4_read_bin(ep4_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the prime elliptic curve point to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is invalid.
  */
-void ep4_write_bin(uint8_t *bin, int len, const ep4_t a, int pack);
+void ep4_write_bin(uint8_t *bin, size_t len, const ep4_t a, int pack);
 
 /**
  * Negates a point represented in affine coordinates in an elliptic curve over
  * a quartic extension.
  *
@@ -1721,11 +1724,11 @@ void ep4_mul_sim_joint(ep4_t r, const ep4_t p, const bn_t k, const ep4_t q, cons
  * @param[out] r			- the result.
  * @param[in] p			- the points to multiply.
  * @param[in] k			- the integer scalars.
  * @param[in] n			- the number of elements to multiply.
  */
-void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], int n);
+void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], size_t n);
 
 /**
  * Multiplies and adds the generator and a prime elliptic curve point
  * simultaneously. Computes R = [k]G + [l]Q.
  *
@@ -1743,11 +1746,11 @@ void ep4_mul_sim_gen(ep4_t r, const bn_t k, const ep4_t q, const bn_t m);
  * @param[out] r			- the result.
  * @param[in] p				- the points to multiply.
  * @param[in] k				- the small scalars.
  * @param[in] len			- the number of points to multiply.
  */
-void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], int len);
+void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], size_t len);
 
 /**
  * Converts a point to affine coordinates.
  *
  * @param[out] r			- the result.
@@ -1769,11 +1772,11 @@ void ep4_norm_sim(ep4_t *r, const ep4_t *t, int n);
  *
  * @param[out] p			- the result.
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  */
-void ep4_map(ep4_t p, const uint8_t *msg, int len);
+void ep4_map(ep4_t p, const uint8_t *msg, size_t len);
 
 /**
  * Maps a byte array to a point in an elliptic curve over a quartic extension
  * using an explicit domain separation tag.
  *
@@ -1781,11 +1784,12 @@ void ep4_map(ep4_t p, const uint8_t *msg, int len);
  * @param[in] msg			- the byte array to map.
  * @param[in] len			- the array length in bytes.
  * @param[in] dst			- the domain separatoin tag.
  * @param[in] dst_len		- the domain separation tag length in bytes.
  */
-void ep4_map_dst(ep4_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
+void ep4_map_dst(ep4_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len);
 
 /**
  * Computes a power of the Gailbraith-Lin-Scott homomorphism of a point
  * represented in affine coordinates on a twisted elliptic curve over a
  * quartic exension. That is, Psi^i(P) = Twist(P)(Frob^i(unTwist(P)).
diff --git a/include/relic_fb.h b/include/relic_fb.h
index 48fe45b8..690d7d45 100644
--- a/include/relic_fb.h
+++ b/include/relic_fb.h
@@ -46,21 +46,21 @@
 /*============================================================================*/
 
 /**
  * Precision in bits of a binary field element.
  */
-#define RLC_FB_BITS 	((int)FB_POLYN)
+#define RLC_FB_BITS 	((size_t)FB_POLYN)
 
 /**
  * Size in digits of a block sufficient to store a binary field element.
  */
-#define RLC_FB_DIGS		((int)RLC_CEIL(RLC_FB_BITS, RLC_DIG))
+#define RLC_FB_DIGS		((size_t)RLC_CEIL(RLC_FB_BITS, RLC_DIG))
 
 /**
  * Size in bytes of a block sufficient to store a binary field element.
  */
-#define RLC_FB_BYTES 	((int)RLC_CEIL(RLC_FB_BITS, 8))
+#define RLC_FB_BYTES 	((size_t)RLC_CEIL(RLC_FB_BITS, 8))
 
 /**
  * Finite field identifiers.
  */
 enum {
@@ -536,20 +536,20 @@ int fb_is_zero(const fb_t a);
  *
  * @param[in] a				- the binary field element.
  * @param[in] bit			- the bit position.
  * @return the bit value.
  */
-int fb_get_bit(const fb_t a, int bit);
+int fb_get_bit(const fb_t a, size_t bit);
 
 /**
  * Stores a bit in a given position on a binary field element.
  *
  * @param[out] a			- the binary field element.
  * @param[in] bit			- the bit position.
  * @param[in] value			- the bit value.
  */
-void fb_set_bit(fb_t a, int bit, int value);
+void fb_set_bit(fb_t a, size_t bit, int value);
 
 /**
  * Assigns a small positive polynomial to a binary field element.
  *
  * The degree of the polynomial must be smaller than RLC_DIG.
@@ -563,11 +563,11 @@ void fb_set_dig(fb_t c, dig_t a);
  * Returns the number of bits of a binary field element.
  *
  * @param[in] a				- the binary field element.
  * @return the number of bits.
  */
-int fb_bits(const fb_t a);
+size_t fb_bits(const fb_t a);
 
 /**
  * Assigns a random value to a binary field element.
  *
  * @param[out] a			- the binary field element to assign.
@@ -588,11 +588,11 @@ void fb_print(const fb_t a);
  * @param[in] a				- the binary field element.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  * @return the number of digits in the given radix.
  */
-int fb_size_str(const fb_t a, int radix);
+size_t fb_size_str(const fb_t a, unsigned int radix);
 
 /**
  * Reads a binary field element from a string in a given radix. The radix must
  * be a power of 2 included in the interval [2, 64].
  *
@@ -601,11 +601,11 @@ int fb_size_str(const fb_t a, int radix);
  * @param[in] len			- the size of the string.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  * @throw ERR_NO_BUFFER		- if the string is too long.
  */
-void fb_read_str(fb_t a, const char *str, int len, int radix);
+void fb_read_str(fb_t a, const char *str, size_t len, unsigned int radix);
 
 /**
  * Writes a binary field element to a string in a given radix. The radix must
  * be a power of 2 included in the interval [2, 64].
  *
@@ -614,31 +614,31 @@ void fb_read_str(fb_t a, const char *str, int len, int radix);
  * @param[in] a				- the binary field element to write.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is insufficient.
  */
-void fb_write_str(char *str, int len, const fb_t a, int radix);
+void fb_write_str(char *str, size_t len, const fb_t a, unsigned int radix);
 
 /**
  * Reads a binary field element from a byte vector in big-endian format.
  *
  * @param[out] a			- the result.
- * @param[in] bin			- the byte vector.
+ * @param[in] bin			- the byte vector.,
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not RLC_FP_BYTES.
  */
-void fb_read_bin(fb_t a, const uint8_t *bin, int len);
+void fb_read_bin(fb_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a binary field element to a byte vector in big-endian format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the binary field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not RLC_FP_BYTES.
  */
-void fb_write_bin(uint8_t *bin, int len, const fb_t a);
+void fb_write_bin(uint8_t *bin, size_t len, const fb_t a);
 
 /**
  * Returns the result of a comparison between two binary field elements.
  *
  * @param[in] a				- the first binary field element.
diff --git a/include/relic_fp.h b/include/relic_fp.h
index 60994860..b78fe2e4 100644
--- a/include/relic_fp.h
+++ b/include/relic_fp.h
@@ -46,21 +46,21 @@
 /*============================================================================*/
 
 /**
  * Precision in bits of a prime field element.
  */
-#define RLC_FP_BITS 	((int)FP_PRIME)
+#define RLC_FP_BITS 	((size_t)FP_PRIME)
 
 /**
  * Size in digits of a block sufficient to store a prime field element.
  */
-#define RLC_FP_DIGS 	((int)RLC_CEIL(RLC_FP_BITS, RLC_DIG))
+#define RLC_FP_DIGS 	((size_t)RLC_CEIL(RLC_FP_BITS, RLC_DIG))
 
 /**
  * Size in bytes of a block sufficient to store a binary field element.
  */
-#define RLC_FP_BYTES 	((int)RLC_CEIL(RLC_FP_BITS, 8))
+#define RLC_FP_BYTES 	((size_t)RLC_CEIL(RLC_FP_BITS, 8))
 
 /*
  * Finite field identifiers.
  */
 enum {
@@ -499,14 +499,14 @@ void fp_prime_set_dense(const bn_t p);
  * Assigns the prime field modulus to a special form sparse prime.
  *
  * @param[in] spars		- the list of powers of 2 describing the prime.
  * @param[in] len		- the number of powers.
  */
-void fp_prime_set_pmers(const int *spars, int len);
+void fp_prime_set_pmers(const int *spars, size_t len);
 
 /**
-* Assigns the prime field modulus to a parametrization from a family of
+ * Assigns the prime field modulus to a parametrization from a family of
  * pairing-friendly curves.
  */
 void fp_prime_set_pairf(const bn_t x, int pairf);
 
 /**
@@ -651,20 +651,20 @@ int fp_is_even(const fp_t a);
  *
  * @param[in] a				- the prime field element.
  * @param[in] bit			- the bit position.
  * @return the bit value.
  */
-int fp_get_bit(const fp_t a, int bit);
+int fp_get_bit(const fp_t a, size_t bit);
 
 /**
  * Stores a bit in a given position on a prime field element.
  *
  * @param[out] a			- the prime field element.
  * @param[in] bit			- the bit position.
  * @param[in] value			- the bit value.
  */
-void fp_set_bit(fp_t a, int bit, int value);
+void fp_set_bit(fp_t a, size_t bit, int value);
 
 /**
  * Assigns a small positive constant to a prime field element.
  *
  * The constant must fit on a multiple precision digit, or dig_t type using
@@ -679,11 +679,11 @@ void fp_set_dig(fp_t c, dig_t a);
  * Returns the number of bits of a prime field element.
  *
  * @param[in] a				- the prime field element.
  * @return the number of bits.
  */
-int fp_bits(const fp_t a);
+size_t fp_bits(const fp_t a);
 
 /**
  * Assigns a random value to a prime field element.
  *
  * @param[out] a			- the prime field element to assign.
@@ -704,11 +704,11 @@ void fp_print(const fp_t a);
  * @param[in] a				- the prime field element.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  * @return the number of digits in the given radix.
  */
-int fp_size_str(const fp_t a, int radix);
+size_t fp_size_str(const fp_t a, unsigned int radix);
 
 /**
  * Reads a prime field element from a string in a given radix. The radix must
  * be a power of 2 included in the interval [2, 64].
  *
@@ -716,11 +716,11 @@ int fp_size_str(const fp_t a, int radix);
  * @param[in] str			- the string.
  * @param[in] len			- the size of the string.
  * @param[in] radix			- the radix.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  */
-void fp_read_str(fp_t a, const char *str, int len, int radix);
+void fp_read_str(fp_t a, const char *str, size_t len, unsigned int radix);
 
 /**
  * Writes a prime field element to a string in a given radix. The radix must
  * be a power of 2 included in the interval [2, 64].
  *
@@ -729,31 +729,31 @@ void fp_read_str(fp_t a, const char *str, int len, int radix);
  * @param[in] a				- the prime field element to write.
  * @param[in] radix			- the radix.
  * @throw ERR_BUFFER		- if the buffer capacity is insufficient.
  * @throw ERR_NO_VALID		- if the radix is invalid.
  */
-void fp_write_str(char *str, int len, const fp_t a, int radix);
+void fp_write_str(char *str, size_t len, const fp_t a, unsigned int radix);
 
 /**
  * Reads a prime field element from a byte vector in big-endian format.
  *
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not RLC_FP_BYTES.
  */
-void fp_read_bin(fp_t a, const uint8_t *bin, int len);
+void fp_read_bin(fp_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a prime field element to a byte vector in big-endian format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the prime field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not RLC_FP_BYTES.
  */
-void fp_write_bin(uint8_t *bin, int len, const fp_t a);
+void fp_write_bin(uint8_t *bin, size_t len, const fp_t a);
 
 /**
  * Returns the result of a comparison between two prime field elements.
  *
  * @param[in] a				- the first prime field element.
diff --git a/include/relic_fpx.h b/include/relic_fpx.h
index 8da84483..acdb6a7d 100644
--- a/include/relic_fpx.h
+++ b/include/relic_fpx.h
@@ -1330,11 +1330,11 @@ int fp2_size_bin(fp2_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp2_read_bin(fp2_t a, const uint8_t *bin, int len);
+void fp2_read_bin(fp2_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a quadratic extension field element to a byte vector in big-endian
  * format.
  *
@@ -1342,11 +1342,11 @@ void fp2_read_bin(fp2_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp2_write_bin(uint8_t *bin, int len, const fp2_t a, int pack);
+void fp2_write_bin(uint8_t *bin, size_t len, const fp2_t a, int pack);
 
 /**
  * Returns the result of a comparison between two quadratic extension field
  * elements.
  *
@@ -1688,22 +1688,22 @@ int fp3_size_bin(fp3_t a);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp3_read_bin(fp3_t a, const uint8_t *bin, int len);
+void fp3_read_bin(fp3_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a cubic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp3_write_bin(uint8_t *bin, int len, const fp3_t a);
+void fp3_write_bin(uint8_t *bin, size_t len, const fp3_t a);
 
 /**
  * Returns the result of a comparison between two cubic extension field
  * elements.
  *
@@ -1954,22 +1954,22 @@ int fp4_size_bin(fp4_t a);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp4_read_bin(fp4_t a, const uint8_t *bin, int len);
+void fp4_read_bin(fp4_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a quartic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp4_write_bin(uint8_t *bin, int len, const fp4_t a);
+void fp4_write_bin(uint8_t *bin, size_t len, const fp4_t a);
 
 /**
  * Returns the result of a comparison between two quartic extension field
  * elements.
  *
@@ -2224,22 +2224,22 @@ int fp6_size_bin(fp6_t a);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp6_read_bin(fp6_t a, const uint8_t *bin, int len);
+void fp6_read_bin(fp6_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a sextic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp6_write_bin(uint8_t *bin, int len, const fp6_t a);
+void fp6_write_bin(uint8_t *bin, size_t len, const fp6_t a);
 
 /**
  * Returns the result of a comparison between two sextic extension field
  * elements.
  *
@@ -2454,22 +2454,22 @@ int fp8_size_bin(fp8_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp8_read_bin(fp8_t a, const uint8_t *bin, int len);
+void fp8_read_bin(fp8_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes an octic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp8_write_bin(uint8_t *bin, int len, const fp8_t a);
+void fp8_write_bin(uint8_t *bin, size_t len, const fp8_t a);
 
 /**
  * Returns the result of a comparison between two octic extension field
  * elements.
  *
@@ -2737,22 +2737,22 @@ int fp9_size_bin(fp9_t a);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp9_read_bin(fp9_t a, const uint8_t *bin, int len);
+void fp9_read_bin(fp9_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a nonic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp9_write_bin(uint8_t *bin, int len, const fp9_t a);
+void fp9_write_bin(uint8_t *bin, size_t len, const fp9_t a);
 
 /**
  * Returns the result of a comparison between two nonic extension field
  * elements.
  *
@@ -2976,11 +2976,11 @@ int fp12_size_bin(fp12_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp12_read_bin(fp12_t a, const uint8_t *bin, int len);
+void fp12_read_bin(fp12_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a dodecic extension field element to a byte vector in big-endian
  * format.
  *
@@ -2988,11 +2988,11 @@ void fp12_read_bin(fp12_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp12_write_bin(uint8_t *bin, int len, const fp12_t a, int pack);
+void fp12_write_bin(uint8_t *bin, size_t len, const fp12_t a, int pack);
 
 /**
  * Returns the result of a comparison between two dodecic extension field
  * elements.
  *
@@ -3280,33 +3280,35 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b);
  * @param[in] a				- the first element to exponentiate.
  * @param[in] b				- the first exponent.
  * @param[in] c				- the second element to exponentiate.
  * @param[in] d				- the second exponent.
  */
-void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const bn_t d);
+void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c,
+		const bn_t d);
 
 /**
  * Computes a power of a cyclotomic dodecic extension field element.
  *
  * @param[out] e			- the result.
  * @param[in] a				- the first element to exponentiate.
  * @param[in] b				- the first exponent.
  * @param[in] c				- the second element to exponentiate.
  * @param[in] d				- the second exponent.
  */
-void fp12_exp_cyc_sim(fp12_t e, const fp12_t a, const bn_t b, const fp12_t c, const bn_t d);
+void fp12_exp_cyc_sim(fp12_t e, const fp12_t a, const bn_t b, const fp12_t c,
+		const bn_t d);
 
 /**
  * Computes a power of a cyclotomic dodecic extension field element.
  *
  * @param[out] c			- the result.
  * @param[in] a				- the basis.
  * @param[in] b				- the exponent in sparse form.
  * @param[in] l				- the length of the exponent in sparse form.
  * @param[in] s				- the sign of the exponent.
  */
-void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, int l, int s);
+void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, size_t l, int s);
 
 /**
  * Compresses a dodecic extension field element.
  *
  * @param[out] r			- the result.
@@ -3393,22 +3395,22 @@ int fp18_size_bin(fp18_t a);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp18_read_bin(fp18_t a, const uint8_t *bin, int len);
+void fp18_read_bin(fp18_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes an octdecic extension field element to a byte vector in big-endian
  * format.
  *
  * @param[out] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp18_write_bin(uint8_t *bin, int len, const fp18_t a);
+void fp18_write_bin(uint8_t *bin, size_t len, const fp18_t a);
 
 /**
  * Returns the result of a comparison between two octdecic extension field
  * elements.
  *
@@ -3655,11 +3657,11 @@ int fp24_size_bin(fp24_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp24_read_bin(fp24_t a, const uint8_t *bin, int len);
+void fp24_read_bin(fp24_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a 24-degree extension field element to a byte vector in big-endian
  * format.
  *
@@ -3667,11 +3669,11 @@ void fp24_read_bin(fp24_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp24_write_bin(uint8_t *bin, int len, const fp24_t a, int pack);
+void fp24_write_bin(uint8_t *bin, size_t len, const fp24_t a, int pack);
 
 /**
  * Returns the result of a comparison between two 24-degree extension field
  * elements.
  *
@@ -3959,11 +3961,11 @@ void fp24_exp_cyc_sim(fp24_t e, const fp24_t a, const bn_t b, const fp24_t c, co
  * @param[in] a				- the basis.
  * @param[in] b				- the exponent in sparse form.
  * @param[in] l				- the length of the exponent in sparse form.
  * @param[in] s				- the sign of the exponent.
  */
-void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, int l, int s);
+void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, size_t l, int s);
 
 /**
  * Compresses a 24-extension field element.
  *
  * @param[out] c			- the result.
@@ -4034,11 +4036,11 @@ int fp48_size_bin(fp48_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp48_read_bin(fp48_t a, const uint8_t *bin, int len);
+void fp48_read_bin(fp48_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a 48-extension field element to a byte vector in big-endian
  * format.
  *
@@ -4046,11 +4048,11 @@ void fp48_read_bin(fp48_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp48_write_bin(uint8_t *bin, int len, const fp48_t a, int pack);
+void fp48_write_bin(uint8_t *bin, size_t len, const fp48_t a, int pack);
 
 /**
  * Returns the result of a comparison between two 48-extension field
  * elements.
  *
@@ -4310,11 +4312,11 @@ void fp48_exp_cyc(fp48_t c, const fp48_t a, const bn_t b);
  * @param[in] a				- the basis.
  * @param[in] b				- the exponent in sparse form.
  * @param[in] l				- the length of the exponent in sparse form.
  * @param[in] s				- the sign of the exponent.
  */
-void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, int l, int s);
+void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, size_t l, int s);
 
 /**
  * Compresses a 48-extension field element.
  *
  * @param[out] c			- the result.
@@ -4385,11 +4387,11 @@ int fp54_size_bin(fp54_t a, int pack);
  * @param[out] a			- the result.
  * @param[in] bin			- the byte vector.
  * @param[in] len			- the buffer capacity.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp54_read_bin(fp54_t a, const uint8_t *bin, int len);
+void fp54_read_bin(fp54_t a, const uint8_t *bin, size_t len);
 
 /**
  * Writes a 54-extension field element to a byte vector in big-endian
  * format.
  *
@@ -4397,11 +4399,11 @@ void fp54_read_bin(fp54_t a, const uint8_t *bin, int len);
  * @param[in] len			- the buffer capacity.
  * @param[in] a				- the extension field element to write.
  * @param[in] pack			- the flag to indicate compression.
  * @throw ERR_NO_BUFFER		- if the buffer capacity is not correct.
  */
-void fp54_write_bin(uint8_t *bin, int len, const fp54_t a, int pack);
+void fp54_write_bin(uint8_t *bin, size_t len, const fp54_t a, int pack);
 
 /**
  * Returns the result of a comparison between two 54-extension field
  * elements.
  *
@@ -4661,11 +4663,11 @@ void fp54_exp_cyc(fp54_t c, const fp54_t a, const bn_t b);
  * @param[in] a				- the basis.
  * @param[in] b				- the exponent in sparse form.
  * @param[in] l				- the length of the exponent in sparse form.
  * @param[in] s				- the sign of the exponent.
  */
-void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, int l, int s);
+void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, size_t l, int s);
 
 /**
  * Compresses a 54-extension field element.
  *
  * @param[out] c			- the result.
diff --git a/include/relic_md.h b/include/relic_md.h
index a624b66c..5ec5d28b 100644
--- a/include/relic_md.h
+++ b/include/relic_md.h
@@ -132,90 +132,90 @@ enum {
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_sh224(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_sh224(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Computes the SHA-256 hash function.
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_sh256(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_sh256(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Computes the SHA-384 hash function.
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_sh384(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_sh384(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Computes the SHA-512 hash function.
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_sh512(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_sh512(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Computes the BLAKE2s-160 hash function.
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_b2s160(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_b2s160(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Computes the BLAKE2s-256 hash function.
  *
  * @param[out] hash				- the digest.
  * @param[in] msg				- the message to hash.
  * @param[in] len				- the message length in bytes.
  */
-void md_map_b2s256(uint8_t *hash, const uint8_t *msg, int len);
+void md_map_b2s256(uint8_t *hash, const uint8_t *msg, size_t len);
 
 /**
  * Derives a key from shared secret material through the standardized KDF2
  * function.
  *
  * @param[out] key				- the resulting key.
  * @param[in] key_len			- the intended key length in bytes.
  * @param[in] in				- the shared secret.
  * @param[in] in_len			- the length of the shared secret in bytes.
  */
-void md_kdf(uint8_t *key, int key_len, const uint8_t *in, int in_len);
+void md_kdf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len);
 
 /**
  * Derives a mask from shared secret material through the PKCS#1 2.1 MGF1
  * function. This is the same as the standardized KDF1 key derivation function.
  *
  * @param[out] key				- the resulting mask.
  * @param[in] key_len			- the intended mask length in bytes.
  * @param[in] in				- the shared secret.
  * @param[in] in_len			- the length of the shared secret in bytes.
  */
-void md_mgf(uint8_t *mask, int mask_len, const uint8_t *in, int in_len);
+void md_mgf(uint8_t *mask, size_t mask_len, const uint8_t *in, size_t in_len);
 
 /**
  * Computes a Message Authentication Code through HMAC.
  *
  * @param[out] mac				- the authentication.
  * @param[in] in				- the date to authenticate.
  * @param[in] in_len			- the number of bytes to authenticate.
  * @param[in] key				- the cryptographic key.
  * @param[in] key_len			- the size of the key in bytes.
  */
-void md_hmac(uint8_t *mac, const uint8_t *in, int in_len, const uint8_t *key,
-		int key_len);
+void md_hmac(uint8_t *mac, const uint8_t *in, size_t in_len, const uint8_t *key,
+		size_t key_len);
 
 /**
  * Map a byte vector and optional domain separation tag to an arbitrary-length
  * pseudorandom output using the SHA-224 hash function.
  *
@@ -224,12 +224,12 @@ void md_hmac(uint8_t *mac, const uint8_t *in, int in_len, const uint8_t *key,
  * @param[in] in					- the message to hash.
  * @param[in] in_len				- the message length in bytes.
  * @param[in] dst					- the domain separation tag.
  * @param[in] dst_len				- the domain separation tag length in bytes.
  */
-void md_xmd_sh224(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
-		const uint8_t *dst, int dst_len);
+void md_xmd_sh224(uint8_t *buf, size_t buf_len, const uint8_t *in,
+		size_t in_len, const uint8_t *dst, size_t dst_len);
 
 /**
  * Map a byte vector and optional domain separation tag to an arbitrary-length
  * pseudorandom output using the SHA-256 hash function.
  *
@@ -238,12 +238,12 @@ void md_xmd_sh224(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
  * @param[in] in					- the message to hash.
  * @param[in] in_len				- the message length in bytes.
  * @param[in] dst					- the domain separation tag.
  * @param[in] dst_len				- the domain separation tag length in bytes.
  */
-void md_xmd_sh256(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
-		const uint8_t *dst, int dst_len);
+void md_xmd_sh256(uint8_t *buf, size_t buf_len, const uint8_t *in,
+		size_t in_len, const uint8_t *dst, size_t dst_len);
 
 /**
  * Map a byte vector and optional domain separation tag to an arbitrary-length
  * pseudorandom output using the SHA-384 hash function.
  *
@@ -252,12 +252,12 @@ void md_xmd_sh256(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
  * @param[in] in					- the message to hash.
  * @param[in] in_len				- the message length in bytes.
  * @param[in] dst					- the domain separation tag.
  * @param[in] dst_len				- the domain separation tag length in bytes.
  */
-void md_xmd_sh384(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
-		const uint8_t *dst, int dst_len);
+void md_xmd_sh384(uint8_t *buf, size_t buf_len, const uint8_t *in,
+		size_t in_len, const uint8_t *dst, size_t dst_len);
 
 /**
  * Map a byte vector and optional domain separation tag to an arbitrary-length
  * pseudorandom output using the SHA-512 hash function.
  *
@@ -266,9 +266,9 @@ void md_xmd_sh384(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
  * @param[in] in					- the message to hash.
  * @param[in] in_len				- the message length in bytes.
  * @param[in] dst					- the domain separation tag.
  * @param[in] dst_len				- the domain separation tag length in bytes.
  */
-void md_xmd_sh512(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
-		const uint8_t *dst, int dst_len);
+void md_xmd_sh512(uint8_t *buf, size_t buf_len, const uint8_t *in,
+		size_t in_len, const uint8_t *dst, size_t dst_len);
 
 #endif /* !RLC_MD_H */
diff --git a/include/relic_rand.h b/include/relic_rand.h
index bf92ac37..7138ff9d 100644
--- a/include/relic_rand.h
+++ b/include/relic_rand.h
@@ -87,22 +87,22 @@ void rand_clean(void);
  *
  * @param[in] buf			- the buffer that represents the initial state.
  * @param[in] size			- the number of bytes.
  * @throw ERR_NO_VALID		- if the entropy length is too small or too large.
  */
-void rand_seed(uint8_t *buf, int size);
+void rand_seed(uint8_t *buf, size_t size);
 
 #else
 
 /**
  * Sets the initial state of the pseudo-random number generator as a function
  * pointer.
  *
  * @param[in] callback		- the callback to call.
  * @param[in] arg			- the argument for the callback.
  */
-void rand_seed(void (*callback)(uint8_t *, int, void *), void *arg);
+void rand_seed(void (*callback)(uint8_t *, size_t, void *), void *arg);
 
 #endif
 
 /**
  * Performs a basic self-test in the pseudo-random number generator output, and
@@ -110,19 +110,19 @@ void rand_seed(void (*callback)(uint8_t *, int, void *), void *arg);
  *
  * @param[out] buf			- the buffer to check.
  * @param[in] size			- the number of bytes to check.
  * @throw ERR_NO_RAND       - if the pseudo-random number generator is stuck.
  */
-int rand_check(uint8_t *buf, int size);
+int rand_check(uint8_t *buf, size_t size);
 
 /**
  * Gathers pseudo-random bytes from the pseudo-random number generator.
  *
  * @param[out] buf			- the buffer to write.
  * @param[in] size			- the number of bytes to gather.
  * @throw ERR_NO_VALID		- if the required length is too large.
  * @throw ERR_NO_READ		- it the pseudo-random number generator cannot
  * 							generate the specified number of bytes.
  */
-void rand_bytes(uint8_t *buf, int size);
+void rand_bytes(uint8_t *buf, size_t size);
 
 #endif /* !RLC_RAND_H */
diff --git a/include/relic_util.h b/include/relic_util.h
index 80698334..48d956eb 100644
--- a/include/relic_util.h
+++ b/include/relic_util.h
@@ -283,11 +283,11 @@ char util_conv_char(dig_t i);
  * Returns the highest bit set on a digit.
  *
  * @param[in] a				- the digit.
  * @return the position of the highest bit set.
  */
-int util_bits_dig(dig_t a);
+size_t util_bits_dig(dig_t a);
 
 /**
  * Compares two buffers in constant time.
  *
  * @param[in] a				- the first buffer.
diff --git a/src/bn/relic_bn_gcd.c b/src/bn/relic_bn_gcd.c
index bc49b118..8f499273 100644
--- a/src/bn/relic_bn_gcd.c
+++ b/src/bn/relic_bn_gcd.c
@@ -200,13 +200,18 @@ void bn_gcd_lehme(bn_t c, const bn_t a, const bn_t b) {
 		} else {
 			bn_abs(x, b);
 			bn_abs(y, a);
 		}
 		while (y->used > 1) {
-			bn_rsh(u, x, bn_bits(x) - RLC_DIG);
+			if (bn_bits(x) > RLC_DIG) {
+				bn_rsh(u, x, bn_bits(x) - RLC_DIG);
+				bn_rsh(v, y, bn_bits(x) - RLC_DIG);
+			} else {
+				bn_copy(u, x);
+				bn_copy(v, y);
+			}
 			_x = u->dp[0];
-			bn_rsh(v, y, bn_bits(x) - RLC_DIG);
 			_y = v->dp[0];
 			_a = _d = 1;
 			_b = _c = 0;
 			t = 0;
 			if (_y != 0) {
@@ -235,12 +240,17 @@ void bn_gcd_lehme(bn_t c, const bn_t a, const bn_t b) {
 			if (_b == 0) {
 				bn_mod(t0, x, y);
 				bn_copy(x, y);
 				bn_copy(y, t0);
 			} else {
-				bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
-				bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
+				if (bn_bits(x) > 2 * RLC_DIG) {
+					bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
+					bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
+				} else {
+					bn_copy(u, x);
+					bn_copy(v, y);
+				}
 				if (_a < 0) {
 					bn_mul_dig(t0, u, -_a);
 					bn_neg(t0, t0);
 				} else {
 					bn_mul_dig(t0, u, _a);
@@ -263,13 +273,18 @@ void bn_gcd_lehme(bn_t c, const bn_t a, const bn_t b) {
 				} else {
 					bn_mul_dig(t3, v, _d);
 				}
 				bn_add(u, t0, t1);
 				bn_add(v, t2, t3);
-				bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
+				if (bn_bits(u) > RLC_DIG) {
+					bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
+					bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
+				} else {
+					bn_copy(t0, u);
+					bn_copy(t1, v);
+				}
 				_x = t0->dp[0];
-				bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
 				_y = t1->dp[0];
 				t = 0;
 				if (_y != 0) {
 					q = _x / _y;
 					t = _x % _y;
@@ -398,13 +413,18 @@ void bn_gcd_ext_lehme(bn_t c, bn_t d, bn_t e, const bn_t a, const bn_t b) {
 
 		bn_zero(t4);
 		bn_set_dig(d, 1);
 
 		while (y->used > 1) {
-			bn_rsh(u, x, bn_bits(x) - RLC_DIG);
+			if (bn_bits(x) > RLC_DIG) {
+				bn_rsh(u, x, bn_bits(x) - RLC_DIG);
+				bn_rsh(v, y, bn_bits(x) - RLC_DIG);
+			} else {
+				bn_copy(u, x);
+				bn_copy(v, y);
+			}
 			_x = u->dp[0];
-			bn_rsh(v, y, bn_bits(x) - RLC_DIG);
 			_y = v->dp[0];
 			_a = _d = 1;
 			_b = _c = 0;
 			t = 0;
 			if (_y != 0) {
@@ -437,12 +457,17 @@ void bn_gcd_ext_lehme(bn_t c, bn_t d, bn_t e, const bn_t a, const bn_t b) {
 				bn_mul(t1, t1, d);
 				bn_sub(t1, t4, t1);
 				bn_copy(t4, d);
 				bn_copy(d, t1);
 			} else {
-				bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
-				bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
+				if (bn_bits(x) > 2 * RLC_DIG) {
+					bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
+					bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
+				} else {
+					bn_copy(u, x);
+					bn_copy(v, y);
+				}
 				if (_a < 0) {
 					bn_mul_dig(t0, u, -_a);
 					bn_neg(t0, t0);
 				} else {
 					bn_mul_dig(t0, u, _a);
@@ -465,13 +490,18 @@ void bn_gcd_ext_lehme(bn_t c, bn_t d, bn_t e, const bn_t a, const bn_t b) {
 				} else {
 					bn_mul_dig(t3, v, _d);
 				}
 				bn_add(u, t0, t1);
 				bn_add(v, t2, t3);
-				bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
+				if (bn_bits(u) > RLC_DIG) {
+					bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
+					bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
+				} else {
+					bn_copy(t0, u);
+					bn_copy(t1, v);
+				}
 				_x = t0->dp[0];
-				bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
 				_y = t1->dp[0];
 				t = 0;
 				if (_y != 0) {
 					q = _x / _y;
 					t = _x % _y;
diff --git a/src/bn/relic_bn_mem.c b/src/bn/relic_bn_mem.c
index f378432a..2d4fead7 100644
--- a/src/bn/relic_bn_mem.c
+++ b/src/bn/relic_bn_mem.c
@@ -39,11 +39,11 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void bn_make(bn_t a, int digits) {
+void bn_make(bn_t a, size_t digits) {
 	if (digits < 0) {
 		RLC_THROW(ERR_NO_VALID);
 	}
 	/* Allocate at least one digit. */
 	digits = RLC_MAX(digits, 1);
@@ -110,11 +110,11 @@ void bn_clean(bn_t a) {
 		a->used = 0;
 		a->sign = RLC_POS;
 	}
 }
 
-void bn_grow(bn_t a, int digits) {
+void bn_grow(bn_t a, size_t digits) {
 #if ALLOC == DYNAMIC
 	dig_t *t;
 
 	if (a->alloc < digits) {
 		/* At least add RLC_BN_SIZE more digits. */
@@ -141,11 +141,11 @@ void bn_trim(bn_t a) {
 	if (a->used <= a->alloc) {
 		while (a->used > 0 && a->dp[a->used - 1] == 0) {
 			--(a->used);
 		}
 		/* Zero can't be negative. */
-		if (a->used <= 0) {
+		if (a->used == 0) {
 			a->used = 1;
 			a->dp[0] = 0;
 			a->sign = RLC_POS;
 		}
 	}
diff --git a/src/bn/relic_bn_mxp.c b/src/bn/relic_bn_mxp.c
index 6fa87099..3f4c1abc 100644
--- a/src/bn/relic_bn_mxp.c
+++ b/src/bn/relic_bn_mxp.c
@@ -114,11 +114,11 @@ void bn_mxp_basic(bn_t c, const bn_t a, const bn_t b, const bn_t m) {
 
 #if BN_MXP == SLIDE || !defined(STRIP)
 
 void bn_mxp_slide(bn_t c, const bn_t a, const bn_t b, const bn_t m) {
 	bn_t tab[RLC_TABLE_SIZE], t, u, r;
-	int i, j, l, w = 1;
+	size_t l, w = 1;
 	uint8_t *win = RLC_ALLOCA(uint8_t, bn_bits(b));
 
 	if (win == NULL) {
 		RLC_THROW(ERR_NO_MEMORY);
 		return;
@@ -138,32 +138,32 @@ void bn_mxp_slide(bn_t c, const bn_t a, const bn_t b, const bn_t m) {
 
 	bn_null(t);
 	bn_null(u);
 	bn_null(r);
 	/* Initialize table. */
-	for (i = 0; i < RLC_TABLE_SIZE; i++) {
+	for (size_t i = 0; i < RLC_TABLE_SIZE; i++) {
 		bn_null(tab[i]);
 	}
 
 	/* Find window size. */
-	i = bn_bits(b);
-	if (i <= 21) {
+	l = bn_bits(b);
+	if (l <= 21) {
 		w = 2;
-	} else if (i <= 32) {
+	} else if (l <= 32) {
 		w = 3;
-	} else if (i <= 128) {
+	} else if (l <= 128) {
 		w = 4;
-	} else if (i <= 256) {
+	} else if (l <= 256) {
 		w = 5;
-	} else if (i <= 512) {
+	} else if (l <= 512) {
 		w = 6;
 	} else {
 		w = 7;
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (w - 1)); i++) {
+		for (size_t i = 0; i < (1 << (w - 1)); i++) {
 			bn_new(tab[i]);
 		}
 
 		bn_new(t);
 		bn_new(u);
@@ -181,23 +181,22 @@ void bn_mxp_slide(bn_t c, const bn_t a, const bn_t b, const bn_t m) {
 
 		bn_copy(tab[0], t);
 		bn_sqr(t, tab[0]);
 		bn_mod(t, t, m, u);
 		/* Create table. */
-		for (i = 1; i < 1 << (w - 1); i++) {
+		for (size_t i = 1; i < 1 << (w - 1); i++) {
 			bn_mul(tab[i], tab[i - 1], t);
 			bn_mod(tab[i], tab[i], m, u);
 		}
 
-		l = bn_bits(b);
 		bn_rec_slw(win, &l, b, w);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				bn_sqr(r, r);
 				bn_mod(r, r, m, u);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					bn_sqr(r, r);
 					bn_mod(r, r, m, u);
 				}
 				bn_mul(r, r, tab[win[i] >> 1]);
 				bn_mod(r, r, m, u);
@@ -216,11 +215,11 @@ void bn_mxp_slide(bn_t c, const bn_t a, const bn_t b, const bn_t m) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (w - 1)); i++) {
+		for (size_t i = 0; i < (1 << (w - 1)); i++) {
 			bn_free(tab[i]);
 		}
 		bn_free(u);
 		bn_free(t);
 		bn_free(r);
diff --git a/src/bn/relic_bn_prime.c b/src/bn/relic_bn_prime.c
index ed232457..b1aed2da 100644
--- a/src/bn/relic_bn_prime.c
+++ b/src/bn/relic_bn_prime.c
@@ -274,11 +274,11 @@ int bn_is_prime_rabin(const bn_t a) {
 
 		/* r = (n - 1)/2^s. */
 		bn_sub_dig(n1, a, 1);
 		bn_copy(r, n1);
 		s = 0;
-		while (bn_is_even(r)) {
+		while (!bn_is_zero(r) && bn_is_even(r)) {
 			s++;
 			bn_rsh(r, r, 1);
 		}
 
 		for (i = 0; i < tests; i++) {
@@ -399,11 +399,11 @@ int bn_is_prime_solov(const bn_t a) {
 	return result;
 }
 
 #if BN_GEN == BASIC || !defined(STRIP)
 
-void bn_gen_prime_basic(bn_t a, int bits) {
+void bn_gen_prime_basic(bn_t a, size_t bits) {
 	while (1) {
 		do {
 			bn_rand(a, RLC_POS, bits);
 		} while (bn_bits(a) != bits);
 		if (bn_is_prime(a)) {
@@ -414,11 +414,11 @@ void bn_gen_prime_basic(bn_t a, int bits) {
 
 #endif
 
 #if BN_GEN == SAFEP || !defined(STRIP)
 
-void bn_gen_prime_safep(bn_t a, int bits) {
+void bn_gen_prime_safep(bn_t a, size_t bits) {
 	while (1) {
 		do {
 			bn_rand(a, RLC_POS, bits);
 		} while (bn_bits(a) != bits);
 		/* Check if (a - 1)/2 is prime. */
@@ -438,11 +438,11 @@ void bn_gen_prime_safep(bn_t a, int bits) {
 
 #endif
 
 #if BN_GEN == STRON || !defined(STRIP)
 
-void bn_gen_prime_stron(bn_t a, int bits) {
+void bn_gen_prime_stron(bn_t a, size_t bits) {
 	dig_t i, j;
 	int found, k;
 	bn_t r, s, t;
 
 	bn_null(r);
@@ -517,11 +517,11 @@ void bn_gen_prime_stron(bn_t a, int bits) {
 	}
 }
 
 #endif
 
-int bn_gen_prime_factor(bn_t a, bn_t b, int abits, int bbits) {
+int bn_gen_prime_factor(bn_t a, bn_t b, size_t abits, size_t bbits) {
 	bn_t t;
 	int result = RLC_OK;
 
     if (! (bbits>abits) ) {
 		return RLC_ERR;
diff --git a/src/bn/relic_bn_rec.c b/src/bn/relic_bn_rec.c
index bdd29dab..9115b7f7 100644
--- a/src/bn/relic_bn_rec.c
+++ b/src/bn/relic_bn_rec.c
@@ -41,11 +41,11 @@
  * @param[in] a				- the multiple precision integer.
  * @param[in] from			- the first bit position.
  * @param[in] to			- the last bit position, inclusive.
  * @return the bits in the chosen positions.
  */
-static char get_bits(const bn_t a, int from, int to) {
+static char get_bits(const bn_t a, size_t from, size_t to) {
 	int f, t;
 	dig_t mf, mt;
 
 	RLC_RIP(from, f, from);
 	RLC_RIP(to, t, to);
@@ -89,11 +89,11 @@ static char get_bits(const bn_t a, int from, int to) {
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w) {
+void bn_rec_win(uint8_t *win, size_t *len, const bn_t k, size_t w) {
 	int i, j, l;
 
 	l = bn_bits(k);
 
 	if (*len < RLC_CEIL(l, w)) {
@@ -110,11 +110,11 @@ void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w) {
 	}
 	win[j++] = get_bits(k, i, bn_bits(k) - 1);
 	*len = j;
 }
 
-void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w) {
+void bn_rec_slw(uint8_t *win, size_t *len, const bn_t k, size_t w) {
 	int i, j, l, s;
 
 	l = bn_bits(k);
 
 	if (*len < l) {
@@ -141,11 +141,11 @@ void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w) {
 		}
 	}
 	*len = j;
 }
 
-void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w) {
+void bn_rec_naf(int8_t *naf, size_t *len, const bn_t k, size_t w) {
 	int i, l;
 	bn_t t;
 	dig_t t0, mask;
 	int8_t u_i;
 
@@ -215,11 +215,12 @@ void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w) {
+void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u,
+		size_t w) {
 	if (u == -1) {
 		switch (w) {
 			case 2:
 			case 3:
 				*t = 2;
@@ -347,11 +348,11 @@ void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w) {
 		gama[58] = gama[59] = gama[60] = (int8_t)(-7 * u);
 		gama[61] = gama[62] = gama[63] = (int8_t)(-7 * u);
 	}
 }
 
-void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m) {
+void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, size_t m) {
 	bn_t t, t0, t1, t2, t3;
 
 	bn_null(t);
 	bn_null(t0);
 	bn_null(t1);
@@ -419,11 +420,12 @@ void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m) {
 		bn_free(t2);
 		bn_free(t3);
 	}
 }
 
-void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w) {
+void bn_rec_tnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
+		size_t w) {
 	int i, l;
 	bn_t tmp, r0, r1;
 	int8_t beta[64], gama[64];
 	uint8_t t_w;
 	dig_t t0, t1, mask;
@@ -549,11 +551,12 @@ void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w) {
 		bn_free(r1);
 		bn_free(tmp);
 	}
 }
 
-void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w) {
+void bn_rec_rtnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
+		size_t w) {
 	int i, l;
 	bn_t tmp, r0, r1;
 	int8_t beta[64], gama[64];
 	uint8_t t_w;
 	dig_t t0, t1, mask;
@@ -691,11 +694,11 @@ void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w)
 		bn_free(r1);
 		bn_free(tmp);
 	}
 }
 
-void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w) {
+void bn_rec_reg(int8_t *naf, size_t *len, const bn_t k, size_t n, size_t w) {
 	int i, l;
 	bn_t t;
 	dig_t t0, mask;
 	int8_t u_i;
 
@@ -744,11 +747,11 @@ void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void bn_rec_jsf(int8_t *jsf, int *len, const bn_t k, const bn_t l) {
+void bn_rec_jsf(int8_t *jsf, size_t *len, const bn_t k, const bn_t l) {
 	bn_t n0, n1;
 	dig_t l0, l1;
 	int8_t u0, u1, d0, d1;
 	int i, j, offset;
 
@@ -826,11 +829,12 @@ void bn_rec_jsf(int8_t *jsf, int *len, const bn_t k, const bn_t l) {
 }
 
 void bn_rec_glv(bn_t k0, bn_t k1, const bn_t k, const bn_t n, const bn_t *v1,
 		const bn_t *v2) {
 	bn_t t, b1, b2;
-	int r1, r2, bits;
+	int r1, r2;
+	size_t bits;
 
 	bn_null(b1);
 	bn_null(b2);
 	bn_null(t);
 
diff --git a/src/bn/relic_bn_shift.c b/src/bn/relic_bn_shift.c
index bf010d9a..7dfe3df4 100644
--- a/src/bn/relic_bn_shift.c
+++ b/src/bn/relic_bn_shift.c
@@ -61,20 +61,16 @@ void bn_hlv(bn_t c, const bn_t a) {
 	bn_copy(c, a);
 	bn_rsh1_low(c->dp, c->dp, c->used);
 	bn_trim(c);
 }
 
-void bn_lsh(bn_t c, const bn_t a, int bits) {
+void bn_lsh(bn_t c, const bn_t a, unsigned int bits) {
 	int digits;
 	dig_t carry;
 
 	bn_copy(c, a);
 
-	if (bits <= 0) {
-		return;
-	}
-
 	RLC_RIP(bits, digits, bits);
 
 	RLC_TRY {
 		bn_grow(c, c->used + digits + (bits > 0));
 
@@ -99,25 +95,26 @@ void bn_lsh(bn_t c, const bn_t a, int bits) {
 	} RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 }
 
-void bn_rsh(bn_t c, const bn_t a, int bits) {
+void bn_rsh(bn_t c, const bn_t a, unsigned int bits) {
 	int digits = 0;
 
 	bn_copy(c, a);
 
-	if (bits <= 0) {
-		return;
-	}
-
 	RLC_RIP(bits, digits, bits);
 
 	if (digits > 0) {
 		dv_rshd(c->dp, a->dp, a->used, digits);
 	}
-	c->used = a->used - digits;
+
+	if (a->used > digits) {
+		c->used = a->used - digits;
+	} else {
+		c->used = 0;
+	}
 	c->sign = a->sign;
 
 	if (c->used > 0 && bits > 0) {
 		if (digits == 0 && c != a) {
 			bn_rshb_low(c->dp, a->dp + digits, a->used - digits, bits);
diff --git a/src/bn/relic_bn_smb.c b/src/bn/relic_bn_smb.c
index 6c297608..98f74dd8 100644
--- a/src/bn/relic_bn_smb.c
+++ b/src/bn/relic_bn_smb.c
@@ -119,11 +119,11 @@ int bn_smb_jac(const bn_t a, const bn_t b) {
 					break;
 				}
 			}
 			/* Write t0 as 2^h * t0. */
 			h = 0;
-			while (bn_is_even(t0)) {
+			while (bn_is_even(t0) && !bn_is_zero(t0)) {
 				h++;
 				bn_rsh(t0, t0, 1);
 			}
 			/* If h != 0 (mod 2) and n != +-1 (mod 8) then t = -t. */
 			bn_mod_2b(r, t1, 3);
diff --git a/src/bn/relic_bn_srt.c b/src/bn/relic_bn_srt.c
index ae347bf4..4cde73a5 100644
--- a/src/bn/relic_bn_srt.c
+++ b/src/bn/relic_bn_srt.c
@@ -36,11 +36,12 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 void bn_srt(bn_t c, bn_t a) {
 	bn_t h, l, m, t;
-	int bits, cmp;
+	size_t bits;
+	int cmp;
 
 	if (bn_sign(a) == RLC_NEG) {
 		RLC_THROW(ERR_NO_VALID);
 	}
 
@@ -56,12 +57,15 @@ void bn_srt(bn_t c, bn_t a) {
 		bn_new(h);
 		bn_new(l);
 		bn_new(m);
 		bn_new(t);
 
+		bn_zero(l);
 		bn_set_2b(h, bits >> 1);
-		bn_set_2b(l, (bits >> 1) - 1);
+		if (bits >= 2) {
+			bn_set_2b(l, (bits >> 1) - 1);
+		}
 
 		/* Trivial binary search approach. */
 		do {
 			bn_add(m, h, l);
 			bn_hlv(m, m);
diff --git a/src/bn/relic_bn_util.c b/src/bn/relic_bn_util.c
index 4020f57c..0a4e1e0c 100644
--- a/src/bn/relic_bn_util.c
+++ b/src/bn/relic_bn_util.c
@@ -103,11 +103,11 @@ int bn_is_even(const bn_t a) {
 		return 1;
 	}
 	return 0;
 }
 
-int bn_bits(const bn_t a) {
+size_t bn_bits(const bn_t a) {
 	int bits;
 
 	if (bn_is_zero(a)) {
 		return 0;
 	}
@@ -116,11 +116,11 @@ int bn_bits(const bn_t a) {
 	bits = (a->used - 1) * RLC_DIG;
 
 	return bits + util_bits_dig(a->dp[a->used - 1]);
 }
 
-int bn_get_bit(const bn_t a, int bit) {
+int bn_get_bit(const bn_t a, size_t bit) {
 	int d;
 
 	if (bit < 0) {
 		RLC_THROW(ERR_NO_VALID);
 		return 0;
@@ -137,11 +137,11 @@ int bn_get_bit(const bn_t a, int bit) {
 	} else {
 		return (a->dp[d] >> bit) & (dig_t)1;
 	}
 }
 
-void bn_set_bit(bn_t a, int bit, int value) {
+void bn_set_bit(bn_t a, size_t bit, int value) {
 	int d;
 
 	if (bit < 0) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
@@ -160,11 +160,11 @@ void bn_set_bit(bn_t a, int bit, int value) {
 		a->dp[d] &= ~((dig_t)1 << bit);
 		bn_trim(a);
 	}
 }
 
-int bn_ham(const bn_t a) {
+size_t bn_ham(const bn_t a) {
 	int c = 0;
 
 	for (int i = 0; i < bn_bits(a); i++) {
 		c += bn_get_bit(a, i);
 	}
@@ -181,15 +181,15 @@ void bn_set_dig(bn_t a, dig_t digit) {
 	a->dp[0] = digit;
 	a->used = 1;
 	a->sign = RLC_POS;
 }
 
-void bn_set_2b(bn_t a, int b) {
+void bn_set_2b(bn_t a, size_t b) {
 	int i, d;
 
-	if (b < 0) {
-		bn_zero(a);
+	if (b >= RLC_BN_SIZE * RLC_DIG) {
+		RLC_THROW(ERR_NO_VALID);
 	} else {
 		RLC_RIP(b, d, b);
 
 		bn_grow(a, d + 1);
 		for (i = 0; i < d; i++) {
@@ -199,11 +199,11 @@ void bn_set_2b(bn_t a, int b) {
 		a->dp[d] = ((dig_t)1 << b);
 		a->sign = RLC_POS;
 	}
 }
 
-void bn_rand(bn_t a, int sign, int bits) {
+void bn_rand(bn_t a, int sign, size_t bits) {
 	int digits;
 
 	RLC_RIP(bits, digits, bits);
 	digits += (bits > 0 ? 1 : 0);
 
@@ -262,11 +262,11 @@ void bn_print(const bn_t a) {
 #endif
 		util_print("\n");
 	}
 }
 
-int bn_size_str(const bn_t a, int radix) {
+size_t bn_size_str(const bn_t a, unsigned int radix) {
 	int digits = 0;
 	bn_t t;
 
 	bn_null(t);
 
@@ -306,11 +306,11 @@ int bn_size_str(const bn_t a, int radix) {
 	}
 
 	return digits + 1;
 }
 
-void bn_read_str(bn_t a, const char *str, int len, int radix) {
+void bn_read_str(bn_t a, const char *str, size_t len, unsigned int radix) {
 	int sign, i, j;
 	char c;
 
 	bn_zero(a);
 
@@ -355,11 +355,11 @@ void bn_read_str(bn_t a, const char *str, int len, int radix) {
 	} RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 }
 
-void bn_write_str(char *str, int len, const bn_t a, int radix) {
+void bn_write_str(char *str, size_t len, const bn_t a, unsigned int radix) {
 	bn_t t;
 	dig_t d;
 	int digits, l, i, j;
 	char c;
 
@@ -424,11 +424,11 @@ void bn_write_str(char *str, int len, const bn_t a, int radix) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-int bn_size_bin(const bn_t a) {
+size_t bn_size_bin(const bn_t a) {
 	dig_t d;
 	int digits;
 
 	digits = (a->used - 1) * (RLC_DIG / 8);
 	d = a->dp[a->used - 1];
@@ -438,11 +438,11 @@ int bn_size_bin(const bn_t a) {
 		digits++;
 	}
 	return digits;
 }
 
-void bn_read_bin(bn_t a, const uint8_t *bin, int len) {
+void bn_read_bin(bn_t a, const uint8_t *bin, size_t len) {
 	int i, j;
 	dig_t d = (RLC_DIG / 8);
 	int digs = (len % d == 0 ? len / d : len / d + 1);
 
 	bn_grow(a, digs);
@@ -468,11 +468,11 @@ void bn_read_bin(bn_t a, const uint8_t *bin, int len) {
 
 	a->sign = RLC_POS;
 	bn_trim(a);
 }
 
-void bn_write_bin(uint8_t *bin, int len, const bn_t a) {
+void bn_write_bin(uint8_t *bin, size_t len, const bn_t a) {
 	int size, k;
 	dig_t d;
 
 	size = bn_size_bin(a);
 
@@ -499,15 +499,15 @@ void bn_write_bin(uint8_t *bin, int len, const bn_t a) {
 	while (k < len) {
 		bin[len - 1 - k++] = 0;
 	}
 }
 
-int bn_size_raw(const bn_t a) {
+size_t bn_size_raw(const bn_t a) {
 	return a->used;
 }
 
-void bn_read_raw(bn_t a, const dig_t *raw, int len) {
+void bn_read_raw(bn_t a, const dig_t *raw, size_t len) {
 	RLC_TRY {
 		bn_grow(a, len);
 		a->used = len;
 		a->sign = RLC_POS;
 		dv_copy(a->dp, raw, len);
@@ -515,11 +515,11 @@ void bn_read_raw(bn_t a, const dig_t *raw, int len) {
 	} RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 }
 
-void bn_write_raw(dig_t *raw, int len, const bn_t a) {
+void bn_write_raw(dig_t *raw, size_t len, const bn_t a) {
 	int i, size;
 
 	size = a->used;
 
 	if (len < size) {
diff --git a/src/cp/relic_cp_bls.c b/src/cp/relic_cp_bls.c
index 1ac4de28..fac290e3 100644
--- a/src/cp/relic_cp_bls.c
+++ b/src/cp/relic_cp_bls.c
@@ -55,11 +55,11 @@ int cp_bls_gen(bn_t d, g2_t q) {
 		bn_free(n);
 	}
 	return result;
 }
 
-int cp_bls_sig(g1_t s, const uint8_t *msg, int len, const bn_t d) {
+int cp_bls_sig(g1_t s, const uint8_t *msg, size_t len, const bn_t d) {
 	g1_t p;
 	int result = RLC_OK;
 
 	g1_null(p);
 
diff --git a/src/cp/relic_cp_cmlhs.c b/src/cp/relic_cp_cmlhs.c
index e6d899f4..83b8c098 100644
--- a/src/cp/relic_cp_cmlhs.c
+++ b/src/cp/relic_cp_cmlhs.c
@@ -63,26 +63,30 @@ int cp_cmlhs_gen(bn_t x[], gt_t hs[], size_t len, uint8_t prf[], size_t plen,
 		g1_get_gen(g1);
 		g2_get_gen(g2);
 		pc_map(gt, g1, g2);
 
 		rand_bytes(prf, plen);
-		if (bls) {
-			cp_bls_gen(sk, pk);
-		} else {
-			cp_ecdsa_gen(sk, g1);
-			fp_copy(pk->x[0], g1->x);
-			fp_copy(pk->y[0], g1->y);
-		}
+
+		bn_rand_mod(d, n);
+		g2_mul_gen(y, d);
 
 		/* Generate elements for n tags. */
 		for (int i = 0; i < len; i++) {
 			bn_rand_mod(x[i], n);
 			gt_exp(hs[i], gt, x[i]);
 		}
 
-		bn_rand_mod(d, n);
-		g2_mul_gen(y, d);
+		if (bls) {
+			result = cp_bls_gen(sk, pk);
+		} else {
+			if (cp_ecdsa_gen(sk, g1) == RLC_OK) {
+				fp_copy(pk->x[0], g1->x);
+				fp_copy(pk->y[0], g1->y);
+			} else {
+				result = RLC_ERR;
+			}
+		}
 	}
 	RLC_CATCH_ANY {
 		result = RLC_ERR;
 	}
 	RLC_FINALLY {
@@ -99,12 +103,13 @@ int cp_cmlhs_sig(g1_t sig, g2_t z, g1_t a, g1_t c, g1_t r, g2_t s,
 		const uint8_t prf[], size_t plen, const bn_t d, const bn_t sk,
 		int bls) {
 	bn_t k, m, n;
 	g1_t t;
 	uint8_t mac[RLC_MD_LEN];
-	int len, dlen = strlen(data), result = RLC_OK;
+	size_t len, dlen = strlen(data);
 	uint8_t *buf = RLC_ALLOCA(uint8_t, 1 + 8 * RLC_PC_BYTES + dlen);
+	int result = RLC_OK;
 
 	bn_null(k);
 	bn_null(m);
 	bn_null(n);
 	g1_null(t);
@@ -209,12 +214,13 @@ int cp_cmlhs_ver(const g1_t r, const g2_t s, const g1_t sig[], const g2_t z[],
 		int bls) {
 	g1_t g1;
 	g2_t g2;
 	gt_t e, u, v;
 	bn_t k, n;
-	int len, dlen = strlen(data), result = 1;
+	size_t len, dlen = strlen(data);
 	uint8_t *buf = RLC_ALLOCA(uint8_t, 1 + 8 * RLC_PC_BYTES + dlen);
+	int result = 1;
 
 	g1_null(g1);
 	g2_null(g2);
 	gt_null(e);
 	gt_null(u);
@@ -329,12 +335,13 @@ int cp_cmlhs_onv(const g1_t r, const g2_t s, const g1_t sig[], const g2_t z[],
 		size_t slen, int bls) {
 	g1_t g1;
 	g2_t g2;
 	gt_t e, u, v;
 	bn_t k, n;
-	int len, dlen = strlen(data), result = 1;
+	size_t len, dlen = strlen(data);
 	uint8_t *buf = RLC_ALLOCA(uint8_t, 1 + 8 * RLC_FP_BYTES + dlen);
+	int result = 1;
 
 	g1_null(g1);
 	g2_null(g2);
 	gt_null(e);
 	gt_null(u);
diff --git a/src/cp/relic_cp_pbpsi.c b/src/cp/relic_cp_pbpsi.c
index acc6fafe..fcd32fa2 100644
--- a/src/cp/relic_cp_pbpsi.c
+++ b/src/cp/relic_cp_pbpsi.c
@@ -112,10 +112,11 @@ int cp_pbpsi_ask(g2_t d[], bn_t r, const bn_t x[], const g2_t s[], size_t m) {
 		for (i = 0; i <= m; i++) {
 			bn_free(p[i]);
 			bn_free(_x[i]);
 		}
 		RLC_FREE(p);
+		RLC_FREE(_x);
 	}
 	return result;
 }
 
 int cp_pbpsi_ans(gt_t t[], g1_t u[], const g1_t ss, const g2_t d,
diff --git a/src/cp/relic_cp_rsa.c b/src/cp/relic_cp_rsa.c
index 7121f080..eb5565a0 100644
--- a/src/cp/relic_cp_rsa.c
+++ b/src/cp/relic_cp_rsa.c
@@ -242,21 +242,22 @@ static uint8_t *hash_id(int md, int *len) {
  * @param[in] m_len		- the message length in bytes.
  * @param[in] k_len		- the key length in bytes.
  * @param[in] operation	- flag to indicate the operation type.
  * @return RLC_ERR if errors occurred, RLC_OK otherwise.
  */
-static int pad_pkcs1(bn_t m, int *p_len, int m_len, int k_len, int operation) {
+static int pad_pkcs1(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) {
 	uint8_t *id, pad = 0;
-	int len, result = RLC_ERR;
+	size_t len;
+	int result = RLC_ERR;
 	bn_t t;
 
 	bn_null(t);
 
 	RLC_TRY {
 		bn_new(t);
 
-		switch (operation) {
+		switch (op) {
 			case RSA_ENC:
 				/* EB = 00 | 02 | PS | 00 | D. */
 				bn_zero(m);
 				bn_lsh(m, m, 8);
 				bn_add_dig(m, m, RSA_PUB);
@@ -417,22 +418,22 @@ static int pad_pkcs1(bn_t m, int *p_len, int m_len, int k_len, int operation) {
  * @param[in] m_len		- the message length in bytes.
  * @param[in] k_len		- the key length in bytes.
  * @param[in] operation	- flag to indicate the operation type.
  * @return RLC_ERR if errors occurred, RLC_OK otherwise.
  */
-static int pad_pkcs2(bn_t m, int *p_len, int m_len, int k_len, int operation) {
+static int pad_pkcs2(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) {
 	uint8_t pad, h1[RLC_MD_LEN], h2[RLC_MD_LEN];
 	uint8_t *mask = RLC_ALLOCA(uint8_t, k_len);
 	int result = RLC_ERR;
 	bn_t t;
 
 	bn_null(t);
 
 	RLC_TRY {
 		bn_new(t);
 
-		switch (operation) {
+		switch (op) {
 			case RSA_ENC:
 				/* DB = lHash | PS | 01 | D. */
 				md_map(h1, NULL, 0);
 				bn_read_bin(m, h1, RLC_MD_LEN);
 				*p_len = k_len - 2 * RLC_MD_LEN - 2 - m_len;
@@ -576,11 +577,11 @@ static int pad_pkcs2(bn_t m, int *p_len, int m_len, int k_len, int operation) {
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-int cp_rsa_gen(rsa_t pub, rsa_t prv, int bits) {
+int cp_rsa_gen(rsa_t pub, rsa_t prv, size_t bits) {
 	bn_t t, r;
 	int result = RLC_OK;
 
 	if (pub == NULL || prv == NULL || bits == 0) {
 		return RLC_ERR;
diff --git a/src/cp/relic_cp_sokaka.c b/src/cp/relic_cp_sokaka.c
index 54ef3edb..5902f2cb 100644
--- a/src/cp/relic_cp_sokaka.c
+++ b/src/cp/relic_cp_sokaka.c
@@ -70,12 +70,12 @@ int cp_sokaka_gen_prv(sokaka_t k, const char *id, bn_t master) {
 	return RLC_OK;
 }
 
 int cp_sokaka_key(uint8_t *key, size_t key_len, const char *id1,
 		const sokaka_t k, const char *id2) {
-	int len1 = strlen(id1), len2 = strlen(id2);
-	int size, first = 0, result = RLC_OK;
+	size_t size, len1 = strlen(id1), len2 = strlen(id2);
+	int first = 0, result = RLC_OK;
 	uint8_t *buf;
 	g1_t p;
 	g2_t q;
 	gt_t e;
 
diff --git a/src/cp/relic_cp_vbnn.c b/src/cp/relic_cp_vbnn.c
index 7734da6f..bc99aa64 100644
--- a/src/cp/relic_cp_vbnn.c
+++ b/src/cp/relic_cp_vbnn.c
@@ -71,13 +71,14 @@ int cp_vbnn_gen(bn_t msk, ec_t mpk) {
 }
 
 int cp_vbnn_gen_prv(bn_t sk, ec_t pk, const bn_t msk, const uint8_t *id,
 		size_t id_len) {
 	uint8_t hash[RLC_MD_LEN];
-	int len, result = RLC_OK;
+	size_t len;
 	uint8_t *buf = NULL;
 	bn_t n, r;
+	int result = RLC_OK;
 
 	/* zero variables */
 	bn_null(n);
 	bn_null(r);
 
@@ -123,14 +124,15 @@ int cp_vbnn_gen_prv(bn_t sk, ec_t pk, const bn_t msk, const uint8_t *id,
 	return result;
 }
 
 int cp_vbnn_sig(ec_t r, bn_t z, bn_t h, const uint8_t *id, size_t id_len,
 		const uint8_t *msg, int msg_len, const bn_t sk, const ec_t pk) {
-	int len, result = RLC_OK;
+	size_t len;
 	uint8_t *buf = NULL, *buf_i, hash[RLC_MD_LEN];
 	bn_t n, y;
 	ec_t t;
+	int result = RLC_OK;
 
 	/* zero variables */
 	bn_null(n);
 	bn_null(y);
 	ec_null(t);
@@ -187,15 +189,15 @@ int cp_vbnn_sig(ec_t r, bn_t z, bn_t h, const uint8_t *id, size_t id_len,
 	return result;
 }
 
 int cp_vbnn_ver(const ec_t r, const bn_t z, const bn_t h, const uint8_t *id,
 		size_t id_len, const uint8_t *msg, int msg_len, const ec_t mpk) {
-	int len, result = 0;
+	size_t len;
 	uint8_t *buf = NULL, *buf_i, hash[RLC_MD_LEN];
 	bn_t n, c, _h;
-	ec_t Z;
-	ec_t t;
+	ec_t Z, t;
+	int result = 0;
 
 	/* zero variables */
 	bn_null(n);
 	bn_null(c);
 	bn_null(_h);
diff --git a/src/eb/relic_eb_map.c b/src/eb/relic_eb_map.c
index 73116a78..69b8a749 100644
--- a/src/eb/relic_eb_map.c
+++ b/src/eb/relic_eb_map.c
@@ -35,11 +35,11 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void eb_map(eb_t p, const uint8_t *msg, int len) {
+void eb_map(eb_t p, const uint8_t *msg, size_t len) {
 	bn_t k;
 	fb_t t0, t1;
 	int i;
 	uint8_t digest[RLC_MD_LEN];
 
diff --git a/src/eb/relic_eb_mul.c b/src/eb/relic_eb_mul.c
index cd3cdb02..378765f5 100644
--- a/src/eb/relic_eb_mul.c
+++ b/src/eb/relic_eb_mul.c
@@ -47,13 +47,14 @@
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void eb_mul_ltnaf_imp(eb_t r, const eb_t p, const bn_t k) {
-	int i, l, n;
+	int i, n;
 	int8_t tnaf[RLC_FB_BITS + 8], u;
 	eb_t t[1 << (EB_WIDTH - 2)];
+	size_t l;
 
 	if (eb_curve_opt_a() == RLC_ZERO) {
 		u = -1;
 	} else {
 		u = 1;
@@ -118,13 +119,14 @@ static void eb_mul_ltnaf_imp(eb_t r, const eb_t p, const bn_t k) {
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void eb_mul_lnaf_imp(eb_t r, const eb_t p, const bn_t k) {
-	int i, l, n;
+	int i, n;
 	int8_t naf[RLC_FB_BITS + 1];
 	eb_t t[1 << (EB_WIDTH - 2)];
+	size_t l;
 
 	RLC_TRY {
 		/* Prepare the precomputation table. */
 		for (i = 0; i < (1 << (EB_WIDTH - 2)); i++) {
 			eb_null(t[i]);
@@ -188,13 +190,14 @@ static void eb_mul_lnaf_imp(eb_t r, const eb_t p, const bn_t k) {
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void eb_mul_rtnaf_imp(eb_t r, const eb_t p, const bn_t k) {
-	int i, l, n;
+	int i, n;
 	int8_t tnaf[RLC_FB_BITS + 8], u;
 	eb_t t[1 << (EB_WIDTH - 2)];
+	size_t l;
 
 	if (eb_curve_opt_a() == RLC_ZERO) {
 		u = -1;
 	} else {
 		u = 1;
@@ -487,13 +490,14 @@ static void eb_mul_rtnaf_imp(eb_t r, const eb_t p, const bn_t k) {
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void eb_mul_rnaf_imp(eb_t r, const eb_t p, const bn_t k) {
-	int i, l, n;
+	int i, n;
 	int8_t naf[RLC_FB_BITS + 1];
 	eb_t t[1 << (EB_WIDTH - 2)];
+	size_t l;
 
 	RLC_TRY {
 		/* Prepare the accumulator table. */
 		for (i = 0; i < (1 << (EB_WIDTH - 2)); i++) {
 			eb_null(t[i]);
@@ -648,14 +652,14 @@ void eb_mul_basic(eb_t r, const eb_t p, const bn_t k) {
 #endif
 
 #if EB_MUL == LODAH || !defined(STRIP)
 
 void eb_mul_lodah(eb_t r, const eb_t p, const bn_t k) {
-	int bits, i, j;
 	dv_t x1, z1, x2, z2, r1, r2, r3, r4, r5;
 	const dig_t *b;
 	bn_t t, n;
+	size_t bits;
 
 	if (bn_is_zero(k)) {
 		eb_set_infty(r);
 		return;
 	}
@@ -719,12 +723,12 @@ void eb_mul_lodah(eb_t r, const eb_t p, const bn_t k) {
 		fb_mul(x1, z1, p->x);
 		fb_rand(r1);
 		fb_mul(z2, z2, r1);
 		fb_mul(x2, x2, r1);
 
-		for (i = bits - 1; i >= 0; i--) {
-			j = bn_get_bit(t, i);
+		for (int i = bits - 1; i >= 0; i--) {
+			int j = bn_get_bit(t, i);
 			fb_mul(r1, x1, z2);
 			fb_mul(r2, x2, z1);
 			fb_add(r3, r1, r2);
 			fb_muln_low(r4, r1, r2);
 			dv_swap_cond(x1, x2, RLC_FB_DIGS, j ^ 1);
@@ -887,15 +891,16 @@ void eb_mul_rwnaf(eb_t r, const eb_t p, const bn_t k) {
 #endif
 
 #if EB_MUL == HALVE || !defined(STRIP)
 
 void eb_mul_halve(eb_t r, const eb_t p, const bn_t k) {
-	int i, j, l, trc, cof;
+	int i, j, trc, cof;
 	int8_t naf[RLC_FB_BITS + 1], *_k;
 	eb_t q, s, t[1 << (EB_WIDTH - 2)];
 	bn_t n, m;
 	fb_t u, v, w, z;
+	size_t l;
 
 	if (bn_is_zero(k) || eb_is_infty(p)) {
 		eb_set_infty(r);
 		return;
 	}
diff --git a/src/eb/relic_eb_mul_sim.c b/src/eb/relic_eb_mul_sim.c
index 31d878ea..a496d643 100644
--- a/src/eb/relic_eb_mul_sim.c
+++ b/src/eb/relic_eb_mul_sim.c
@@ -52,14 +52,15 @@
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to a precomputed table.
  */
 static void eb_mul_sim_kbltz(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 		const bn_t m, const eb_t *t) {
-	int i, l, l0, l1, n0, n1, w, g;
+	int i, n0, n1, w, g;
 	int8_t u, tnaf0[RLC_FB_BITS + 8], tnaf1[RLC_FB_BITS + 8], *_k, *_m;
 	eb_t t0[1 << (EB_WIDTH - 2)];
 	eb_t t1[1 << (EB_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	for (i =  0; i < (1 << (EB_WIDTH - 2)); i++) {
 		eb_null(t0[i]);
 		eb_null(t1[i]);
 	}
@@ -181,14 +182,15 @@ static void eb_mul_sim_kbltz(eb_t r, const eb_t p, const bn_t k, const eb_t q,
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to a precomputed table.
  */
 static void eb_mul_sim_plain(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 		const bn_t m, const eb_t *t) {
-	int i, l, l0, l1, n0, n1, w, g;
+	int i, n0, n1, w, g;
 	int8_t naf0[RLC_FB_BITS + 1], naf1[RLC_FB_BITS + 1], *_k, *_m;
 	eb_t t0[1 << (EB_WIDTH - 2)];
 	eb_t t1[1 << (EB_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	for (i =  0; i < (1 << (EB_WIDTH - 2)); i++) {
 		eb_null(t0[i]);
 		eb_null(t1[i]);
 	}
@@ -308,11 +310,11 @@ void eb_mul_sim_basic(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 #if EB_SIM == TRICK || !defined(STRIP)
 
 void eb_mul_sim_trick(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 		const bn_t m) {
 	eb_t t0[1 << (EB_WIDTH / 2)], t1[1 << (EB_WIDTH / 2)], t[1 << EB_WIDTH];
-	int l0, l1, w = EB_WIDTH / 2;
+	size_t l0, l1, w = EB_WIDTH / 2;
 	uint8_t w0[RLC_FB_BITS], w1[RLC_FB_BITS];
 	bn_t n;
 
 	bn_null(n);
 
@@ -434,12 +436,13 @@ void eb_mul_sim_inter(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 #if EB_SIM == JOINT || !defined(STRIP)
 
 void eb_mul_sim_joint(eb_t r, const eb_t p, const bn_t k, const eb_t q,
 		const bn_t m) {
 	eb_t t[5];
-	int i, u_i, len, offset;
+	int i, u_i, offset;
 	int8_t jsf[2 * (RLC_FB_BITS + 1)];
+	size_t len;
 
 	if (bn_is_zero(k) || eb_is_infty(p)) {
 		eb_mul(r, q, m);
 		return;
 	}
diff --git a/src/eb/relic_eb_util.c b/src/eb/relic_eb_util.c
index 0671fbb0..87f04871 100644
--- a/src/eb/relic_eb_util.c
+++ b/src/eb/relic_eb_util.c
@@ -498,11 +498,11 @@ int eb_size_bin(const eb_t a, int pack) {
 	}
 
 	return size;
 }
 
-void eb_read_bin(eb_t a, const uint8_t *bin, int len) {
+void eb_read_bin(eb_t a, const uint8_t *bin, size_t len) {
 	if (len == 1) {
 		if (bin[0] == 0) {
 			eb_set_infty(a);
 			return;
 		} else {
@@ -548,11 +548,11 @@ void eb_read_bin(eb_t a, const uint8_t *bin, int len) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
 	}
 }
 
-void eb_write_bin(uint8_t *bin, int len, const eb_t a, int pack) {
+void eb_write_bin(uint8_t *bin, size_t len, const eb_t a, int pack) {
 	eb_t t;
 
 	eb_null(t);
 
 	memset(bin, 0, len);
diff --git a/src/ed/relic_ed_map.c b/src/ed/relic_ed_map.c
index 260d98db..e95f3da7 100644
--- a/src/ed/relic_ed_map.c
+++ b/src/ed/relic_ed_map.c
@@ -185,11 +185,12 @@ void ed_map_ell2_5mod8(ed_t p, fp_t t) {
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void ed_map_dst(ed_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len) {
+void ed_map_dst(ed_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len) {
 	bn_t k;
 	fp_t t;
 	ed_t q;
 	/* enough space for two field elements plus extra bytes for uniformity */
 	const int len_per_elm = (FP_PRIME + ed_param_level() + 7) / 8;
@@ -252,8 +253,8 @@ void ed_map_dst(ed_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst
 		ed_free(q);
 		RLC_FREE(pseudo_random_bytes);
 	}
 }
 
-void ed_map(ed_t p, const uint8_t *msg, int len) {
+void ed_map(ed_t p, const uint8_t *msg, size_t len) {
 	ed_map_dst(p, msg, len, (const uint8_t *)"RELIC", 5);
 }
diff --git a/src/ed/relic_ed_mul.c b/src/ed/relic_ed_mul.c
index 72540159..e5eec2f4 100644
--- a/src/ed/relic_ed_mul.c
+++ b/src/ed/relic_ed_mul.c
@@ -37,13 +37,14 @@
 /*============================================================================*/
 
 #if ED_MUL == LWNAF || !defined(STRIP)
 
 static void ed_mul_naf_imp(ed_t r, const ed_t p, const bn_t k) {
-	int l, i, n;
+	int i, n;
 	int8_t naf[RLC_FP_BITS + 1];
 	ed_t t[1 << (ED_WIDTH - 2)];
+	size_t l;
 
 	if (bn_is_zero(k)) {
 		ed_set_infty(r);
 		return;
 	}
@@ -108,13 +109,14 @@ static void ed_mul_naf_imp(ed_t r, const ed_t p, const bn_t k) {
 
 #if ED_MUL == LWREG || !defined(STRIP)
 
 static void ed_mul_reg_imp(ed_t r, const ed_t p, const bn_t k) {
 	bn_t _k;
-	int i, j, l, n;
+	int i, j, n;
 	int8_t s, reg[RLC_CEIL(RLC_FP_BITS + 1, ED_WIDTH - 1)];
 	ed_t t[1 << (ED_WIDTH - 2)], u, v;
+	size_t l;
 
 	bn_null(_k);
 	if (bn_is_zero(k)) {
 		ed_set_infty(r);
 		return;
@@ -231,22 +233,22 @@ void ed_mul_basic(ed_t r, const ed_t p, const bn_t k) {
 
 #if ED_MUL == SLIDE || !defined(STRIP)
 
 void ed_mul_slide(ed_t r, const ed_t p, const bn_t k) {
 	ed_t t[1 << (EP_WIDTH - 1)], q;
-	int i, j, l;
 	uint8_t win[RLC_FP_BITS + 1];
+	size_t l;
 
 	ed_null(q);
 
 	if (bn_is_zero(k) || ed_is_infty(p)) {
 		ed_set_infty(r);
 		return;
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
 			ed_null(t[i]);
 			ed_new(t[i]);
 		}
 
 		ed_new(q);
@@ -257,26 +259,26 @@ void ed_mul_slide(ed_t r, const ed_t p, const bn_t k) {
 #if defined(EP_MIXED)
 		ed_norm(q, q);
 #endif
 
 		/* Create table. */
-		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
 			ed_add(t[i], t[i - 1], q);
 		}
 
 #if defined(EP_MIXED)
 		ed_norm_sim(t + 1, (const ed_t *)t + 1, (1 << (EP_WIDTH - 1)) - 1);
 #endif
 
 		ed_set_infty(q);
 		l = RLC_FP_BITS + 1;
 		bn_rec_slw(win, &l, k, EP_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				ed_dbl(q, q);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					ed_dbl(q, q);
 				}
 				ed_add(q, q, t[win[i] >> 1]);
 			}
 		}
@@ -288,11 +290,11 @@ void ed_mul_slide(ed_t r, const ed_t p, const bn_t k) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
 			ed_free(t[i]);
 		}
 		ed_free(q);
 	}
 }
@@ -407,11 +409,11 @@ void ed_mul_gen(ed_t r, const bn_t k) {
 
 void ed_mul_dig(ed_t r, const ed_t p, dig_t k) {
 	ed_t t;
 	bn_t _k;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	ed_null(t);
 	bn_null(_k);
 
 	if (k == 0 || ed_is_infty(p)) {
diff --git a/src/ed/relic_ed_mul_fix.c b/src/ed/relic_ed_mul_fix.c
index 175a759a..0e0f6863 100644
--- a/src/ed/relic_ed_mul_fix.c
+++ b/src/ed/relic_ed_mul_fix.c
@@ -45,12 +45,13 @@
  * @param[out] r 				- the result.
  * @param[in] t					- the precomputed table.
  * @param[in] k					- the integer.
  */
 static void ed_mul_fix_plain(ed_t r, const ed_t * t, const bn_t k) {
-	int l, i, n;
+	int i, n;
 	int8_t naf[RLC_FP_BITS + 1], *_k;
+	size_t l;
 
 	/* Compute the w-TNAF representation of k. */
 	l = RLC_FP_BITS + 1;
 	bn_rec_naf(naf, &l, k, ED_DEPTH);
 
diff --git a/src/ed/relic_ed_mul_sim.c b/src/ed/relic_ed_mul_sim.c
index b03e556a..68980fd1 100644
--- a/src/ed/relic_ed_mul_sim.c
+++ b/src/ed/relic_ed_mul_sim.c
@@ -51,14 +51,15 @@
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ed_mul_sim_plain(ed_t r, const ed_t p, const bn_t k, const ed_t q,
 		const bn_t m, const ed_t *t) {
-	int i, l, l0, l1, n0, n1, w, gen;
+	int i, n0, n1, w, gen;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], *_k, *_m;
 	ed_t t0[1 << (ED_WIDTH - 2)];
 	ed_t t1[1 << (ED_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	RLC_TRY {
 		gen = (t == NULL ? 0 : 1);
 		if (!gen) {
 			for (i = 0; i < (1 << (ED_WIDTH - 2)); i++) {
@@ -174,11 +175,11 @@ void ed_mul_sim_basic(ed_t r, const ed_t p, const bn_t k, const ed_t q,
 
 void ed_mul_sim_trick(ed_t r, const ed_t p, const bn_t k, const ed_t q,
 		const bn_t m) {
 	ed_t t0[1 << (ED_WIDTH / 2)], t1[1 << (ED_WIDTH / 2)], t[1 << ED_WIDTH];
 	bn_t n;
-	int l0, l1, w = ED_WIDTH / 2;
+	size_t l0, l1, w = ED_WIDTH / 2;
 	uint8_t w0[RLC_FP_BITS + 1], w1[RLC_FP_BITS + 1];
 
 	bn_null(n);
 
 	if (bn_is_zero(k) || ed_is_infty(p)) {
@@ -291,12 +292,13 @@ void ed_mul_sim_inter(ed_t r, const ed_t p, const bn_t k, const ed_t q,
 #if ED_SIM == JOINT || !defined(STRIP)
 
 void ed_mul_sim_joint(ed_t r, const ed_t p, const bn_t k, const ed_t q,
 		const bn_t m) {
 	ed_t t[5];
-	int i, l, u_i, offset;
+	int i, u_i, offset;
 	int8_t jsf[2 * (RLC_FP_BITS + 1)];
+	size_t l;
 
 	if (bn_is_zero(k) || ed_is_infty(p)) {
 		ed_mul(r, q, m);
 		return;
 	}
@@ -396,13 +398,14 @@ void ed_mul_sim_gen(ed_t r, const bn_t k, const ed_t q, const bn_t m) {
 		ed_free(g);
 	}
 }
 
 void ed_mul_sim_lot(ed_t r, const ed_t p[], const bn_t k[], int n) {
-	int i, j, l, *_l = RLC_ALLOCA(int, n);
-	ed_t *_p = RLC_ALLOCA(ed_t, n);
+	int i, j;
 	int8_t *naf = NULL;
+	ed_t *_p = RLC_ALLOCA(ed_t, n);
+	size_t l, *_l = RLC_ALLOCA(size_t, n);
 
 	RLC_TRY {
 		l = 0;
 		for (i = 0; i < n; i++) {
 			l = RLC_MAX(l, bn_bits(k[i]) + 1);
diff --git a/src/ed/relic_ed_util.c b/src/ed/relic_ed_util.c
index 55c3d68d..b9dd43b3 100644
--- a/src/ed/relic_ed_util.c
+++ b/src/ed/relic_ed_util.c
@@ -240,11 +240,11 @@ int ed_size_bin(const ed_t a, int pack) {
 	}
 
 	return size;
 }
 
-void ed_read_bin(ed_t a, const uint8_t *bin, int len) {
+void ed_read_bin(ed_t a, const uint8_t *bin, size_t len) {
 	if (len == 1) {
 		if (bin[0] == 0) {
 			ed_set_infty(a);
 			return;
 		} else {
@@ -295,11 +295,11 @@ void ed_read_bin(ed_t a, const uint8_t *bin, int len) {
 	if (!ed_on_curve(a)) {
 		RLC_THROW(ERR_NO_VALID);
 	}
 }
 
-void ed_write_bin(uint8_t *bin, int len, const ed_t a, int pack) {
+void ed_write_bin(uint8_t *bin, size_t len, const ed_t a, int pack) {
 	ed_t t;
 
 	ed_null(t);
 
 	memset(bin, 0, len);
diff --git a/src/ep/relic_ep_map.c b/src/ep/relic_ep_map.c
index 53e1d3ea..c34d3a4c 100644
--- a/src/ep/relic_ep_map.c
+++ b/src/ep/relic_ep_map.c
@@ -46,39 +46,45 @@
  * @param[out] c		- the result.
  * @param[in] a			- the input value.
  * @param[in] coeffs	- the vector of coefficients in the polynomial.
  * @param[in] deg 		- the degree of the polynomial.
  */
-TMPL_MAP_HORNER(fp, fp_st)
+TMPL_MAP_HORNER(fp, fp_st);
+
 /**
  * Generic isogeny map evaluation for use with SSWU map.
  */
-		TMPL_MAP_ISOGENY_MAP(ep, fp, iso)
+TMPL_MAP_ISOGENY_MAP(ep, fp, iso);
+
 #endif /* EP_CTMAP */
+
+#define EP_MAP_COPY_COND(O, I, C) dv_copy_cond(O, I, RLC_FP_DIGS, C)
 /**
  * Simplified SWU mapping from Section 4 of
  * "Fast and simple constant-time hashing to the BLS12-381 Elliptic Curve"
  */
-#define EP_MAP_COPY_COND(O, I, C) dv_copy_cond(O, I, RLC_FP_DIGS, C)
-		TMPL_MAP_SSWU(ep, fp, dig_t, EP_MAP_COPY_COND)
+TMPL_MAP_SSWU(ep, fp, dig_t, EP_MAP_COPY_COND);
+
 /**
  * Shallue--van de Woestijne map, based on the definition from
  * draft-irtf-cfrg-hash-to-curve-06, Section 6.6.1
  */
-TMPL_MAP_SVDW(ep, fp, dig_t, EP_MAP_COPY_COND)
+TMPL_MAP_SVDW(ep, fp, dig_t, EP_MAP_COPY_COND);
+
 #undef EP_MAP_COPY_COND
-/* caution: this function overwrites k, which it uses as an auxiliary variable */
+
+/* caution: this function overwrites k, which used as an auxiliary variable */
 static inline int fp_sgn0(const fp_t t, bn_t k) {
 	fp_prime_back(k, t);
 	return bn_get_bit(k, 0);
 }
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len) {
+void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, size_t len) {
 	bn_t k;
 	fp_t t;
 	ep_t q;
 	int neg;
 	/* enough space for two field elements plus extra bytes for uniformity */
@@ -98,30 +104,30 @@ void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len) {
 		ep_new(q);
 
 		/* figure out which hash function to use */
 		const int abNeq0 = (ep_curve_opt_a() != RLC_ZERO) &&
 				(ep_curve_opt_b() != RLC_ZERO);
-		void (*const map_fn)(ep_t, fp_t) =(ep_curve_is_ctmap() ||
+		void (*const map_fn)(ep_t, fp_t) = (ep_curve_is_ctmap() ||
 				abNeq0) ? ep_map_sswu : ep_map_svdw;
 
-#define EP_MAP_CONVERT_BYTES(IDX)                                       \
-    do {                                                                \
-      bn_read_bin(k, uniform_bytes + IDX * len_per_elm, len_per_elm);   \
-      fp_prime_conv(t, k);                                              \
-    } while (0)
-
-#define EP_MAP_APPLY_MAP(PT)                                    \
-    do {                                                        \
-      /* check sign of t */                                     \
-      neg = fp_sgn0(t, k);                                      \
-      /* convert */                                             \
-      map_fn(PT, t);                                            \
-      /* compare sign of y and sign of t; fix if necessary */   \
-      neg = neg != fp_sgn0(PT->y, k);                             \
-      fp_neg(t, PT->y);                                          \
-      dv_copy_cond(PT->y, t, RLC_FP_DIGS, neg);                  \
-    } while (0)
+#define EP_MAP_CONVERT_BYTES(IDX)											\
+		do {																\
+			bn_read_bin(k, uniform_bytes + IDX * len_per_elm, len_per_elm);	\
+			fp_prime_conv(t, k);											\
+		} while (0)
+
+#define EP_MAP_APPLY_MAP(PT)												\
+		do {																\
+			/* check sign of t */											\
+			neg = fp_sgn0(t, k);											\
+			/* convert */													\
+			map_fn(PT, t);													\
+			/* compare sign of y and sign of t; fix if necessary */			\
+			neg = neg != fp_sgn0(PT->y, k);									\
+			fp_neg(t, PT->y);												\
+			dv_copy_cond(PT->y, t, RLC_FP_DIGS, neg);						\
+		} while (0)
 
 		/* first map invocation */
 		EP_MAP_CONVERT_BYTES(0);
 		EP_MAP_APPLY_MAP(p);
 		TMPL_MAP_CALL_ISOMAP(ep, p);
@@ -177,12 +183,12 @@ void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len) {
 		fp_free(t);
 		ep_free(q);
 	}
 }
 
-void ep_map_dst(ep_t p, const uint8_t *msg, int len, const uint8_t *dst,
-		int dst_len) {
+void ep_map_dst(ep_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len) {
 
 	/* enough space for two field elements plus extra bytes for uniformity */
 	const int len_per_elm = (FP_PRIME + ep_param_level() + 7) / 8;
 	uint8_t *pseudo_random_bytes = RLC_ALLOCA(uint8_t, 2 * len_per_elm);
 
@@ -200,8 +206,8 @@ void ep_map_dst(ep_t p, const uint8_t *msg, int len, const uint8_t *dst,
 	RLC_FINALLY {
 		RLC_FREE(pseudo_random_bytes);
 	}
 }
 
-void ep_map(ep_t p, const uint8_t *msg, int len) {
+void ep_map(ep_t p, const uint8_t *msg, size_t len) {
 	ep_map_dst(p, msg, len, (const uint8_t *)"RELIC", 5);
 }
diff --git a/src/ep/relic_ep_mul.c b/src/ep/relic_ep_mul.c
index d73353c3..772f3406 100644
--- a/src/ep/relic_ep_mul.c
+++ b/src/ep/relic_ep_mul.c
@@ -38,14 +38,15 @@
 #if EP_MUL == LWNAF || !defined(STRIP)
 
 #if defined(EP_ENDOM)
 
 static void ep_mul_glv_imp(ep_t r, const ep_t p, const bn_t k) {
-	int l, l0, l1, i, n0, n1, s0, s1;
+	int i, n0, n1, s0, s1;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], *t0, *t1;
 	bn_t n, _k, k0, k1, v1[3], v2[3];
 	ep_t q, t[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	bn_null(n);
 	bn_null(_k);
 	bn_null(k0);
 	bn_null(k1);
@@ -151,24 +152,24 @@ static void ep_mul_glv_imp(ep_t r, const ep_t p, const bn_t k) {
 #endif /* EP_ENDOM */
 
 #if defined(EP_PLAIN) || defined(EP_SUPER)
 
 static void ep_mul_naf_imp(ep_t r, const ep_t p, const bn_t k) {
-	int i, l;
 	/* Some of the supported prime curves have order > field. */
 	int8_t u, naf[RLC_FP_BITS + 2];
 	ep_t t[1 << (EP_WIDTH - 2)];
 	bn_t _k, n;
+	size_t l;
 
 	bn_null(n);
 	bn_null(_k);
 
 	RLC_TRY {
 		bn_new(n);
 		bn_new(_k);
 		/* Prepare the precomputation table. */
-		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
 			ep_null(t[i]);
 			ep_new(t[i]);
 		}
 
 		ep_curve_get_ord(n);
@@ -180,11 +181,11 @@ static void ep_mul_naf_imp(ep_t r, const ep_t p, const bn_t k) {
 		/* Compute the w-NAF representation of k. */
 		l = RLC_FP_BITS + 2;
 		bn_rec_naf(naf, &l, _k, EP_WIDTH);
 
 		ep_set_infty(r);
-		for (i = l - 1; i >= 0; i--) {
+		for (int i = l - 1; i >= 0; i--) {
 			ep_dbl(r, r);
 
 			u = naf[i];
 			if (u > 0) {
 				ep_add(r, r, t[u / 2]);
@@ -203,11 +204,11 @@ static void ep_mul_naf_imp(ep_t r, const ep_t p, const bn_t k) {
 	}
 	RLC_FINALLY {
 		bn_free(n);
 		bn_free(_k);
 		/* Free the precomputation table. */
-		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
 			ep_free(t[i]);
 		}
 	}
 }
 
@@ -217,14 +218,15 @@ static void ep_mul_naf_imp(ep_t r, const ep_t p, const bn_t k) {
 #if EP_MUL == LWREG || !defined(STRIP)
 
 #if defined(EP_ENDOM)
 
 static void ep_mul_reg_glv(ep_t r, const ep_t p, const bn_t k) {
-	int i, j, l, n0, n1, s0, s1, b0, b1;
+	int i, j, n0, n1, s0, s1, b0, b1;
 	int8_t _s0, _s1, reg0[RLC_FP_BITS + 1], reg1[RLC_FP_BITS + 1];
 	bn_t n, _k, k0, k1, v1[3], v2[3];
 	ep_t q, t[1 << (EP_WIDTH - 2)], u, v, w;
+	size_t l;
 
 	bn_null(n);
 	bn_null(_k);
 	bn_null(k0);
 	bn_null(k1);
@@ -369,13 +371,14 @@ static void ep_mul_reg_glv(ep_t r, const ep_t p, const bn_t k) {
 
 #if defined(EP_PLAIN) || defined(EP_SUPER)
 
 static void ep_mul_reg_imp(ep_t r, const ep_t p, const bn_t k) {
 	bn_t _k;
-	int i, j, l, n;
+	int i, j, n;
 	int8_t s, reg[1 + RLC_CEIL(RLC_FP_BITS + 1, EP_WIDTH - 1)];
 	ep_t t[1 << (EP_WIDTH - 2)], u, v;
+	size_t l;
 
 	if (bn_is_zero(k)) {
 		ep_set_infty(r);
 		return;
 	}
@@ -500,12 +503,12 @@ void ep_mul_basic(ep_t r, const ep_t p, const bn_t k) {
 #if EP_MUL == SLIDE || !defined(STRIP)
 
 void ep_mul_slide(ep_t r, const ep_t p, const bn_t k) {
 	bn_t _k, n;
 	ep_t t[1 << (EP_WIDTH - 1)], q;
-	int i, j, l;
 	uint8_t win[RLC_FP_BITS + 1];
+	size_t l;
 
 	if (bn_is_zero(k) || ep_is_infty(p)) {
 		ep_set_infty(r);
 		return;
 	}
@@ -515,11 +518,11 @@ void ep_mul_slide(ep_t r, const ep_t p, const bn_t k) {
 	bn_null(_k);
 
 	RLC_TRY {
 		bn_new(n);
 		bn_new(_k);
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
 			ep_null(t[i]);
 			ep_new(t[i]);
 		}
 		ep_new(q);
 
@@ -532,26 +535,26 @@ void ep_mul_slide(ep_t r, const ep_t p, const bn_t k) {
 
 		ep_curve_get_ord(n);
 		bn_mod(_k, k, n);
 
 		/* Create table. */
-		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep_add(t[i], t[i - 1], q);
 		}
 
 #if defined(EP_MIXED)
 		ep_norm_sim(t + 1, (const ep_t *)t + 1, (1 << (EP_WIDTH - 1)) - 1);
 #endif
 
 		ep_set_infty(q);
 		l = RLC_FP_BITS + 1;
 		bn_rec_slw(win, &l, _k, EP_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				ep_dbl(q, q);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					ep_dbl(q, q);
 				}
 				ep_add(q, q, t[win[i] >> 1]);
 			}
 		}
@@ -565,11 +568,11 @@ void ep_mul_slide(ep_t r, const ep_t p, const bn_t k) {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		bn_free(n);
 		bn_free(_k);
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep_free(t[i]);
 		}
 		ep_free(q);
 	}
 }
@@ -577,13 +580,13 @@ void ep_mul_slide(ep_t r, const ep_t p, const bn_t k) {
 #endif
 #include "assert.h"
 #if EP_MUL == MONTY || !defined(STRIP)
 
 void ep_mul_monty(ep_t r, const ep_t p, const bn_t k) {
-	int i, j, bits;
 	ep_t t[2];
 	bn_t n, l, _k;
+	size_t bits;
 
 	bn_null(n);
 	bn_null(l);
 	bn_null(_k);
 	ep_null(t[0]);
@@ -617,12 +620,12 @@ void ep_mul_monty(ep_t r, const ep_t p, const bn_t k) {
 
 		/* Blind both points independently. */
 		ep_blind(t[0], t[0]);
 		ep_blind(t[1], t[1]);
 
-		for (i = bits - 1; i >= 0; i--) {
-			j = bn_get_bit(l, i);
+		for (int i = bits - 1; i >= 0; i--) {
+			int j = bn_get_bit(l, i);
 			dv_swap_cond(t[0]->x, t[1]->x, RLC_FP_DIGS, j ^ 1);
 			dv_swap_cond(t[0]->y, t[1]->y, RLC_FP_DIGS, j ^ 1);
 			dv_swap_cond(t[0]->z, t[1]->z, RLC_FP_DIGS, j ^ 1);
 			ep_add(t[0], t[0], t[1]);
 			ep_dbl(t[1], t[1]);
@@ -721,11 +724,11 @@ void ep_mul_gen(ep_t r, const bn_t k) {
 
 void ep_mul_dig(ep_t r, const ep_t p, dig_t k) {
 	ep_t t;
 	bn_t _k;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	ep_null(t);
 	bn_null(_k);
 
 	if (k == 0 || ep_is_infty(p)) {
diff --git a/src/ep/relic_ep_mul_fix.c b/src/ep/relic_ep_mul_fix.c
index 913c392d..ea309ad5 100644
--- a/src/ep/relic_ep_mul_fix.c
+++ b/src/ep/relic_ep_mul_fix.c
@@ -44,12 +44,13 @@
  * @param[out] r 				- the result.
  * @param[in] t					- the precomputed table.
  * @param[in] k					- the integer.
  */
 static void ep_mul_fix_plain(ep_t r, const ep_t *t, const bn_t k) {
-	int l, i, n;
+	int i, n;
 	int8_t naf[RLC_FP_BITS + 1];
+	size_t l;
 
 	/* Compute the w-TNAF representation of k. */
 	l = RLC_FP_BITS + 1;
 	bn_rec_naf(naf, &l, k, EP_DEPTH);
 
diff --git a/src/ep/relic_ep_mul_sim.c b/src/ep/relic_ep_mul_sim.c
index efba4b9c..c4c78e3f 100644
--- a/src/ep/relic_ep_mul_sim.c
+++ b/src/ep/relic_ep_mul_sim.c
@@ -52,18 +52,19 @@
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ep_mul_sim_endom(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 		const bn_t m, const ep_t *t) {
-	int i, l, l0, l1, l2, l3, sk0, sk1, sl0, sl1, w, g = 0;
+	int i, sk0, sk1, sl0, sl1, w, g = 0;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], *t0, *t1, u;
 	int8_t naf2[RLC_FP_BITS + 1], naf3[RLC_FP_BITS + 1], *t2, *t3;
 	bn_t n, k0, k1, m0, m1;
 	bn_t v1[3], v2[3];
 	ep_t v;
 	ep_t tab0[1 << (EP_WIDTH - 2)];
 	ep_t tab1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1, l2, l3;
 
 	bn_null(n);
 	bn_null(k0);
 	bn_null(k1);
 	bn_null(m0);
@@ -262,13 +263,14 @@ static void ep_mul_sim_endom(ep_t r, const ep_t p, const bn_t k, const ep_t q,
  * @param[out] k			- the integer scalars.
  * @param[out] n			- the number of elements to multiply.
  */
 void ep_mul_sim_lot_endom(ep_t r, const ep_t p[], const bn_t k[], int n) {
 	const int len = RLC_FP_BITS + 1;
-	int i, j, m, l, _l[2], sk;
+	int i, j, m, sk;
 	bn_t _k[2], q, v1[3], v2[3];
 	int8_t ptr, *naf = RLC_ALLOCA(int8_t, 2 * n * len);
+	size_t l, _l[2];
 
 	bn_null(q);
 
 	if (n <= 10) {
 		ep_t *_p = RLC_ALLOCA(ep_t, 2 * n);
@@ -484,14 +486,15 @@ void ep_mul_sim_lot_endom(ep_t r, const ep_t p[], const bn_t k[], int n) {
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ep_mul_sim_plain(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 		const bn_t m, const ep_t *t) {
-	int i, l, l0, l1, w, gen;
+	int i, w, gen;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], n0, n1, *u, *v;
 	ep_t t0[1 << (EP_WIDTH - 2)];
 	ep_t t1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	RLC_TRY {
 		gen = (t == NULL ? 0 : 1);
 		if (!gen) {
 			for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
@@ -581,13 +584,14 @@ static void ep_mul_sim_plain(ep_t r, const ep_t p, const bn_t k, const ep_t q,
  * @param[out] p			- the elements to multiply.
  * @param[out] k			- the integer scalars.
  * @param[out] n			- the number of elements to multiply.
  */
 void ep_mul_sim_lot_plain(ep_t r, const ep_t p[], const bn_t k[], int n) {
-	int i, j, l, *_l = RLC_ALLOCA(int, n);
-	ep_t *_p = RLC_ALLOCA(ep_t, n);
+	int i, j;
 	int8_t *naf = NULL;
+	ep_t *_p = RLC_ALLOCA(ep_t, n);
+	size_t l, *_l = RLC_ALLOCA(size_t, n);
 
 	RLC_TRY {
 		l = 0;
 		for (i = 0; i < n; i++) {
 			l = RLC_MAX(l, bn_bits(k[i]) + 1);
@@ -675,11 +679,11 @@ void ep_mul_sim_basic(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 
 void ep_mul_sim_trick(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 		const bn_t m) {
 	ep_t t0[1 << (EP_WIDTH / 2)], t1[1 << (EP_WIDTH / 2)], t[1 << EP_WIDTH];
 	bn_t n, _k, _m;
-	int l0, l1, w = EP_WIDTH / 2;
+	size_t l0, l1, w = EP_WIDTH / 2;
 	uint8_t w0[RLC_FP_BITS + 1], w1[RLC_FP_BITS + 1];
 
 	if (bn_is_zero(k) || ep_is_infty(p)) {
 		ep_mul(r, q, m);
 		return;
@@ -836,12 +840,13 @@ void ep_mul_sim_inter(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 
 void ep_mul_sim_joint(ep_t r, const ep_t p, const bn_t k, const ep_t q,
 		const bn_t m) {
 	bn_t n, _k, _m;
 	ep_t t[5];
-	int i, l, u_i, offset;
+	int i, u_i, offset;
 	int8_t jsf[2 * (RLC_FP_BITS + 1)];
+	size_t l;
 
 	if (bn_is_zero(k) || ep_is_infty(p)) {
 		ep_mul(r, q, m);
 		return;
 	}
diff --git a/src/ep/relic_ep_util.c b/src/ep/relic_ep_util.c
index 54b13eab..2d2d5871 100644
--- a/src/ep/relic_ep_util.c
+++ b/src/ep/relic_ep_util.c
@@ -232,11 +232,11 @@ int ep_size_bin(const ep_t a, int pack) {
 	}
 
 	return size;
 }
 
-void ep_read_bin(ep_t a, const uint8_t *bin, int len) {
+void ep_read_bin(ep_t a, const uint8_t *bin, size_t len) {
 	if (len == 1) {
 		if (bin[0] == 0) {
 			ep_set_infty(a);
 			return;
 		} else {
@@ -282,11 +282,11 @@ void ep_read_bin(ep_t a, const uint8_t *bin, int len) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
 	}
 }
 
-void ep_write_bin(uint8_t *bin, int len, const ep_t a, int pack) {
+void ep_write_bin(uint8_t *bin, size_t len, const ep_t a, int pack) {
 	ep_t t;
 
 	ep_null(t);
 
 	memset(bin, 0, len);
diff --git a/src/epx/relic_ep2_map.c b/src/epx/relic_ep2_map.c
index 7764679a..8a130c0f 100644
--- a/src/epx/relic_ep2_map.c
+++ b/src/epx/relic_ep2_map.c
@@ -46,33 +46,29 @@
  * @param[in] a				- the input value.
  * @param[in] coeffs		- the vector of coefficients in the polynomial.
  * @param[in] len			- the degree of the polynomial.
  */
 TMPL_MAP_HORNER(fp2, fp2_t)
-
 /**
  * Generic isogeny map evaluation for use with SSWU map.
  */
 TMPL_MAP_ISOGENY_MAP(ep2, fp2, iso2)
 #endif /* EP_CTMAP */
-
 /**
  * Simplified SWU mapping.
  */
-#define EP2_MAP_COPY_COND(O, I, C)                                                       \
-	do {                                                                                 \
-		dv_copy_cond(O[0], I[0], RLC_FP_DIGS, C);                                        \
-		dv_copy_cond(O[1], I[1], RLC_FP_DIGS, C);                                        \
+#define EP2_MAP_COPY_COND(O, I, C)											\
+	do {																	\
+		dv_copy_cond(O[0], I[0], RLC_FP_DIGS, C);							\
+		dv_copy_cond(O[1], I[1], RLC_FP_DIGS, C);							\
 	} while (0)
 TMPL_MAP_SSWU(ep2, fp2, fp_t, EP2_MAP_COPY_COND)
-
 /**
  * Shallue--van de Woestijne map.
  */
 TMPL_MAP_SVDW(ep2, fp2, fp_t, EP2_MAP_COPY_COND)
 #undef EP2_MAP_COPY_COND
-
 /* caution: this function overwrites k, which it uses as an auxiliary variable */
 static inline int fp2_sgn0(const fp2_t t, bn_t k) {
 	const int t_0_zero = fp_is_zero(t[0]);
 
 	fp_prime_back(k, t[0]);
@@ -87,107 +83,108 @@ static inline int fp2_sgn0(const fp2_t t, bn_t k) {
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, int len) {
-        bn_t k;
-        fp2_t t;
-        ep2_t q;
-        int neg;
-        /* enough space for two extension field elements plus extra bytes for uniformity */
-        const int len_per_elm = (FP_PRIME + ep_param_level() + 7) / 8;
-
-        bn_null(k);
-        fp2_null(t);
-        ep2_null(q);
-
-        RLC_TRY {
-                if (len != 2* len_per_elm) {
-                  RLC_THROW(ERR_NO_VALID);
-                }
-
-                bn_new(k);
-                fp2_new(t);
-                ep2_new(q);
-
-                /* which hash function should we use? */
-                const int abNeq0 = (ep2_curve_opt_a() != RLC_ZERO) && (ep2_curve_opt_b() != RLC_ZERO);
-                void (*const map_fn)(ep2_t, fp2_t) = (ep2_curve_is_ctmap() || abNeq0) ? ep2_map_sswu : ep2_map_svdw;
-
-#define EP2_MAP_CONVERT_BYTES(IDX)                                                       \
-        do {                                                                                 \
-                bn_read_bin(k, uniform_bytes + 2 * IDX * len_per_elm, len_per_elm);        \
-                fp_prime_conv(t[0], k);                                                          \
-                bn_read_bin(k, uniform_bytes + (2 * IDX + 1) * len_per_elm, len_per_elm);  \
-                fp_prime_conv(t[1], k);                                                          \
-        } while (0)
-
-#define EP2_MAP_APPLY_MAP(PT)                                                            \
-        do {                                                                                 \
-                /* sign of t */                                                                  \
-                neg = fp2_sgn0(t, k);                                                            \
-                /* convert */                                                                    \
-                map_fn(PT, t);                                                                   \
-                /* compare sign of y to sign of t; fix if necessary */                           \
-                neg = neg != fp2_sgn0(PT->y, k);                                                 \
-                fp2_neg(t, PT->y);                                                               \
-                dv_copy_cond(PT->y[0], t[0], RLC_FP_DIGS, neg);                                  \
-                dv_copy_cond(PT->y[1], t[1], RLC_FP_DIGS, neg);                                  \
-        } while (0)
-
-                /* first map invocation */
-                EP2_MAP_CONVERT_BYTES(0);
-                EP2_MAP_APPLY_MAP(p);
-                TMPL_MAP_CALL_ISOMAP(ep2, p);
-
-                /* second map invocation */
-                EP2_MAP_CONVERT_BYTES(1);
-                EP2_MAP_APPLY_MAP(q);
-                TMPL_MAP_CALL_ISOMAP(ep2, q);
-
-                /* XXX(rsw) could add p and q and then apply isomap,
-                 * but need ep_add to support addition on isogeny curves */
+void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, size_t len) {
+	bn_t k;
+	fp2_t t;
+	ep2_t q;
+	int neg;
+	/* enough space for two extension field elements plus extra bytes for uniformity */
+	const int lpe = (FP_PRIME + ep_param_level() + 7) / 8;
+
+	bn_null(k);
+	fp2_null(t);
+	ep2_null(q);
+
+	RLC_TRY {
+		if (len != 2 * lpe) {
+			RLC_THROW(ERR_NO_VALID);
+		}
+
+		bn_new(k);
+		fp2_new(t);
+		ep2_new(q);
+
+		/* which hash function should we use? */
+		const int abNeq0 = (ep2_curve_opt_a() != RLC_ZERO) &&
+				(ep2_curve_opt_b() != RLC_ZERO);
+		void (*const map_fn)(ep2_t, fp2_t) = (ep2_curve_is_ctmap() ||
+				abNeq0) ? ep2_map_sswu : ep2_map_svdw;
+
+#define EP2_MAP_CONVERT_BYTES(IDX)											\
+		do {																\
+			bn_read_bin(k, uniform_bytes + 2 * IDX * lpe, lpe);				\
+			fp_prime_conv(t[0], k);											\
+			bn_read_bin(k, uniform_bytes + (2 * IDX + 1) * lpe, lpe);		\
+			fp_prime_conv(t[1], k);											\
+	    } while (0)
+
+#define EP2_MAP_APPLY_MAP(PT)												\
+		do {																\
+            /* sign of t */													\
+            neg = fp2_sgn0(t, k);											\
+            /* convert */													\
+            map_fn(PT, t);													\
+            /* compare sign of y to sign of t; fix if necessary */			\
+            neg = neg != fp2_sgn0(PT->y, k);								\
+            fp2_neg(t, PT->y);												\
+            dv_copy_cond(PT->y[0], t[0], RLC_FP_DIGS, neg);					\
+            dv_copy_cond(PT->y[1], t[1], RLC_FP_DIGS, neg);					\
+		} while (0)
+
+		/* first map invocation */
+		EP2_MAP_CONVERT_BYTES(0);
+		EP2_MAP_APPLY_MAP(p);
+		TMPL_MAP_CALL_ISOMAP(ep2, p);
+
+		/* second map invocation */
+		EP2_MAP_CONVERT_BYTES(1);
+		EP2_MAP_APPLY_MAP(q);
+		TMPL_MAP_CALL_ISOMAP(ep2, q);
+
+		/* XXX(rsw) could add p and q and then apply isomap,
+		 * but need ep_add to support addition on isogeny curves */
 
 #undef EP2_MAP_CONVERT_BYTES
 #undef EP2_MAP_APPLY_MAP
 
-                /* sum the result */
-                ep2_add(p, p, q);
-                ep2_norm(p, p);
-                ep2_mul_cof(p, p);
-        }
-        RLC_CATCH_ANY {
-                RLC_THROW(ERR_CAUGHT);
-        }
-        RLC_FINALLY {
-                bn_free(k);
-                fp2_free(t);
-                ep2_free(q);
-        }
+		/* sum the result */
+		ep2_add(p, p, q);
+		ep2_norm(p, p);
+		ep2_mul_cof(p, p);
+	}
+	RLC_CATCH_ANY {
+		RLC_THROW(ERR_CAUGHT);
+	}
+	RLC_FINALLY {
+		bn_free(k);
+		fp2_free(t);
+		ep2_free(q);
+	}
 }
 
 
-void ep2_map_dst(ep2_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len) {
-
-        /* enough space for two field elements plus extra bytes for uniformity */
-        const int len_per_elm = (FP_PRIME + ep_param_level() + 7) / 8;
-        uint8_t *pseudo_random_bytes = RLC_ALLOCA(uint8_t, 4 * len_per_elm);
-
-        RLC_TRY {
-
-                /* XXX(rsw) See note in ep/relic_ep_map.c about using MD_MAP. */
-                /* hash to a pseudorandom string using md_xmd */
-                md_xmd(pseudo_random_bytes, 4 * len_per_elm, msg, len, dst, dst_len);
-                ep2_map_from_field(p, pseudo_random_bytes, 2 * len_per_elm);
-        }
-        RLC_CATCH_ANY {
-                RLC_THROW(ERR_CAUGHT);
-        }
-        RLC_FINALLY {
-                RLC_FREE(pseudo_random_bytes);
-        }
+void ep2_map_dst(ep2_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
+		size_t dst_len) {
+	/* enough space for two field elements plus extra bytes for uniformity */
+	const int lpe = (FP_PRIME + ep_param_level() + 7) / 8;
+	uint8_t *pseudo_random_bytes = RLC_ALLOCA(uint8_t, 4 * lpe);
+
+	RLC_TRY {
+		/* XXX(rsw) See note in ep/relic_ep_map.c about using MD_MAP. */
+		/* hash to a pseudorandom string using md_xmd */
+		md_xmd(pseudo_random_bytes, 4 * lpe, msg, len, dst, dst_len);
+		ep2_map_from_field(p, pseudo_random_bytes, 2 * lpe);
+	}
+	RLC_CATCH_ANY {
+		RLC_THROW(ERR_CAUGHT);
+	}
+	RLC_FINALLY {
+		RLC_FREE(pseudo_random_bytes);
+	}
 }
 
-void ep2_map(ep2_t p, const uint8_t *msg, int len) {
+void ep2_map(ep2_t p, const uint8_t *msg, size_t len) {
 	ep2_map_dst(p, msg, len, (const uint8_t *)"RELIC", 5);
 }
diff --git a/src/epx/relic_ep2_mul.c b/src/epx/relic_ep2_mul.c
index 684b5eb5..1d45f9cc 100644
--- a/src/epx/relic_ep2_mul.c
+++ b/src/epx/relic_ep2_mul.c
@@ -39,22 +39,22 @@
 #if EP_MUL == LWNAF || !defined(STRIP)
 
 #if defined(EP_ENDOM)
 
 static void ep2_mul_glv_imp(ep2_t r, const ep2_t p, const bn_t k) {
-	int i, j, l, _l[4];
+	size_t l, _l[4];
 	bn_t n, _k[4], u;
 	int8_t naf[4][RLC_FP_BITS + 1];
 	ep2_t q[4];
 
 	bn_null(n);
 	bn_null(u);
 
 	RLC_TRY {
 		bn_new(n);
 		bn_new(u);
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			bn_null(_k[i]);
 			ep2_null(q[i]);
 			bn_new(_k[i]);
 			ep2_new(q[i]);
 		}
@@ -68,24 +68,24 @@ static void ep2_mul_glv_imp(ep2_t r, const ep2_t p, const bn_t k) {
 		ep2_frb(q[1], q[0], 1);
 		ep2_frb(q[2], q[1], 1);
 		ep2_frb(q[3], q[2], 1);
 
 		l = 0;
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			if (bn_sign(_k[i]) == RLC_NEG) {
 				ep2_neg(q[i], q[i]);
 			}
 			_l[i] = RLC_FP_BITS + 1;
 			bn_rec_naf(naf[i], &_l[i], _k[i], 2);
 			l = RLC_MAX(l, _l[i]);
 		}
 
 		ep2_set_infty(r);
-		for (j = l - 1; j >= 0; j--) {
+		for (int j = l - 1; j >= 0; j--) {
 			ep2_dbl(r, r);
 
-			for (i = 0; i < 4; i++) {
+			for (int i = 0; i < 4; i++) {
 				if (naf[i][j] > 0) {
 					ep2_add(r, r, q[i]);
 				}
 				if (naf[i][j] < 0) {
 					ep2_sub(r, r, q[i]);
@@ -100,28 +100,28 @@ static void ep2_mul_glv_imp(ep2_t r, const ep2_t p, const bn_t k) {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		bn_free(n);
 		bn_free(u);
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			bn_free(_k[i]);
 			ep2_free(q[i]);
 		}
 
 	}
 }
 
 #endif /* EP_ENDOM */
 
 static void ep2_mul_naf_imp(ep2_t r, const ep2_t p, const bn_t k) {
-	int l, i, n;
+	size_t l, n;
 	int8_t naf[RLC_FP_BITS + 1];
 	ep2_t t[1 << (EP_WIDTH - 2)];
 
 	RLC_TRY {
 		/* Prepare the precomputation table. */
-		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
 			ep2_null(t[i]);
 			ep2_new(t[i]);
 		}
 		/* Compute the precomputation table. */
 		ep2_tab(t, p, EP_WIDTH);
@@ -129,11 +129,11 @@ static void ep2_mul_naf_imp(ep2_t r, const ep2_t p, const bn_t k) {
 		/* Compute the w-NAF representation of k. */
 		l = sizeof(naf);
 		bn_rec_naf(naf, &l, k, EP_WIDTH);
 
 		ep2_set_infty(r);
-		for (i = l - 1; i >= 0; i--) {
+		for (int i = l - 1; i >= 0; i--) {
 			ep2_dbl(r, r);
 
 			n = naf[i];
 			if (n > 0) {
 				ep2_add(r, r, t[n / 2]);
@@ -151,11 +151,11 @@ static void ep2_mul_naf_imp(ep2_t r, const ep2_t p, const bn_t k) {
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		/* Free the precomputation table. */
-		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
 			ep2_free(t[i]);
 		}
 	}
 }
 
@@ -164,11 +164,11 @@ static void ep2_mul_naf_imp(ep2_t r, const ep2_t p, const bn_t k) {
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
 void ep2_mul_basic(ep2_t r, const ep2_t p, const bn_t k) {
-	int i, l;
+	size_t l;
 	ep2_t t;
 
 	ep2_null(t);
 
 	if (bn_is_zero(k) || ep2_is_infty(p)) {
@@ -184,11 +184,11 @@ void ep2_mul_basic(ep2_t r, const ep2_t p, const bn_t k) {
 			ep2_copy(t, p);
 		} else {
 			ep2_set_infty(t);
 		}
 
-		for (i = l - 2; i >= 0; i--) {
+		for (int i = l - 2; i >= 0; i--) {
 			ep2_dbl(t, t);
 			if (bn_get_bit(k, i)) {
 				ep2_add(t, t, p);
 			}
 		}
@@ -209,22 +209,22 @@ void ep2_mul_basic(ep2_t r, const ep2_t p, const bn_t k) {
 
 #if EP_MUL == SLIDE || !defined(STRIP)
 
 void ep2_mul_slide(ep2_t r, const ep2_t p, const bn_t k) {
 	ep2_t t[1 << (EP_WIDTH - 1)], q;
-	int i, j, l;
 	uint8_t win[RLC_FP_BITS + 1];
+	size_t l;
 
 	ep2_null(q);
 
 	if (bn_is_zero(k) || ep2_is_infty(p)) {
 		ep2_set_infty(r);
 		return;
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
+		for (int i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
 			ep2_null(t[i]);
 			ep2_new(t[i]);
 		}
 
 		ep2_new(q);
@@ -235,26 +235,26 @@ void ep2_mul_slide(ep2_t r, const ep2_t p, const bn_t k) {
 #if defined(EP_MIXED)
 		ep2_norm(q, q);
 #endif
 
 		/* Create table. */
-		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep2_add(t[i], t[i - 1], q);
 		}
 
 #if defined(EP_MIXED)
 		ep2_norm_sim(t + 1, t + 1, (1 << (EP_WIDTH - 1)) - 1);
 #endif
 
 		ep2_set_infty(q);
 		l = RLC_FP_BITS + 1;
 		bn_rec_slw(win, &l, k, EP_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				ep2_dbl(q, q);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					ep2_dbl(q, q);
 				}
 				ep2_add(q, q, t[win[i] >> 1]);
 			}
 		}
@@ -266,11 +266,11 @@ void ep2_mul_slide(ep2_t r, const ep2_t p, const bn_t k) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep2_free(t[i]);
 		}
 		ep2_free(q);
 	}
 }
@@ -385,11 +385,11 @@ void ep2_mul_gen(ep2_t r, const bn_t k) {
 
 void ep2_mul_dig(ep2_t r, const ep2_t p, const dig_t k) {
 	ep2_t t;
 	bn_t _k;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	ep2_null(t);
 	bn_null(_k);
 
 	if (k == 0 || ep2_is_infty(p)) {
diff --git a/src/epx/relic_ep2_mul_fix.c b/src/epx/relic_ep2_mul_fix.c
index e2effd67..9ba36c39 100644
--- a/src/epx/relic_ep2_mul_fix.c
+++ b/src/epx/relic_ep2_mul_fix.c
@@ -45,12 +45,13 @@
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void ep2_mul_fix_plain(ep2_t r, const ep2_t *table, const bn_t k) {
-	int len, i, n;
 	int8_t naf[2 * RLC_FP_BITS + 1], *t;
+	size_t len;
+	int n;
 
 	if (bn_is_zero(k)) {
 		ep2_set_infty(r);
 		return;
 	}
@@ -59,11 +60,11 @@ static void ep2_mul_fix_plain(ep2_t r, const ep2_t *table, const bn_t k) {
 	len = 2 * RLC_FP_BITS + 1;
 	bn_rec_naf(naf, &len, k, EP_DEPTH);
 
 	t = naf + len - 1;
 	ep2_set_infty(r);
-	for (i = len - 1; i >= 0; i--, t--) {
+	for (int i = len - 1; i >= 0; i--, t--) {
 		ep2_dbl(r, r);
 
 		n = *t;
 		if (n > 0) {
 			ep2_add(r, r, table[n / 2]);
diff --git a/src/epx/relic_ep2_mul_sim.c b/src/epx/relic_ep2_mul_sim.c
index 0371975e..d2016d94 100644
--- a/src/epx/relic_ep2_mul_sim.c
+++ b/src/epx/relic_ep2_mul_sim.c
@@ -52,11 +52,11 @@
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
 		const bn_t m) {
-	int i, j, l, _l[4];
+	size_t l, _l[4];
 	bn_t _k[4], _m[4], n, u;
 	int8_t naf0[4][RLC_FP_BITS + 1];
 	int8_t naf1[4][RLC_FP_BITS + 1];
 	ep2_t _p[4], _q[4];
 
@@ -64,11 +64,11 @@ static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
 	bn_null(u);
 
 	RLC_TRY {
 		bn_new(n);
 		bn_new(u);
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			bn_null(_k[i]);
 			bn_new(_k[i]);
 			bn_null(_m[i]);
 			bn_new(_m[i]);
 			ep2_null(_p[i]);
@@ -92,11 +92,11 @@ static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
 		bn_rec_frb(_k, 4, _k[0], u, n, ep_curve_is_pairf() == EP_BN);
 		bn_mod(_m[0], m, n);
 		bn_rec_frb(_m, 4, _m[0], u, n, ep_curve_is_pairf() == EP_BN);
 
 		l = 0;
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			_l[i] = RLC_FP_BITS + 1;
 			bn_rec_naf(naf0[i], &_l[i], _k[i], 2);
 			if (bn_sign(_k[i]) == RLC_NEG) {
 				ep2_neg(_p[i], _p[i]);
 			}
@@ -108,13 +108,13 @@ static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
 			}
 			l = RLC_MAX(l, _l[i]);
 		}
 
 		ep2_set_infty(r);
-		for (i = l - 1; i >= 0; i--) {
+		for (int i = l - 1; i >= 0; i--) {
 			ep2_dbl(r, r);
-			for (j = 0; j < 4; j++) {
+			for (int j = 0; j < 4; j++) {
 				if (naf0[j][i] > 0) {
 					ep2_add(r, r, _p[j]);
 				}
 				if (naf0[j][i] < 0) {
 					ep2_sub(r, r, _p[j]);
@@ -133,11 +133,11 @@ static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
 	} RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	} RLC_FINALLY {
 		bn_free(n);
 		bn_free(u);
-		for (i = 0; i < 4; i++) {
+		for (int i = 0; i < 4; i++) {
 			bn_free(_k[i]);
 			bn_free(_m[i]);
 			ep2_free(_p[i]);
 			ep2_free(_q[i]);
 		}
@@ -160,14 +160,15 @@ static void ep2_mul_sim_endom(ep2_t r, const ep2_t p, const bn_t k, ep2_t q,
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ep2_mul_sim_plain(ep2_t r, const ep2_t p, const bn_t k,
 		const ep2_t q, const bn_t m, const ep2_t *t) {
-	int i, l, l0, l1, n0, n1, w, gen;
+	int i, n0, n1, w, gen;
 	int8_t naf0[2 * RLC_FP_BITS + 1], naf1[2 * RLC_FP_BITS + 1], *_k, *_m;
 	ep2_t t0[1 << (EP_WIDTH - 2)];
 	ep2_t t1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	RLC_TRY {
 		gen = (t == NULL ? 0 : 1);
 		if (!gen) {
 			for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
@@ -287,11 +288,11 @@ void ep2_mul_sim_trick(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
 		const bn_t m) {
 	ep2_t t0[1 << (EP_WIDTH / 2)];
 	ep2_t t1[1 << (EP_WIDTH / 2)];
 	ep2_t t[1 << EP_WIDTH];
 	bn_t n, _k, _m;
-	int l0, l1, w = EP_WIDTH / 2;
+	size_t l0, l1, w = EP_WIDTH / 2;
 	uint8_t w0[2 * RLC_FP_BITS], w1[2 * RLC_FP_BITS];
 
 	bn_null(n);
 	bn_null(_k);
 	bn_null(_m);
@@ -449,12 +450,13 @@ void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
 
 void ep2_mul_sim_joint(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
 		const bn_t m) {
 	bn_t n, _k, _m;
 	ep2_t t[5];
-	int i, l, u_i, offset;
+	int i, u_i, offset;
 	int8_t jsf[2 * (RLC_FP_BITS + 1)];
+	size_t l;
 
 	if (bn_is_zero(k) || ep2_is_infty(p)) {
 		ep2_mul(r, q, m);
 		return;
 	}
@@ -599,11 +601,11 @@ void ep2_mul_sim_gen(ep2_t r, const bn_t k, const ep2_t q, const bn_t m) {
 		bn_free(_k);
 		bn_free(_m);
 	}
 }
 
-void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], int len) {
+void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], size_t len) {
 	ep2_t t;
 	int max;
 
 	ep2_null(t);
 
@@ -633,15 +635,16 @@ void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], int len) {
 	RLC_FINALLY {
 		ep2_free(t);
 	}
 }
 
-void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], int n) {
-	const int len = RLC_FP_BITS + 1;
-	int i, j, m, l, _l[4];
+void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], size_t n) {
+	const size_t len = RLC_FP_BITS + 1;
+	int i, j, m;
 	bn_t _k[4], q, x;
 	int8_t ptr, *naf = RLC_ALLOCA(int8_t, 4 * n * len);
+	size_t l, _l[4];
 
 	if (n == 0) {
 		ep2_set_infty(r);
 		return;
 	}
diff --git a/src/epx/relic_ep2_util.c b/src/epx/relic_ep2_util.c
index 2df0fd6c..a7668243 100644
--- a/src/epx/relic_ep2_util.c
+++ b/src/epx/relic_ep2_util.c
@@ -241,11 +241,11 @@ int ep2_size_bin(const ep2_t a, int pack) {
 	}
 
 	return size;
 }
 
-void ep2_read_bin(ep2_t a, const uint8_t *bin, int len) {
+void ep2_read_bin(ep2_t a, const uint8_t *bin, size_t len) {
 	if (len == 1) {
 		if (bin[0] == 0) {
 			ep2_set_infty(a);
 			return;
 		} else {
@@ -291,11 +291,11 @@ void ep2_read_bin(ep2_t a, const uint8_t *bin, int len) {
 	if (!ep2_on_curve(a)) {
 		RLC_THROW(ERR_NO_VALID);
 	}
 }
 
-void ep2_write_bin(uint8_t *bin, int len, const ep2_t a, int pack) {
+void ep2_write_bin(uint8_t *bin, size_t len, const ep2_t a, int pack) {
 	ep2_t t;
 
 	ep2_null(t);
 
 	memset(bin, 0, len);
diff --git a/src/epx/relic_ep4_map.c b/src/epx/relic_ep4_map.c
index 4fc73ed8..6701e8fb 100644
--- a/src/epx/relic_ep4_map.c
+++ b/src/epx/relic_ep4_map.c
@@ -35,11 +35,11 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void ep4_map(ep4_t p, const uint8_t *msg, int len) {
+void ep4_map(ep4_t p, const uint8_t *msg, size_t len) {
 	bn_t x;
 	fp4_t t0;
 	uint8_t digest[RLC_MD_LEN];
 
 	bn_null(x);
diff --git a/src/epx/relic_ep4_mul.c b/src/epx/relic_ep4_mul.c
index ca871d90..6109da91 100644
--- a/src/epx/relic_ep4_mul.c
+++ b/src/epx/relic_ep4_mul.c
@@ -39,14 +39,15 @@
 #if EP_MUL == LWNAF || !defined(STRIP)
 
 #if defined(EP_ENDOM)
 
 static void ep4_mul_glv_imp(ep4_t r, const ep4_t p, const bn_t k) {
-	int sign, i, j, l, _l[8];
+	int sign, i, j;
 	bn_t n, _k[8], u, v;
 	int8_t naf[8][RLC_FP_BITS + 1];
 	ep4_t q[8];
+	size_t l, _l[8];
 
 	bn_null(n);
 	bn_null(u);
 	bn_null(v);
 
@@ -129,13 +130,14 @@ static void ep4_mul_glv_imp(ep4_t r, const ep4_t p, const bn_t k) {
 }
 
 #endif /* EP_ENDOM */
 
 static void ep4_mul_naf_imp(ep4_t r, const ep4_t p, const bn_t k) {
-	int l, i, n;
+	int i, n;
 	int8_t naf[RLC_FP_BITS + 1];
 	ep4_t t[1 << (EP_WIDTH - 2)];
+	size_t l;
 
 	RLC_TRY {
 		/* Prepare the precomputation table. */
 		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
 			ep4_null(t[i]);
@@ -227,22 +229,22 @@ void ep4_mul_basic(ep4_t r, const ep4_t p, const bn_t k) {
 
 #if EP_MUL == SLIDE || !defined(STRIP)
 
 void ep4_mul_slide(ep4_t r, const ep4_t p, const bn_t k) {
 	ep4_t t[1 << (EP_WIDTH - 1)], q;
-	int i, j, l;
 	uint8_t win[RLC_FP_BITS + 1];
+	size_t l;
 
 	ep4_null(q);
 
 	if (bn_is_zero(k) || ep4_is_infty(p)) {
 		ep4_set_infty(r);
 		return;
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
 			ep4_null(t[i]);
 			ep4_new(t[i]);
 		}
 
 		ep4_new(q);
@@ -253,26 +255,26 @@ void ep4_mul_slide(ep4_t r, const ep4_t p, const bn_t k) {
 #if defined(EP_MIXED)
 		ep4_norm(q, q);
 #endif
 
 		/* Create table. */
-		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep4_add(t[i], t[i - 1], q);
 		}
 
 #if defined(EP_MIXED)
 		ep4_norm_sim(t + 1, t + 1, (1 << (EP_WIDTH - 1)) - 1);
 #endif
 
 		ep4_set_infty(q);
 		l = RLC_FP_BITS + 1;
 		bn_rec_slw(win, &l, k, EP_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				ep4_dbl(q, q);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					ep4_dbl(q, q);
 				}
 				ep4_add(q, q, t[win[i] >> 1]);
 			}
 		}
@@ -284,11 +286,11 @@ void ep4_mul_slide(ep4_t r, const ep4_t p, const bn_t k) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
 			ep4_free(t[i]);
 		}
 		ep4_free(q);
 	}
 }
diff --git a/src/epx/relic_ep4_mul_fix.c b/src/epx/relic_ep4_mul_fix.c
index c9b7c20a..0f54510b 100644
--- a/src/epx/relic_ep4_mul_fix.c
+++ b/src/epx/relic_ep4_mul_fix.c
@@ -43,25 +43,23 @@
  *
  * @param[out] t				- the destination table.
  * @param[in] p					- the point to multiply.
  */
 static void ep4_mul_pre_ordin(ep4_t *t, const ep4_t p) {
-	int i;
-
 	ep4_dbl(t[0], p);
 #if defined(EP_MIXED)
 	ep4_norm(t[0], t[0]);
 #endif
 
 #if EP_DEPTH > 2
 	ep4_add(t[1], t[0], p);
-	for (i = 2; i < (1 << (EP_DEPTH - 2)); i++) {
+	for (int i = 2; i < (1 << (EP_DEPTH - 2)); i++) {
 		ep4_add(t[i], t[i - 1], t[0]);
 	}
 
 #if defined(EP_MIXED)
-	for (i = 1; i < (1 << (EP_DEPTH - 2)); i++) {
+	for (int i = 1; i < (1 << (EP_DEPTH - 2)); i++) {
 		ep4_norm(t[i], t[i]);
 	}
 #endif
 
 #endif
@@ -75,12 +73,13 @@ static void ep4_mul_pre_ordin(ep4_t *t, const ep4_t p) {
  * @param[out] r 				- the result.
  * @param[in] p					- the point to multiply.
  * @param[in] k					- the integer.
  */
 static void ep4_mul_fix_ordin(ep4_t r, const ep4_t *table, const bn_t k) {
-	int len, i, n;
 	int8_t naf[2 * RLC_FP_BITS + 1], *t;
+	size_t len;
+	int n;
 
 	if (bn_is_zero(k)) {
 		ep4_set_infty(r);
 		return;
 	}
@@ -89,11 +88,11 @@ static void ep4_mul_fix_ordin(ep4_t r, const ep4_t *table, const bn_t k) {
 	len = 2 * RLC_FP_BITS + 1;
 	bn_rec_naf(naf, &len, k, EP_DEPTH);
 
 	t = naf + len - 1;
 	ep4_set_infty(r);
-	for (i = len - 1; i >= 0; i--, t--) {
+	for (int i = len - 1; i >= 0; i--, t--) {
 		ep4_dbl(r, r);
 
 		n = *t;
 		if (n > 0) {
 			ep4_add(r, r, table[n / 2]);
diff --git a/src/epx/relic_ep4_mul_sim.c b/src/epx/relic_ep4_mul_sim.c
index fdc4cd57..0e0ae873 100644
--- a/src/epx/relic_ep4_mul_sim.c
+++ b/src/epx/relic_ep4_mul_sim.c
@@ -50,14 +50,15 @@
  * @param[in] m					- the second integer.
  * @param[in] t					- the pointer to the precomputed table.
  */
 static void ep4_mul_sim_plain(ep4_t r, const ep4_t p, const bn_t k,
 		const ep4_t q, const bn_t m, ep4_t *t) {
-	int i, l, l0, l1, n0, n1, w, gen;
+	int i, n0, n1, w, gen;
 	int8_t naf0[2 * RLC_FP_BITS + 1], naf1[2 * RLC_FP_BITS + 1], *_k, *_m;
 	ep4_t t0[1 << (EP_WIDTH - 2)];
 	ep4_t t1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	RLC_TRY {
 		gen = (t == NULL ? 0 : 1);
 		if (!gen) {
 			for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
@@ -175,11 +176,11 @@ void ep4_mul_sim_trick(ep4_t r, const ep4_t p, const bn_t k, const ep4_t q,
 		const bn_t m) {
 	ep4_t t0[1 << (EP_WIDTH / 2)];
 	ep4_t t1[1 << (EP_WIDTH / 2)];
 	ep4_t t[1 << EP_WIDTH];
 	bn_t n;
-	int l0, l1, w = EP_WIDTH / 2;
+	size_t l0, l1, w = EP_WIDTH / 2;
 	uint8_t w0[2 * RLC_FP_BITS], w1[2 * RLC_FP_BITS];
 
 	bn_null(n);
 
 	if (bn_is_zero(k) || ep4_is_infty(p)) {
@@ -291,12 +292,13 @@ void ep4_mul_sim_inter(ep4_t r, const ep4_t p, const bn_t k, const ep4_t q,
 #if EP_SIM == JOINT || !defined(STRIP)
 
 void ep4_mul_sim_joint(ep4_t r, const ep4_t p, const bn_t k, const ep4_t q,
 		const bn_t m) {
 	ep4_t t[5];
-	int i, l, u_i, offset;
+	int i, u_i, offset;
 	int8_t jsf[4 * (RLC_FP_BITS + 1)];
+	size_t l;
 
 	if (bn_is_zero(k) || ep4_is_infty(p)) {
 		ep4_mul(r, q, m);
 		return;
 	}
@@ -394,11 +396,11 @@ void ep4_mul_sim_gen(ep4_t r, const bn_t k, const ep4_t q, const bn_t m) {
 	RLC_FINALLY {
 		ep4_free(gen);
 	}
 }
 
-void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], int len) {
+void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], size_t len) {
 	ep4_t t;
 	int max;
 
 	ep4_null(t);
 
@@ -428,15 +430,16 @@ void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], int len) {
 	RLC_FINALLY {
 		ep4_free(t);
 	}
 }
 
-void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], int n) {
-	const int len = RLC_FP_BITS + 1;
-	int i, j, m, l, *_l = RLC_ALLOCA(int, 8 * n);
+void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], size_t n) {
+	const size_t len = RLC_FP_BITS + 1;
+	int i, j, m;
 	bn_t _k[8], q, x;
 	int8_t *naf = RLC_ALLOCA(int8_t, 8 * n * len);
+	size_t l, *_l = RLC_ALLOCA(size_t, 8 * n);
 
 	bn_null(q);
 	bn_null(x);
 
 	if (n <= 10) {
diff --git a/src/epx/relic_ep4_util.c b/src/epx/relic_ep4_util.c
index 51b1dafe..d7db36fe 100644
--- a/src/epx/relic_ep4_util.c
+++ b/src/epx/relic_ep4_util.c
@@ -252,11 +252,11 @@ int ep4_size_bin(const ep4_t a, int pack) {
 	}
 
 	return size;
 }
 
-void ep4_read_bin(ep4_t a, const uint8_t *bin, int len) {
+void ep4_read_bin(ep4_t a, const uint8_t *bin, size_t len) {
 	if (len == 1) {
 		if (bin[0] == 0) {
 			ep4_set_infty(a);
 			return;
 		} else {
@@ -286,11 +286,11 @@ void ep4_read_bin(ep4_t a, const uint8_t *bin, int len) {
 	if (!ep4_on_curve(a)) {
 		RLC_THROW(ERR_NO_VALID);
 	}
 }
 
-void ep4_write_bin(uint8_t *bin, int len, const ep4_t a, int pack) {
+void ep4_write_bin(uint8_t *bin, size_t len, const ep4_t a, int pack) {
 	ep4_t t;
 
 	ep4_null(t);
 
 	memset(bin, 0, len);
diff --git a/src/fb/relic_fb_exp.c b/src/fb/relic_fb_exp.c
index 6b3e3146..6ecb5f04 100644
--- a/src/fb/relic_fb_exp.c
+++ b/src/fb/relic_fb_exp.c
@@ -42,11 +42,11 @@
 /*============================================================================*/
 
 #if FB_EXP == BASIC || !defined(STRIP)
 
 void fb_exp_basic(fb_t c, const fb_t a, const bn_t b) {
-	int i, l;
+	size_t l;
 	fb_t r;
 
 	if (bn_is_zero(b)) {
 		fb_set_dig(c, 1);
 		return;
@@ -59,11 +59,11 @@ void fb_exp_basic(fb_t c, const fb_t a, const bn_t b) {
 
 		l = bn_bits(b);
 
 		fb_copy(r, a);
 
-		for (i = l - 2; i >= 0; i--) {
+		for (int i = l - 2; i >= 0; i--) {
 			fb_sqr(r, r);
 			if (bn_get_bit(b, i)) {
 				fb_mul(r, r, a);
 			}
 		}
@@ -86,48 +86,48 @@ void fb_exp_basic(fb_t c, const fb_t a, const bn_t b) {
 
 #if FB_EXP == SLIDE || !defined(STRIP)
 
 void fb_exp_slide(fb_t c, const fb_t a, const bn_t b) {
 	fb_t t[1 << (FB_WIDTH - 1)], r;
-	int i, j, l;
 	uint8_t win[RLC_FB_BITS + 1];
+	size_t l;
 
 	fb_null(r);
 
 	if (bn_is_zero(b)) {
 		fb_set_dig(c, 1);
 		return;
 	}
 
 
 	/* Initialize table. */
-	for (i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
+	for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
 		fb_null(t[i]);
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (FB_WIDTH - 1)); i ++) {
+		for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i ++) {
 			fb_new(t[i]);
 		}
 		fb_new(r);
 
 		fb_copy(t[0], a);
 		fb_sqr(r, a);
 
 		/* Create table. */
-		for (i = 1; i < 1 << (FB_WIDTH - 1); i++) {
+		for (size_t i = 1; i < 1 << (FB_WIDTH - 1); i++) {
 			fb_mul(t[i], t[i - 1], r);
 		}
 
 		fb_set_dig(r, 1);
 		l = RLC_FB_BITS + 1;
 		bn_rec_slw(win, &l, b, FB_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				fb_sqr(r, r);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					fb_sqr(r, r);
 				}
 				fb_mul(r, r, t[win[i] >> 1]);
 			}
 		}
@@ -140,11 +140,11 @@ void fb_exp_slide(fb_t c, const fb_t a, const bn_t b) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
 			fb_free(t[i]);
 		}
 		fb_free(r);
 	}
 }
diff --git a/src/fb/relic_fb_util.c b/src/fb/relic_fb_util.c
index b576c8ba..d84eb292 100644
--- a/src/fb/relic_fb_util.c
+++ b/src/fb/relic_fb_util.c
@@ -43,11 +43,11 @@
  * Checks if a radix is a power of two.
  *
  * @param[in] radix				- the radix to check.
  * @return if radix is a valid radix.
  */
-static int valid_radix(int radix) {
+static int valid_radix(unsigned int radix) {
 	while (radix > 0) {
 		if (radix != 1 && radix % 2 == 1)
 			return 0;
 		radix = radix / 2;
 	}
@@ -58,11 +58,11 @@ static int valid_radix(int radix) {
  * Computes the logarithm of a valid radix in basis two.
  *
  * @param[in] radix				- the valid radix.
  * @return the logarithm of the radix in basis two.
  */
-static int log_radix(int radix) {
+static int log_radix(unsigned int radix) {
 	int l = 0;
 
 	while (radix > 0) {
 		radix = radix / 2;
 		l++;
@@ -91,19 +91,19 @@ int fb_is_zero(const fb_t a) {
 	}
 
 	return !t;
 }
 
-int fb_get_bit(const fb_t a, int bit) {
+int fb_get_bit(const fb_t a, size_t bit) {
 	int d;
 
 	RLC_RIP(bit, d, bit);
 
 	return (a[d] >> bit) & 1;
 }
 
-void fb_set_bit(fb_t a, int bit, int value) {
+void fb_set_bit(fb_t a, size_t bit, int value) {
 	int d;
 	dig_t mask;
 
 	RLC_RIP(bit, d, bit);
 
@@ -114,11 +114,11 @@ void fb_set_bit(fb_t a, int bit, int value) {
 	} else {
 		a[d] &= ~mask;
 	}
 }
 
-int fb_bits(const fb_t a) {
+size_t fb_bits(const fb_t a) {
 	int i = RLC_FB_DIGS - 1;
 
 	while (i >= 0 && a[i] == 0) {
 		i--;
 	}
@@ -134,11 +134,11 @@ void fb_set_dig(fb_t c, dig_t a) {
 	fb_zero(c);
 	c[0] = a;
 }
 
 void fb_rand(fb_t a) {
-	int bits, digits;
+	size_t bits, digits;
 
 	rand_bytes((uint8_t *)a, RLC_FB_DIGS * sizeof(dig_t));
 
 	RLC_RIP(bits, digits, RLC_FB_BITS);
 	if (bits > 0) {
@@ -146,25 +146,23 @@ void fb_rand(fb_t a) {
 		a[RLC_FB_DIGS - 1] &= mask;
 	}
 }
 
 void fb_print(const fb_t a) {
-	int i;
-
 	/* Suppress possible unused parameter warning. */
 	(void)a;
-	for (i = RLC_FB_DIGS - 1; i > 0; i--) {
+	for (int i = RLC_FB_DIGS - 1; i > 0; i--) {
 		util_print_dig(a[i], 1);
 		util_print(" ");
 	}
 	util_print_dig(a[0], 1);
 	util_print("\n");
 }
 
-int fb_size_str(const fb_t a, int radix) {
+size_t fb_size_str(const fb_t a, unsigned int radix) {
 	bn_t t;
-	int digits = 0;
+	size_t digits = 0;
 
 	bn_null(t);
 
 	if (!valid_radix(radix)) {
 		RLC_THROW(ERR_NO_VALID);
@@ -186,11 +184,11 @@ int fb_size_str(const fb_t a, int radix) {
 	}
 
 	return digits;
 }
 
-void fb_read_str(fb_t a, const char *str, int len, int radix) {
+void fb_read_str(fb_t a, const char *str, size_t len, unsigned int radix) {
 	bn_t t;
 
 	bn_null(t);
 
 	if (!valid_radix(radix)) {
@@ -215,11 +213,11 @@ void fb_read_str(fb_t a, const char *str, int len, int radix) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void fb_write_str(char *str, int len, const fb_t a, int radix) {
+void fb_write_str(char *str, size_t len, const fb_t a, unsigned int radix) {
 	fb_t t;
 	int d, l, i, j;
 	char c;
 
 	fb_null(t);
@@ -274,11 +272,11 @@ void fb_write_str(char *str, int len, const fb_t a, int radix) {
 	RLC_FINALLY {
 		fb_free(t);
 	}
 }
 
-void fb_read_bin(fb_t a, const uint8_t *bin, int len) {
+void fb_read_bin(fb_t a, const uint8_t *bin, size_t len) {
 	bn_t t;
 
 	bn_null(t);
 
 	if (len != RLC_FB_BYTES) {
@@ -299,11 +297,11 @@ void fb_read_bin(fb_t a, const uint8_t *bin, int len) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void fb_write_bin(uint8_t *bin, int len, const fb_t a) {
+void fb_write_bin(uint8_t *bin, size_t len, const fb_t a) {
 	bn_t t;
 
 	bn_null(t);
 
 	if (len != RLC_FB_BYTES) {
diff --git a/src/fp/relic_fp_exp.c b/src/fp/relic_fp_exp.c
index 54cc11ca..9290689a 100644
--- a/src/fp/relic_fp_exp.c
+++ b/src/fp/relic_fp_exp.c
@@ -37,11 +37,11 @@
 /*============================================================================*/
 
 #if FP_EXP == BASIC || !defined(STRIP)
 
 void fp_exp_basic(fp_t c, const fp_t a, const bn_t b) {
-	int i, l;
+	size_t l;
 	fp_t r;
 
 	fp_null(r);
 
 	if (bn_is_zero(b)) {
@@ -54,11 +54,11 @@ void fp_exp_basic(fp_t c, const fp_t a, const bn_t b) {
 
 		l = bn_bits(b);
 
 		fp_copy(r, a);
 
-		for (i = l - 2; i >= 0; i--) {
+		for (int i = l - 2; i >= 0; i--) {
 			fp_sqr(r, r);
 			if (bn_get_bit(b, i)) {
 				fp_mul(r, r, a);
 			}
 		}
@@ -81,48 +81,48 @@ void fp_exp_basic(fp_t c, const fp_t a, const bn_t b) {
 
 #if FP_EXP == SLIDE || !defined(STRIP)
 
 void fp_exp_slide(fp_t c, const fp_t a, const bn_t b) {
 	fp_t t[1 << (FP_WIDTH - 1)], r;
-	int i, j, l;
 	uint8_t win[RLC_FP_BITS + 1];
+	size_t l;
 
 	fp_null(r);
 
 	if (bn_is_zero(b)) {
 		fp_set_dig(c, 1);
 		return;
 	}
 
 
 	/* Initialize table. */
-	for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
+	for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
 		fp_null(t[i]);
 	}
 
 	RLC_TRY {
-		for (i = 0; i < (1 << (FP_WIDTH - 1)); i ++) {
+		for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i ++) {
 			fp_new(t[i]);
 		}
 		fp_new(r);
 
 		fp_copy(t[0], a);
 		fp_sqr(r, a);
 
 		/* Create table. */
-		for (i = 1; i < 1 << (FP_WIDTH - 1); i++) {
+		for (size_t i = 1; i < 1 << (FP_WIDTH - 1); i++) {
 			fp_mul(t[i], t[i - 1], r);
 		}
 
 		fp_set_dig(r, 1);
 		l = RLC_FP_BITS + 1;
 		bn_rec_slw(win, &l, b, FP_WIDTH);
-		for (i = 0; i < l; i++) {
+		for (size_t i = 0; i < l; i++) {
 			if (win[i] == 0) {
 				fp_sqr(r, r);
 			} else {
-				for (j = 0; j < util_bits_dig(win[i]); j++) {
+				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
 					fp_sqr(r, r);
 				}
 				fp_mul(r, r, t[win[i] >> 1]);
 			}
 		}
@@ -135,11 +135,11 @@ void fp_exp_slide(fp_t c, const fp_t a, const bn_t b) {
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
-		for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
+		for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
 			fp_free(t[i]);
 		}
 		fp_free(r);
 	}
 }
diff --git a/src/fp/relic_fp_prime.c b/src/fp/relic_fp_prime.c
index 7cbdbcb2..1dbbf634 100644
--- a/src/fp/relic_fp_prime.c
+++ b/src/fp/relic_fp_prime.c
@@ -294,11 +294,11 @@ void fp_prime_set_dense(const bn_t p) {
 }
 
 void fp_prime_set_pairf(const bn_t x, int pairf) {
 	bn_t p, t0, t1;
 	ctx_t *ctx = core_get();
-	int len = bn_bits(x) + 1;
+	size_t len = bn_bits(x) + 1;
 	int8_t s[RLC_FP_BITS + 1];
 
 	bn_null(p);
 	bn_null(t0);
 	bn_null(t1);
@@ -457,11 +457,11 @@ void fp_prime_set_pairf(const bn_t x, int pairf) {
 		bn_free(t0);
 		bn_free(t1);
 	}
 }
 
-void fp_prime_set_pmers(const int *f, int len) {
+void fp_prime_set_pmers(const int *f, size_t len) {
 	bn_t p, t;
 
 	bn_null(p);
 	bn_null(t);
 
diff --git a/src/fp/relic_fp_util.c b/src/fp/relic_fp_util.c
index 84d2c50a..2ee18002 100644
--- a/src/fp/relic_fp_util.c
+++ b/src/fp/relic_fp_util.c
@@ -60,19 +60,19 @@ int fp_is_even(const fp_t a) {
 		return 1;
 	}
 	return 0;
 }
 
-int fp_get_bit(const fp_t a, int bit) {
+int fp_get_bit(const fp_t a, size_t bit) {
 	int d;
 
 	RLC_RIP(bit, d, bit);
 
 	return (a[d] >> bit) & 1;
 }
 
-void fp_set_bit(fp_t a, int bit, int value) {
+void fp_set_bit(fp_t a, size_t bit, int value) {
 	int d;
 	dig_t mask;
 
 	RLC_RIP(bit, d, bit);
 
@@ -83,11 +83,11 @@ void fp_set_bit(fp_t a, int bit, int value) {
 	} else {
 		a[d] &= ~mask;
 	}
 }
 
-int fp_bits(const fp_t a) {
+size_t fp_bits(const fp_t a) {
 	int i = RLC_FP_DIGS - 1;
 
 	while (i >= 0 && a[i] == 0) {
 		i--;
 	}
@@ -155,13 +155,13 @@ void fp_print(const fp_t a) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-int fp_size_str(const fp_t a, int radix) {
+size_t fp_size_str(const fp_t a, unsigned int radix) {
 	bn_t t;
-	int digits = 0;
+	size_t digits = 0;
 
 	bn_null(t);
 
 	RLC_TRY {
 		bn_new(t);
@@ -177,11 +177,11 @@ int fp_size_str(const fp_t a, int radix) {
 	}
 
 	return digits;
 }
 
-void fp_read_str(fp_t a, const char *str, int len, int radix) {
+void fp_read_str(fp_t a, const char *str, size_t len, unsigned int radix) {
 	bn_t t;
 
 	bn_null(t);
 
 	RLC_TRY {
@@ -206,11 +206,11 @@ void fp_read_str(fp_t a, const char *str, int len, int radix) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void fp_write_str(char *str, int len, const fp_t a, int radix) {
+void fp_write_str(char *str, size_t len, const fp_t a, unsigned int radix) {
 	bn_t t;
 
 	bn_null(t);
 
 	RLC_TRY {
@@ -225,11 +225,11 @@ void fp_write_str(char *str, int len, const fp_t a, int radix) {
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void fp_read_bin(fp_t a, const uint8_t *bin, int len) {
+void fp_read_bin(fp_t a, const uint8_t *bin, size_t len) {
 	bn_t t;
 
 	bn_null(t);
 
 	if (len != RLC_FP_BYTES) {
@@ -251,22 +251,22 @@ void fp_read_bin(fp_t a, const uint8_t *bin, int len) {
 				if (t->used == 1) {
 					fp_prime_conv_dig(a, t->dp[0]);
 				} else {
 					fp_prime_conv(a, t);
 				}
-			}	
+			}
 		}
 	}
 	RLC_CATCH_ANY {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		bn_free(t);
 	}
 }
 
-void fp_write_bin(uint8_t *bin, int len, const fp_t a) {
+void fp_write_bin(uint8_t *bin, size_t len, const fp_t a) {
 	bn_t t;
 
 	bn_null(t);
 
 	if (len != RLC_FP_BYTES) {
diff --git a/src/fpx/relic_fpx_cyc.c b/src/fpx/relic_fpx_cyc.c
index 459c195d..5d54a0fb 100644
--- a/src/fpx/relic_fpx_cyc.c
+++ b/src/fpx/relic_fpx_cyc.c
@@ -81,12 +81,12 @@ int fp2_test_cyc(const fp2_t a) {
 	return result;
 }
 
 void fp2_exp_cyc(fp2_t c, const fp2_t a, const bn_t b) {
 	fp2_t r, s, t[1 << (FP_WIDTH - 2)];
-	int i, l;
 	int8_t naf[RLC_FP_BITS + 1], *k;
+	size_t l;
 
 	if (bn_is_zero(b)) {
 		return fp2_set_dig(c, 1);
 	}
 
@@ -94,11 +94,11 @@ void fp2_exp_cyc(fp2_t c, const fp2_t a, const bn_t b) {
 	fp2_null(s);
 
 	RLC_TRY {
 		fp2_new(r);
 		fp2_new(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
 			fp2_null(t[i]);
 			fp2_new(t[i]);
 		}
 
 #if FP_WIDTH > 2
@@ -114,11 +114,11 @@ void fp2_exp_cyc(fp2_t c, const fp2_t a, const bn_t b) {
 		fp2_set_dig(r, 1);
 		bn_rec_naf(naf, &l, b, FP_WIDTH);
 
 		k = naf + l - 1;
 
-		for (i = l - 1; i >= 0; i--, k--) {
+		for (int i = l - 1; i >= 0; i--, k--) {
 			fp2_sqr(r, r);
 
 			if (*k > 0) {
 				fp2_mul(r, r, t[*k / 2]);
 			}
@@ -138,11 +138,11 @@ void fp2_exp_cyc(fp2_t c, const fp2_t a, const bn_t b) {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		fp2_free(r);
 		fp2_free(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
 			fp2_free(t[i]);
 		}
 	}
 }
 
@@ -191,12 +191,12 @@ int fp8_test_cyc(const fp8_t a) {
 	return result;
 }
 
 void fp8_exp_cyc(fp8_t c, const fp8_t a, const bn_t b) {
 	fp8_t r, s, t[1 << (FP_WIDTH - 2)];
-	int i, l;
 	int8_t naf[RLC_FP_BITS + 1], *k;
+	size_t l;
 
 	if (bn_is_zero(b)) {
 		return fp8_set_dig(c, 1);
 	}
 
@@ -204,11 +204,11 @@ void fp8_exp_cyc(fp8_t c, const fp8_t a, const bn_t b) {
 	fp8_null(s);
 
 	RLC_TRY {
 		fp8_new(r);
 		fp8_new(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
 			fp8_null(t[i]);
 			fp8_new(t[i]);
 		}
 
 #if FP_WIDTH > 2
@@ -224,11 +224,11 @@ void fp8_exp_cyc(fp8_t c, const fp8_t a, const bn_t b) {
 		fp8_set_dig(r, 1);
 		bn_rec_naf(naf, &l, b, FP_WIDTH);
 
 		k = naf + l - 1;
 
-		for (i = l - 1; i >= 0; i--, k--) {
+		for (int i = l - 1; i >= 0; i--, k--) {
 			fp8_sqr_cyc(r, r);
 
 			if (*k > 0) {
 				fp8_mul(r, r, t[*k / 2]);
 			}
@@ -248,11 +248,11 @@ void fp8_exp_cyc(fp8_t c, const fp8_t a, const bn_t b) {
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		fp8_free(r);
 		fp8_free(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
 			fp8_free(t[i]);
 		}
 	}
 }
 
@@ -468,29 +468,29 @@ void fp12_back_cyc_sim(fp12_t c[], const fp12_t a[], int n) {
 		RLC_FREE(t);
 	}
 }
 
 void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
-	int i, j, k, l, w = bn_ham(b);
+	size_t j, k, l, w = bn_ham(b);
 
 	if (bn_is_zero(b)) {
 		return fp12_set_dig(c, 1);
 	}
 
 	if ((bn_bits(b) > RLC_DIG) && ((w << 3) > bn_bits(b))) {
-		int _l[4];
+		size_t _l[4];
 		int8_t naf[4][RLC_FP_BITS + 1];
 		fp12_t t[4];
 		bn_t _b[4], n, u;
 
 		bn_null(n);
 		bn_null(u);
 
 		RLC_TRY {
 			bn_new(n);
 			bn_new(u);
-			for (i = 0; i < 4; i++) {
+			for (int i = 0; i < 4; i++) {
 				bn_null(_b[i]);
 				bn_new(_b[i]);
 				fp12_null(t[i]);
 				fp12_new(t[i]);
 			}
@@ -504,23 +504,23 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 				fp12_frb(t[1], t[0], 1);
 				fp12_frb(t[2], t[1], 1);
 				fp12_frb(t[3], t[2], 1);
 
 				l = 0;
-				for (i = 0; i < 4; i++) {
+				for (int i = 0; i < 4; i++) {
 					if (bn_sign(_b[i]) == RLC_NEG) {
 						fp12_inv_cyc(t[i], t[i]);
 					}
 					_l[i] = RLC_FP_BITS + 1;
 					bn_rec_naf(naf[i], &_l[i], _b[i], 2);
 					l = RLC_MAX(l, _l[i]);
 				}
 
 				fp12_set_dig(c, 1);
-				for (i = l - 1; i >= 0; i--) {
+				for (int i = l - 1; i >= 0; i--) {
 					fp12_sqr_cyc(c, c);
-					for (j = 0; j < 4; j++) {
+					for (int j = 0; j < 4; j++) {
 						if (naf[j][i] > 0) {
 							fp12_mul(c, c, t[j]);
 						}
 						if (naf[j][i] < 0) {
 							fp12_inv_cyc(t[j], t[j]);
@@ -530,11 +530,11 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 					}
 				}
 			} else {
 				fp12_copy(t[0], a);
 
-				for (i = bn_bits(b) - 2; i >= 0; i--) {
+				for (int i = bn_bits(b) - 2; i >= 0; i--) {
 					fp12_sqr_cyc(t[0], t[0]);
 					if (bn_get_bit(b, i)) {
 						fp12_mul(t[0], t[0], a);
 					}
 				}
@@ -549,11 +549,11 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 			RLC_THROW(ERR_CAUGHT);
 		}
 		RLC_FINALLY {
 			bn_free(n);
 			bn_free(u);
-			for (i = 0; i < 4; i++) {
+			for (int i = 0; i < 4; i++) {
 				bn_free(_b[i]);
 				fp12_free(t[i]);
 			}
 		}
 	} else {
@@ -563,19 +563,19 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 
 		RLC_TRY {
 			if (u == NULL) {
 				RLC_THROW(ERR_NO_MEMORY);
 			}
-			for (i = 0; i < w; i++) {
+			for (int i = 0; i < w; i++) {
 				fp12_null(u[i]);
 				fp12_new(u[i]);
 			}
 			fp12_new(t);
 
 			j = 0;
 			fp12_copy(t, a);
-			for (i = 1; i < bn_bits(b); i++) {
+			for (size_t i = 1; i < bn_bits(b); i++) {
 				fp12_sqr_pck(t, t);
 				if (bn_get_bit(b, i)) {
 					fp12_copy(u[j++], t);
 				}
 			}
@@ -594,11 +594,11 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 				fp12_copy(c, a);
 			} else {
 				fp12_copy(c, u[0]);
 			}
 
-			for (i = j; i < k; i++) {
+			for (size_t i = j; i < k; i++) {
 				fp12_mul(c, c, u[i]);
 			}
 
 			if (bn_sign(b) == RLC_NEG) {
 				fp12_inv_cyc(c, c);
@@ -606,24 +606,25 @@ void fp12_exp_cyc(fp12_t c, const fp12_t a, const bn_t b) {
 		}
 		RLC_CATCH_ANY {
 			RLC_THROW(ERR_CAUGHT);
 		}
 		RLC_FINALLY {
-			for (i = 0; i < w; i++) {
+			for (size_t i = 0; i < w; i++) {
 				fp12_free(u[i]);
 			}
 			fp12_free(t);
 			RLC_FREE(u);
 		}
 	}
 }
 
 void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const bn_t d) {
-	int i, l, n0, n1, l0, l1;
+	int n0, n1;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], *_k, *_m;
 	fp2_t r, t0[1 << (EP_WIDTH - 2)];
 	fp2_t s, t1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	if (bn_is_zero(b)) {
 		return fp2_exp_cyc(e, c, d);
 	}
 
@@ -635,11 +636,11 @@ void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const
 	fp2_null(s);
 
 	RLC_TRY {
 		fp2_new(r);
 		fp2_new(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
 			fp2_null(t0[i]);
 			fp2_null(t1[i]);
 			fp2_new(t0[i]);
 			fp2_new(t1[i]);
 		}
@@ -664,25 +665,25 @@ void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const
 		bn_rec_naf(naf0, &l0, b, FP_WIDTH);
 		bn_rec_naf(naf1, &l1, d, FP_WIDTH);
 
 		l = RLC_MAX(l0, l1);
 		if (bn_sign(b) == RLC_NEG) {
-			for (i = 0; i < l0; i++) {
+			for (size_t i = 0; i < l0; i++) {
 				naf0[i] = -naf0[i];
 			}
 		}
 		if (bn_sign(d) == RLC_NEG) {
-			for (i = 0; i < l1; i++) {
+			for (size_t i = 0; i < l1; i++) {
 				naf1[i] = -naf1[i];
 			}
 		}
 
 		_k = naf0 + l - 1;
 		_m = naf1 + l - 1;
 
 		fp2_set_dig(r, 1);
-		for (i = l - 1; i >= 0; i--, _k--, _m--) {
+		for (int i = l - 1; i >= 0; i--, _k--, _m--) {
 			fp2_sqr(r, r);
 
 			n0 = *_k;
 			n1 = *_m;
 
@@ -708,11 +709,11 @@ void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		fp2_free(r);
 		fp2_free(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
 			fp2_free(t0[i]);
 			fp2_free(t1[i]);
 		}
 	}
 }
@@ -818,12 +819,13 @@ void fp12_exp_cyc_sim(fp12_t e, const fp12_t a, const bn_t b, const fp12_t c, co
 			fp12_free(u[i]);
 		}
 	}
 }
 
-void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, int len, int sign) {
-	int i, j, k, w = len;
+void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, size_t len,
+		int sign) {
+	size_t i, j, k, w = len;
     fp12_t t, *u = RLC_ALLOCA(fp12_t, w);
 
 	if (len == 0) {
 		RLC_FREE(u);
 		fp12_set_dig(c, 1);
@@ -1095,30 +1097,30 @@ void fp24_back_cyc_sim(fp24_t c[], const fp24_t a[], int n) {
 		RLC_FREE(t);
 	}
 }
 
 void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
-	int i, j, k, w = bn_ham(b);
+	size_t j, k, w = bn_ham(b);
 
 	if (bn_is_zero(b)) {
 		fp24_set_dig(c, 1);
 		return;
 	}
 
 	if ((bn_bits(b) > RLC_DIG) && ((w << 3) > bn_bits(b))) {
-		int l, _l[8];
+		size_t l, _l[8];
 		int8_t naf[8][RLC_FP_BITS + 1];
 		fp24_t t[8];
 		bn_t _b[8], n, x;
 
 		bn_null(n);
 		bn_null(x);
 
 		RLC_TRY {
 			bn_new(n);
 			bn_new(x);
-			for (i = 0; i < 8; i++) {
+			for (int i = 0; i < 8; i++) {
 				bn_null(_b[i]);
 				bn_new(_b[i]);
 				fp24_null(t[i]);
 				fp24_new(t[i]);
 			}
@@ -1129,29 +1131,29 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 
 			if (ep_curve_is_pairf()) {
 				l = 0;
 
 				fp24_copy(t[0], a);
-				for (i = 0; i < 8; i++) {
+				for (int i = 0; i < 8; i++) {
 					_l[i] = RLC_FP_BITS + 1;
 					bn_rec_naf(naf[i], &_l[i], _b[i], 2);
 					l = RLC_MAX(l, _l[i]);
 					if (i > 0) {
 						fp24_frb(t[i], t[i - 1], 1);
 					}
 				}
 
-				for (i = 0; i < 8; i++) {
+				for (int i = 0; i < 8; i++) {
 					if (bn_sign(_b[i]) == RLC_NEG) {
 						fp24_inv_cyc(t[i], t[i]);
 					}
 				}
 
 				fp24_set_dig(c, 1);
-				for (i = l - 1; i >= 0; i--) {
+				for (int i = l - 1; i >= 0; i--) {
 					fp24_sqr_cyc(c, c);
-					for (j = 0; j < 8; j++) {
+					for (int j = 0; j < 8; j++) {
 						if (naf[j][i] > 0) {
 							fp24_mul(c, c, t[j]);
 						}
 						if (naf[j][i] < 0) {
 							fp24_inv_cyc(t[j], t[j]);
@@ -1161,11 +1163,11 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 					}
 				}
 			} else {
 				fp24_copy(t[0], a);
 
-				for (i = bn_bits(b) - 2; i >= 0; i--) {
+				for (int i = bn_bits(b) - 2; i >= 0; i--) {
 					fp24_sqr_cyc(t[0], t[0]);
 					if (bn_get_bit(b, i)) {
 						fp24_mul(t[0], t[0], a);
 					}
 				}
@@ -1180,11 +1182,11 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 			RLC_THROW(ERR_CAUGHT);
 		}
 		RLC_FINALLY {
 			bn_free(n);
 			bn_free(x);
-			for (i = 0; i < 8; i++) {
+			for (int i = 0; i < 8; i++) {
 				bn_free(_b[i]);
 				fp24_free(t[i]);
 			}
 		}
 	} else {
@@ -1194,19 +1196,19 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 
 		RLC_TRY {
 			if (u == NULL) {
 				RLC_THROW(ERR_NO_MEMORY);
 			}
-			for (i = 0; i < w; i++) {
+			for (size_t i = 0; i < w; i++) {
 				fp24_null(u[i]);
 				fp24_new(u[i]);
 			}
 			fp24_new(t);
 
 			j = 0;
 			fp24_copy(t, a);
-			for (i = 1; i < bn_bits(b); i++) {
+			for (size_t i = 1; i < bn_bits(b); i++) {
 				fp24_sqr_pck(t, t);
 				if (bn_get_bit(b, i)) {
 					fp24_copy(u[j++], t);
 				}
 			}
@@ -1225,11 +1227,11 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 				fp24_copy(c, a);
 			} else {
 				fp24_copy(c, u[0]);
 			}
 
-			for (i = j; i < k; i++) {
+			for (size_t i = j; i < k; i++) {
 				fp24_mul(c, c, u[i]);
 			}
 
 			if (bn_sign(b) == RLC_NEG) {
 				fp24_inv_cyc(c, c);
@@ -1237,24 +1239,25 @@ void fp24_exp_cyc(fp24_t c, const fp24_t a, const bn_t b) {
 		}
 		RLC_CATCH_ANY {
 			RLC_THROW(ERR_CAUGHT);
 		}
 		RLC_FINALLY {
-			for (i = 0; i < w; i++) {
+			for (size_t i = 0; i < w; i++) {
 				fp24_free(u[i]);
 			}
 			fp24_free(t);
 			RLC_FREE(u);
 		}
 	}
 }
 
 void fp24_exp_cyc_sim(fp24_t e, const fp24_t a, const bn_t b, const fp24_t c, const bn_t d) {
-	int i, l, n0, n1, l0, l1;
+	int n0, n1;
 	int8_t naf0[RLC_FP_BITS + 1], naf1[RLC_FP_BITS + 1], *_k, *_m;
 	fp24_t r, t0[1 << (EP_WIDTH - 2)];
 	fp24_t s, t1[1 << (EP_WIDTH - 2)];
+	size_t l, l0, l1;
 
 	if (bn_is_zero(b)) {
 		return fp24_exp_cyc(e, c, d);
 	}
 
@@ -1266,11 +1269,11 @@ void fp24_exp_cyc_sim(fp24_t e, const fp24_t a, const bn_t b, const fp24_t c, co
 	fp24_null(s);
 
 	RLC_TRY {
 		fp24_new(r);
 		fp24_new(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
 			fp24_null(t0[i]);
 			fp24_null(t1[i]);
 			fp24_new(t0[i]);
 			fp24_new(t1[i]);
 		}
@@ -1295,25 +1298,25 @@ void fp24_exp_cyc_sim(fp24_t e, const fp24_t a, const bn_t b, const fp24_t c, co
 		bn_rec_naf(naf0, &l0, b, FP_WIDTH);
 		bn_rec_naf(naf1, &l1, d, FP_WIDTH);
 
 		l = RLC_MAX(l0, l1);
 		if (bn_sign(b) == RLC_NEG) {
-			for (i = 0; i < l0; i++) {
+			for (size_t i = 0; i < l0; i++) {
 				naf0[i] = -naf0[i];
 			}
 		}
 		if (bn_sign(d) == RLC_NEG) {
-			for (i = 0; i < l1; i++) {
+			for (size_t i = 0; i < l1; i++) {
 				naf1[i] = -naf1[i];
 			}
 		}
 
 		_k = naf0 + l - 1;
 		_m = naf1 + l - 1;
 
 		fp24_set_dig(r, 1);
-		for (i = l - 1; i >= 0; i--, _k--, _m--) {
+		for (int i = l - 1; i >= 0; i--, _k--, _m--) {
 			fp24_sqr(r, r);
 
 			n0 = *_k;
 			n1 = *_m;
 
@@ -1339,19 +1342,20 @@ void fp24_exp_cyc_sim(fp24_t e, const fp24_t a, const bn_t b, const fp24_t c, co
 		RLC_THROW(ERR_CAUGHT);
 	}
 	RLC_FINALLY {
 		fp24_free(r);
 		fp24_free(s);
-		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
+		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
 			fp24_free(t0[i]);
 			fp24_free(t1[i]);
 		}
 	}
 }
 
-void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, int len, int sign) {
-	int i, j, k, w = len;
+void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, size_t len,
+		int sign) {
+	size_t i, j, k, w = len;
     fp24_t t, *u = RLC_ALLOCA(fp24_t, w);
 
 	if (len == 0) {
 		RLC_FREE(u);
 		fp24_set_dig(c, 1);
@@ -1717,12 +1721,13 @@ void fp48_exp_cyc(fp48_t c, const fp48_t a, const bn_t b) {
 			RLC_FREE(u);
 		}
 	}
 }
 
-void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, int len, int sign) {
-	int i, j, k, w = len;
+void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, size_t len,
+		int sign) {
+	size_t i, j, k, w = len;
     fp48_t t, *u = RLC_ALLOCA(fp48_t, w);
 
 	if (len == 0) {
 		RLC_FREE(u);
 		fp48_set_dig(c, 1);
@@ -2088,12 +2093,13 @@ void fp54_exp_cyc(fp54_t c, const fp54_t a, const bn_t b) {
 			RLC_FREE(u);
 		}
 	}
 }
 
-void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, int len, int sign) {
-	int i, j, k, w = len;
+void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, size_t len,
+		int sign) {
+	size_t i, j, k, w = len;
     fp54_t t, *u = RLC_ALLOCA(fp54_t, w);
 
 	if (len == 0) {
 		RLC_FREE(u);
 		fp54_set_dig(c, 1);
diff --git a/src/fpx/relic_fpx_exp.c b/src/fpx/relic_fpx_exp.c
index 02d4a62c..f586bdb8 100644
--- a/src/fpx/relic_fpx_exp.c
+++ b/src/fpx/relic_fpx_exp.c
@@ -327,11 +327,11 @@ void fp12_exp(fp12_t c, const fp12_t a, const bn_t b) {
 
 void fp12_exp_dig(fp12_t c, const fp12_t a, dig_t b) {
 	bn_t _b;
 	fp12_t t, v;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	if (b == 0) {
 		fp12_set_dig(c, 1);
 		return;
 	}
@@ -459,11 +459,11 @@ void fp24_exp(fp24_t c, const fp24_t a, const bn_t b) {
 
 void fp24_exp_dig(fp24_t c, const fp24_t a, dig_t b) {
 	bn_t _b;
 	fp24_t t, v;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	if (b == 0) {
 		fp24_set_dig(c, 1);
 		return;
 	}
@@ -559,11 +559,11 @@ void fp48_exp(fp48_t c, const fp48_t a, const bn_t b) {
 
 void fp48_exp_dig(fp48_t c, const fp48_t a, dig_t b) {
 	bn_t _b;
 	fp48_t t, v;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	if (b == 0) {
 		fp48_set_dig(c, 1);
 		return;
 	}
@@ -655,11 +655,11 @@ void fp54_exp(fp54_t c, const fp54_t a, const bn_t b) {
 
 void fp54_exp_dig(fp54_t c, const fp54_t a, dig_t b) {
 	bn_t _b;
 	fp54_t t, v;
 	int8_t u, naf[RLC_DIG + 1];
-	int l;
+	size_t l;
 
 	if (b == 0) {
 		fp54_set_dig(c, 1);
 		return;
 	}
diff --git a/src/fpx/relic_fpx_util.c b/src/fpx/relic_fpx_util.c
index 7f0c9459..168142bb 100644
--- a/src/fpx/relic_fpx_util.c
+++ b/src/fpx/relic_fpx_util.c
@@ -70,11 +70,11 @@ int fp2_size_bin(fp2_t a, int pack) {
 	} else {
 		return 2 * RLC_FP_BYTES;
 	}
 }
 
-void fp2_read_bin(fp2_t a, const uint8_t *bin, int len) {
+void fp2_read_bin(fp2_t a, const uint8_t *bin, size_t len) {
 	if (len != RLC_FP_BYTES + 1 && len != 2 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	if (len == RLC_FP_BYTES + 1) {
@@ -87,11 +87,11 @@ void fp2_read_bin(fp2_t a, const uint8_t *bin, int len) {
 		fp_read_bin(a[0], bin, RLC_FP_BYTES);
 		fp_read_bin(a[1], bin + RLC_FP_BYTES, RLC_FP_BYTES);
 	}
 }
 
-void fp2_write_bin(uint8_t *bin, int len, const fp2_t a, int pack) {
+void fp2_write_bin(uint8_t *bin, size_t len, const fp2_t a, int pack) {
 	fp2_t t;
 
 	fp2_null(t);
 
 	RLC_TRY {
@@ -158,21 +158,21 @@ void fp3_print(const fp3_t a) {
 
 int fp3_size_bin(fp3_t a) {
 	return 3 * RLC_FP_BYTES;
 }
 
-void fp3_read_bin(fp3_t a, const uint8_t *bin, int len) {
+void fp3_read_bin(fp3_t a, const uint8_t *bin, size_t len) {
 	if (len != 3 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp_read_bin(a[0], bin, RLC_FP_BYTES);
 	fp_read_bin(a[1], bin + RLC_FP_BYTES, RLC_FP_BYTES);
 	fp_read_bin(a[2], bin + 2 * RLC_FP_BYTES, RLC_FP_BYTES);
 }
 
-void fp3_write_bin(uint8_t *bin, int len, const fp3_t a) {
+void fp3_write_bin(uint8_t *bin, size_t len, const fp3_t a) {
 	if (len != 3 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp_write_bin(bin, RLC_FP_BYTES, a[0]);
@@ -212,20 +212,20 @@ void fp4_print(const fp4_t a) {
 
 int fp4_size_bin(fp4_t a) {
 	return 4 * RLC_FP_BYTES;
 }
 
-void fp4_read_bin(fp4_t a, const uint8_t *bin, int len) {
+void fp4_read_bin(fp4_t a, const uint8_t *bin, size_t len) {
 	if (len != 4 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp2_read_bin(a[0], bin, 2 * RLC_FP_BYTES);
 	fp2_read_bin(a[1], bin + 2 * RLC_FP_BYTES, 2 * RLC_FP_BYTES);
 }
 
-void fp4_write_bin(uint8_t *bin, int len, const fp4_t a) {
+void fp4_write_bin(uint8_t *bin, size_t len, const fp4_t a) {
 	if (len != 4 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp2_write_bin(bin, 2 * RLC_FP_BYTES, a[0], 0);
@@ -267,21 +267,21 @@ void fp6_print(const fp6_t a) {
 
 int fp6_size_bin(fp6_t a) {
 	return 6 * RLC_FP_BYTES;
 }
 
-void fp6_read_bin(fp6_t a, const uint8_t *bin, int len) {
+void fp6_read_bin(fp6_t a, const uint8_t *bin, size_t len) {
 	if (len != 6 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp2_read_bin(a[0], bin, 2 * RLC_FP_BYTES);
 	fp2_read_bin(a[1], bin + 2 * RLC_FP_BYTES, 2 * RLC_FP_BYTES);
 	fp2_read_bin(a[2], bin + 4 * RLC_FP_BYTES, 2 * RLC_FP_BYTES);
 }
 
-void fp6_write_bin(uint8_t *bin, int len, const fp6_t a) {
+void fp6_write_bin(uint8_t *bin, size_t len, const fp6_t a) {
 	if (len != 6 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp2_write_bin(bin, 2 * RLC_FP_BYTES, a[0], 0);
@@ -329,20 +329,20 @@ int fp8_size_bin(fp8_t a, int pack) {
 	} else {
 		return 8 * RLC_FP_BYTES;
 	}
 }
 
-void fp8_read_bin(fp8_t a, const uint8_t *bin, int len) {
+void fp8_read_bin(fp8_t a, const uint8_t *bin, size_t len) {
 	if (len != 8 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp4_read_bin(a[0], bin, 4 * RLC_FP_BYTES);
 	fp4_read_bin(a[1], bin + 4 * RLC_FP_BYTES, 4 * RLC_FP_BYTES);
 }
 
-void fp8_write_bin(uint8_t *bin, int len, const fp8_t a) {
+void fp8_write_bin(uint8_t *bin, size_t len, const fp8_t a) {
 	if (len != 8 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp4_write_bin(bin, 4 * RLC_FP_BYTES, a[0]);
@@ -384,21 +384,21 @@ void fp9_print(const fp9_t a) {
 
 int fp9_size_bin(fp9_t a) {
 	return 9 * RLC_FP_BYTES;
 }
 
-void fp9_read_bin(fp9_t a, const uint8_t *bin, int len) {
+void fp9_read_bin(fp9_t a, const uint8_t *bin, size_t len) {
 	if (len != 9 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp3_read_bin(a[0], bin, 3 * RLC_FP_BYTES);
 	fp3_read_bin(a[1], bin + 3 * RLC_FP_BYTES, 3 * RLC_FP_BYTES);
 	fp3_read_bin(a[2], bin + 6 * RLC_FP_BYTES, 3 * RLC_FP_BYTES);
 }
 
-void fp9_write_bin(uint8_t *bin, int len, const fp9_t a) {
+void fp9_write_bin(uint8_t *bin, size_t len, const fp9_t a) {
 	if (len != 9 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp3_write_bin(bin, 3 * RLC_FP_BYTES, a[0]);
@@ -446,11 +446,11 @@ int fp12_size_bin(fp12_t a, int pack) {
 	} else {
 		return 12 * RLC_FP_BYTES;
 	}
 }
 
-void fp12_read_bin(fp12_t a, const uint8_t *bin, int len) {
+void fp12_read_bin(fp12_t a, const uint8_t *bin, size_t len) {
 	if (len != 8 * RLC_FP_BYTES && len != 12 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	if (len == 8 * RLC_FP_BYTES) {
@@ -466,11 +466,11 @@ void fp12_read_bin(fp12_t a, const uint8_t *bin, int len) {
 		fp6_read_bin(a[0], bin, 6 * RLC_FP_BYTES);
 		fp6_read_bin(a[1], bin + 6 * RLC_FP_BYTES, 6 * RLC_FP_BYTES);
 	}
 }
 
-void fp12_write_bin(uint8_t *bin, int len, const fp12_t a, int pack) {
+void fp12_write_bin(uint8_t *bin, size_t len, const fp12_t a, int pack) {
 	fp12_t t;
 
 	fp12_null(t);
 
 	RLC_TRY {
@@ -530,20 +530,20 @@ void fp18_print(const fp18_t a) {
 
 int fp18_size_bin(fp18_t a) {
 	return 18 * RLC_FP_BYTES;
 }
 
-void fp18_read_bin(fp18_t a, const uint8_t *bin, int len) {
+void fp18_read_bin(fp18_t a, const uint8_t *bin, size_t len) {
 	if (len != 18 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp9_read_bin(a[0], bin, 9 * RLC_FP_BYTES);
 	fp9_read_bin(a[1], bin + 9 * RLC_FP_BYTES, 9 * RLC_FP_BYTES);
 }
 
-void fp18_write_bin(uint8_t *bin, int len, const fp18_t a) {
+void fp18_write_bin(uint8_t *bin, size_t len, const fp18_t a) {
 	if (len != 18 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	fp9_write_bin(bin, 9 * RLC_FP_BYTES, a[0]);
@@ -593,11 +593,11 @@ int fp24_size_bin(fp24_t a, int pack) {
 	} else {
 		return 24 * RLC_FP_BYTES;
 	}
 }
 
-void fp24_read_bin(fp24_t a, const uint8_t *bin, int len) {
+void fp24_read_bin(fp24_t a, const uint8_t *bin, size_t len) {
 	if (len != 16 * RLC_FP_BYTES && len != 24 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	if (len == 16 * RLC_FP_BYTES) {
@@ -614,11 +614,11 @@ void fp24_read_bin(fp24_t a, const uint8_t *bin, int len) {
 		fp8_read_bin(a[1], bin + 8 * RLC_FP_BYTES, 8 * RLC_FP_BYTES);
 		fp8_read_bin(a[2], bin + 16 * RLC_FP_BYTES, 8 * RLC_FP_BYTES);
 	}
 }
 
-void fp24_write_bin(uint8_t *bin, int len, const fp24_t a, int pack) {
+void fp24_write_bin(uint8_t *bin, size_t len, const fp24_t a, int pack) {
 	fp24_t t;
 
 	fp24_null(t);
 
 	RLC_TRY {
@@ -688,11 +688,11 @@ int fp48_size_bin(fp48_t a, int pack) {
 	} else {
 		return 48 * RLC_FP_BYTES;
 	}
 }
 
-void fp48_read_bin(fp48_t a, const uint8_t *bin, int len) {
+void fp48_read_bin(fp48_t a, const uint8_t *bin, size_t len) {
 	if (len != 32 * RLC_FP_BYTES && len != 48 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	if (len == 32 * RLC_FP_BYTES) {
@@ -708,11 +708,11 @@ void fp48_read_bin(fp48_t a, const uint8_t *bin, int len) {
 		fp24_read_bin(a[0], bin, 24 * RLC_FP_BYTES);
 		fp24_read_bin(a[1], bin + 24 * RLC_FP_BYTES, 24 * RLC_FP_BYTES);
 	}
 }
 
-void fp48_write_bin(uint8_t *bin, int len, const fp48_t a, int pack) {
+void fp48_write_bin(uint8_t *bin, size_t len, const fp48_t a, int pack) {
 	fp48_t t;
 
 	fp48_null(t);
 
 	RLC_TRY {
@@ -784,11 +784,11 @@ int fp54_size_bin(fp54_t a, int pack) {
 	} else {
 		return 54 * RLC_FP_BYTES;
 	}
 }
 
-void fp54_read_bin(fp54_t a, const uint8_t *bin, int len) {
+void fp54_read_bin(fp54_t a, const uint8_t *bin, size_t len) {
 	if (len != 36 * RLC_FP_BYTES && len != 54 * RLC_FP_BYTES) {
 		RLC_THROW(ERR_NO_BUFFER);
 		return;
 	}
 	if (len == 36 * RLC_FP_BYTES) {
@@ -805,11 +805,11 @@ void fp54_read_bin(fp54_t a, const uint8_t *bin, int len) {
 		fp18_read_bin(a[1], bin + 18 * RLC_FP_BYTES, 18 * RLC_FP_BYTES);
 		fp18_read_bin(a[2], bin + 36 * RLC_FP_BYTES, 18 * RLC_FP_BYTES);
 	}
 }
 
-void fp54_write_bin(uint8_t *bin, int len, const fp54_t a, int pack) {
+void fp54_write_bin(uint8_t *bin, size_t len, const fp54_t a, int pack) {
 	fp54_t t;
 
 	fp54_null(t);
 
 	RLC_TRY {
diff --git a/src/md/relic_md_blake2s.c b/src/md/relic_md_blake2s.c
index cf0b79b5..2ba759ad 100644
--- a/src/md/relic_md_blake2s.c
+++ b/src/md/relic_md_blake2s.c
@@ -41,20 +41,20 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if MD_MAP == B2S160 || !defined(STRIP)
 
-void md_map_b2s160(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_b2s160(uint8_t *hash, const uint8_t *msg, size_t len) {
 	memset(hash, 0, RLC_MD_LEN_B2S160);
 	blake2s(hash, RLC_MD_LEN_B2S160, msg, len, NULL, 0);
 }
 
 #endif
 
 #if MD_MAP == B2S256 || !defined(STRIP)
 
-void md_map_b2s256(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_b2s256(uint8_t *hash, const uint8_t *msg, size_t len) {
 	memset(hash, 0, RLC_MD_LEN_B2S256);
 	blake2s(hash, RLC_MD_LEN_B2S256, msg, len, NULL, 0);
 }
 
 #endif
diff --git a/src/md/relic_md_hmac.c b/src/md/relic_md_hmac.c
index 00b7e63b..0d524f27 100644
--- a/src/md/relic_md_hmac.c
+++ b/src/md/relic_md_hmac.c
@@ -38,12 +38,12 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void md_hmac(uint8_t *mac, const uint8_t *in, int in_len, const uint8_t *key,
-    int key_len) {
+void md_hmac(uint8_t *mac, const uint8_t *in, size_t in_len, const uint8_t *key,
+    size_t key_len) {
 #if MD_MAP == SH224 || MD_MAP == SH256 || MD_MAP == B2S160 || MD_MAP == B2S256
   #define block_size 64
 #elif MD_MAP == SH384 || MD_MAP == SH512
   #define block_size  128
 #endif
diff --git a/src/md/relic_md_kdf.c b/src/md/relic_md_kdf.c
index 47c42cfb..0c3df427 100644
--- a/src/md/relic_md_kdf.c
+++ b/src/md/relic_md_kdf.c
@@ -38,12 +38,11 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void md_kdf(uint8_t *key, int key_len, const uint8_t *in,
-		int in_len) {
+void md_kdf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len) {
 	uint32_t i, j, d;
 	uint8_t* buffer = RLC_ALLOCA(uint8_t, in_len + sizeof(uint32_t));
 	uint8_t* t = RLC_ALLOCA(uint8_t, key_len + RLC_MD_LEN);
 	if (buffer == NULL || t == NULL) {
 		RLC_FREE(buffer);
diff --git a/src/md/relic_md_mgf.c b/src/md/relic_md_mgf.c
index 1c773763..100f66a3 100644
--- a/src/md/relic_md_mgf.c
+++ b/src/md/relic_md_mgf.c
@@ -38,12 +38,11 @@
 
 /*============================================================================*/
 /* Public definitions                                                         */
 /*============================================================================*/
 
-void md_mgf(uint8_t *key, int key_len, const uint8_t *in,
-		int in_len) {
+void md_mgf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len) {
 	uint32_t i, j, d;
 	uint8_t *buffer = RLC_ALLOCA(uint8_t, in_len + sizeof(uint32_t));
 	uint8_t *t = RLC_ALLOCA(uint8_t, key_len + RLC_MD_LEN);
 
 	if (buffer == NULL || t == NULL) {
diff --git a/src/md/relic_md_sha224.c b/src/md/relic_md_sha224.c
index 7009f00a..67989b3d 100644
--- a/src/md/relic_md_sha224.c
+++ b/src/md/relic_md_sha224.c
@@ -38,11 +38,11 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if MD_MAP == SH224 || !defined(STRIP)
 
-void md_map_sh224(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_sh224(uint8_t *hash, const uint8_t *msg, size_t len) {
 	SHA224Context ctx;
 
 	if (SHA224Reset(&ctx) != shaSuccess) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
diff --git a/src/md/relic_md_sha256.c b/src/md/relic_md_sha256.c
index 2e589cb7..06db3487 100644
--- a/src/md/relic_md_sha256.c
+++ b/src/md/relic_md_sha256.c
@@ -38,11 +38,11 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if MD_MAP == SH256 || !defined(STRIP)
 
-void md_map_sh256(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_sh256(uint8_t *hash, const uint8_t *msg, size_t len) {
 	SHA256Context ctx;
 
 	if (SHA256Reset(&ctx) != shaSuccess) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
diff --git a/src/md/relic_md_sha384.c b/src/md/relic_md_sha384.c
index cc93c8b2..5b4cddc2 100644
--- a/src/md/relic_md_sha384.c
+++ b/src/md/relic_md_sha384.c
@@ -38,11 +38,11 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if MD_MAP == SH384 || !defined(STRIP)
 
-void md_map_sh384(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_sh384(uint8_t *hash, const uint8_t *msg, size_t len) {
 	SHA384Context ctx;
 
 	if (SHA384Reset(&ctx) != shaSuccess) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
diff --git a/src/md/relic_md_sha512.c b/src/md/relic_md_sha512.c
index 5d2cd3b3..329fda86 100644
--- a/src/md/relic_md_sha512.c
+++ b/src/md/relic_md_sha512.c
@@ -38,11 +38,11 @@
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if MD_MAP == SH512 || !defined(STRIP)
 
-void md_map_sh512(uint8_t *hash, const uint8_t *msg, int len) {
+void md_map_sh512(uint8_t *hash, const uint8_t *msg, size_t len) {
 	SHA512Context ctx;
 
 	if (SHA512Reset(&ctx) != shaSuccess) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
diff --git a/src/pp/relic_pp_map_k12.c b/src/pp/relic_pp_map_k12.c
index 4f228763..2446a083 100644
--- a/src/pp/relic_pp_map_k12.c
+++ b/src/pp/relic_pp_map_k12.c
@@ -50,11 +50,12 @@
  */
 static void pp_mil_k12(fp12_t r, ep2_t *t, ep2_t *q, ep_t *p, int m, bn_t a) {
 	fp12_t l;
 	ep_t *_p = RLC_ALLOCA(ep_t, m);
 	ep2_t *_q = RLC_ALLOCA(ep2_t, m);
-	int i, j, len = bn_bits(a) + 1;
+	size_t len = bn_bits(a) + 1;
+	int i, j;
 	int8_t s[RLC_FP_BITS + 1];
 
 	if (m == 0) {
 		return;
 	}
diff --git a/src/pp/relic_pp_map_k24.c b/src/pp/relic_pp_map_k24.c
index 26ebe9cd..91701eee 100644
--- a/src/pp/relic_pp_map_k24.c
+++ b/src/pp/relic_pp_map_k24.c
@@ -50,11 +50,12 @@
  */
 static void pp_mil_k24(fp24_t r, ep4_t *t, ep4_t *q, ep_t *p, int m, bn_t a) {
 	fp24_t l;
 	ep_t *_p = RLC_ALLOCA(ep_t, m);
 	ep4_t *_q = RLC_ALLOCA(ep4_t, m);
-	int i, j, len = bn_bits(a) + 1;
+	size_t len = bn_bits(a) + 1;
+	int i, j;
 	int8_t s[RLC_FP_BITS + 1];
 
 	if (m == 0) {
 		return;
 	}
diff --git a/src/pp/relic_pp_map_k48.c b/src/pp/relic_pp_map_k48.c
index e17d28c7..e47e5028 100644
--- a/src/pp/relic_pp_map_k48.c
+++ b/src/pp/relic_pp_map_k48.c
@@ -40,11 +40,12 @@
 static void pp_mil_k48(fp48_t r, const fp8_t qx, const fp8_t qy, const ep_t p,
 		const bn_t a) {
 	fp48_t l;
 	ep_t _p;
 	fp8_t rx, ry, rz, qn;
-	int i, len = bn_bits(a) + 1;
+	size_t len = bn_bits(a) + 1;
+	int i;
 	int8_t s[RLC_FP_BITS + 1];
 
 	fp48_null(l);
 	ep_null(_p);
 	fp8_null(rx);
diff --git a/src/pp/relic_pp_map_k54.c b/src/pp/relic_pp_map_k54.c
index f5184d54..4f869cf3 100644
--- a/src/pp/relic_pp_map_k54.c
+++ b/src/pp/relic_pp_map_k54.c
@@ -40,11 +40,12 @@
 static void pp_mil_k54(fp54_t r, const fp9_t qx, const fp9_t qy, const ep_t p,
 		const bn_t a) {
 	fp54_t l;
 	ep_t _p;
 	fp9_t rx, ry, rz, sx, sy, sz, qn;
-	int i, len = bn_bits(a) + 1;
+	size_t len = bn_bits(a) + 1;
+	int i;
 	int8_t s[RLC_FP_BITS + 1];
 
 	fp54_null(l);
 	ep_null(_p);
 	fp9_null(rx);
diff --git a/src/pp/relic_pp_map_k8.c b/src/pp/relic_pp_map_k8.c
index 28e42a71..0d5e2ee8 100644
--- a/src/pp/relic_pp_map_k8.c
+++ b/src/pp/relic_pp_map_k8.c
@@ -51,11 +51,12 @@
  */
 static void pp_mil_k8(fp8_t r, ep2_t *t, ep2_t *q, ep_t *p, int m, bn_t a) {
 	fp8_t l;
 	ep_t *_p = RLC_ALLOCA(ep_t, m);
 	ep2_t *_q = RLC_ALLOCA(ep2_t, m);
-	int i, j, len = bn_bits(a) + 1;
+	size_t len = bn_bits(a) + 1;
+	int i, j;
 	int8_t s[RLC_FP_BITS + 1];
 
 	if (m == 0) {
 		return;
 	}
diff --git a/src/rand/relic_rand_core.c b/src/rand/relic_rand_core.c
index 10f8929f..c21c5a90 100644
--- a/src/rand/relic_rand_core.c
+++ b/src/rand/relic_rand_core.c
@@ -181,11 +181,11 @@ void rand_init(void) {
 #else
 	rand_seed(NULL, NULL);
 #endif
 }
 
-int rand_check(uint8_t *buf, int size) {
+int rand_check(uint8_t *buf, size_t size) {
 	int count = 0;
 
 	for (int i = 1; i < size; i++) {
 		if (buf[i] == buf[i - 1]) {
 			count++;
diff --git a/src/rand/relic_rand_hashd.c b/src/rand/relic_rand_hashd.c
index f91414a3..0b0b753d 100644
--- a/src/rand/relic_rand_hashd.c
+++ b/src/rand/relic_rand_hashd.c
@@ -51,13 +51,14 @@
  * param[out] out       - the result.
  * param[in] out_len    - the number of bytes to return.
  * param[in] in         - the input string.
  * param[in] in_len     - the number of bytes in the input.
  */
-static void rand_hash(uint8_t *out, int out_len, uint8_t *in, int in_len) {
+static void rand_hash(uint8_t *out, size_t out_len, uint8_t *in,
+		size_t in_len) {
 	uint32_t j = util_conv_big(8 * out_len);
-	int len = RLC_CEIL(out_len, RLC_MD_LEN);
+	size_t len = RLC_CEIL(out_len, RLC_MD_LEN);
 	uint8_t* buf = RLC_ALLOCA(uint8_t, 1 + sizeof(uint32_t) + in_len);
 	uint8_t hash[RLC_MD_LEN];
 
 	if (buf == NULL) {
 		RLC_THROW(ERR_NO_MEMORY);
@@ -86,11 +87,11 @@ static void rand_hash(uint8_t *out, int out_len, uint8_t *in, int in_len) {
  * Accumulates a small integer in the internal state.
  *
  * @param[in,out] state		- the internal state.
  * @param[in] digit			- the small integer.
  */
-static int rand_inc(uint8_t *data, int size, int digit) {
+static int rand_inc(uint8_t *data, size_t size, int digit) {
 	int carry = digit;
 	for (int i = size - 1; i >= 0; i--) {
 		int16_t s;
 		s = (data[i] + carry);
 		data[i] = s & 0xFF;
@@ -103,11 +104,11 @@ static int rand_inc(uint8_t *data, int size, int digit) {
  * Accumulates the hash value in the internal state.
  *
  * @param[in,out] state		- the internal state.
  * @param[in] hash			- the hash value.
  */
-static int rand_add(uint8_t *state, uint8_t *hash, int size) {
+static int rand_add(uint8_t *state, uint8_t *hash, size_t size) {
 	int carry = 0;
 	for (int i = size - 1; i >= 0; i--) {
 		/* Make sure carries are detected. */
 		int16_t s;
 		s = (state[i] + hash[i] + carry);
@@ -121,11 +122,11 @@ static int rand_add(uint8_t *state, uint8_t *hash, int size) {
  * Generates pseudo-random bytes by iterating the hash function.
  *
  * @param[out] out 			- the buffer to write.
  * @param[in] out_len		- the number of bytes to write.
  */
-static void rand_gen(uint8_t *out, int out_len) {
+static void rand_gen(uint8_t *out, size_t out_len) {
 	int m = RLC_CEIL(out_len, RLC_MD_LEN);
 	uint8_t hash[RLC_MD_LEN], data[(RLC_RAND_SIZE - 1)/2];
 	ctx_t *ctx = core_get();
 
 	/* data = V */
@@ -148,11 +149,11 @@ static void rand_gen(uint8_t *out, int out_len) {
 /* Public definitions                                                         */
 /*============================================================================*/
 
 #if RAND == HASHD
 
-void rand_bytes(uint8_t *buf, int size) {
+void rand_bytes(uint8_t *buf, size_t size) {
 	uint8_t hash[RLC_MD_LEN];
 	int carry, len  = (RLC_RAND_SIZE - 1)/2;
 	ctx_t *ctx = core_get();
 
 	if (sizeof(int) > 2 && size > (1 << 16)) {
@@ -171,13 +172,13 @@ void rand_bytes(uint8_t *buf, int size) {
 	rand_inc(ctx->rand, len - RLC_MD_LEN + 1, carry);
 	rand_inc(ctx->rand, len + 1, ctx->counter);
 	ctx->counter = ctx->counter + 1;
 }
 
-void rand_seed(uint8_t *buf, int size) {
+void rand_seed(uint8_t *buf, size_t size) {
 	ctx_t *ctx = core_get();
-	int len = (RLC_RAND_SIZE - 1) / 2;
+	size_t len = (RLC_RAND_SIZE - 1) / 2;
 
 	if (size <= 0) {
 		RLC_THROW(ERR_NO_VALID);
 		return;
 	}
diff --git a/src/relic_util.c b/src/relic_util.c
index ba7840f7..06a47d51 100644
--- a/src/relic_util.c
+++ b/src/relic_util.c
@@ -132,11 +132,11 @@ char util_conv_char(dig_t i) {
 			"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
 	return conv_table[i];
 #endif
 }
 
-int util_bits_dig(dig_t a) {
+size_t util_bits_dig(dig_t a) {
     return RLC_DIG - arch_lzcnt(a);
 }
 
 int util_cmp_const(const void *a, const void *b, int size) {
 	const uint8_t *_a = (const uint8_t *)a;
diff --git a/test/test_bn.c b/test/test_bn.c
index 30a92536..a9cfb5fa 100644
--- a/test/test_bn.c
+++ b/test/test_bn.c
@@ -232,11 +232,11 @@ static int util(void) {
 			bits %= (RLC_DIG);
 			TEST_ASSERT(bn_cmp(a, b) == RLC_EQ, end);
 		} TEST_END;
 
 		TEST_CASE("reading and writing a positive number are consistent") {
-			int len = RLC_CEIL(RLC_BN_BITS, 8);
+			size_t len = RLC_CEIL(RLC_BN_BITS, 8);
 			bn_rand(a, RLC_POS, RLC_BN_BITS);
 			for (int j = 2; j <= 64; j++) {
 				bits = bn_size_str(a, j);
 				bn_write_str(str, bits, a, j);
 				bn_read_str(b, str, bits, j);
@@ -260,11 +260,11 @@ static int util(void) {
 			TEST_ASSERT(bn_size_raw(a) == a->used, end);
 		}
 		TEST_END;
 
 		TEST_CASE("reading and writing a negative number are consistent") {
-			int len = RLC_CEIL(RLC_BN_BITS, 8);
+			size_t len = RLC_CEIL(RLC_BN_BITS, 8);
 			bn_rand(a, RLC_NEG, RLC_BN_BITS);
 			for (int j = 2; j <= 64; j++) {
 				bits = bn_size_str(a, j);
 				bn_write_str(str, bits, a, j);
 				bn_read_str(b, str, bits, j);
@@ -1137,12 +1137,13 @@ static int exponentiation(void) {
 	crt_free(crt);
 	return code;
 }
 
 static int square_root(void) {
-	int bits, code = RLC_ERR;
+	size_t bits;
 	bn_t a, b, c;
+	int code = RLC_ERR;
 
 	bn_null(a);
 	bn_null(b);
 	bn_null(c);
 
@@ -1936,13 +1937,14 @@ static int factor(void) {
 }
 
 static int recoding(void) {
 	int code = RLC_ERR;
 	bn_t a, b, c, v1[3], v2[3];
-	int w, k, l;
+	int w, k;
 	uint8_t d[RLC_BN_BITS + 1];
 	int8_t e[2 * (RLC_BN_BITS + 1)];
+	size_t l;
 
 	bn_null(a);
 	bn_null(b);
 	bn_null(c);
 	for (k = 0; k < 3; k++) {
diff --git a/test/test_core.c b/test/test_core.c
index a7642f08..12678dc0 100644
--- a/test/test_core.c
+++ b/test/test_core.c
@@ -121,10 +121,11 @@ int main(void) {
 				core_clean();
 			}
 		}
 		TEST_ASSERT(code == RLC_OK, end);
 
+		core_clean();
 		core_init();
 #pragma omp parallel copyin(core_ctx) shared(code)
 		{
 			if (core_get() == NULL) {
 				code = RLC_ERR;
diff --git a/test/test_pc.c b/test/test_pc.c
index dff4fab8..d743aff7 100644
--- a/test/test_pc.c
+++ b/test/test_pc.c
@@ -1243,11 +1243,11 @@ static int memory(void) {
   end:
 	return code;
 }
 
 int util(void) {
-	int l, code = RLC_ERR;
+	int code = RLC_ERR;
 	gt_t a, b, c;
 	uint8_t bin[24 * RLC_PC_BYTES];
 
 	gt_null(a);
 	gt_null(b);
diff --git a/test/test_rand.c b/test/test_rand.c
index e3de369d..7f90ab8f 100644
--- a/test/test_rand.c
+++ b/test/test_rand.c
@@ -361,11 +361,11 @@ static int test(void) {
 
 #elif RAND == RDRND
 
 static int test(void) {
 	uint8_t out[64];
-	int len = sizeof(out) / 2, code = RLC_ERR;
+	size_t len = sizeof(out) / 2, code = RLC_ERR;
 
 	TEST_ONCE("rdrand hardware generator is non-trivial") {
 		memset(out, 0, 2 * len);
 		rand_bytes(out, len);
 		/* This fails with negligible probability. */

Vulnerable code: 	int len;
		int len;
			int len = RLC_BN_BITS + 1;
		int len = RLC_BN_BITS + 1;
	BENCH_ONE("cp_bdpe_gen", cp_bdpe_gen(pub, prv, bn_get_prime(47),
		RLC_BN_BITS), 1);
		in = out[0] % bn_get_prime(47);
		in = out[0] % bn_get_prime(47);
	BENCH_FEW("ep2_null", ep4_null(a[i]), 1);
	BENCH_FEW("ep2_new", ep4_new(a[i]), 1);
	BENCH_FEW("ep2_free", ep4_free(a[i]), 1);
#define RLC_BN_BITS 	((int)BN_PRECI)
#define RLC_BN_DIGS		((int)RLC_CEIL(BN_PRECI, RLC_DIG))
#define RLC_BN_SIZE		((int)(2 * RLC_BN_DIGS + 2))
#define RLC_BN_SIZE		((int)(RLC_BN_DIGS + 1))
#define RLC_BN_SIZE		((int)RLC_BN_DIGS)
	int alloc;
	int used;
void bn_make(bn_t a, int digits);
void bn_grow(bn_t a, int digits);
int bn_bits(const bn_t a);
int bn_get_bit(const bn_t a, int bit);
void bn_set_bit(bn_t a, int bit, int value);
int bn_ham(const bn_t a);
void bn_set_2b(bn_t a, int b);
void bn_rand(bn_t a, int sign, int bits);
int bn_size_str(const bn_t a, int radix);
void bn_read_str(bn_t a, const char *str, int len, int radix);
void bn_write_str(char *str, int len, const bn_t a, int radix);
int bn_size_bin(const bn_t a);
void bn_read_bin(bn_t a, const uint8_t *bin, int len);
void bn_write_bin(uint8_t *bin, int len, const bn_t a);
int bn_size_raw(const bn_t a);
void bn_read_raw(bn_t a, const dig_t *raw, int len);
void bn_write_raw(dig_t *raw, int len, const bn_t a);
void bn_lsh(bn_t c, const bn_t a, int bits);
void bn_rsh(bn_t c, const bn_t a, int bits);
void bn_gen_prime_basic(bn_t a, int bits);
void bn_gen_prime_safep(bn_t a, int bits);
void bn_gen_prime_stron(bn_t a, int bits);
int bn_gen_prime_factor(bn_t a, bn_t b, int abits, int bbits);
void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w);
void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w);
void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w);
void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w);
void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w);
void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m);
void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w);
void bn_rec_jsf(int8_t *jsf, int *len, const bn_t k, const bn_t l);
int cp_rsa_gen(rsa_t pub, rsa_t prv, int bits);
int cp_bls_sig(g1_t s, const uint8_t *msg, int len, const bn_t d);
void eb_read_bin(eb_t a, const uint8_t *bin, int len);
void eb_write_bin(uint8_t *bin, int len, const eb_t a, int pack);
void eb_map(eb_t p, const uint8_t *msg, int len);
void ed_map(ed_t p, const uint8_t *msg, int len);
void ed_map_dst(ed_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
void ed_read_bin(ed_t a, const uint8_t *bin, int len);
void ed_write_bin(uint8_t *bin, int len, const ed_t a, int pack);
void ep_read_bin(ep_t a, const uint8_t *bin, int len);
void ep_write_bin(uint8_t *bin, int len, const ep_t a, int pack);
void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len);
void ep_map(ep_t p, const uint8_t *msg, int len);
void ep_map_dst(ep_t p, const uint8_t *msg, int len, const uint8_t *dst,
		int dst_len);
void ep2_read_bin(ep2_t a, const uint8_t *bin, int len);
void ep2_write_bin(uint8_t *bin, int len, const ep2_t a, int pack);
void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, const bn_t m);
void ep2_mul_sim_joint(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q, const bn_t m);
void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], int n);
void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], int len);
void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, int len);
void ep2_map(ep2_t p, const uint8_t *msg, int len);
void ep2_map_dst(ep2_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
void ep4_read_bin(ep4_t a, const uint8_t *bin, int len);
void ep4_write_bin(uint8_t *bin, int len, const ep4_t a, int pack);
void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], int n);
void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], int len);
void ep4_map(ep4_t p, const uint8_t *msg, int len);
void ep4_map_dst(ep4_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len);
#define RLC_FB_BITS 	((int)FB_POLYN)
#define RLC_FB_DIGS		((int)RLC_CEIL(RLC_FB_BITS, RLC_DIG))
#define RLC_FB_BYTES 	((int)RLC_CEIL(RLC_FB_BITS, 8))
int fb_get_bit(const fb_t a, int bit);
void fb_set_bit(fb_t a, int bit, int value);
int fb_bits(const fb_t a);
int fb_size_str(const fb_t a, int radix);
void fb_read_str(fb_t a, const char *str, int len, int radix);
void fb_write_str(char *str, int len, const fb_t a, int radix);
 * @param[in] bin			- the byte vector.
void fb_read_bin(fb_t a, const uint8_t *bin, int len);
void fb_write_bin(uint8_t *bin, int len, const fb_t a);
#define RLC_FP_BITS 	((int)FP_PRIME)
#define RLC_FP_DIGS 	((int)RLC_CEIL(RLC_FP_BITS, RLC_DIG))
#define RLC_FP_BYTES 	((int)RLC_CEIL(RLC_FP_BITS, 8))
void fp_prime_set_pmers(const int *spars, int len);
* Assigns the prime field modulus to a parametrization from a family of
int fp_get_bit(const fp_t a, int bit);
void fp_set_bit(fp_t a, int bit, int value);
int fp_bits(const fp_t a);
int fp_size_str(const fp_t a, int radix);
void fp_read_str(fp_t a, const char *str, int len, int radix);
void fp_write_str(char *str, int len, const fp_t a, int radix);
void fp_read_bin(fp_t a, const uint8_t *bin, int len);
void fp_write_bin(uint8_t *bin, int len, const fp_t a);
void fp2_read_bin(fp2_t a, const uint8_t *bin, int len);
void fp2_write_bin(uint8_t *bin, int len, const fp2_t a, int pack);
void fp3_read_bin(fp3_t a, const uint8_t *bin, int len);
void fp3_write_bin(uint8_t *bin, int len, const fp3_t a);
void fp4_read_bin(fp4_t a, const uint8_t *bin, int len);
void fp4_write_bin(uint8_t *bin, int len, const fp4_t a);
void fp6_read_bin(fp6_t a, const uint8_t *bin, int len);
void fp6_write_bin(uint8_t *bin, int len, const fp6_t a);
void fp8_read_bin(fp8_t a, const uint8_t *bin, int len);
void fp8_write_bin(uint8_t *bin, int len, const fp8_t a);
void fp9_read_bin(fp9_t a, const uint8_t *bin, int len);
void fp9_write_bin(uint8_t *bin, int len, const fp9_t a);
void fp12_read_bin(fp12_t a, const uint8_t *bin, int len);
void fp12_write_bin(uint8_t *bin, int len, const fp12_t a, int pack);
void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c, const bn_t d);
void fp12_exp_cyc_sim(fp12_t e, const fp12_t a, const bn_t b, const fp12_t c, const bn_t d);
void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, int l, int s);
void fp18_read_bin(fp18_t a, const uint8_t *bin, int len);
void fp18_write_bin(uint8_t *bin, int len, const fp18_t a);
void fp24_read_bin(fp24_t a, const uint8_t *bin, int len);
void fp24_write_bin(uint8_t *bin, int len, const fp24_t a, int pack);
void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, int l, int s);
void fp48_read_bin(fp48_t a, const uint8_t *bin, int len);
void fp48_write_bin(uint8_t *bin, int len, const fp48_t a, int pack);
void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, int l, int s);
void fp54_read_bin(fp54_t a, const uint8_t *bin, int len);
void fp54_write_bin(uint8_t *bin, int len, const fp54_t a, int pack);
void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, int l, int s);
void md_map_sh224(uint8_t *hash, const uint8_t *msg, int len);
void md_map_sh256(uint8_t *hash, const uint8_t *msg, int len);
void md_map_sh384(uint8_t *hash, const uint8_t *msg, int len);
void md_map_sh512(uint8_t *hash, const uint8_t *msg, int len);
void md_map_b2s160(uint8_t *hash, const uint8_t *msg, int len);
void md_map_b2s256(uint8_t *hash, const uint8_t *msg, int len);
void md_kdf(uint8_t *key, int key_len, const uint8_t *in, int in_len);
void md_mgf(uint8_t *mask, int mask_len, const uint8_t *in, int in_len);
void md_hmac(uint8_t *mac, const uint8_t *in, int in_len, const uint8_t *key,
		int key_len);
void md_xmd_sh224(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
		const uint8_t *dst, int dst_len);
void md_xmd_sh256(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
		const uint8_t *dst, int dst_len);
void md_xmd_sh384(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
		const uint8_t *dst, int dst_len);
void md_xmd_sh512(uint8_t *buf, int buf_len, const uint8_t *in, int in_len,
		const uint8_t *dst, int dst_len);
void rand_seed(uint8_t *buf, int size);
void rand_seed(void (*callback)(uint8_t *, int, void *), void *arg);
int rand_check(uint8_t *buf, int size);
void rand_bytes(uint8_t *buf, int size);
int util_bits_dig(dig_t a);
			bn_rsh(u, x, bn_bits(x) - RLC_DIG);
			bn_rsh(v, y, bn_bits(x) - RLC_DIG);
				bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
				bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
				bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
				bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
			bn_rsh(u, x, bn_bits(x) - RLC_DIG);
			bn_rsh(v, y, bn_bits(x) - RLC_DIG);
				bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
				bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
				bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
				bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
void bn_make(bn_t a, int digits) {
void bn_grow(bn_t a, int digits) {
		if (a->used <= 0) {
	int i, j, l, w = 1;
	for (i = 0; i < RLC_TABLE_SIZE; i++) {
	i = bn_bits(b);
	if (i <= 21) {
	} else if (i <= 32) {
	} else if (i <= 128) {
	} else if (i <= 256) {
	} else if (i <= 512) {
		for (i = 0; i < (1 << (w - 1)); i++) {
		for (i = 1; i < 1 << (w - 1); i++) {
		l = bn_bits(b);
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (w - 1)); i++) {
		while (bn_is_even(r)) {
void bn_gen_prime_basic(bn_t a, int bits) {
void bn_gen_prime_safep(bn_t a, int bits) {
void bn_gen_prime_stron(bn_t a, int bits) {
int bn_gen_prime_factor(bn_t a, bn_t b, int abits, int bbits) {
static char get_bits(const bn_t a, int from, int to) {
void bn_rec_win(uint8_t *win, int *len, const bn_t k, int w) {
void bn_rec_slw(uint8_t *win, int *len, const bn_t k, int w) {
void bn_rec_naf(int8_t *naf, int *len, const bn_t k, int w) {
void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u, int w) {
void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, int m) {
void bn_rec_tnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w) {
void bn_rec_rtnaf(int8_t *tnaf, int *len, const bn_t k, int8_t u, int m, int w) {
void bn_rec_reg(int8_t *naf, int *len, const bn_t k, int n, int w) {
void bn_rec_jsf(int8_t *jsf, int *len, const bn_t k, const bn_t l) {
	int r1, r2, bits;
void bn_lsh(bn_t c, const bn_t a, int bits) {
	if (bits <= 0) {
		return;
	}

void bn_rsh(bn_t c, const bn_t a, int bits) {
	if (bits <= 0) {
		return;
	}

	c->used = a->used - digits;
			while (bn_is_even(t0)) {
	int bits, cmp;
		bn_set_2b(l, (bits >> 1) - 1);
int bn_bits(const bn_t a) {
int bn_get_bit(const bn_t a, int bit) {
void bn_set_bit(bn_t a, int bit, int value) {
int bn_ham(const bn_t a) {
void bn_set_2b(bn_t a, int b) {
	if (b < 0) {
		bn_zero(a);
void bn_rand(bn_t a, int sign, int bits) {
int bn_size_str(const bn_t a, int radix) {
void bn_read_str(bn_t a, const char *str, int len, int radix) {
void bn_write_str(char *str, int len, const bn_t a, int radix) {
int bn_size_bin(const bn_t a) {
void bn_read_bin(bn_t a, const uint8_t *bin, int len) {
void bn_write_bin(uint8_t *bin, int len, const bn_t a) {
int bn_size_raw(const bn_t a) {
void bn_read_raw(bn_t a, const dig_t *raw, int len) {
void bn_write_raw(dig_t *raw, int len, const bn_t a) {
int cp_bls_sig(g1_t s, const uint8_t *msg, int len, const bn_t d) {
		if (bls) {
			cp_bls_gen(sk, pk);
		} else {
			cp_ecdsa_gen(sk, g1);
			fp_copy(pk->x[0], g1->x);
			fp_copy(pk->y[0], g1->y);
		}
		bn_rand_mod(d, n);
		g2_mul_gen(y, d);
	int len, dlen = strlen(data), result = RLC_OK;
	int len, dlen = strlen(data), result = 1;
	int len, dlen = strlen(data), result = 1;
static int pad_pkcs1(bn_t m, int *p_len, int m_len, int k_len, int operation) {
	int len, result = RLC_ERR;
		switch (operation) {
static int pad_pkcs2(bn_t m, int *p_len, int m_len, int k_len, int operation) {
		switch (operation) {
int cp_rsa_gen(rsa_t pub, rsa_t prv, int bits) {
	int len1 = strlen(id1), len2 = strlen(id2);
	int size, first = 0, result = RLC_OK;
	int len, result = RLC_OK;
	int len, result = RLC_OK;
	int len, result = 0;
	ec_t Z;
	ec_t t;
void eb_map(eb_t p, const uint8_t *msg, int len) {
	int i, l, n;
	int i, l, n;
	int i, l, n;
	int i, l, n;
	int bits, i, j;
		for (i = bits - 1; i >= 0; i--) {
			j = bn_get_bit(t, i);
	int i, j, l, trc, cof;
	int i, l, l0, l1, n0, n1, w, g;
	int i, l, l0, l1, n0, n1, w, g;
	int l0, l1, w = EB_WIDTH / 2;
	int i, u_i, len, offset;
void eb_read_bin(eb_t a, const uint8_t *bin, int len) {
void eb_write_bin(uint8_t *bin, int len, const eb_t a, int pack) {
void ed_map_dst(ed_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len) {
void ed_map(ed_t p, const uint8_t *msg, int len) {
	int l, i, n;
	int i, j, l, n;
	int i, j, l;
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	int l;
	int l, i, n;
	int i, l, l0, l1, n0, n1, w, gen;
	int l0, l1, w = ED_WIDTH / 2;
	int i, l, u_i, offset;
	int i, j, l, *_l = RLC_ALLOCA(int, n);
	ed_t *_p = RLC_ALLOCA(ed_t, n);
void ed_read_bin(ed_t a, const uint8_t *bin, int len) {
void ed_write_bin(uint8_t *bin, int len, const ed_t a, int pack) {
TMPL_MAP_HORNER(fp, fp_st)
		TMPL_MAP_ISOGENY_MAP(ep, fp, iso)
#define EP_MAP_COPY_COND(O, I, C) dv_copy_cond(O, I, RLC_FP_DIGS, C)
		TMPL_MAP_SSWU(ep, fp, dig_t, EP_MAP_COPY_COND)
TMPL_MAP_SVDW(ep, fp, dig_t, EP_MAP_COPY_COND)
/* caution: this function overwrites k, which it uses as an auxiliary variable */
void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, int len) {
		void (*const map_fn)(ep_t, fp_t) =(ep_curve_is_ctmap() ||
#define EP_MAP_CONVERT_BYTES(IDX)                                       \
    do {                                                                \
      bn_read_bin(k, uniform_bytes + IDX * len_per_elm, len_per_elm);   \
      fp_prime_conv(t, k);                                              \
    } while (0)

#define EP_MAP_APPLY_MAP(PT)                                    \
    do {                                                        \
      /* check sign of t */                                     \
      neg = fp_sgn0(t, k);                                      \
      /* convert */                                             \
      map_fn(PT, t);                                            \
      /* compare sign of y and sign of t; fix if necessary */   \
      neg = neg != fp_sgn0(PT->y, k);                             \
      fp_neg(t, PT->y);                                          \
      dv_copy_cond(PT->y, t, RLC_FP_DIGS, neg);                  \
    } while (0)
void ep_map_dst(ep_t p, const uint8_t *msg, int len, const uint8_t *dst,
		int dst_len) {
void ep_map(ep_t p, const uint8_t *msg, int len) {
	int l, l0, l1, i, n0, n1, s0, s1;
	int i, l;
		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
		for (i = l - 1; i >= 0; i--) {
		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
	int i, j, l, n0, n1, s0, s1, b0, b1;
	int i, j, l, n;
	int i, j, l;
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	int i, j, bits;
		for (i = bits - 1; i >= 0; i--) {
			j = bn_get_bit(l, i);
	int l;
	int l, i, n;
	int i, l, l0, l1, l2, l3, sk0, sk1, sl0, sl1, w, g = 0;
	int i, j, m, l, _l[2], sk;
	int i, l, l0, l1, w, gen;
	int i, j, l, *_l = RLC_ALLOCA(int, n);
	ep_t *_p = RLC_ALLOCA(ep_t, n);
	int l0, l1, w = EP_WIDTH / 2;
	int i, l, u_i, offset;
void ep_read_bin(ep_t a, const uint8_t *bin, int len) {
void ep_write_bin(uint8_t *bin, int len, const ep_t a, int pack) {


#define EP2_MAP_COPY_COND(O, I, C)                                                       \
	do {                                                                                 \
		dv_copy_cond(O[0], I[0], RLC_FP_DIGS, C);                                        \
		dv_copy_cond(O[1], I[1], RLC_FP_DIGS, C);                                        \


void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, int len) {
        bn_t k;
        fp2_t t;
        ep2_t q;
        int neg;
        /* enough space for two extension field elements plus extra bytes for uniformity */
        const int len_per_elm = (FP_PRIME + ep_param_level() + 7) / 8;

        bn_null(k);
        fp2_null(t);
        ep2_null(q);

        RLC_TRY {
                if (len != 2* len_per_elm) {
                  RLC_THROW(ERR_NO_VALID);
                }

                bn_new(k);
                fp2_new(t);
                ep2_new(q);

                /* which hash function should we use? */
                const int abNeq0 = (ep2_curve_opt_a() != RLC_ZERO) && (ep2_curve_opt_b() != RLC_ZERO);
                void (*const map_fn)(ep2_t, fp2_t) = (ep2_curve_is_ctmap() || abNeq0) ? ep2_map_sswu : ep2_map_svdw;

#define EP2_MAP_CONVERT_BYTES(IDX)                                                       \
        do {                                                                                 \
                bn_read_bin(k, uniform_bytes + 2 * IDX * len_per_elm, len_per_elm);        \
                fp_prime_conv(t[0], k);                                                          \
                bn_read_bin(k, uniform_bytes + (2 * IDX + 1) * len_per_elm, len_per_elm);  \
                fp_prime_conv(t[1], k);                                                          \
        } while (0)

#define EP2_MAP_APPLY_MAP(PT)                                                            \
        do {                                                                                 \
                /* sign of t */                                                                  \
                neg = fp2_sgn0(t, k);                                                            \
                /* convert */                                                                    \
                map_fn(PT, t);                                                                   \
                /* compare sign of y to sign of t; fix if necessary */                           \
                neg = neg != fp2_sgn0(PT->y, k);                                                 \
                fp2_neg(t, PT->y);                                                               \
                dv_copy_cond(PT->y[0], t[0], RLC_FP_DIGS, neg);                                  \
                dv_copy_cond(PT->y[1], t[1], RLC_FP_DIGS, neg);                                  \
        } while (0)

                /* first map invocation */
                EP2_MAP_CONVERT_BYTES(0);
                EP2_MAP_APPLY_MAP(p);
                TMPL_MAP_CALL_ISOMAP(ep2, p);

                /* second map invocation */
                EP2_MAP_CONVERT_BYTES(1);
                EP2_MAP_APPLY_MAP(q);
                TMPL_MAP_CALL_ISOMAP(ep2, q);

                /* XXX(rsw) could add p and q and then apply isomap,
                 * but need ep_add to support addition on isogeny curves */
                /* sum the result */
                ep2_add(p, p, q);
                ep2_norm(p, p);
                ep2_mul_cof(p, p);
        }
        RLC_CATCH_ANY {
                RLC_THROW(ERR_CAUGHT);
        }
        RLC_FINALLY {
                bn_free(k);
                fp2_free(t);
                ep2_free(q);
        }
void ep2_map_dst(ep2_t p, const uint8_t *msg, int len, const uint8_t *dst, int dst_len) {

        /* enough space for two field elements plus extra bytes for uniformity */
        const int len_per_elm = (FP_PRIME + ep_param_level() + 7) / 8;
        uint8_t *pseudo_random_bytes = RLC_ALLOCA(uint8_t, 4 * len_per_elm);

        RLC_TRY {

                /* XXX(rsw) See note in ep/relic_ep_map.c about using MD_MAP. */
                /* hash to a pseudorandom string using md_xmd */
                md_xmd(pseudo_random_bytes, 4 * len_per_elm, msg, len, dst, dst_len);
                ep2_map_from_field(p, pseudo_random_bytes, 2 * len_per_elm);
        }
        RLC_CATCH_ANY {
                RLC_THROW(ERR_CAUGHT);
        }
        RLC_FINALLY {
                RLC_FREE(pseudo_random_bytes);
        }
void ep2_map(ep2_t p, const uint8_t *msg, int len) {
	int i, j, l, _l[4];
		for (i = 0; i < 4; i++) {
		for (i = 0; i < 4; i++) {
		for (j = l - 1; j >= 0; j--) {
			for (i = 0; i < 4; i++) {
		for (i = 0; i < 4; i++) {
	int l, i, n;
		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
		for (i = l - 1; i >= 0; i--) {
		for (i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
	int i, l;
		for (i = l - 2; i >= 0; i--) {
	int i, j, l;
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	int l;
	int len, i, n;
	for (i = len - 1; i >= 0; i--, t--) {
	int i, j, l, _l[4];
		for (i = 0; i < 4; i++) {
		for (i = 0; i < 4; i++) {
		for (i = l - 1; i >= 0; i--) {
			for (j = 0; j < 4; j++) {
		for (i = 0; i < 4; i++) {
	int i, l, l0, l1, n0, n1, w, gen;
	int l0, l1, w = EP_WIDTH / 2;
	int i, l, u_i, offset;
void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], int len) {
void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], int n) {
	const int len = RLC_FP_BITS + 1;
	int i, j, m, l, _l[4];
void ep2_read_bin(ep2_t a, const uint8_t *bin, int len) {
void ep2_write_bin(uint8_t *bin, int len, const ep2_t a, int pack) {
void ep4_map(ep4_t p, const uint8_t *msg, int len) {
	int sign, i, j, l, _l[8];
	int l, i, n;
	int i, j, l;
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	int i;

	for (i = 2; i < (1 << (EP_DEPTH - 2)); i++) {
	for (i = 1; i < (1 << (EP_DEPTH - 2)); i++) {
	int len, i, n;
	for (i = len - 1; i >= 0; i--, t--) {
	int i, l, l0, l1, n0, n1, w, gen;
	int l0, l1, w = EP_WIDTH / 2;
	int i, l, u_i, offset;
void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], int len) {
void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], int n) {
	const int len = RLC_FP_BITS + 1;
	int i, j, m, l, *_l = RLC_ALLOCA(int, 8 * n);
void ep4_read_bin(ep4_t a, const uint8_t *bin, int len) {
void ep4_write_bin(uint8_t *bin, int len, const ep4_t a, int pack) {
	int i, l;
		for (i = l - 2; i >= 0; i--) {
	int i, j, l;
	for (i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
		for (i = 0; i < (1 << (FB_WIDTH - 1)); i ++) {
		for (i = 1; i < 1 << (FB_WIDTH - 1); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
static int valid_radix(int radix) {
static int log_radix(int radix) {
int fb_get_bit(const fb_t a, int bit) {
void fb_set_bit(fb_t a, int bit, int value) {
int fb_bits(const fb_t a) {
	int bits, digits;
	int i;

	for (i = RLC_FB_DIGS - 1; i > 0; i--) {
int fb_size_str(const fb_t a, int radix) {
	int digits = 0;
void fb_read_str(fb_t a, const char *str, int len, int radix) {
void fb_write_str(char *str, int len, const fb_t a, int radix) {
void fb_read_bin(fb_t a, const uint8_t *bin, int len) {
void fb_write_bin(uint8_t *bin, int len, const fb_t a) {
	int i, l;
		for (i = l - 2; i >= 0; i--) {
	int i, j, l;
	for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
		for (i = 0; i < (1 << (FP_WIDTH - 1)); i ++) {
		for (i = 1; i < 1 << (FP_WIDTH - 1); i++) {
		for (i = 0; i < l; i++) {
				for (j = 0; j < util_bits_dig(win[i]); j++) {
		for (i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
	int len = bn_bits(x) + 1;
void fp_prime_set_pmers(const int *f, int len) {
int fp_get_bit(const fp_t a, int bit) {
void fp_set_bit(fp_t a, int bit, int value) {
int fp_bits(const fp_t a) {
int fp_size_str(const fp_t a, int radix) {
	int digits = 0;
void fp_read_str(fp_t a, const char *str, int len, int radix) {
void fp_write_str(char *str, int len, const fp_t a, int radix) {
void fp_read_bin(fp_t a, const uint8_t *bin, int len) {
			}	
void fp_write_bin(uint8_t *bin, int len, const fp_t a) {
	int i, l;
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
		for (i = l - 1; i >= 0; i--, k--) {
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
	int i, l;
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
		for (i = l - 1; i >= 0; i--, k--) {
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
	int i, j, k, l, w = bn_ham(b);
		int _l[4];
			for (i = 0; i < 4; i++) {
				for (i = 0; i < 4; i++) {
				for (i = l - 1; i >= 0; i--) {
					for (j = 0; j < 4; j++) {
				for (i = bn_bits(b) - 2; i >= 0; i--) {
			for (i = 0; i < 4; i++) {
			for (i = 0; i < w; i++) {
			for (i = 1; i < bn_bits(b); i++) {
			for (i = j; i < k; i++) {
			for (i = 0; i < w; i++) {
	int i, l, n0, n1, l0, l1;
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
			for (i = 0; i < l0; i++) {
			for (i = 0; i < l1; i++) {
		for (i = l - 1; i >= 0; i--, _k--, _m--) {
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, int len, int sign) {
	int i, j, k, w = len;
	int i, j, k, w = bn_ham(b);
		int l, _l[8];
			for (i = 0; i < 8; i++) {
				for (i = 0; i < 8; i++) {
				for (i = 0; i < 8; i++) {
				for (i = l - 1; i >= 0; i--) {
					for (j = 0; j < 8; j++) {
				for (i = bn_bits(b) - 2; i >= 0; i--) {
			for (i = 0; i < 8; i++) {
			for (i = 0; i < w; i++) {
			for (i = 1; i < bn_bits(b); i++) {
			for (i = j; i < k; i++) {
			for (i = 0; i < w; i++) {
	int i, l, n0, n1, l0, l1;
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
			for (i = 0; i < l0; i++) {
			for (i = 0; i < l1; i++) {
		for (i = l - 1; i >= 0; i--, _k--, _m--) {
		for (i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, int len, int sign) {
	int i, j, k, w = len;
void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, int len, int sign) {
	int i, j, k, w = len;
void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, int len, int sign) {
	int i, j, k, w = len;
	int l;
	int l;
	int l;
	int l;
void fp2_read_bin(fp2_t a, const uint8_t *bin, int len) {
void fp2_write_bin(uint8_t *bin, int len, const fp2_t a, int pack) {
void fp3_read_bin(fp3_t a, const uint8_t *bin, int len) {
void fp3_write_bin(uint8_t *bin, int len, const fp3_t a) {
void fp4_read_bin(fp4_t a, const uint8_t *bin, int len) {
void fp4_write_bin(uint8_t *bin, int len, const fp4_t a) {
void fp6_read_bin(fp6_t a, const uint8_t *bin, int len) {
void fp6_write_bin(uint8_t *bin, int len, const fp6_t a) {
void fp8_read_bin(fp8_t a, const uint8_t *bin, int len) {
void fp8_write_bin(uint8_t *bin, int len, const fp8_t a) {
void fp9_read_bin(fp9_t a, const uint8_t *bin, int len) {
void fp9_write_bin(uint8_t *bin, int len, const fp9_t a) {
void fp12_read_bin(fp12_t a, const uint8_t *bin, int len) {
void fp12_write_bin(uint8_t *bin, int len, const fp12_t a, int pack) {
void fp18_read_bin(fp18_t a, const uint8_t *bin, int len) {
void fp18_write_bin(uint8_t *bin, int len, const fp18_t a) {
void fp24_read_bin(fp24_t a, const uint8_t *bin, int len) {
void fp24_write_bin(uint8_t *bin, int len, const fp24_t a, int pack) {
void fp48_read_bin(fp48_t a, const uint8_t *bin, int len) {
void fp48_write_bin(uint8_t *bin, int len, const fp48_t a, int pack) {
void fp54_read_bin(fp54_t a, const uint8_t *bin, int len) {
void fp54_write_bin(uint8_t *bin, int len, const fp54_t a, int pack) {
void md_map_b2s160(uint8_t *hash, const uint8_t *msg, int len) {
void md_map_b2s256(uint8_t *hash, const uint8_t *msg, int len) {
void md_hmac(uint8_t *mac, const uint8_t *in, int in_len, const uint8_t *key,
    int key_len) {
void md_kdf(uint8_t *key, int key_len, const uint8_t *in,
		int in_len) {
void md_mgf(uint8_t *key, int key_len, const uint8_t *in,
		int in_len) {
void md_map_sh224(uint8_t *hash, const uint8_t *msg, int len) {
void md_map_sh256(uint8_t *hash, const uint8_t *msg, int len) {
void md_map_sh384(uint8_t *hash, const uint8_t *msg, int len) {
void md_map_sh512(uint8_t *hash, const uint8_t *msg, int len) {
	int i, j, len = bn_bits(a) + 1;
	int i, j, len = bn_bits(a) + 1;
	int i, len = bn_bits(a) + 1;
	int i, len = bn_bits(a) + 1;
	int i, j, len = bn_bits(a) + 1;
int rand_check(uint8_t *buf, int size) {
static void rand_hash(uint8_t *out, int out_len, uint8_t *in, int in_len) {
	int len = RLC_CEIL(out_len, RLC_MD_LEN);
static int rand_inc(uint8_t *data, int size, int digit) {
static int rand_add(uint8_t *state, uint8_t *hash, int size) {
static void rand_gen(uint8_t *out, int out_len) {
void rand_bytes(uint8_t *buf, int size) {
void rand_seed(uint8_t *buf, int size) {
	int len = (RLC_RAND_SIZE - 1) / 2;
int util_bits_dig(dig_t a) {
			int len = RLC_CEIL(RLC_BN_BITS, 8);
			int len = RLC_CEIL(RLC_BN_BITS, 8);
	int bits, code = RLC_ERR;
	int w, k, l;
	int l, code = RLC_ERR;
	int len = sizeof(out) / 2, code = RLC_ERR;
Fixed code: 	size_t len;
		size_t len;
			size_t len = RLC_BN_BITS + 1;
		size_t len = RLC_BN_BITS + 1;
	dig_t prime = 0xFB;
	BENCH_ONE("cp_bdpe_gen", cp_bdpe_gen(pub, prv, prime, RLC_BN_BITS), 1);
		in = out[0] % prime;
		in = out[0] % prime;
	BENCH_FEW("ep2_null", ep2_null(a[i]), 1);
	BENCH_FEW("ep2_new", ep2_new(a[i]), 1);
	BENCH_FEW("ep2_free", ep2_free(a[i]), 1);
#define RLC_BN_BITS 	((size_t)BN_PRECI)
#define RLC_BN_DIGS		((size_t)RLC_CEIL(BN_PRECI, RLC_DIG))
#define RLC_BN_SIZE		((size_t)(2 * RLC_BN_DIGS + 2))
#define RLC_BN_SIZE		((size_t)(RLC_BN_DIGS + 1))
#define RLC_BN_SIZE		((size_t)RLC_BN_DIGS)
	size_t alloc;
	size_t used;
void bn_make(bn_t a, size_t digits);
void bn_grow(bn_t a, size_t digits);
size_t bn_bits(const bn_t a);
int bn_get_bit(const bn_t a, size_t bit);
void bn_set_bit(bn_t a, size_t bit, int value);
size_t bn_ham(const bn_t a);
void bn_set_2b(bn_t a, size_t b);
void bn_rand(bn_t a, int sign, size_t bits);
size_t bn_size_str(const bn_t a, unsigned int radix);
void bn_read_str(bn_t a, const char *str, size_t len, unsigned int radix);
void bn_write_str(char *str, size_t len, const bn_t a, unsigned radix);
size_t bn_size_bin(const bn_t a);
void bn_read_bin(bn_t a, const uint8_t *bin, size_t len);
void bn_write_bin(uint8_t *bin, size_t len, const bn_t a);
size_t bn_size_raw(const bn_t a);
void bn_read_raw(bn_t a, const dig_t *raw, size_t len);
void bn_write_raw(dig_t *raw, size_t len, const bn_t a);
void bn_lsh(bn_t c, const bn_t a, unsigned int bits);
void bn_rsh(bn_t c, const bn_t a, unsigned int bits);
void bn_gen_prime_basic(bn_t a, size_t bits);
void bn_gen_prime_safep(bn_t a, size_t bits);
void bn_gen_prime_stron(bn_t a, size_t bits);
int bn_gen_prime_factor(bn_t a, bn_t b, size_t abits, size_t bbits);
void bn_rec_win(uint8_t *win, size_t *len, const bn_t k, size_t w);
void bn_rec_slw(uint8_t *win, size_t *len, const bn_t k, size_t w);
void bn_rec_naf(int8_t *naf, size_t *len, const bn_t k, size_t w);
void bn_rec_tnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
		size_t w);
void bn_rec_rtnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
		size_t w);
void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u,
		size_t w);
void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, size_t m);
void bn_rec_reg(int8_t *naf, size_t *len, const bn_t k, size_t n, size_t w);
void bn_rec_jsf(int8_t *jsf, size_t *len, const bn_t k, const bn_t l);
int cp_rsa_gen(rsa_t pub, rsa_t prv, size_t bits);
int cp_bls_sig(g1_t s, const uint8_t *msg, size_t len, const bn_t d);
void eb_read_bin(eb_t a, const uint8_t *bin, size_t len);
void eb_write_bin(uint8_t *bin, size_t len, const eb_t a, int pack);
void eb_map(eb_t p, const uint8_t *msg, size_t len);
void ed_map(ed_t p, const uint8_t *msg, size_t len);
void ed_map_dst(ed_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len);
void ed_read_bin(ed_t a, const uint8_t *bin, size_t len);
void ed_write_bin(uint8_t *bin, size_t len, const ed_t a, int pack);
void ep_read_bin(ep_t a, const uint8_t *bin, size_t len);
void ep_write_bin(uint8_t *bin, size_t len, const ep_t a, int pack);
void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, size_t len);
void ep_map(ep_t p, const uint8_t *msg, size_t len);
void ep_map_dst(ep_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len);
void ep2_read_bin(ep2_t a, const uint8_t *bin, size_t len);
void ep2_write_bin(uint8_t *bin, size_t len, const ep2_t a, int pack);
void ep2_mul_sim_inter(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
			const bn_t m);
void ep2_mul_sim_joint(ep2_t r, const ep2_t p, const bn_t k, const ep2_t q,
			const bn_t m);
void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], size_t n);
void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], size_t len);
void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, size_t len);
void ep2_map(ep2_t p, const uint8_t *msg, size_t len);
void ep2_map_dst(ep2_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len);
void ep4_read_bin(ep4_t a, const uint8_t *bin, size_t len);
void ep4_write_bin(uint8_t *bin, size_t len, const ep4_t a, int pack);
void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], size_t n);
void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], size_t len);
void ep4_map(ep4_t p, const uint8_t *msg, size_t len);
void ep4_map_dst(ep4_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len);
#define RLC_FB_BITS 	((size_t)FB_POLYN)
#define RLC_FB_DIGS		((size_t)RLC_CEIL(RLC_FB_BITS, RLC_DIG))
#define RLC_FB_BYTES 	((size_t)RLC_CEIL(RLC_FB_BITS, 8))
int fb_get_bit(const fb_t a, size_t bit);
void fb_set_bit(fb_t a, size_t bit, int value);
size_t fb_bits(const fb_t a);
size_t fb_size_str(const fb_t a, unsigned int radix);
void fb_read_str(fb_t a, const char *str, size_t len, unsigned int radix);
void fb_write_str(char *str, size_t len, const fb_t a, unsigned int radix);
 * @param[in] bin			- the byte vector.,
void fb_read_bin(fb_t a, const uint8_t *bin, size_t len);
void fb_write_bin(uint8_t *bin, size_t len, const fb_t a);
#define RLC_FP_BITS 	((size_t)FP_PRIME)
#define RLC_FP_DIGS 	((size_t)RLC_CEIL(RLC_FP_BITS, RLC_DIG))
#define RLC_FP_BYTES 	((size_t)RLC_CEIL(RLC_FP_BITS, 8))
void fp_prime_set_pmers(const int *spars, size_t len);
 * Assigns the prime field modulus to a parametrization from a family of
int fp_get_bit(const fp_t a, size_t bit);
void fp_set_bit(fp_t a, size_t bit, int value);
size_t fp_bits(const fp_t a);
size_t fp_size_str(const fp_t a, unsigned int radix);
void fp_read_str(fp_t a, const char *str, size_t len, unsigned int radix);
void fp_write_str(char *str, size_t len, const fp_t a, unsigned int radix);
void fp_read_bin(fp_t a, const uint8_t *bin, size_t len);
void fp_write_bin(uint8_t *bin, size_t len, const fp_t a);
void fp2_read_bin(fp2_t a, const uint8_t *bin, size_t len);
void fp2_write_bin(uint8_t *bin, size_t len, const fp2_t a, int pack);
void fp3_read_bin(fp3_t a, const uint8_t *bin, size_t len);
void fp3_write_bin(uint8_t *bin, size_t len, const fp3_t a);
void fp4_read_bin(fp4_t a, const uint8_t *bin, size_t len);
void fp4_write_bin(uint8_t *bin, size_t len, const fp4_t a);
void fp6_read_bin(fp6_t a, const uint8_t *bin, size_t len);
void fp6_write_bin(uint8_t *bin, size_t len, const fp6_t a);
void fp8_read_bin(fp8_t a, const uint8_t *bin, size_t len);
void fp8_write_bin(uint8_t *bin, size_t len, const fp8_t a);
void fp9_read_bin(fp9_t a, const uint8_t *bin, size_t len);
void fp9_write_bin(uint8_t *bin, size_t len, const fp9_t a);
void fp12_read_bin(fp12_t a, const uint8_t *bin, size_t len);
void fp12_write_bin(uint8_t *bin, size_t len, const fp12_t a, int pack);
void fp2_exp_cyc_sim(fp2_t e, const fp2_t a, const bn_t b, const fp2_t c,
		const bn_t d);
void fp12_exp_cyc_sim(fp12_t e, const fp12_t a, const bn_t b, const fp12_t c,
		const bn_t d);
void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, size_t l, int s);
void fp18_read_bin(fp18_t a, const uint8_t *bin, size_t len);
void fp18_write_bin(uint8_t *bin, size_t len, const fp18_t a);
void fp24_read_bin(fp24_t a, const uint8_t *bin, size_t len);
void fp24_write_bin(uint8_t *bin, size_t len, const fp24_t a, int pack);
void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, size_t l, int s);
void fp48_read_bin(fp48_t a, const uint8_t *bin, size_t len);
void fp48_write_bin(uint8_t *bin, size_t len, const fp48_t a, int pack);
void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, size_t l, int s);
void fp54_read_bin(fp54_t a, const uint8_t *bin, size_t len);
void fp54_write_bin(uint8_t *bin, size_t len, const fp54_t a, int pack);
void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, size_t l, int s);
void md_map_sh224(uint8_t *hash, const uint8_t *msg, size_t len);
void md_map_sh256(uint8_t *hash, const uint8_t *msg, size_t len);
void md_map_sh384(uint8_t *hash, const uint8_t *msg, size_t len);
void md_map_sh512(uint8_t *hash, const uint8_t *msg, size_t len);
void md_map_b2s160(uint8_t *hash, const uint8_t *msg, size_t len);
void md_map_b2s256(uint8_t *hash, const uint8_t *msg, size_t len);
void md_kdf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len);
void md_mgf(uint8_t *mask, size_t mask_len, const uint8_t *in, size_t in_len);
void md_hmac(uint8_t *mac, const uint8_t *in, size_t in_len, const uint8_t *key,
		size_t key_len);
void md_xmd_sh224(uint8_t *buf, size_t buf_len, const uint8_t *in,
		size_t in_len, const uint8_t *dst, size_t dst_len);
void md_xmd_sh256(uint8_t *buf, size_t buf_len, const uint8_t *in,
		size_t in_len, const uint8_t *dst, size_t dst_len);
void md_xmd_sh384(uint8_t *buf, size_t buf_len, const uint8_t *in,
		size_t in_len, const uint8_t *dst, size_t dst_len);
void md_xmd_sh512(uint8_t *buf, size_t buf_len, const uint8_t *in,
		size_t in_len, const uint8_t *dst, size_t dst_len);
void rand_seed(uint8_t *buf, size_t size);
void rand_seed(void (*callback)(uint8_t *, size_t, void *), void *arg);
int rand_check(uint8_t *buf, size_t size);
void rand_bytes(uint8_t *buf, size_t size);
size_t util_bits_dig(dig_t a);
			if (bn_bits(x) > RLC_DIG) {
				bn_rsh(u, x, bn_bits(x) - RLC_DIG);
				bn_rsh(v, y, bn_bits(x) - RLC_DIG);
			} else {
				bn_copy(u, x);
				bn_copy(v, y);
			}
				if (bn_bits(x) > 2 * RLC_DIG) {
					bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
					bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
				} else {
					bn_copy(u, x);
					bn_copy(v, y);
				}
				if (bn_bits(u) > RLC_DIG) {
					bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
					bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
				} else {
					bn_copy(t0, u);
					bn_copy(t1, v);
				}
			if (bn_bits(x) > RLC_DIG) {
				bn_rsh(u, x, bn_bits(x) - RLC_DIG);
				bn_rsh(v, y, bn_bits(x) - RLC_DIG);
			} else {
				bn_copy(u, x);
				bn_copy(v, y);
			}
				if (bn_bits(x) > 2 * RLC_DIG) {
					bn_rsh(u, x, bn_bits(x) - 2 * RLC_DIG);
					bn_rsh(v, y, bn_bits(x) - 2 * RLC_DIG);
				} else {
					bn_copy(u, x);
					bn_copy(v, y);
				}
				if (bn_bits(u) > RLC_DIG) {
					bn_rsh(t0, u, bn_bits(u) - RLC_DIG);
					bn_rsh(t1, v, bn_bits(u) - RLC_DIG);
				} else {
					bn_copy(t0, u);
					bn_copy(t1, v);
				}
void bn_make(bn_t a, size_t digits) {
void bn_grow(bn_t a, size_t digits) {
		if (a->used == 0) {
	size_t l, w = 1;
	for (size_t i = 0; i < RLC_TABLE_SIZE; i++) {
	l = bn_bits(b);
	if (l <= 21) {
	} else if (l <= 32) {
	} else if (l <= 128) {
	} else if (l <= 256) {
	} else if (l <= 512) {
		for (size_t i = 0; i < (1 << (w - 1)); i++) {
		for (size_t i = 1; i < 1 << (w - 1); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (w - 1)); i++) {
		while (!bn_is_zero(r) && bn_is_even(r)) {
void bn_gen_prime_basic(bn_t a, size_t bits) {
void bn_gen_prime_safep(bn_t a, size_t bits) {
void bn_gen_prime_stron(bn_t a, size_t bits) {
int bn_gen_prime_factor(bn_t a, bn_t b, size_t abits, size_t bbits) {
static char get_bits(const bn_t a, size_t from, size_t to) {
void bn_rec_win(uint8_t *win, size_t *len, const bn_t k, size_t w) {
void bn_rec_slw(uint8_t *win, size_t *len, const bn_t k, size_t w) {
void bn_rec_naf(int8_t *naf, size_t *len, const bn_t k, size_t w) {
void bn_rec_tnaf_get(uint8_t *t, int8_t *beta, int8_t *gama, int8_t u,
		size_t w) {
void bn_rec_tnaf_mod(bn_t r0, bn_t r1, const bn_t k, int u, size_t m) {
void bn_rec_tnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
		size_t w) {
void bn_rec_rtnaf(int8_t *tnaf, size_t *len, const bn_t k, int8_t u, size_t m,
		size_t w) {
void bn_rec_reg(int8_t *naf, size_t *len, const bn_t k, size_t n, size_t w) {
void bn_rec_jsf(int8_t *jsf, size_t *len, const bn_t k, const bn_t l) {
	int r1, r2;
	size_t bits;
void bn_lsh(bn_t c, const bn_t a, unsigned int bits) {
void bn_rsh(bn_t c, const bn_t a, unsigned int bits) {

	if (a->used > digits) {
		c->used = a->used - digits;
	} else {
		c->used = 0;
	}
			while (bn_is_even(t0) && !bn_is_zero(t0)) {
	size_t bits;
	int cmp;
		bn_zero(l);
		if (bits >= 2) {
			bn_set_2b(l, (bits >> 1) - 1);
		}
size_t bn_bits(const bn_t a) {
int bn_get_bit(const bn_t a, size_t bit) {
void bn_set_bit(bn_t a, size_t bit, int value) {
size_t bn_ham(const bn_t a) {
void bn_set_2b(bn_t a, size_t b) {
	if (b >= RLC_BN_SIZE * RLC_DIG) {
		RLC_THROW(ERR_NO_VALID);
void bn_rand(bn_t a, int sign, size_t bits) {
size_t bn_size_str(const bn_t a, unsigned int radix) {
void bn_read_str(bn_t a, const char *str, size_t len, unsigned int radix) {
void bn_write_str(char *str, size_t len, const bn_t a, unsigned int radix) {
size_t bn_size_bin(const bn_t a) {
void bn_read_bin(bn_t a, const uint8_t *bin, size_t len) {
void bn_write_bin(uint8_t *bin, size_t len, const bn_t a) {
size_t bn_size_raw(const bn_t a) {
void bn_read_raw(bn_t a, const dig_t *raw, size_t len) {
void bn_write_raw(dig_t *raw, size_t len, const bn_t a) {
int cp_bls_sig(g1_t s, const uint8_t *msg, size_t len, const bn_t d) {

		bn_rand_mod(d, n);
		g2_mul_gen(y, d);
		if (bls) {
			result = cp_bls_gen(sk, pk);
		} else {
			if (cp_ecdsa_gen(sk, g1) == RLC_OK) {
				fp_copy(pk->x[0], g1->x);
				fp_copy(pk->y[0], g1->y);
			} else {
				result = RLC_ERR;
			}
		}
	size_t len, dlen = strlen(data);
	int result = RLC_OK;
	size_t len, dlen = strlen(data);
	int result = 1;
	size_t len, dlen = strlen(data);
	int result = 1;
		RLC_FREE(_x);
static int pad_pkcs1(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) {
	size_t len;
	int result = RLC_ERR;
		switch (op) {
static int pad_pkcs2(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) {
		switch (op) {
int cp_rsa_gen(rsa_t pub, rsa_t prv, size_t bits) {
	size_t size, len1 = strlen(id1), len2 = strlen(id2);
	int first = 0, result = RLC_OK;
	size_t len;
	int result = RLC_OK;
	size_t len;
	int result = RLC_OK;
	size_t len;
	ec_t Z, t;
	int result = 0;
void eb_map(eb_t p, const uint8_t *msg, size_t len) {
	int i, n;
	size_t l;
	int i, n;
	size_t l;
	int i, n;
	size_t l;
	int i, n;
	size_t l;
	size_t bits;
		for (int i = bits - 1; i >= 0; i--) {
			int j = bn_get_bit(t, i);
	int i, j, trc, cof;
	size_t l;
	int i, n0, n1, w, g;
	size_t l, l0, l1;
	int i, n0, n1, w, g;
	size_t l, l0, l1;
	size_t l0, l1, w = EB_WIDTH / 2;
	int i, u_i, offset;
	size_t len;
void eb_read_bin(eb_t a, const uint8_t *bin, size_t len) {
void eb_write_bin(uint8_t *bin, size_t len, const eb_t a, int pack) {
void ed_map_dst(ed_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len) {
void ed_map(ed_t p, const uint8_t *msg, size_t len) {
	int i, n;
	size_t l;
	int i, j, n;
	size_t l;
	size_t l;
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	size_t l;
	int i, n;
	size_t l;
	int i, n0, n1, w, gen;
	size_t l, l0, l1;
	size_t l0, l1, w = ED_WIDTH / 2;
	int i, u_i, offset;
	size_t l;
	int i, j;
	ed_t *_p = RLC_ALLOCA(ed_t, n);
	size_t l, *_l = RLC_ALLOCA(size_t, n);
void ed_read_bin(ed_t a, const uint8_t *bin, size_t len) {
void ed_write_bin(uint8_t *bin, size_t len, const ed_t a, int pack) {
TMPL_MAP_HORNER(fp, fp_st);

TMPL_MAP_ISOGENY_MAP(ep, fp, iso);


#define EP_MAP_COPY_COND(O, I, C) dv_copy_cond(O, I, RLC_FP_DIGS, C)
TMPL_MAP_SSWU(ep, fp, dig_t, EP_MAP_COPY_COND);

TMPL_MAP_SVDW(ep, fp, dig_t, EP_MAP_COPY_COND);


/* caution: this function overwrites k, which used as an auxiliary variable */
void ep_map_from_field(ep_t p, const uint8_t *uniform_bytes, size_t len) {
		void (*const map_fn)(ep_t, fp_t) = (ep_curve_is_ctmap() ||
#define EP_MAP_CONVERT_BYTES(IDX)											\
		do {																\
			bn_read_bin(k, uniform_bytes + IDX * len_per_elm, len_per_elm);	\
			fp_prime_conv(t, k);											\
		} while (0)

#define EP_MAP_APPLY_MAP(PT)												\
		do {																\
			/* check sign of t */											\
			neg = fp_sgn0(t, k);											\
			/* convert */													\
			map_fn(PT, t);													\
			/* compare sign of y and sign of t; fix if necessary */			\
			neg = neg != fp_sgn0(PT->y, k);									\
			fp_neg(t, PT->y);												\
			dv_copy_cond(PT->y, t, RLC_FP_DIGS, neg);						\
		} while (0)
void ep_map_dst(ep_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len) {
void ep_map(ep_t p, const uint8_t *msg, size_t len) {
	int i, n0, n1, s0, s1;
	size_t l, l0, l1;
	size_t l;
		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
		for (int i = l - 1; i >= 0; i--) {
		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
	int i, j, n0, n1, s0, s1, b0, b1;
	size_t l;
	int i, j, n;
	size_t l;
	size_t l;
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	size_t bits;
		for (int i = bits - 1; i >= 0; i--) {
			int j = bn_get_bit(l, i);
	size_t l;
	int i, n;
	size_t l;
	int i, sk0, sk1, sl0, sl1, w, g = 0;
	size_t l, l0, l1, l2, l3;
	int i, j, m, sk;
	size_t l, _l[2];
	int i, w, gen;
	size_t l, l0, l1;
	int i, j;
	ep_t *_p = RLC_ALLOCA(ep_t, n);
	size_t l, *_l = RLC_ALLOCA(size_t, n);
	size_t l0, l1, w = EP_WIDTH / 2;
	int i, u_i, offset;
	size_t l;
void ep_read_bin(ep_t a, const uint8_t *bin, size_t len) {
void ep_write_bin(uint8_t *bin, size_t len, const ep_t a, int pack) {
#define EP2_MAP_COPY_COND(O, I, C)											\
	do {																	\
		dv_copy_cond(O[0], I[0], RLC_FP_DIGS, C);							\
		dv_copy_cond(O[1], I[1], RLC_FP_DIGS, C);							\
void ep2_map_from_field(ep2_t p, const uint8_t *uniform_bytes, size_t len) {
	bn_t k;
	fp2_t t;
	ep2_t q;
	int neg;
	/* enough space for two extension field elements plus extra bytes for uniformity */
	const int lpe = (FP_PRIME + ep_param_level() + 7) / 8;

	bn_null(k);
	fp2_null(t);
	ep2_null(q);

	RLC_TRY {
		if (len != 2 * lpe) {
			RLC_THROW(ERR_NO_VALID);
		}

		bn_new(k);
		fp2_new(t);
		ep2_new(q);

		/* which hash function should we use? */
		const int abNeq0 = (ep2_curve_opt_a() != RLC_ZERO) &&
				(ep2_curve_opt_b() != RLC_ZERO);
		void (*const map_fn)(ep2_t, fp2_t) = (ep2_curve_is_ctmap() ||
				abNeq0) ? ep2_map_sswu : ep2_map_svdw;

#define EP2_MAP_CONVERT_BYTES(IDX)											\
		do {																\
			bn_read_bin(k, uniform_bytes + 2 * IDX * lpe, lpe);				\
			fp_prime_conv(t[0], k);											\
			bn_read_bin(k, uniform_bytes + (2 * IDX + 1) * lpe, lpe);		\
			fp_prime_conv(t[1], k);											\
	    } while (0)

#define EP2_MAP_APPLY_MAP(PT)												\
		do {																\
            /* sign of t */													\
            neg = fp2_sgn0(t, k);											\
            /* convert */													\
            map_fn(PT, t);													\
            /* compare sign of y to sign of t; fix if necessary */			\
            neg = neg != fp2_sgn0(PT->y, k);								\
            fp2_neg(t, PT->y);												\
            dv_copy_cond(PT->y[0], t[0], RLC_FP_DIGS, neg);					\
            dv_copy_cond(PT->y[1], t[1], RLC_FP_DIGS, neg);					\
		} while (0)

		/* first map invocation */
		EP2_MAP_CONVERT_BYTES(0);
		EP2_MAP_APPLY_MAP(p);
		TMPL_MAP_CALL_ISOMAP(ep2, p);

		/* second map invocation */
		EP2_MAP_CONVERT_BYTES(1);
		EP2_MAP_APPLY_MAP(q);
		TMPL_MAP_CALL_ISOMAP(ep2, q);

		/* XXX(rsw) could add p and q and then apply isomap,
		 * but need ep_add to support addition on isogeny curves */
		/* sum the result */
		ep2_add(p, p, q);
		ep2_norm(p, p);
		ep2_mul_cof(p, p);
	}
	RLC_CATCH_ANY {
		RLC_THROW(ERR_CAUGHT);
	}
	RLC_FINALLY {
		bn_free(k);
		fp2_free(t);
		ep2_free(q);
	}
void ep2_map_dst(ep2_t p, const uint8_t *msg, size_t len, const uint8_t *dst,
		size_t dst_len) {
	/* enough space for two field elements plus extra bytes for uniformity */
	const int lpe = (FP_PRIME + ep_param_level() + 7) / 8;
	uint8_t *pseudo_random_bytes = RLC_ALLOCA(uint8_t, 4 * lpe);

	RLC_TRY {
		/* XXX(rsw) See note in ep/relic_ep_map.c about using MD_MAP. */
		/* hash to a pseudorandom string using md_xmd */
		md_xmd(pseudo_random_bytes, 4 * lpe, msg, len, dst, dst_len);
		ep2_map_from_field(p, pseudo_random_bytes, 2 * lpe);
	}
	RLC_CATCH_ANY {
		RLC_THROW(ERR_CAUGHT);
	}
	RLC_FINALLY {
		RLC_FREE(pseudo_random_bytes);
	}
void ep2_map(ep2_t p, const uint8_t *msg, size_t len) {
	size_t l, _l[4];
		for (int i = 0; i < 4; i++) {
		for (int i = 0; i < 4; i++) {
		for (int j = l - 1; j >= 0; j--) {
			for (int i = 0; i < 4; i++) {
		for (int i = 0; i < 4; i++) {
	size_t l, n;
		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
		for (int i = l - 1; i >= 0; i--) {
		for (int i = 0; i < (1 << (EP_WIDTH - 2)); i++) {
	size_t l;
		for (int i = l - 2; i >= 0; i--) {
	size_t l;
		for (int i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	size_t l;
	size_t len;
	int n;
	for (int i = len - 1; i >= 0; i--, t--) {
	size_t l, _l[4];
		for (int i = 0; i < 4; i++) {
		for (int i = 0; i < 4; i++) {
		for (int i = l - 1; i >= 0; i--) {
			for (int j = 0; j < 4; j++) {
		for (int i = 0; i < 4; i++) {
	int i, n0, n1, w, gen;
	size_t l, l0, l1;
	size_t l0, l1, w = EP_WIDTH / 2;
	int i, u_i, offset;
	size_t l;
void ep2_mul_sim_dig(ep2_t r, const ep2_t p[], const dig_t k[], size_t len) {
void ep2_mul_sim_lot(ep2_t r, const ep2_t p[], const bn_t k[], size_t n) {
	const size_t len = RLC_FP_BITS + 1;
	int i, j, m;
	size_t l, _l[4];
void ep2_read_bin(ep2_t a, const uint8_t *bin, size_t len) {
void ep2_write_bin(uint8_t *bin, size_t len, const ep2_t a, int pack) {
void ep4_map(ep4_t p, const uint8_t *msg, size_t len) {
	int sign, i, j;
	size_t l, _l[8];
	int i, n;
	size_t l;
	size_t l;
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < (1 << (EP_WIDTH - 1)); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (EP_WIDTH - 1)); i++) {
	for (int i = 2; i < (1 << (EP_DEPTH - 2)); i++) {
	for (int i = 1; i < (1 << (EP_DEPTH - 2)); i++) {
	size_t len;
	int n;
	for (int i = len - 1; i >= 0; i--, t--) {
	int i, n0, n1, w, gen;
	size_t l, l0, l1;
	size_t l0, l1, w = EP_WIDTH / 2;
	int i, u_i, offset;
	size_t l;
void ep4_mul_sim_dig(ep4_t r, const ep4_t p[], const dig_t k[], size_t len) {
void ep4_mul_sim_lot(ep4_t r, const ep4_t p[], const bn_t k[], size_t n) {
	const size_t len = RLC_FP_BITS + 1;
	int i, j, m;
	size_t l, *_l = RLC_ALLOCA(size_t, 8 * n);
void ep4_read_bin(ep4_t a, const uint8_t *bin, size_t len) {
void ep4_write_bin(uint8_t *bin, size_t len, const ep4_t a, int pack) {
	size_t l;
		for (int i = l - 2; i >= 0; i--) {
	size_t l;
	for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
		for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < 1 << (FB_WIDTH - 1); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (FB_WIDTH - 1)); i++) {
static int valid_radix(unsigned int radix) {
static int log_radix(unsigned int radix) {
int fb_get_bit(const fb_t a, size_t bit) {
void fb_set_bit(fb_t a, size_t bit, int value) {
size_t fb_bits(const fb_t a) {
	size_t bits, digits;
	for (int i = RLC_FB_DIGS - 1; i > 0; i--) {
size_t fb_size_str(const fb_t a, unsigned int radix) {
	size_t digits = 0;
void fb_read_str(fb_t a, const char *str, size_t len, unsigned int radix) {
void fb_write_str(char *str, size_t len, const fb_t a, unsigned int radix) {
void fb_read_bin(fb_t a, const uint8_t *bin, size_t len) {
void fb_write_bin(uint8_t *bin, size_t len, const fb_t a) {
	size_t l;
		for (int i = l - 2; i >= 0; i--) {
	size_t l;
	for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
		for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i ++) {
		for (size_t i = 1; i < 1 << (FP_WIDTH - 1); i++) {
		for (size_t i = 0; i < l; i++) {
				for (size_t j = 0; j < util_bits_dig(win[i]); j++) {
		for (size_t i = 0; i < (1 << (FP_WIDTH - 1)); i++) {
	size_t len = bn_bits(x) + 1;
void fp_prime_set_pmers(const int *f, size_t len) {
int fp_get_bit(const fp_t a, size_t bit) {
void fp_set_bit(fp_t a, size_t bit, int value) {
size_t fp_bits(const fp_t a) {
size_t fp_size_str(const fp_t a, unsigned int radix) {
	size_t digits = 0;
void fp_read_str(fp_t a, const char *str, size_t len, unsigned int radix) {
void fp_write_str(char *str, size_t len, const fp_t a, unsigned int radix) {
void fp_read_bin(fp_t a, const uint8_t *bin, size_t len) {
			}
void fp_write_bin(uint8_t *bin, size_t len, const fp_t a) {
	size_t l;
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
		for (int i = l - 1; i >= 0; i--, k--) {
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
	size_t l;
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
		for (int i = l - 1; i >= 0; i--, k--) {
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
	size_t j, k, l, w = bn_ham(b);
		size_t _l[4];
			for (int i = 0; i < 4; i++) {
				for (int i = 0; i < 4; i++) {
				for (int i = l - 1; i >= 0; i--) {
					for (int j = 0; j < 4; j++) {
				for (int i = bn_bits(b) - 2; i >= 0; i--) {
			for (int i = 0; i < 4; i++) {
			for (int i = 0; i < w; i++) {
			for (size_t i = 1; i < bn_bits(b); i++) {
			for (size_t i = j; i < k; i++) {
			for (size_t i = 0; i < w; i++) {
	int n0, n1;
	size_t l, l0, l1;
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
			for (size_t i = 0; i < l0; i++) {
			for (size_t i = 0; i < l1; i++) {
		for (int i = l - 1; i >= 0; i--, _k--, _m--) {
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
void fp12_exp_cyc_sps(fp12_t c, const fp12_t a, const int *b, size_t len,
		int sign) {
	size_t i, j, k, w = len;
	size_t j, k, w = bn_ham(b);
		size_t l, _l[8];
			for (int i = 0; i < 8; i++) {
				for (int i = 0; i < 8; i++) {
				for (int i = 0; i < 8; i++) {
				for (int i = l - 1; i >= 0; i--) {
					for (int j = 0; j < 8; j++) {
				for (int i = bn_bits(b) - 2; i >= 0; i--) {
			for (int i = 0; i < 8; i++) {
			for (size_t i = 0; i < w; i++) {
			for (size_t i = 1; i < bn_bits(b); i++) {
			for (size_t i = j; i < k; i++) {
			for (size_t i = 0; i < w; i++) {
	int n0, n1;
	size_t l, l0, l1;
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i ++) {
			for (size_t i = 0; i < l0; i++) {
			for (size_t i = 0; i < l1; i++) {
		for (int i = l - 1; i >= 0; i--, _k--, _m--) {
		for (int i = 0; i < (1 << (FP_WIDTH - 2)); i++) {
void fp24_exp_cyc_sps(fp24_t c, const fp24_t a, const int *b, size_t len,
		int sign) {
	size_t i, j, k, w = len;
void fp48_exp_cyc_sps(fp48_t c, const fp48_t a, const int *b, size_t len,
		int sign) {
	size_t i, j, k, w = len;
void fp54_exp_cyc_sps(fp54_t c, const fp54_t a, const int *b, size_t len,
		int sign) {
	size_t i, j, k, w = len;
	size_t l;
	size_t l;
	size_t l;
	size_t l;
void fp2_read_bin(fp2_t a, const uint8_t *bin, size_t len) {
void fp2_write_bin(uint8_t *bin, size_t len, const fp2_t a, int pack) {
void fp3_read_bin(fp3_t a, const uint8_t *bin, size_t len) {
void fp3_write_bin(uint8_t *bin, size_t len, const fp3_t a) {
void fp4_read_bin(fp4_t a, const uint8_t *bin, size_t len) {
void fp4_write_bin(uint8_t *bin, size_t len, const fp4_t a) {
void fp6_read_bin(fp6_t a, const uint8_t *bin, size_t len) {
void fp6_write_bin(uint8_t *bin, size_t len, const fp6_t a) {
void fp8_read_bin(fp8_t a, const uint8_t *bin, size_t len) {
void fp8_write_bin(uint8_t *bin, size_t len, const fp8_t a) {
void fp9_read_bin(fp9_t a, const uint8_t *bin, size_t len) {
void fp9_write_bin(uint8_t *bin, size_t len, const fp9_t a) {
void fp12_read_bin(fp12_t a, const uint8_t *bin, size_t len) {
void fp12_write_bin(uint8_t *bin, size_t len, const fp12_t a, int pack) {
void fp18_read_bin(fp18_t a, const uint8_t *bin, size_t len) {
void fp18_write_bin(uint8_t *bin, size_t len, const fp18_t a) {
void fp24_read_bin(fp24_t a, const uint8_t *bin, size_t len) {
void fp24_write_bin(uint8_t *bin, size_t len, const fp24_t a, int pack) {
void fp48_read_bin(fp48_t a, const uint8_t *bin, size_t len) {
void fp48_write_bin(uint8_t *bin, size_t len, const fp48_t a, int pack) {
void fp54_read_bin(fp54_t a, const uint8_t *bin, size_t len) {
void fp54_write_bin(uint8_t *bin, size_t len, const fp54_t a, int pack) {
void md_map_b2s160(uint8_t *hash, const uint8_t *msg, size_t len) {
void md_map_b2s256(uint8_t *hash, const uint8_t *msg, size_t len) {
void md_hmac(uint8_t *mac, const uint8_t *in, size_t in_len, const uint8_t *key,
    size_t key_len) {
void md_kdf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len) {
void md_mgf(uint8_t *key, size_t key_len, const uint8_t *in, size_t in_len) {
void md_map_sh224(uint8_t *hash, const uint8_t *msg, size_t len) {
void md_map_sh256(uint8_t *hash, const uint8_t *msg, size_t len) {
void md_map_sh384(uint8_t *hash, const uint8_t *msg, size_t len) {
void md_map_sh512(uint8_t *hash, const uint8_t *msg, size_t len) {
	size_t len = bn_bits(a) + 1;
	int i, j;
	size_t len = bn_bits(a) + 1;
	int i, j;
	size_t len = bn_bits(a) + 1;
	int i;
	size_t len = bn_bits(a) + 1;
	int i;
	size_t len = bn_bits(a) + 1;
	int i, j;
int rand_check(uint8_t *buf, size_t size) {
static void rand_hash(uint8_t *out, size_t out_len, uint8_t *in,
		size_t in_len) {
	size_t len = RLC_CEIL(out_len, RLC_MD_LEN);
static int rand_inc(uint8_t *data, size_t size, int digit) {
static int rand_add(uint8_t *state, uint8_t *hash, size_t size) {
static void rand_gen(uint8_t *out, size_t out_len) {
void rand_bytes(uint8_t *buf, size_t size) {
void rand_seed(uint8_t *buf, size_t size) {
	size_t len = (RLC_RAND_SIZE - 1) / 2;
size_t util_bits_dig(dig_t a) {
			size_t len = RLC_CEIL(RLC_BN_BITS, 8);
			size_t len = RLC_CEIL(RLC_BN_BITS, 8);
	size_t bits;
	int code = RLC_ERR;
	int w, k;
	size_t l;
		core_clean();
	int code = RLC_ERR;
	size_t len = sizeof(out) / 2, code = RLC_ERR;