]> git.xonotic.org Git - xonotic/d0_blind_id.git/blobdiff - d0_bignum-gmp.c
turn some buffers into TLS
[xonotic/d0_blind_id.git] / d0_bignum-gmp.c
index 41e1a2087d0480ad538ca70387e9eb563e59df82..535370454463f931a1be8a97b69bd517cfb68bfb 100644 (file)
@@ -29,7 +29,8 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $Format:commit %H$, $Id$
+ * $Format:commit %H$
+ * $Id$
  */
 
 /* NOTE: this file links against libgmp (http://gmplib.org), which is under the
@@ -55,16 +56,16 @@ struct d0_bignum_s
        mpz_t z;
 };
 
-static gmp_randstate_t RANDSTATE;
-static d0_bignum_t temp;
+static gmp_randstate_t RANDSTATE; // FIXME make threadsafe
+static d0_bignum_t temp; // FIXME make threadsafe
 
 #include <time.h>
 #include <stdio.h>
 
-WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void)
+D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void)
 {
        FILE *f;
-       BOOL ret = 1;
+       D0_BOOL ret = 1;
        unsigned char buf[256];
        d0_bignum_init(&temp);
        gmp_randinit_mt(RANDSTATE);
@@ -130,9 +131,9 @@ void d0_bignum_SHUTDOWN(void)
        gmp_randclear(RANDSTATE);
 }
 
-BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
+D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 {
-       static unsigned char numbuf[65536];
+       static __thread unsigned char numbuf[65536];
        size_t count = 0;
        numbuf[0] = mpz_sgn(bignum->z) & 3;
        if((numbuf[0] & 3) != 0) // nonzero
@@ -147,7 +148,7 @@ BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum)
 
 d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum)
 {
-       static unsigned char numbuf[65536];
+       static __thread unsigned char numbuf[65536];
        size_t count = sizeof(numbuf);
        if(!d0_iobuf_read_packet(buf, numbuf, &count))
                return NULL;
@@ -194,7 +195,7 @@ ssize_t d0_bignum_export_unsigned(const d0_bignum_t *bignum, void *buf, size_t b
                // BAD
                // mpz_sizeinbase lied to us
                // move the number
-               if(bufsize == 0)
+               if(count == 0)
                {
                        memset(buf, 0, count);
                }
@@ -362,6 +363,13 @@ d0_bignum_t *d0_bignum_mod_add(d0_bignum_t *r, const d0_bignum_t *a, const d0_bi
        return r;
 }
 
+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)
+{
+       r = d0_bignum_sub(r, a, b);
+       mpz_fdiv_r(r->z, r->z, m->z);
+       return r;
+}
+
 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)
 {
        r = d0_bignum_mul(r, a, b);
@@ -376,13 +384,13 @@ d0_bignum_t *d0_bignum_mod_pow(d0_bignum_t *r, const d0_bignum_t *a, const d0_bi
        return r;
 }
 
-BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
+D0_BOOL d0_bignum_mod_inv(d0_bignum_t *r, const d0_bignum_t *a, const d0_bignum_t *m)
 {
        // here, r MUST be set, as otherwise we cannot return error state!
        return mpz_invert(r->z, a->z, m->z);
 }
 
-int d0_bignum_isprime(d0_bignum_t *r, int param)
+int d0_bignum_isprime(const d0_bignum_t *r, int param)
 {
        return mpz_probab_prime_p(r->z, param);
 }