]>
git.saurik.com Git - apple/security.git/blob - OSX/include/security_cryptkit/CurveParamDocs/giants.h
1 /**************************************************************
5 * Header file for large-integer arithmetic library giants.c.
8 * 18 Jul 99 REC Added fer_mod().
9 * 30 Apr 98 JF USE_ASSEMBLER_MUL removed
10 * 29 Apr 98 JF Function prototypes cleaned up
13 * c. 1997 Perfectly Scientific, Inc.
14 * All Rights Reserved.
16 **************************************************************/
19 /**************************************************************
23 **************************************************************/
25 #define DIVIDEBYZERO 1
34 /**************************************************************
36 * Preprocessor definitions
38 **************************************************************/
40 /* 2^(16*MAX_SHORTS)-1 will fit into a giant, but take care:
41 * one usually has squares, etc. of giants involved, and
42 * every intermediate giant in a calculation must fit into
43 * this many shorts. Thus, if you want systematically to effect
44 * arithmetic on B-bit operands, you need MAX_SHORTS > B/8,
45 * perferably a tad larger than this; e.g. MAX_SHORTS > B/7.
47 #define MAX_SHORTS (1<<19)
52 #define COLUMNWIDTH 64
54 #define TWOPI (double)(2*3.1415926535897932384626433)
55 #define SQRT2 (double)(1.414213562373095048801688724209)
56 #define SQRTHALF (double)(0.707106781186547524400844362104)
57 #define TWO16 (double)(65536.0)
58 #define TWOM16 (double)(0.0000152587890625)
60 /* Decimal digit ceiling in digit-input routines. */
61 #define MAX_DIGITS 10000
63 /* Next, mumber of shorts per operand
64 at which Karatsuba breaks over. */
65 #define KARAT_BREAK 40
67 /* Next, mumber of shorts per operand
68 at which FFT breaks over. */
71 #define newmin(a,b) ((a)<(b)? (a) : (b))
72 #define newmax(a,b) ((a)>(b)? (a) : (b))
74 /* Maximum number of recursive steps needed to calculate
75 * gcds of integers. */
78 /* The limit below which hgcd is too ponderous */
81 /* The limit below which ordinary ints will be used */
84 /* Size by which to increment the stack used in pushg() and popg(). */
87 #define gin(x) gread(x,stdin)
88 #define gout(x) gwriteln(x,stdout)
91 /**************************************************************
93 * Structure definitions
95 **************************************************************/
100 unsigned short n
[1]; /* number of shorts = abs(sign) */
103 typedef giantstruct
*giant
;
105 typedef struct _matrix
107 giant ul
; /* upper left */
108 giant ur
; /* upper right */
109 giant ll
; /* lower left */
110 giant lr
; /* lower right */
120 /**************************************************************
122 * Function Prototypes
124 **************************************************************/
126 /**************************************************************
128 * Initialization and utility functions
130 **************************************************************/
133 void init_sinCos(int);
138 /* Creates a new giant, numshorts = INFINITY invokes the
139 * maximum MAX_SHORTS. */
140 giant
newgiant(int numshorts
);
142 /* Creates a new giant matrix, but does not malloc
143 * the component giants. */
144 gmatrix
newgmatrix(void);
146 /* Returns the bit-length n; e.g. n=7 returns 3. */
149 /* Returns the value of the pos bit of n. */
150 int bitval(giant n
, int pos
);
152 /* Returns whether g is one. */
155 /* Returns whether g is zero. */
158 /* Copies one giant to another. */
159 void gtog(giant src
, giant dest
);
161 /* Integer <-> giant. */
162 void itog(int n
, giant g
);
163 signed int gtoi (giant
);
165 /* Returns the sign of g: -1, 0, 1. */
168 /* Returns 1, 0, -1 as a>b, a=b, a<b. */
169 int gcompg(giant a
, giant b
);
171 /* Set AUTO_MUL for automatic FFT crossover (this is the
172 * default), set FFT_MUL for forced FFT multiply, set
173 * GRAMMAR_MUL for forced grammar school multiply. */
174 void setmulmode(int mode
);
176 /**************************************************************
180 **************************************************************/
182 /* Output the giant in decimal, with optional newlines. */
183 void gwrite(giant g
, FILE *fp
, int newlines
);
185 /* Output the giant in decimal, with both '\'-newline
186 * notation and a final newline. */
187 void gwriteln(giant g
, FILE *fp
);
189 /* Input the giant in decimal, assuming the formatting of
191 void gread(giant g
, FILE *fp
);
193 /**************************************************************
197 **************************************************************/
205 /* g += i, with i non-negative and < 2^16. */
206 void iaddg(int i
,giant g
);
209 void addg(giant a
, giant b
);
212 void subg(giant a
, giant b
);
214 /* Returns the number of trailing zero bits in g. */
215 int numtrailzeros(giant g
);
217 /* u becomes greatest power of two not exceeding u/v. */
218 void bdivg(giant v
, giant u
);
220 /* Same as invg, but uses bdivg. */
221 int binvg(giant n
, giant x
);
223 /* If 1/x exists (mod n), 1 is returned and x := 1/x. If
224 * inverse does not exist, 0 is returned and x := GCD(n, x). */
225 int invg(giant n
, giant x
);
227 int mersenneinvg(int q
, giant x
);
229 /* Classical GCD, x:= GCD(n, x). */
230 void cgcdg(giant n
, giant x
);
232 /* General GCD, x:= GCD(n, x). */
233 void gcdg(giant n
, giant x
);
235 /* Binary GCD, x:= GCD(n, x). */
236 void bgcdg(giant n
, giant x
);
238 /* g := m^n, no mod is performed. */
239 void powerg(int a
, int b
, giant g
);
241 /* r becomes the steady-state reciprocal 2^(2b)/d, where
242 * b = bit-length of d-1. */
243 void make_recip(giant d
, giant r
);
245 /* n := [n/d], d positive, using stored reciprocal directly. */
246 void divg_via_recip(giant d
, giant r
, giant n
);
248 /* n := n % d, d positive, using stored reciprocal directly. */
249 void modg_via_recip(giant d
, giant r
, giant n
);
251 /* num := num % den, any positive den. */
252 void modg(giant den
, giant num
);
254 /* a becomes (a*b) (mod 2^q-k) where q % 16 == 0 and k is "small"
255 * (0 < k < 65535). Returns 0 if unsuccessful, otherwise 1. */
256 int feemulmod(giant x
, giant y
, int q
, int k
);
258 /* g := g/n, and (g mod n) is returned. */
259 int idivg(int n
, giant g
);
261 /* num := [num/den], any positive den. */
262 void divg(giant den
, giant num
);
264 /* num := [num/den], any positive den. */
265 void powermod(giant x
, int n
, giant z
);
267 /* x := x^n (mod z). */
268 void powermodg(giant x
, giant n
, giant z
);
270 /* x := x^n (mod 2^q+1). */
271 void fermatpowermod(giant x
, int n
, int q
);
273 /* x := x^n (mod 2^q+1). */
274 void fermatpowermodg(giant x
, giant n
, int q
);
276 /* x := x^n (mod 2^q-1). */
277 void mersennepowermod(giant x
, int n
, int q
);
279 /* x := x^n (mod 2^q-1). */
280 void mersennepowermodg(giant x
, giant n
, int q
);
282 /* Shift g left by bits, introducing zeros on the right. */
283 void gshiftleft(int bits
, giant g
);
285 /* Shift g right by bits, losing bits on the right. */
286 void gshiftright(int bits
, giant g
);
288 /* dest becomes lowermost n bits of src.
289 * Equivalent to dest = src % 2^n. */
290 void extractbits(int n
, giant src
, giant dest
);
292 /* negate g. g is mod 2^n+1. */
293 void fermatnegate(int n
, giant g
);
295 /* g := g (mod 2^n-1). */
296 void mersennemod(int n
, giant g
);
298 /* g := g (mod 2^n+1). */
299 void fermatmod(int n
, giant g
);
301 /* g := g (mod 2^n+1). */
302 void fer_mod(int n
, giant g
, giant modulus
);
305 void smulg(unsigned short s
, giant g
);
308 void squareg(giant g
);
311 void mulg(giant a
, giant b
);
313 /* A giant gcd. Modifies its arguments. */
314 void ggcd(giant xx
, giant yy
);