]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
Merge branch 'master' of github.com:divVerent/d0_blind_id
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
1 /*
2 Blind-ID library for user identification using RSA blind signatures
3 Copyright (C) 2010  Rudolf Polzer
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 #include "d0_bignum.h"
21
22 #include <gmp.h>
23 #include <string.h>
24
25 struct d0_bignum_s
26 {
27         mpz_t z;
28 };
29
30 static gmp_randstate_t RANDSTATE;
31 static d0_bignum_t temp;
32
33 #include <time.h>
34 #include <stdio.h>
35 void d0_bignum_INITIALIZE(void)
36 {
37         FILE *f;
38         d0_bignum_init(&temp);
39         gmp_randinit_mt(RANDSTATE);
40         gmp_randseed_ui(RANDSTATE, time(NULL));
41         f = fopen("/dev/urandom", "rb");
42         if(!f)
43                 f = fopen("/dev/random", "rb");
44         if(f)
45         {
46                 unsigned char buf[256];
47                 setbuf(f, NULL);
48                 if(fread(buf, sizeof(buf), 1, f) == 1)
49                 {
50                         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
51                         gmp_randseed(RANDSTATE, temp.z);
52                 }
53                 fclose(f);
54         }
55 }
56
57 void d0_bignum_SHUTDOWN(void)
58 {
59         d0_bignum_clear(&temp);
60         gmp_randclear(RANDSTATE);
61 }
62
63 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
64 {
65         static unsigned char numbuf[65536];
66         size_t count = 0;
67         numbuf[0] = mpz_sgn(bignum->z) & 3;
68         if((numbuf[0] & 3) != 0) // nonzero
69         {
70                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
71                 if(count > sizeof(numbuf) - 1)
72                         return 0;
73                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
74         }
75         return d0_iobuf_write_packet(buf, numbuf, count + 1);
76 }
77
78 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
79 {
80         static unsigned char numbuf[65536];
81         size_t count = sizeof(numbuf);
82         if(!d0_iobuf_read_packet(buf, numbuf, &count))
83                 return NULL;
84         if(count < 1)
85                 return NULL;
86         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
87         if(numbuf[0] & 3) // nonzero
88         {
89                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
90                 if(numbuf[0] & 2) // negative
91                         mpz_neg(bignum->z, bignum->z);
92         }
93         else // zero
94         {
95                 mpz_set_ui(bignum->z, 0);
96         }
97         return bignum;
98 }
99
100 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
101 {
102         size_t count;
103         count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
104         if(count > bufsize)
105                 return -1;
106         if(bufsize > count)
107         {
108                 // pad from left (big endian numbers!)
109                 memset(buf, 0, bufsize - count);
110                 buf += bufsize - count;
111         }
112         bufsize = count;
113         mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
114         if(bufsize > count)
115         {
116                 // REALLY BAD
117                 // mpz_sizeinbase lied to us
118                 // buffer overflow
119                 // there is no sane way whatsoever to handle this
120                 abort();
121         }
122         if(bufsize < count)
123         {
124                 // BAD
125                 // mpz_sizeinbase lied to us
126                 // move the number
127                 if(bufsize == 0)
128                 {
129                         memset(buf, 0, count);
130                 }
131                 else
132                 {
133                         memmove(buf + count - bufsize, buf, bufsize);
134                         memset(buf, 0, count - bufsize);
135                 }
136         }
137         return bufsize;
138 }
139
140 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
141 {
142         size_t count;
143         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
144         mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
145         return bignum;
146 }
147
148 d0_bignum_t *d0_bignum_new(void)
149 {
150         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
151         mpz_init(b->z);
152         return b;
153 }
154
155 void d0_bignum_free(d0_bignum_t *a)
156 {
157         mpz_clear(a->z);
158         d0_free(a);
159 }
160
161 void d0_bignum_init(d0_bignum_t *b)
162 {
163         mpz_init(b->z);
164 }
165
166 void d0_bignum_clear(d0_bignum_t *a)
167 {
168         mpz_clear(a->z);
169 }
170
171 size_t d0_bignum_size(const d0_bignum_t *r)
172 {
173         return mpz_sizeinbase(r->z, 2);
174 }
175
176 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
177 {
178         return mpz_cmp(a->z, b->z);
179 }
180
181 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
182 {
183         if(!r) r = d0_bignum_new(); if(!r) return NULL;
184         mpz_sub(temp.z, max->z, min->z);
185         mpz_urandomm(r->z, RANDSTATE, temp.z);
186         mpz_add(r->z, r->z, min->z);
187         return r;
188 }
189
190 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
191 {
192         if(!r) r = d0_bignum_new(); if(!r) return NULL;
193         mpz_urandomb(r->z, RANDSTATE, n);
194         return r;
195 }
196
197 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
198 {
199         if(!r) r = d0_bignum_new(); if(!r) return NULL;
200         mpz_urandomb(r->z, RANDSTATE, n-1);
201         mpz_setbit(r->z, n-1);
202         return r;
203 }
204
205 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
206 {
207         return d0_bignum_int(r, 0);
208 }
209
210 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
211 {
212         return d0_bignum_int(r, 1);
213 }
214
215 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
216 {
217         if(!r) r = d0_bignum_new(); if(!r) return NULL;
218         mpz_set_si(r->z, n);
219         return r;
220 }
221
222 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
223 {
224         if(r == a)
225                 return r; // trivial
226         if(!r) r = d0_bignum_new(); if(!r) return NULL;
227         mpz_set(r->z, a->z);
228         return r;
229 }
230
231 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
232 {
233         if(!r) r = d0_bignum_new(); if(!r) return NULL;
234         mpz_neg(r->z, a->z);
235         return r;
236 }
237
238 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
239 {
240         if(!r) r = d0_bignum_new(); if(!r) return NULL;
241         if(n > 0)
242                 mpz_mul_2exp(r->z, a->z, n);
243         else if(n < 0)
244                 mpz_fdiv_q_2exp(r->z, a->z, -n);
245         else
246                 mpz_set(r->z, a->z);
247         return r;
248 }
249
250 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
251 {
252         if(!r) r = d0_bignum_new(); if(!r) return NULL;
253         mpz_add(r->z, a->z, b->z);
254         return r;
255 }
256
257 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
258 {
259         if(!r) r = d0_bignum_new(); if(!r) return NULL;
260         mpz_sub(r->z, a->z, b->z);
261         return r;
262 }
263
264 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
265 {
266         if(!r) r = d0_bignum_new(); if(!r) return NULL;
267         mpz_mul(r->z, a->z, b->z);
268         return r;
269 }
270
271 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
272 {
273         if(!q && !m)
274                 m = d0_bignum_new();
275         if(q)
276                 if(m)
277                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
278                 else
279                         mpz_fdiv_q(q->z, a->z, b->z);
280         else
281                 mpz_fdiv_r(m->z, a->z, b->z);
282         if(m)
283                 return m;
284         else
285                 return q;
286 }
287
288 d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
289 {
290         r = d0_bignum_add(r, a, b);
291         mpz_fdiv_r(r->z, r->z, m->z);
292         return r;
293 }
294
295 d0_bignum_t *d0_bignum_mod_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
296 {
297         r = d0_bignum_mul(r, a, b);
298         mpz_fdiv_r(r->z, r->z, m->z);
299         return r;
300 }
301
302 d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
303 {
304         if(!r) r = d0_bignum_new(); if(!r) return NULL;
305         mpz_powm(r->z, a->z, b->z, m->z);
306         return r;
307 }
308
309 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
310 {
311         // here, r MUST be set, as otherwise we cannot return error state!
312         return mpz_invert(r->z, a->z, m->z);
313 }
314
315 int d0_bignum_isprime(d0_bignum_t *r, int param)
316 {
317         return mpz_probab_prime_p(r->z, param);
318 }
319
320 d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const d0_bignum_t *a, const d0_bignum_t *b)
321 {
322         if(!r) r = d0_bignum_new(); if(!r) return NULL;
323         if(s)
324                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
325         else if(t)
326                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
327         else
328                 mpz_gcd(r->z, a->z, b->z);
329         return r;
330 }
331
332 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
333 {
334         return mpz_get_str(NULL, base, x->z);
335 }