]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blob - d0_bignum-gmp.c
automatically include the license notice into .so or .a files
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
1 /*
2  * FILE:        d0_bignum-gmp.c
3  * AUTHOR:      Rudolf Polzer - divVerent@xonotic.org
4  * 
5  * Copyright (c) 2010, Rudolf Polzer
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holder nor the names of contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  * 
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id$
33  */
34
35 /* NOTE: this file links against libgmp (http://gmplib.org), which is under the
36  * Lesser General Public License 2.1 or later. You may have to abide to its
37  * terms too if you use this file.
38  * To alternatively link to OpenSSL, provide the option --with-openssl to
39  * ./configure.
40  */
41
42 #ifdef WIN32
43 #include <windows.h>
44 #include <wincrypt.h>
45 #endif
46
47 #include "d0_bignum.h"
48
49 #include <gmp.h>
50 #include <string.h>
51 #include <stdlib.h>
52
53 struct d0_bignum_s
54 {
55         mpz_t z;
56 };
57
58 static gmp_randstate_t RANDSTATE;
59 static d0_bignum_t temp;
60
61 #include <time.h>
62 #include <stdio.h>
63
64 WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
65 {
66         FILE *f;
67         BOOL ret = 1;
68         unsigned char buf[256];
69         d0_bignum_init(&temp);
70         gmp_randinit_mt(RANDSTATE);
71         gmp_randseed_ui(RANDSTATE, time(NULL));
72         * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
73 #ifdef WIN32
74         {
75                 HCRYPTPROV hCryptProv;
76                 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
77                 {
78                         if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
79                         {
80                                 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
81                                 ret = 0;
82                         }
83                         CryptReleaseContext(hCryptProv, 0);
84                 }
85                 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
86                 {
87                         if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
88                         {
89                                 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
90                                 ret = 0;
91                         }
92                         CryptReleaseContext(hCryptProv, 0);
93                 }
94                 else
95                 {
96                         fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
97                         ret = 0;
98                 }
99         }
100 #else
101         f = fopen("/dev/urandom", "rb");
102         if(!f)
103                 f = fopen("/dev/random", "rb");
104         if(f)
105         {
106                 setbuf(f, NULL);
107                 if(fread(buf, sizeof(buf), 1, f) != 1)
108                 {
109                         fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
110                         ret = 0;
111                 }
112                 fclose(f);
113         }
114         else
115         {
116                 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
117                 ret = 0;
118         }
119 #endif
120
121         mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
122         gmp_randseed(RANDSTATE, temp.z);
123
124         return ret;
125 }
126
127 void d0_bignum_SHUTDOWN(void)
128 {
129         d0_bignum_clear(&temp);
130         gmp_randclear(RANDSTATE);
131 }
132
133 BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
134 {
135         static unsigned char numbuf[65536];
136         size_t count = 0;
137         numbuf[0] = mpz_sgn(bignum->z) & 3;
138         if((numbuf[0] & 3) != 0) // nonzero
139         {
140                 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
141                 if(count > sizeof(numbuf) - 1)
142                         return 0;
143                 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
144         }
145         return d0_iobuf_write_packet(buf, numbuf, count + 1);
146 }
147
148 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
149 {
150         static unsigned char numbuf[65536];
151         size_t count = sizeof(numbuf);
152         if(!d0_iobuf_read_packet(buf, numbuf, &count))
153                 return NULL;
154         if(count < 1)
155                 return NULL;
156         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
157         if(numbuf[0] & 3) // nonzero
158         {
159                 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
160                 if(numbuf[0] & 2) // negative
161                         mpz_neg(bignum->z, bignum->z);
162         }
163         else // zero
164         {
165                 mpz_set_ui(bignum->z, 0);
166         }
167         return bignum;
168 }
169
170 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
171 {
172         size_t count;
173         count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
174         if(count > bufsize)
175                 return -1;
176         if(bufsize > count)
177         {
178                 // pad from left (big endian numbers!)
179                 memset(buf, 0, bufsize - count);
180                 buf += bufsize - count;
181         }
182         bufsize = count;
183         mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
184         if(bufsize > count)
185         {
186                 // REALLY BAD
187                 // mpz_sizeinbase lied to us
188                 // buffer overflow
189                 // there is no sane way whatsoever to handle this
190                 abort();
191         }
192         if(bufsize < count)
193         {
194                 // BAD
195                 // mpz_sizeinbase lied to us
196                 // move the number
197                 if(bufsize == 0)
198                 {
199                         memset(buf, 0, count);
200                 }
201                 else
202                 {
203                         memmove(buf + count - bufsize, buf, bufsize);
204                         memset(buf, 0, count - bufsize);
205                 }
206         }
207         return bufsize;
208 }
209
210 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
211 {
212         size_t count;
213         if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
214         mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
215         return bignum;
216 }
217
218 d0_bignum_t *d0_bignum_new(void)
219 {
220         d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
221         mpz_init(b->z);
222         return b;
223 }
224
225 void d0_bignum_free(d0_bignum_t *a)
226 {
227         mpz_clear(a->z);
228         d0_free(a);
229 }
230
231 void d0_bignum_init(d0_bignum_t *b)
232 {
233         mpz_init(b->z);
234 }
235
236 void d0_bignum_clear(d0_bignum_t *a)
237 {
238         mpz_clear(a->z);
239 }
240
241 size_t d0_bignum_size(const d0_bignum_t *r)
242 {
243         return mpz_sizeinbase(r->z, 2);
244 }
245
246 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
247 {
248         return mpz_cmp(a->z, b->z);
249 }
250
251 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
252 {
253         if(!r) r = d0_bignum_new(); if(!r) return NULL;
254         mpz_sub(temp.z, max->z, min->z);
255         mpz_urandomm(r->z, RANDSTATE, temp.z);
256         mpz_add(r->z, r->z, min->z);
257         return r;
258 }
259
260 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
261 {
262         if(!r) r = d0_bignum_new(); if(!r) return NULL;
263         mpz_urandomb(r->z, RANDSTATE, n);
264         return r;
265 }
266
267 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
268 {
269         if(!r) r = d0_bignum_new(); if(!r) return NULL;
270         mpz_urandomb(r->z, RANDSTATE, n-1);
271         mpz_setbit(r->z, n-1);
272         return r;
273 }
274
275 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
276 {
277         return d0_bignum_int(r, 0);
278 }
279
280 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
281 {
282         return d0_bignum_int(r, 1);
283 }
284
285 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
286 {
287         if(!r) r = d0_bignum_new(); if(!r) return NULL;
288         mpz_set_si(r->z, n);
289         return r;
290 }
291
292 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
293 {
294         if(r == a)
295                 return r; // trivial
296         if(!r) r = d0_bignum_new(); if(!r) return NULL;
297         mpz_set(r->z, a->z);
298         return r;
299 }
300
301 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
302 {
303         if(!r) r = d0_bignum_new(); if(!r) return NULL;
304         mpz_neg(r->z, a->z);
305         return r;
306 }
307
308 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
309 {
310         if(!r) r = d0_bignum_new(); if(!r) return NULL;
311         if(n > 0)
312                 mpz_mul_2exp(r->z, a->z, n);
313         else if(n < 0)
314                 mpz_fdiv_q_2exp(r->z, a->z, -n);
315         else
316                 mpz_set(r->z, a->z);
317         return r;
318 }
319
320 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
321 {
322         if(!r) r = d0_bignum_new(); if(!r) return NULL;
323         mpz_add(r->z, a->z, b->z);
324         return r;
325 }
326
327 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
328 {
329         if(!r) r = d0_bignum_new(); if(!r) return NULL;
330         mpz_sub(r->z, a->z, b->z);
331         return r;
332 }
333
334 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
335 {
336         if(!r) r = d0_bignum_new(); if(!r) return NULL;
337         mpz_mul(r->z, a->z, b->z);
338         return r;
339 }
340
341 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
342 {
343         if(!q && !m)
344                 m = d0_bignum_new();
345         if(q)
346                 if(m)
347                         mpz_fdiv_qr(q->z, m->z, a->z, b->z);
348                 else
349                         mpz_fdiv_q(q->z, a->z, b->z);
350         else
351                 mpz_fdiv_r(m->z, a->z, b->z);
352         if(m)
353                 return m;
354         else
355                 return q;
356 }
357
358 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)
359 {
360         r = d0_bignum_add(r, a, b);
361         mpz_fdiv_r(r->z, r->z, m->z);
362         return r;
363 }
364
365 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)
366 {
367         r = d0_bignum_mul(r, a, b);
368         mpz_fdiv_r(r->z, r->z, m->z);
369         return r;
370 }
371
372 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)
373 {
374         if(!r) r = d0_bignum_new(); if(!r) return NULL;
375         mpz_powm(r->z, a->z, b->z, m->z);
376         return r;
377 }
378
379 BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
380 {
381         // here, r MUST be set, as otherwise we cannot return error state!
382         return mpz_invert(r->z, a->z, m->z);
383 }
384
385 int d0_bignum_isprime(d0_bignum_t *r, int param)
386 {
387         return mpz_probab_prime_p(r->z, param);
388 }
389
390 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)
391 {
392         if(!r) r = d0_bignum_new(); if(!r) return NULL;
393         if(s)
394                 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
395         else if(t)
396                 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
397         else
398                 mpz_gcd(r->z, a->z, b->z);
399         return r;
400 }
401
402 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
403 {
404         return mpz_get_str(NULL, base, x->z);
405 }