-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtompoly.h
124 lines (90 loc) · 3.52 KB
/
tompoly.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* LibTomPoly, Polynomial Basis Math -- Tom St Denis
*
* LibTomPoly is a public domain library that provides
* polynomial basis arithmetic support. It relies on
* LibTomMath for large integer support.
*
* This library is free for all purposes without any
* express guarantee that it works.
*
* Tom St Denis, [email protected], http://poly.libtomcrypt.org
*/
#ifndef TOMPOLY_H_
#define TOMPOLY_H_
#include <tommath.h>
/* this structure holds a polynomial */
typedef struct {
int used, /* number of terms */
alloc; /* number of terms available (total) */
mp_int characteristic, /* characteristic, zero if not finite */
*terms; /* terms of polynomial */
} pb_poly;
/* default number of terms */
#define PB_TERMS 4
/* Compare codes */
#define PB_EQ 0 /* They're exactly equal */
#define PB_DEG_LT 1 /* The left has a lower degree */
#define PB_DEG_EQ 2 /* same degree */
#define PB_DEG_GT 3 /* The left has a higher degree */
int pb_init(pb_poly *a, mp_int *characteristic);
int pb_init_size(pb_poly *a, mp_int *characteristic, int size);
int pb_init_copy(pb_poly *a, pb_poly *b);
int pb_init_multi(mp_int *characteristic, pb_poly *pb, ...);
void pb_clear_multi(pb_poly *mp, ...);
void pb_clear(pb_poly *a);
int pb_shrink(pb_poly *a);
int pb_grow(pb_poly *a, int size);
void pb_clamp(pb_poly *a);
/* dest(x) := src(x) */
int pb_copy(pb_poly *src, pb_poly *dest);
/* compare these */
int pb_cmp(pb_poly *a, pb_poly *b);
/* swap contents of a(x) and b(x) */
void pb_exch(pb_poly *a, pb_poly *b);
/* a(x) = 0 */
void pb_zero(pb_poly *a);
/* a(x) = a(x) / I(x)^x */
int pb_rshd(pb_poly *a, int x);
/* a(x) = a(x) * I(x)^x */
int pb_lshd(pb_poly *a, int x);
/* c(x) = a(x) + b(x) */
int pb_add(pb_poly *a, pb_poly *b, pb_poly *c);
/* c(x) = a(x) - b(x) */
int pb_sub(pb_poly *a, pb_poly *b, pb_poly *c);
/* c(x) = a(x) * b(x) */
int pb_mul(pb_poly *a, pb_poly *b, pb_poly *c);
/* c(x) * b(x) + d(x) = a(x) */
int pb_div(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d);
/* c(x) = a(x) mod b(x) */
int pb_mod(pb_poly *a, pb_poly *b, pb_poly *c);
/* d(x) = (a(x) + b(x)) mod c(x) */
int pb_addmod(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d);
/* d(x) = (a(x) - b(x)) mod c(x) */
int pb_submod(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d);
/* d(x) = (a(x) * b(x)) mod c(x) */
int pb_mulmod(pb_poly *a, pb_poly *b, pb_poly *c, pb_poly *d);
/* mathy stuff */
/* makes b equal to the monic polynomial form of a */
int pb_monic(pb_poly *a, pb_poly *b);
/* returns the monic GCD of a,b in GF(p^k)[x] */
int pb_gcd(pb_poly *a, pb_poly *b, pb_poly *c);
/* Extended euclidean algorithm of (a, b) produces a*u1 + b*u2 = u3 */
int pb_exteuclid(pb_poly *a, pb_poly *b, pb_poly *U1, pb_poly *U2, pb_poly *U3);
/* finds the inverse of a modulo b and stores it in c such that a*c == 1 mod b */
int pb_invmod(pb_poly *a, pb_poly *b, pb_poly *c);
/* computes Y == G^X mod P [accepts negative values for X] */
int pb_exptmod (pb_poly * G, mp_int * X, pb_poly * P, pb_poly * Y);
/* is a(x) irreducible (GF(p)[x] only) */
int pb_isirreduc(pb_poly *a, int *res);
/* I/O */
int pb_rawsize(pb_poly *a);
int pb_toraw(pb_poly *a, unsigned char *dst);
int pb_readraw(pb_poly *a, unsigned char *buf, int len);
/* What follows should be in a private header, but it's fine for now like that. */
#ifndef PB_MIN
#define PB_MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#ifndef PB_MAX
#define PB_MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
#endif