]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_cryptkit/lib/giantIntegers.h
1 /* Copyright (c) 1998,2011-2012,2014 Apple Inc. All Rights Reserved.
3 * NOTICE: USE OF THE MATERIALS ACCOMPANYING THIS NOTICE IS SUBJECT
4 * TO THE TERMS OF THE SIGNED "FAST ELLIPTIC ENCRYPTION (FEE) REFERENCE
5 * SOURCE CODE EVALUATION AGREEMENT" BETWEEN APPLE, INC. AND THE
6 * ORIGINAL LICENSEE THAT OBTAINED THESE MATERIALS FROM APPLE,
7 * INC. ANY USE OF THESE MATERIALS NOT PERMITTED BY SUCH AGREEMENT WILL
8 * EXPOSE YOU TO LIABILITY.
9 ***************************************************************************
11 * giantIntegers.h - large-integer arithmetic library.
16 * Default "unsigned int" giantDigit for __i386__ and __i486__
18 * Changed size of giantstruct.n to 1 for Mac build
22 #ifndef _CK_NSGIANTINTS_H_
23 #define _CK_NSGIANTINTS_H_
25 #include <security_cryptkit/ckconfig.h>
32 * Size of giant digit.
34 #if NeXT || __i386__ || __i486__ || __x86_64__
36 typedef unsigned int giantDigit
;
39 * used to divide by GIANT_BITS_PER_DIGIT via shift - no easy way to get
40 * the compiler to calculate this.
42 #define GIANT_LOG2_BITS_PER_DIGIT 5
44 #elif defined(macintosh) || defined(__ppc__)
46 typedef unsigned int giantDigit
;
47 #define GIANT_LOG2_BITS_PER_DIGIT 5
51 typedef unsigned short giantDigit
;
52 #define GIANT_LOG2_BITS_PER_DIGIT 4
56 /* platform-independent digit manipulation macros */
58 #define GIANT_BYTES_PER_DIGIT (sizeof(giantDigit))
59 #define GIANT_BITS_PER_DIGIT (8 * GIANT_BYTES_PER_DIGIT)
60 #define GIANT_DIGIT_MASK ((giantDigit)~0)
61 #define BYTES_TO_GIANT_DIGITS(x) \
62 ((x + GIANT_BYTES_PER_DIGIT - 1) / GIANT_BYTES_PER_DIGIT)
64 #define MAX_DIGITS ((1<<18)+(1<<17))
65 /* 2^(16*MAX_DIGITS)-1 will fit into a giant. */
68 int sign
; /* number of giantDigits = abs(sign) */
69 unsigned capacity
; /* largest possible number of giantDigits */
70 giantDigit n
[1]; /* n[0] is l.s. digit */
72 typedef giantstruct
*giant
;
74 giant
newGiant(unsigned numDigits
);
75 giant
copyGiant(giant x
);
76 void freeGiant(giant x
);
78 giant
borrowGiant(unsigned numDigits
); /* get a temporary */
79 void returnGiant(giant
); /* return it */
80 unsigned bitlen(giant n
); /* Returns the bit-length n;
81 * e.g. n=7 returns 3. */
82 int bitval(giant n
, int pos
); /* Returns the value of bit pos of n */
83 int isZero(giant g
); /* Returns whether g is zero */
84 int isone(giant g
); /* Returns whether g is 1 */
85 void gtog(giant src
, giant dest
); /* Copies one giant to another */
86 void int_to_giant(int n
, giant g
); /* Gives a giant an int value */
87 int gcompg(giant a
, giant b
); /* Returns 1, 0, -1 as a>b, a=b, a<b */
88 void addg(giant a
, giant b
); /* b += a */
89 void iaddg(int a
, giant b
); /* b += a */
90 void subg(giant a
, giant b
); /* b -= a. */
91 void imulg(unsigned n
, giant g
); /* g *= n */
92 void negg(giant g
); /* g := -g. */
93 int binvg(giant n
, giant x
); /* Same as invg(), but uses binary
95 int binvaux(giant p
, giant x
);
96 void gmersennemod(int n
, giant g
); /* g := g (mod 2^n-1). */
97 void gshiftleft(int bits
, giant g
); /* Shift g left by bits, introducing
98 * zeros on the right. */
99 void gshiftright(int bits
, giant g
); /* Shift g right by bits, losing bits
101 void extractbits(unsigned n
, giant src
, giant dest
);
102 /* dest becomes lowermost n bits of
104 * dest = src % 2^n */
106 void grammarSquare(giant a
); /* g *= g. */
107 #define gsquare(g) grammarSquare(g)
109 void mulg(giant a
, giant b
); /* b *= a. */
110 int gsign(giant g
); /* Returns the sign of g: -1, 0, 1. */
111 void gtrimSign(giant g
); /* Adjust sign for possible leading
112 * (m.s.) zero digits */
114 void divg(giant d
, giant n
); /* n becomes |n|/d. n is arbitrary,
115 * but the denominator d must be
117 int scompg(int n
, giant g
);
118 void modg(giant den
, giant num
); /* num := num mod den, any positive
120 void clearGiant(giant g
); /* zero a giant's data */
123 * Optimized modg and divg, with routine to calculate necessary reciprocal
125 void make_recip(giant d
, giant r
);
126 void divg_via_recip(giant denom
, giant recip
, giant numer
);
127 /* numer := |n|/d. */
128 void modg_via_recip(giant denom
, giant recip
, giant numer
);
129 /* num := num mod den */
135 #endif /* _CK_NSGIANTINTS_H_ */