Permalink
Browse files

added libtommath-0.03

  • Loading branch information...
Tom St Denis authored and sjaeckel committed Feb 28, 2003
1 parent 8c97c9e commit f89172c3af832d7225e2a733f4cbbba943a82153
Showing with 511 additions and 151 deletions.
  1. +356 −102 bn.c
  2. +17 −7 bn.h
  3. BIN bn.pdf
  4. +33 −24 bn.tex
  5. +12 −0 changes.txt
  6. +76 −16 demo.c
  7. +1 −1 makefile
  8. +16 −1 mtest/mtest.c
View
458 bn.c

Large diffs are not rendered by default.

Oops, something went wrong.
View
24 bn.h
@@ -46,7 +46,9 @@
#define DIGIT_BIT ((CHAR_BIT * sizeof(mp_digit) - 1)) /* bits per digit */
#endif
-#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
+#define MP_DIGIT_BIT DIGIT_BIT
+#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
+#define MP_DIGIT_MAX MP_MASK
/* equalities */
#define MP_LT -1 /* less than */
@@ -57,8 +59,9 @@
#define MP_NEG 1 /* negative */
#define MP_OKAY 0 /* ok result */
-#define MP_MEM 1 /* out of mem */
-#define MP_VAL 2 /* invalid input */
+#define MP_MEM -2 /* out of mem */
+#define MP_VAL -3 /* invalid input */
+#define MP_RANGE MP_VAL
#define KARATSUBA_MUL_CUTOFF 80 /* Min. number of digits before Karatsuba multiplication is used. */
#define KARATSUBA_SQR_CUTOFF 80 /* Min. number of digits before Karatsuba squaring is used. */
@@ -68,6 +71,10 @@ typedef struct {
mp_digit *dp;
} mp_int;
+#define USED(m) ((m)->used)
+#define DIGIT(m,k) ((m)->dp[k])
+#define SIGN(m) ((m)->sign)
+
/* ---> init and deinit bignum functions <--- */
/* init a bignum */
@@ -155,8 +162,8 @@ int mp_sqr(mp_int *a, mp_int *b);
/* a/b => cb + d == a */
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
-/* c == a mod b */
-#define mp_mod(a, b, c) mp_div(a, b, NULL, c)
+/* c = a mod b, 0 <= c < b */
+int mp_mod(mp_int *a, mp_int *b, mp_int *c);
/* ---> single digit functions <--- */
@@ -175,8 +182,11 @@ int mp_mul_d(mp_int *a, mp_digit b, mp_int *c);
/* a/b => cb + d == a */
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
-/* c = a mod b */
-#define mp_mod_d(a,b,c) mp_div_d(a, b, NULL, c)
+/* c = a^b */
+int mp_expt_d(mp_int *a, mp_digit b, mp_int *c);
+
+/* c = a mod b, 0 <= c < b */
+int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
/* ---> number theory <--- */
View
BIN bn.pdf
Binary file not shown.
View
57 bn.tex
@@ -1,7 +1,7 @@
\documentclass{article}
\begin{document}
-\title{LibTomMath v0.02 \\ A Free Multiple Precision Integer Library}
+\title{LibTomMath v0.03 \\ A Free Multiple Precision Integer Library}
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
\newpage
@@ -82,6 +82,15 @@ \subsection{The mp\_int Structure}
to how many digits have been allocated off the heap. There is also the $\beta$ quantity which is equal to $2^W$ where
$W$ is the number of bits in a digit (default is 28).
+\subsection{Calling Functions}
+Most functions expect pointers to mp\_int's as parameters. To save on memory usage it is possible to have source
+variables as destinations. For example:
+\begin{verbatim}
+ mp_add(&x, &y, &x); /* x = x + y */
+ mp_mul(&x, &z, &x); /* x = x * z */
+ mp_div_2(&x, &x); /* x = x / 2 */
+\end{verbatim}
+
\subsection{Basic Functionality}
Essentially all LibTomMath functions return one of three values to indicate if the function worked as desired. A
function will return \textbf{MP\_OKAY} if the function was successful. A function will return \textbf{MP\_MEM} if
@@ -219,8 +228,8 @@ \subsection{Basic Arithmetic}
/* a/b => cb + d == a */
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d);
-/* c == a mod b */
-#define mp_mod(a, b, c) mp_div(a, b, NULL, c)
+/* c = a mod b, 0 <= c < b */
+int mp_mod(mp_int *a, mp_int *b, mp_int *c);
\end{verbatim}
The \textbf{mp\_cmp} will compare two integers. It will return \textbf{MP\_LT} if the first parameter is less than
@@ -251,8 +260,8 @@ \subsection{Basic Arithmetic}
/* a/b => cb + d == a */
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d);
-/* c = a mod b */
-#define mp_mod_d(a,b,c) mp_div_d(a, b, NULL, c)
+/* c = a mod b, 0 <= c < b */
+int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c);
\end{verbatim}
Note that care should be taken for the value of the digit passed. By default, any 28-bit integer is a valid digit that can
@@ -328,27 +337,27 @@ \subsection{Observed Timings}
\begin{tabular}{c|c|c|c}
\hline \textbf{Operation} & \textbf{Size (bits)} & \textbf{Time with MPI (cycles)} & \textbf{Time with LibTomMath (cycles)} \\
\hline
-Multiply & 128 & 1,394 & 893 \\
-Multiply & 256 & 2,559 & 1,744 \\
-Multiply & 512 & 7,919 & 4,484 \\
-Multiply & 1024 & 28,460 & 9,326, \\
-Multiply & 2048 & 109,637 & 30,140 \\
-Multiply & 4096 & 467,226 & 122,290 \\
+Multiply & 128 & 1,426 & 928 \\
+Multiply & 256 & 2,551 & 1,787 \\
+Multiply & 512 & 7,913 & 3,458 \\
+Multiply & 1024 & 28,496 & 9,271 \\
+Multiply & 2048 & 109,897 & 29,917 \\
+Multiply & 4096 & 469,970 & 123,934 \\
\hline
-Square & 128 & 1,288 & 1,172 \\
-Square & 256 & 1,705 & 2,162 \\
-Square & 512 & 5,365 & 3,723 \\
-Square & 1024 & 18,836 & 9,063 \\
-Square & 2048 & 72,334 & 27,489 \\
-Square & 4096 & 306,252 & 110,372 \\
+Square & 128 & 1,319 & 1,230 \\
+Square & 256 & 1,776 & 2,131 \\
+Square & 512 & 5,399 & 3,694 \\
+Square & 1024 & 18,991 & 9,172 \\
+Square & 2048 & 72,126 & 27,352 \\
+Square & 4096 & 306,269 & 110,607 \\
\hline
-Exptmod & 512 & 30,497,732 & 6,898,504 \\
-Exptmod & 768 & 98,943,020 & 15,510,779 \\
-Exptmod & 1024 & 221,123,749 & 27,962,904 \\
-Exptmod & 2048 & 1,694,796,907 & 146,631,975 \\
-Exptmod & 2560 & 3,262,360,107 & 305,530,060 \\
-Exptmod & 3072 & 5,647,243,373 & 472,572,762 \\
-Exptmod & 4096 & 13,345,194,048 & 984,415,240
+Exptmod & 512 & 32,021,586 & 6,880,075 \\
+Exptmod & 768 & 97,595,492 & 15,202,614 \\
+Exptmod & 1024 & 223,302,532 & 28,081,865 \\
+Exptmod & 2048 & 1,682,223,369 & 146,545,454 \\
+Exptmod & 2560 & 3,268,615,571 & 310,970,112 \\
+Exptmod & 3072 & 5,597,240,141 & 480,703,712 \\
+Exptmod & 4096 & 13,347,270,891 & 985,918,868
\end{tabular}
\end{center}
View
@@ -1,3 +1,15 @@
+Dec 27th, 2002
+v0.03 -- Sped up s_mp_mul_high_digs by not computing the carries of the lower digits
+ -- Fixed a bug where mp_set_int wouldn't zero the value first and set the used member.
+ -- fixed a bug in s_mp_mul_high_digs where the limit placed on the result digits was not calculated properly
+ -- fixed bugs in add/sub/mul/sqr_mod functions where if the modulus and dest were the same it wouldn't work
+ -- fixed a bug in mp_mod and mp_mod_d concerning negative inputs
+ -- mp_mul_d didn't preserve sign
+ -- Many many many many fixes
+ -- Works in LibTomCrypt now :-)
+ -- Added iterations to the timing demos... more accurate.
+ -- Tom needs a job.
+
Dec 26th, 2002
v0.02 -- Fixed a few "slips" in the manual. This is "LibTomMath" afterall :-)
-- Added mp_cmp_mag, mp_neg, mp_abs and mp_radix_size that were missing.
View
92 demo.c
@@ -21,22 +21,37 @@ void reset(void) { _tt = clock(); }
unsigned long long rdtsc(void) { return clock() - _tt; }
#endif
-static void draw(mp_int *a)
+void draw(mp_int *a)
{
char buf[4096];
- int x;
printf("a->used == %d\na->alloc == %d\na->sign == %d\n", a->used, a->alloc, a->sign);
mp_toradix(a, buf, 10);
printf("num == %s\n", buf);
printf("\n");
}
+unsigned long lfsr = 0xAAAAAAAAUL;
+
+int lbit(void)
+{
+ if (lfsr & 0x80000000UL) {
+ lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL;
+ return 1;
+ } else {
+ lfsr <<= 1;
+ return 0;
+ }
+}
+
+
+
int main(void)
{
mp_int a, b, c, d, e, f;
- unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n;
+ unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, inv_n;
unsigned char cmd[4096], buf[4096];
int rr;
+ mp_digit tom;
#ifdef TIMER
int n;
@@ -50,29 +65,33 @@ int main(void)
mp_init(&e);
mp_init(&f);
+ mp_read_radix(&a, "-2", 10);
+ mp_read_radix(&b, "2", 10);
+ mp_expt_d(&a, 3, &a);
+ draw(&a);
#ifdef TIMER
mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
while (a.used * DIGIT_BIT < 8192) {
reset();
- for (rr = 0; rr < 10000; rr++) {
+ for (rr = 0; rr < 100000; rr++) {
mp_mul(&a, &a, &b);
}
tt = rdtsc();
- printf("Multiplying %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)10000));
+ printf("Multiplying %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)100000));
mp_copy(&b, &a);
}
mp_read_radix(&a, "340282366920938463463374607431768211455", 10);
while (a.used * DIGIT_BIT < 8192) {
reset();
- for (rr = 0; rr < 10000; rr++) {
+ for (rr = 0; rr < 100000; rr++) {
mp_sqr(&a, &b);
}
tt = rdtsc();
- printf("Squaring %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)10000));
+ printf("Squaring %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)100000));
mp_copy(&b, &a);
}
@@ -87,19 +106,18 @@ int main(void)
"1214855636816562637502584060163403830270705000634713483015101384881871978446801224798536155406895823305035467591632531067547890948695117172076954220727075688048751022421198712032848890056357845974246560748347918630050853933697792254955890439720297560693579400297062396904306270145886830719309296352765295712183040773146419022875165382778007040109957609739589875590885701126197906063620133954893216612678838507540777138437797705602453719559017633986486649523611975865005712371194067612263330335590526176087004421363598470302731349138773205901447704682181517904064735636518462452242791676541725292378925568296858010151852326316777511935037531017413910506921922450666933202278489024521263798482237150056835746454842662048692127173834433089016107854491097456725016327709663199738238442164843147132789153725513257167915555162094970853584447993125488607696008169807374736711297007473812256272245489405898470297178738029484459690836250560495461579533254473316340608217876781986188705928270735695752830825527963838355419762516246028680280988020401914551825487349990306976304093109384451438813251211051597392127491464898797406789175453067960072008590614886532333015881171367104445044718144312416815712216611576221546455968770801413440778423979",
NULL
};
- srand(time(NULL));
for (n = 0; primes[n]; n++) {
mp_read_radix(&a, primes[n], 10);
mp_zero(&b);
for (rr = 0; rr < mp_count_bits(&a); rr++) {
mp_mul_2d(&b, 1, &b);
- b.dp[0] |= (rand()&1);
+ b.dp[0] |= lbit();
}
mp_sub_d(&a, 1, &c);
mp_mod(&b, &c, &b);
mp_set(&c, 3);
reset();
- for (rr = 0; rr < 20; rr++) {
+ for (rr = 0; rr < 35; rr++) {
mp_exptmod(&c, &b, &a, &d);
}
tt = rdtsc();
@@ -112,15 +130,15 @@ int main(void)
draw(&d);
exit(0);
}
- printf("Exponentiating %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)20));
+ printf("Exponentiating %d-bit took %llu cycles\n", mp_count_bits(&a), tt / ((unsigned long long)35));
}
}
#endif
- expt_n = lcm_n = gcd_n = add_n = sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = 0;
+ inv_n = expt_n = lcm_n = gcd_n = add_n = sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = 0;
for (;;) {
- printf("add=%7lu sub=%7lu mul=%7lu div=%7lu sqr=%7lu mul2d=%7lu div2d=%7lu gcd=%7lu lcm=%7lu expt=%7lu\r", add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, expt_n);
+ printf("%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu/%7lu\r", add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n, expt_n, inv_n);
fgets(cmd, 4095, stdin);
cmd[strlen(cmd)-1] = 0;
printf("%s ]\r",cmd);
@@ -161,6 +179,33 @@ int main(void)
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
+
+ /* test the sign/unsigned storage functions */
+
+ rr = mp_signed_bin_size(&c);
+ mp_to_signed_bin(&c, cmd);
+ memset(cmd+rr, rand()&255, sizeof(cmd)-rr);
+ mp_read_signed_bin(&d, cmd, rr);
+ if (mp_cmp(&c, &d) != MP_EQ) {
+ printf("mp_signed_bin failure!\n");
+ draw(&c);
+ draw(&d);
+ return 0;
+ }
+
+
+ rr = mp_unsigned_bin_size(&c);
+ mp_to_unsigned_bin(&c, cmd);
+ memset(cmd+rr, rand()&255, sizeof(cmd)-rr);
+ mp_read_unsigned_bin(&d, cmd, rr);
+ if (mp_cmp_mag(&c, &d) != MP_EQ) {
+ printf("mp_unsigned_bin failure!\n");
+ draw(&c);
+ draw(&d);
+ return 0;
+ }
+
+
} else if (!strcmp(cmd, "sub")) { ++sub_n;
fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 10);
fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 10);
@@ -210,7 +255,7 @@ draw(&a);draw(&b);draw(&c);
mp_gcd(&a, &b, &d);
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
- printf("gcd %lu failure!\n", sqr_n);
+ printf("gcd %lu failure!\n", gcd_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
@@ -221,7 +266,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
mp_lcm(&a, &b, &d);
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
- printf("lcm %lu failure!\n", sqr_n);
+ printf("lcm %lu failure!\n", lcm_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
@@ -232,11 +277,26 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); mp_read_radix(&d, buf, 10);
mp_exptmod(&a, &b, &c, &e);
if (mp_cmp(&d, &e) != MP_EQ) {
- printf("expt %lu failure!\n", sqr_n);
+ printf("expt %lu failure!\n", expt_n);
draw(&a);draw(&b);draw(&c);draw(&d); draw(&e);
return 0;
}
+ } else if (!strcmp(cmd, "invmod")) { ++inv_n;
+ fgets(buf, 4095, stdin); mp_read_radix(&a, buf, 10);
+ fgets(buf, 4095, stdin); mp_read_radix(&b, buf, 10);
+ fgets(buf, 4095, stdin); mp_read_radix(&c, buf, 10);
+ mp_invmod(&a, &b, &d);
+ mp_mulmod(&d,&a,&b,&e);
+ if (mp_cmp_d(&e, 1) != MP_EQ) {
+ printf("inv [wrong value from MPI?!] failure\n");
+ draw(&a);draw(&b);draw(&c);draw(&d);
+ mp_gcd(&a, &b, &e);
+ draw(&e);
+ return 0;
+ }
+
}
+
}
return 0;
}
View
@@ -1,7 +1,7 @@
CC = gcc
CFLAGS += -Wall -W -O3 -funroll-loops
-VERSION=0.02
+VERSION=0.03
default: test
Oops, something went wrong.

0 comments on commit f89172c

Please sign in to comment.