Skip to content

Commit 03749f6

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtompoly-0.03
1 parent 7b08266 commit 03749f6

File tree

14 files changed

+150
-37
lines changed

14 files changed

+150
-37
lines changed

TODO

Lines changed: 0 additions & 13 deletions
This file was deleted.

WARNING

Lines changed: 0 additions & 8 deletions
This file was deleted.

changes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Jan 25th, 2004
2+
v0.03 - Added pb_rawsize(), pb_readraw(), pb_toraw()
3+
- Fixed bug in pb_monic() where it would only check the first [not leading] coefficient for one
4+
15
Jan 3rd, 2004
26
v0.02 - Update pb_div() to shift r(x) after multplying it wit b(x) to save a bit of time
37
- improved pb_gcd() to handle inputs which are zero

makefile

Lines changed: 3 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.02
4+
VERSION=0.03
55

66
#default files to install
77
LIBNAME=libtompoly.a
@@ -20,7 +20,8 @@ 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 pb_shrink.o pb_exteuclid.o pb_monic.o pb_exptmod.o pb_isirreduc.o
23+
pb_cmp.o pb_shrink.o pb_exteuclid.o pb_monic.o pb_exptmod.o pb_isirreduc.o pb_rawsize.o \
24+
pb_toraw.o pb_readraw.o
2425

2526
libtompoly.a: $(OBJECTS)
2627
ar $(ARFLAGS) libtompoly.a $(OBJECTS)

makefile.msvc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ default: tompoly.lib
66
OBJECTS = pb_init.obj pb_clear.obj pb_init_size.obj pb_grow.obj pb_copy.obj pb_clamp.obj pb_init_copy.obj \
77
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 \
88
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
9+
pb_cmp.obj pb_shrink.obj pb_exteuclid.obj pb_monic.obj pb_exptmod.obj pb_isirreduc.obj pb_rawsize.obj \
10+
pb_toraw.obj pb_readraw.obj
1011

1112
tompoly.lib: $(OBJECTS)
1213
lib /out:tompoly.lib $(OBJECTS)

pb.pdf

95 Bytes
Binary file not shown.

pb.tex

Lines changed: 1 addition & 1 deletion
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.02}
52+
\title{LibTomPoly User Manual \\ v0.03}
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

pb_gcd.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
int pb_gcd(pb_poly *a, pb_poly *b, pb_poly *c)
1616
{
1717
pb_poly A, B, tmp;
18-
mp_int inv;
1918
int err;
2019

2120
if (mp_iszero(&(c->characteristic)) == MP_YES) {
@@ -29,7 +28,6 @@ int pb_gcd(pb_poly *a, pb_poly *b, pb_poly *c)
2928
c->used = 1;
3029
mp_set(&(c->terms[0]), 1);
3130
return MP_OKAY;
32-
3331
} else if (a->used == 0) {
3432
return pb_copy(b, c);
3533
} else if (b->used == 0) {
@@ -45,27 +43,23 @@ int pb_gcd(pb_poly *a, pb_poly *b, pb_poly *c)
4543
if ((err = pb_init_copy(&B, b)) != MP_OKAY) {
4644
goto __A;
4745
}
48-
if ((err = mp_init(&inv)) != MP_OKAY) {
49-
goto __B;
50-
}
5146

5247
while (B.used > 0) {
5348
if ((err = pb_mod(&A, &B, &tmp)) != MP_OKAY) {
54-
goto __INV;
49+
goto __B;
5550
}
5651
/* A = B, B = tmp */
5752
if ((err = pb_copy(&B, &A)) != MP_OKAY) {
58-
goto __INV;
53+
goto __B;
5954
}
6055
if ((err = pb_copy(&tmp, &B)) != MP_OKAY) {
61-
goto __INV;
56+
goto __B;
6257
}
6358
}
6459

6560
/* ensure it's monic */
6661
err = pb_monic(&A, c);
6762

68-
__INV: mp_clear(&inv);
6963
__B : pb_clear(&B);
7064
__A : pb_clear(&A);
7165
__TMP: pb_clear(&tmp);

pb_isirreduc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ int pb_isirreduc(pb_poly *a, int *res)
4242
mp_set(&(tmp.terms[1]), 1); tmp.used = 2;
4343
if ((err = pb_sub(&u, &tmp, &tmp)) != MP_OKAY) { goto _ERR; }
4444

45-
/* d = gcd(u, tmp) */
46-
if ((err = pb_gcd(&fm, &tmp, &d)) != MP_OKAY) { goto _ERR; }
45+
/* d = gcd(fm, tmp) */
46+
if ((err = pb_gcd(&fm, &tmp, &d)) != MP_OKAY) { goto _ERR; }
4747

4848
/* if d != 1 then reducible */
4949
if (d.used > 1) { err = MP_OKAY; goto _ERR; }

pb_monic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int pb_monic(pb_poly *a, pb_poly *b)
2323
}
2424

2525
/* if it's already one just copy */
26-
if (mp_cmp_d(&(a->terms[0]), 1) == MP_EQ) {
26+
if (mp_cmp_d(&(a->terms[a->used - 1]), 1) == MP_EQ) {
2727
return pb_copy(a, b);
2828
}
2929

pb_rawsize.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* LibTomPoly, Polynomial Basis Math -- Tom St Denis
2+
*
3+
* LibTomPoly is a public domain library that provides
4+
* polynomial basis arithmetic support. It relies on
5+
* LibTomMath for large integer support.
6+
*
7+
* This library is free for all purposes without any
8+
* express guarantee that it works.
9+
*
10+
* Tom St Denis, tomstdenis@iahu.ca, http://poly.libtomcrypt.org
11+
*/
12+
#include <tompoly.h>
13+
14+
int pb_rawsize(pb_poly *a)
15+
{
16+
int x, y;
17+
18+
/* 2 bytes for # of coefficients */
19+
y = 2;
20+
21+
/* characteristic */
22+
y += 2 + mp_signed_bin_size(&(a->characteristic));
23+
24+
/* all of the terms */
25+
for (x = 0; x < a->used; x++) {
26+
y += 2 + mp_signed_bin_size(&(a->terms[x]));
27+
}
28+
29+
return y;
30+
}
31+
32+

pb_readraw.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* LibTomPoly, Polynomial Basis Math -- Tom St Denis
2+
*
3+
* LibTomPoly is a public domain library that provides
4+
* polynomial basis arithmetic support. It relies on
5+
* LibTomMath for large integer support.
6+
*
7+
* This library is free for all purposes without any
8+
* express guarantee that it works.
9+
*
10+
* Tom St Denis, tomstdenis@iahu.ca, http://poly.libtomcrypt.org
11+
*/
12+
#include <tompoly.h>
13+
14+
int pb_readraw(pb_poly *a, unsigned char *buf, int len)
15+
{
16+
int terms, x, y, z, err;
17+
18+
/* zero poly */
19+
pb_zero(a);
20+
21+
/* must be at least four bytes */
22+
if (len < 4) {
23+
return MP_VAL;
24+
}
25+
26+
/* number of terms */
27+
terms = ((unsigned)buf[0]) | ((unsigned)buf[1]<<8);
28+
y = 2;
29+
30+
/* grow to the right size */
31+
if (a->alloc < terms) {
32+
if ((err = pb_grow(a, terms)) != MP_OKAY) {
33+
return err;
34+
}
35+
}
36+
37+
/* read characteristic */
38+
z = ((unsigned)buf[y]) | ((unsigned)buf[y+1]<<8); y += 2;
39+
if (z + y > len) { return MP_VAL; }
40+
if ((err = mp_read_signed_bin(&(a->characteristic), buf+y, z)) != MP_OKAY) { return err; }
41+
y += z;
42+
43+
44+
/* read terms */
45+
for (x = 0; x < terms; x++) {
46+
z = ((unsigned)buf[y]) | ((unsigned)buf[y+1]<<8); y += 2;
47+
if (z + y > len) { return MP_VAL; }
48+
if ((err = mp_read_signed_bin(&(a->characteristic), buf+y, z)) != MP_OKAY) { return err; }
49+
y += z;
50+
}
51+
52+
return MP_OKAY;
53+
}
54+
55+

pb_toraw.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* LibTomPoly, Polynomial Basis Math -- Tom St Denis
2+
*
3+
* LibTomPoly is a public domain library that provides
4+
* polynomial basis arithmetic support. It relies on
5+
* LibTomMath for large integer support.
6+
*
7+
* This library is free for all purposes without any
8+
* express guarantee that it works.
9+
*
10+
* Tom St Denis, tomstdenis@iahu.ca, http://poly.libtomcrypt.org
11+
*/
12+
#include <tompoly.h>
13+
14+
int pb_toraw(pb_poly *a, unsigned char *dst)
15+
{
16+
int err, x, y, z;
17+
18+
/* store the # of terms */
19+
dst[0] = a->used & 255;
20+
dst[1] = (a->used >> 8) & 255;
21+
y = 2;
22+
23+
/* store the characteristic */
24+
z = mp_signed_bin_size(&(a->characteristic));
25+
dst[y++] = z&255;
26+
dst[y++] = (z>>8)&255;
27+
if ((err = mp_to_signed_bin(&(a->characteristic), dst+y)) != MP_OKAY) { return err; }
28+
y += z;
29+
30+
for (x = 0; x < a->used; x++) {
31+
z = mp_signed_bin_size(&(a->terms[x]));
32+
dst[y++] = z&255;
33+
dst[y++] = (z>>8)&255;
34+
if ((err = mp_to_signed_bin(&(a->terms[x]), dst+y)) != MP_OKAY) { return err; }
35+
y += z;
36+
}
37+
38+
return MP_OKAY;
39+
}
40+
41+
42+

tompoly.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ int pb_exptmod (pb_poly * G, mp_int * X, pb_poly * P, pb_poly * Y);
107107
int pb_isirreduc(pb_poly *a, int *res);
108108

109109

110+
/* I/O */
111+
int pb_rawsize(pb_poly *a);
112+
int pb_toraw(pb_poly *a, unsigned char *dst);
113+
int pb_readraw(pb_poly *a, unsigned char *buf, int len);
114+
110115
#endif

0 commit comments

Comments
 (0)