Skip to content

Commit 7b08266

Browse files
Tom St Denissjaeckel
authored andcommitted
added libtompoly-0.02
1 parent f52f0bf commit 7b08266

16 files changed

Lines changed: 589 additions & 83 deletions

TODO

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@ Short term goals [say around June]
44
- Stable code, demos to show for it
55
- Completed user manual
66

7-
For v0.02
8-
- Make sure all of the functions are consistent as to where they take the source
9-
characteristic from the rightmost argument
10-
- Get the pb_invmod to work
11-
- Get the core functions [add/sub/mul/div] stable by testing their dependents [grow/init/copy more]
12-
- Add a pb_exptmod, pb_isirreducible and various helper functions (e.g. to work with constants)
13-
- Add more to the testing demo
14-
- Add a MSVC makefile
15-
- Add some examples to manual
16-
177
Down the road
188
- Add a Karatsuba multiplier for larger polynomials and add a tuning function
199
- write a division algo for Z[x]
2010
- Some form of trace function?
11+
- I/O functions (to_raw, read_raw)
2112

2213

changes.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Jan 3rd, 2004
2+
v0.02 - Update pb_div() to shift r(x) after multplying it wit b(x) to save a bit of time
3+
- improved pb_gcd() to handle inputs which are zero
4+
- Added pb_shrink()
5+
- fixed pb_invmod()
6+
- added pb_exteuclid() [back ported that code into LTM... hehehe]
7+
- added pb_exptmod() [this led me to find a bug in LTM!!!]
8+
- added pb_monic()
9+
- added pb_isirreduc()
10+
- Some minor additions to test/documentation
11+
12+
Dec 31st, 2003
113
v0.01 ++ thanks goes to Martin Marcel, Greg Rose and Colin Percival for providing some missing knowledge and
214
helping me get this release going
315
- Initial release.

demo/demo.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ void draw_poly(pb_poly *a)
2727
int main(void)
2828
{
2929
mp_int chara;
30-
pb_poly a,b,c,d,e;
30+
pb_poly a,b,c,d,e;
31+
mp_int aa,bb,cc,dd,ee;
32+
int res;
3133

3234
mp_init(&chara);
35+
mp_init_multi(&aa,&bb,&cc,&dd,&ee,NULL);
3336
pb_init_size(&a, &chara, 32);
3437
pb_init_size(&b, &chara, 32);
3538
pb_init_size(&c, &chara, 32);
@@ -170,10 +173,10 @@ int main(void)
170173
printf("a == \n");
171174
draw_poly(&a);
172175

173-
/* take inverse of x + 1 */
176+
/* take inverse of 2x + 9 */
174177
pb_zero(&b);
175-
mp_set(&(b.terms[1]), 1);
176-
mp_set(&(b.terms[0]), 1);
178+
mp_set(&(b.terms[1]), 2);
179+
mp_set(&(b.terms[0]), 9);
177180
b.used = 2;
178181
pb_clamp(&b);
179182
printf("b == \n");
@@ -187,7 +190,24 @@ int main(void)
187190
/* test */
188191
pb_mulmod(&b, &c, &a, &d);
189192
pb_mul(&b, &c, &e);
190-
draw_poly(&d); draw_poly(&e);
193+
printf("This should be 1 : "); draw_poly(&d);
194+
printf("This should be equal to k*a + 1: "); draw_poly(&e);
195+
196+
/* now b has order [dividing] 17^2 - 1 == 288 so b^287 should equal c */
197+
printf("exptmod test\n");
198+
mp_set(&aa, 287);
199+
pb_exptmod(&b, &aa, &a, &d);
200+
printf("This should be invmod : "); draw_poly(&d);
201+
202+
/* test irreduc */
203+
printf("Irreducibility testing\n");
204+
pb_isirreduc(&a, &res);
205+
printf("This should be 1 : %d\n", res);
206+
207+
pb_isirreduc(&b, &res);
208+
printf("This should be 0 : %d\n", res);
209+
210+
191211

192212
return EXIT_SUCCESS;
193213
}

makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#Makefile for GCC by Tom St Denis
22
CFLAGS += -I. -Os -Wall -W
33

4-
VERSION=0.01
4+
VERSION=0.02
55

66
#default files to install
77
LIBNAME=libtompoly.a
@@ -20,7 +20,7 @@ default: libtompoly.a
2020
OBJECTS = pb_init.o pb_clear.o pb_init_size.o pb_grow.o pb_copy.o pb_clamp.o pb_init_copy.o \
2121
pb_add.o pb_sub.o pb_mul.o pb_div.o pb_zero.o pb_lshd.o pb_rshd.o pb_exch.o pb_mod.o \
2222
pb_addmod.o pb_submod.o pb_mulmod.o pb_gcd.o pb_init_multi.o pb_clear_multi.o pb_invmod.o \
23-
pb_cmp.o
23+
pb_cmp.o pb_shrink.o pb_exteuclid.o pb_monic.o pb_exptmod.o pb_isirreduc.o
2424

2525
libtompoly.a: $(OBJECTS)
2626
ar $(ARFLAGS) libtompoly.a $(OBJECTS)

makefile.msvc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#Makefile for MSVC by Tom St Denis
2+
CFLAGS = /W3 /Ox /I.
3+
4+
default: tompoly.lib
5+
6+
OBJECTS = pb_init.obj pb_clear.obj pb_init_size.obj pb_grow.obj pb_copy.obj pb_clamp.obj pb_init_copy.obj \
7+
pb_add.obj pb_sub.obj pb_mul.obj pb_div.obj pb_zero.obj pb_lshd.obj pb_rshd.obj pb_exch.obj pb_mod.obj \
8+
pb_addmod.obj pb_submod.obj pb_mulmod.obj pb_gcd.obj pb_init_multi.obj pb_clear_multi.obj pb_invmod.obj \
9+
pb_cmp.obj pb_shrink.obj pb_exteuclid.obj pb_monic.obj pb_exptmod.obj pb_isirreduc.obj
10+
11+
tompoly.lib: $(OBJECTS)
12+
lib /out:tompoly.lib $(OBJECTS)
13+
14+
demo: demo/demo.obj tompoly.lib
15+
cl demo.obj tompoly.lib tommath.lib
16+

pb.pdf

15.7 KB
Binary file not shown.

pb.tex

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
\begin{document}
5050
\frontmatter
5151
\pagestyle{empty}
52-
\title{LibTomPoly User Manual \\ v0.01}
52+
\title{LibTomPoly User Manual \\ v0.02}
5353
\author{Tom St Denis \\ tomstdenis@iahu.ca}
5454
\maketitle
5555
This text and library are hereby placed in the public domain. This book has been
@@ -291,7 +291,12 @@ \section{Multiplying and Dividing by $x$}
291291
int pb_rshd(pb_poly *a, int i);
292292
\end{alltt}
293293
These will multiply (or divide, respectfully) the polynomial ``a'' by $x^i$. If $i \le 0$ the functions return without
294-
performing any operation.
294+
performing any operation. For example,
295+
296+
\begin{alltt}
297+
pb_lshd(a, 2); /* a(x) = a(x) * x^2 */
298+
pb_rshd(a, 7); /* a(x) = a(x) / x^7 */
299+
\end{alltt}
295300

296301
\chapter{Basic Arithmetic}
297302
\section{Addition, Subtraction and Multiplication}
@@ -319,9 +324,14 @@ \section{Division}
319324
\begin{alltt}
320325
int pb_div(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d);
321326
\end{alltt}
322-
This will divide the polynomial ``a'' by ``b'' and store the quotient in ``c'' and remainder in ``d''. Either of
323-
``c'' and ``d'' can be set to \textbf{NULL} to signify their value is not desired. This is useful if you only want the
324-
quotient or remainder but not both.
327+
This will divide the polynomial ``a'' by ``b'' and store the quotient in ``c'' and remainder in ``d''. That is
328+
329+
\begin{equation}
330+
b(x) \cdot c(x) + d(x) = a(x)
331+
\end{equation}
332+
333+
The value of $deg(d(x))$ is always less than $deg(b(x))$. Either of ``c'' and ``d'' can be set to \textbf{NULL} to
334+
signify their value is not desired. This is useful if you only want the quotient or remainder but not both.
325335

326336
Since one of the destinations can be \textbf{NULL} the characteristic of the result is taken from ``b''. The function
327337
will return an error if the characteristic of ``a'' differs from that of ``b''.
@@ -341,6 +351,37 @@ \section{Modular Functions}
341351
and store the result in the polynomial ``d''.
342352

343353
\chapter{Algebraic Functions}
354+
355+
\section{Monic Reductions}
356+
\index{pb\_monic}
357+
\begin{alltt}
358+
int pb_monic(pb_poly *a, pb_poly *b)
359+
\end{alltt}
360+
Makes ``b'' the monic representation of ``a'' by ensuring the most significant coefficient is one. Only defined
361+
over $GF(p)[x]$. Note that this is not a straight copy to ``b'' so you must ensure the characteristic of the two
362+
are equal before you call the function\footnote{Note that $a == b$ is acceptable as well.}. Monic polynomials
363+
are related to their original polynomial through an integer $k$ as follows
364+
365+
\begin{equation}
366+
a(x) \cdot k^{-1} \equiv b(x)
367+
\end{equation}
368+
369+
\section{Extended Euclidean Algorithm}
370+
\index{pb\_exteuclid}
371+
\begin{alltt}
372+
int pb_exteuclid(pb_poly *a, pb_poly *b,
373+
pb_poly *U1, pb_poly *U2, pb_poly *U3);
374+
\end{alltt}
375+
376+
This will compute the Euclidean algorithm and find values ``U1'', ``U2'', ``U3'' such that
377+
378+
\begin{equation}
379+
a(x) \cdot U1(x) + b(x) \cdot U2(x) = U3(x)
380+
\end{equation}
381+
382+
The value of ``U3'' is reduced to a monic polynomial. The three destination variables are all optional and can
383+
be specified as \textbf{NULL} if they are not desired.
384+
344385
\section{Greatest Common Divisor}
345386
\index{pb\_gcd}
346387
\begin{alltt}
@@ -355,7 +396,33 @@ \section{Modular Inverse}
355396
int pb_invmod(pb_poly *a, pb_poly *b, pb_poly *c);
356397
\end{alltt}
357398
This finds the modular inverse of ``a'' modulo ``b'' and stores the result in ``c''. The operation is only defined over
358-
$GF(p)[x]$. If the operation succeed then the congruent $a(x)b(x) \equiv 1 \mbox{ (mod }c(x)\mbox{)}$ should hold true.
399+
$GF(p)[x]$. If the operation succeed then the following congruency should hold true.
400+
401+
\begin{equation}
402+
a(x)c(x) \equiv 1 \mbox{ (mod }b(x)\mbox{)}
403+
\end{equation}
404+
405+
\section{Modular Exponentiation}
406+
\index{pb\_exptmod}
407+
\begin{alltt}
408+
int pb_exptmod (pb_poly * G, mp_int * X, pb_poly * P, pb_poly * Y);
409+
\end{alltt}
410+
411+
This raise ``G'' to the power of ``X'' modulo ``P'' and stores the result in ``Y''. Or as a congruence
412+
413+
\begin{equation}
414+
Y(x) \equiv G(x)^X \mbox{ (mod }P(x)\mbox{)}
415+
\end{equation}
416+
417+
Where ``X'' can be negative\footnote{But in that case $G^{-1}(x)$ must exist modulo $P(x)$.} or positive. This function
418+
is only defined over $GF(p)[x]$.
419+
420+
\section{Irreducibility Testing}
421+
\index{pb\_isirreduc}
422+
\begin{alltt}
423+
int pb_isirreduc(pb_poly *a, int *res);
424+
\end{alltt}
425+
Sets ``res'' to MP\_YES is ``a'' is irreducible (only for $GF(p)[x]$) otherwise sets ``res'' to MP\_NO.
359426

360427
\input{pb.ind}
361428

pb_div.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,14 @@ int pb_div(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d)
9999
/* tmp is now a term of the quotient */
100100
if ((err = mp_copy(&tmp, &(q.terms[x - b->used + 1]))) != MP_OKAY) { goto __TMP2; }
101101

102-
/* create r(x) = C * x^k */
102+
/* create r(x) = C */
103103
pb_zero(&r);
104104
if ((err = mp_copy(&tmp, &(r.terms[0]))) != MP_OKAY) { goto __TMP2; }
105105
r.used = 1;
106-
if ((err = pb_lshd(&r, x - b->used + 1)) != MP_OKAY) { goto __TMP2; }
107106

108-
/* now multiply r(x) by b(x) and subtract from p(x) */
107+
/* now multiply r(x) by b(x)*x^k and subtract from p(x) */
109108
if ((err = pb_mul(b, &r, &r)) != MP_OKAY) { goto __TMP2; }
110-
109+
if ((err = pb_lshd(&r, x - b->used + 1)) != MP_OKAY) { goto __TMP2; }
111110
if ((err = pb_sub(&p, &r, &p)) != MP_OKAY) { goto __TMP2; }
112111
}
113112

0 commit comments

Comments
 (0)