Split up the 150+ mpi test cases into smaller groups.

This commit is contained in:
Dag-Erling Smørgrav 2017-03-13 23:57:59 +01:00
parent 95ba4d39da
commit fc99e7973b
7 changed files with 1445 additions and 1022 deletions

3
t/.gitignore vendored
View File

@ -30,6 +30,9 @@
/t_md5_openssl
/t_memset_s
/t_mpi
/t_mpi_addsub
/t_mpi_compar
/t_mpi_muldiv
/t_murmur3_32
/t_oath
/t_pearson

View File

@ -210,8 +210,12 @@ endif CRYB_MAC
# libcryb-mpi
if CRYB_MPI
TESTS += t_mpi
TESTS += t_mpi t_mpi_addsub t_mpi_compar t_mpi_muldiv
t_mpi_LDADD = $(libt) $(libmpi)
t_mpi_addsub_LDADD = $(libt) $(libmpi)
t_mpi_compar_LDADD = $(libt) $(libmpi)
t_mpi_muldiv_LDADD = $(libt) $(libmpi)
noinst_HEADER = t_mpi.h
endif CRYB_MPI
# libcryb-oath

1027
t/t_mpi.c

File diff suppressed because it is too large Load Diff

199
t/t_mpi.h Normal file
View File

@ -0,0 +1,199 @@
/*
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef T_MPI_H_INCLUDED
#define T_MPI_H_INCLUDED
/***************************************************************************
* Useful constants
*/
static const cryb_mpi z;
#define SMALL_V_SIZE (size_t)64
#define SMALL_E_SIZE (size_t)16
static uint8_t small_v[SMALL_V_SIZE];
static uint32_t small_e[SMALL_E_SIZE];
#define LARGE_V_SIZE (size_t)256
#define LARGE_E_SIZE (size_t)64
static uint8_t large_v[LARGE_V_SIZE];
static uint32_t large_e[LARGE_E_SIZE];
/***************************************************************************
* Utilities
*/
static void t_printv_mpi(const cryb_mpi *) CRYB_UNUSED;
/*
* Print an MPI in semi-human-readable form
*/
static void
t_printv_mpi(const cryb_mpi *x)
{
t_printv("%c", x->neg ? '-' : '+');
t_printv("%08x", x->msb == 0 ? 0 : x->words[0]);
for (unsigned int i = 1; i < (x->msb + 31) / 32; ++i)
t_printv(" %08x", x->words[i]);
}
/***************************************************************************
* Commonly used predicates
*/
static int t_mpi_not_grown(const cryb_mpi *) CRYB_UNUSED;
static int t_mpi_grown(const cryb_mpi *) CRYB_UNUSED;
static int t_mpi_is_zero(const cryb_mpi *) CRYB_UNUSED;
static int t_compare_mpi(const cryb_mpi *, const cryb_mpi *) CRYB_UNUSED;
/*
* Verify that an MPI has never grown.
*/
static int
t_mpi_not_grown(const cryb_mpi *x)
{
int ret = 1;
ret &= t_compare_ptr(x->swords, x->words);
ret &= t_compare_sz(CRYB_MPI_SWORDS, x->size);
return (ret);
}
/*
* Verify that an MPI has grown.
*/
static int
t_mpi_grown(const cryb_mpi *x)
{
int ret = 1;
/* XXX we need inequality predicates */
if (x->words == x->swords) {
t_printv("value was expected to change");
ret &= 0;
}
if (x->size == CRYB_MPI_SWORDS) {
t_printv("value was expected to change");
ret &= 0;
}
return (ret);
}
/*
* Verify that an MPI is zero.
*/
static int
t_mpi_is_zero(const cryb_mpi *x)
{
int ret = 1;
ret &= t_mpi_not_grown(x);
ret &= t_compare_mem(t_zero, x->words, CRYB_MPI_SWORDS);
ret &= t_compare_u(0, x->msb);
ret &= t_compare_i(0, x->neg);
return (ret);
}
/*
* Verify that an MPI has the expected value
*/
static int
t_compare_mpi(const cryb_mpi *e, const cryb_mpi *x)
{
int ret = 1;
if (e == x)
return (1);
if (e == NULL || x == NULL)
return (0);
if (x->words == NULL) {
t_printv("uninitialized MPI\n");
return (0);
}
if (e->neg != x->neg || e->msb != x->msb ||
memcmp(e->words, x->words, (e->msb + 7) / 8) != 0) {
t_printv("expected ");
t_printv_mpi(e);
t_printv("\n");
t_printv("received ");
t_printv_mpi(x);
t_printv("\n");
ret = 0;
}
return (ret);
}
/***************************************************************************
* Boilerplate
*/
static void t_mpi_prepare(void);
static void t_mpi_cleanup(void);
/*
* Initialize constants used in multiple test cases
*/
static void
t_mpi_prepare(void)
{
unsigned int i;
t_assert(sizeof small_e == sizeof small_v);
t_assert(sizeof small_e == sizeof z.swords);
for (i = 0; i < SMALL_V_SIZE; ++i)
small_v[i] = ~i;
for (i = 0; i < SMALL_E_SIZE; ++i)
small_e[SMALL_E_SIZE - 1 - i] = be32dec(small_v + i * 4);
t_assert(sizeof large_e == sizeof large_v);
t_assert(sizeof large_e > sizeof z.swords);
for (i = 0; i < LARGE_V_SIZE; ++i)
large_v[i] = ~i;
for (i = 0; i < LARGE_E_SIZE; ++i)
large_e[LARGE_E_SIZE - 1 - i] = be32dec(large_v + i * 4);
}
/*
* Cleanup and sanity checks
*/
static void
t_mpi_cleanup(void)
{
t_assert(memcmp(&z, t_zero, sizeof z) == 0);
}
#endif

879
t/t_mpi_addsub.c Normal file
View File

@ -0,0 +1,879 @@
/*
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cryb/endian.h>
#include <cryb/mpi.h>
#include <cryb/test.h>
#include "t_mpi.h"
/***************************************************************************
* Addition
*/
static struct t_add_case {
const char *desc;
uint8_t a[16];
size_t amsb;
int aneg:1;
uint8_t b[16];
size_t bmsb;
int bneg:1;
uint8_t e[16];
size_t emsb;
int eneg:1;
} t_add_cases[] = {
{
"0 + 0 == 0",
{ }, 0, 0,
{ }, 0, 0,
{ }, 0, 0,
},
{
"0 + 1 == 1",
{ }, 0, 0,
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
},
{
"1 + 0 == 1",
{ 0x01, }, 1, 0,
{ }, 0, 0,
{ 0x01, }, 1, 0,
},
{
"2 + 2 == 4",
{ 0x02, }, 2, 0,
{ 0x02, }, 2, 0,
{ 0x04, }, 3, 0,
},
{
"|0x20140901| + |0x19700101| == 0x39840a02",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 0,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 0,
{ 0x39, 0x84, 0x0a, 0x02, }, 30, 0,
},
{
"|-0x20140901| + |0x19700101| == 0x39840a02",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 1,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 0,
{ 0x39, 0x84, 0x0a, 0x02, }, 30, 0,
},
{
"|0x20140901| + |-0x19700101| == 0x39840a02",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 0,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 1,
{ 0x39, 0x84, 0x0a, 0x02, }, 30, 0,
},
{
"|-0x20140901| + |-0x19700101| == 0x39840a02",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 1,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 1,
{ 0x39, 0x84, 0x0a, 0x02, }, 30, 0,
},
{
"simple carry, "
"0xffffffff + 0x01 == 0x0100000000",
{ 0xff, 0xff, 0xff, 0xff, }, 32, 0,
{ 0x01, }, 1, 0,
{ 0x01, 0x00, 0x00, 0x00, 0x00, }, 33, 0,
},
{
"complex carry, "
"0x0100000000ffffffff + 0x01 == 0x010000000100000000",
{ 0x01, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, }, 64, 0,
{ 0x01, }, 1, 0,
{ 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, }, 64, 0,
},
};
static int
t_mpi_add_tc(char **desc CRYB_UNUSED, void *arg)
{
struct t_add_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, tc->a, (tc->amsb + 7) / 8);
a.neg = tc->aneg;
mpi_load(&b, tc->b, (tc->bmsb + 7) / 8);
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_add_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* All operands are different
*/
static int
t_mpi_add(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* As above, but first allocation will fail
*/
static int
t_mpi_add_fail1(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, large_v, sizeof large_v);
mpi_set(&b, 0x19700101);
mpi_set(&x, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&x, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* As above, but second allocation will fail
*/
static int
t_mpi_add_fail2(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_load(&b, large_v, sizeof large_v);
mpi_set(&x, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&x, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* Target is the first operand
*/
static int
t_mpi_add_b_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_add_b_to_a_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_load(&b, large_v, sizeof large_v);
mpi_set(&e, 0x19700101);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&a, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the first operand and both operands are equal
*/
static int
t_mpi_add_b_to_a_equal(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the second operand
*/
static int
t_mpi_add_a_to_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x19700101 + 0x20140901);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_add_a_to_b_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, large_v, sizeof large_v);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&b, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the second operand and both operands are equal
*/
static int
t_mpi_add_a_to_b_equal(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is both operands
*/
static int
t_mpi_add_a_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &a));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_add_a_to_a_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, large_v, sizeof large_v);
mpi_load(&e, large_v, sizeof large_v);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&a, &a, &a));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
return (ret);
}
/*
* Operands are the same
*/
static int
t_mpi_add_a_and_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&e, 0x19700101 + 0x19700101);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &a));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_add_a_and_a_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, large_v, sizeof large_v);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_add_abs(&b, &a, &a));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
return (ret);
}
/*
* Target is the first operand, second operand is zero
*/
static int
t_mpi_add_zero_to_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_zero(&b);
mpi_copy(&e, &a);
ret &= t_compare_i(0, mpi_add_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the second operand, first operand is zero
*/
static int
t_mpi_add_zero_to_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_zero(&a);
mpi_set(&b, 0x20140901);
mpi_copy(&e, &b);
ret &= t_compare_i(0, mpi_add_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/***************************************************************************
* Subtraction
*/
static struct t_sub_case {
const char *desc;
uint8_t a[16];
size_t amsb;
int aneg:1;
uint8_t b[16];
size_t bmsb;
int bneg:1;
uint8_t e[16];
size_t emsb;
int eneg:1;
} t_sub_cases[] = {
{
"0 - 0 == 0",
{ }, 0, 0,
{ }, 0, 0,
{ }, 0, 0,
},
{
"0 - 1 == 1",
{ }, 0, 0,
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
},
{
"1 - 0 == 1",
{ 0x01, }, 1, 0,
{ }, 0, 0,
{ 0x01, }, 1, 0,
},
{
"1 - 1 == 0",
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
{ }, 0, 0,
},
{
"4 - 2 == 2",
{ 0x04, }, 3, 0,
{ 0x02, }, 2, 0,
{ 0x02, }, 2, 0,
},
{
"2 - 4 == 2",
{ 0x02, }, 2, 0,
{ 0x04, }, 3, 0,
{ 0x02, }, 2, 0,
},
{
"| |0x20140901| - |0x19700101| | == 0x6a40800",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 0,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 0,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"| |-0x20140901| - |0x19700101| | == 0x6a40800",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 1,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 0,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"| |0x20140901| - |-0x19700101| | == 0x6a40800",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 0,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 1,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"| |-0x20140901| - |-0x19700101| | == 0x6a40800",
{ 0x20, 0x14, 0x09, 0x01, }, 30, 1,
{ 0x19, 0x70, 0x01, 0x01, }, 29, 1,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"0x120140901 - 0x119700101 == 0x6a40800",
{ 0x01, 0x20, 0x14, 0x09, 0x01, }, 33, 0,
{ 0x01, 0x19, 0x70, 0x01, 0x01, }, 33, 0,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"0x119700101 - 0x120140901 == 0x6a40800",
{ 0x01, 0x19, 0x70, 0x01, 0x01, }, 33, 0,
{ 0x01, 0x20, 0x14, 0x09, 0x01, }, 33, 0,
{ 0x06, 0xa4, 0x08, 0x00, }, 27, 0,
},
{
"simple carry, "
"0x1000000000 - 0xfffffffff == 0x01",
{ 0x10, 0x00, 0x00, 0x00, 0x00, }, 37, 0,
{ 0x0f, 0xff, 0xff, 0xff, 0xff, }, 36, 0,
{ 0x01, }, 1, 0,
},
{
"simple carry, "
"0x1000000000 - 0x01 == 0xfffffffff",
{ 0x10, 0x00, 0x00, 0x00, 0x00, }, 37, 0,
{ 0x01, }, 1, 0,
{ 0x0f, 0xff, 0xff, 0xff, 0xff, }, 36, 0,
},
{
"complex carry, "
"0x010000000000000000 - 0xffffffffffffffff == 0x01",
{ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, 65, 0,
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }, 64, 0,
{ 0x01, }, 1, 0,
},
{
"complex carry, "
"0x010000000000000000 - 0x01 == 0xffffffffffffffff",
{ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }, 65, 0,
{ 0x01, }, 1, 0,
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }, 64, 0,
},
};
static int
t_mpi_sub_tc(char **desc CRYB_UNUSED, void *arg)
{
struct t_sub_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, tc->a, (tc->amsb + 7) / 8);
a.neg = tc->aneg;
mpi_load(&b, tc->b, (tc->bmsb + 7) / 8);
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* All operands are different
*/
static int
t_mpi_sub(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_sub_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, small_v, sizeof small_v);
mpi_load(&b, large_v, sizeof large_v);
mpi_set(&x, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_sub_abs(&x, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/*
* Target is the first operand
*/
static int
t_mpi_sub_b_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x20140901);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_sub_b_from_a_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_load(&b, large_v, sizeof large_v);
mpi_set(&e, 0x19700101);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_sub_abs(&a, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the second operand
*/
static int
t_mpi_sub_a_from_b(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901 - 0x19700101);
ret &= t_compare_i(0, mpi_sub_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* As above, but allocation will fail
*/
static int
t_mpi_sub_a_from_b_fail(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, large_v, sizeof large_v);
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901);
++t_malloc_fail;
ret &= t_compare_i(-1, mpi_sub_abs(&b, &a, &b));
--t_malloc_fail;
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is both operands
*/
static int
t_mpi_sub_a_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x19700101);
mpi_set(&e, 0);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &a));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the first operand, second operand is zero
*/
static int
t_mpi_sub_zero_from_a(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x20140901);
mpi_set(&e, 0x20140901);
ret &= t_compare_i(0, mpi_sub_abs(&a, &a, &b));
ret &= t_compare_mpi(&e, &a);
mpi_destroy(&a);
mpi_destroy(&e);
return (ret);
}
/*
* Target is the second operand, first operand is zero
*/
static int
t_mpi_sub_b_from_zero(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&b, 0x20140901);
mpi_set(&e, 0x20140901);
ret &= t_compare_i(0, mpi_sub_abs(&b, &a, &b));
ret &= t_compare_mpi(&e, &b);
mpi_destroy(&b);
mpi_destroy(&e);
return (ret);
}
/*
* Target is negative
*/
static int
t_mpi_sub_neg_target(char **desc CRYB_UNUSED, void *arg CRYB_UNUSED)
{
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, 0x20140901);
mpi_set(&b, 0x19700101);
mpi_set(&e, 0x20140901 - 0x19700101);
mpi_set(&x, -1);
ret &= t_compare_i(0, mpi_sub_abs(&x, &a, &b));
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/***************************************************************************
* Boilerplate
*/
static int
t_prepare(int argc, char *argv[])
{
unsigned int i;
(void)argc;
(void)argv;
t_mpi_prepare();
/* addition */
for (i = 0; i < sizeof t_add_cases / sizeof t_add_cases[0]; ++i)
t_add_test(t_mpi_add_tc, &t_add_cases[i],
"%s", t_add_cases[i].desc);
t_add_test(t_mpi_add, NULL, "x = a + b");
t_add_test(t_mpi_add_fail1, NULL, "x = a + b (failure 1)");
t_add_test(t_mpi_add_fail2, NULL, "x = a + b (failure 2)");
t_add_test(t_mpi_add_b_to_a, NULL, "a = a + b");
t_add_test(t_mpi_add_b_to_a_fail, NULL, "a = a + b (failure)");
t_add_test(t_mpi_add_b_to_a_equal, NULL, "a = a + b (a == b)");
t_add_test(t_mpi_add_a_to_b, NULL, "b = a + b");
t_add_test(t_mpi_add_a_to_b_fail, NULL, "b = a + b (failure)");
t_add_test(t_mpi_add_a_to_b_equal, NULL, "b = a + b (a == b)");
t_add_test(t_mpi_add_a_to_a, NULL, "a = a + a");
t_add_test(t_mpi_add_a_to_a_fail, NULL, "a = a + a (failure)");
t_add_test(t_mpi_add_a_and_a, NULL, "b = a + a");
t_add_test(t_mpi_add_a_and_a_fail, NULL, "b = a + a (failure)");
t_add_test(t_mpi_add_zero_to_a, NULL, "a = a + 0");
t_add_test(t_mpi_add_zero_to_b, NULL, "b = 0 + b");
/* subtraction */
for (i = 0; i < sizeof t_sub_cases / sizeof t_sub_cases[0]; ++i)
t_add_test(t_mpi_sub_tc, &t_sub_cases[i],
"%s", t_sub_cases[i].desc);
t_add_test(t_mpi_sub, NULL, "x = a - b");
t_add_test(t_mpi_sub_fail, NULL, "x = a - b (failure)");
t_add_test(t_mpi_sub_b_from_a, NULL, "a = a - b");
t_add_test(t_mpi_sub_b_from_a_fail, NULL, "a = a - b (failure)");
t_add_test(t_mpi_sub_a_from_b, NULL, "b = a - b");
t_add_test(t_mpi_sub_a_from_b_fail, NULL, "b = a - b (failure)");
t_add_test(t_mpi_sub_a_from_a, NULL, "a = a - a");
t_add_test(t_mpi_sub_b_from_zero, NULL, "b = 0 - b");
t_add_test(t_mpi_sub_zero_from_a, NULL, "a = a - z");
t_add_test(t_mpi_sub_neg_target, NULL, "x = a - b (x initially < 0)");
return (0);
}
static void
t_cleanup(void)
{
t_mpi_cleanup();
}
int
main(int argc, char *argv[])
{
t_main(t_prepare, t_cleanup, argc, argv);
}

164
t/t_mpi_compar.c Normal file
View File

@ -0,0 +1,164 @@
/*
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cryb/endian.h>
#include <cryb/mpi.h>
#include <cryb/test.h>
#include "t_mpi.h"
/***************************************************************************
* Comparison
*/
static struct t_cmp_case {
const char *desc;
int a, b, cmp;
} t_cmp_cases[] = {
{ "-3 == -3", -3, -3, 0 },
{ "-3 < -2", -3, -2, -1 },
{ "-3 < -1", -3, -1, -1 },
{ "-3 < 0", -3, 0, -1 },
{ "-3 < 1", -3, 1, -1 },
{ "-3 < 2", -3, 2, -1 },
{ "-3 < 3", -3, 3, -1 },
{ "-2 > -3", -2, -3, 1 },
{ "-2 == -2", -2, -2, 0 },
{ "-2 < -1", -2, -1, -1 },
{ "-2 < 0", -2, 0, -1 },
{ "-2 < 1", -2, 1, -1 },
{ "-2 < 2", -2, 2, -1 },
{ "-2 < 3", -2, 3, -1 },
{ "-1 > -3", -1, -3, 1 },
{ "-1 > -2", -1, -2, 1 },
{ "-1 == -1", -1, -1, 0 },
{ "-1 < 0", -1, 0, -1 },
{ "-1 < 1", -1, 1, -1 },
{ "-1 < 2", -1, 2, -1 },
{ "-1 < 3", -1, 3, -1 },
{ "0 > -3", 0, -3, 1 },
{ "0 > -2", 0, -2, 1 },
{ "0 > -1", 0, -1, 1 },
{ "0 == 0", 0, 0, 0 },
{ "0 < 1", 0, 1, -1 },
{ "0 < 2", 0, 2, -1 },
{ "0 < 3", 0, 3, -1 },
{ "1 > -3", 1, -3, 1 },
{ "1 > -2", 1, -2, 1 },
{ "1 > -1", 1, -1, 1 },
{ "1 > 0", 1, 0, 1 },
{ "1 == 1", 1, 1, 0 },
{ "1 < 2", 1, 2, -1 },
{ "1 < 3", 1, 3, -1 },
{ "2 > -3", 2, -3, 1 },
{ "2 > -2", 2, -2, 1 },
{ "2 > -1", 2, -1, 1 },
{ "2 > 0", 2, 0, 1 },
{ "2 > 1", 2, 1, 1 },
{ "2 == 2", 2, 2, 0 },
{ "2 < 3", 2, 3, -1 },
{ "3 > -3", 3, -3, 1 },
{ "3 > -2", 3, -2, 1 },
{ "3 > -1", 3, -1, 1 },
{ "3 > 0", 3, 0, 1 },
{ "3 > 1", 3, 1, 1 },
{ "3 > 2", 3, 2, 1 },
{ "3 == 3", 3, 3, 0 },
};
/*
* Compare two MPIs
*/
static int
t_mpi_cmp(char **desc CRYB_UNUSED, void *arg)
{
struct t_cmp_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO;
int ret = 1;
mpi_set(&a, tc->a);
mpi_set(&b, tc->b);
ret &= t_compare_i(tc->cmp, mpi_cmp(&a, &b));
mpi_destroy(&a);
mpi_destroy(&b);
return (ret);
}
/***************************************************************************
* Boilerplate
*/
static int
t_prepare(int argc, char *argv[])
{
unsigned int i;
(void)argc;
(void)argv;
t_mpi_prepare();
/* comparison */
for (i = 0; i < sizeof t_cmp_cases / sizeof t_cmp_cases[0]; ++i)
t_add_test(t_mpi_cmp, &t_cmp_cases[i],
"%s", t_cmp_cases[i].desc);
return (0);
}
static void
t_cleanup(void)
{
t_mpi_cleanup();
}
int
main(int argc, char *argv[])
{
t_main(t_prepare, t_cleanup, argc, argv);
}

189
t/t_mpi_muldiv.c Normal file
View File

@ -0,0 +1,189 @@
/*
* Copyright (c) 2014-2017 Dag-Erling Smørgrav
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "cryb/impl.h"
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <cryb/endian.h>
#include <cryb/mpi.h>
#include <cryb/test.h>
#include "t_mpi.h"
/***************************************************************************
* Multiplication
*/
static struct t_mul_case {
const char *desc;
uint8_t a[16];
size_t amsb;
int aneg:1;
uint8_t b[16];
size_t bmsb;
int bneg:1;
uint8_t e[16];
size_t emsb;
int eneg:1;
} t_mul_cases[] = {
{
"1 * 1 == 1",
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
},
};
static int
t_mpi_mul_tc(char **desc CRYB_UNUSED, void *arg)
{
struct t_mul_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, tc->a, (tc->amsb + 7) / 8);
a.neg = tc->aneg;
mpi_load(&b, tc->b, (tc->bmsb + 7) / 8);
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
#if 0
ret &= t_compare_i(0, mpi_mul_abs(&x, &a, &b));
#else
mpi_load(&x, tc->e, (tc->emsb + 7) / 8);
#endif
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/***************************************************************************
* Division
*/
static struct t_div_case {
const char *desc;
uint8_t a[16];
size_t amsb;
int aneg:1;
uint8_t b[16];
size_t bmsb;
int bneg:1;
uint8_t e[16];
size_t emsb;
int eneg:1;
} t_div_cases[] = {
{
"1 / 1 == 1",
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
{ 0x01, }, 1, 0,
},
};
static int
t_mpi_div_tc(char **desc CRYB_UNUSED, void *arg)
{
struct t_div_case *tc = arg;
cryb_mpi a = CRYB_MPI_ZERO, b = CRYB_MPI_ZERO, e = CRYB_MPI_ZERO;
cryb_mpi x = CRYB_MPI_ZERO;
int ret = 1;
mpi_load(&a, tc->a, (tc->amsb + 7) / 8);
a.neg = tc->aneg;
mpi_load(&b, tc->b, (tc->bmsb + 7) / 8);
b.neg = tc->bneg;
mpi_load(&e, tc->e, (tc->emsb + 7) / 8);
e.neg = tc->eneg;
#if 0
ret &= t_compare_i(0, mpi_div_abs(&x, &a, &b));
#else
mpi_load(&x, tc->e, (tc->emsb + 7) / 8);
#endif
ret &= t_compare_mpi(&e, &x);
mpi_destroy(&a);
mpi_destroy(&b);
mpi_destroy(&e);
mpi_destroy(&x);
return (ret);
}
/***************************************************************************
* Boilerplate
*/
static int
t_prepare(int argc, char *argv[])
{
unsigned int i;
(void)argc;
(void)argv;
t_mpi_prepare();
/* multiplication */
for (i = 0; i < sizeof t_mul_cases / sizeof t_mul_cases[0]; ++i)
t_add_test(t_mpi_mul_tc, &t_mul_cases[i],
"%s", t_mul_cases[i].desc);
/* division */
for (i = 0; i < sizeof t_div_cases / sizeof t_div_cases[0]; ++i)
t_add_test(t_mpi_div_tc, &t_div_cases[i],
"%s", t_div_cases[i].desc);
return (0);
}
static void
t_cleanup(void)
{
t_mpi_cleanup();
}
int
main(int argc, char *argv[])
{
t_main(t_prepare, t_cleanup, argc, argv);
}