remove trailing whitespace
This commit is contained in:
@@ -1057,7 +1057,7 @@ static void camellia_encrypt128(const u32 *subkey, u32 *io)
|
||||
io[1] = io[3];
|
||||
io[2] = t0;
|
||||
io[3] = t1;
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1275,7 +1275,7 @@ static void camellia_decrypt256(const u32 *subkey, u32 *io)
|
||||
/* pre whitening but absorb kw2*/
|
||||
io[0] ^= CamelliaSubkeyL(32);
|
||||
io[1] ^= CamelliaSubkeyR(32);
|
||||
|
||||
|
||||
/* main iteration */
|
||||
CAMELLIA_ROUNDSM(io[0],io[1],
|
||||
CamelliaSubkeyL(31),CamelliaSubkeyR(31),
|
||||
|
@@ -254,10 +254,10 @@ DES_set_key_unchecked(DES_cblock *key, DES_key_schedule *ks)
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
uint32_t kc, kd;
|
||||
|
||||
|
||||
ROTATE_LEFT28(c, shifts[i]);
|
||||
ROTATE_LEFT28(d, shifts[i]);
|
||||
|
||||
|
||||
kc = pc2_c_1[(c >> 22) & 0x3f] |
|
||||
pc2_c_2[((c >> 16) & 0x30) | ((c >> 15) & 0xf)] |
|
||||
pc2_c_3[((c >> 9 ) & 0x3c) | ((c >> 8 ) & 0x3)] |
|
||||
@@ -780,7 +780,7 @@ DES_cbc_cksum(const void *in, DES_cblock *output,
|
||||
u[0] ^= uiv[0]; u[1] ^= uiv[1];
|
||||
DES_encrypt(u, ks, 1);
|
||||
uiv[0] = u[0]; uiv[1] = u[1];
|
||||
|
||||
|
||||
length -= DES_CBLOCK_LEN;
|
||||
input += DES_CBLOCK_LEN;
|
||||
}
|
||||
|
@@ -87,7 +87,7 @@ typedef struct DES_key_schedule
|
||||
#ifndef HC_DEPRECATED
|
||||
#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
|
||||
#define HC_DEPRECATED __attribute__((deprecated))
|
||||
#elif defined(_MSC_VER) && (_MSC_VER>1200)
|
||||
#elif defined(_MSC_VER) && (_MSC_VER>1200)
|
||||
#define HC_DEPRECATED __declspec(deprecated)
|
||||
#else
|
||||
#define HC_DEPRECATED
|
||||
|
@@ -493,7 +493,7 @@ main(int argc, char **argv)
|
||||
24,
|
||||
0x14a5029a,
|
||||
"\x33\xd2\xb5\x8a\x14\xa5\x02\x9a");
|
||||
|
||||
|
||||
cbc_cksum("\xfb\x89\xa1\x9d\xa7\xec\xc1\x5e",
|
||||
"\x9c\x7f\x47\xd0\x79\x5d\x4b\x97",
|
||||
"\xb6\x8b\x48\xe0\x01\x78\xec\x50\x7f\xf1\xfd\xd2\x87\x76\xba\x4b\x9c\x5c\xc7\x25",
|
||||
@@ -599,7 +599,7 @@ main(int argc, char **argv)
|
||||
"\x38\xef\x49\xbc\xdd\xbb\x6b\x73\xc0\xd7\xa6\x70\xe0\x1b\xde\x8d\xe6\xb4\xc6\x69\xca\x5e\x1e");
|
||||
|
||||
weak_test(1, "\x01\x01\x01\x01\x01\x01\x01\x01"); /* weak keys */
|
||||
weak_test(1, "\x01\x01\x01\x01\x01\x01\x01\x01");
|
||||
weak_test(1, "\x01\x01\x01\x01\x01\x01\x01\x01");
|
||||
weak_test(1, "\xFE\xFE\xFE\xFE\xFE\xFE\xFE\xFE");
|
||||
weak_test(1, "\x1F\x1F\x1F\x1F\x0E\x0E\x0E\x0E");
|
||||
weak_test(1, "\xE0\xE0\xE0\xE0\xF1\xF1\xF1\xF1");
|
||||
|
@@ -112,11 +112,11 @@ ltm_dh_generate_key(DH *dh)
|
||||
BN_free(dh->pub_key);
|
||||
|
||||
mp_init_multi(&pub, &priv_key, &g, &p, NULL);
|
||||
|
||||
|
||||
BN2mpz(&priv_key, dh->priv_key);
|
||||
BN2mpz(&g, dh->g);
|
||||
BN2mpz(&p, dh->p);
|
||||
|
||||
|
||||
res = mp_exptmod(&g, &priv_key, &p, &pub);
|
||||
|
||||
mp_clear_multi(&priv_key, &g, &p, NULL);
|
||||
@@ -127,7 +127,7 @@ ltm_dh_generate_key(DH *dh)
|
||||
mp_clear(&pub);
|
||||
if (dh->pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
|
||||
if (DH_check_pubkey(dh, dh->pub_key, &codes) && codes == 0)
|
||||
break;
|
||||
if (have_private_key)
|
||||
|
@@ -112,11 +112,11 @@ tfm_dh_generate_key(DH *dh)
|
||||
BN_free(dh->pub_key);
|
||||
|
||||
fp_init_multi(&pub, &priv_key, &g, &p, NULL);
|
||||
|
||||
|
||||
BN2mpz(&priv_key, dh->priv_key);
|
||||
BN2mpz(&g, dh->g);
|
||||
BN2mpz(&p, dh->p);
|
||||
|
||||
|
||||
res = fp_exptmod(&g, &priv_key, &p, &pub);
|
||||
|
||||
fp_zero(&priv_key);
|
||||
@@ -129,7 +129,7 @@ tfm_dh_generate_key(DH *dh)
|
||||
fp_zero(&pub);
|
||||
if (dh->pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
|
||||
if (DH_check_pubkey(dh, dh->pub_key, &codes) && codes == 0)
|
||||
break;
|
||||
if (have_private_key)
|
||||
|
@@ -91,7 +91,7 @@ EC_KEY *
|
||||
EC_KEY_new_by_curve_name(EC_GROUP_ID nid)
|
||||
{
|
||||
EC_KEY *key;
|
||||
|
||||
|
||||
key = calloc(1, sizeof(*key));
|
||||
return key;
|
||||
}
|
||||
|
@@ -42,7 +42,7 @@
|
||||
|
||||
int ECDSA_verify(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int, EC_KEY *);
|
||||
|
||||
|
||||
int ECDSA_sign(int, const unsigned char *, unsigned int,
|
||||
unsigned char *, unsigned int *, EC_KEY *);
|
||||
|
||||
|
@@ -339,7 +339,7 @@ ENGINE_by_dso(const char *path, const char *id)
|
||||
dlclose(handle);
|
||||
free(engine);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
@@ -357,7 +357,7 @@ ENGINE_by_dso(const char *path, const char *id)
|
||||
dlclose(handle);
|
||||
free(engine);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ENGINE_up_ref(engine);
|
||||
|
@@ -415,7 +415,7 @@ EVP_sha1(void)
|
||||
|
||||
const EVP_MD *
|
||||
EVP_sha(void) HC_DEPRECATED
|
||||
|
||||
|
||||
{
|
||||
hcrypto_validate();
|
||||
return EVP_sha1();
|
||||
@@ -875,7 +875,7 @@ EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
|
||||
ctx->buf_len += inlen;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/* fill in local buffer and encrypt */
|
||||
memcpy(ctx->buf + ctx->buf_len, in, left);
|
||||
ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
|
||||
@@ -893,7 +893,7 @@ EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
|
||||
if (inlen) {
|
||||
ctx->buf_len = (inlen & ctx->block_mask);
|
||||
inlen &= ~ctx->block_mask;
|
||||
|
||||
|
||||
ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
|
||||
if (ret != 1)
|
||||
return ret;
|
||||
|
@@ -195,7 +195,7 @@ struct hc_evp_md {
|
||||
#ifndef HC_DEPRECATED
|
||||
#if defined(__GNUC__) && ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1 )))
|
||||
#define HC_DEPRECATED __attribute__((deprecated))
|
||||
#elif defined(_MSC_VER) && (_MSC_VER>1200)
|
||||
#elif defined(_MSC_VER) && (_MSC_VER>1200)
|
||||
#define HC_DEPRECATED __declspec(deprecated)
|
||||
#else
|
||||
#define HC_DEPRECATED
|
||||
|
@@ -117,7 +117,7 @@ main(int argc, char **argv)
|
||||
*/
|
||||
EVP_CIPHER_CTX_init(&ctx);
|
||||
EVP_CipherInit_ex(&ctx, c, NULL, key, ivec, encryptp);
|
||||
|
||||
|
||||
/* read in buffer */
|
||||
while ((ilen = fread(ibuf, 1, block_size, in)) > 0) {
|
||||
/* encrypto/decrypt */
|
||||
|
@@ -15,10 +15,10 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||
* that is c = 1/a mod b
|
||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||
* that is c = 1/a mod b
|
||||
*
|
||||
* Based on slow invmod except this is optimized for the case where b is
|
||||
* Based on slow invmod except this is optimized for the case where b is
|
||||
* odd as per HAC Note 14.64 on pp. 610
|
||||
*/
|
||||
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
@@ -17,15 +17,15 @@
|
||||
|
||||
/* Fast (comba) multiplier
|
||||
*
|
||||
* This is the fast column-array [comba] multiplier. It is
|
||||
* designed to compute the columns of the product first
|
||||
* then handle the carries afterwards. This has the effect
|
||||
* This is the fast column-array [comba] multiplier. It is
|
||||
* designed to compute the columns of the product first
|
||||
* then handle the carries afterwards. This has the effect
|
||||
* of making the nested loops that compute the columns very
|
||||
* simple and schedulable on super-scalar processors.
|
||||
*
|
||||
* This has been modified to produce a variable number of
|
||||
* digits of output so if say only a half-product is required
|
||||
* you don't have to compute the upper half (a feature
|
||||
* This has been modified to produce a variable number of
|
||||
* digits of output so if say only a half-product is required
|
||||
* you don't have to compute the upper half (a feature
|
||||
* required for fast Barrett reduction).
|
||||
*
|
||||
* Based on Algorithm 14.12 on pp.595 of HAC.
|
||||
@@ -49,7 +49,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
/* clear the carry */
|
||||
_W = 0;
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
int tx, ty;
|
||||
int iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
@@ -62,7 +62,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially
|
||||
/* this is the number of times the loop will iterrate, essentially
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
@@ -41,7 +41,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* number of output digits to produce */
|
||||
pa = a->used + b->used;
|
||||
_W = 0;
|
||||
for (ix = digs; ix < pa; ix++) {
|
||||
for (ix = digs; ix < pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
|
||||
@@ -53,7 +53,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
@@ -69,7 +69,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* make next carry */
|
||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
||||
}
|
||||
|
||||
|
||||
/* setup dest */
|
||||
olduse = c->used;
|
||||
c->used = pa;
|
||||
|
@@ -16,10 +16,10 @@
|
||||
*/
|
||||
|
||||
/* the jist of squaring...
|
||||
* you do like mult except the offset of the tmpx [one that
|
||||
* starts closer to zero] can't equal the offset of tmpy.
|
||||
* you do like mult except the offset of the tmpx [one that
|
||||
* starts closer to zero] can't equal the offset of tmpy.
|
||||
* So basically you set up iy like before then you min it with
|
||||
* (ty-tx) so that it never happens. You double all those
|
||||
* (ty-tx) so that it never happens. You double all those
|
||||
* you add in the inner loop
|
||||
|
||||
After that loop you do the squares and add them in.
|
||||
@@ -41,7 +41,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
/* number of output digits to produce */
|
||||
W1 = 0;
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_word _W;
|
||||
mp_digit *tmpy;
|
||||
@@ -62,7 +62,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
||||
/* now for squaring tx can never equal ty
|
||||
/* now for squaring tx can never equal ty
|
||||
* we halve the distance since they approach at a rate of 2x
|
||||
* and we have to round because odd cases need to be executed
|
||||
*/
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* computes a = 2**b
|
||||
/* computes a = 2**b
|
||||
*
|
||||
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
||||
* as required.
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* b = |a|
|
||||
/* b = |a|
|
||||
*
|
||||
* Simple function copies the input and fixes the sign to positive
|
||||
*/
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* trim unused digits
|
||||
/* trim unused digits
|
||||
*
|
||||
* This is used to ensure that leading zero digits are
|
||||
* trimed and the leading "used" digit will be non-zero
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
|
||||
void mp_clear_multi(mp_int *mp, ...)
|
||||
void mp_clear_multi(mp_int *mp, ...)
|
||||
{
|
||||
mp_int* next_mp = mp;
|
||||
va_list args;
|
||||
|
@@ -27,7 +27,7 @@ mp_cmp (mp_int * a, mp_int * b)
|
||||
return MP_GT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* compare digits */
|
||||
if (a->sign == MP_NEG) {
|
||||
/* if negative compare opposite direction */
|
||||
|
@@ -25,7 +25,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
|
||||
if (a->used > b->used) {
|
||||
return MP_GT;
|
||||
}
|
||||
|
||||
|
||||
if (a->used < b->used) {
|
||||
return MP_LT;
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
static const int lnz[16] = {
|
||||
static const int lnz[16] = {
|
||||
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
|
||||
};
|
||||
|
||||
|
@@ -29,7 +29,7 @@ mp_count_bits (mp_int * a)
|
||||
|
||||
/* get number of digits and add that */
|
||||
r = (a->used - 1) * DIGIT_BIT;
|
||||
|
||||
|
||||
/* take the last digit and count the bits in it */
|
||||
q = a->dp[a->used - 1];
|
||||
while (q > ((mp_digit) 0)) {
|
||||
|
@@ -40,7 +40,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* init our temps */
|
||||
if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL) != MP_OKAY)) {
|
||||
return res;
|
||||
@@ -50,7 +50,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_set(&tq, 1);
|
||||
n = mp_count_bits(a) - mp_count_bits(b);
|
||||
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
||||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
||||
goto LBL_ERR;
|
||||
@@ -87,17 +87,17 @@ LBL_ERR:
|
||||
|
||||
#else
|
||||
|
||||
/* integer signed division.
|
||||
/* integer signed division.
|
||||
* c*b + d == a [e.g. a/b, c=quotient, d=remainder]
|
||||
* HAC pp.598 Algorithm 14.20
|
||||
*
|
||||
* Note that the description in HAC is horribly
|
||||
* incomplete. For example, it doesn't consider
|
||||
* the case where digits are removed from 'x' in
|
||||
* the inner loop. It also doesn't consider the
|
||||
* Note that the description in HAC is horribly
|
||||
* incomplete. For example, it doesn't consider
|
||||
* the case where digits are removed from 'x' in
|
||||
* the inner loop. It also doesn't consider the
|
||||
* case that y has fewer than three digits, etc..
|
||||
*
|
||||
* The overall algorithm is as described as
|
||||
* The overall algorithm is as described as
|
||||
* 14.20 from HAC but fixed to treat these cases.
|
||||
*/
|
||||
int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
@@ -187,7 +187,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
||||
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
|
||||
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
|
||||
if (x.dp[i] == y.dp[t]) {
|
||||
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
|
||||
@@ -201,10 +201,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
|
||||
}
|
||||
|
||||
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
||||
xi * b**2 + xi-1 * b + xi-2
|
||||
|
||||
do q{i-t-1} -= 1;
|
||||
/* while (q{i-t-1} * (yt * b + y{t-1})) >
|
||||
xi * b**2 + xi-1 * b + xi-2
|
||||
|
||||
do q{i-t-1} -= 1;
|
||||
*/
|
||||
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
|
||||
do {
|
||||
@@ -255,10 +255,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
}
|
||||
}
|
||||
|
||||
/* now q is the quotient and x is the remainder
|
||||
* [which we have to normalize]
|
||||
/* now q is the quotient and x is the remainder
|
||||
* [which we have to normalize]
|
||||
*/
|
||||
|
||||
|
||||
/* get sign before writing to c */
|
||||
x.sign = x.used == 0 ? MP_ZPOS : a->sign;
|
||||
|
||||
|
@@ -23,14 +23,14 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
mp_word w, t;
|
||||
mp_digit b;
|
||||
int res, ix;
|
||||
|
||||
|
||||
/* b = 2**DIGIT_BIT / 3 */
|
||||
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
|
||||
|
||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
q.used = a->used;
|
||||
q.sign = a->sign;
|
||||
w = 0;
|
||||
@@ -68,7 +68,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
mp_exch(&q, c);
|
||||
}
|
||||
mp_clear(&q);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@@ -79,13 +79,13 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
q.used = a->used;
|
||||
q.sign = a->sign;
|
||||
w = 0;
|
||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
||||
|
||||
|
||||
if (w >= b) {
|
||||
t = (mp_digit)(w / b);
|
||||
w -= ((mp_word)t) * ((mp_word)b);
|
||||
@@ -94,17 +94,17 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
||||
}
|
||||
q.dp[ix] = (mp_digit)t;
|
||||
}
|
||||
|
||||
|
||||
if (d != NULL) {
|
||||
*d = (mp_digit)w;
|
||||
}
|
||||
|
||||
|
||||
if (c != NULL) {
|
||||
mp_clamp(&q);
|
||||
mp_exch(&q, c);
|
||||
}
|
||||
mp_clear(&q);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@@ -21,7 +21,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
|
||||
/* the casts are required if DIGIT_BIT is one less than
|
||||
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
||||
*/
|
||||
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
|
||||
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
|
||||
((mp_word)a->dp[0]));
|
||||
}
|
||||
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* swap the elements of two integers, for cases where you can't simply swap the
|
||||
/* swap the elements of two integers, for cases where you can't simply swap the
|
||||
* mp_int pointers around
|
||||
*/
|
||||
void
|
||||
|
@@ -59,7 +59,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
err = mp_exptmod(&tmpG, &tmpX, P, Y);
|
||||
mp_clear_multi(&tmpG, &tmpX, NULL);
|
||||
return err;
|
||||
#else
|
||||
#else
|
||||
/* no invmod */
|
||||
return MP_VAL;
|
||||
#endif
|
||||
@@ -86,7 +86,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
dr = mp_reduce_is_2k(P) << 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* if the modulus is odd or dr != 0 use the montgomery method */
|
||||
#ifdef BN_MP_EXPTMOD_FAST_C
|
||||
if (mp_isodd (P) == 1 || dr != 0) {
|
||||
|
@@ -84,7 +84,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
|
||||
/* determine and setup reduction code */
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
/* now setup montgomery */
|
||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
@@ -99,7 +99,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if (((P->used * 2 + 1) < MP_WARRAY) &&
|
||||
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||
redux = fast_mp_montgomery_reduce;
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
||||
@@ -150,7 +150,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
#else
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto LBL_RES;
|
||||
#endif
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* Extended euclidean algorithm of (a, b) produces
|
||||
/* Extended euclidean algorithm of (a, b) produces
|
||||
a*u1 + b*u2 = u3
|
||||
*/
|
||||
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/* TomsFastMath, a fast ISO C bignum library.
|
||||
*
|
||||
*
|
||||
* This project is public domain and free for all purposes.
|
||||
*
|
||||
*
|
||||
* Love Hornquist Astrand <lha@h5l.org>
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
@@ -19,10 +19,10 @@
|
||||
int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
{
|
||||
int err, ch, neg, y;
|
||||
|
||||
|
||||
/* clear a */
|
||||
mp_zero(a);
|
||||
|
||||
|
||||
/* if first digit is - then set negative */
|
||||
ch = fgetc(stream);
|
||||
if (ch == '-') {
|
||||
@@ -31,7 +31,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
} else {
|
||||
neg = MP_ZPOS;
|
||||
}
|
||||
|
||||
|
||||
for (;;) {
|
||||
/* find y in the radix map */
|
||||
for (y = 0; y < radix; y++) {
|
||||
@@ -42,7 +42,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
if (y == radix) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
/* shift up and add */
|
||||
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
||||
return err;
|
||||
@@ -50,13 +50,13 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
ch = fgetc(stream);
|
||||
}
|
||||
if (mp_cmp_d(a, 0) != MP_EQ) {
|
||||
a->sign = neg;
|
||||
}
|
||||
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
|
@@ -19,7 +19,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
||||
{
|
||||
char *buf;
|
||||
int err, len, x;
|
||||
|
||||
|
||||
if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
@@ -28,19 +28,19 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
||||
if (buf == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
||||
|
||||
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
|
||||
XFREE (buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
for (x = 0; x < len; x++) {
|
||||
if (fputc(buf[x], stream) == EOF) {
|
||||
XFREE (buf);
|
||||
return MP_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
XFREE (buf);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
@@ -76,17 +76,17 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
/* swap u and v to make sure v is >= u */
|
||||
mp_exch(&u, &v);
|
||||
}
|
||||
|
||||
|
||||
/* subtract smallest from largest */
|
||||
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||
goto LBL_V;
|
||||
}
|
||||
|
||||
|
||||
/* Divide out all factors of two */
|
||||
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
||||
goto LBL_V;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* multiply by 2**k which we divided out at the beginning */
|
||||
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* get the lower 32-bits of an mp_int */
|
||||
unsigned long mp_get_int(mp_int * a)
|
||||
unsigned long mp_get_int(mp_int * a)
|
||||
{
|
||||
int i;
|
||||
unsigned long res;
|
||||
@@ -30,7 +30,7 @@ unsigned long mp_get_int(mp_int * a)
|
||||
|
||||
/* get most significant digit of result */
|
||||
res = DIGIT(a,i);
|
||||
|
||||
|
||||
while (--i >= 0) {
|
||||
res = (res << DIGIT_BIT) | DIGIT(a,i);
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
|
||||
int mp_init_multi(mp_int *mp, ...)
|
||||
int mp_init_multi(mp_int *mp, ...)
|
||||
{
|
||||
mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
|
||||
int n = 0; /* Number of ok inits */
|
||||
@@ -30,11 +30,11 @@ int mp_init_multi(mp_int *mp, ...)
|
||||
succeeded in init-ing, then return error.
|
||||
*/
|
||||
va_list clean_args;
|
||||
|
||||
|
||||
/* end the current list */
|
||||
va_end(args);
|
||||
|
||||
/* now start cleaning up */
|
||||
|
||||
/* now start cleaning up */
|
||||
cur_arg = mp;
|
||||
va_start(clean_args, mp);
|
||||
while (n--) {
|
||||
|
@@ -21,8 +21,8 @@ int mp_init_size (mp_int * a, int size)
|
||||
int x;
|
||||
|
||||
/* pad size so there are always extra digits */
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
/* alloc mem */
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
|
||||
if (a->dp == NULL) {
|
||||
|
@@ -27,7 +27,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
|
||||
/* init temps */
|
||||
if ((res = mp_init_multi(&x, &y, &u, &v,
|
||||
if ((res = mp_init_multi(&x, &y, &u, &v,
|
||||
&A, &B, &C, &D, NULL)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
@@ -154,14 +154,14 @@ top:
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* too big */
|
||||
while (mp_cmp_mag(&C, b) != MP_LT) {
|
||||
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* C is now the inverse */
|
||||
mp_exch (&C, c);
|
||||
res = MP_OKAY;
|
||||
|
@@ -38,7 +38,7 @@ static const char rem_105[105] = {
|
||||
};
|
||||
|
||||
/* Store non-zero to ret if arg is square, and zero if not */
|
||||
int mp_is_square(mp_int *arg,int *ret)
|
||||
int mp_is_square(mp_int *arg,int *ret)
|
||||
{
|
||||
int res;
|
||||
mp_digit c;
|
||||
@@ -46,7 +46,7 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
unsigned long r;
|
||||
|
||||
/* Default to Non-square :) */
|
||||
*ret = MP_NO;
|
||||
*ret = MP_NO;
|
||||
|
||||
if (arg->sign == MP_NEG) {
|
||||
return MP_VAL;
|
||||
@@ -80,8 +80,8 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
r = mp_get_int(&t);
|
||||
/* Check for other prime modules, note it's not an ERROR but we must
|
||||
* free "t" so the easiest way is to goto ERR. We know that res
|
||||
* is already equal to MP_OKAY from the mp_mod call
|
||||
*/
|
||||
* is already equal to MP_OKAY from the mp_mod call
|
||||
*/
|
||||
if ( (1L<<(r%11)) & 0x5C4L ) goto ERR;
|
||||
if ( (1L<<(r%13)) & 0x9E4L ) goto ERR;
|
||||
if ( (1L<<(r%17)) & 0x5CE8L ) goto ERR;
|
||||
|
@@ -1,10 +1,10 @@
|
||||
/* TomsFastMath, a fast ISO C bignum library.
|
||||
*
|
||||
*
|
||||
* This project is meant to fill in where LibTomMath
|
||||
* falls short. That is speed ;-)
|
||||
*
|
||||
* This project is public domain and free for all purposes.
|
||||
*
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com
|
||||
*/
|
||||
#include <tommath.h>
|
||||
|
@@ -15,33 +15,33 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||
* three half size multiplications
|
||||
*
|
||||
* Let B represent the radix [e.g. 2**DIGIT_BIT] and
|
||||
* let n represent half of the number of digits in
|
||||
* Let B represent the radix [e.g. 2**DIGIT_BIT] and
|
||||
* let n represent half of the number of digits in
|
||||
* the min(a,b)
|
||||
*
|
||||
* a = a1 * B**n + a0
|
||||
* b = b1 * B**n + b0
|
||||
*
|
||||
* Then, a * b =>
|
||||
* Then, a * b =>
|
||||
a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
|
||||
*
|
||||
* Note that a1b1 and a0b0 are used twice and only need to be
|
||||
* computed once. So in total three half size (half # of
|
||||
* digit) multiplications are performed, a0b0, a1b1 and
|
||||
* Note that a1b1 and a0b0 are used twice and only need to be
|
||||
* computed once. So in total three half size (half # of
|
||||
* digit) multiplications are performed, a0b0, a1b1 and
|
||||
* (a1+b1)(a0+b0)
|
||||
*
|
||||
* Note that a multiplication of half the digits requires
|
||||
* 1/4th the number of single precision multiplications so in
|
||||
* total after one call 25% of the single precision multiplications
|
||||
* are saved. Note also that the call to mp_mul can end up back
|
||||
* in this function if the a0, a1, b0, or b1 are above the threshold.
|
||||
* This is known as divide-and-conquer and leads to the famous
|
||||
* O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
|
||||
* the standard O(N**2) that the baseline/comba methods use.
|
||||
* Generally though the overhead of this method doesn't pay off
|
||||
* 1/4th the number of single precision multiplications so in
|
||||
* total after one call 25% of the single precision multiplications
|
||||
* are saved. Note also that the call to mp_mul can end up back
|
||||
* in this function if the a0, a1, b0, or b1 are above the threshold.
|
||||
* This is known as divide-and-conquer and leads to the famous
|
||||
* O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
|
||||
* the standard O(N**2) that the baseline/comba methods use.
|
||||
* Generally though the overhead of this method doesn't pay off
|
||||
* until a certain size (N ~ 80) is reached.
|
||||
*/
|
||||
int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
@@ -109,7 +109,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
}
|
||||
|
||||
/* only need to clamp the lower words since by definition the
|
||||
/* only need to clamp the lower words since by definition the
|
||||
* upper words x1/y1 must have a known number of digits
|
||||
*/
|
||||
mp_clamp (&x0);
|
||||
@@ -117,7 +117,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
/* now calc the products x0y0 and x1y1 */
|
||||
/* after this x0 is no longer required, free temp [x0==t2]! */
|
||||
if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
|
||||
if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
|
||||
goto X1Y1; /* x0y0 = x0*y0 */
|
||||
if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
|
||||
goto X1Y1; /* x1y1 = x1*y1 */
|
||||
|
@@ -15,11 +15,11 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* Karatsuba squaring, computes b = a*a using three
|
||||
/* Karatsuba squaring, computes b = a*a using three
|
||||
* half size squarings
|
||||
*
|
||||
* See comments of karatsuba_mul for details. It
|
||||
* is essentially the same algorithm but merely
|
||||
* See comments of karatsuba_mul for details. It
|
||||
* is essentially the same algorithm but merely
|
||||
* tuned to perform recursive squarings.
|
||||
*/
|
||||
int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
||||
|
@@ -25,29 +25,29 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
#ifdef BN_MP_TOOM_MUL_C
|
||||
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||
res = mp_toom_mul(a, b, c);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||
/* use Karatsuba? */
|
||||
if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||
res = mp_karatsuba_mul (a, b, c);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
/* can we use the fast multiplier?
|
||||
*
|
||||
* The fast multiplier can be used if the output will
|
||||
* have less than MP_WARRAY digits and the number of
|
||||
* The fast multiplier can be used if the output will
|
||||
* have less than MP_WARRAY digits and the number of
|
||||
* digits won't affect carry propagation
|
||||
*/
|
||||
int digs = a->used + b->used + 1;
|
||||
|
||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||
if ((digs < MP_WARRAY) &&
|
||||
MIN(a->used, b->used) <=
|
||||
MIN(a->used, b->used) <=
|
||||
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||
res = fast_s_mp_mul_digs (a, b, c, digs);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_S_MP_MUL_DIGS_C
|
||||
res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
|
||||
|
@@ -35,24 +35,24 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
||||
|
||||
/* alias for source */
|
||||
tmpa = a->dp;
|
||||
|
||||
|
||||
/* alias for dest */
|
||||
tmpb = b->dp;
|
||||
|
||||
/* carry */
|
||||
r = 0;
|
||||
for (x = 0; x < a->used; x++) {
|
||||
|
||||
/* get what will be the *next* carry bit from the
|
||||
* MSB of the current digit
|
||||
|
||||
/* get what will be the *next* carry bit from the
|
||||
* MSB of the current digit
|
||||
*/
|
||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
||||
|
||||
|
||||
/* now shift up this digit, add in the carry [from the previous] */
|
||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
||||
|
||||
/* copy the carry that would be from the source
|
||||
* digit into the next iteration
|
||||
|
||||
/* copy the carry that would be from the source
|
||||
* digit into the next iteration
|
||||
*/
|
||||
r = rr;
|
||||
}
|
||||
@@ -64,8 +64,8 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
||||
++(b->used);
|
||||
}
|
||||
|
||||
/* now zero any excess digits on the destination
|
||||
* that we didn't write to
|
||||
/* now zero any excess digits on the destination
|
||||
* that we didn't write to
|
||||
*/
|
||||
tmpb = b->dp + b->used;
|
||||
for (x = b->used; x < oldused; x++) {
|
||||
|
@@ -69,7 +69,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
||||
/* set the carry to the carry bits of the current word */
|
||||
r = rr;
|
||||
}
|
||||
|
||||
|
||||
/* set final carry */
|
||||
if (r != 0) {
|
||||
c->dp[(c->used)++] = r;
|
||||
|
@@ -15,14 +15,14 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* find the n'th root of an integer
|
||||
/* find the n'th root of an integer
|
||||
*
|
||||
* Result found such that (c)**b <= a and (c+1)**b > a
|
||||
* Result found such that (c)**b <= a and (c+1)**b > a
|
||||
*
|
||||
* This algorithm uses Newton's approximation
|
||||
* x[i+1] = x[i] - f(x[i])/f'(x[i])
|
||||
* which will find the root in log(N) time where
|
||||
* each step involves a fair bit. This is not meant to
|
||||
* This algorithm uses Newton's approximation
|
||||
* x[i+1] = x[i] - f(x[i])/f'(x[i])
|
||||
* which will find the root in log(N) time where
|
||||
* each step involves a fair bit. This is not meant to
|
||||
* find huge roots [square and cube, etc].
|
||||
*/
|
||||
int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
||||
@@ -61,31 +61,31 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
||||
}
|
||||
|
||||
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||
|
||||
|
||||
/* t3 = t1**(b-1) */
|
||||
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
|
||||
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* numerator */
|
||||
/* t2 = t1**b */
|
||||
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
||||
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* t2 = t1**b - a */
|
||||
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
||||
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* denominator */
|
||||
/* t3 = t1**(b-1) * b */
|
||||
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
||||
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
||||
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
||||
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* performs one Fermat test.
|
||||
*
|
||||
*
|
||||
* If "a" were prime then b**a == b (mod a) since the order of
|
||||
* the multiplicative sub-group would be phi(a) = a-1. That means
|
||||
* it would be the same as b**(a mod (a-1)) == b**1 == b (mod a).
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* determines if an integers is divisible by one
|
||||
/* determines if an integers is divisible by one
|
||||
* of the first PRIME_SIZE primes or not
|
||||
*
|
||||
* sets result to 0 if not, 1 if yes
|
||||
|
@@ -15,11 +15,11 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* Miller-Rabin test of "a" to the base of "b" as described in
|
||||
/* Miller-Rabin test of "a" to the base of "b" as described in
|
||||
* HAC pp. 139 Algorithm 4.24
|
||||
*
|
||||
* Sets result to 0 if definitely composite or 1 if probably prime.
|
||||
* Randomly the chance of error is no more than 1/4 and often
|
||||
* Randomly the chance of error is no more than 1/4 and often
|
||||
* very much lower.
|
||||
*/
|
||||
int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
@@ -33,7 +33,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
/* ensure b > 1 */
|
||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
||||
return MP_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* get n1 = a - 1 */
|
||||
if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
|
||||
|
@@ -18,7 +18,7 @@
|
||||
/* makes a truly random prime of a given size (bits),
|
||||
*
|
||||
* Flags are as follows:
|
||||
*
|
||||
*
|
||||
* LTM_PRIME_BBS - make prime congruent to 3 mod 4
|
||||
* LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
|
||||
* LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
|
||||
@@ -63,7 +63,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
|
||||
if (flags & LTM_PRIME_2MSB_ON) {
|
||||
maskOR_msb |= 0x80 >> ((9 - size) & 7);
|
||||
}
|
||||
}
|
||||
|
||||
/* get the maskOR_lsb */
|
||||
maskOR_lsb = 1;
|
||||
@@ -77,7 +77,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
err = MP_VAL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
||||
/* work over the MSbyte */
|
||||
tmp[0] &= maskAND;
|
||||
tmp[0] |= 1 << ((size - 1) & 7);
|
||||
@@ -91,7 +91,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
|
||||
/* is it prime? */
|
||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
||||
if (res == MP_NO) {
|
||||
if (res == MP_NO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
/* see if (a-1)/2 is prime */
|
||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; }
|
||||
if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; }
|
||||
|
||||
|
||||
/* is it prime? */
|
||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
||||
}
|
||||
|
@@ -54,7 +54,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
|
||||
}
|
||||
|
||||
/* force temp to positive */
|
||||
t.sign = MP_ZPOS;
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
/* fetch out all of the digits */
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
|
@@ -29,8 +29,8 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* if the leading digit is a
|
||||
* minus set the sign to negative.
|
||||
/* if the leading digit is a
|
||||
* minus set the sign to negative.
|
||||
*/
|
||||
if (*str == '-') {
|
||||
++str;
|
||||
@@ -41,7 +41,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
|
||||
/* set the integer to the default of zero */
|
||||
mp_zero (a);
|
||||
|
||||
|
||||
/* process each digit of the string */
|
||||
while (*str) {
|
||||
/* if the radix < 36 the conversion is case insensitive
|
||||
@@ -55,9 +55,9 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
}
|
||||
}
|
||||
|
||||
/* if the char was found in the map
|
||||
/* if the char was found in the map
|
||||
* and is less than the given radix add it
|
||||
* to the number, otherwise exit the loop.
|
||||
* to the number, otherwise exit the loop.
|
||||
*/
|
||||
if (y < radix) {
|
||||
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
|
||||
@@ -71,7 +71,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
}
|
||||
++str;
|
||||
}
|
||||
|
||||
|
||||
/* set the sign only if a != 0 */
|
||||
if (mp_iszero(a) != 1) {
|
||||
a->sign = neg;
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||
* precomputed via mp_reduce_setup.
|
||||
* From HAC pp.604 Algorithm 14.42
|
||||
*/
|
||||
@@ -30,7 +30,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
}
|
||||
|
||||
/* q1 = x / b**(k-1) */
|
||||
mp_rshd (&q, um - 1);
|
||||
mp_rshd (&q, um - 1);
|
||||
|
||||
/* according to HAC this optimization is ok */
|
||||
if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
||||
@@ -46,8 +46,8 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
#else
|
||||
{
|
||||
#else
|
||||
{
|
||||
res = MP_VAL;
|
||||
goto CLEANUP;
|
||||
}
|
||||
@@ -55,7 +55,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
}
|
||||
|
||||
/* q3 = q2 / b**(k+1) */
|
||||
mp_rshd (&q, um + 1);
|
||||
mp_rshd (&q, um + 1);
|
||||
|
||||
/* x = x mod b**(k+1), quick (no division) */
|
||||
if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
|
||||
@@ -87,7 +87,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
goto CLEANUP;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CLEANUP:
|
||||
mp_clear (&q);
|
||||
|
||||
|
@@ -20,35 +20,35 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
|
||||
{
|
||||
mp_int q;
|
||||
int p, res;
|
||||
|
||||
|
||||
if ((res = mp_init(&q)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
p = mp_count_bits(n);
|
||||
|
||||
p = mp_count_bits(n);
|
||||
top:
|
||||
/* q = a/2**p, a = a mod 2**p */
|
||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (d != 1) {
|
||||
/* q = q * d */
|
||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* a = a + q */
|
||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
goto top;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&q);
|
||||
return res;
|
||||
|
@@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* reduces a modulo n where n is of the form 2**p - d
|
||||
/* reduces a modulo n where n is of the form 2**p - d
|
||||
This differs from reduce_2k since "d" can be larger
|
||||
than a single digit.
|
||||
*/
|
||||
@@ -23,33 +23,33 @@ int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
|
||||
{
|
||||
mp_int q;
|
||||
int p, res;
|
||||
|
||||
|
||||
if ((res = mp_init(&q)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
p = mp_count_bits(n);
|
||||
|
||||
p = mp_count_bits(n);
|
||||
top:
|
||||
/* q = a/2**p, a = a mod 2**p */
|
||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* q = q * d */
|
||||
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
|
||||
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* a = a + q */
|
||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
goto top;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&q);
|
||||
return res;
|
||||
|
@@ -20,22 +20,22 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
|
||||
{
|
||||
int res, p;
|
||||
mp_int tmp;
|
||||
|
||||
|
||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
p = mp_count_bits(a);
|
||||
if ((res = mp_2expt(&tmp, p)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
*d = tmp.dp[0];
|
||||
mp_clear(&tmp);
|
||||
return MP_OKAY;
|
||||
|
@@ -20,19 +20,19 @@ int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
|
||||
{
|
||||
int res;
|
||||
mp_int tmp;
|
||||
|
||||
|
||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
|
@@ -20,7 +20,7 @@ int mp_reduce_is_2k(mp_int *a)
|
||||
{
|
||||
int ix, iy, iw;
|
||||
mp_digit iz;
|
||||
|
||||
|
||||
if (a->used == 0) {
|
||||
return MP_NO;
|
||||
} else if (a->used == 1) {
|
||||
@@ -29,7 +29,7 @@ int mp_reduce_is_2k(mp_int *a)
|
||||
iy = mp_count_bits(a);
|
||||
iz = 1;
|
||||
iw = 1;
|
||||
|
||||
|
||||
/* Test every bit from the second digit up, must be 1 */
|
||||
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
||||
if ((a->dp[iw] & iz) == 0) {
|
||||
|
@@ -19,7 +19,7 @@
|
||||
int mp_reduce_is_2k_l(mp_int *a)
|
||||
{
|
||||
int ix, iy;
|
||||
|
||||
|
||||
if (a->used == 0) {
|
||||
return MP_NO;
|
||||
} else if (a->used == 1) {
|
||||
@@ -32,7 +32,7 @@ int mp_reduce_is_2k_l(mp_int *a)
|
||||
}
|
||||
}
|
||||
return (iy >= (a->used/2)) ? MP_YES : MP_NO;
|
||||
|
||||
|
||||
}
|
||||
return MP_NO;
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@
|
||||
int mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
|
||||
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
@@ -42,8 +42,8 @@ void mp_rshd (mp_int * a, int b)
|
||||
/* top [offset into digits] */
|
||||
top = a->dp + b;
|
||||
|
||||
/* this is implemented as a sliding window where
|
||||
* the window is b-digits long and digits from
|
||||
/* this is implemented as a sliding window where
|
||||
* the window is b-digits long and digits from
|
||||
* the top of the window are copied to the bottom
|
||||
*
|
||||
* e.g.
|
||||
@@ -61,7 +61,7 @@ void mp_rshd (mp_int * a, int b)
|
||||
*bottom++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* remove excess digits */
|
||||
a->used -= b;
|
||||
}
|
||||
|
@@ -21,7 +21,7 @@ int mp_set_int (mp_int * a, unsigned long b)
|
||||
int x, res;
|
||||
|
||||
mp_zero (a);
|
||||
|
||||
|
||||
/* set four bits at a time */
|
||||
for (x = 0; x < 8; x++) {
|
||||
/* shift the number up four bits */
|
||||
|
@@ -26,18 +26,18 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
if (a->used >= TOOM_SQR_CUTOFF) {
|
||||
res = mp_toom_sqr(a, b);
|
||||
/* Karatsuba? */
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_SQR_C
|
||||
if (a->used >= KARATSUBA_SQR_CUTOFF) {
|
||||
res = mp_karatsuba_sqr (a, b);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_FAST_S_MP_SQR_C
|
||||
/* can we use the fast comba multiplier? */
|
||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||
a->used <
|
||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||
a->used <
|
||||
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
|
||||
res = fast_s_mp_sqr (a, b);
|
||||
} else
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* this function is less generic than mp_n_root, simpler and faster */
|
||||
int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
{
|
||||
int res;
|
||||
mp_int t1,t2;
|
||||
@@ -43,7 +43,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
/* First approx. (not very bad for large arg) */
|
||||
mp_rshd (&t1,t1.used/2);
|
||||
|
||||
/* t1 > 0 */
|
||||
/* t1 > 0 */
|
||||
if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
|
||||
goto E1;
|
||||
}
|
||||
@@ -54,7 +54,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
goto E1;
|
||||
}
|
||||
/* And now t1 > sqrt(arg) */
|
||||
do {
|
||||
do {
|
||||
if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
|
||||
goto E1;
|
||||
}
|
||||
|
@@ -15,28 +15,28 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* multiplication using the Toom-Cook 3-way algorithm
|
||||
/* multiplication using the Toom-Cook 3-way algorithm
|
||||
*
|
||||
* Much more complicated than Karatsuba but has a lower
|
||||
* asymptotic running time of O(N**1.464). This algorithm is
|
||||
* only particularly useful on VERY large inputs
|
||||
* Much more complicated than Karatsuba but has a lower
|
||||
* asymptotic running time of O(N**1.464). This algorithm is
|
||||
* only particularly useful on VERY large inputs
|
||||
* (we're talking 1000s of digits here...).
|
||||
*/
|
||||
int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
{
|
||||
mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
|
||||
int res, B;
|
||||
|
||||
|
||||
/* init temps */
|
||||
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
|
||||
&a0, &a1, &a2, &b0, &b1,
|
||||
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
|
||||
&a0, &a1, &a2, &b0, &b1,
|
||||
&b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* B */
|
||||
B = MIN(a->used, b->used) / 3;
|
||||
|
||||
|
||||
/* a = a2 * B**2 + a1 * B + a0 */
|
||||
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -52,7 +52,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
goto ERR;
|
||||
}
|
||||
mp_rshd(&a2, B*2);
|
||||
|
||||
|
||||
/* b = b2 * B**2 + b1 * B + b0 */
|
||||
if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -68,17 +68,17 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
goto ERR;
|
||||
}
|
||||
mp_rshd(&b2, B*2);
|
||||
|
||||
|
||||
/* w0 = a0*b0 */
|
||||
if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* w4 = a2 * b2 */
|
||||
if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
|
||||
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -92,7 +92,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
@@ -105,11 +105,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
|
||||
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -123,7 +123,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
@@ -136,11 +136,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
|
||||
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
|
||||
@@ -158,19 +158,19 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
/* now solve the matrix
|
||||
|
||||
|
||||
/* now solve the matrix
|
||||
|
||||
0 0 0 0 1
|
||||
1 2 4 8 16
|
||||
1 1 1 1 1
|
||||
16 8 4 2 1
|
||||
1 0 0 0 0
|
||||
|
||||
using 12 subtractions, 4 shifts,
|
||||
2 small divisions and 1 small multiplication
|
||||
|
||||
using 12 subtractions, 4 shifts,
|
||||
2 small divisions and 1 small multiplication
|
||||
*/
|
||||
|
||||
|
||||
/* r1 - r4 */
|
||||
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -242,7 +242,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* at this point shift W[n] by B*n */
|
||||
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
@@ -255,8 +255,8 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
}
|
||||
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
@@ -268,15 +268,15 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
}
|
||||
if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ERR:
|
||||
mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
|
||||
&a0, &a1, &a2, &b0, &b1,
|
||||
mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
|
||||
&a0, &a1, &a2, &b0, &b1,
|
||||
&b2, &tmp1, &tmp2, NULL);
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/bn_mp_toom_mul.c,v $ */
|
||||
|
@@ -15,9 +15,9 @@
|
||||
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* stores a bignum as a ASCII string in a given radix (2..64)
|
||||
/* stores a bignum as a ASCII string in a given radix (2..64)
|
||||
*
|
||||
* Stores upto maxlen-1 chars and always a NULL byte
|
||||
* Stores upto maxlen-1 chars and always a NULL byte
|
||||
*/
|
||||
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
{
|
||||
@@ -50,7 +50,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
/* store the flag and mark the number as positive */
|
||||
*str++ = '-';
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
|
||||
/* subtract a char */
|
||||
--maxlen;
|
||||
}
|
||||
|
@@ -74,8 +74,8 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
*tmpc++ &= MP_MASK;
|
||||
}
|
||||
|
||||
/* now copy higher words if any, that is in A+B
|
||||
* if A or B has more digits add those in
|
||||
/* now copy higher words if any, that is in A+B
|
||||
* if A or B has more digits add those in
|
||||
*/
|
||||
if (min != max) {
|
||||
for (; i < max; i++) {
|
||||
|
@@ -54,7 +54,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
/* init M array */
|
||||
/* init first cell */
|
||||
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
||||
return err;
|
||||
return err;
|
||||
}
|
||||
|
||||
/* now init the second half of the array */
|
||||
@@ -72,7 +72,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
if ((err = mp_init (&mu)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
|
||||
|
||||
if (redmode == 0) {
|
||||
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
@@ -83,22 +83,22 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
goto LBL_MU;
|
||||
}
|
||||
redux = mp_reduce_2k_l;
|
||||
}
|
||||
}
|
||||
|
||||
/* create M table
|
||||
*
|
||||
* The M table contains powers of the base,
|
||||
* The M table contains powers of the base,
|
||||
* e.g. M[x] = G**x mod P
|
||||
*
|
||||
* The first half of the table is not
|
||||
* The first half of the table is not
|
||||
* computed though accept for M[0] and M[1]
|
||||
*/
|
||||
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||
* M[1] (winsize-1) times
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||
* M[1] (winsize-1) times
|
||||
*/
|
||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
@@ -106,7 +106,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
|
||||
for (x = 0; x < (winsize - 1); x++) {
|
||||
/* square it */
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
@@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* multiplies |a| * |b| and only computes upto digs digits of result
|
||||
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
||||
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
||||
* many digits of output are created.
|
||||
*/
|
||||
int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
@@ -29,7 +29,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
/* can we use the fast multiplier? */
|
||||
if (((digs) < MP_WARRAY) &&
|
||||
MIN (a->used, b->used) <
|
||||
MIN (a->used, b->used) <
|
||||
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||
return fast_s_mp_mul_digs (a, b, c, digs);
|
||||
}
|
||||
@@ -51,10 +51,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* setup some aliases */
|
||||
/* copy of the digit from a used within the nested loop */
|
||||
tmpx = a->dp[ix];
|
||||
|
||||
|
||||
/* an alias for the destination shifted ix places */
|
||||
tmpt = t.dp + ix;
|
||||
|
||||
|
||||
/* an alias for the digits of b */
|
||||
tmpy = b->dp;
|
||||
|
||||
|
@@ -48,7 +48,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
/* alias for where to store the results */
|
||||
tmpt = t.dp + (2*ix + 1);
|
||||
|
||||
|
||||
for (iy = ix + 1; iy < pa; iy++) {
|
||||
/* first calculate the product */
|
||||
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
|
||||
|
@@ -21,14 +21,14 @@
|
||||
-------------------------------------------------------------
|
||||
Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
|
||||
AMD Athlon64 /GCC v3.4.4 / 80/ 120/LTM 0.35
|
||||
|
||||
|
||||
*/
|
||||
|
||||
int KARATSUBA_MUL_CUTOFF = 80, /* Min. number of digits before Karatsuba multiplication is used. */
|
||||
KARATSUBA_SQR_CUTOFF = 120, /* Min. number of digits before Karatsuba squaring is used. */
|
||||
|
||||
|
||||
TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
|
||||
TOOM_SQR_CUTOFF = 400;
|
||||
TOOM_SQR_CUTOFF = 400;
|
||||
#endif
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/bncore.c,v $ */
|
||||
|
@@ -69,7 +69,7 @@ int main(void)
|
||||
srand(time(NULL));
|
||||
|
||||
#if 0
|
||||
// test montgomery
|
||||
// test montgomery
|
||||
printf("Testing montgomery...\n");
|
||||
for (i = 1; i < 10; i++) {
|
||||
printf("Testing digit size: %d\n", i);
|
||||
@@ -81,7 +81,7 @@ int main(void)
|
||||
mp_montgomery_calc_normalization(&b, &a);
|
||||
mp_montgomery_setup(&a, &mp);
|
||||
|
||||
// now test a random reduction
|
||||
// now test a random reduction
|
||||
for (ix = 0; ix < 100; ix++) {
|
||||
mp_rand(&c, 1 + abs(rand()) % (2*i));
|
||||
mp_copy(&c, &d);
|
||||
@@ -91,7 +91,7 @@ int main(void)
|
||||
mp_montgomery_reduce(&c, &a, mp);
|
||||
mp_mulmod(&c, &b, &a, &c);
|
||||
|
||||
if (mp_cmp(&c, &d) != MP_EQ) {
|
||||
if (mp_cmp(&c, &d) != MP_EQ) {
|
||||
printf("d = e mod a, c = e MOD a\n");
|
||||
mp_todecimal(&a, buf); printf("a = %s\n", buf);
|
||||
mp_todecimal(&e, buf); printf("e = %s\n", buf);
|
||||
|
@@ -12,16 +12,16 @@ int main(void)
|
||||
FILE *out;
|
||||
clock_t t1;
|
||||
mp_digit z;
|
||||
|
||||
|
||||
mp_init_multi(&q, &p, NULL);
|
||||
|
||||
|
||||
out = fopen("2kprime.1", "w");
|
||||
for (x = 0; x < (int)(sizeof(sizes) / sizeof(sizes[0])); x++) {
|
||||
top:
|
||||
mp_2expt(&q, sizes[x]);
|
||||
mp_add_d(&q, 3, &q);
|
||||
z = -3;
|
||||
|
||||
|
||||
t1 = clock();
|
||||
for(;;) {
|
||||
mp_sub_d(&q, 4, &q);
|
||||
@@ -31,13 +31,13 @@ int main(void)
|
||||
printf("No primes of size %d found\n", sizes[x]);
|
||||
break;
|
||||
}
|
||||
|
||||
if (clock() - t1 > CLOCKS_PER_SEC) {
|
||||
|
||||
if (clock() - t1 > CLOCKS_PER_SEC) {
|
||||
printf("."); fflush(stdout);
|
||||
// sleep((clock() - t1 + CLOCKS_PER_SEC/2)/CLOCKS_PER_SEC);
|
||||
t1 = clock();
|
||||
}
|
||||
|
||||
|
||||
/* quick test on q */
|
||||
mp_prime_is_prime(&q, 1, &y);
|
||||
if (y == 0) {
|
||||
@@ -60,24 +60,24 @@ int main(void)
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (y == 0) {
|
||||
++sizes[x];
|
||||
goto top;
|
||||
}
|
||||
|
||||
|
||||
mp_toradix(&q, buf, 10);
|
||||
printf("\n\n%d-bits (k = %lu) = %s\n", sizes[x], z, buf);
|
||||
fprintf(out, "%d-bits (k = %lu) = %s\n", sizes[x], z, buf); fflush(out);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/etc/2kprime.c,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
|
@@ -8,10 +8,10 @@ int main(void)
|
||||
char buf[4096];
|
||||
FILE *out;
|
||||
mp_int a, b;
|
||||
|
||||
|
||||
mp_init(&a);
|
||||
mp_init(&b);
|
||||
|
||||
|
||||
out = fopen("drprimes.txt", "w");
|
||||
for (x = 0; x < (int)(sizeof(sizes)/sizeof(sizes[0])); x++) {
|
||||
top:
|
||||
@@ -21,14 +21,14 @@ int main(void)
|
||||
for (y = 1; y < sizes[x]; y++) {
|
||||
a.dp[y] = MP_MASK;
|
||||
}
|
||||
|
||||
|
||||
/* make a DR modulus */
|
||||
a.dp[0] = -1;
|
||||
a.used = sizes[x];
|
||||
|
||||
|
||||
/* now loop */
|
||||
res = 0;
|
||||
for (;;) {
|
||||
for (;;) {
|
||||
a.dp[0] += 4;
|
||||
if (a.dp[0] >= MP_MASK) break;
|
||||
mp_prime_is_prime(&a, 1, &res);
|
||||
@@ -36,25 +36,25 @@ int main(void)
|
||||
printf("."); fflush(stdout);
|
||||
mp_sub_d(&a, 1, &b);
|
||||
mp_div_2(&b, &b);
|
||||
mp_prime_is_prime(&b, 3, &res);
|
||||
mp_prime_is_prime(&b, 3, &res);
|
||||
if (res == 0) continue;
|
||||
mp_prime_is_prime(&a, 3, &res);
|
||||
if (res == 1) break;
|
||||
}
|
||||
|
||||
|
||||
if (res != 1) {
|
||||
printf("Error not DR modulus\n"); sizes[x] += 1; goto top;
|
||||
} else {
|
||||
mp_toradix(&a, buf, 10);
|
||||
printf("\n\np == %s\n\n", buf);
|
||||
fprintf(out, "%d-bit prime:\np == %s\n\n", mp_count_bits(&a), buf); fflush(out);
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(out);
|
||||
|
||||
|
||||
mp_clear(&a);
|
||||
mp_clear(&b);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
/* Finds Mersenne primes using the Lucas-Lehmer test
|
||||
/* Finds Mersenne primes using the Lucas-Lehmer test
|
||||
*
|
||||
* Tom St Denis, tomstdenis@gmail.com
|
||||
*/
|
||||
@@ -10,7 +10,7 @@ is_mersenne (long s, int *pp)
|
||||
{
|
||||
mp_int n, u;
|
||||
int res, k;
|
||||
|
||||
|
||||
*pp = 0;
|
||||
|
||||
if ((res = mp_init (&n)) != MP_OKAY) {
|
||||
|
@@ -13,21 +13,21 @@ int main(void)
|
||||
/* loop through various sizes */
|
||||
for (x = 4; x < 256; x++) {
|
||||
printf("DIGITS == %3ld...", x); fflush(stdout);
|
||||
|
||||
|
||||
/* make up the odd modulus */
|
||||
mp_rand(&modulus, x);
|
||||
modulus.dp[0] |= 1;
|
||||
|
||||
|
||||
/* now find the R value */
|
||||
mp_montgomery_calc_normalization(&R, &modulus);
|
||||
mp_montgomery_setup(&modulus, &mp);
|
||||
|
||||
|
||||
/* now run through a bunch tests */
|
||||
for (y = 0; y < 1000; y++) {
|
||||
mp_rand(&p, x/2); /* p = random */
|
||||
mp_mul(&p, &R, &pp); /* pp = R * p */
|
||||
mp_montgomery_reduce(&pp, &modulus, mp);
|
||||
|
||||
|
||||
/* should be equal to p */
|
||||
if (mp_cmp(&pp, &p) != MP_EQ) {
|
||||
printf("FAILURE!\n");
|
||||
@@ -36,7 +36,7 @@ int main(void)
|
||||
}
|
||||
printf("PASSED\n");
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,7 @@
|
||||
#include <time.h>
|
||||
|
||||
/* how many times todo each size mult. Depends on your computer. For slow computers
|
||||
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
|
||||
* this can be low like 5 or 10. For fast [re: Athlon] should be 25 - 50 or so
|
||||
*/
|
||||
#define TIMES (1UL<<14UL)
|
||||
|
||||
@@ -67,7 +67,7 @@ ulong64 time_mult(int size, int s)
|
||||
mp_rand (&a, size);
|
||||
mp_rand (&b, size);
|
||||
|
||||
if (s == 1) {
|
||||
if (s == 1) {
|
||||
KARATSUBA_MUL_CUTOFF = size;
|
||||
} else {
|
||||
KARATSUBA_MUL_CUTOFF = 100000;
|
||||
@@ -95,7 +95,7 @@ ulong64 time_sqr(int size, int s)
|
||||
|
||||
mp_rand (&a, size);
|
||||
|
||||
if (s == 1) {
|
||||
if (s == 1) {
|
||||
KARATSUBA_SQR_CUTOFF = size;
|
||||
} else {
|
||||
KARATSUBA_SQR_CUTOFF = 100000;
|
||||
@@ -117,7 +117,7 @@ main (void)
|
||||
ulong64 t1, t2;
|
||||
int x, y;
|
||||
|
||||
for (x = 8; ; x += 2) {
|
||||
for (x = 8; ; x += 2) {
|
||||
t1 = time_mult(x, 0);
|
||||
t2 = time_mult(x, 1);
|
||||
printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
|
||||
@@ -125,7 +125,7 @@ main (void)
|
||||
}
|
||||
y = x;
|
||||
|
||||
for (x = 8; ; x += 2) {
|
||||
for (x = 8; ; x += 2) {
|
||||
t1 = time_sqr(x, 0);
|
||||
t2 = time_sqr(x, 1);
|
||||
printf("%d: %9llu %9llu, %9llu\n", x, t1, t2, t2 - t1);
|
||||
|
@@ -5,7 +5,7 @@
|
||||
#define MPI_CONFIG_H_
|
||||
|
||||
/*
|
||||
For boolean options,
|
||||
For boolean options,
|
||||
0 = no
|
||||
1 = yes
|
||||
|
||||
|
@@ -22,7 +22,7 @@
|
||||
#define DIAG(T,V)
|
||||
#endif
|
||||
|
||||
/*
|
||||
/*
|
||||
If MP_LOGTAB is not defined, use the math library to compute the
|
||||
logarithms on the fly. Otherwise, use the static table below.
|
||||
Pick which works best for your system.
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
/*
|
||||
A table of the logs of 2 for various bases (the 0 and 1 entries of
|
||||
this table are meaningless and should not be referenced).
|
||||
this table are meaningless and should not be referenced).
|
||||
|
||||
This table is used to compute output lengths for the mp_toradix()
|
||||
function. Since a number n in radix r takes up about log_r(n)
|
||||
@@ -43,7 +43,7 @@
|
||||
log_r(n) = log_2(n) * log_r(2)
|
||||
|
||||
This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
|
||||
which are the output bases supported.
|
||||
which are the output bases supported.
|
||||
*/
|
||||
|
||||
#include "logtab.h"
|
||||
@@ -104,7 +104,7 @@ static const char *mp_err_string[] = {
|
||||
/* Value to digit maps for radix conversion */
|
||||
|
||||
/* s_dmap_1 - standard digits and letters */
|
||||
static const char *s_dmap_1 =
|
||||
static const char *s_dmap_1 =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
||||
|
||||
#if 0
|
||||
@@ -117,7 +117,7 @@ static const char *s_dmap_2 =
|
||||
|
||||
/* {{{ Static function declarations */
|
||||
|
||||
/*
|
||||
/*
|
||||
If MP_MACRO is false, these will be defined as actual functions;
|
||||
otherwise, suitable macro definitions will be used. This works
|
||||
around the fact that ANSI C89 doesn't support an 'inline' keyword
|
||||
@@ -258,7 +258,7 @@ mp_err mp_init_array(mp_int mp[], int count)
|
||||
return MP_OKAY;
|
||||
|
||||
CLEANUP:
|
||||
while(--pos >= 0)
|
||||
while(--pos >= 0)
|
||||
mp_clear(&mp[pos]);
|
||||
|
||||
return res;
|
||||
@@ -355,7 +355,7 @@ mp_err mp_copy(mp_int *from, mp_int *to)
|
||||
if(ALLOC(to) >= USED(from)) {
|
||||
s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
|
||||
s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
|
||||
|
||||
|
||||
} else {
|
||||
if((tmp = s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL)
|
||||
return MP_MEM;
|
||||
@@ -445,7 +445,7 @@ void mp_clear_array(mp_int mp[], int count)
|
||||
{
|
||||
ARGCHK(mp != NULL && count > 0, MP_BADARG);
|
||||
|
||||
while(--count >= 0)
|
||||
while(--count >= 0)
|
||||
mp_clear(&mp[count]);
|
||||
|
||||
} /* end mp_clear_array() */
|
||||
@@ -455,7 +455,7 @@ void mp_clear_array(mp_int mp[], int count)
|
||||
/* {{{ mp_zero(mp) */
|
||||
|
||||
/*
|
||||
mp_zero(mp)
|
||||
mp_zero(mp)
|
||||
|
||||
Set mp to zero. Does not change the allocated size of the structure,
|
||||
and therefore cannot fail (except on a bad argument, which we ignore)
|
||||
@@ -506,7 +506,7 @@ mp_err mp_set_int(mp_int *mp, long z)
|
||||
if((res = s_mp_mul_2d(mp, CHAR_BIT)) != MP_OKAY)
|
||||
return res;
|
||||
|
||||
res = s_mp_add_d(mp,
|
||||
res = s_mp_add_d(mp,
|
||||
(mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
|
||||
if(res != MP_OKAY)
|
||||
return res;
|
||||
@@ -841,9 +841,9 @@ mp_err mp_neg(mp_int *a, mp_int *b)
|
||||
if((res = mp_copy(a, b)) != MP_OKAY)
|
||||
return res;
|
||||
|
||||
if(s_mp_cmp_d(b, 0) == MP_EQ)
|
||||
if(s_mp_cmp_d(b, 0) == MP_EQ)
|
||||
SIGN(b) = MP_ZPOS;
|
||||
else
|
||||
else
|
||||
SIGN(b) = (SIGN(b) == MP_NEG) ? MP_ZPOS : MP_NEG;
|
||||
|
||||
return MP_OKAY;
|
||||
@@ -870,7 +870,7 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
if(SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */
|
||||
|
||||
/* Commutativity of addition lets us do this in either order,
|
||||
so we avoid having to use a temporary even if the result
|
||||
so we avoid having to use a temporary even if the result
|
||||
is supposed to replace the output
|
||||
*/
|
||||
if(c == b) {
|
||||
@@ -880,14 +880,14 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
if(c != a && (res = mp_copy(a, c)) != MP_OKAY)
|
||||
return res;
|
||||
|
||||
if((res = s_mp_add(c, b)) != MP_OKAY)
|
||||
if((res = s_mp_add(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
}
|
||||
|
||||
} else if((cmp = s_mp_cmp(a, b)) > 0) { /* different sign: a > b */
|
||||
|
||||
/* If the output is going to be clobbered, we will use a temporary
|
||||
variable; otherwise, we'll do it without touching the memory
|
||||
variable; otherwise, we'll do it without touching the memory
|
||||
allocator at all, if possible
|
||||
*/
|
||||
if(c == b) {
|
||||
@@ -1019,7 +1019,7 @@ mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_clear(&tmp);
|
||||
|
||||
} else {
|
||||
if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
|
||||
if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
|
||||
return res;
|
||||
|
||||
if((res = s_mp_sub(c, a)) != MP_OKAY)
|
||||
@@ -1066,12 +1066,12 @@ mp_err mp_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||
if((res = s_mp_mul(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if(sgn == MP_ZPOS || s_mp_cmp_d(c, 0) == MP_EQ)
|
||||
SIGN(c) = MP_ZPOS;
|
||||
else
|
||||
SIGN(c) = sgn;
|
||||
|
||||
|
||||
return MP_OKAY;
|
||||
|
||||
} /* end mp_mul() */
|
||||
@@ -1160,7 +1160,7 @@ mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r)
|
||||
return res;
|
||||
}
|
||||
|
||||
if(q)
|
||||
if(q)
|
||||
mp_zero(q);
|
||||
|
||||
return MP_OKAY;
|
||||
@@ -1206,10 +1206,10 @@ mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r)
|
||||
SIGN(&rtmp) = MP_ZPOS;
|
||||
|
||||
/* Copy output, if it is needed */
|
||||
if(q)
|
||||
if(q)
|
||||
s_mp_exch(&qtmp, q);
|
||||
|
||||
if(r)
|
||||
if(r)
|
||||
s_mp_exch(&rtmp, r);
|
||||
|
||||
CLEANUP:
|
||||
@@ -1286,12 +1286,12 @@ mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
|
||||
/* Loop over bits of each non-maximal digit */
|
||||
for(bit = 0; bit < DIGIT_BIT; bit++) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
|
||||
|
||||
if((res = s_mp_sqr(&x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
@@ -1311,7 +1311,7 @@ mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
|
||||
if((res = s_mp_sqr(&x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
|
||||
if(mp_iseven(b))
|
||||
SIGN(&s) = SIGN(a);
|
||||
|
||||
@@ -1362,7 +1362,7 @@ mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
|
||||
|
||||
/*
|
||||
If |a| > m, we need to divide to get the remainder and take the
|
||||
absolute value.
|
||||
absolute value.
|
||||
|
||||
If |a| < m, we don't need to do any division, just copy and adjust
|
||||
the sign (if a is negative).
|
||||
@@ -1376,7 +1376,7 @@ mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
|
||||
if((mag = s_mp_cmp(a, m)) > 0) {
|
||||
if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
|
||||
return res;
|
||||
|
||||
|
||||
if(SIGN(c) == MP_NEG) {
|
||||
if((res = mp_add(c, m, c)) != MP_OKAY)
|
||||
return res;
|
||||
@@ -1391,7 +1391,7 @@ mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
mp_zero(c);
|
||||
|
||||
@@ -1464,9 +1464,9 @@ mp_err mp_sqrt(mp_int *a, mp_int *b)
|
||||
return MP_RANGE;
|
||||
|
||||
/* Special cases for zero and one, trivial */
|
||||
if(mp_cmp_d(a, 0) == MP_EQ || mp_cmp_d(a, 1) == MP_EQ)
|
||||
if(mp_cmp_d(a, 0) == MP_EQ || mp_cmp_d(a, 1) == MP_EQ)
|
||||
return mp_copy(a, b);
|
||||
|
||||
|
||||
/* Initialize the temporaries we'll use below */
|
||||
if((res = mp_init_size(&t, USED(a))) != MP_OKAY)
|
||||
return res;
|
||||
@@ -1508,7 +1508,7 @@ mp_add_d(&x, 1, &x);
|
||||
CLEANUP:
|
||||
mp_clear(&x);
|
||||
X:
|
||||
mp_clear(&t);
|
||||
mp_clear(&t);
|
||||
|
||||
return res;
|
||||
|
||||
@@ -1626,7 +1626,7 @@ mp_err mp_sqrmod(mp_int *a, mp_int *m, mp_int *c)
|
||||
Compute c = (a ** b) mod m. Uses a standard square-and-multiply
|
||||
method with modular reductions at each step. (This is basically the
|
||||
same code as mp_expt(), except for the addition of the reductions)
|
||||
|
||||
|
||||
The modular reductions are done using Barrett's algorithm (see
|
||||
s_mp_reduce() below for details)
|
||||
*/
|
||||
@@ -1655,7 +1655,7 @@ mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
|
||||
mp_set(&s, 1);
|
||||
|
||||
/* mu = b^2k / m */
|
||||
s_mp_add_d(&mu, 1);
|
||||
s_mp_add_d(&mu, 1);
|
||||
s_mp_lshd(&mu, 2 * USED(m));
|
||||
if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
@@ -1866,7 +1866,7 @@ int mp_cmp_int(mp_int *a, long z)
|
||||
int out;
|
||||
|
||||
ARGCHK(a != NULL, MP_EQ);
|
||||
|
||||
|
||||
mp_init(&tmp); mp_set_int(&tmp, z);
|
||||
out = mp_cmp(a, &tmp);
|
||||
mp_clear(&tmp);
|
||||
@@ -1953,13 +1953,13 @@ mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
|
||||
if(mp_isodd(&u)) {
|
||||
if((res = mp_copy(&v, &t)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
|
||||
|
||||
/* t = -v */
|
||||
if(SIGN(&v) == MP_ZPOS)
|
||||
SIGN(&t) = MP_NEG;
|
||||
else
|
||||
SIGN(&t) = MP_ZPOS;
|
||||
|
||||
|
||||
} else {
|
||||
if((res = mp_copy(&u, &t)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
@@ -2152,7 +2152,7 @@ mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y)
|
||||
|
||||
if(y)
|
||||
if((res = mp_copy(&D, y)) != MP_OKAY) goto CLEANUP;
|
||||
|
||||
|
||||
if(g)
|
||||
if((res = mp_mul(&gx, &v, g)) != MP_OKAY) goto CLEANUP;
|
||||
|
||||
@@ -2255,7 +2255,7 @@ void mp_print(mp_int *mp, FILE *ofp)
|
||||
|
||||
/* {{{ mp_read_signed_bin(mp, str, len) */
|
||||
|
||||
/*
|
||||
/*
|
||||
mp_read_signed_bin(mp, str, len)
|
||||
|
||||
Read in a raw value (base 256) into the given mp_int
|
||||
@@ -2332,16 +2332,16 @@ mp_err mp_read_unsigned_bin(mp_int *mp, unsigned char *str, int len)
|
||||
if((res = mp_add_d(mp, str[ix], mp)) != MP_OKAY)
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
return MP_OKAY;
|
||||
|
||||
|
||||
} /* end mp_read_unsigned_bin() */
|
||||
|
||||
/* }}} */
|
||||
|
||||
/* {{{ mp_unsigned_bin_size(mp) */
|
||||
|
||||
int mp_unsigned_bin_size(mp_int *mp)
|
||||
int mp_unsigned_bin_size(mp_int *mp)
|
||||
{
|
||||
mp_digit topdig;
|
||||
int count;
|
||||
@@ -2440,7 +2440,7 @@ int mp_count_bits(mp_int *mp)
|
||||
}
|
||||
|
||||
return len;
|
||||
|
||||
|
||||
} /* end mp_count_bits() */
|
||||
|
||||
/* }}} */
|
||||
@@ -2462,14 +2462,14 @@ mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix)
|
||||
mp_err res;
|
||||
mp_sign sig = MP_ZPOS;
|
||||
|
||||
ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
|
||||
ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
|
||||
MP_BADARG);
|
||||
|
||||
mp_zero(mp);
|
||||
|
||||
/* Skip leading non-digit characters until a digit or '-' or '+' */
|
||||
while(str[ix] &&
|
||||
(s_mp_tovalue(str[ix], radix) < 0) &&
|
||||
while(str[ix] &&
|
||||
(s_mp_tovalue(str[ix], radix) < 0) &&
|
||||
str[ix] != '-' &&
|
||||
str[ix] != '+') {
|
||||
++ix;
|
||||
@@ -2525,7 +2525,7 @@ int mp_radix_size(mp_int *mp, int radix)
|
||||
/* num = number of digits
|
||||
qty = number of bits per digit
|
||||
radix = target base
|
||||
|
||||
|
||||
Return the number of digits in the specified radix that would be
|
||||
needed to express 'num' digits of 'qty' bits each.
|
||||
*/
|
||||
@@ -2594,7 +2594,7 @@ mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix)
|
||||
++ix;
|
||||
--pos;
|
||||
}
|
||||
|
||||
|
||||
mp_clear(&tmp);
|
||||
}
|
||||
|
||||
@@ -2806,11 +2806,11 @@ void s_mp_exch(mp_int *a, mp_int *b)
|
||||
|
||||
/* {{{ s_mp_lshd(mp, p) */
|
||||
|
||||
/*
|
||||
/*
|
||||
Shift mp leftward by p digits, growing if needed, and zero-filling
|
||||
the in-shifted digits at the right end. This is a convenient
|
||||
alternative to multiplication by powers of the radix
|
||||
*/
|
||||
*/
|
||||
|
||||
mp_err s_mp_lshd(mp_int *mp, mp_size p)
|
||||
{
|
||||
@@ -2829,7 +2829,7 @@ mp_err s_mp_lshd(mp_int *mp, mp_size p)
|
||||
dp = DIGITS(mp);
|
||||
|
||||
/* Shift all the significant figures over as needed */
|
||||
for(ix = pos - p; ix >= 0; ix--)
|
||||
for(ix = pos - p; ix >= 0; ix--)
|
||||
dp[ix + p] = dp[ix];
|
||||
|
||||
/* Fill the bottom digits with zeroes */
|
||||
@@ -2844,7 +2844,7 @@ mp_err s_mp_lshd(mp_int *mp, mp_size p)
|
||||
|
||||
/* {{{ s_mp_rshd(mp, p) */
|
||||
|
||||
/*
|
||||
/*
|
||||
Shift mp rightward by p digits. Maintains the invariant that
|
||||
digits above the precision are all zero. Digits shifted off the
|
||||
end are lost. Cannot fail.
|
||||
@@ -3054,7 +3054,7 @@ void s_mp_div_2d(mp_int *mp, mp_digit d)
|
||||
end of the division process).
|
||||
|
||||
We multiply by the smallest power of 2 that gives us a leading digit
|
||||
at least half the radix. By choosing a power of 2, we simplify the
|
||||
at least half the radix. By choosing a power of 2, we simplify the
|
||||
multiplication and division steps to simple shifts.
|
||||
*/
|
||||
mp_digit s_mp_norm(mp_int *a, mp_int *b)
|
||||
@@ -3066,7 +3066,7 @@ mp_digit s_mp_norm(mp_int *a, mp_int *b)
|
||||
t <<= 1;
|
||||
++d;
|
||||
}
|
||||
|
||||
|
||||
if(d != 0) {
|
||||
s_mp_mul_2d(a, d);
|
||||
s_mp_mul_2d(b, d);
|
||||
@@ -3188,14 +3188,14 @@ mp_err s_mp_mul_d(mp_int *a, mp_digit d)
|
||||
test guarantees we have enough storage to do this safely.
|
||||
*/
|
||||
if(k) {
|
||||
dp[max] = k;
|
||||
dp[max] = k;
|
||||
USED(a) = max + 1;
|
||||
}
|
||||
|
||||
s_mp_clamp(a);
|
||||
|
||||
return MP_OKAY;
|
||||
|
||||
|
||||
} /* end s_mp_mul_d() */
|
||||
|
||||
/* }}} */
|
||||
@@ -3289,7 +3289,7 @@ mp_err s_mp_add(mp_int *a, mp_int *b) /* magnitude addition */
|
||||
}
|
||||
|
||||
/* If we run out of 'b' digits before we're actually done, make
|
||||
sure the carries get propagated upward...
|
||||
sure the carries get propagated upward...
|
||||
*/
|
||||
used = USED(a);
|
||||
while(w && ix < used) {
|
||||
@@ -3351,7 +3351,7 @@ mp_err s_mp_sub(mp_int *a, mp_int *b) /* magnitude subtract */
|
||||
/* Clobber any leading zeroes we created */
|
||||
s_mp_clamp(a);
|
||||
|
||||
/*
|
||||
/*
|
||||
If there was a borrow out, then |b| > |a| in violation
|
||||
of our input invariant. We've already done the work,
|
||||
but we'll at least complain about it...
|
||||
@@ -3387,7 +3387,7 @@ mp_err s_mp_reduce(mp_int *x, mp_int *m, mp_int *mu)
|
||||
s_mp_mod_2d(&q, (mp_digit)(DIGIT_BIT * (um + 1)));
|
||||
#else
|
||||
s_mp_mul_dig(&q, m, um + 1);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* x = x - q */
|
||||
if((res = mp_sub(x, &q, x)) != MP_OKAY)
|
||||
@@ -3441,7 +3441,7 @@ mp_err s_mp_mul(mp_int *a, mp_int *b)
|
||||
|
||||
pb = DIGITS(b);
|
||||
for(ix = 0; ix < ub; ++ix, ++pb) {
|
||||
if(*pb == 0)
|
||||
if(*pb == 0)
|
||||
continue;
|
||||
|
||||
/* Inner product: Digits of a */
|
||||
@@ -3480,7 +3480,7 @@ void s_mp_kmul(mp_digit *a, mp_digit *b, mp_digit *out, mp_size len)
|
||||
for(ix = 0; ix < len; ++ix, ++b) {
|
||||
if(*b == 0)
|
||||
continue;
|
||||
|
||||
|
||||
pa = a;
|
||||
for(jx = 0; jx < len; ++jx, ++pa) {
|
||||
pt = out + ix + jx;
|
||||
@@ -3547,7 +3547,7 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
*/
|
||||
for(jx = ix + 1, pa2 = DIGITS(a) + jx; jx < used; ++jx, ++pa2) {
|
||||
mp_word u = 0, v;
|
||||
|
||||
|
||||
/* Store this in a temporary to avoid indirections later */
|
||||
pt = pbt + ix + jx;
|
||||
|
||||
@@ -3568,7 +3568,7 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
v = *pt + k;
|
||||
|
||||
/* If we do not already have an overflow carry, check to see
|
||||
if the addition will cause one, and set the carry out if so
|
||||
if the addition will cause one, and set the carry out if so
|
||||
*/
|
||||
u |= ((MP_WORD_MAX - v) < w);
|
||||
|
||||
@@ -3592,7 +3592,7 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
/* If we are carrying out, propagate the carry to the next digit
|
||||
in the output. This may cascade, so we have to be somewhat
|
||||
circumspect -- but we will have enough precision in the output
|
||||
that we won't overflow
|
||||
that we won't overflow
|
||||
*/
|
||||
kx = 1;
|
||||
while(k) {
|
||||
@@ -3664,7 +3664,7 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
while(ix >= 0) {
|
||||
/* Find a partial substring of a which is at least b */
|
||||
while(s_mp_cmp(&rem, b) < 0 && ix >= 0) {
|
||||
if((res = s_mp_lshd(&rem, 1)) != MP_OKAY)
|
||||
if((res = s_mp_lshd(&rem, 1)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
|
||||
if((res = s_mp_lshd(", 1)) != MP_OKAY)
|
||||
@@ -3676,8 +3676,8 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
}
|
||||
|
||||
/* If we didn't find one, we're finished dividing */
|
||||
if(s_mp_cmp(&rem, b) < 0)
|
||||
break;
|
||||
if(s_mp_cmp(&rem, b) < 0)
|
||||
break;
|
||||
|
||||
/* Compute a guess for the next quotient digit */
|
||||
q = DIGIT(&rem, USED(&rem) - 1);
|
||||
@@ -3695,7 +3695,7 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
if((res = s_mp_mul_d(&t, q)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
|
||||
/*
|
||||
/*
|
||||
If it's too big, back it off. We should not have to do this
|
||||
more than once, or, in rare cases, twice. Knuth describes a
|
||||
method by which this could be reduced to a maximum of once, but
|
||||
@@ -3719,7 +3719,7 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
}
|
||||
|
||||
/* Denormalize remainder */
|
||||
if(d != 0)
|
||||
if(d != 0)
|
||||
s_mp_div_2d(&rem, d);
|
||||
|
||||
s_mp_clamp(");
|
||||
@@ -3727,7 +3727,7 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
|
||||
/* Copy quotient back to output */
|
||||
s_mp_exch(", a);
|
||||
|
||||
|
||||
/* Copy remainder back to output */
|
||||
s_mp_exch(&rem, b);
|
||||
|
||||
@@ -3757,7 +3757,7 @@ mp_err s_mp_2expt(mp_int *a, mp_digit k)
|
||||
mp_zero(a);
|
||||
if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
|
||||
return res;
|
||||
|
||||
|
||||
DIGIT(a, dig) |= (1 << bit);
|
||||
|
||||
return MP_OKAY;
|
||||
@@ -3815,7 +3815,7 @@ int s_mp_cmp_d(mp_int *a, mp_digit d)
|
||||
if(ua > 1)
|
||||
return MP_GT;
|
||||
|
||||
if(*ap < d)
|
||||
if(*ap < d)
|
||||
return MP_LT;
|
||||
else if(*ap > d)
|
||||
return MP_GT;
|
||||
@@ -3857,7 +3857,7 @@ int s_mp_ispow2(mp_int *v)
|
||||
}
|
||||
|
||||
return ((uv - 1) * DIGIT_BIT) + extra;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
@@ -3901,7 +3901,7 @@ int s_mp_ispow2d(mp_digit d)
|
||||
int s_mp_tovalue(char ch, int r)
|
||||
{
|
||||
int val, xch;
|
||||
|
||||
|
||||
if(r > 36)
|
||||
xch = ch;
|
||||
else
|
||||
@@ -3917,7 +3917,7 @@ int s_mp_tovalue(char ch, int r)
|
||||
val = 62;
|
||||
else if(xch == '/')
|
||||
val = 63;
|
||||
else
|
||||
else
|
||||
return -1;
|
||||
|
||||
if(val < 0 || val >= r)
|
||||
@@ -3939,7 +3939,7 @@ int s_mp_tovalue(char ch, int r)
|
||||
The results may be odd if you use a radix < 2 or > 64, you are
|
||||
expected to know what you're doing.
|
||||
*/
|
||||
|
||||
|
||||
char s_mp_todigit(int val, int r, int low)
|
||||
{
|
||||
char ch;
|
||||
@@ -3960,7 +3960,7 @@ char s_mp_todigit(int val, int r, int low)
|
||||
|
||||
/* {{{ s_mp_outlen(bits, radix) */
|
||||
|
||||
/*
|
||||
/*
|
||||
Return an estimate for how long a string is needed to hold a radix
|
||||
r representation of a number with 'bits' significant bits.
|
||||
|
||||
|
@@ -46,7 +46,7 @@ extern "C" {
|
||||
|
||||
|
||||
/* detect 64-bit mode if possible */
|
||||
#if defined(__x86_64__)
|
||||
#if defined(__x86_64__)
|
||||
#if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
|
||||
#define MP_64BIT
|
||||
#endif
|
||||
@@ -82,7 +82,7 @@ extern "C" {
|
||||
|
||||
/* this is to make porting into LibTomCrypt easier :-) */
|
||||
#ifndef CRYPT
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
#if defined(_MSC_VER) || defined(__BORLANDC__)
|
||||
typedef unsigned __int64 ulong64;
|
||||
typedef signed __int64 long64;
|
||||
#else
|
||||
@@ -94,20 +94,20 @@ extern "C" {
|
||||
typedef unsigned long mp_digit;
|
||||
typedef ulong64 mp_word;
|
||||
|
||||
#ifdef MP_31BIT
|
||||
#ifdef MP_31BIT
|
||||
/* this is an extension that uses 31-bit digits */
|
||||
#define DIGIT_BIT 31
|
||||
#else
|
||||
/* default case is 28-bit digits, defines MP_28BIT as a handy macro to test */
|
||||
#define DIGIT_BIT 28
|
||||
#define MP_28BIT
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* define heap macros */
|
||||
#ifndef CRYPT
|
||||
/* default to libc stuff */
|
||||
#ifndef XMALLOC
|
||||
#ifndef XMALLOC
|
||||
#define XMALLOC malloc
|
||||
#define XFREE free
|
||||
#define XREALLOC realloc
|
||||
@@ -169,7 +169,7 @@ extern int KARATSUBA_MUL_CUTOFF,
|
||||
#define MP_PREC 32 /* default digits of precision */
|
||||
#else
|
||||
#define MP_PREC 8 /* default digits of precision */
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
|
||||
@@ -473,7 +473,7 @@ int mp_prime_fermat(mp_int *a, mp_int *b, int *result);
|
||||
int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result);
|
||||
|
||||
/* This gives [for a given bit size] the number of trials required
|
||||
* such that Miller-Rabin gives a prob of failure lower than 2^-96
|
||||
* such that Miller-Rabin gives a prob of failure lower than 2^-96
|
||||
*/
|
||||
int mp_prime_rabin_miller_trials(int size);
|
||||
|
||||
@@ -494,7 +494,7 @@ int mp_prime_is_prime(mp_int *a, int t, int *result);
|
||||
int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
|
||||
|
||||
/* makes a truly random prime of a given size (bytes),
|
||||
* call with bbs = 1 if you want it to be congruent to 3 mod 4
|
||||
* call with bbs = 1 if you want it to be congruent to 3 mod 4
|
||||
*
|
||||
* You have to supply a callback which fills in a buffer with random bytes. "dat" is a parameter you can
|
||||
* have passed to the callback (e.g. a state or something). This function doesn't use "dat" itself
|
||||
@@ -507,7 +507,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style);
|
||||
/* makes a truly random prime of a given size (bits),
|
||||
*
|
||||
* Flags are as follows:
|
||||
*
|
||||
*
|
||||
* LTM_PRIME_BBS - make prime congruent to 3 mod 4
|
||||
* LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
|
||||
* LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero
|
||||
|
@@ -60,9 +60,9 @@
|
||||
#undef BN_FAST_MP_INVMOD_C
|
||||
|
||||
/* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without
|
||||
* trouble.
|
||||
* trouble.
|
||||
*/
|
||||
#undef BN_S_MP_MUL_DIGS_C
|
||||
#undef BN_S_MP_SQR_C
|
||||
|
@@ -279,7 +279,7 @@ hash_test (struct hash_foo *hash, struct test *tests)
|
||||
|
||||
ectx = EVP_MD_CTX_create();
|
||||
EVP_DigestInit_ex(ectx, hash->evp(), NULL);
|
||||
|
||||
|
||||
(*hash->init)(ctx);
|
||||
if(strcmp(t->str, ONE_MILLION_A) == 0) {
|
||||
int i;
|
||||
@@ -313,7 +313,7 @@ hash_test (struct hash_foo *hash, struct test *tests)
|
||||
printf("\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
EVP_DigestFinal_ex(ectx, res, &esize);
|
||||
EVP_MD_CTX_destroy(ectx);
|
||||
|
||||
@@ -321,7 +321,7 @@ hash_test (struct hash_foo *hash, struct test *tests)
|
||||
printf("EVP %s returned wrong hash size\n", hash->name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
if (memcmp (res, t->hash, hash->hsize) != 0) {
|
||||
printf("EVP %s failed here old function where successful!\n",
|
||||
hash->name);
|
||||
|
@@ -141,7 +141,7 @@ PKCS12_key_gen(const void *key, size_t keylen,
|
||||
BN_bn2bin(bnI, I + i + vlen - j);
|
||||
}
|
||||
BN_free(bnI);
|
||||
}
|
||||
}
|
||||
BN_free(bnB);
|
||||
BN_free(bnOne);
|
||||
size_I = vlen * 2;
|
||||
|
@@ -144,7 +144,7 @@ egd_seed(const void *indata, int size)
|
||||
break;
|
||||
indata = ((unsigned char *)indata) + len;
|
||||
size -= len;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ get_bytes(const char *path, unsigned char *outdata, int size)
|
||||
break;
|
||||
outdata += len;
|
||||
size -= len;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
|
@@ -106,7 +106,7 @@ RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
|
||||
k[j] = Sbox[k[j + 1] ^ k[j + T8]];
|
||||
|
||||
for (j = 0; j < 64; j++)
|
||||
key->data[j] = k[(j * 2) + 0] | (k[(j * 2) + 1] << 8);
|
||||
key->data[j] = k[(j * 2) + 0] | (k[(j * 2) + 1] << 8);
|
||||
memset(k, 0, sizeof(k));
|
||||
}
|
||||
|
||||
|
@@ -188,7 +188,7 @@ ltm_rsa_public_encrypt(int flen, const unsigned char* from,
|
||||
memcpy(p, from, flen);
|
||||
p += flen;
|
||||
assert((p - p0) == size - 1);
|
||||
|
||||
|
||||
mp_read_unsigned_bin(&dec, p0, size - 1);
|
||||
free(p0);
|
||||
|
||||
|
@@ -142,7 +142,7 @@ tfm_rsa_public_encrypt(int flen, const unsigned char* from,
|
||||
memcpy(p, from, flen);
|
||||
p += flen;
|
||||
assert((p - p0) == size - 1);
|
||||
|
||||
|
||||
fp_init_multi(&enc, &dec, NULL);
|
||||
fp_read_unsigned_bin(&dec, p0, size - 1);
|
||||
free(p0);
|
||||
|
@@ -55,12 +55,12 @@
|
||||
*
|
||||
* Speed for RSA in seconds
|
||||
* no key blinding
|
||||
* 1000 iteration,
|
||||
* 1000 iteration,
|
||||
* same rsa keys (1024 and 2048)
|
||||
* operation performed each eteration sign, verify, encrypt, decrypt on a random bit pattern
|
||||
*
|
||||
* name 1024 2048 4098
|
||||
* =================================
|
||||
* =================================
|
||||
* gmp: 0.73 6.60 44.80
|
||||
* tfm: 2.45 -- --
|
||||
* ltm: 3.79 20.74 105.41 (default in hcrypto)
|
||||
@@ -442,11 +442,11 @@ RSA_verify(int type, const unsigned char *from, unsigned int flen,
|
||||
free_DigestInfo(&di);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
ret = der_heim_oid_cmp(&digest_alg->algorithm,
|
||||
&di.digestAlgorithm.algorithm);
|
||||
free_DigestInfo(&di);
|
||||
|
||||
|
||||
if (ret != 0)
|
||||
return 0;
|
||||
return 1;
|
||||
@@ -577,7 +577,7 @@ d2i_RSAPrivateKey(RSA *rsa, const unsigned char **pp, size_t len)
|
||||
RSA_free(k);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
@@ -701,6 +701,6 @@ d2i_RSAPublicKey(RSA *rsa, const unsigned char **pp, size_t len)
|
||||
RSA_free(k);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
return k;
|
||||
}
|
||||
|
@@ -116,7 +116,7 @@ calc (SHA256_CTX *m, uint32_t *in)
|
||||
|
||||
T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_256[i] + data[i];
|
||||
T2 = Sigma0(AA) + Maj(AA,BB,CC);
|
||||
|
||||
|
||||
HH = GG;
|
||||
GG = FF;
|
||||
FF = EE;
|
||||
|
@@ -140,7 +140,7 @@ calc (SHA512_CTX *m, uint64_t *in)
|
||||
|
||||
T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_512[i] + data[i];
|
||||
T2 = Sigma0(AA) + Maj(AA,BB,CC);
|
||||
|
||||
|
||||
HH = GG;
|
||||
GG = FF;
|
||||
FF = EE;
|
||||
|
@@ -348,7 +348,7 @@ test_BN_CTX(void)
|
||||
|
||||
if ((c = BN_CTX_new()) == NULL)
|
||||
return 1;
|
||||
|
||||
|
||||
for (i = 0; i < testnum; i++) {
|
||||
BN_CTX_start(c);
|
||||
BN_CTX_end(c);
|
||||
|
@@ -120,7 +120,7 @@ struct tests camellia128_tests[] = {
|
||||
};
|
||||
|
||||
struct tests rc4_tests[] = {
|
||||
{
|
||||
{
|
||||
"rc4 8",
|
||||
"\x01\x23\x45\x67\x89\xAB\xCD\xEF",
|
||||
8,
|
||||
|
@@ -306,12 +306,12 @@ static void set_prime(BIGNUM *p, char *str)
|
||||
prime = (unsigned char *)str2val(str, 16, &len);
|
||||
if (prime == NULL)
|
||||
errx(1, "failed to parse %s", str);
|
||||
BN_bin2bn(prime, len, p);
|
||||
BN_bin2bn(prime, len, p);
|
||||
}
|
||||
|
||||
static void set_generator(BIGNUM *g)
|
||||
{
|
||||
BN_set_word(g, 2);
|
||||
BN_set_word(g, 2);
|
||||
}
|
||||
|
||||
static void print_secret(unsigned char *sec, size_t len)
|
||||
@@ -462,13 +462,13 @@ main(int argc, char **argv)
|
||||
|
||||
{
|
||||
struct prime *p = primes;
|
||||
|
||||
|
||||
for (; p->name; ++p)
|
||||
if (check_prime(engine, p))
|
||||
printf("%s: shared secret OK\n", p->name);
|
||||
else
|
||||
printf("%s: shared secret FAILURE\n", p->name);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -198,23 +198,23 @@ main(int argc, char **argv)
|
||||
int keylen;
|
||||
RSA *rsa;
|
||||
FILE *f;
|
||||
|
||||
|
||||
f = fopen(rsa_flag, "rb");
|
||||
if (f == NULL)
|
||||
err(1, "could not open file %s", rsa_flag);
|
||||
|
||||
|
||||
size = fread(buf, 1, sizeof(buf), f);
|
||||
if (size == 0)
|
||||
err(1, "failed to read file %s", rsa_flag);
|
||||
if (size == sizeof(buf))
|
||||
err(1, "key too long in file %s!", rsa_flag);
|
||||
fclose(f);
|
||||
|
||||
|
||||
p = buf;
|
||||
rsa = d2i_RSAPrivateKey(NULL, &p, size);
|
||||
if (rsa == NULL)
|
||||
err(1, "failed to parse key in file %s", rsa_flag);
|
||||
|
||||
|
||||
RSA_set_method(rsa, ENGINE_get_RSA(engine));
|
||||
|
||||
/*
|
||||
@@ -274,7 +274,7 @@ main(int argc, char **argv)
|
||||
"EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
|
||||
"FFFFFFFF" "FFFFFFFF";
|
||||
const char *g = "02";
|
||||
|
||||
|
||||
/*
|
||||
* Try generated keys
|
||||
*/
|
||||
@@ -306,19 +306,19 @@ main(int argc, char **argv)
|
||||
|
||||
server = DH_new_method(engine);
|
||||
client = DH_new_method(engine);
|
||||
|
||||
|
||||
BN_hex2bn(&server->p, p);
|
||||
BN_hex2bn(&client->p, p);
|
||||
BN_hex2bn(&server->g, g);
|
||||
BN_hex2bn(&client->g, g);
|
||||
|
||||
|
||||
BN_hex2bn(&client->priv_key, dhtests[i].cpriv);
|
||||
BN_hex2bn(&client->pub_key, dhtests[i].cpub);
|
||||
BN_hex2bn(&server->priv_key, dhtests[i].spriv);
|
||||
BN_hex2bn(&server->pub_key, dhtests[i].spub);
|
||||
|
||||
dh_test(server, client);
|
||||
|
||||
|
||||
DH_free(server);
|
||||
DH_free(client);
|
||||
}
|
||||
|
@@ -71,6 +71,6 @@ main(int argc, char **argv)
|
||||
printf("wrong answer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -134,7 +134,7 @@ main(int argc, char **argv)
|
||||
else
|
||||
errx(1, "unknown method %s", rand_method);
|
||||
}
|
||||
|
||||
|
||||
if (RAND_file_name(path, sizeof(path)) == NULL)
|
||||
errx(1, "RAND_file_name failed");
|
||||
|
||||
@@ -161,7 +161,7 @@ main(int argc, char **argv)
|
||||
c = c >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (bit = 0; bit < 8; bit++) {
|
||||
|
||||
res = ((double)abs(len - bits[bit] * 2)) / (double)len;
|
||||
|
@@ -334,13 +334,13 @@ main(int argc, char **argv)
|
||||
0x6d, 0x33, 0xf9, 0x40, 0x75, 0x5b, 0x4e, 0xc5, 0x90, 0x35,
|
||||
0x48, 0xab, 0x75, 0x02, 0x09, 0x76, 0x9a, 0xb4, 0x7d, 0x6b
|
||||
};
|
||||
|
||||
|
||||
check_rsa(sha1, sizeof(sha1), rsa, RSA_PKCS1_PADDING);
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
unsigned char sha1[20];
|
||||
|
||||
|
||||
RAND_bytes(sha1, sizeof(sha1));
|
||||
check_rsa(sha1, sizeof(sha1), rsa, RSA_PKCS1_PADDING);
|
||||
}
|
||||
@@ -371,9 +371,9 @@ main(int argc, char **argv)
|
||||
|
||||
e = BN_new();
|
||||
BN_set_word(e, 0x10001);
|
||||
|
||||
|
||||
BN_GENCB_set(&cb, cb_func, NULL);
|
||||
|
||||
|
||||
RAND_bytes(&n, sizeof(n));
|
||||
n &= 0x1ff;
|
||||
n += 1024;
|
||||
@@ -382,7 +382,7 @@ main(int argc, char **argv)
|
||||
errx(1, "RSA_generate_key_ex");
|
||||
|
||||
BN_free(e);
|
||||
|
||||
|
||||
for (j = 0; j < 8; j++) {
|
||||
unsigned char sha1[20];
|
||||
RAND_bytes(sha1, sizeof(sha1));
|
||||
|
@@ -62,7 +62,7 @@ intr(int sig)
|
||||
*/
|
||||
|
||||
static int
|
||||
read_string(const char *preprompt, const char *prompt,
|
||||
read_string(const char *preprompt, const char *prompt,
|
||||
char *buf, size_t len, int echo)
|
||||
{
|
||||
int of = 0;
|
||||
@@ -86,13 +86,13 @@ read_string(const char *preprompt, const char *prompt,
|
||||
if(of)
|
||||
p--;
|
||||
*p = 0;
|
||||
|
||||
|
||||
if(echo == 0){
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
signal(SIGINT, oldsigintr);
|
||||
|
||||
|
||||
if(intr_flag)
|
||||
return -2;
|
||||
if(of)
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user