Subversion Repositories planix.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 33
Line 6... Line 6...
6
/*
6
/*
7
 * the code assumes mpdigit to be at least an int
7
 * the code assumes mpdigit to be at least an int
8
 * mpdigit must be an atomic type.  mpdigit is defined
8
 * mpdigit must be an atomic type.  mpdigit is defined
9
 * in the architecture specific u.h
9
 * in the architecture specific u.h
10
 */
10
 */
11
 
-
 
12
typedef struct mpint mpint;
11
typedef struct mpint mpint;
13
 
12
 
14
struct mpint
13
struct mpint
15
{
14
{
16
	int	sign;	/* +1 or -1 */
15
	int	sign;	/* +1 or -1 */
Line 20... Line 19...
20
	char	flags;
19
	char	flags;
21
};
20
};
22
 
21
 
23
enum
22
enum
24
{
23
{
25
	MPstatic=	0x01,
24
	MPstatic=	0x01,	/* static constant */
-
 
25
	MPnorm=		0x02,	/* normalization status */
-
 
26
	MPtimesafe=	0x04,	/* request time invariant computation */
-
 
27
	MPfield=	0x08,	/* this mpint is a field modulus */
-
 
28
 
26
	Dbytes=		sizeof(mpdigit),	/* bytes per digit */
29
	Dbytes=		sizeof(mpdigit),	/* bytes per digit */
27
	Dbits=		Dbytes*8		/* bits per digit */
30
	Dbits=		Dbytes*8		/* bits per digit */
28
};
31
};
29
 
32
 
30
/* allocation */
33
/* allocation */
31
void	mpsetminbits(int n);	/* newly created mpint's get at least n bits */
34
void	mpsetminbits(int n);	/* newly created mpint's get at least n bits */
32
mpint*	mpnew(int n);		/* create a new mpint with at least n bits */
35
mpint*	mpnew(int n);		/* create a new mpint with at least n bits */
33
void	mpfree(mpint *b);
36
void	mpfree(mpint *b);
34
void	mpbits(mpint *b, int n);	/* ensure that b has at least n bits */
37
void	mpbits(mpint *b, int n);	/* ensure that b has at least n bits */
35
void	mpnorm(mpint *b);		/* dump leading zeros */
38
mpint*	mpnorm(mpint *b);		/* dump leading zeros */
36
mpint*	mpcopy(mpint *b);
39
mpint*	mpcopy(mpint *b);
37
void	mpassign(mpint *old, mpint *new);
40
void	mpassign(mpint *old, mpint *new);
38
 
41
 
39
/* random bits */
42
/* random bits */
40
mpint*	mprand(int bits, void (*gen)(uchar*, int), mpint *b);
43
mpint*	mprand(int bits, void (*gen)(uchar*, int), mpint *b);
-
 
44
/* return uniform random [0..n-1] */
-
 
45
mpint*	mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b);
41
 
46
 
42
/* conversion */
47
/* conversion */
43
mpint*	strtomp(char*, char**, int, mpint*);	/* ascii */
48
mpint*	strtomp(char*, char**, int, mpint*);	/* ascii */
44
int	mpfmt(Fmt*);
49
int	mpfmt(Fmt*);
45
char*	mptoa(mpint*, int, char*, int);
50
char*	mptoa(mpint*, int, char*, int);
46
mpint*	letomp(uchar*, uint, mpint*);	/* byte array, little-endian */
51
mpint*	letomp(uchar*, uint, mpint*);	/* byte array, little-endian */
47
int	mptole(mpint*, uchar*, uint, uchar**);
52
int	mptole(mpint*, uchar*, uint, uchar**);
-
 
53
void	mptolel(mpint *b, uchar *p, int n);
48
mpint*	betomp(uchar*, uint, mpint*);	/* byte array, little-endian */
54
mpint*	betomp(uchar*, uint, mpint*);	/* byte array, big-endian */
49
int	mptobe(mpint*, uchar*, uint, uchar**);
55
int	mptobe(mpint*, uchar*, uint, uchar**);
-
 
56
void	mptober(mpint *b, uchar *p, int n);
50
uint	mptoui(mpint*);			/* unsigned int */
57
uint	mptoui(mpint*);			/* unsigned int */
51
mpint*	uitomp(uint, mpint*);
58
mpint*	uitomp(uint, mpint*);
52
int	mptoi(mpint*);			/* int */
59
int	mptoi(mpint*);			/* int */
53
mpint*	itomp(int, mpint*);
60
mpint*	itomp(int, mpint*);
54
uvlong	mptouv(mpint*);			/* unsigned vlong */
61
uvlong	mptouv(mpint*);			/* unsigned vlong */
55
mpint*	uvtomp(uvlong, mpint*);
62
mpint*	uvtomp(uvlong, mpint*);
56
vlong	mptov(mpint*);			/* vlong */
63
vlong	mptov(mpint*);			/* vlong */
57
mpint*	vtomp(vlong, mpint*);
64
mpint*	vtomp(vlong, mpint*);
-
 
65
double	mptod(mpint*);			/* double */
-
 
66
mpint*	dtomp(double, mpint*);
58
 
67
 
59
/* divide 2 digits by one */
68
/* divide 2 digits by one */
60
void	mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
69
void	mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
61
 
70
 
62
/* in the following, the result mpint may be */
71
/* in the following, the result mpint may be */
63
/* the same as one of the inputs. */
72
/* the same as one of the inputs. */
64
void	mpadd(mpint *b1, mpint *b2, mpint *sum);	/* sum = b1+b2 */
73
void	mpadd(mpint *b1, mpint *b2, mpint *sum);	/* sum = b1+b2 */
65
void	mpsub(mpint *b1, mpint *b2, mpint *diff);	/* diff = b1-b2 */
74
void	mpsub(mpint *b1, mpint *b2, mpint *diff);	/* diff = b1-b2 */
66
void	mpleft(mpint *b, int shift, mpint *res);	/* res = b<<shift */
75
void	mpleft(mpint *b, int shift, mpint *res);	/* res = b<<shift */
67
void	mpright(mpint *b, int shift, mpint *res);	/* res = b>>shift */
76
void	mpright(mpint *b, int shift, mpint *res);	/* res = b>>shift */
68
void	mpmul(mpint *b1, mpint *b2, mpint *prod);	/* prod = b1*b2 */
77
void	mpmul(mpint *b1, mpint *b2, mpint *prod);	/* prod = b1*b2 */
69
void	mpexp(mpint *b, mpint *e, mpint *m, mpint *res);	/* res = b**e mod m */
78
void	mpexp(mpint *b, mpint *e, mpint *m, mpint *res);	/* res = b**e mod m */
70
void	mpmod(mpint *b, mpint *m, mpint *remainder);	/* remainder = b mod m */
79
void	mpmod(mpint *b, mpint *m, mpint *remainder);	/* remainder = b mod m */
-
 
80
 
-
 
81
/* logical operations */
-
 
82
void	mpand(mpint *b1, mpint *b2, mpint *res);
-
 
83
void	mpbic(mpint *b1, mpint *b2, mpint *res);
-
 
84
void	mpor(mpint *b1, mpint *b2, mpint *res);
-
 
85
void	mpnot(mpint *b, mpint *res);
-
 
86
void	mpxor(mpint *b1, mpint *b2, mpint *res);
-
 
87
void	mptrunc(mpint *b, int n, mpint *res);
-
 
88
void	mpxtend(mpint *b, int n, mpint *res);
-
 
89
void	mpasr(mpint *b, int shift, mpint *res);
-
 
90
 
-
 
91
/* modular arithmetic, time invariant when 0≤b1≤m-1 and 0≤b2≤m-1 */
-
 
92
void	mpmodadd(mpint *b1, mpint *b2, mpint *m, mpint *sum);	/* sum = b1+b2 % m */
-
 
93
void	mpmodsub(mpint *b1, mpint *b2, mpint *m, mpint *diff);	/* diff = b1-b2 % m */
-
 
94
void	mpmodmul(mpint *b1, mpint *b2, mpint *m, mpint *prod);	/* prod = b1*b2 % m */
71
 
95
 
72
/* quotient = dividend/divisor, remainder = dividend % divisor */
96
/* quotient = dividend/divisor, remainder = dividend % divisor */
73
void	mpdiv(mpint *dividend, mpint *divisor,  mpint *quotient, mpint *remainder);
97
void	mpdiv(mpint *dividend, mpint *divisor,  mpint *quotient, mpint *remainder);
74
 
98
 
75
/* return neg, 0, pos as b1-b2 is neg, 0, pos */
99
/* return neg, 0, pos as b1-b2 is neg, 0, pos */
76
int	mpcmp(mpint *b1, mpint *b2);
100
int	mpcmp(mpint *b1, mpint *b2);
-
 
101
 
-
 
102
/* res = s != 0 ? b1 : b2 */
-
 
103
void	mpsel(int s, mpint *b1, mpint *b2, mpint *res);
-
 
104
 
-
 
105
/* return n! */
-
 
106
mpint*	mpfactorial(ulong n);
77
 
107
 
78
/* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
108
/* extended gcd return d, x, and y, s.t. d = gcd(a,b) and ax+by = d */
79
void	mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
109
void	mpextendedgcd(mpint *a, mpint *b, mpint *d, mpint *x, mpint *y);
80
 
110
 
81
/* res = b**-1 mod m */
111
/* res = b**-1 mod m */
Line 102... Line 132...
102
 
132
 
103
/* p[0:n] -= m * b[0:n-1] */
133
/* p[0:n] -= m * b[0:n-1] */
104
/* prereq: p has room for n+1 digits */
134
/* prereq: p has room for n+1 digits */
105
int	mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
135
int	mpvecdigmulsub(mpdigit *b, int n, mpdigit m, mpdigit *p);
106
 
136
 
107
/* p[0:alen*blen-1] = a[0:alen-1] * b[0:blen-1] */
137
/* p[0:alen+blen-1] = a[0:alen-1] * b[0:blen-1] */
108
/* prereq: alen >= blen, p has room for m*n digits */
138
/* prereq: alen >= blen, p has room for m*n digits */
109
void	mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
139
void	mpvecmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
-
 
140
void	mpvectsmul(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *p);
110
 
141
 
111
/* sign of a - b or zero if the same */
142
/* sign of a - b or zero if the same */
112
int	mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
143
int	mpveccmp(mpdigit *a, int alen, mpdigit *b, int blen);
-
 
144
int	mpvectscmp(mpdigit *a, int alen, mpdigit *b, int blen);
113
 
145
 
114
/* divide the 2 digit dividend by the one digit divisor and stick in quotient */
146
/* divide the 2 digit dividend by the one digit divisor and stick in quotient */
115
/* we assume that the result is one digit - overflow is all 1's */
147
/* we assume that the result is one digit - overflow is all 1's */
116
void	mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
148
void	mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient);
117
 
149
 
Line 137... Line 169...
137
CRTres*	crtin(CRTpre*, mpint*);			/* convert mpint to residues */
169
CRTres*	crtin(CRTpre*, mpint*);			/* convert mpint to residues */
138
void	crtout(CRTpre*, CRTres*, mpint*);	/* convert residues to mpint */
170
void	crtout(CRTpre*, CRTres*, mpint*);	/* convert residues to mpint */
139
void	crtprefree(CRTpre*);
171
void	crtprefree(CRTpre*);
140
void	crtresfree(CRTres*);
172
void	crtresfree(CRTres*);
141
 
173
 
-
 
174
/* fast field arithmetic */
-
 
175
typedef struct Mfield	Mfield;
-
 
176
 
-
 
177
struct Mfield
-
 
178
{
-
 
179
	mpint;
-
 
180
	int	(*reduce)(Mfield*, mpint*, mpint*);
-
 
181
};
-
 
182
 
-
 
183
mpint *mpfield(mpint*);
-
 
184
 
-
 
185
Mfield *gmfield(mpint*);
-
 
186
Mfield *cnfield(mpint*);
142
 
187
 
143
#pragma	varargck	type	"B"	mpint*
188
#pragma	varargck	type	"B"	mpint*