Skip to content

Commit

Permalink
Refactoring of types.
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaranha committed Apr 19, 2024
1 parent 7bcf101 commit 9a42dea
Show file tree
Hide file tree
Showing 82 changed files with 546 additions and 257 deletions.
2 changes: 1 addition & 1 deletion bench/bench_fb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions bench/bench_fp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <sys/stat.h>
#include <fcntl.h>

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) {
Expand Down
52 changes: 31 additions & 21 deletions include/low/relic_bn_low.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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 */
Expand Down
10 changes: 5 additions & 5 deletions include/low/relic_fb_low.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions include/low/relic_fp_low.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions include/relic_conf.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion include/relic_eb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/relic_ed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion include/relic_ep.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions include/relic_epx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions include/relic_fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
Loading

0 comments on commit 9a42dea

Please sign in to comment.