2 * FILE: d0_bignum-gmp.c
3 * AUTHOR: Rudolf Polzer - divVerent@xonotic.org
5 * Copyright (c) 2010, Rudolf Polzer
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 /* NOTE: this file links against libgmp (http://gmplib.org), which is under the
37 * Lesser General Public License 2.1 or later. You may have to abide to its
38 * terms too if you use this file.
39 * To alternatively link to OpenSSL, provide the option --with-openssl to
48 #include "d0_bignum.h"
59 static gmp_randstate_t RANDSTATE; // FIXME make threadsafe
60 static d0_bignum_t temp; // FIXME make threadsafe
65 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
69 unsigned char buf[256];
70 d0_bignum_init(&temp);
71 gmp_randinit_mt(RANDSTATE);
72 gmp_randseed_ui(RANDSTATE, time(NULL));
73 * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
76 HCRYPTPROV hCryptProv;
77 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
79 if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
81 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
84 CryptReleaseContext(hCryptProv, 0);
86 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
88 if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
90 fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
93 CryptReleaseContext(hCryptProv, 0);
97 fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
102 f = fopen("/dev/urandom", "rb");
104 f = fopen("/dev/random", "rb");
108 if(fread(buf, sizeof(buf), 1, f) != 1)
110 fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
117 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
122 mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
123 gmp_randseed(RANDSTATE, temp.z);
128 void d0_bignum_SHUTDOWN(void)
130 d0_bignum_clear(&temp);
131 gmp_randclear(RANDSTATE);
134 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
136 static __thread unsigned char numbuf[65536];
138 numbuf[0] = mpz_sgn(bignum->z) & 3;
139 if((numbuf[0] & 3) != 0) // nonzero
141 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
142 if(count > sizeof(numbuf) - 1)
144 mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
146 return d0_iobuf_write_packet(buf, numbuf, count + 1);
149 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
151 static __thread unsigned char numbuf[65536];
152 size_t count = sizeof(numbuf);
153 if(!d0_iobuf_read_packet(buf, numbuf, &count))
157 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
158 if(numbuf[0] & 3) // nonzero
160 mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
161 if(numbuf[0] & 2) // negative
162 mpz_neg(bignum->z, bignum->z);
166 mpz_set_ui(bignum->z, 0);
171 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
174 count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
179 // pad from left (big endian numbers!)
180 memset(buf, 0, bufsize - count);
181 buf += bufsize - count;
184 mpz_export(buf, &bufsize, 1, 1, 0, 0, bignum->z);
188 // mpz_sizeinbase lied to us
190 // there is no sane way whatsoever to handle this
196 // mpz_sizeinbase lied to us
200 memset(buf, 0, count);
204 memmove(buf + count - bufsize, buf, bufsize);
205 memset(buf, 0, count - bufsize);
211 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
214 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
215 mpz_import(bignum->z, bufsize, 1, 1, 0, 0, buf);
219 d0_bignum_t *d0_bignum_new(void)
221 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
226 void d0_bignum_free(d0_bignum_t *a)
232 void d0_bignum_init(d0_bignum_t *b)
237 void d0_bignum_clear(d0_bignum_t *a)
242 size_t d0_bignum_size(const d0_bignum_t *r)
244 return mpz_sizeinbase(r->z, 2);
247 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
249 return mpz_cmp(a->z, b->z);
252 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
254 if(!r) r = d0_bignum_new(); if(!r) return NULL;
255 mpz_sub(temp.z, max->z, min->z);
256 mpz_urandomm(r->z, RANDSTATE, temp.z);
257 mpz_add(r->z, r->z, min->z);
261 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
263 if(!r) r = d0_bignum_new(); if(!r) return NULL;
264 mpz_urandomb(r->z, RANDSTATE, n);
268 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
270 if(!r) r = d0_bignum_new(); if(!r) return NULL;
271 mpz_urandomb(r->z, RANDSTATE, n-1);
272 mpz_setbit(r->z, n-1);
276 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
278 return d0_bignum_int(r, 0);
281 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
283 return d0_bignum_int(r, 1);
286 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
288 if(!r) r = d0_bignum_new(); if(!r) return NULL;
293 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
297 if(!r) r = d0_bignum_new(); if(!r) return NULL;
302 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
304 if(!r) r = d0_bignum_new(); if(!r) return NULL;
309 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
311 if(!r) r = d0_bignum_new(); if(!r) return NULL;
313 mpz_mul_2exp(r->z, a->z, n);
315 mpz_fdiv_q_2exp(r->z, a->z, -n);
321 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
323 if(!r) r = d0_bignum_new(); if(!r) return NULL;
324 mpz_add(r->z, a->z, b->z);
328 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
330 if(!r) r = d0_bignum_new(); if(!r) return NULL;
331 mpz_sub(r->z, a->z, b->z);
335 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
337 if(!r) r = d0_bignum_new(); if(!r) return NULL;
338 mpz_mul(r->z, a->z, b->z);
342 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
348 mpz_fdiv_qr(q->z, m->z, a->z, b->z);
350 mpz_fdiv_q(q->z, a->z, b->z);
352 mpz_fdiv_r(m->z, a->z, b->z);
359 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)
361 r = d0_bignum_add(r, a, b);
362 mpz_fdiv_r(r->z, r->z, m->z);
366 d0_bignum_t *d0_bignum_mod_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b, const d0_bignum_t *m)
368 r = d0_bignum_sub(r, a, b);
369 mpz_fdiv_r(r->z, r->z, m->z);
373 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)
375 r = d0_bignum_mul(r, a, b);
376 mpz_fdiv_r(r->z, r->z, m->z);
380 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)
382 if(!r) r = d0_bignum_new(); if(!r) return NULL;
383 mpz_powm(r->z, a->z, b->z, m->z);
387 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
389 // here, r MUST be set, as otherwise we cannot return error state!
390 return mpz_invert(r->z, a->z, m->z);
393 int d0_bignum_isprime(const d0_bignum_t *r, int param)
395 return mpz_probab_prime_p(r->z, param);
398 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)
400 if(!r) r = d0_bignum_new(); if(!r) return NULL;
402 mpz_gcdext(r->z, s->z, t ? t->z : NULL, a->z, b->z);
404 mpz_gcdext(r->z, t->z, NULL, b->z, a->z);
406 mpz_gcd(r->z, a->z, b->z);
410 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
412 return mpz_get_str(NULL, base, x->z);