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"
60 static d0_bignum_t temp;
65 HCRYPTPROV hCryptProv;
70 void rand_bytes(unsigned char *buf, size_t n)
73 CryptGenRandom(hCryptProv, n, (PBYTE) buf);
77 fread(buf, 1, n, randf);
81 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
84 unsigned char buf[256];
85 d0_bignum_init(&temp);
88 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
91 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
96 fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
102 randf = fopen("/dev/urandom", "rb");
104 randf = fopen("/dev/random", "rb");
111 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
119 void d0_bignum_SHUTDOWN(void)
121 d0_bignum_clear(&temp);
125 CryptReleaseContext(hCryptProv, 0);
131 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
133 static unsigned char numbuf[65536];
135 numbuf[0] = (mp_iszero(&bignum->z) ? 0 : (bignum->z.sign == MP_ZPOS) ? 1 : 3);
136 if((numbuf[0] & 3) != 0) // nonzero
138 count = mp_unsigned_bin_size(&bignum->z);
139 if(count > sizeof(numbuf) - 1)
141 mp_to_unsigned_bin(&bignum->z, numbuf+1);
143 return d0_iobuf_write_packet(buf, numbuf, count + 1);
146 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
148 static unsigned char numbuf[65536];
149 size_t count = sizeof(numbuf);
150 if(!d0_iobuf_read_packet(buf, numbuf, &count))
154 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
155 if(numbuf[0] & 3) // nonzero
157 mp_read_unsigned_bin(&bignum->z, numbuf+1, count-1);
158 if(numbuf[0] & 2) // negative
159 bignum->z.sign = MP_NEG;
168 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
171 count = mp_unsigned_bin_size(&bignum->z);
176 // pad from left (big endian numbers!)
177 memset(buf, 0, bufsize - count);
178 buf += bufsize - count;
180 mp_to_unsigned_bin_n(&bignum->z, buf, &count);
184 // mpz_sizeinbase lied to us
186 // there is no sane way whatsoever to handle this
192 // mpz_sizeinbase lied to us
196 memset(buf, 0, count);
200 memmove(buf + count - bufsize, buf, bufsize);
201 memset(buf, 0, count - bufsize);
207 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
210 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
211 mp_read_unsigned_bin(&bignum->z, buf, bufsize);
215 d0_bignum_t *d0_bignum_new(void)
217 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
222 void d0_bignum_free(d0_bignum_t *a)
228 void d0_bignum_init(d0_bignum_t *b)
233 void d0_bignum_clear(d0_bignum_t *a)
238 size_t d0_bignum_size(const d0_bignum_t *r)
240 return mp_count_bits(&r->z);
243 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
245 return mp_cmp(&a->z, &b->z);
248 static d0_bignum_t *d0_bignum_rand_0_to_limit(d0_bignum_t *r, const d0_bignum_t *limit)
250 size_t n = d0_bignum_size(limit);
251 size_t b = (n + 7) / 8;
252 unsigned char mask = "\xFF\x7F\x3F\x1F\x0F\x07\x03\x01"[8*b - n];
253 unsigned char numbuf[65536];
254 assert(b <= sizeof(numbuf));
257 rand_bytes(&numbuf, b);
259 r = d0_bignum_import_unsigned(r, numbuf, b);
260 if(d0_bignum_cmp(r, limit) < 0)
265 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
267 mp_sub(&max->z, &min->z, &temp.z);
268 r = d0_bignum_rand_0_to_limit(r, &temp);
269 mp_add(&r->z, &min->z, &r->z);
273 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
275 d0_bignum_one(&temp);
276 d0_bignum_shl(&temp, &temp, n);
277 r = d0_bignum_rand_0_to_limit(r, &temp);
281 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
283 d0_bignum_one(&temp);
284 d0_bignum_shl(&temp, &temp, n-1);
285 r = d0_bignum_rand_0_to_limit(r, &temp);
286 d0_bignum_add(r, r, &temp);
290 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
292 if(!r) r = d0_bignum_new(); if(!r) return NULL;
297 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
299 return d0_bignum_int(r, 1);
302 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
304 if(!r) r = d0_bignum_new(); if(!r) return NULL;
305 mp_set_int(&r->z, n);
309 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
313 if(!r) r = d0_bignum_new(); if(!r) return NULL;
314 mp_copy(&r->z, &a->z);
318 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
320 if(!r) r = d0_bignum_new(); if(!r) return NULL;
321 mp_neg(&a->z, &r->z);
325 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
327 r = d0_bignum_mov(r, a);
329 mp_mul_2d(&r->z, n, &r->z);
331 mp_div_2d(&r->z, -n, &r->z, NULL);
335 d0_bignum_t *d0_bignum_add(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 mp_add(&a->z, &b->z, &r->z);
342 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
344 if(!r) r = d0_bignum_new(); if(!r) return NULL;
345 mp_sub(&a->z, &b->z, &r->z);
349 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
351 if(!r) r = d0_bignum_new(); if(!r) return NULL;
352 mp_mul(&a->z, &b->z, &r->z);
356 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
361 mp_div(&a->z, &b->z, &q->z, m ? &m->z : NULL);
363 mp_mod(&a->z, &b->z, &m->z);
370 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)
372 if(!r) r = d0_bignum_new(); if(!r) return NULL;
373 mp_addmod(&a->z, &b->z, &m->z, &r->z);
377 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)
379 if(!r) r = d0_bignum_new(); if(!r) return NULL;
380 mp_submod(&a->z, &b->z, &m->z, &r->z);
384 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)
386 if(!r) r = d0_bignum_new(); if(!r) return NULL;
387 mp_mulmod(&a->z, &b->z, &m->z, &r->z);
391 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)
393 if(!r) r = d0_bignum_new(); if(!r) return NULL;
394 mp_exptmod(&a->z, &b->z, &m->z, &r->z);
398 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
400 // here, r MUST be set, as otherwise we cannot return error state!
401 return mp_invmod(&a->z, &m->z, &r->z) == MP_OKAY;
404 int d0_bignum_isprime(d0_bignum_t *r, int param)
409 mp_prime_is_prime(&r->z, param, &ret);
413 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)
415 if(!r) r = d0_bignum_new(); if(!r) return NULL;
417 mp_exteuclid(&a->z, &b->z, s ? &s->z : NULL, t ? &t->z : NULL, &r->z);
419 mp_gcd(&a->z, &b->z, &r->z);
423 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
425 static char str[65536];
426 mp_toradix_n(&x->z, str, base, sizeof(str));