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
30 changes: 30 additions & 0 deletions bn_mp_factors_add.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_ADD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Add one element (mp_int) to a factor-array (an array of mp_int's) */
int mp_factors_add(const mp_int *a, mp_factors *f)
{
int new_size = f->alloc;
mp_int *tmp;
mp_int t;
if (f->alloc <= f->length) {
new_size += LTM_TRIAL_GROWTH;
tmp = (mp_int *) XREALLOC(f->factors, sizeof(*tmp) * f->alloc,
sizeof(*tmp) * (size_t)(new_size));
if (tmp == NULL) {
return MP_MEM;
}
f->factors = tmp;
f->alloc = new_size;
}
mp_init(&t);
mp_copy(a,&t);
f->factors[f->length] = t;
f->length++;
return MP_OKAY;
}

#endif

22 changes: 22 additions & 0 deletions bn_mp_factors_clear.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_CLEAR_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Clear and free factor-array (an array of mp_int's) */
void mp_factors_clear(mp_factors *f)
{
int i;
if (f->factors != NULL) {
for (i = 0; i < f->length; i++) {
mp_clear(&f->factors[i]);
}
XFREE(f->factors, sizeof(mp_int) * f->alloc);
f->factors = NULL;
f->alloc = 0;
f->length = 0;
}
}

#endif

68 changes: 68 additions & 0 deletions bn_mp_factors_compress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_COMPRESS_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */


/* Change (sorted) factors array into format prime,prime-count,...,prime,prime-count */
int mp_factors_compress(mp_factors *f, int sorted, mp_factors *g)
{
int i, e = MP_OKAY;
mp_int count;
mp_factors in, out, t;

if ((e = mp_init(&count)) != MP_OKAY) {
goto LTM_ERR;
}
if ((e = mp_factors_init(&out)) != MP_OKAY) {
goto LTM_ERR;
}

if ((e = mp_factors_init(&in)) != MP_OKAY) {
goto LTM_ERR;
}
for (i = 0; i < f->length; i++) {
if ((e = mp_factors_add(&(f->factors[i]), &in)) != MP_OKAY) {
goto LTM_ERR;
}
}
if (sorted == MP_NO) {
mp_factors_sort(&in);
}

for (i = 1; i < in.length; i++) {
if ((e = mp_incr(&count)) != MP_OKAY) {
goto LTM_ERR;
}
if (mp_cmp(&(in.factors[i-1]), &(in.factors[i])) != MP_EQ) {
if ((e = mp_factors_add(&(in.factors[i-1]), &out)) != MP_OKAY) {
goto LTM_ERR;
}
if ((e = mp_factors_add(&count, &out)) != MP_OKAY) {
goto LTM_ERR;
}
mp_zero(&count);
}
}
/* and the last one, too */
if ((e = mp_incr(&count)) != MP_OKAY) {
goto LTM_ERR;
}
if ((e = mp_factors_add(&(in.factors[i-1]), &out)) != MP_OKAY) {
goto LTM_ERR;
}
if ((e = mp_factors_add(&count, &out)) != MP_OKAY) {
goto LTM_ERR;
}

t = *g;
*g = out;
out = t;

LTM_ERR:
mp_factors_clear(&in);
mp_clear(&count);
mp_factors_clear(&out);
return e;
}
#endif
19 changes: 19 additions & 0 deletions bn_mp_factors_init.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_INIT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Init factor-array (an array of mp_int's) */
int mp_factors_init(mp_factors *f)
{
f->factors = (mp_int *) XCALLOC((size_t)(LTM_TRIAL_GROWTH), sizeof(mp_int));
if ((f->factors) == NULL) {
return MP_MEM;
}
f->alloc = LTM_TRIAL_GROWTH;
f->length = 0;
return MP_OKAY;
}

#endif

32 changes: 32 additions & 0 deletions bn_mp_factors_print.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_PRINT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Print the elememts of a factor-array (an array of mp_int's) */
int mp_factors_print(const mp_factors *f, int base, char delimiter, FILE *stream)
{
int i, e = MP_OKAY;
int d;
if (delimiter != 0) {
d = (int)delimiter;
} else {
d = ',';
}
if (f->factors != NULL) {
for (i = 0; i < f->length; i++) {
if ((e = mp_fwrite(&(f->factors[i]),base, stream)) != MP_OKAY) {
return e;
}
if (i < ((f->length)-1)) {
if (fputc(d, stream) == EOF) {
return MP_VAL;
}
}
}
}
return e;
}

#endif

63 changes: 63 additions & 0 deletions bn_mp_factors_product.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_PRODUCT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Compute the product of the elements of "factors" */
static int s_product_lowlevel(const mp_factors *factors, int start, int n, mp_int *result)
{
int i, first_half, second_half;
mp_int tmp;
int e = MP_OKAY;

if (n == 0) {
mp_set(result, 1uL);
return MP_OKAY;
}
/* Do the rest linearily. Faster for primorials at least, but YMMV */
if (n <= 64) {
mp_set_long(result,1uL);
for (i = 0; i < n; i++) {
if ((e = mp_mul(result, &(factors->factors[i+start]), result)) != MP_OKAY) {
return e;
}
}
return MP_OKAY;
}

first_half = n / 2;
second_half = n - first_half;
if ((e = s_product_lowlevel(factors, start, second_half, result)) != MP_OKAY) {
return e;
}
if ((e = mp_init(&tmp)) != MP_OKAY) {
return e;
}
if ((e = s_product_lowlevel(factors, start+second_half, first_half, &tmp)) != MP_OKAY) {
goto LTM_ERR;
}
if ((e = mp_mul(result, &tmp, result)) != MP_OKAY) {
goto LTM_ERR;
}

LTM_ERR:
mp_clear(&tmp);
return e;
}

int mp_factors_product(const mp_factors *factors, mp_int *p)
{
int e, r;
r = factors->length;
if (r == 0) {
return MP_VAL;
}
if (r == 1) {
return mp_copy(&(factors->factors[0]), p);
}
if ((e = s_product_lowlevel(factors, 0, r, p)) != MP_OKAY) {
return e;
}
return MP_OKAY;
}
#endif
27 changes: 27 additions & 0 deletions bn_mp_factors_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_SORT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Sort factor-array (an array of mp_int's) */
/* TODO: Sorts inline. Work on copy instead? */
void mp_factors_sort(mp_factors *f)
{
int i, idx;
mp_int tmp;

/*
It will be almost always be already sorted and even if it is unsorted
it will just show a bushy tail. So insertion sort it is.
*/
for (i = 1 ; i < f->length; i++) {
idx = i;
while ((idx > 0) && (mp_cmp(&(f->factors[idx-1]),&(f->factors[idx])) == MP_GT)) {
mp_exch(&(f->factors[idx]), &tmp);
mp_exch(&(f->factors[idx-1]), &(f->factors[idx]));
mp_exch(&tmp, &(f->factors[idx-1]));
idx--;
}
}
}
#endif
33 changes: 33 additions & 0 deletions bn_mp_factors_zero.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "tommath_private.h"
#ifdef BN_MP_FACTORS_ZERO_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */

/* Zero the elements of a factor-array (an array of mp_int's) and realloc to default
size in that order */
int mp_factors_zero(mp_factors *f)
{
int i, e = MP_OKAY;
mp_int *tmp;

if (f->factors != NULL) {
for (i = 0; i < f->length; i++) {
mp_clear(&(f->factors[i]));
}
}

tmp = (mp_int *) XREALLOC(f->factors, sizeof(*tmp) * f->alloc,
sizeof(*tmp) * LTM_TRIAL_GROWTH);
if (tmp == NULL) {
return MP_MEM;
}

f->factors = tmp;
f->alloc = LTM_TRIAL_GROWTH;
f->length = 0;

return e;
}

#endif

Loading