Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first batch of simplifications #435

Merged
merged 2 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions demo/mtest_opponent.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ static int mtest_opponent(void)
/* test the sign/unsigned storage functions */

rr = (unsigned)mp_sbin_size(&c);
DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
DO(mp_to_sbin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
DO(mp_from_sbin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mp_signed_bin failure!\n");
draw(&c);
Expand All @@ -150,9 +150,9 @@ static int mtest_opponent(void)
}

rr = (unsigned)mp_ubin_size(&c);
DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
DO(mp_to_ubin(&c, (uint8_t *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
DO(mp_from_ubin(&d, (uint8_t *) cmd, (size_t)rr));
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("mp_unsigned_bin failure!\n");
draw(&c);
Expand Down
6 changes: 3 additions & 3 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ static int test_mp_read_write_ubin(void)
{
mp_int a, b, c;
size_t size, len;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

DOR(mp_init_multi(&a, &b, &c, NULL));

Expand Down Expand Up @@ -2207,7 +2207,7 @@ static int test_mp_read_write_sbin(void)
{
mp_int a, b, c;
size_t size, len;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

DOR(mp_init_multi(&a, &b, &c, NULL));

Expand Down Expand Up @@ -2246,7 +2246,7 @@ static int test_mp_pack_unpack(void)
{
mp_int a, b;
size_t written, count;
unsigned char *buf = NULL;
uint8_t *buf = NULL;

mp_order order = MP_LSB_FIRST;
mp_endian endianess = MP_NATIVE_ENDIAN;
Expand Down
8 changes: 4 additions & 4 deletions doc/bn.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2444,15 +2444,15 @@ \section{Binary Conversions}

\index{mp\_to\_ubin}
\begin{alltt}
mp_err mp_to_ubin(const mp_int *a, unsigned char *buf, size_t maxlen, size_t *written)
mp_err mp_to_ubin(const mp_int *a, uint8_t *buf, size_t maxlen, size_t *written)
\end{alltt}
This will store $a$ into the buffer \texttt{buf} of size \texttt{maxlen} in big--endian format
storing the number of bytes written in \texttt{len}. Fortunately this is exactly what DER (or is
it ASN?) requires. It does not store the sign of the integer.

\index{mp\_from\_ubin}
\begin{alltt}
mp_err mp_from_ubin(mp_int *a, unsigned char *b, size_t size);
mp_err mp_from_ubin(mp_int *a, uint8_t *b, size_t size);
\end{alltt}
This will read in an unsigned big--endian array of bytes (octets) from \texttt{b} of length
\texttt{size} into $a$. The resulting big--integer $a$ will always be positive.
Expand All @@ -2462,8 +2462,8 @@ \section{Binary Conversions}
\index{mp\_sbin\_size} \index{mp\_from\_sbin} \index{mp\_to\_sbin}
\begin{alltt}
size_t mp_sbin_size(const mp_int *a);
mp_err mp_from_sbin(mp_int *a, const unsigned char *b, size_t size);
mp_err mp_to_sbin(const mp_int *a, unsigned char *b, size_t maxsize, size_t *len);
mp_err mp_from_sbin(mp_int *a, const uint8_t *b, size_t size);
mp_err mp_to_sbin(const mp_int *a, uint8_t *b, size_t maxsize, size_t *len);
\end{alltt}
They operate essentially the same as the unsigned copies except they prefix the data with zero or
non--zero byte depending on the sign. If the sign is \texttt{MP\_ZPOS} (e.g. not negative) the
Expand Down
3 changes: 1 addition & 2 deletions mp_abs.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
*/
mp_err mp_abs(const mp_int *a, mp_int *b)
{
mp_err err;

/* copy a to b */
if (a != b) {
mp_err err;
if ((err = mp_copy(a, b)) != MP_OKAY) {
return err;
}
Expand Down
37 changes: 14 additions & 23 deletions mp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,24 @@
/* high level addition (handles signs) */
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
{
mp_sign sa, sb;
mp_err err;

/* get sign of both inputs */
sa = a->sign;
sb = b->sign;

/* handle two cases, not four */
if (sa == sb) {
if (a->sign == b->sign) {
/* both positive or both negative */
/* add their magnitudes, copy the sign */
c->sign = sa;
err = s_mp_add(a, b, c);
} else {
/* one positive, the other negative */
/* subtract the one with the greater magnitude from */
/* the one of the lesser magnitude. The result gets */
/* the sign of the one with the greater magnitude. */
if (mp_cmp_mag(a, b) == MP_LT) {
c->sign = sb;
err = s_mp_sub(b, a, c);
} else {
c->sign = sa;
err = s_mp_sub(a, b, c);
}
c->sign = a->sign;
return s_mp_add(a, b, c);
}
return err;

/* one positive, the other negative */
/* subtract the one with the greater magnitude from */
/* the one of the lesser magnitude. The result gets */
/* the sign of the one with the greater magnitude. */
if (mp_cmp_mag(a, b) == MP_LT) {
MP_EXCH(const mp_int *, a, b);
}

c->sign = a->sign;
return s_mp_sub(a, b, c);
}

#endif
7 changes: 3 additions & 4 deletions mp_clear_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

void mp_clear_multi(mp_int *mp, ...)
{
mp_int *next_mp = mp;
va_list args;
va_start(args, mp);
while (next_mp != NULL) {
mp_clear(next_mp);
next_mp = va_arg(args, mp_int *);
while (mp != NULL) {
mp_clear(mp);
mp = va_arg(args, mp_int *);
}
va_end(args);
}
Expand Down
15 changes: 5 additions & 10 deletions mp_cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ mp_ord mp_cmp(const mp_int *a, const mp_int *b)
{
/* compare based on sign */
if (a->sign != b->sign) {
if (a->sign == MP_NEG) {
return MP_LT;
} else {
return MP_GT;
}
return a->sign == MP_NEG ? MP_LT : MP_GT;
}

/* compare digits */
/* if negative compare opposite direction */
if (a->sign == MP_NEG) {
/* if negative compare opposite direction */
return mp_cmp_mag(b, a);
} else {
return mp_cmp_mag(a, b);
MP_EXCH(const mp_int *, a, b);
}

return mp_cmp_mag(a, b);
}
#endif
10 changes: 4 additions & 6 deletions mp_cmp_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
}

/* compare the only digit of a to b */
if (a->dp[0] > b) {
return MP_GT;
} else if (a->dp[0] < b) {
return MP_LT;
} else {
return MP_EQ;
if (a->dp[0] != b) {
return a->dp[0] > b ? MP_GT : MP_LT;
}

return MP_EQ;
}
#endif
28 changes: 7 additions & 21 deletions mp_cmp_mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,20 @@
/* compare maginitude of two ints (unsigned) */
mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
{
int n;
const mp_digit *tmpa, *tmpb;
int n;

/* compare based on # of non-zero digits */
if (a->used > b->used) {
return MP_GT;
if (a->used != b->used) {
return a->used > b->used ? MP_GT : MP_LT;
}

if (a->used < b->used) {
return MP_LT;
}

/* alias for a */
tmpa = a->dp + (a->used - 1);

/* alias for b */
tmpb = b->dp + (a->used - 1);

/* compare based on digits */
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
if (*tmpa > *tmpb) {
return MP_GT;
}

if (*tmpa < *tmpb) {
return MP_LT;
for (n = a->used; n --> 0;) {
if (a->dp[n] != b->dp[n]) {
return a->dp[n] > b->dp[n] ? MP_GT : MP_LT;
}
}

return MP_EQ;
}
#endif
11 changes: 6 additions & 5 deletions mp_cnt_lsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

static const int lnz[16] = {
static const char lnz[16] = {
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
};

/* Counts the number of lsbs which are zero before the first zero bit */
int mp_cnt_lsb(const mp_int *a)
{
int x;
mp_digit q, qq;
mp_digit q;

/* easy out */
if (mp_iszero(a)) {
Expand All @@ -25,11 +25,12 @@ int mp_cnt_lsb(const mp_int *a)

/* now scan this digit until a 1 is found */
if ((q & 1u) == 0u) {
mp_digit p;
do {
qq = q & 15u;
x += lnz[qq];
p = q & 15u;
x += lnz[p];
q >>= 4;
} while (qq == 0u);
} while (p == 0u);
}
return x;
}
Expand Down
14 changes: 3 additions & 11 deletions mp_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
mp_err mp_copy(const mp_int *a, mp_int *b)
{
int n;
mp_digit *tmpa, *tmpb;
mp_err err;

/* if dst == src do nothing */
if (a == b) {
Expand All @@ -17,27 +15,21 @@ mp_err mp_copy(const mp_int *a, mp_int *b)

/* grow dest */
if (b->alloc < a->used) {
mp_err err;
if ((err = mp_grow(b, a->used)) != MP_OKAY) {
return err;
}
}

/* zero b and copy the parameters over */
/* pointer aliases */

/* source */
tmpa = a->dp;

/* destination */
tmpb = b->dp;

/* copy all the digits */
for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++;
b->dp[n] = a->dp[n];
}

/* clear high digits */
MP_ZERO_DIGITS(tmpb, b->used - n);
MP_ZERO_DIGITS(b->dp + a->used, b->used - a->used);

/* copy used count and sign */
b->used = a->used;
Expand Down
13 changes: 7 additions & 6 deletions mp_div.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
/* if a < b then q = 0, r = a */
if (mp_cmp_mag(a, b) == MP_LT) {
if (d != NULL) {
err = mp_copy(a, d);
} else {
err = MP_OKAY;
if ((err = mp_copy(a, d)) != MP_OKAY) {
return err;
}
}
if (c != NULL) {
mp_zero(c);
}
return err;
return MP_OKAY;
}

if (MP_HAS(S_MP_DIV_RECURSIVE)
Expand All @@ -31,11 +31,12 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
err = s_mp_div_recursive(a, b, c, d);
} else if (MP_HAS(S_MP_DIV_SCHOOL)) {
err = s_mp_div_school(a, b, c, d);
} else {
} else if (MP_HAS(S_MP_DIV_SMALL)) {
err = s_mp_div_small(a, b, c, d);
} else {
err = MP_VAL;
}

return err;
}
#endif

7 changes: 4 additions & 3 deletions mp_div_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
{
mp_int q;
mp_word w, t;
mp_word w;
mp_digit b;
mp_err err;
int ix;
Expand All @@ -22,7 +22,8 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
q.used = a->used;
q.sign = a->sign;
w = 0;
for (ix = a->used - 1; ix >= 0; ix--) {
for (ix = a->used; ix --> 0;) {
mp_word t;
w = (w << (mp_word)MP_DIGIT_BIT) | (mp_word)a->dp[ix];

if (w >= 3u) {
Expand Down Expand Up @@ -57,7 +58,7 @@ mp_err mp_div_3(const mp_int *a, mp_int *c, mp_digit *d)
}
mp_clear(&q);

return err;
return MP_OKAY;
}

#endif
6 changes: 1 addition & 5 deletions mp_exch.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
*/
void mp_exch(mp_int *a, mp_int *b)
{
mp_int t;

t = *a;
*a = *b;
*b = t;
MP_EXCH(mp_int, *a, *b);
}
#endif
Loading