* 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
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);
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
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;
// BAD
// mpz_sizeinbase lied to us
// move the number
- if(bufsize == 0)
+ if(count == 0)
{
memset(buf, 0, count);
}
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);
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);
}