Added SILC Thread Queue API
[silc.git] / lib / silcmath / mp_tma.c
1 /*
2
3   mp_tma.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 - 2007 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silc.h"
22 #include "mp_tma.h"
23
24 void silc_mp_init(SilcMPInt *mp)
25 {
26   (void)tma_mp_init(mp);
27 }
28
29 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp)
30 {
31   /* XXX TODO */
32   tma_mp_init(mp);
33   return TRUE;
34 }
35
36 void silc_mp_uninit(SilcMPInt *mp)
37 {
38   tma_mp_clear(mp);
39 }
40
41 void silc_mp_suninit(SilcStack stack, SilcMPInt *mp)
42 {
43   if (!stack)
44     tma_mp_clear(mp);
45 }
46
47 size_t silc_mp_size(SilcMPInt *mp)
48 {
49   return tma_mp_unsigned_bin_size(mp);
50 }
51
52 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base)
53 {
54   int size = 0;
55   tma_mp_radix_size(mp, base, &size);
56   if (size > 1)
57     size--;
58   return size;
59 }
60
61 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src)
62 {
63   (void)tma_mp_copy(src, dst);
64 }
65
66 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui)
67 {
68   (void)tma_mp_set_int(dst, ui);
69 }
70
71 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si)
72 {
73   (void)tma_mp_set_int(dst, si);
74 }
75
76 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base)
77 {
78   (void)tma_mp_read_radix(dst, str, base);
79 }
80
81 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp)
82 {
83   return (SilcUInt32)tma_mp_get_int(mp);
84 }
85
86 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base)
87 {
88   if (tma_mp_toradix(mp, str, base) != MP_OKAY)
89     return NULL;
90   return str;
91 }
92
93 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
94 {
95   (void)tma_mp_add(mp1, mp2, dst);
96 }
97
98 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
99 {
100   tma_mp_add_d(mp1, (tma_mp_digit)ui, dst);
101 }
102
103 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
104 {
105   (void)tma_mp_sub(mp1, mp2, dst);
106 }
107
108 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
109 {
110   (void)tma_mp_sub_d(mp1, (tma_mp_digit)ui, dst);
111 }
112
113 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
114 {
115   (void)tma_mp_mul(mp1, mp2, dst);
116 }
117
118 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
119 {
120   (void)tma_mp_mul_d(mp1, (tma_mp_digit)ui, dst);
121 }
122
123 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
124 {
125   (void)tma_mp_mul_2d(mp1, exp, dst);
126 }
127
128 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src)
129 {
130   (void)tma_mp_sqrt(src, dst);
131 }
132
133 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
134 {
135   (void)tma_mp_div(mp1, mp2, dst, NULL);
136 }
137
138 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
139 {
140   (void)tma_mp_div_d(mp1, (tma_mp_digit)ui, dst, NULL);
141 }
142
143 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
144                     SilcMPInt *mp2)
145 {
146   (void)tma_mp_div(mp1, mp2, q, r);
147 }
148
149 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
150 {
151   (void)tma_mp_div_2d(mp1, exp, dst, NULL);
152 }
153
154 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
155                          SilcUInt32 exp)
156 {
157   (void)tma_mp_div_2d(mp1, exp, q, r);
158 }
159
160 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
161 {
162   (void)tma_mp_mod(mp1, mp2, dst);
163 }
164
165 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
166 {
167   tma_mp_digit d;
168   (void)tma_mp_mod_d(mp1, ui, &d);
169   silc_mp_set_ui(dst, d);
170 }
171
172 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
173 {
174   (void)tma_mp_mod_2d(mp1, ui, dst);
175 }
176
177 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp)
178 {
179   SILC_NOT_IMPLEMENTED("silc_mp_pow");
180   assert(FALSE);
181 }
182
183 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
184 {
185   (void)tma_mp_expt_d(mp1, (tma_mp_digit)exp, dst);
186 }
187
188 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
189                      SilcMPInt *mod)
190 {
191   (void)tma_mp_exptmod(mp1, exp, mod, dst);
192 }
193
194 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
195                         SilcMPInt *mod)
196 {
197   SilcMPInt tmp;
198   silc_mp_init(&tmp);
199   silc_mp_set_ui(&tmp, exp);
200   silc_mp_pow_mod(dst, mp1, &tmp, mod);
201   silc_mp_uninit(&tmp);
202 }
203
204 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
205 {
206   (void)tma_mp_gcd(mp1, mp2, dst);
207 }
208
209 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
210                     SilcMPInt *mp2)
211 {
212   (void)tma_mp_exteuclid(mp1, mp2, s, t, g);
213 }
214
215 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2)
216 {
217   return tma_mp_cmp(mp1, mp2);
218 }
219
220 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si)
221 {
222   return tma_mp_cmp_d(mp1, si);
223 }
224
225 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui)
226 {
227   return tma_mp_cmp_d(mp1, ui);
228 }
229
230 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src)
231 {
232   (void)tma_mp_abs(src, dst);
233 }
234
235 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src)
236 {
237   (void)tma_mp_neg(src, dst);
238 }
239
240 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
241 {
242   (void)tma_mp_and(mp1, mp2, dst);
243 }
244
245 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
246 {
247   (void)tma_mp_or(mp1, mp2, dst);
248 }
249
250 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
251 {
252   (void)tma_mp_xor(mp1, mp2, dst);
253 }