updates.
[silc.git] / lib / silcmath / mp_mpi.c
1 /*
2
3   mp_mpi.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23 #include "mpi.h"
24 #include "mplogic.h"
25
26 void silc_mp_init(SilcMPInt *mp)
27 {
28   (void)mp_init(mp);
29 }
30
31 void silc_mp_uninit(SilcMPInt *mp)
32 {
33   (void)mp_clear(mp);
34 }
35
36 size_t silc_mp_size(SilcMPInt *mp)
37 {
38   return mp_raw_size(mp);
39 }
40
41 size_t silc_mp_sizeinbase(SilcMPInt *mp, int base)
42 {
43   return mp_radix_size(mp, base) - 2; /* XXX This is actually wrong since
44                                          this might produce wrong balue.
45                                          But, it looks like MPI always returns
46                                          correct value plus one, whereas
47                                          GMP returns always the right value. */
48 }
49
50 void silc_mp_set(SilcMPInt *dst, SilcMPInt *src)
51 {
52   (void)mp_copy(src, dst);
53 }
54
55 void silc_mp_set_ui(SilcMPInt *dst, uint32 ui)
56 {
57   mp_set(dst, ui);
58 }
59
60 void silc_mp_set_si(SilcMPInt *dst, int32 si)
61 {
62   (void)mp_set_int(dst, si);
63 }
64
65 void silc_mp_set_str(SilcMPInt *dst, const char *str, int base)
66 {
67   (void)mp_read_variable_radix(dst, str, base);
68 }
69
70 uint32 silc_mp_get_ui(SilcMPInt *mp)
71 {
72   return (uint32)MP_DIGIT(mp, 0);
73 }
74
75 char *silc_mp_get_str(char *str, SilcMPInt *mp, int base)
76 {
77   if (mp_toradix(mp, str, base) != MP_OKAY)
78     return NULL;
79   return str;
80 }
81
82 void silc_mp_add(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
83 {
84   (void)mp_add(mp1, mp2, dst);
85 }
86
87 void silc_mp_add_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
88 {
89   mp_add_d(mp1, ui, dst);
90 }
91
92 void silc_mp_sub(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
93 {
94   (void)mp_sub(mp1, mp2, dst);
95 }
96
97 void silc_mp_sub_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
98 {
99   (void)mp_sub_d(mp1, (mp_digit)ui, dst);
100 }
101
102 void silc_mp_mul(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
103 {
104   (void)mp_mul(mp1, mp2, dst);
105 }
106
107 void silc_mp_mul_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
108 {
109   (void)mp_mul_d(mp1, (mp_digit)ui, dst);
110 }
111
112 void silc_mp_mul_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp)
113 {
114   SilcMPInt tmp;
115   silc_mp_init(&tmp);
116   (void)mp_2expt(&tmp, (mp_digit)exp);
117   (void)mp_mul(mp1, &tmp, dst);
118   silc_mp_uninit(&tmp);
119 }
120
121 void silc_mp_sqrt(SilcMPInt *dst, SilcMPInt *src)
122 {
123   (void)mp_sqrt(src, dst);
124 }
125
126 void silc_mp_div(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
127 {
128   (void)mp_div(mp1, mp2, dst, NULL);
129 }
130
131 void silc_mp_div_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
132 {
133   (void)mp_div_d(mp1, (mp_digit)ui, dst, NULL);
134 }
135
136 void silc_mp_div_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
137                     SilcMPInt *mp2)
138 {
139   (void)mp_div(mp1, mp2, q, r);
140 }
141
142 void silc_mp_div_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp)
143 {
144   SilcMPInt tmp;
145   silc_mp_init(&tmp);
146   (void)mp_2expt(&tmp, (mp_digit)exp);
147   (void)mp_div(mp1, &tmp, dst, NULL);
148   silc_mp_uninit(&tmp);
149 }
150
151 void silc_mp_div_2exp_qr(SilcMPInt *q, SilcMPInt *r, SilcMPInt *mp1, 
152                          uint32 exp)
153 {
154   if (q) {
155     (void)mp_2expt(q, (mp_digit)exp);
156     (void)mp_div(mp1, q, q, r);
157   }
158 }
159
160 void silc_mp_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
161 {
162   (void)mp_mod(mp1, mp2, dst);
163 }
164
165 void silc_mp_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
166 {
167   mp_digit uidst;
168   (void)mp_mod_d(mp1, (mp_digit)ui, &uidst);
169   mp_set(dst, uidst);
170 }
171
172 void silc_mp_mod_2exp(SilcMPInt *dst, SilcMPInt *mp1, uint32 ui)
173 {
174   SilcMPInt tmp;
175   silc_mp_init(&tmp);
176   (void)mp_2expt(&tmp, (mp_digit)ui);
177   (void)mp_mod(mp1, &tmp, dst);
178   silc_mp_uninit(&tmp);
179 }
180
181 void silc_mp_pow(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp)
182 {
183   (void)mp_expt(mp1, exp, dst);
184 }
185
186 void silc_mp_pow_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp)
187 {
188   (void)mp_expt_d(mp1, (mp_digit)exp, dst);
189 }
190
191 void silc_mp_pow_mod(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *exp, 
192                      SilcMPInt *mod)
193 {
194   (void)mp_exptmod(mp1, exp, mod, dst);
195 }
196
197 void silc_mp_pow_mod_ui(SilcMPInt *dst, SilcMPInt *mp1, uint32 exp, 
198                         SilcMPInt *mod)
199 {
200   (void)mp_exptmod_d(mp1, (mp_digit)exp, mod, dst);
201 }
202
203 void silc_mp_gcd(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
204 {
205   (void)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)mp_xgcd(mp1, mp2, g, s, t);
212 }
213
214 int silc_mp_cmp(SilcMPInt *mp1, SilcMPInt *mp2)
215 {
216   return mp_cmp(mp1, mp2);
217 }
218
219 int silc_mp_cmp_si(SilcMPInt *mp1, int32 si)
220 {
221   return mp_cmp_int(mp1, (long)si);
222 }
223
224 int silc_mp_cmp_ui(SilcMPInt *mp1, uint32 ui)
225 {
226   return mp_cmp_d(mp1, ui);
227 }
228
229 void silc_mp_abs(SilcMPInt *dst, SilcMPInt *src)
230 {
231   mp_abs(src, dst);
232 }
233
234 void silc_mp_neg(SilcMPInt *dst, SilcMPInt *src)
235 {
236   mp_neg(src, dst);
237 }
238
239 void silc_mp_and(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
240 {
241   mpl_and(mp1, mp2, dst);
242 }
243
244 void silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
245 {
246   mpl_or(mp1, mp2, dst);
247 }
248
249 void silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2)
250 {
251   mpl_xor(mp1, mp2, dst);
252 }