]> de.git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0_bignum-gmp.c
on G*nt**, initialized globals seem to be made of fail and crash
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
index 535370454463f931a1be8a97b69bd517cfb68bfb..d5da8b5889d64ee68e36febcc368d99487221952 100644 (file)
@@ -56,17 +56,45 @@ struct d0_bignum_s
        mpz_t z;
 };
 
-static gmp_randstate_t RANDSTATE; // FIXME make threadsafe
-static d0_bignum_t temp; // FIXME make threadsafe
+static gmp_randstate_t RANDSTATE;
+static d0_bignum_t temp;
+static unsigned char numbuf[65536];
+static void *tempmutex = NULL; // hold this mutex when using RANDSTATE or temp or numbuf
 
 #include <time.h>
 #include <stdio.h>
 
+static void *allocate_function (size_t alloc_size)
+{
+       return d0_malloc(alloc_size);
+}
+static void *reallocate_function (void *ptr, size_t old_size, size_t new_size)
+{
+       void *data;
+       if(old_size == new_size)
+               return ptr;
+       data = d0_malloc(new_size);
+       if(ptr && data)
+               memcpy(data, ptr, (old_size < new_size) ? old_size : new_size);
+       d0_free(ptr);
+       return data;
+}
+void deallocate_function (void *ptr, size_t size)
+{
+       d0_free(ptr);
+}
+
 D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
 {
        FILE *f;
        D0_BOOL ret = 1;
        unsigned char buf[256];
+
+       tempmutex = d0_createmutex();
+       d0_lockmutex(tempmutex);
+
+       mp_set_memory_functions(allocate_function, reallocate_function, deallocate_function);
+
        d0_bignum_init(&temp);
        gmp_randinit_mt(RANDSTATE);
        gmp_randseed_ui(RANDSTATE, time(NULL));
@@ -122,39 +150,67 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
        mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
        gmp_randseed(RANDSTATE, temp.z);
 
+       d0_unlockmutex(tempmutex);
+
        return ret;
 }
 
 void d0_bignum_SHUTDOWN(void)
 {
+       d0_lockmutex(tempmutex);
+
        d0_bignum_clear(&temp);
        gmp_randclear(RANDSTATE);
+
+       d0_unlockmutex(tempmutex);
+       d0_destroymutex(tempmutex);
+       tempmutex = NULL;
 }
 
 D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
+       D0_BOOL ret;
        size_t count = 0;
+
+       d0_lockmutex(tempmutex);
        numbuf[0] = mpz_sgn(bignum->z) & 3;
        if((numbuf[0] & 3) != 0) // nonzero
        {
                count = (mpz_sizeinbase(bignum->z, 2) + 7) / 8;
                if(count > sizeof(numbuf) - 1)
+               {
+                       d0_unlockmutex(tempmutex);
                        return 0;
+               }
                mpz_export(numbuf+1, &count, 1, 1, 0, 0, bignum->z);
        }
-       return d0_iobuf_write_packet(buf, numbuf, count + 1);
+       ret = d0_iobuf_write_packet(buf, numbuf, count + 1);
+       d0_unlockmutex(tempmutex);
+       return ret;
 }
 
 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
 {
-       static __thread unsigned char numbuf[65536];
        size_t count = sizeof(numbuf);
+
+       d0_lockmutex(tempmutex);
        if(!d0_iobuf_read_packet(buf, numbuf, &count))
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
+       }
        if(count < 1)
+       {
+               d0_unlockmutex(tempmutex);
                return NULL;
-       if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL;
+       }
+       if(!bignum)
+               bignum = d0_bignum_new();
+       if(!bignum)
+       {
+               d0_unlockmutex(tempmutex);
+               return NULL;
+       }
        if(numbuf[0] & 3) // nonzero
        {
                mpz_import(bignum->z, count-1, 1, 1, 0, 0, numbuf+1);
@@ -165,6 +221,7 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
        {
                mpz_set_ui(bignum->z, 0);
        }
+       d0_unlockmutex(tempmutex);
        return bignum;
 }
 
@@ -252,8 +309,10 @@ int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b)
 d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
+       d0_lockmutex(tempmutex);
        mpz_sub(temp.z, max->z, min->z);
        mpz_urandomm(r->z, RANDSTATE, temp.z);
+       d0_unlockmutex(tempmutex);
        mpz_add(r->z, r->z, min->z);
        return r;
 }
@@ -261,14 +320,18 @@ d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const
 d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
+       d0_lockmutex(tempmutex);
        mpz_urandomb(r->z, RANDSTATE, n);
+       d0_unlockmutex(tempmutex);
        return r;
 }
 
 d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n)
 {
        if(!r) r = d0_bignum_new(); if(!r) return NULL;
+       d0_lockmutex(tempmutex);
        mpz_urandomb(r->z, RANDSTATE, n-1);
+       d0_unlockmutex(tempmutex);
        mpz_setbit(r->z, n-1);
        return r;
 }
@@ -409,5 +472,5 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const
 
 char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base)
 {
-       return mpz_get_str(NULL, base, x->z);
+       return mpz_get_str(NULL, base, x->z); // this allocates!
 }