2 * FILE: d0_bignum-tommath.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
41 #include "d0_bignum.h"
47 // tommath/tomsfastmath distinction
50 # define TM(name) MP_##name
51 # define tm(name) mp_##name
52 #elif defined(TOMSFASTMATH)
54 # define TM(name) FP_##name
55 # define tm(name) fp_##name
58 # error Either TOMMATH or TOMSFASTMATH must be defined.
66 static d0_bignum_t temp;
67 static unsigned char numbuf[65536];
68 static void *tempmutex = NULL; // hold this mutex when using temp or numbuf
73 HCRYPTPROV hCryptProv;
78 void rand_bytes(unsigned char *buf, size_t n)
81 CryptGenRandom(hCryptProv, n, (PBYTE) buf);
85 fread(buf, 1, n, randf);
89 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
92 unsigned char buf[256];
94 tempmutex = d0_createmutex();
95 d0_lockmutex(tempmutex);
97 d0_bignum_init(&temp);
100 if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
103 else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET))
108 fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
114 randf = fopen("/dev/urandom", "rb");
116 randf = fopen("/dev/random", "rb");
123 fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
128 d0_unlockmutex(tempmutex);
133 void d0_bignum_SHUTDOWN(void)
135 d0_lockmutex(tempmutex);
137 d0_bignum_clear(&temp);
141 CryptReleaseContext(hCryptProv, 0);
146 d0_unlockmutex(tempmutex);
147 d0_destroymutex(tempmutex);
151 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
156 d0_lockmutex(tempmutex);
157 numbuf[0] = (tm(iszero)(&bignum->z) ? 0 : (bignum->z.sign == TM(ZPOS)) ? 1 : 3);
158 if((numbuf[0] & 3) != 0) // nonzero
160 count = tm(unsigned_bin_size)((tm(int) *) &bignum->z);
161 if(count > sizeof(numbuf) - 1)
163 d0_unlockmutex(tempmutex);
166 tm(to_unsigned_bin)((tm(int) *) &bignum->z, numbuf+1);
168 ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
169 d0_unlockmutex(tempmutex);
173 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
175 size_t count = sizeof(numbuf);
176 d0_lockmutex(tempmutex);
177 if(!d0_iobuf_read_packet(buf, numbuf, &count))
179 d0_unlockmutex(tempmutex);
184 d0_unlockmutex(tempmutex);
188 bignum = d0_bignum_new();
191 d0_unlockmutex(tempmutex);
194 if(numbuf[0] & 3) // nonzero
196 tm(read_unsigned_bin)(&bignum->z, numbuf+1, count-1);
197 if(numbuf[0] & 2) // negative
198 bignum->z.sign = TM(NEG);
202 tm(zero)(&bignum->z);
204 d0_unlockmutex(tempmutex);
208 ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t bufsize)
210 unsigned long bufsize_;
212 count = tm(unsigned_bin_size)((tm(int) *) &bignum->z);
217 // pad from left (big endian numbers!)
218 memset(buf, 0, bufsize - count);
219 buf += bufsize - count;
221 tm(to_unsigned_bin)((tm(int) *) &bignum->z, buf);
225 d0_bignum_t *d0_bignum_import_unsigned(d0_bignum_t *bignum, const void *buf, size_t bufsize)
228 if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
229 tm(read_unsigned_bin)(&bignum->z, (void *) buf, bufsize);
233 d0_bignum_t *d0_bignum_new(void)
235 d0_bignum_t *b = d0_malloc(sizeof(d0_bignum_t));
240 void d0_bignum_free(d0_bignum_t *a)
248 void d0_bignum_init(d0_bignum_t *b)
253 void d0_bignum_clear(d0_bignum_t *a)
260 size_t d0_bignum_size(const d0_bignum_t *r)
262 return tm(count_bits)((tm(int) *) &r->z);
265 int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
267 return tm(cmp)((tm(int) *) &a->z, (tm(int) *) &b->z);
270 static d0_bignum_t *d0_bignum_rand_0_to_limit(d0_bignum_t *r, const d0_bignum_t *limit)
272 size_t n = d0_bignum_size(limit);
273 size_t b = (n + 7) / 8;
274 unsigned char mask = "\xFF\x7F\x3F\x1F\x0F\x07\x03\x01"[8*b - n];
275 assert(b <= sizeof(numbuf));
276 d0_lockmutex(tempmutex);
279 rand_bytes(numbuf, b);
281 r = d0_bignum_import_unsigned(r, numbuf, b);
282 if(d0_bignum_cmp(r, limit) < 0)
284 d0_unlockmutex(tempmutex);
290 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
292 d0_lockmutex(tempmutex);
293 tm(sub)((tm(int) *) &max->z, (tm(int) *) &min->z, &temp.z);
294 r = d0_bignum_rand_0_to_limit(r, &temp);
295 d0_unlockmutex(tempmutex);
296 tm(add)((tm(int) *) &r->z, (tm(int) *) &min->z, &r->z);
300 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
302 d0_lockmutex(tempmutex);
303 if(!d0_bignum_one(&temp))
305 d0_unlockmutex(tempmutex);
308 if(!d0_bignum_shl(&temp, &temp, n))
310 d0_unlockmutex(tempmutex);
313 r = d0_bignum_rand_0_to_limit(r, &temp);
314 d0_unlockmutex(tempmutex);
318 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
320 d0_lockmutex(tempmutex);
321 if(!d0_bignum_one(&temp))
323 d0_unlockmutex(tempmutex);
326 if(!d0_bignum_shl(&temp, &temp, n-1))
328 d0_unlockmutex(tempmutex);
331 r = d0_bignum_rand_0_to_limit(r, &temp);
332 if(!d0_bignum_add(r, r, &temp))
334 d0_unlockmutex(tempmutex);
337 d0_unlockmutex(tempmutex);
341 d0_bignum_t *d0_bignum_zero(d0_bignum_t *r)
343 if(!r) r = d0_bignum_new(); if(!r) return NULL;
348 d0_bignum_t *d0_bignum_one(d0_bignum_t *r)
350 return d0_bignum_int(r, 1);
353 d0_bignum_t *d0_bignum_int(d0_bignum_t *r, int n)
355 if(!r) r = d0_bignum_new(); if(!r) return NULL;
357 tm(set_int)(&r->z, n);
359 // libtomsfastmath lacks this function
361 assert(!"Sorry, importing signed is not implemented");
363 unsigned char nbuf[sizeof(n)];
366 for (i = sizeof(n); i-- > 0; )
371 tm(read_unsigned_bin)(&r->z, nbuf, sizeof(n));
378 d0_bignum_t *d0_bignum_mov(d0_bignum_t *r, const d0_bignum_t *a)
382 if(!r) r = d0_bignum_new(); if(!r) return NULL;
383 tm(copy)((tm(int) *) &a->z, &r->z);
387 d0_bignum_t *d0_bignum_neg(d0_bignum_t *r, const d0_bignum_t *a)
389 if(!r) r = d0_bignum_new(); if(!r) return NULL;
390 tm(neg)((tm(int) *) &a->z, &r->z);
394 d0_bignum_t *d0_bignum_shl(d0_bignum_t *r, const d0_bignum_t *a, ssize_t n)
396 if(!r) r = d0_bignum_new(); if(!r) return NULL;
398 tm(mul_2d)((tm(int) *) &a->z, n, &r->z);
400 tm(div_2d)((tm(int) *) &a->z, -n, &r->z, NULL);
402 tm(copy)((tm(int) *) &a->z, &r->z);
406 d0_bignum_t *d0_bignum_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
408 if(!r) r = d0_bignum_new(); if(!r) return NULL;
409 tm(add)((tm(int) *) &a->z, (tm(int) *) &b->z, &r->z);
413 d0_bignum_t *d0_bignum_sub(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
415 if(!r) r = d0_bignum_new(); if(!r) return NULL;
416 tm(sub)((tm(int) *) &a->z, (tm(int) *) &b->z, &r->z);
420 d0_bignum_t *d0_bignum_mul(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *b)
422 if(!r) r = d0_bignum_new(); if(!r) return NULL;
423 tm(mul)((tm(int) *) &a->z, (tm(int) *) &b->z, &r->z);
427 d0_bignum_t *d0_bignum_divmod(d0_bignum_t *q, d0_bignum_t *m, const d0_bignum_t *a, const d0_bignum_t *b)
432 tm(div)((tm(int) *) &a->z, (tm(int) *) &b->z, &q->z, m ? &m->z : NULL);
434 tm(mod)((tm(int) *) &a->z, (tm(int) *) &b->z, &m->z);
441 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)
443 if(!r) r = d0_bignum_new(); if(!r) return NULL;
444 tm(addmod)((tm(int) *) &a->z, (tm(int) *) &b->z, (tm(int) *) &m->z, &r->z);
448 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)
450 if(!r) r = d0_bignum_new(); if(!r) return NULL;
451 tm(submod)((tm(int) *) &a->z, (tm(int) *) &b->z, (tm(int) *) &m->z, &r->z);
455 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)
457 if(!r) r = d0_bignum_new(); if(!r) return NULL;
458 tm(mulmod)((tm(int) *) &a->z, (tm(int) *) &b->z, (tm(int) *) &m->z, &r->z);
462 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)
464 if(!r) r = d0_bignum_new(); if(!r) return NULL;
465 tm(exptmod)((tm(int) *) &a->z, (tm(int) *) &b->z, (tm(int) *) &m->z, &r->z);
469 D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
471 // here, r MUST be set, as otherwise we cannot return error state!
472 return tm(invmod)((tm(int) *) &a->z, (tm(int) *) &m->z, &r->z) == TM(OKAY);
475 int d0_bignum_isprime(const d0_bignum_t *r, int param)
482 tm(prime_is_prime)((tm(int) *) &r->z, param, &ret);
486 // this does 8 rabin tests; for param > 8, do more?
487 return tm(isprime)((tm(int) *) &r->z);
491 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)
493 if(!r) r = d0_bignum_new(); if(!r) return NULL;
497 tm(exteuclid)((tm(int) *) &a->z, (tm(int) *) &b->z, s ? &s->z : NULL, t ? &t->z : NULL, &r->z);
499 assert(!"Extended gcd not implemented");
503 tm(gcd)((tm(int) *) &a->z, (tm(int) *) &b->z, &r->z);
507 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
511 tm(radix_size)((tm(int) *) &x->z, base, &sz);
512 str = d0_malloc(sz + 1);
513 tm(toradix)((tm(int) *) &x->z, str, base);