diff --git a/bench/bench_fb.c b/bench/bench_fb.c index 41fbb026e..8f20e8fab 100644 --- a/bench/bench_fb.c +++ b/bench/bench_fb.c @@ -165,7 +165,7 @@ static void arith(void) { fb_st t[RLC_FB_TABLE_MAX]; dv_t e; bn_t f; - int bits; + uint_t bits; fb_null(a); fb_null(b); diff --git a/bench/bench_fp.c b/bench/bench_fp.c index 0d8ae5254..95a758169 100644 --- a/bench/bench_fp.c +++ b/bench/bench_fp.c @@ -583,6 +583,15 @@ static void arith(void) { BENCH_END; #endif +#if FP_SMB == BINAR || !defined(STRIP) + BENCH_RUN("fp_smb_binar") { + fp_rand(a); + fp_sqr(a, a); + BENCH_ADD(fp_smb_binar(a)); + } + BENCH_END; +#endif + #if FP_SMB == DIVST || !defined(STRIP) BENCH_RUN("fp_smb_divst") { fp_rand(a); diff --git a/bench/bench_rand.c b/bench/bench_rand.c index c9be79622..be7a7e016 100644 --- a/bench/bench_rand.c +++ b/bench/bench_rand.c @@ -40,7 +40,7 @@ #include #include -static void test_bytes(uint8_t *buf, int size, void *args) { +static void test_bytes(uint8_t *buf, size_t size, void *args) { int c, l, fd = *(int *)args; if (fd == -1) { diff --git a/include/low/relic_bn_low.h b/include/low/relic_bn_low.h index 7c1747c0d..b6d041855 100644 --- a/include/low/relic_bn_low.h +++ b/include/low/relic_bn_low.h @@ -74,7 +74,7 @@ * @param[in] size - the number of digits in the first operand. * @return the carry of the last digit addition. */ -dig_t bn_add1_low(dig_t *c, const dig_t *a, const dig_t digit, const int size); +dig_t bn_add1_low(dig_t *c, const dig_t *a, const dig_t digit, size_t size); /** * Adds two digit vectors of the same size. Computes c = a + b. @@ -85,7 +85,7 @@ dig_t bn_add1_low(dig_t *c, const dig_t *a, const dig_t digit, const int size); * @param[in] size - the number of digits to add. * @return the carry of the last digit addition. */ -dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size); +dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size); /** * Subtracts a digit from a digit vector. Computes c = a - digit. @@ -96,7 +96,7 @@ dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size); * @param[in] size - the number of digits in a. * @return the carry of the last digit subtraction. */ -dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size); +dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size); /** * Subtracts a digit vector from another digit vector. Computes c = a - b. @@ -107,7 +107,17 @@ dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size); * @param[in] size - the number of digits to subtract. * @return the carry of the last digit subtraction. */ -dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, int size); +dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size); + +/** + * Conditionally negate a digit vector using two's complement representation. + * + * @param[out] c - the result. + * @param[in] a - the digit vector to conditionally negate. + * @param[in] sa - the sign of the digit vector. + * @param[in] n - the number of digits to conditionally negate. + */ +void bn_negs_low(dig_t *c, const dig_t *a, dig_t sa, size_t size); /** * Compares two digits. @@ -126,7 +136,7 @@ int bn_cmp1_low(dig_t a, dig_t b); * @param[in] size - the number of digits to compare. * @return BN_LT if a < b, BN_EQ if a == b and BN_GT if a > b. */ -int bn_cmpn_low(const dig_t *a, const dig_t *b, int size); +int bn_cmpn_low(const dig_t *a, const dig_t *b, size_t size); /** * Shifts a digit vector to the left by 1 bit. Computes c = a << 1. @@ -136,7 +146,7 @@ int bn_cmpn_low(const dig_t *a, const dig_t *b, int size); * @param[in] size - the number of digits to shift. * @return the carry of the last digit shift. */ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size); +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size); /** * Shifts a digit vector to the left by an amount smaller than a digit. Computes @@ -148,7 +158,7 @@ dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits); +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits); /** * Shifts a digit vector to the right by 1 bit. Computes c = a >> 1. @@ -158,7 +168,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits); * @param[in] size - the number of digits to shift. * @return the carry of the last digit shift. */ -dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size); +dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size); /** * Shifts a digit vector to the right by an amount smaller than a digit. @@ -170,7 +180,7 @@ dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits); +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits); /** * Shifts a signed digit vector to the right by an amount smaller than a digit. @@ -182,7 +192,7 @@ dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t bn_rshs_low(dig_t *c, const dig_t *a, int size, int bits); +dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits); /** * Multiplies a digit vector by a digit and adds this result to another digit @@ -194,7 +204,7 @@ dig_t bn_rshs_low(dig_t *c, const dig_t *a, int size, int bits); * @param[in] size - the number of digits to multiply. * @return the carry of the last addition. */ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size); +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size); /** * Multiplies a digit vector by a digit and stores this result in another digit @@ -206,7 +216,7 @@ dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size); * @param[in] size - the number of digits to multiply. * @return the most significant digit. */ -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size); +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size); /** * Multiplies a signed digit vector by a signed digit and stores this result in @@ -219,7 +229,7 @@ dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size); * @param[in] size - the number of digits to multiply. * @return the most significant digit. */ -dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size); +dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, size_t size); /** * Multiplies two digit vectors of the same size. Computes c = a * b. @@ -229,7 +239,7 @@ dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size); * @param[in] b - the second digit vector to multiply. * @param[in] size - the number of digits to multiply. */ -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size); +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size); /** * Multiplies two digit vectors of different sizes, with sa > sb. Computes @@ -244,8 +254,8 @@ void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size); * @param[in] low - the first digit to compute. * @param[in] high - the last digit to compute. */ -void bn_muld_low(dig_t *c, const dig_t *a, int sa, const dig_t *b, int sb, - int low, int high); +void bn_muld_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *b, size_t sb, + uint_t low, uint_t high); /** * Squares a digit vector and adds this result to another digit vector. @@ -256,7 +266,7 @@ void bn_muld_low(dig_t *c, const dig_t *a, int sa, const dig_t *b, int sb, * @param[in] size - the number of digits to square. * @return the carry of the last addition. */ -dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size); +dig_t bn_sqra_low(dig_t *c, const dig_t *a, size_t size); /** * Squares a digit vector. Computes c = a * a. @@ -265,7 +275,7 @@ dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size); * @param[in] a - the digit vector to square. * @param[in] size - the number of digits to square. */ -void bn_sqrn_low(dig_t *c, const dig_t *a, int size); +void bn_sqrn_low(dig_t *c, const dig_t *a, size_t size); /** * Divides a digit vector by another digit vector. Computes c = floor(a / b) and @@ -278,7 +288,7 @@ void bn_sqrn_low(dig_t *c, const dig_t *a, int size); * @param[in,out] b - the divisor. * @param[in] sb - the size of the divisor. */ -void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb); +void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, size_t sa, dig_t *b, size_t sb); /** * Divides a digit vector by a digit. Computes c = floor(a / digit) and @@ -290,7 +300,7 @@ void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb); * @param[in] size - the size of the dividend. * @param[in] digit - the divisor. */ -void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, int size, dig_t digit); +void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, dig_t digit, size_t size); /** * Reduces a digit vector modulo m by Montgomery's algorithm. @@ -302,7 +312,7 @@ void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, int size, dig_t digit); * @param[in] sm - the size of the modulus. * @param[in] u - the reciprocal of the modulus. */ -void bn_modn_low(dig_t *c, const dig_t *a, int sa, const dig_t *m, int sm, +void bn_modn_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *m, size_t sm, dig_t u); #endif /* !ASM */ diff --git a/include/low/relic_fb_low.h b/include/low/relic_fb_low.h index 323afcc84..4fd3f8b9f 100644 --- a/include/low/relic_fb_low.h +++ b/include/low/relic_fb_low.h @@ -85,7 +85,7 @@ void fb_addn_low(dig_t *c, const dig_t *a, const dig_t *b); * @param[in] b - the second digit vector to add. * @param[in] size - the number of digits to add. */ -void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, int size); +void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size); /** * Shifts a digit vector to the left by 1 bit. Computes c = a * z. @@ -106,7 +106,7 @@ dig_t fb_lsh1_low(dig_t *c, const dig_t *a); * @param[in] bits - the shift ammount. * @return the carry of the last digit shift. */ -dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits); +dig_t fb_lshb_low(dig_t *c, const dig_t *a, uint_t bits); /** * Shifts a digit vector to the right by 1 bit. Computes c = a / z. @@ -127,7 +127,7 @@ dig_t fb_rsh1_low(dig_t *c, const dig_t *a); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits); +dig_t fb_rshb_low(dig_t *c, const dig_t *a, uint_t bits); /** * Adds a left-shifted digit vector to another digit vector. @@ -140,7 +140,7 @@ dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits); * @param[in] bits - the shift amount. * @return the carry of the last shift. */ -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size); +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size); /** * Multiplies a digit vector by a digit. @@ -169,7 +169,7 @@ void fb_muln_low(dig_t *c, const dig_t *a, const dig_t *b); * @param[in] b - the second digit vector to multiply. * @param[in] size - the size of the digit vectors. */ -void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, int size); +void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size); /** * Multiplies two digit vectors of the same size with embedded modular diff --git a/include/low/relic_fp_low.h b/include/low/relic_fp_low.h index f31f305f0..5eb6fe474 100644 --- a/include/low/relic_fp_low.h +++ b/include/low/relic_fp_low.h @@ -216,7 +216,7 @@ dig_t fp_lsh1_low(dig_t *c, const dig_t *a); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits); +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits); /** * Shifts a digit vector to the right by 1 bit. Computes c = a >> 1. @@ -236,7 +236,7 @@ dig_t fp_rsh1_low(dig_t *c, const dig_t *a); * @param[in] bits - the shift amount. * @return the carry of the last digit shift. */ -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits); +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits); /** * Multiplies a digit vector by a digit and adds this result to another digit diff --git a/include/relic_conf.h.in b/include/relic_conf.h.in index e11eb7a44..f9bb79a25 100644 --- a/include/relic_conf.h.in +++ b/include/relic_conf.h.in @@ -266,6 +266,8 @@ /** Legendre by Fermat's Little Theorem. */ #define BASIC 1 +/** Binary method. */ +#define BINAR 2 /** Constant-time inversion by Bernstein-Yang division steps. */ #define DIVST 5 /** Constant-time inversion by Bernstein-Yang jump division steps. */ diff --git a/include/relic_eb.h b/include/relic_eb.h index 1a5da9899..d5f72482c 100644 --- a/include/relic_eb.h +++ b/include/relic_eb.h @@ -549,7 +549,7 @@ void eb_print(const eb_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int eb_size_bin(const eb_t a, int pack); +size_t eb_size_bin(const eb_t a, int pack); /** * Reads a binary elliptic curve point from a byte vector in big-endian format. diff --git a/include/relic_ed.h b/include/relic_ed.h index b30d196fe..b741ebef2 100644 --- a/include/relic_ed.h +++ b/include/relic_ed.h @@ -830,7 +830,7 @@ int ed_on_curve(const ed_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ed_size_bin(const ed_t a, int pack); +size_t ed_size_bin(const ed_t a, int pack); /** * Reads an Edwards elliptic curve point from a byte vector in big-endian format. diff --git a/include/relic_ep.h b/include/relic_ep.h index 6daa5b96d..870eedca8 100644 --- a/include/relic_ep.h +++ b/include/relic_ep.h @@ -839,7 +839,7 @@ void ep_print(const ep_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ep_size_bin(const ep_t a, int pack); +size_t ep_size_bin(const ep_t a, int pack); /** * Reads a prime elliptic curve point from a byte vector in big-endian format. diff --git a/include/relic_epx.h b/include/relic_epx.h index ffd06f2ee..050e6ac71 100644 --- a/include/relic_epx.h +++ b/include/relic_epx.h @@ -1131,7 +1131,7 @@ void ep2_print(const ep2_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ep2_size_bin(const ep2_t a, int pack); +size_t ep2_size_bin(const ep2_t a, int pack); /** * Reads a prime elliptic curve point over a quadratic extension from a byte @@ -1798,7 +1798,7 @@ void ep3_print(const ep3_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ep3_size_bin(const ep3_t a, int pack); +size_t ep3_size_bin(const ep3_t a, int pack); /** * Reads a prime elliptic curve point over a cubic extension from a byte @@ -2442,7 +2442,7 @@ void ep4_print(const ep4_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ep4_size_bin(const ep4_t a, int pack); +size_t ep4_size_bin(const ep4_t a, int pack); /** * Reads a prime elliptic curve point over a quartic extension from a byte @@ -3087,7 +3087,7 @@ void ep8_print(const ep8_t p); * @param[in] pack - the flag to indicate compression. * @return the number of bytes. */ -int ep8_size_bin(const ep8_t a, int pack); +size_t ep8_size_bin(const ep8_t a, int pack); /** * Reads a prime elliptic curve point over an octic extension from a byte diff --git a/include/relic_fp.h b/include/relic_fp.h index 2ae5474af..53c85cede 100644 --- a/include/relic_fp.h +++ b/include/relic_fp.h @@ -406,6 +406,8 @@ typedef rlc_align dig_t fp_st[RLC_FP_DIGS + RLC_PAD(RLC_FP_BYTES)/(RLC_DIG / 8)] */ #if FP_SMB == BASIC #define fp_smb(A) fp_smb_basic(A) +#elif FP_SMB == BINAR +#define fp_smb(A) fp_smb_binar(A) #elif FP_SMB == DIVST #define fp_smb(A) fp_smb_divst(A) #elif FP_SMB == JMPDS @@ -1155,6 +1157,14 @@ void fp_inv_sim(fp_t *c, const fp_t *a, int n); */ int fp_smb_basic(const fp_t a); +/** + * Computes Legendre symbol of a prime field element using the binary method. + * + * @param[in] a - the prime field element to compute. + * @return the result. + */ +int fp_smb_binar(const fp_t a); + /** * Computes Legendre symbol of a prime field element using the constant-time * division step approach by Bernstein and Bo-Yin Yang. diff --git a/include/relic_label.h b/include/relic_label.h index e030b9128..c51625c4e 100644 --- a/include/relic_label.h +++ b/include/relic_label.h @@ -543,6 +543,7 @@ #undef fp_inv_lower #undef fp_inv_sim #undef fp_smb_basic +#undef fp_smb_binar #undef fp_smb_divst #undef fp_smb_jmpds #undef fp_smb_lower @@ -641,6 +642,7 @@ #define fp_inv_lower RLC_PREFIX(fp_inv_lower) #define fp_inv_sim RLC_PREFIX(fp_inv_sim) #define fp_smb_basic RLC_PREFIX(fp_smb_basic) +#define fp_smb_binar RLC_PREFIX(fp_smb_binar) #define fp_smb_divst RLC_PREFIX(fp_smb_divst) #define fp_smb_jmpds RLC_PREFIX(fp_smb_jmpds) #define fp_smb_lower RLC_PREFIX(fp_smb_lower) diff --git a/src/bn/relic_bn_div.c b/src/bn/relic_bn_div.c index a3a32a754..ddd858b04 100644 --- a/src/bn/relic_bn_div.c +++ b/src/bn/relic_bn_div.c @@ -170,7 +170,7 @@ void bn_div_dig(bn_t c, const bn_t a, dig_t b) { bn_new(q); bn_copy(q, a); - bn_div1_low(q->dp, &r, (const dig_t *)a->dp, a->used, b); + bn_div1_low(q->dp, &r, (const dig_t *)a->dp, b, a->used); if (c != NULL) { bn_copy(c, q); } @@ -208,7 +208,7 @@ void bn_div_rem_dig(bn_t c, dig_t *d, const bn_t a, dig_t b) { bn_new(q); bn_copy(q, a); - bn_div1_low(q->dp, &r, (const dig_t *)a->dp, a->used, b); + bn_div1_low(q->dp, &r, (const dig_t *)a->dp, b, a->used); if (c != NULL) { bn_copy(c, q); diff --git a/src/bn/relic_bn_rec.c b/src/bn/relic_bn_rec.c index 9115b7f7c..d48b63660 100644 --- a/src/bn/relic_bn_rec.c +++ b/src/bn/relic_bn_rec.c @@ -876,6 +876,45 @@ void bn_rec_glv(bn_t k0, bn_t k1, const bn_t k, const bn_t n, const bn_t *v1, } } +void bn_rec_sac(bn_t *b, bn_t *k, size_t m, bn_t n) { + /* Assume k0 is the sign-aligner. */ + bn_t *t = RLC_ALLOCA(bn_t, m); + size_t l = RLC_CEIL(bn_bits(n), m) + 1; + + if (t == NULL) { + RLC_THROW(ERR_NO_MEMORY); + return; + } + + RLC_TRY { + for (size_t i = 0; i < m; i++) { + bn_null(t[i]); + bn_new(t[i]); + bn_copy(t[i], k[i]); + } + + bn_set_bit(b[0], l - 1, 0); + for (size_t i = 0; i < l - 1; i++) { + bn_set_bit(b[0], i, 1 - bn_get_bit(k[0], i + 1)); + } + for (size_t j = 1; j < m; j++) { + for (size_t i = 0; i < l; i++) { + uint8_t bji = bn_get_bit(t[j], 0); + bn_set_bit(b[j], i, bji); + bn_hlv(t[j], t[j]); + bn_add_dig(t[j], t[j], bji & bn_get_bit(b[0], i)); + } + } + } RLC_CATCH_ANY { + RLC_THROW(ERR_CAUGHT); + } RLC_FINALLY { + for (size_t i = 0; i < m; i++) { + bn_free(t[i]); + RLC_FREE(t); + } + } +} + void bn_rec_frb(bn_t *ki, int sub, const bn_t k, const bn_t x, const bn_t n, int cof) { int i, l, sk, sx; diff --git a/src/bn/relic_bn_util.c b/src/bn/relic_bn_util.c index a1e9f0e0e..8ec06b744 100644 --- a/src/bn/relic_bn_util.c +++ b/src/bn/relic_bn_util.c @@ -106,7 +106,7 @@ int bn_is_even(const bn_t a) { } size_t bn_bits(const bn_t a) { - int bits; + size_t bits; if (bn_is_zero(a)) { return 0; @@ -458,7 +458,7 @@ 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) { - int size, k; + size_t size, k; dig_t d; size = bn_size_bin(a); diff --git a/src/cp/relic_cp_bdpe.c b/src/cp/relic_cp_bdpe.c index 73aea6e14..c4a3ab56a 100644 --- a/src/cp/relic_cp_bdpe.c +++ b/src/cp/relic_cp_bdpe.c @@ -109,7 +109,8 @@ int cp_bdpe_gen(bdpe_t pub, bdpe_t prv, dig_t block, size_t bits) { int cp_bdpe_enc(uint8_t *out, size_t *out_len, dig_t in, const bdpe_t pub) { bn_t m, u; - int size, result = RLC_OK; + size_t size; + int result = RLC_OK; bn_null(m); bn_null(u); @@ -154,7 +155,8 @@ int cp_bdpe_enc(uint8_t *out, size_t *out_len, dig_t in, const bdpe_t pub) { int cp_bdpe_dec(dig_t *out, const uint8_t *in, size_t in_len, const bdpe_t prv) { bn_t m, t, z; - int size, result = RLC_OK; + size_t size; + int result = RLC_OK; dig_t i; size = bn_size_bin(prv->n); diff --git a/src/cp/relic_cp_rabin.c b/src/cp/relic_cp_rabin.c index 08ec3ab85..39ea8a7e5 100644 --- a/src/cp/relic_cp_rabin.c +++ b/src/cp/relic_cp_rabin.c @@ -98,7 +98,8 @@ int cp_rabin_gen(rabin_t pub, rabin_t prv, size_t bits) { int cp_rabin_enc(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, const rabin_t pub) { bn_t m, t; - int size, result = RLC_OK; + size_t size; + int result = RLC_OK; bn_null(m); bn_null(t); @@ -149,7 +150,8 @@ int cp_rabin_enc(uint8_t *out, size_t *out_len, const uint8_t *in, int cp_rabin_dec(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, const rabin_t prv) { bn_t m, m0, m1, t, n; - int size, result = RLC_OK; + size_t size; + int result = RLC_OK; uint8_t pad; if (in_len < RABIN_PAD_LEN) { diff --git a/src/cp/relic_cp_rsa.c b/src/cp/relic_cp_rsa.c index 863f4d28e..4bfba223a 100644 --- a/src/cp/relic_cp_rsa.c +++ b/src/cp/relic_cp_rsa.c @@ -118,7 +118,8 @@ * @param[in] operation - flag to indicate the operation type. * @return RLC_ERR if errors occurred, RLC_OK otherwise. */ -static int pad_basic(bn_t m, int *p_len, int m_len, int k_len, int operation) { +static int pad_basic(bn_t m, size_t *p_len, size_t m_len, size_t k_len, + int operation) { uint8_t pad = 0; int result = RLC_ERR; bn_t t; @@ -244,7 +245,8 @@ static uint8_t *hash_id(int md, int *len) { * @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, size_t m_len, size_t k_len, int op) { +static int pad_pkcs1(bn_t m, size_t *p_len, size_t m_len, size_t k_len, + int operation) { uint8_t *id, pad = 0; size_t len = 0; int result = RLC_ERR; @@ -255,7 +257,7 @@ static int pad_pkcs1(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) { RLC_TRY { bn_new(t); - switch (op) { + switch (operation) { case RSA_ENC: /* EB = 00 | 02 | PS | 00 | D. */ bn_zero(m); @@ -420,7 +422,8 @@ static int pad_pkcs1(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) { * @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, size_t m_len, size_t k_len, int op) { +static int pad_pkcs2(bn_t m, size_t *p_len, size_t m_len, size_t k_len, + int operation) { uint8_t pad, h1[RLC_MD_LEN], h2[RLC_MD_LEN]; uint8_t *mask = RLC_ALLOCA(uint8_t, k_len); int result = RLC_ERR; @@ -431,7 +434,7 @@ static int pad_pkcs2(bn_t m, int *p_len, size_t m_len, size_t k_len, int op) { RLC_TRY { bn_new(t); - switch (op) { + switch (operation) { case RSA_ENC: /* DB = lHash | PS | 01 | D. */ md_map(h1, NULL, 0); @@ -667,7 +670,8 @@ int cp_rsa_gen(rsa_t pub, rsa_t prv, size_t bits) { int cp_rsa_enc(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, const rsa_t pub) { bn_t m, eb; - int size, pad_len, result = RLC_OK; + size_t size, pad_len; + int result = RLC_OK; bn_null(m); bn_null(eb); @@ -725,7 +729,8 @@ int cp_rsa_enc(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, int cp_rsa_dec(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, const rsa_t prv) { bn_t m, eb; - int size, pad_len, result = RLC_OK; + size_t size, pad_len; + int result = RLC_OK; bn_null(m); bn_null(eb); @@ -781,7 +786,8 @@ int cp_rsa_dec(uint8_t *out, size_t *out_len, const uint8_t *in, size_t in_len, int cp_rsa_sig(uint8_t *sig, size_t *sig_len, const uint8_t *msg, size_t msg_len, int hash, const rsa_t prv) { bn_t m, eb; - int pad_len, size, result = RLC_OK; + size_t size, pad_len; + int result = RLC_OK; uint8_t h[RLC_MD_LEN]; if (prv == NULL || msg_len < 0) { @@ -870,7 +876,8 @@ int cp_rsa_sig(uint8_t *sig, size_t *sig_len, const uint8_t *msg, int cp_rsa_ver(uint8_t *sig, size_t sig_len, const uint8_t *msg, size_t msg_len, int hash, const rsa_t pub) { bn_t m, eb; - int size, pad_len, result; + size_t size, pad_len; + int result; uint8_t *h1 = RLC_ALLOCA(uint8_t, RLC_MAX(msg_len, RLC_MD_LEN) + 8); uint8_t *h2 = RLC_ALLOCA(uint8_t, RLC_MAX(msg_len, RLC_MD_LEN)); diff --git a/src/eb/relic_eb_util.c b/src/eb/relic_eb_util.c index 92316d739..761ffee10 100644 --- a/src/eb/relic_eb_util.c +++ b/src/eb/relic_eb_util.c @@ -489,8 +489,8 @@ void eb_print(const eb_t p) { fb_print(p->z); } -int eb_size_bin(const eb_t a, int pack) { - int size = 0; +size_t eb_size_bin(const eb_t a, int pack) { + size_t size = 0; if (eb_is_infty(a)) { return 1; diff --git a/src/ed/relic_ed_util.c b/src/ed/relic_ed_util.c index 698780ac5..7021ee956 100644 --- a/src/ed/relic_ed_util.c +++ b/src/ed/relic_ed_util.c @@ -198,8 +198,8 @@ void ed_print(const ed_t p) { fp_print(p->z); } -int ed_size_bin(const ed_t a, int pack) { - int size = 0; +size_t ed_size_bin(const ed_t a, int pack) { + size_t size = 0; if (ed_is_infty(a)) { return 1; diff --git a/src/ep/relic_ep_curve.c b/src/ep/relic_ep_curve.c index 707a7cfdd..aae056af0 100644 --- a/src/ep/relic_ep_curve.c +++ b/src/ep/relic_ep_curve.c @@ -433,7 +433,7 @@ void ep_curve_set_super(const fp_t a, const fp_t b, const ep_t g, const bn_t r, void ep_curve_set_endom(const fp_t a, const fp_t b, const ep_t g, const bn_t r, const bn_t h, const fp_t beta, const bn_t l, int ctmap) { - int bits = bn_bits(r); + uint_t bits = bn_bits(r); ctx_t *ctx = core_get(); ctx->ep_is_endom = 1; ctx->ep_is_super = 0; diff --git a/src/ep/relic_ep_util.c b/src/ep/relic_ep_util.c index a39269949..09fb14431 100644 --- a/src/ep/relic_ep_util.c +++ b/src/ep/relic_ep_util.c @@ -223,8 +223,8 @@ void ep_print(const ep_t p) { fp_print(p->z); } -int ep_size_bin(const ep_t a, int pack) { - int size = 0; +size_t ep_size_bin(const ep_t a, int pack) { + size_t size = 0; if (ep_is_infty(a)) { return 1; diff --git a/src/epx/relic_ep2_util.c b/src/epx/relic_ep2_util.c index 4ac588532..ec7e3cea3 100644 --- a/src/epx/relic_ep2_util.c +++ b/src/epx/relic_ep2_util.c @@ -224,9 +224,9 @@ void ep2_print(const ep2_t p) { fp2_print(p->z); } -int ep2_size_bin(const ep2_t a, int pack) { +size_t ep2_size_bin(const ep2_t a, int pack) { ep2_t t; - int size = 0; + size_t size = 0; ep2_null(t); diff --git a/src/epx/relic_ep3_util.c b/src/epx/relic_ep3_util.c index a3b9676b2..32c1e9ddd 100644 --- a/src/epx/relic_ep3_util.c +++ b/src/epx/relic_ep3_util.c @@ -223,9 +223,9 @@ void ep3_print(const ep3_t p) { fp3_print(p->z); } -int ep3_size_bin(const ep3_t a, int pack) { +size_t ep3_size_bin(const ep3_t a, int pack) { ep3_t t; - int size = 0; + size_t size = 0; ep3_null(t); diff --git a/src/epx/relic_ep4_util.c b/src/epx/relic_ep4_util.c index 8564d9cca..8a35a1eed 100644 --- a/src/epx/relic_ep4_util.c +++ b/src/epx/relic_ep4_util.c @@ -223,9 +223,9 @@ void ep4_print(const ep4_t p) { fp4_print(p->z); } -int ep4_size_bin(const ep4_t a, int pack) { +size_t ep4_size_bin(const ep4_t a, int pack) { ep4_t t; - int size = 0; + size_t size = 0; ep4_null(t); diff --git a/src/epx/relic_ep8_util.c b/src/epx/relic_ep8_util.c index 0c44ff045..4ce0c4fa5 100644 --- a/src/epx/relic_ep8_util.c +++ b/src/epx/relic_ep8_util.c @@ -225,9 +225,9 @@ void ep8_print(const ep8_t p) { fp8_print(p->z); } -int ep8_size_bin(const ep8_t a, int pack) { +size_t ep8_size_bin(const ep8_t a, int pack) { ep8_t t; - int size = 0; + size_t size = 0; ep8_null(t); diff --git a/src/fb/relic_fb_mul.c b/src/fb/relic_fb_mul.c index d5bed8e4e..e77b44699 100644 --- a/src/fb/relic_fb_mul.c +++ b/src/fb/relic_fb_mul.c @@ -51,8 +51,8 @@ * @param b - the second binary field element. * @param size - the number of digits to multiply. */ -static void fb_mul_basic_imp(dig_t *c, const dig_t *a, const dig_t *b, int size) { - int i; +static void fb_mul_basic_imp(dig_t *c, const dig_t *a, const dig_t *b, + size_t size) { dv_t s; dv_null(s); @@ -68,7 +68,7 @@ static void fb_mul_basic_imp(dig_t *c, const dig_t *a, const dig_t *b, int size) if (a[0] & 1) { dv_copy(c, b, size); } - for (i = 1; i <= (RLC_DIG * size) - 1; i++) { + for (uint_t i = 1; i <= (RLC_DIG * size) - 1; i++) { fb_lsh1_low(s, s); fb_rdc(s, s); if (fb_get_bit(a, i)) { @@ -96,7 +96,7 @@ static void fb_mul_basic_imp(dig_t *c, const dig_t *a, const dig_t *b, int size) * @param[in] size - the number of digits to multiply. * @param[in] level - the number of Karatsuba steps to apply. */ -static void fb_mul_karat_imp(dv_t c, const fb_t a, const fb_t b, int size, +static void fb_mul_karat_imp(dv_t c, const fb_t a, const fb_t b, size_t size, int level) { int i, h, h1; dv_t a1, b1, ab; diff --git a/src/fp/relic_fp_inv.c b/src/fp/relic_fp_inv.c index 4ca9b265b..724619c3b 100644 --- a/src/fp/relic_fp_inv.c +++ b/src/fp/relic_fp_inv.c @@ -39,25 +39,7 @@ #if FP_INV == JMPDS || !defined(STRIP) -/** - * Conditionally negate a digit vector using two's complement representation. - * - * @param[out] c - the result. - * @param[in] a - the digit vector to conditionally negate. - * @param[in] sa - the sign of the digit vector. - * @param[in] n - the number of digits to conditionally negate. - */ -static void bn_negs_low(dig_t c[], const dig_t a[], dig_t sa, size_t n) { - dig_t carry = sa & 1; - - sa = -sa; - for (int i = 0; i < n; i++) { - c[i] = (a[i] ^ sa) + carry; - carry = (c[i] < carry); - } -} - -static void bn_mul2_low(dig_t *c, const dig_t *a, dis_t digit, int size) { +static void bn_mul2_low(dig_t *c, const dig_t *a, dis_t digit, size_t size) { int sd = digit >> (RLC_DIG - 1); digit = (digit ^ sd) - sd; c[size] = bn_mul1_low(c, a, digit, size); diff --git a/src/fp/relic_fp_mul.c b/src/fp/relic_fp_mul.c index a9cf0aa67..dc9926abc 100644 --- a/src/fp/relic_fp_mul.c +++ b/src/fp/relic_fp_mul.c @@ -48,8 +48,8 @@ * @param[in] size - the number of digits to multiply. * @param[in] level - the number of Karatsuba steps to apply. */ -static void fp_mul_karat_imp(dv_t c, const fp_t a, const fp_t b, int size, - int level) { +static void fp_mul_karat_imp(dv_t c, const fp_t a, const fp_t b, size_t size, + uint_t level) { /* Compute half the digits of a or b. */ int h = size >> 1; int h1 = size - h; diff --git a/src/fp/relic_fp_smb.c b/src/fp/relic_fp_smb.c index bdab95d3d..785e173c8 100644 --- a/src/fp/relic_fp_smb.c +++ b/src/fp/relic_fp_smb.c @@ -39,24 +39,6 @@ #if FP_SMB == JMPDS || !defined(STRIP) -/** - * Conditionally negate a digit vector using two's complement representation. - * - * @param[out] c - the result. - * @param[in] a - the digit vector to conditionally negate. - * @param[in] sa - the sign of the digit vector. - * @param[in] n - the number of digits to conditionally negate. - */ -static void bn_negs_low(dig_t c[], const dig_t a[], dig_t sa, size_t n) { - dig_t carry = sa & 1; - - sa = -sa; - for (int i = 0; i < n; i++) { - c[i] = (a[i] ^ sa) + carry; - carry = (c[i] < carry); - } -} - static dis_t jumpdivstep(dis_t m[4], dig_t *k, dis_t delta, dis_t y, dis_t x, int s) { dig_t d0, t0, t1, t2, c0, c1, yi, ai = 1, bi = 0, ci = 0, di = 1, u = 0; @@ -163,6 +145,204 @@ int fp_smb_basic(const fp_t a) { #endif +#if FP_SMB == BINAR || !defined(STRIP) + +static inline dig_t is_zero(dig_t l) { + l = ~l & (l - 1); + return (l >> (RLC_DIG - 1)); +} + +static dig_t lshift_2(dig_t hi, dig_t lo, size_t l) { + size_t r = RLC_DIG - l; + dig_t mask = 0 - (is_zero(l) ^ 1); + return (hi << (l & (RLC_DIG - 1))) | ((lo & mask) >> (r & (RLC_DIG - 1))); +} + +static void ab_approximation_n(dig_t a_[2], const dig_t a[], + dig_t b_[2], const dig_t b[]) { + dig_t a_hi, a_lo, b_hi, b_lo, mask; + size_t i; + + i = RLC_FP_DIGS - 1; + a_hi = a[i], a_lo = a[i - 1]; + b_hi = b[i], b_lo = b[i - 1]; + for (int j = i - 1; j >= 0; j--) { + mask = 0 - is_zero(a_hi | b_hi); + a_hi = ((a_lo ^ a_hi) & mask) ^ a_hi; + b_hi = ((b_lo ^ b_hi) & mask) ^ b_hi; + a_lo = ((a[j] ^ a_lo) & mask) ^ a_lo; + b_lo = ((b[j] ^ b_lo) & mask) ^ b_lo; + } + i = RLC_DIG - util_bits_dig(a_hi | b_hi); + /* |i| can be RLC_DIG if all a[2..]|b[2..] were zeros */ + + a_[0] = a[0], a_[1] = lshift_2(a_hi, a_lo, i); + b_[0] = b[0], b_[1] = lshift_2(b_hi, b_lo, i); +} + +static dig_t smul_n_shift_n(dig_t ret[], const dig_t a[], dig_t *f_, + const dig_t b[], dig_t *g_) { + dig_t a_[RLC_FP_DIGS + 1], b_[RLC_FP_DIGS + 1], f, g, neg, carry, hi; + size_t i; + + /* |a|*|f_| */ + f = *f_; + neg = 0 - RLC_SIGN(f); + f = (f ^ neg) - neg; /* ensure |f| is positive */ + bn_negs_low(a_, a, -neg, RLC_FP_DIGS); + hi = fp_mul1_low(a_, a_, f); + a_[RLC_FP_DIGS] = hi - (f & neg); + + /* |b|*|g_| */ + g = *g_; + neg = 0 - RLC_SIGN(g); + g = (g ^ neg) - neg; /* ensure |g| is positive */ + bn_negs_low(b_, b, -neg, RLC_FP_DIGS); + hi = fp_mul1_low(b_, b_, g); + b_[RLC_FP_DIGS] = hi - (g & neg); + + /* |a|*|f_| + |b|*|g_| */ + (void)bn_addn_low(a_, a_, b_, RLC_FP_DIGS + 1); + + /* (|a|*|f_| + |b|*|g_|) >> k */ + for (carry = a_[0], i = 0; i < RLC_FP_DIGS; i++) { + hi = carry >> (RLC_DIG - 2); + carry = a_[i + 1]; + ret[i] = hi | (carry << 2); + } + + /* ensure result is non-negative, fix up |f_| and |g_| accordingly */ + neg = 0 - RLC_SIGN(carry); + *f_ = (*f_ ^ neg) - neg; + *g_ = (*g_ ^ neg) - neg; + bn_negs_low(ret, ret, -neg, RLC_FP_DIGS); + + return neg; +} + +/* + * Copy of inner_loop_n above, but with |L| updates. + */ +static dig_t legendre_loop_n(dig_t l, dig_t m[4], const dig_t a_[2], + const dig_t b_[2], size_t n) { + dig_t limbx, f0 = 1, g0 = 0, f1 = 0, g1 = 1; + dig_t a_lo, a_hi, b_lo, b_hi, t_lo, t_hi, odd, borrow, xorm; + + a_lo = a_[0], a_hi = a_[1]; + b_lo = b_[0], b_hi = b_[1]; + + while (n--) { + odd = 0 - (a_lo & 1); + + /* a_ -= b_ if a_ is odd */ + t_lo = a_lo, t_hi = a_hi; + + borrow = 0; + limbx = a_lo - (b_lo & odd); + borrow = (a_lo < limbx); + a_lo = limbx; + + limbx = a_hi - (b_hi & odd); + xorm = limbx - borrow; + borrow = -((a_hi < limbx) || (borrow && !limbx)); + a_hi = xorm; + + l += ((t_lo & b_lo) >> 1) & borrow; + + /* negate a_-b_ if it borrowed */ + a_lo ^= borrow; + a_hi ^= borrow; + limbx = a_lo + (borrow & 1); + a_hi += (a_lo < limbx); + a_lo = limbx; + + /* b_=a_ if a_-b_ borrowed */ + b_lo = ((t_lo ^ b_lo) & borrow) ^ b_lo; + b_hi = ((t_hi ^ b_hi) & borrow) ^ b_hi; + + /* exchange f0 and f1 if a_-b_ borrowed */ + xorm = (f0 ^ f1) & borrow; + f0 ^= xorm; + f1 ^= xorm; + + /* exchange g0 and g1 if a_-b_ borrowed */ + xorm = (g0 ^ g1) & borrow; + g0 ^= xorm; + g1 ^= xorm; + + /* subtract if a_ was odd */ + f0 -= f1 & odd; + g0 -= g1 & odd; + + f1 <<= 1; + g1 <<= 1; + a_lo >>= 1; + a_lo |= a_hi << (RLC_DIG - 1); + a_hi >>= 1; + + l += (b_lo + 2) >> 2; + } + + m[0] = f0; + m[1] = g0; + m[2] = f1; + m[3] = g1; + + return l; +} + +int fp_smb_binar(const fp_t a) { + const size_t s = RLC_DIG - 2; + dv_t x, y, t; + dig_t a_[2], b_[2], neg, l = 0, m[4]; + bn_t _t; + int iterations = 2 * RLC_FP_DIGS * RLC_DIG; + + if (fp_is_zero(a)) { + return 0; + } + + bn_null(_t); + dv_null(x); + dv_null(y); + dv_null(t); + + RLC_TRY { + bn_new(_t); + dv_new(x); + dv_new(y); + dv_new(t); + + fp_prime_back(_t, a); + dv_zero(x, RLC_FP_DIGS); + dv_copy(x, _t->dp, _t->used); + dv_copy(y, fp_prime_get(), RLC_FP_DIGS); + + for (size_t i = 0; i < iterations / s; i++) { + ab_approximation_n(a_, x, b_, y); + l = legendre_loop_n(l, m, a_, b_, s); + neg = smul_n_shift_n(t, x, &m[0], y, &m[1]); + (void)smul_n_shift_n(y, x, &m[2], y, &m[3]); + fp_copy(x, t); + l += (y[0] >> 1) & neg; + } + + l = legendre_loop_n(l, m, x, y, iterations % s); + + } RLC_CATCH_ANY { + RLC_THROW(ERR_CAUGHT) + } RLC_FINALLY { + bn_free(_t); + dv_free(x); + dv_free(y); + dv_free(t); + } + + return (l & 1 ? -1 : 1); +} + +#endif + #if FP_SMB == DIVST || !defined(STRIP) int fp_smb_divst(const fp_t a) { diff --git a/src/fp/relic_fp_sqr.c b/src/fp/relic_fp_sqr.c index 2d2f83be0..0365cc412 100644 --- a/src/fp/relic_fp_sqr.c +++ b/src/fp/relic_fp_sqr.c @@ -49,7 +49,7 @@ * @param[in] size - the number of digits to square. * @param[in] level - the number of Karatsuba steps to apply. */ -static void fp_sqr_karat_imp(dv_t c, const fp_t a, int size, int level) { +static void fp_sqr_karat_imp(dv_t c, const fp_t a, size_t size, uint_t level) { int i, h, h1; dv_t t0, t1, a0a0, a1a1; dig_t carry; diff --git a/src/fp/relic_fp_util.c b/src/fp/relic_fp_util.c index 9556a9fd8..ba4d66626 100644 --- a/src/fp/relic_fp_util.c +++ b/src/fp/relic_fp_util.c @@ -122,7 +122,7 @@ void fp_set_dig(fp_t c, dig_t a) { } void fp_rand(fp_t a) { - int bits, digits; + uint_t bits, digits; rand_bytes((uint8_t *)a, RLC_FP_DIGS * sizeof(dig_t)); diff --git a/src/low/avr-asm-158/relic_bn_shift_low.c b/src/low/avr-asm-158/relic_bn_shift_low.c index 52d70ad78..5a147b701 100755 --- a/src/low/avr-asm-158/relic_bn_shift_low.c +++ b/src/low/avr-asm-158/relic_bn_shift_low.c @@ -37,7 +37,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift; @@ -57,7 +57,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { return carry; } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift; diff --git a/src/low/avr-asm-158/relic_fp_shift_low.c b/src/low/avr-asm-158/relic_fp_shift_low.c index 17bf95535..c2a1d5245 100755 --- a/src/low/avr-asm-158/relic_fp_shift_low.c +++ b/src/low/avr-asm-158/relic_fp_shift_low.c @@ -36,7 +36,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -55,7 +55,7 @@ dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { return carry; } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; diff --git a/src/low/avr-asm-163/relic_bn_shift_low.c b/src/low/avr-asm-163/relic_bn_shift_low.c index c480af7da..e4a92d6ed 100755 --- a/src/low/avr-asm-163/relic_bn_shift_low.c +++ b/src/low/avr-asm-163/relic_bn_shift_low.c @@ -37,7 +37,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; @@ -55,7 +55,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { return carry; } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; diff --git a/src/low/avr-asm-163/relic_fb_shift_low.c b/src/low/avr-asm-163/relic_fb_shift_low.c index f26c94bf8..b8acf24c7 100755 --- a/src/low/avr-asm-163/relic_fb_shift_low.c +++ b/src/low/avr-asm-163/relic_fb_shift_low.c @@ -46,20 +46,20 @@ * @param size - the size of the digit vector. * @return the carry of the last shift. */ -dig_t fb_lsha1_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha2_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha3_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha4_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha5_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha6_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lsha7_low(dig_t *c, const dig_t *a, int size); +dig_t fb_lsha1_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha2_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha3_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha4_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha5_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha6_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lsha7_low(dig_t *c, const dig_t *a, size_t size); /*@} */ /*============================================================================*/ /* Public definitions */ /*============================================================================*/ -dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, shift; @@ -80,7 +80,7 @@ dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits) { return carry; } -dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -106,7 +106,7 @@ dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { #if FB_INV == EXGCD || !defined(STRIP) -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size) { if (bits == 1) return fb_lsha1_low(c, a, size); if (bits == 2) @@ -129,7 +129,7 @@ dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { #else -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size) { int i, j; dig_t b1, b2; diff --git a/src/low/curve2251-sse/relic_fb_add_low.c b/src/low/curve2251-sse/relic_fb_add_low.c index 02e999eff..ea60bcb02 100755 --- a/src/low/curve2251-sse/relic_fb_add_low.c +++ b/src/low/curve2251-sse/relic_fb_add_low.c @@ -61,7 +61,7 @@ void fb_addn_low(dig_t *c, const dig_t *a, const dig_t *b) { *(__m128i *)(c + 2) = XOR(*(__m128i*)(a + 2), *(__m128i*)(b + 2)); } -void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { if (size == 2 * RLC_FB_DIGS) { *(__m128i *)c = XOR(*(__m128i*)(a), *(__m128i*)(b)); *(__m128i *)(c + 2) = XOR(*(__m128i*)(a + 2), *(__m128i*)(b + 2)); diff --git a/src/low/curve2251-sse/relic_fb_mul_low.c b/src/low/curve2251-sse/relic_fb_mul_low.c index 071895ccd..ca8f19073 100755 --- a/src/low/curve2251-sse/relic_fb_mul_low.c +++ b/src/low/curve2251-sse/relic_fb_mul_low.c @@ -44,7 +44,7 @@ /* Public definitions */ /*============================================================================*/ -void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { rlc_align dig_t t[16][size + 1]; dig_t u, r0, r1, r2, r4, r8, *tmpc; const dig_t *tmpa; diff --git a/src/low/easy/relic_bn_add_low.c b/src/low/easy/relic_bn_add_low.c index c2e2f316d..2bb27880f 100644 --- a/src/low/easy/relic_bn_add_low.c +++ b/src/low/easy/relic_bn_add_low.c @@ -37,7 +37,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { int i; register dig_t carry, r0; @@ -53,7 +53,7 @@ dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { return carry; } -dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { int i; register dig_t carry, c0, c1, r0, r1; @@ -69,7 +69,7 @@ dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { return carry; } -dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { int i; dig_t carry, r0; @@ -85,7 +85,7 @@ dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { return carry; } -dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { int i; dig_t carry, r0, diff; @@ -99,3 +99,13 @@ dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { } return carry; } + +void bn_negs_low(dig_t *c, const dig_t *a, dig_t sa, size_t size) { + dig_t carry = sa & 1; + + sa = -sa; + for (int i = 0; i < size; i++) { + c[i] = (a[i] ^ sa) + carry; + carry = (c[i] < carry); + } +} diff --git a/src/low/easy/relic_bn_div_low.c b/src/low/easy/relic_bn_div_low.c index c03e37fa9..8ca8d997f 100644 --- a/src/low/easy/relic_bn_div_low.c +++ b/src/low/easy/relic_bn_div_low.c @@ -38,7 +38,7 @@ /* Public definitions */ /*============================================================================*/ -void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) { +void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, size_t sa, dig_t *b, size_t sb) { int norm, i, n, t, sd; dig_t carry, t1[3], t2[3]; @@ -125,7 +125,7 @@ void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) { bn_rshb_low(d, a, sb, norm); } -void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, int size, dig_t b) { +void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, dig_t b, size_t size) { dig_t q, r, w = 0; for (int i = size - 1; i >= 0; i--) { diff --git a/src/low/easy/relic_bn_mod_low.c b/src/low/easy/relic_bn_mod_low.c index 304422580..1ffe116a8 100644 --- a/src/low/easy/relic_bn_mod_low.c +++ b/src/low/easy/relic_bn_mod_low.c @@ -38,7 +38,8 @@ /* Public definitions */ /*============================================================================*/ -void bn_modn_low(dig_t *c, const dig_t *a, int sa, const dig_t *m, int sm, dig_t u) { +void bn_modn_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *m, size_t sm, + dig_t u) { int i, j; dig_t t, r0, r1, r2; dig_t *tmp, *tmpc; diff --git a/src/low/easy/relic_bn_mul_low.c b/src/low/easy/relic_bn_mul_low.c index 4233d2eb5..882127e46 100644 --- a/src/low/easy/relic_bn_mul_low.c +++ b/src/low/easy/relic_bn_mul_low.c @@ -38,7 +38,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t _c, r0, r1, carry = 0; for (int i = 0; i < size; i++, a++, c++) { /* Multiply the digit *a by d and accumulate with the previous @@ -54,7 +54,7 @@ dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { return carry; } -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t r0, r1, carry = 0; for (int i = 0; i < size; i++, a++, c++) { RLC_MUL_DIG(r1, r0, *a, digit); @@ -65,7 +65,7 @@ dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { } dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, - int size) { + size_t size) { dig_t r, _c, c0, c1, sign, sd = digit >> (RLC_DIG - 1); sa = -sa; @@ -88,7 +88,7 @@ dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, return (c0 ^ sign) + c1; } -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { int i, j; const dig_t *tmpa, *tmpb; dig_t r0, r1, r2; @@ -118,8 +118,8 @@ void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { } } -void bn_muld_low(dig_t *c, const dig_t *a, int sa, const dig_t *b, int sb, - int l, int h) { +void bn_muld_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *b, size_t sb, + uint_t l, uint_t h) { int i, j, ta; const dig_t *tmpa, *tmpb; dig_t r0, r1, r2; diff --git a/src/low/easy/relic_bn_shift_low.c b/src/low/easy/relic_bn_shift_low.c index d333df931..6f8705379 100644 --- a/src/low/easy/relic_bn_shift_low.c +++ b/src/low/easy/relic_bn_shift_low.c @@ -37,7 +37,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { int i; dig_t r, carry; @@ -53,7 +53,7 @@ dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { return carry; } -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; @@ -71,7 +71,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { return carry; } -void bn_lshd_low(dig_t *c, const dig_t *a, int size, int digits) { +void bn_lshd_low(dig_t *c, const dig_t *a, size_t size, int digits) { dig_t *top; const dig_t *bot; int i; @@ -87,7 +87,7 @@ void bn_lshd_low(dig_t *c, const dig_t *a, int size, int digits) { } } -dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { int i; dig_t r, carry; @@ -106,7 +106,7 @@ dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size) { return carry; } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; @@ -127,7 +127,7 @@ dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { return carry; } -dig_t bn_rshs_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { dig_t r, carry, shift, mask; /* Prepare the bit mask. */ diff --git a/src/low/easy/relic_bn_sqr_low.c b/src/low/easy/relic_bn_sqr_low.c index ad4872f9f..9b83e4a4f 100644 --- a/src/low/easy/relic_bn_sqr_low.c +++ b/src/low/easy/relic_bn_sqr_low.c @@ -38,7 +38,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_sqra_low(dig_t *c, const dig_t *a, size_t size) { int i; dig_t c0, c1; @@ -105,7 +105,7 @@ dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { return c1; } -void bn_sqrn_low(dig_t *c, const dig_t *a, int size) { +void bn_sqrn_low(dig_t *c, const dig_t *a, size_t size) { int i, j; const dig_t *tmpa, *tmpb; dig_t r0, r1, r2; diff --git a/src/low/easy/relic_fb_add_low.c b/src/low/easy/relic_fb_add_low.c index 8ceb166d0..314dc8ccc 100644 --- a/src/low/easy/relic_fb_add_low.c +++ b/src/low/easy/relic_fb_add_low.c @@ -56,7 +56,7 @@ void fb_addn_low(dig_t *c, const dig_t *a, const dig_t *b) { } } -void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { int i; for (i = 0; i < size; i++, a++, b++, c++) { diff --git a/src/low/easy/relic_fb_mul_low.c b/src/low/easy/relic_fb_mul_low.c index 8f131700d..f48ca1398 100644 --- a/src/low/easy/relic_fb_mul_low.c +++ b/src/low/easy/relic_fb_mul_low.c @@ -149,7 +149,7 @@ void fb_muln_low(dig_t *c, const dig_t *a, const dig_t *b) { } } -void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { dig_t *tt = RLC_ALLOCA(dig_t, 16 * (size + 1)); dig_t *t[16]; dig_t u, r0, r1, r2, r4, r8, *tmpc; diff --git a/src/low/easy/relic_fb_shift_low.c b/src/low/easy/relic_fb_shift_low.c index ca721bd3a..11c497adc 100644 --- a/src/low/easy/relic_fb_shift_low.c +++ b/src/low/easy/relic_fb_shift_low.c @@ -54,7 +54,7 @@ dig_t fb_lsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -92,7 +92,7 @@ dig_t fb_rsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -113,7 +113,7 @@ dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { return carry; } -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size) { int i, j; dig_t b1, b2; diff --git a/src/low/easy/relic_fp_shift_low.c b/src/low/easy/relic_fp_shift_low.c index d62e56d7f..eb6bd7b76 100644 --- a/src/low/easy/relic_fp_shift_low.c +++ b/src/low/easy/relic_fp_shift_low.c @@ -53,7 +53,7 @@ dig_t fp_lsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -91,7 +91,7 @@ dig_t fp_rsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; diff --git a/src/low/gmp-sec/relic_bn_add_low.c b/src/low/gmp-sec/relic_bn_add_low.c index d80a52a91..2558f04e6 100644 --- a/src/low/gmp-sec/relic_bn_add_low.c +++ b/src/low/gmp-sec/relic_bn_add_low.c @@ -40,24 +40,35 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t *t = RLC_ALLOCA(dig_t, mpn_sec_add_1_itch(size)); dig_t r = mpn_sec_add_1(c, a, size, digit, t); RLC_FREE(t); return r; } -dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { return mpn_add_n(c, a, b, size); } -dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t *t = RLC_ALLOCA(dig_t, mpn_sec_sub_1_itch(size)); dig_t r = mpn_sec_sub_1(c, a, size, digit, t); RLC_FREE(t); return r; } -dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { return mpn_sub_n(c, a, b, size); } + +void bn_negs_low(dig_t *c, const dig_t *a, dig_t sa, size_t size) { + dig_t carry = sa & 1; + + sa = -sa; + for (int i = 0; i < size; i++) { + c[i] = (a[i] ^ sa) + carry; + carry = (c[i] < carry); + } + bn_add1_low(a, ) +} \ No newline at end of file diff --git a/src/low/gmp-sec/relic_bn_div_low.c b/src/low/gmp-sec/relic_bn_div_low.c index 4b83667b1..31f8dc411 100644 --- a/src/low/gmp-sec/relic_bn_div_low.c +++ b/src/low/gmp-sec/relic_bn_div_low.c @@ -39,7 +39,7 @@ /* Public definitions */ /*============================================================================*/ -void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) { +void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, size_t sa, dig_t *b, size_t sb) { dig_t u[sa], *t = RLC_ALLOCA(dig_t, mpn_sec_div_qr_itch(sa, sb)); mpn_copyd(u, a, sa); @@ -48,7 +48,7 @@ void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) { RLC_FREE(t); } -void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, int size, dig_t b) { +void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, dig_t b, size_t size) { dig_t u[size], *t = RLC_ALLOCA(dig_t, mpn_sec_div_qr_itch(size, 1)); mpn_copyd(u, a, size); diff --git a/src/low/gmp-sec/relic_bn_mod_low.c b/src/low/gmp-sec/relic_bn_mod_low.c index 0bb394c67..aedbb72ad 100644 --- a/src/low/gmp-sec/relic_bn_mod_low.c +++ b/src/low/gmp-sec/relic_bn_mod_low.c @@ -42,7 +42,7 @@ /* Public definitions */ /*============================================================================*/ -void bn_modn_low(dig_t *c, const dig_t *a, int sa, const dig_t *m, int sm, +void bn_modn_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *m, size_t sm, dig_t u) { dig_t *s = RLC_ALLOCA(dig_t, mpn_sec_mul_itch(sm, 1)); dig_t r, *tc = c, t[sm + 1]; diff --git a/src/low/gmp-sec/relic_bn_mul_low.c b/src/low/gmp-sec/relic_bn_mul_low.c index 082815ede..8b1ddeb29 100644 --- a/src/low/gmp-sec/relic_bn_mul_low.c +++ b/src/low/gmp-sec/relic_bn_mul_low.c @@ -41,20 +41,21 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t u[size + 1], *t = RLC_ALLOCA(dig_t, mpn_sec_mul_itch(size, 1)); mpn_sec_mul(u, a, size, &digit, 1, t); return u[size] + mpn_add_n(c, c, u, size); } -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { dig_t u[size + 1], *t = RLC_ALLOCA(dig_t, mpn_sec_mul_itch(size, 1)); mpn_sec_mul(u, a, size, &digit, 1, t); mpn_copyd(c, u, size); return u[size]; } -dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size) { +dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, + size_t size) { dig_t _a[size], carry, sign, sd = digit >> (RLC_DIG - 1); sa = -sa; @@ -68,14 +69,14 @@ dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size) { return (carry ^ sign) + bn_add1_low(c, c, -sign, size); } -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { dig_t *t = RLC_ALLOCA(dig_t, mpn_sec_mul_itch(size, size)); mpn_sec_mul(c, a, size, b, size, t); RLC_FREE(t); } -void bn_muld_low(dig_t *c, const dig_t *a, int sa, const dig_t *b, int sb, - int low, int high) { +void bn_muld_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *b, size_t sb, + uint_t low, uint_t high) { (void)low; (void)high; dig_t *t = RLC_ALLOCA(dig_t, mpn_sec_mul_itch(sa, sb)); diff --git a/src/low/gmp-sec/relic_bn_shift_low.c b/src/low/gmp-sec/relic_bn_shift_low.c index 5127b3e4a..57ceff773 100644 --- a/src/low/gmp-sec/relic_bn_shift_low.c +++ b/src/low/gmp-sec/relic_bn_shift_low.c @@ -41,23 +41,23 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_lshift(c, a, size, 1); } -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_lshift(c, a, size, bits); } -dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_rshift(c, a, size, 1); } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_rshift(c, a, size, bits); } -dig_t bn_rshs_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { dig_t r, carry, shift, mask; /* Prepare the bit mask. */ diff --git a/src/low/gmp-sec/relic_bn_sqr_low.c b/src/low/gmp-sec/relic_bn_sqr_low.c index 8e960c558..78a02df93 100644 --- a/src/low/gmp-sec/relic_bn_sqr_low.c +++ b/src/low/gmp-sec/relic_bn_sqr_low.c @@ -43,7 +43,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_sqra_low(dig_t *c, const dig_t *a, size_t size) { dig_t c0, c1, digit = a[0]; c0 = mpn_addmul_1(c, a, size, a[0]); @@ -55,7 +55,7 @@ dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { return c1; } -void bn_sqrn_low(dig_t *c, const dig_t *a, int size) { +void bn_sqrn_low(dig_t *c, const dig_t *a, size_t size) { dig_t *t = RLC_ALLOCA(dig_t, mpn_sec_sqr_itch(size)); mpn_sec_sqr(c, a, size, t); RLC_FREE(t); diff --git a/src/low/gmp-sec/relic_fp_shift_low.c b/src/low/gmp-sec/relic_fp_shift_low.c index ff52aaa70..d0aba8901 100644 --- a/src/low/gmp-sec/relic_fp_shift_low.c +++ b/src/low/gmp-sec/relic_fp_shift_low.c @@ -42,7 +42,7 @@ dig_t fp_lsh1_low(dig_t *c, const dig_t *a) { return mpn_lshift(c, a, RLC_FP_DIGS, 1); } -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } @@ -50,6 +50,6 @@ dig_t fp_rsh1_low(dig_t *c, const dig_t *a) { return mpn_rshift(c, a, RLC_FP_DIGS, 1); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/gmp/relic_bn_add_low.c b/src/low/gmp/relic_bn_add_low.c index 1c0cdfe9d..3827914b9 100644 --- a/src/low/gmp/relic_bn_add_low.c +++ b/src/low/gmp/relic_bn_add_low.c @@ -39,18 +39,29 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_add_1(c, a, size, digit); } -dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_addn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { return mpn_add_n(c, a, b, size); } -dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_sub_1(c, a, size, digit); } -dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +dig_t bn_subn_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { return mpn_sub_n(c, a, b, size); } + +void bn_negs_low(dig_t *c, const dig_t *a, dig_t sa, size_t size) { + dig_t carry = sa & 1; + + sa = -sa; + for (int i = 0; i < size; i++) { + c[i] = (a[i] ^ sa) + carry; + carry = (c[i] < carry); + } + bn_add1_low(a, ) +} diff --git a/src/low/gmp/relic_bn_div_low.c b/src/low/gmp/relic_bn_div_low.c index 96cc11eb9..6727c0791 100644 --- a/src/low/gmp/relic_bn_div_low.c +++ b/src/low/gmp/relic_bn_div_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) { +void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, size_t sa, dig_t *b, size_t sb) { mpn_tdiv_qr(c, d, 0, a, sa, b, sb); } -void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, int size, dig_t b) { +void bn_div1_low(dig_t *c, dig_t *d, const dig_t *a, dig_t b, size_t size) { *d = mpn_divrem_1(c, 0, a, size, b); } diff --git a/src/low/gmp/relic_bn_mod_low.c b/src/low/gmp/relic_bn_mod_low.c index f219a8a5b..45a3985ed 100644 --- a/src/low/gmp/relic_bn_mod_low.c +++ b/src/low/gmp/relic_bn_mod_low.c @@ -41,7 +41,7 @@ /* Public definitions */ /*============================================================================*/ -void bn_modn_low(dig_t *c, const dig_t *a, int sa, const dig_t *m, int sm, +void bn_modn_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *m, size_t sm, dig_t u) { int i; dig_t r, *tmpc; diff --git a/src/low/gmp/relic_bn_mul_low.c b/src/low/gmp/relic_bn_mul_low.c index 6b88f9c12..321343e9c 100644 --- a/src/low/gmp/relic_bn_mul_low.c +++ b/src/low/gmp/relic_bn_mul_low.c @@ -40,15 +40,16 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_addmul_1(c, a, size, digit); } -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_mul_1(c, a, size, digit); } -dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size) { +dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, + size_t size) { dig_t carry, sign, sd = digit >> (RLC_DIG - 1); sa = -sa; @@ -62,13 +63,13 @@ dig_t bn_muls_low(dig_t *c, const dig_t *a, dig_t sa, dis_t digit, int size) { return (carry ^ sign) + mpn_add_1(c, c, size, -sign); } -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { mpn_mul_n(c, a, b, size); } -void bn_muld_low(dig_t *c, const dig_t *a, int sizea, const dig_t *b, int sizeb, - int low, int high) { +void bn_muld_low(dig_t *c, const dig_t *a, size_t sa, const dig_t *b, size_t sb, + uint_t low, uint_t high) { (void)low; (void)high; - mpn_mul(c, a, sizea, b, sizeb); + mpn_mul(c, a, sa, b, sb); } diff --git a/src/low/gmp/relic_bn_shift_low.c b/src/low/gmp/relic_bn_shift_low.c index eb8ed87b1..b65acc1dd 100644 --- a/src/low/gmp/relic_bn_shift_low.c +++ b/src/low/gmp/relic_bn_shift_low.c @@ -41,23 +41,23 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_lshift(c, a, size, 1); } -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_lshift(c, a, size, bits); } -dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_rshift(c, a, size, 1); } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_rshift(c, a, size, bits); } -dig_t bn_rshs_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { dig_t r, carry, shift, mask; /* Prepare the bit mask. */ diff --git a/src/low/gmp/relic_bn_sqr_low.c b/src/low/gmp/relic_bn_sqr_low.c index 8ca81d334..fdbdb0d43 100644 --- a/src/low/gmp/relic_bn_sqr_low.c +++ b/src/low/gmp/relic_bn_sqr_low.c @@ -40,7 +40,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_sqra_low(dig_t *c, const dig_t *a, size_t size) { dig_t c0, c1, digit = a[0]; c0 = mpn_addmul_1(c, a, size, a[0]); @@ -52,6 +52,6 @@ dig_t bn_sqra_low(dig_t *c, const dig_t *a, int size) { return c1; } -void bn_sqrn_low(dig_t *c, const dig_t *a, int size) { +void bn_sqrn_low(dig_t *c, const dig_t *a, size_t size) { mpn_mul_n(c, a, a, size); } diff --git a/src/low/gmp/relic_fb_add_low.c b/src/low/gmp/relic_fb_add_low.c index ef4a876c4..e4e646329 100644 --- a/src/low/gmp/relic_fb_add_low.c +++ b/src/low/gmp/relic_fb_add_low.c @@ -53,6 +53,6 @@ void fb_addn_low(dig_t *c, const dig_t *a, const dig_t *b) { mpn_xor_n(c, a, b, RLC_FB_DIGS); } -void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { mpn_xor_n(c, a, b, size); } diff --git a/src/low/gmp/relic_fb_shift_low.c b/src/low/gmp/relic_fb_shift_low.c index 3ba5cf16c..b28e04051 100644 --- a/src/low/gmp/relic_fb_shift_low.c +++ b/src/low/gmp/relic_fb_shift_low.c @@ -43,7 +43,7 @@ dig_t fb_lsh1_low(dig_t *c, const dig_t *a) { return mpn_lshift(c, a, RLC_FB_DIGS, 1); } -dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FB_DIGS, bits); } @@ -51,11 +51,11 @@ dig_t fb_rsh1_low(dig_t *c, const dig_t *a) { return mpn_rshift(c, a, RLC_FB_DIGS, 1); } -dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FB_DIGS, bits); } -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size) { int i, j; dig_t b1, b2; diff --git a/src/low/gmp/relic_fp_shift_low.c b/src/low/gmp/relic_fp_shift_low.c index fd176b9f4..954c75265 100644 --- a/src/low/gmp/relic_fp_shift_low.c +++ b/src/low/gmp/relic_fp_shift_low.c @@ -42,7 +42,7 @@ dig_t fp_lsh1_low(dig_t *c, const dig_t *a) { return mpn_lshift(c, a, RLC_FP_DIGS, 1); } -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } @@ -50,6 +50,6 @@ dig_t fp_rsh1_low(dig_t *c, const dig_t *a) { return mpn_rshift(c, a, RLC_FP_DIGS, 1); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/msp-asm/relic_bn_add_low.c b/src/low/msp-asm/relic_bn_add_low.c index 9c8ec9679..0f649d23f 100644 --- a/src/low/msp-asm/relic_bn_add_low.c +++ b/src/low/msp-asm/relic_bn_add_low.c @@ -36,7 +36,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { int i; register dig_t carry, r0; @@ -52,7 +52,7 @@ dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { return carry; } -//dig_t bn_addn_low(dig_t *c, dig_t *a, dig_t *b, int size) { +//dig_t bn_addn_low(dig_t *c, dig_t *a, dig_t *b, size_t size) { // int i; // register dig_t carry, c0, c1, r0, r1; // @@ -68,7 +68,7 @@ dig_t bn_add1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { // return carry; //} -dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { int i; dig_t carry, r0; @@ -84,7 +84,7 @@ dig_t bn_sub1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { return carry; } -//dig_t bn_subn_low(dig_t *c, dig_t *a, dig_t *b, int size) { +//dig_t bn_subn_low(dig_t *c, dig_t *a, dig_t *b, size_t size) { // int i; // dig_t carry, r0, diff; // diff --git a/src/low/msp-asm/relic_bn_shift_low.c b/src/low/msp-asm/relic_bn_shift_low.c index 3f784c556..421185378 100644 --- a/src/low/msp-asm/relic_bn_shift_low.c +++ b/src/low/msp-asm/relic_bn_shift_low.c @@ -36,7 +36,7 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { int i; dig_t r, carry; @@ -52,7 +52,7 @@ dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { return carry; } -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; @@ -70,7 +70,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { return carry; } -//dig_t bn_rsh1_low(dig_t *c, dig_t *a, int size) { +//dig_t bn_rsh1_low(dig_t *c, dig_t *a, size_t size) { // int i; // dig_t r, carry; // @@ -89,7 +89,7 @@ dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { // return carry; //} -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { int i; dig_t r, carry, shift, mask; diff --git a/src/low/msp-asm/relic_fb_add_low.c b/src/low/msp-asm/relic_fb_add_low.c index 07a50dc9f..13b3440ca 100644 --- a/src/low/msp-asm/relic_fb_add_low.c +++ b/src/low/msp-asm/relic_fb_add_low.c @@ -46,7 +46,7 @@ void fb_add1_low(dig_t *c, const dig_t *a, dig_t digit) { (*c) = (*a); } -void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_addd_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { int i; for (i = 0; i < size; i++, a++, b++, c++) diff --git a/src/low/msp-asm/relic_fb_mul_low.c b/src/low/msp-asm/relic_fb_mul_low.c index b79bdda9d..f0d01dcb0 100644 --- a/src/low/msp-asm/relic_fb_mul_low.c +++ b/src/low/msp-asm/relic_fb_mul_low.c @@ -150,7 +150,7 @@ void fb_muln_low(dig_t *c, dig_t *a, dig_t *b) { } } */ -void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void fb_muld_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { dv_t table[16]; dig_t u, *tmpc, r0, r1, r2, r4, r8; const dig_t *tmpa; diff --git a/src/low/msp-asm/relic_fb_shift_low.c b/src/low/msp-asm/relic_fb_shift_low.c index 593445682..5c15b58c8 100644 --- a/src/low/msp-asm/relic_fb_shift_low.c +++ b/src/low/msp-asm/relic_fb_shift_low.c @@ -36,14 +36,14 @@ /* Public definitions */ /*============================================================================*/ -dig_t fb_lshadd1_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd2_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd3_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd4_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd5_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd6_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd7_low(dig_t *c, const dig_t *a, int size); -dig_t fb_lshadd8_low(dig_t *c, const dig_t *a, int size); +dig_t fb_lshadd1_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd2_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd3_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd4_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd5_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd6_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd7_low(dig_t *c, const dig_t *a, size_t size); +dig_t fb_lshadd8_low(dig_t *c, const dig_t *a, size_t size); dig_t fb_lsh1_low(dig_t *c, const dig_t *a) { int i; @@ -62,7 +62,7 @@ dig_t fb_lsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fb_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -100,7 +100,7 @@ dig_t fb_rsh1_low(dig_t *c, const dig_t *a) { return carry; } -dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fb_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { int i; dig_t r, carry, mask, shift; @@ -121,7 +121,7 @@ dig_t fb_rshb_low(dig_t *c, const dig_t *a, int bits) { return carry; } -dig_t fb_lsha_low(dig_t *c, const dig_t *a, int bits, int size) { +dig_t fb_lsha_low(dig_t *c, const dig_t *a, uint_t bits, size_t size) { int i, j; dig_t b1, b2; diff --git a/src/low/x64-asm-4l/relic_fp_shift_low.c b/src/low/x64-asm-4l/relic_fp_shift_low.c index 19f67ecaf..9d12a75ce 100755 --- a/src/low/x64-asm-4l/relic_fp_shift_low.c +++ b/src/low/x64-asm-4l/relic_fp_shift_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/x64-asm-5l/relic_fp_shift_low.c b/src/low/x64-asm-5l/relic_fp_shift_low.c index f240db5f4..132efbb3b 100644 --- a/src/low/x64-asm-5l/relic_fp_shift_low.c +++ b/src/low/x64-asm-5l/relic_fp_shift_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/x64-asm-6l/relic_bn_mul_low.c b/src/low/x64-asm-6l/relic_bn_mul_low.c index 2229b6d2e..75acca77d 100644 --- a/src/low/x64-asm-6l/relic_bn_mul_low.c +++ b/src/low/x64-asm-6l/relic_bn_mul_low.c @@ -40,19 +40,19 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_addmul_1(c, a, size, digit); } -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_mul_1(c, a, size, digit); } -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { mpn_mul_n(c, a, b, size); } -void bn_muld_low(dig_t *c, const dig_t *a, int sizea, const dig_t *b, int sizeb, +void bn_muld_low(dig_t *c, const dig_t *a, size_t sizea, const dig_t *b, size_t sizeb, int low, int high) { (void)low; (void)high; diff --git a/src/low/x64-asm-6l/relic_bn_shift_low.c b/src/low/x64-asm-6l/relic_bn_shift_low.c index 0445b5ab8..99bc7e57f 100644 --- a/src/low/x64-asm-6l/relic_bn_shift_low.c +++ b/src/low/x64-asm-6l/relic_bn_shift_low.c @@ -41,18 +41,18 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_lsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_lshift(c, a, size, 1); } -dig_t bn_lshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_lshift(c, a, size, bits); } -dig_t bn_rsh1_low(dig_t *c, const dig_t *a, int size) { +dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { return mpn_rshift(c, a, size, 1); } -dig_t bn_rshb_low(dig_t *c, const dig_t *a, int size, int bits) { +dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { return mpn_rshift(c, a, size, bits); } diff --git a/src/low/x64-asm-6l/relic_fp_shift_low.c b/src/low/x64-asm-6l/relic_fp_shift_low.c index f240db5f4..132efbb3b 100644 --- a/src/low/x64-asm-6l/relic_fp_shift_low.c +++ b/src/low/x64-asm-6l/relic_fp_shift_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/x64-asm-7l/relic_fp_shift_low.c b/src/low/x64-asm-7l/relic_fp_shift_low.c index 483de745b..004a75c92 100644 --- a/src/low/x64-asm-7l/relic_fp_shift_low.c +++ b/src/low/x64-asm-7l/relic_fp_shift_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/src/low/x64-asm-8l/relic_bn_mul_low.c b/src/low/x64-asm-8l/relic_bn_mul_low.c index 2229b6d2e..75acca77d 100644 --- a/src/low/x64-asm-8l/relic_bn_mul_low.c +++ b/src/low/x64-asm-8l/relic_bn_mul_low.c @@ -40,19 +40,19 @@ /* Public definitions */ /*============================================================================*/ -dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mula_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_addmul_1(c, a, size, digit); } -dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, int size) { +dig_t bn_mul1_low(dig_t *c, const dig_t *a, dig_t digit, size_t size) { return mpn_mul_1(c, a, size, digit); } -void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, int size) { +void bn_muln_low(dig_t *c, const dig_t *a, const dig_t *b, size_t size) { mpn_mul_n(c, a, b, size); } -void bn_muld_low(dig_t *c, const dig_t *a, int sizea, const dig_t *b, int sizeb, +void bn_muld_low(dig_t *c, const dig_t *a, size_t sizea, const dig_t *b, size_t sizeb, int low, int high) { (void)low; (void)high; diff --git a/src/low/x64-asm-8l/relic_fp_shift_low.c b/src/low/x64-asm-8l/relic_fp_shift_low.c index 483de745b..004a75c92 100644 --- a/src/low/x64-asm-8l/relic_fp_shift_low.c +++ b/src/low/x64-asm-8l/relic_fp_shift_low.c @@ -38,10 +38,10 @@ /* Public definitions */ /*============================================================================*/ -dig_t fp_lshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_lshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_lshift(c, a, RLC_FP_DIGS, bits); } -dig_t fp_rshb_low(dig_t *c, const dig_t *a, int bits) { +dig_t fp_rshb_low(dig_t *c, const dig_t *a, uint_t bits) { return mpn_rshift(c, a, RLC_FP_DIGS, bits); } diff --git a/test/test_bn.c b/test/test_bn.c index c10f08497..cc04b56f6 100644 --- a/test/test_bn.c +++ b/test/test_bn.c @@ -63,7 +63,7 @@ static int memory(void) { } static int util(void) { - int bits, code = RLC_ERR; + uint_t bits, code = RLC_ERR; char str[RLC_BN_BITS + 2]; dig_t digit, raw[RLC_BN_DIGS]; uint8_t bin[RLC_CEIL(RLC_BN_BITS, 8)]; diff --git a/test/test_fb.c b/test/test_fb.c index fc979a171..58dafaf72 100644 --- a/test/test_fb.c +++ b/test/test_fb.c @@ -62,7 +62,8 @@ static int memory(void) { } static int util(void) { - int bits, code = RLC_ERR; + int code = RLC_ERR; + uint_t bits; fb_t a, b; char str[RLC_FB_BITS + 1]; uint8_t bin[RLC_FB_BYTES]; diff --git a/test/test_fp.c b/test/test_fp.c index 49fa961a3..edcec0300 100644 --- a/test/test_fp.c +++ b/test/test_fp.c @@ -62,7 +62,8 @@ static int memory(void) { } static int util(void) { - int bits, code = RLC_ERR; + int code = RLC_ERR; + uint_t bits; /* Allocate two extra for sign and null terminator. */ char str[RLC_FP_BITS + 2]; uint8_t bin[RLC_FP_BYTES]; @@ -947,6 +948,13 @@ static int symbol(void) { } TEST_END; #endif +#if FP_SMB == BINAR || !defined(STRIP) + TEST_CASE("binary symbol computation is correct") { + fp_rand(a); + TEST_ASSERT(fp_smb(a) == fp_smb_binar(a), end); + } TEST_END; +#endif + #if FP_SMB == DIVST || !defined(STRIP) TEST_CASE("division step symbol computation is correct") { fp_rand(a); diff --git a/test/test_rand.c b/test/test_rand.c index 7f90ab8f8..d6b76b23a 100644 --- a/test/test_rand.c +++ b/test/test_rand.c @@ -385,7 +385,7 @@ static int test(void) { #include #include -static void test_bytes(uint8_t *buf, int size, void *args) { +static void test_bytes(uint8_t *buf, size_t size, void *args) { int c, l, fd = *(int *)args; if (fd == -1) {