Skip to content
Closed
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
116 changes: 116 additions & 0 deletions demo/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,121 @@ static int test_trivial_stuff(void)
return EXIT_FAILURE;
}

static int test_null_guards(void)
{
mp_int a, b, c;
char str[64];
uint8_t ubuf[64];
size_t sz;
int val;
bool bval;

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

/* mp_init / mp_init_size */
EXPECT(mp_init(NULL) == MP_VAL);
EXPECT(mp_init_size(NULL, 10) == MP_VAL);
EXPECT(mp_init_copy(NULL, &a) == MP_VAL);
EXPECT(mp_init_copy(&b, NULL) == MP_VAL);

/* mp_clear / mp_zero / mp_clamp */
mp_clear(NULL);
mp_zero(NULL);
mp_clamp(NULL);
mp_set(NULL, 1);
mp_exch(NULL, &a);
mp_exch(&a, NULL);

/* mp_grow / mp_shrink / mp_copy */
EXPECT(mp_grow(NULL, 10) == MP_VAL);
EXPECT(mp_shrink(NULL) == MP_VAL);
EXPECT(mp_copy(NULL, &a) == MP_VAL);
EXPECT(mp_copy(&a, NULL) == MP_VAL);

/* comparisons */
EXPECT(mp_cmp(NULL, &a) == MP_LT);
EXPECT(mp_cmp(&a, NULL) == MP_GT);
EXPECT(mp_cmp(NULL, NULL) == MP_EQ);
EXPECT(mp_cmp_mag(NULL, &a) == MP_LT);
EXPECT(mp_cmp_mag(&a, NULL) == MP_GT);
EXPECT(mp_cmp_d(NULL, 10) == MP_LT);

/* count bits, sizes, get/set double */
EXPECT(mp_count_bits(NULL) == 0);
EXPECT(mp_ubin_size(NULL) == 0u);
EXPECT(mp_sbin_size(NULL) == 0u);
EXPECT(mp_get_double(NULL) == 0.0);
EXPECT(mp_set_double(NULL, 1.0) == MP_VAL);

/* arithmetic */
EXPECT(mp_add(NULL, &a, &b) == MP_VAL);
EXPECT(mp_add(&a, NULL, &b) == MP_VAL);
EXPECT(mp_add(&a, &a, NULL) == MP_VAL);
EXPECT(mp_sub(NULL, &a, &b) == MP_VAL);
EXPECT(mp_mul(NULL, &a, &b) == MP_VAL);
EXPECT(mp_div(NULL, &a, &b, &c) == MP_VAL);
EXPECT(mp_div(&a, NULL, &b, &c) == MP_VAL);
EXPECT(mp_mod(NULL, &a, &b) == MP_VAL);

/* radix / string */
EXPECT(mp_read_radix(NULL, "123", 10) == MP_VAL);
EXPECT(mp_read_radix(&a, NULL, 10) == MP_VAL);
EXPECT(mp_to_radix(NULL, str, sizeof(str), &sz, 10) == MP_VAL);
EXPECT(mp_to_radix(&a, NULL, sizeof(str), &sz, 10) == MP_VAL);
EXPECT(mp_radix_size(NULL, 10, &sz) == MP_VAL);
EXPECT(mp_radix_size(&a, 10, NULL) == MP_VAL);

/* bin / binary import/export */
EXPECT(mp_to_ubin(NULL, ubuf, sizeof(ubuf), &sz) == MP_VAL);
EXPECT(mp_to_ubin(&a, NULL, sizeof(ubuf), &sz) == MP_VAL);
EXPECT(mp_from_ubin(NULL, ubuf, sizeof(ubuf)) == MP_VAL);
EXPECT(mp_from_ubin(&a, NULL, sizeof(ubuf)) == MP_VAL);
EXPECT(mp_to_sbin(NULL, ubuf, sizeof(ubuf), &sz) == MP_VAL);
EXPECT(mp_to_sbin(&a, NULL, sizeof(ubuf), &sz) == MP_VAL);
EXPECT(mp_from_sbin(NULL, ubuf, sizeof(ubuf)) == MP_VAL);
EXPECT(mp_from_sbin(&a, NULL, sizeof(ubuf)) == MP_VAL);

/* bitwise */
EXPECT(mp_and(NULL, &a, &b) == MP_VAL);
EXPECT(mp_or(NULL, &a, &b) == MP_VAL);
EXPECT(mp_xor(NULL, &a, &b) == MP_VAL);
EXPECT(mp_2expt(NULL, 5) == MP_VAL);
EXPECT(mp_lshd(NULL, 1) == MP_VAL);
mp_rshd(NULL, 1);
EXPECT(mp_div_2d(NULL, 1, &b, &c) == MP_VAL);
EXPECT(mp_div_2d(&a, 1, NULL, &c) == MP_VAL);
EXPECT(mp_mod_2d(NULL, 1, &b) == MP_VAL);
EXPECT(mp_mod_2d(&a, 1, NULL) == MP_VAL);
EXPECT(mp_mul_2d(NULL, 1, &b) == MP_VAL);
EXPECT(mp_mul_2d(&a, 1, NULL) == MP_VAL);

/* math functions */
EXPECT(mp_gcd(NULL, &a, &b) == MP_VAL);
EXPECT(mp_lcm(NULL, &a, &b) == MP_VAL);
EXPECT(mp_exptmod(NULL, &a, &a, &b) == MP_VAL);
EXPECT(mp_invmod(NULL, &a, &b) == MP_VAL);
EXPECT(mp_sqrt(NULL, &b) == MP_VAL);
EXPECT(mp_sqrt(&a, NULL) == MP_VAL);
EXPECT(mp_is_square(NULL, &bval) == MP_VAL);
EXPECT(mp_is_square(&a, NULL) == MP_VAL);
EXPECT(mp_root_n(NULL, 2, &b) == MP_VAL);
EXPECT(mp_root_n(&a, 2, NULL) == MP_VAL);
EXPECT(mp_log(NULL, &a, &val) == MP_VAL);
EXPECT(mp_kronecker(NULL, &a, &val) == MP_VAL);
EXPECT(mp_prime_is_prime(NULL, 5, &bval) == MP_VAL);
EXPECT(mp_prime_is_prime(&a, 5, NULL) == MP_VAL);
EXPECT(mp_prime_next_prime(NULL, 5, false) == MP_VAL);
EXPECT(mp_rand(NULL, 5) == MP_VAL);
EXPECT(mp_signed_rsh(NULL, 1, &b) == MP_VAL);
EXPECT(mp_pack_count(NULL, 0, 1) == 0u);

mp_clear_multi(&a, &b, &c, NULL);
return EXIT_SUCCESS;
LBL_ERR:
mp_clear_multi(&a, &b, &c, NULL);
return EXIT_FAILURE;
}

static int test_mp_hash(void)
{
mp_int a;
Expand Down Expand Up @@ -2556,6 +2671,7 @@ static int unit_tests(int argc, char **argv)
#define T3(n, o1, o2, o3) { #n, (MP_HAS(o1) && MP_HAS(o2) && MP_HAS(o3)) ? test_##n : NULL }
T0(feature_detection),
T0(trivial_stuff),
T0(null_guards),
T1(mp_hash, MP_HASH),
T2(mp_get_set_i32, MP_GET_I32, MP_GET_MAG_U32),
T2(mp_get_set_i64, MP_GET_I64, MP_GET_MAG_U64),
Expand Down
2 changes: 1 addition & 1 deletion mp_2expt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mp_err mp_2expt(mp_int *a, int b)
{
mp_err err;

if (b < 0) {
if (a == NULL || b < 0) {
return MP_VAL;
}

Expand Down
4 changes: 4 additions & 0 deletions mp_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/* high level addition (handles signs) */
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
{
if (a == NULL || b == NULL || c == NULL) {
return MP_VAL;
}

/* handle two cases, not four */
if (a->sign == b->sign) {
/* both positive or both negative */
Expand Down
11 changes: 9 additions & 2 deletions mp_and.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
/* two complement and */
mp_err mp_and(const mp_int *a, const mp_int *b, mp_int *c)
{
int used = MP_MAX(a->used, b->used) + 1, i;
int used, i;
mp_err err;
mp_digit ac = 1, bc = 1, cc = 1;
bool neg = (mp_isneg(a) && mp_isneg(b));
bool neg;

if (a == NULL || b == NULL || c == NULL) {
return MP_VAL;
}

used = MP_MAX(a->used, b->used) + 1;
neg = (mp_isneg(a) && mp_isneg(b));

if ((err = mp_grow(c, used)) != MP_OKAY) {
return err;
Expand Down
3 changes: 3 additions & 0 deletions mp_clamp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
void mp_clamp(mp_int *a)
{
if (a == NULL) {
return;
}
/* decrease used while the most significant digit is
* zero.
*/
Expand Down
2 changes: 1 addition & 1 deletion mp_clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void mp_clear(mp_int *a)
{
/* only do anything if a hasn't been freed previously */
if (a->dp != NULL) {
if (a != NULL && a->dp != NULL) {
/* free ram */
MP_FREE_DIGS(a->dp, a->alloc);

Expand Down
10 changes: 10 additions & 0 deletions mp_cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
/* compare two ints (signed)*/
mp_ord mp_cmp(const mp_int *a, const mp_int *b)
{
if (a == b) {
return MP_EQ;
}
if (a == NULL) {
return MP_LT;
}
if (b == NULL) {
return MP_GT;
}

/* compare based on sign */
if (a->sign != b->sign) {
return mp_isneg(a) ? MP_LT : MP_GT;
Expand Down
4 changes: 4 additions & 0 deletions mp_cmp_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/* compare a digit */
mp_ord mp_cmp_d(const mp_int *a, mp_digit b)
{
if (a == NULL) {
return MP_LT;
}

/* compare based on sign */
if (mp_isneg(a)) {
return MP_LT;
Expand Down
10 changes: 10 additions & 0 deletions mp_cmp_mag.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ mp_ord mp_cmp_mag(const mp_int *a, const mp_int *b)
{
int n;

if (a == b) {
return MP_EQ;
}
if (a == NULL) {
return MP_LT;
}
if (b == NULL) {
return MP_GT;
}

/* compare based on # of non-zero digits */
if (a->used != b->used) {
return a->used > b->used ? MP_GT : MP_LT;
Expand Down
4 changes: 4 additions & 0 deletions mp_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mp_err mp_copy(const mp_int *a, mp_int *b)
{
mp_err err;

if (a == NULL || b == NULL) {
return MP_VAL;
}

/* if dst == src do nothing */
if (a == b) {
return MP_OKAY;
Expand Down
2 changes: 1 addition & 1 deletion mp_count_bits.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int mp_count_bits(const mp_int *a)
mp_digit q;

/* shortcut */
if (mp_iszero(a)) {
if (a == NULL || mp_iszero(a)) {
return 0;
}

Expand Down
4 changes: 4 additions & 0 deletions mp_div.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
{
mp_err err;

if (a == NULL || b == NULL) {
return MP_VAL;
}

/* is divisor zero ? */
if (mp_iszero(b)) {
return MP_VAL;
Expand Down
2 changes: 1 addition & 1 deletion mp_div_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mp_err mp_div_2d(const mp_int *a, int b, mp_int *c, mp_int *d)
{
mp_err err;

if (b < 0) {
if (a == NULL || c == NULL || b < 0) {
return MP_VAL;
}

Expand Down
3 changes: 3 additions & 0 deletions mp_exch.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/
void mp_exch(mp_int *a, mp_int *b)
{
if (a == NULL || b == NULL || a == b) {
return;
}
MP_EXCH(mp_int, *a, *b);
}
#endif
4 changes: 4 additions & 0 deletions mp_exptmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ mp_err mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y)
{
int dr;

if (G == NULL || X == NULL || P == NULL || Y == NULL) {
return MP_VAL;
}

/* modulus P must be positive */
if (mp_isneg(P)) {
return MP_VAL;
Expand Down
4 changes: 4 additions & 0 deletions mp_fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ mp_err mp_fread(mp_int *a, int radix, FILE *stream)
mp_sign sign = MP_ZPOS;
int ch;

if (a == NULL || stream == NULL) {
return MP_VAL;
}

/* make sure the radix is ok */
if ((radix < 2) || (radix > 64)) {
return MP_VAL;
Expand Down
4 changes: 4 additions & 0 deletions mp_from_sbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mp_err mp_from_sbin(mp_int *a, const uint8_t *buf, size_t size)
{
mp_err err;

if (a == NULL || (size > 0u && buf == NULL)) {
return MP_VAL;
}

if (size == 0) {
return MP_VAL;
}
Expand Down
4 changes: 4 additions & 0 deletions mp_from_ubin.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mp_err mp_from_ubin(mp_int *a, const uint8_t *buf, size_t size)
{
mp_err err;

if (a == NULL || (size > 0u && buf == NULL)) {
return MP_VAL;
}

/* make sure there are at least two digits */
if ((err = mp_grow(a, 2)) != MP_OKAY) {
return err;
Expand Down
4 changes: 4 additions & 0 deletions mp_fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ mp_err mp_fwrite(const mp_int *a, int radix, FILE *stream)
mp_err err;
size_t size, written;

if (a == NULL || stream == NULL) {
return MP_VAL;
}

if ((err = mp_radix_size_overestimate(a, radix, &size)) != MP_OKAY) {
return err;
}
Expand Down
4 changes: 4 additions & 0 deletions mp_gcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ mp_err mp_gcd(const mp_int *a, const mp_int *b, mp_int *c)
int k, u_lsb, v_lsb;
mp_err err;

if (a == NULL || b == NULL || c == NULL) {
return MP_VAL;
}

/* either zero than gcd is the largest */
if (mp_iszero(a)) {
return mp_abs(b, c);
Expand Down
5 changes: 5 additions & 0 deletions mp_get_double.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ double mp_get_double(const mp_int *a)
{
int i;
double d = 0.0, fac = 1.0;

if (a == NULL) {
return 0.0;
}

for (i = 0; i < MP_DIGIT_BIT; ++i) {
fac *= 2.0;
}
Expand Down
2 changes: 1 addition & 1 deletion mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* grow as required */
mp_err mp_grow(mp_int *a, int size)
{
if (size < 0) {
if (a == NULL || size < 0) {
return MP_VAL;
}

Expand Down
4 changes: 4 additions & 0 deletions mp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/* init a new mp_int */
mp_err mp_init(mp_int *a)
{
if (a == NULL) {
return MP_VAL;
}

/* allocate memory required and clear it */
a->dp = (mp_digit *) MP_CALLOC((size_t)MP_DEFAULT_DIGIT_COUNT, sizeof(mp_digit));
if (a->dp == NULL) {
Expand Down
4 changes: 4 additions & 0 deletions mp_init_copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ mp_err mp_init_copy(mp_int *a, const mp_int *b)
{
mp_err err;

if (a == NULL || b == NULL) {
return MP_VAL;
}

if ((err = mp_init_size(a, b->used)) != MP_OKAY) {
return err;
}
Expand Down
Loading