hcrypto: import libtommath v1.2.0

This commit is contained in:
Luke Howard
2020-04-12 18:37:13 +10:00
parent 7181c109d0
commit c403b66082
287 changed files with 28273 additions and 38374 deletions

View File

@@ -1,67 +1,60 @@
#include <tommath.h>
#include "tommath_private.h"
#ifdef BN_MP_FREAD_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#ifndef MP_NO_FILE
/* read a bigint from a file stream in ASCII */
int mp_fread(mp_int *a, int radix, FILE *stream)
mp_err mp_fread(mp_int *a, int radix, FILE *stream)
{
int err, ch, neg, y;
/* clear a */
mp_zero(a);
mp_err err;
mp_sign neg;
/* if first digit is - then set negative */
ch = fgetc(stream);
if (ch == '-') {
int ch = fgetc(stream);
if (ch == (int)'-') {
neg = MP_NEG;
ch = fgetc(stream);
} else {
neg = MP_ZPOS;
}
for (;;) {
/* find y in the radix map */
for (y = 0; y < radix; y++) {
if (mp_s_rmap[y] == ch) {
break;
}
/* no digits, return error */
if (ch == EOF) {
return MP_ERR;
}
/* clear a */
mp_zero(a);
do {
int y;
unsigned pos = (unsigned)(ch - (int)'(');
if (mp_s_rmap_reverse_sz < pos) {
break;
}
if (y == radix) {
y = (int)mp_s_rmap_reverse[pos];
if ((y == 0xff) || (y >= radix)) {
break;
}
/* shift up and add */
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
if ((err = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
return err;
}
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
if ((err = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
return err;
}
} while ((ch = fgetc(stream)) != EOF);
ch = fgetc(stream);
}
if (mp_cmp_d(a, 0) != MP_EQ) {
if (a->used != 0) {
a->sign = neg;
}
return MP_OKAY;
}
#endif
/* $Source: /cvs/libtom/libtommath/bn_mp_fread.c,v $ */
/* $Revision: 1.4 $ */
/* $Date: 2006/12/28 01:25:13 $ */
#endif