Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
aad9a9b
Added better initial value
czurnieden Oct 2, 2013
d92a441
Added limits for Toom-Cook and FFT
czurnieden Oct 15, 2013
c4faf91
Internal functions for FFT multiplication
czurnieden Oct 15, 2013
1309f4d
FFT multiplication
czurnieden Oct 15, 2013
1d7a4dc
FFT squaring
czurnieden Oct 15, 2013
6159edb
Added FFT multiplication
czurnieden Oct 15, 2013
c7ff221
Added FFT squaring
czurnieden Oct 15, 2013
0cdcfdc
Added FFT multiplying and squaring
czurnieden Oct 15, 2013
79c362e
Added FFT multiplying and squaring
czurnieden Oct 15, 2013
b41bc39
Added names of files related to FFT multiplication
czurnieden Oct 15, 2013
d3a2562
Added names of files related to FFT multiplication
czurnieden Oct 15, 2013
ee199bf
Deleted printf's from debugging
czurnieden Oct 15, 2013
e2a0c20
Added names of functions related to FFT multiplication
czurnieden Oct 15, 2013
f88a9f7
The factorial function
czurnieden Oct 16, 2013
7464ac2
Added dependencies for the fatorial and the primorial functions
czurnieden Oct 16, 2013
4d8fe08
Addition of the factorial and the primorial functions
czurnieden Oct 16, 2013
cd09b5d
Calculate the primorial
czurnieden Oct 16, 2013
541ca5d
The factorial function
czurnieden Oct 16, 2013
500e207
Fixed a memory leak
czurnieden Oct 17, 2013
72c81f0
The subfactorial
czurnieden Oct 17, 2013
296dce3
Addition of file bn_mp_subfactorial.c
czurnieden Oct 17, 2013
5dd56d5
Addition of functions related to subfactorial
czurnieden Oct 17, 2013
19dbdb0
Addition of dependencies related to subfactorial
czurnieden Oct 17, 2013
3823284
Preprocessorcode for bitset has been moved to tommath.h
czurnieden Oct 19, 2013
bcfc821
Function to test if the number has 32 or more bits
czurnieden Oct 19, 2013
ccc36d7
Function to test if big nmber is divisible by another big number
czurnieden Oct 19, 2013
bdc3e6e
Function to test if big nmber is divisible by a mp_digit
czurnieden Oct 19, 2013
c6ccb79
Moved bitset from bn_mp_primorial.c here and added the prototypes for…
czurnieden Oct 19, 2013
cc18ef7
Addition of the newly added files
czurnieden Oct 19, 2013
23b7c15
Addition of the newly added functions and their dependencies
czurnieden Oct 19, 2013
46d685f
Returns zero-based index of highest bit.
czurnieden Oct 19, 2013
93c0328
Returns zero-based index of lowest bit.
czurnieden Oct 19, 2013
87ae2f2
Corrected branch handling 64 bit
czurnieden Oct 19, 2013
7a4f808
Function to test if big nmber is a perfect power
czurnieden Oct 19, 2013
c0c6755
Addition of mp_isperfpower
czurnieden Oct 19, 2013
7e791d4
max(a,b) for big numbers
czurnieden Oct 20, 2013
b698551
min(a,b) for big numbers
czurnieden Oct 20, 2013
b4f1cd3
Addition of functions min() and max() for big numbers
czurnieden Oct 20, 2013
3f91ce9
Function to compute big number to the power of big number
czurnieden Oct 21, 2013
078fa7f
Added prototype and dependencies for function mp_expt()
czurnieden Oct 21, 2013
b942aac
Assumed libtommath's naming convention
czurnieden Oct 28, 2013
09f15fe
Fast computation of the binomial coefficients
czurnieden Oct 30, 2013
e4654ab
Corrected a typo
czurnieden Oct 31, 2013
ee1fb4f
Fast computation of the Ctalan numbers
czurnieden Oct 31, 2013
ad4d805
Fixed C&P error
czurnieden Oct 31, 2013
35e0323
Calculation of the double factorials, odd and even, for positive numbers
czurnieden Oct 31, 2013
6e1e028
Calculation of Fibinacci and Lucas numbers
czurnieden Oct 31, 2013
bc20e74
Oops, sorry
czurnieden Oct 31, 2013
2737290
Function to compute Euler numbers added.
czurnieden Oct 31, 2013
9affa55
Added forgotten object file for bn_mp_catalan.c
czurnieden Nov 1, 2013
d56768b
Fixed a bug in bn_mp_fibonacci.c which fixes a bug in bn_mp_lucas.c, too
czurnieden Nov 3, 2013
a95ec03
Addition of Pell numbers with relatives
czurnieden Nov 3, 2013
95e4bd6
Fast matrix exponentiating for a 2x2 matrix used for some sort of rec…
czurnieden Nov 3, 2013
95b8777
Computing Pell numbers
czurnieden Nov 3, 2013
060aebb
Computing Pell-Lucas numbers, also known as companion Pell numbers
czurnieden Nov 3, 2013
12763c8
Computing modified Pell numbers
czurnieden Nov 3, 2013
99f03a1
Corrected typo
czurnieden Nov 3, 2013
da344cb
Addition of several of the more obscure integer functions: Jacobsthal…
czurnieden Nov 5, 2013
514d11c
corrected fatal typo resulting in failed compiling caused by some aut…
czurnieden Nov 5, 2013
a59ebaf
Addition of methods to do some basic arithmetic with prime factorizat…
czurnieden Nov 5, 2013
c88b84d
Addition of Bell numbers
czurnieden Nov 9, 2013
e4839bf
Addition of Bell numbers with an algorithm that is trading memory, a …
czurnieden Nov 9, 2013
dd88b37
Addition of Stirling numbers of the first kind
czurnieden Nov 9, 2013
257a1ac
Addition of Stirling numbers of the first kind, precomputing the whol…
czurnieden Nov 9, 2013
f068ff2
Addition of Stirling numbers of the second kind
czurnieden Nov 9, 2013
029a6dd
Addition of Stirling numbers of the second kind, precomputing the who…
czurnieden Nov 9, 2013
f2b1475
Included stirling* and Bell numbers into the building process
czurnieden Nov 9, 2013
4c2fb4a
Included stirling* and Bell numbers into the building process
czurnieden Nov 9, 2013
b1866a2
Repair of errors caused by automated helpers and manual programmers
czurnieden Nov 9, 2013
81afcfa
addition of rising factoriral
czurnieden Nov 9, 2013
4d2b4ce
addition of falling factoriral
czurnieden Nov 9, 2013
2ff3fa3
auto-deb updated
czurnieden Nov 9, 2013
83dc8c1
Bug-fix (an off-by-all)
czurnieden Nov 9, 2013
2fcbb5e
Bug-fix (an off-by-one in malloc. Oh, my... )
czurnieden Nov 9, 2013
fbea60c
Addition of function superfactorial
czurnieden Nov 9, 2013
b3836ae
corrected calculation of memory to be more conserative. Just in case.
czurnieden Nov 9, 2013
177dc88
Addition of a function to set a mp_word directly which is probably bi…
czurnieden Nov 15, 2013
23c4e0f
made multiplication and squaring a bit faster
czurnieden Dec 2, 2013
d5f0109
BUGFIX: several
czurnieden Oct 26, 2014
27d027f
ADDED: set and get a double
czurnieden Oct 26, 2014
cc9f668
rerun dep.pl
czurnieden Oct 26, 2014
2c447f8
replaced constant with a general macro from the C-standard
czurnieden Oct 26, 2014
b2de1e8
bracket c99 stuff (rounding) out with #if __STDC_VERSION__ >= 199901L
czurnieden Oct 26, 2014
eb8e537
bracket more c99 stuff out with #if __STDC_VERSION__ >= 199901L
czurnieden Oct 26, 2014
bc9453a
BUGFIX: bn_mp_[sg]et_double.c is in the build now
czurnieden Feb 26, 2015
08e516f
BUGFIX: typo in bn_mp_set_double.c
czurnieden Feb 26, 2015
c052000
ADDED: some shortcuts for printing
czurnieden Feb 28, 2015
c1f5c26
ADDED: subquadratic division (Burnickel and Ziegeler's)
czurnieden Mar 17, 2015
3ce7d17
ADDED: subquadratic division (Newton)
czurnieden Mar 18, 2015
6d6ecb0
ADDED: integer logarithm to arbitrary positive integer base
czurnieden Mar 22, 2015
3a359d4
CHANGED: (compile time) option to use an integer logarithm for mp_rad…
czurnieden Mar 22, 2015
09bf369
BUGFIXES: several for 64 bit
czurnieden Mar 31, 2015
b1f54b4
BUGFIX: commented out some testcode
czurnieden Aug 15, 2015
3007dc0
BUGFIX: in toom-cook 4&5
czurnieden Dec 21, 2015
86f91f4
Added suspenders to the belt
czurnieden Dec 29, 2015
ca2930f
corrected calculation of number of steps
czurnieden Dec 29, 2015
5c27116
cleaned up
czurnieden Dec 29, 2015
ba9680c
BUGFIX: moved 64-bit check to the right place
czurnieden Dec 29, 2015
6cd5d0d
ADDED: subquadratic radix conversion (mp_int to string only)
czurnieden Dec 29, 2015
9ea5dda
chores
czurnieden Dec 29, 2015
2b1d5e1
expanded to support all bases toradix supports
czurnieden Dec 29, 2015
02d041a
complete base independance
czurnieden Dec 29, 2015
1ade6b9
ADDED: fast radix conversion from string
czurnieden Dec 30, 2015
00c9567
Fixed a bug caused by lack of confidence
czurnieden Jan 3, 2016
947ec5d
refined FFT cutoffs
czurnieden Jan 3, 2016
f5752f1
BUGFIX: ouch!
czurnieden Jan 3, 2016
db7e0be
A larger cleanup
czurnieden Jan 14, 2016
c2b1869
Refined cut-offs for 64-bit
czurnieden Jan 14, 2016
20b566f
made independant of libm
czurnieden Jan 14, 2016
f796f28
balancing still not working
czurnieden Jan 16, 2016
9c117b7
Ouch!
czurnieden Jan 16, 2016
f288962
linear and recursive balancing switchable (sort of)
czurnieden Jan 17, 2016
2292d4b
changed to more general default for balancing
czurnieden Jan 17, 2016
29ad1ff
Initialized some uninitalized values (valgrind said so)
czurnieden Jan 17, 2016
0d38da8
cleanup
czurnieden Jan 18, 2016
200f211
BUGFIX div_newton lacked the last round
czurnieden Jan 20, 2016
1fd21db
set cutoffs for MP-64BIT
czurnieden Jan 21, 2016
8ea70b0
Bugfix
czurnieden Jan 23, 2016
82c4fe9
Neither uses floats nor libmath anymore
czurnieden Feb 5, 2016
c35d7ee
removed need for global memory and added checks
czurnieden Feb 9, 2016
22c067a
Added clean-up for allocated memory
czurnieden Feb 9, 2016
a1cbd6e
BUGFIX: several
czurnieden Feb 12, 2016
a21fd10
BUGFIX: memory leak
czurnieden Feb 12, 2016
6adff96
implemented faster algorithm
czurnieden Feb 17, 2016
0449642
added forgotten file
czurnieden Feb 19, 2016
93233b2
corrected data types
czurnieden Feb 21, 2016
a720058
INITIAL recontres numbers a(n,k) = binomial(n,k)*subfactorial(n-k)
czurnieden Feb 21, 2016
2010e3a
BUGFIX: added check for radix difference
czurnieden Feb 23, 2016
d620f79
BUGFIX: double allocation caused memory leak
czurnieden Mar 22, 2016
02b7239
Implemented a fast algorithm
czurnieden Jul 26, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bn_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
*/

static const struct {
int code;
const char *msg;
int code;
const char *msg;
} msgs[] = {
{ MP_OKAY, "Successful" },
{ MP_MEM, "Out of heap" },
{ MP_VAL, "Value out of range" }
{ MP_OKAY, "Successful" },
{ MP_MEM, "Out of heap" },
{ MP_VAL, "Value out of range" }
};

/* return a char * string for a given code */
Expand All @@ -31,9 +31,9 @@ const char *mp_error_to_string(int code)

/* scan the lookup table for the given message */
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
if (msgs[x].code == code) {
return msgs[x].msg;
}
if (msgs[x].code == code) {
return msgs[x].msg;
}
}

/* generic reply for invalid code */
Expand Down
223 changes: 112 additions & 111 deletions bn_fast_mp_invmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,131 +15,132 @@
* Tom St Denis, [email protected], 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)
int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c)
{
mp_int x, y, u, v, B, D;
int res, neg;

/* 2. [modified] b must be odd */
if (mp_iseven (b) == 1) {
return MP_VAL;
}

/* init all our temps */
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
return res;
}

/* x == modulus, y == value to invert */
if ((res = mp_copy (b, &x)) != MP_OKAY) {
goto LBL_ERR;
}

/* we need y = |a| */
if ((res = mp_mod (a, b, &y)) != MP_OKAY) {
goto LBL_ERR;
}

/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto LBL_ERR;
}
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
goto LBL_ERR;
}
mp_set (&D, 1);
mp_int x, y, u, v, B, D;
int res, neg;

top:
/* 4. while u is even do */
while (mp_iseven (&u) == 1) {
/* 4.1 u = u/2 */
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto LBL_ERR;
}
/* 4.2 if B is odd then */
if (mp_isodd (&B) == 1) {
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* B = B/2 */
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* 2. [modified] b must be odd */
if (mp_iseven(b) == 1) {
return MP_VAL;
}

/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
/* 5.1 v = v/2 */
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto LBL_ERR;
}
/* 5.2 if D is odd then */
if (mp_isodd (&D) == 1) {
/* D = (D-x)/2 */
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* D = D/2 */
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
/* init all our temps */
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
return res;
}

/* x == modulus, y == value to invert */
if ((res = mp_copy(b, &x)) != MP_OKAY) {
goto LBL_ERR;
}
}
}

/* 6. if u >= v then */
if (mp_cmp (&u, &v) != MP_LT) {
/* u = u - v, B = B - D */
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
/* we need y = |a| */
if ((res = mp_mod(a, b, &y)) != MP_OKAY) {
goto LBL_ERR;
}
}

if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
goto LBL_ERR;
}
} else {
/* v - v - u, D = D - B */
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
}
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
goto LBL_ERR;
}
}
mp_set(&D, 1);

if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}

/* if not zero goto step 4 */
if (mp_iszero (&u) == 0) {
goto top;
}

/* now a = C, b = D, gcd == g*v */

/* if v != 1 then there is no inverse */
if (mp_cmp_d (&v, 1) != MP_EQ) {
res = MP_VAL;
goto LBL_ERR;
}

/* b is now the inverse */
neg = a->sign;
while (D.sign == MP_NEG) {
if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
top:
/* 4. while u is even do */
while (mp_iseven(&u) == 1) {
/* 4.1 u = u/2 */
if ((res = mp_div_2(&u, &u)) != MP_OKAY) {
goto LBL_ERR;
}
/* 4.2 if B is odd then */
if (mp_isodd(&B) == 1) {
if ((res = mp_sub(&B, &x, &B)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* B = B/2 */
if ((res = mp_div_2(&B, &B)) != MP_OKAY) {
goto LBL_ERR;
}
}

/* 5. while v is even do */
while (mp_iseven(&v) == 1) {
/* 5.1 v = v/2 */
if ((res = mp_div_2(&v, &v)) != MP_OKAY) {
goto LBL_ERR;
}
/* 5.2 if D is odd then */
if (mp_isodd(&D) == 1) {
/* D = (D-x)/2 */
if ((res = mp_sub(&D, &x, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* D = D/2 */
if ((res = mp_div_2(&D, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}

/* 6. if u >= v then */
if (mp_cmp(&u, &v) != MP_LT) {
/* u = u - v, B = B - D */
if ((res = mp_sub(&u, &v, &u)) != MP_OKAY) {
goto LBL_ERR;
}

if ((res = mp_sub(&B, &D, &B)) != MP_OKAY) {
goto LBL_ERR;
}
} else {
/* v - v - u, D = D - B */
if ((res = mp_sub(&v, &u, &v)) != MP_OKAY) {
goto LBL_ERR;
}

if ((res = mp_sub(&D, &B, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}

/* if not zero goto step 4 */
if (mp_iszero(&u) == 0) {
goto top;
}

/* now a = C, b = D, gcd == g*v */

/* if v != 1 then there is no inverse */
if (mp_cmp_d(&v, 1) != MP_EQ) {
res = MP_VAL;
goto LBL_ERR;
}
}
mp_exch (&D, c);
c->sign = neg;
res = MP_OKAY;

LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
return res;
}

/* b is now the inverse */
neg = a->sign;
while (D.sign == MP_NEG) {
if ((res = mp_add(&D, b, &D)) != MP_OKAY) {
goto LBL_ERR;
}
}
mp_exch(&D, c);
c->sign = neg;
res = MP_OKAY;

LBL_ERR:
mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
return res;
}
#endif

Expand Down
Loading