Created SILC Crypto Toolkit git repository.
[crypto.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
20 #include "silccrypto.h"
21 #include "mp_tma.h"
22
23 void silc_mp_init(SilcMPInt *mp)
24 {
25   (void)tma_mp_init(mp);
26 }
27
28 SilcBool silc_mp_sinit(SilcStack stack, SilcMPInt *mp)
29 {
30   /* XXX TODO */
31   tma_mp_init(mp);
32   return TRUE;
33 }
34
35 void silc_mp_uninit(SilcMPInt *mp)
36 {
37   tma_mp_clear(mp);
38 }
39
40 void silc_mp_suninit(SilcStack stack, SilcMPInt *mp)
41 {
42   if (!stack)
43     tma_mp_clear(mp);
44 }
45
46 size_t silc_mp_size(SilcMPInt *mp)
47 {
48   return tma_mp_unsigned_bin_size(mp);
49 }
50
51 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base)
52 {
53   int size = 0;
54   tma_mp_radix_size(mp, base, &size);
55   if (size > 1)
56     size--;
57   return size;
58 }
59
60 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src)
61 {
62   (void)tma_mp_copy(src, dst);
63 }
64
65 void silc_mp_set_ui(SilcMPInt *dst, SilcUInt32 ui)
66 {
67   (void)tma_mp_set_int(dst, ui);
68 }
69
70 void silc_mp_set_si(SilcMPInt *dst, SilcInt32 si)
71 {
72   (void)tma_mp_set_int(dst, si);
73 }
74
75 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base)
76 {
77   (void)tma_mp_read_radix(dst, str, base);
78 }
79
80 SilcUInt32 silc_mp_get_ui(SilcMPInt *mp)
81 {
82   return (SilcUInt32)tma_mp_get_int(mp);
83 }
84
85 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base)
86 {
87   if (tma_mp_toradix(mp, str, base) != MP_OKAY)
88     return NULL;
89   return str;
90 }
91
92 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
93 {
94   (void)tma_mp_add(mp1, mp2, dst);
95 }
96
97 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
98 {
99   tma_mp_add_d(mp1, (tma_mp_digit)ui, dst);
100 }
101
102 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
103 {
104   (void)tma_mp_sub(mp1, mp2, dst);
105 }
106
107 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
108 {
109   (void)tma_mp_sub_d(mp1, (tma_mp_digit)ui, dst);
110 }
111
112 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
113 {
114   (void)tma_mp_mul(mp1, mp2, dst);
115 }
116
117 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
118 {
119   (void)tma_mp_mul_d(mp1, (tma_mp_digit)ui, dst);
120 }
121
122 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
123 {
124   (void)tma_mp_mul_2d(mp1, exp, dst);
125 }
126
127 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src)
128 {
129   (void)tma_mp_sqrt(src, dst);
130 }
131
132 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
133 {
134   (void)tma_mp_div(mp1, mp2, dst, NULL);
135 }
136
137 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
138 {
139   (void)tma_mp_div_d(mp1, (tma_mp_digit)ui, dst, NULL);
140 }
141
142 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
143                     SilcMPInt *mp2)
144 {
145   (void)tma_mp_div(mp1, mp2, q, r);
146 }
147
148 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
149 {
150   (void)tma_mp_div_2d(mp1, exp, dst, NULL);
151 }
152
153 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1,
154                          SilcUInt32 exp)
155 {
156   (void)tma_mp_div_2d(mp1, exp, q, r);
157 }
158
159 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
160 {
161   (void)tma_mp_mod(mp1, mp2, dst);
162 }
163
164 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
165 {
166   tma_mp_digit d;
167   (void)tma_mp_mod_d(mp1, ui, &d);
168   silc_mp_set_ui(dst, d);
169 }
170
171 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 ui)
172 {
173   (void)tma_mp_mod_2d(mp1, ui, dst);
174 }
175
176 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp)
177 {
178   SILC_NOT_IMPLEMENTED("silc_mp_pow");
179   assert(FALSE);
180 }
181
182 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp)
183 {
184   (void)tma_mp_expt_d(mp1, (tma_mp_digit)exp, dst);
185 }
186
187 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp,
188                      SilcMPInt *mod)
189 {
190   (void)tma_mp_exptmod(mp1, exp, mod, dst);
191 }
192
193 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, SilcUInt32 exp,
194                         SilcMPInt *mod)
195 {
196   SilcMPInt tmp;
197   silc_mp_init(&tmp);
198   silc_mp_set_ui(&tmp, exp);
199   silc_mp_pow_mod(dst, mp1, &tmp, mod);
200   silc_mp_uninit(&tmp);
201 }
202
203 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
204 {
205   (void)tma_mp_gcd(mp1, mp2, dst);
206 }
207
208 void silc_mp_gcdext(SilcMPInt *g, SilcMPInt *s, SilcMPInt *t, SilcMPInt *mp1,
209                     SilcMPInt *mp2)
210 {
211   (void)tma_mp_exteuclid(mp1, mp2, s, t, g);
212 }
213
214 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2)
215 {
216   return tma_mp_cmp(mp1, mp2);
217 }
218
219 int silc_mp_cmp_si(SilcMPInt *mp1, SilcInt32 si)
220 {
221   return tma_mp_cmp_d(mp1, si);
222 }
223
224 int silc_mp_cmp_ui(SilcMPInt *mp1, SilcUInt32 ui)
225 {
226   return tma_mp_cmp_d(mp1, ui);
227 }
228
229 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src)
230 {
231   (void)tma_mp_abs(src, dst);
232 }
233
234 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src)
235 {
236   (void)tma_mp_neg(src, dst);
237 }
238
239 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
240 {
241   (void)tma_mp_and(mp1, mp2, dst);
242 }
243
244 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
245 {
246   (void)tma_mp_or(mp1, mp2, dst);
247 }
248
249 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
250 {
251   (void)tma_mp_xor(mp1, mp2, dst);
252 }