Example #1
0
static PyObject *
GMPy_MPZ_Ior_Slot(PyObject *self, PyObject *other)
{
    MPZ_Object *result;

    if (CHECK_MPZANY(self)) {
        if (CHECK_MPZANY(other)) {
            if (!(result = GMPy_MPZ_New(NULL)))
                return NULL;
            mpz_ior(result->z, MPZ(self), MPZ(other));
        }
        else {
            if (!(result = GMPy_MPZ_From_Integer(other, NULL)))
                return NULL;
            mpz_ior(result->z, MPZ(self), result->z);
        }
    }
    else if (CHECK_MPZANY(other)) {
        if (!(result = GMPy_MPZ_From_Integer(self, NULL)))
            return NULL;
        mpz_ior(result->z, result->z, MPZ(other));
    }
    else {
        Py_RETURN_NOTIMPLEMENTED;
    }
    return (PyObject*)result;
}
Example #2
0
void
mpc_ior (mpc_t *rop, mpc_t op1, mpc_t op2)
{
  mpc_t temp;
  
  temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision;
  mpz_init (temp.object);

  if (op1.precision == op2.precision)
    mpz_ior (temp.object, op1.object, op2.object);
  else if (op1.precision > op2.precision)
    {
      mpz_set (temp.object, op2.object);
      power_of_ten (temp.object, op1.precision - op2.precision);
      mpz_ior (temp.object, op1.object, temp.object);
    }
  else
    {
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, op2.precision - op1.precision);
      mpz_ior (temp.object, temp.object, op2.object);
    }

  rop->precision = temp.precision;
  mpz_set (rop->object, temp.object);
}
Example #3
0
static object
integer_log_op2(fixnum op,object x,enum type tx,object y,enum type ty) {

  object u=big_fixnum1;
  object ux=tx==t_bignum ? x : (mpz_set_si(MP(big_fixnum2),fix(x)), big_fixnum2);
  object uy=ty==t_bignum ? y : (mpz_set_si(MP(big_fixnum3),fix(y)), big_fixnum3);
  
  switch(op) {
  case BOOLCLR:	 mpz_set_si(MP(u),0);break;
  case BOOLSET:	 mpz_set_si(MP(u),-1);break;
  case BOOL1:	 mpz_set(MP(u),MP(ux));break;
  case BOOL2:	 mpz_set(MP(u),MP(uy));break;
  case BOOLC1:	 mpz_com(MP(u),MP(ux));break;
  case BOOLC2:	 mpz_com(MP(u),MP(uy));break;
  case BOOLAND:	 mpz_and(MP(u),MP(ux),MP(uy));break;
  case BOOLIOR:	 mpz_ior(MP(u),MP(ux),MP(uy));break;
  case BOOLXOR:	 mpz_xor(MP(u),MP(ux),MP(uy));break;
  case BOOLEQV:	 mpz_xor(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLNAND: mpz_and(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLNOR:	 mpz_ior(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLANDC1:mpz_com(MP(u),MP(ux));mpz_and(MP(u),MP(u),MP(uy));break;
  case BOOLANDC2:mpz_com(MP(u),MP(uy));mpz_and(MP(u),MP(ux),MP(u));break;
  case BOOLORC1: mpz_com(MP(u),MP(ux));mpz_ior(MP(u),MP(u),MP(uy));break;
  case BOOLORC2: mpz_com(MP(u),MP(uy));mpz_ior(MP(u),MP(ux),MP(u));break;
  default:break;/*FIXME error*/
  }
    
  return u;

}
Example #4
0
static PyObject *
GMPy_XMPZ_IIor_Slot(PyObject *self, PyObject *other)
{
    if(CHECK_MPZANY(other)) {
        mpz_ior(MPZ(self), MPZ(self), MPZ(other));
        Py_INCREF(self);
        return self;
    }

    if(PyIntOrLong_Check(other)) {
        mpz_set_PyIntOrLong(global.tempz, other);
        mpz_ior(MPZ(self), MPZ(self), global.tempz);
        Py_INCREF(self);
        return self;
    }

    Py_RETURN_NOTIMPLEMENTED;
}
Example #5
0
static _rs_inline obj bignum_or( obj a, obj b )
{
    mpz_t r, a1, b1;

    OBJ_TO_MPZ(a1, a);
    OBJ_TO_MPZ(b1, b);

    mpz_init(r);
    mpz_ior(r, a1, b1);
    return mpz_to_bignum(r);
}
Example #6
0
VAL bigOr(VM* vm, VAL x, VAL y) {
    idris_requireAlloc(IDRIS_MAXGMP);

    mpz_t* bigint;
    VAL cl = allocate(sizeof(Closure) + sizeof(mpz_t), 0);
    idris_doneAlloc();
    bigint = (mpz_t*)(((char*)cl) + sizeof(Closure));
    mpz_ior(*bigint, GETMPZ(GETBIG(vm,x)), GETMPZ(GETBIG(vm,y)));
    SETTY(cl, CT_BIGINT);
    cl -> info.ptr = (void*)bigint;
    return cl;
}
Example #7
0
static PyObject *
Pyxmpz_inplace_ior(PyObject *self, PyObject *other)
{
    mpz_t tempz;

    if(CHECK_MPZANY(other)) {
        mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(other));
        Py_INCREF(self);
        return self;
    }

    if(PyIntOrLong_Check(other)) {
        mpz_inoc(tempz);
        mpz_set_PyIntOrLong(tempz, other);
        mpz_ior(Pyxmpz_AS_MPZ(self), Pyxmpz_AS_MPZ(self), tempz);
        mpz_cloc(tempz);
        Py_INCREF(self);
        return self;
    }

    Py_RETURN_NOTIMPLEMENTED;
}
Example #8
0
/* RecoverSign : sign extend a MP_INT with the bit width-1 */
void RecoverSign (PtrMP_INT val, Bits width)
{
    mpz_setbit (val, width);

    if (mpz_scan1 (val, width - 1) == width - 1)
    {
        PtrMP_INT bitMask = MakeMaskForRange (width, 0);

        mpz_com (bitMask, bitMask); /* 00001111 -> 11110000 */
        mpz_ior (val, val, bitMask);
        DeleteMP_INT (bitMask);
    } else
        mpz_clrbit (val, width);
}
Example #9
0
/* u4_op_con():
**
**   Produce (a | b).
*/
u4_atom
u4_op_con(u4_lane lane,
          u4_atom a,
          u4_atom b)
{
  mpz_t mp_a, mp_b;

  u4_a_gmp(a, mp_a);
  u4_a_gmp(b, mp_b);

  mpz_ior(mp_a, mp_a, mp_b);
  mpz_clear(mp_b);

  return u4_k_atom_gmp(lane, mp_a);
}
Example #10
0
num_t
num_int_two_op(optype_t op_type, num_t a, num_t b)
{
	num_t r;

	r = num_new_z(N_TEMP, NULL);
	a = num_new_z(N_TEMP, a);
	b = num_new_z(N_TEMP, b);

	switch (op_type) {
	case OP_AND:
		mpz_and(Z(r), Z(a), Z(b));
		break;

	case OP_OR:
		mpz_ior(Z(r), Z(a), Z(b));
		break;

	case OP_XOR:
		mpz_xor(Z(r), Z(a), Z(b));
		break;

	case OP_SHR:
		if (!mpz_fits_ulong_p(Z(b))) {
			yyxerror
			    ("Second argument to shift needs to fit into an unsigned long C datatype");
			return NULL;
		}
		mpz_fdiv_q_2exp(Z(r), Z(a), mpz_get_ui(Z(b)));
		break;

	case OP_SHL:
		if (!mpz_fits_ulong_p(Z(b))) {
			yyxerror
			    ("Second argument to shift needs to fit into an unsigned long C datatype");
			return NULL;
		}
		mpz_mul_2exp(Z(r), Z(a), mpz_get_ui(Z(b)));
		break;

	default:
		yyxerror("Unknown op in num_int_two_op");
	}

	return r;
}
Example #11
0
static _rs_inline obj bignum_xor( obj a, obj b )
{
    mpz_t r, a1, b1, a2, b2;

    OBJ_TO_MPZ(a1, a);
    OBJ_TO_MPZ(b1, b);

    mpz_init(r);
    mpz_init(a2);
    mpz_init(b2);
    mpz_com(a2, a1);
    mpz_com(b2, b1);

    mpz_and(r, a1, b2);
    mpz_and(b2, a2, b1);
    mpz_ior(a1, r, b2);
    return mpz_to_bignum(a1);
}
Example #12
0
static obj int64_to_bignum_u(INT_64 a)
{
    INT_32 p_hi, p_lo;
    mpz_t z1, z2, z3;

#ifndef HAVE_INT_64
    p_hi = a.digits[0] << 16 | a.digits[1];
    p_lo = a.digits[2] << 16 | a.digits[3];
#else
    p_hi = ((unsigned)a) >> 32;
    p_lo = a & 0xffffffff;
#endif

    mpz_init_set_ui(z1, p_hi);
    mpz_init(z2);
    mpz_init(z3);
    mpz_mul_2exp(z2, z1, 32);
    mpz_set_ui(z1, p_lo);
    mpz_ior(z3, z1, z2);
    return mpz_to_bignum(z3);
}
Example #13
0
// gets a random prime of the specified length
// return 1 if probably prime, and 2 if definitely prime
// takes random number generator that may produce numbers with a certain congruency
int rand_prime_3mod4(mpz_t p, mp_bitcnt_t bits) {
	int primality;
	mpz_t three;

	mpz_init_set_ui(three, 3);

	for(;;) {
		if(debug)fprintf(stderr, "Generating a large number ...\n");

		//use the generator function to get random numbers
		rand_bigint(p,bits);
		mpz_ior(p,p,three);
	
		if(debug)fprintf(stderr, "Testing a possible prime ...\n");
		primality = mpz_probab_prime_p(p, IRONPIPE_PRIME_TEST_REPS);
		if(primality) {
			if(debug)fprintf(stderr, "Got a prime!\n");
			return primality;
		}
	}
}
Example #14
0
//------------------------------------------------------------------------------
// Name: bitwise_or
//------------------------------------------------------------------------------
knumber_base *knumber_integer::bitwise_or(knumber_base *rhs) {

	if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) {
		mpz_ior(mpz_, mpz_, p->mpz_);
		return this;
	} else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) {
		knumber_float *f = new knumber_float(this);
		delete this;
		return f->bitwise_or(p);
	} else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) {
		knumber_fraction *f = new knumber_fraction(this);
		delete this;
		return f->bitwise_or(p);
	} else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) {
		delete this;
		return p->clone();
	}

	Q_ASSERT(0);
	return 0;
}
Example #15
0
static Variant HHVM_FUNCTION(gmp_or,
                             const Variant& dataA,
                             const Variant& dataB) {
  mpz_t gmpDataA, gmpDataB, gmpReturn;

  if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_OR, gmpDataA, dataA)) {
    return false;
  }
  if (!variantToGMPData(cs_GMP_FUNC_NAME_GMP_OR, gmpDataB, dataB)) {
    mpz_clear(gmpDataA);
    return false;
  }

  mpz_init(gmpReturn);
  mpz_ior(gmpReturn, gmpDataA, gmpDataB);

  Variant ret = NEWOBJ(GMPResource)(gmpReturn);

  mpz_clear(gmpDataA);
  mpz_clear(gmpDataB);
  mpz_clear(gmpReturn);

  return ret;
}
Example #16
0
void
hex_random_op3 (enum hex_random_op op,  unsigned long maxbits,
		char **ap, char **bp, char **rp)
{
  mpz_t a, b, r;
  unsigned long abits, bbits;
  unsigned signs;

  mpz_init (a);
  mpz_init (b);
  mpz_init (r);

  abits = gmp_urandomb_ui (state, 32) % maxbits;
  bbits = gmp_urandomb_ui (state, 32) % maxbits;

  mpz_rrandomb (a, state, abits);
  mpz_rrandomb (b, state, bbits);

  signs = gmp_urandomb_ui (state, 3);
  if (signs & 1)
    mpz_neg (a, a);
  if (signs & 2)
    mpz_neg (b, b);

  switch (op)
    {
    default:
      abort ();
    case OP_ADD:
      mpz_add (r, a, b);
      break;
    case OP_SUB:
      mpz_sub (r, a, b);
      break;
    case OP_MUL:
      mpz_mul (r, a, b);
      break;
    case OP_GCD:
      if (signs & 4)
	{
	  /* Produce a large gcd */
	  unsigned long gbits = gmp_urandomb_ui (state, 32) % maxbits;
	  mpz_rrandomb (r, state, gbits);
	  mpz_mul (a, a, r);
	  mpz_mul (b, b, r);
	}
      mpz_gcd (r, a, b);
      break;
    case OP_LCM:
      if (signs & 4)
	{
	  /* Produce a large gcd */
	  unsigned long gbits = gmp_urandomb_ui (state, 32) % maxbits;
	  mpz_rrandomb (r, state, gbits);
	  mpz_mul (a, a, r);
	  mpz_mul (b, b, r);
	}
      mpz_lcm (r, a, b);
      break;
    case OP_AND:
      mpz_and (r, a, b);
      break;
    case OP_IOR:
      mpz_ior (r, a, b);
      break;
    case OP_XOR:
      mpz_xor (r, a, b);
      break;
    }

  gmp_asprintf (ap, "%Zx", a);
  gmp_asprintf (bp, "%Zx", b);
  gmp_asprintf (rp, "%Zx", r);

  mpz_clear (a);
  mpz_clear (b);
  mpz_clear (r);
}
Example #17
0
/* Evaluate the expression E and put the result in R.  */
void
mpz_eval_expr (mpz_ptr r, expr_t e)
{
  mpz_t lhs, rhs;

  switch (e->op)
    {
    case LIT:
      mpz_set (r, e->operands.val);
      return;
    case PLUS:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_add (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MINUS:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_sub (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MULT:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_mul (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case DIV:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_fdiv_q (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case MOD:
      mpz_init (rhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_abs (rhs, rhs);
      mpz_eval_mod_expr (r, e->operands.ops.lhs, rhs);
      mpz_clear (rhs);
      return;
    case REM:
      /* Check if lhs operand is POW expression and optimize for that case.  */
      if (e->operands.ops.lhs->op == POW)
	{
	  mpz_t powlhs, powrhs;
	  mpz_init (powlhs);
	  mpz_init (powrhs);
	  mpz_init (rhs);
	  mpz_eval_expr (powlhs, e->operands.ops.lhs->operands.ops.lhs);
	  mpz_eval_expr (powrhs, e->operands.ops.lhs->operands.ops.rhs);
	  mpz_eval_expr (rhs, e->operands.ops.rhs);
	  mpz_powm (r, powlhs, powrhs, rhs);
	  if (mpz_cmp_si (rhs, 0L) < 0)
	    mpz_neg (r, r);
	  mpz_clear (powlhs);
	  mpz_clear (powrhs);
	  mpz_clear (rhs);
	  return;
	}

      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_fdiv_r (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION >= 2
    case INVMOD:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_invert (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case POW:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_cmpabs_ui (lhs, 1) <= 0)
	{
	  /* For 0^rhs and 1^rhs, we just need to verify that
	     rhs is well-defined.  For (-1)^rhs we need to
	     determine (rhs mod 2).  For simplicity, compute
	     (rhs mod 2) for all three cases.  */
	  expr_t two, et;
	  two = malloc (sizeof (struct expr));
	  two -> op = LIT;
	  mpz_init_set_ui (two->operands.val, 2L);
	  makeexp (&et, MOD, e->operands.ops.rhs, two);
	  e->operands.ops.rhs = et;
	}

      mpz_eval_expr (rhs, e->operands.ops.rhs);
      if (mpz_cmp_si (rhs, 0L) == 0)
	/* x^0 is 1 */
	mpz_set_ui (r, 1L);
      else if (mpz_cmp_si (lhs, 0L) == 0)
	/* 0^y (where y != 0) is 0 */
	mpz_set_ui (r, 0L);
      else if (mpz_cmp_ui (lhs, 1L) == 0)
	/* 1^y is 1 */
	mpz_set_ui (r, 1L);
      else if (mpz_cmp_si (lhs, -1L) == 0)
	/* (-1)^y just depends on whether y is even or odd */
	mpz_set_si (r, (mpz_get_ui (rhs) & 1) ? -1L : 1L);
      else if (mpz_cmp_si (rhs, 0L) < 0)
	/* x^(-n) is 0 */
	mpz_set_ui (r, 0L);
      else
	{
	  unsigned long int cnt;
	  unsigned long int y;
	  /* error if exponent does not fit into an unsigned long int.  */
	  if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	    goto pow_err;

	  y = mpz_get_ui (rhs);
	  /* x^y == (x/(2^c))^y * 2^(c*y) */
#if __GNU_MP_VERSION >= 2
	  cnt = mpz_scan1 (lhs, 0);
#else
	  cnt = 0;
#endif
	  if (cnt != 0)
	    {
	      if (y * cnt / cnt != y)
		goto pow_err;
	      mpz_tdiv_q_2exp (lhs, lhs, cnt);
	      mpz_pow_ui (r, lhs, y);
	      mpz_mul_2exp (r, r, y * cnt);
	    }
	  else
	    mpz_pow_ui (r, lhs, y);
	}
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    pow_err:
      error = "result of `pow' operator too large";
      mpz_clear (lhs); mpz_clear (rhs);
      longjmp (errjmpbuf, 1);
    case GCD:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_gcd (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case LCM:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_lcm (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case AND:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_and (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case IOR:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_ior (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case XOR:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      mpz_xor (r, lhs, rhs);
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case NEG:
      mpz_eval_expr (r, e->operands.ops.lhs);
      mpz_neg (r, r);
      return;
    case NOT:
      mpz_eval_expr (r, e->operands.ops.lhs);
      mpz_com (r, r);
      return;
    case SQRT:
      mpz_init (lhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_sgn (lhs) < 0)
	{
	  error = "cannot take square root of negative numbers";
	  mpz_clear (lhs);
	  longjmp (errjmpbuf, 1);
	}
      mpz_sqrt (r, lhs);
      return;
#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
    case ROOT:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      if (mpz_sgn (rhs) <= 0)
	{
	  error = "cannot take non-positive root orders";
	  mpz_clear (lhs); mpz_clear (rhs);
	  longjmp (errjmpbuf, 1);
	}
      if (mpz_sgn (lhs) < 0 && (mpz_get_ui (rhs) & 1) == 0)
	{
	  error = "cannot take even root orders of negative numbers";
	  mpz_clear (lhs); mpz_clear (rhs);
	  longjmp (errjmpbuf, 1);
	}

      {
	unsigned long int nth = mpz_get_ui (rhs);
	if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	  {
	    /* If we are asked to take an awfully large root order, cheat and
	       ask for the largest order we can pass to mpz_root.  This saves
	       some error prone special cases.  */
	    nth = ~(unsigned long int) 0;
	  }
	mpz_root (r, lhs, nth);
      }
      mpz_clear (lhs); mpz_clear (rhs);
      return;
#endif
    case FAC:
      mpz_eval_expr (r, e->operands.ops.lhs);
      if (mpz_size (r) > 1)
	{
	  error = "result of `!' operator too large";
	  longjmp (errjmpbuf, 1);
	}
      mpz_fac_ui (r, mpz_get_ui (r));
      return;
#if __GNU_MP_VERSION >= 2
    case POPCNT:
      mpz_eval_expr (r, e->operands.ops.lhs);
      { long int cnt;
	cnt = mpz_popcount (r);
	mpz_set_si (r, cnt);
      }
      return;
    case HAMDIST:
      { long int cnt;
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	mpz_eval_expr (rhs, e->operands.ops.rhs);
	cnt = mpz_hamdist (lhs, rhs);
	mpz_clear (lhs); mpz_clear (rhs);
	mpz_set_si (r, cnt);
      }
      return;
#endif
    case LOG2:
      mpz_eval_expr (r, e->operands.ops.lhs);
      { unsigned long int cnt;
	if (mpz_sgn (r) <= 0)
	  {
	    error = "logarithm of non-positive number";
	    longjmp (errjmpbuf, 1);
	  }
	cnt = mpz_sizeinbase (r, 2);
	mpz_set_ui (r, cnt - 1);
      }
      return;
    case LOG:
      { unsigned long int cnt;
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	mpz_eval_expr (rhs, e->operands.ops.rhs);
	if (mpz_sgn (lhs) <= 0)
	  {
	    error = "logarithm of non-positive number";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	if (mpz_cmp_ui (rhs, 256) >= 0)
	  {
	    error = "logarithm base too large";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	cnt = mpz_sizeinbase (lhs, mpz_get_ui (rhs));
	mpz_set_ui (r, cnt - 1);
	mpz_clear (lhs); mpz_clear (rhs);
      }
      return;
    case FERMAT:
      {
	unsigned long int t;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	t = (unsigned long int) 1 << mpz_get_ui (lhs);
	if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0 || t == 0)
	  {
	    error = "too large Mersenne number index";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	mpz_set_ui (r, 1);
	mpz_mul_2exp (r, r, t);
	mpz_add_ui (r, r, 1);
	mpz_clear (lhs);
      }
      return;
    case MERSENNE:
      mpz_init (lhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      if (mpz_cmp_ui (lhs, ~(unsigned long int) 0) > 0)
	{
	  error = "too large Mersenne number index";
	  mpz_clear (lhs);
	  longjmp (errjmpbuf, 1);
	}
      mpz_set_ui (r, 1);
      mpz_mul_2exp (r, r, mpz_get_ui (lhs));
      mpz_sub_ui (r, r, 1);
      mpz_clear (lhs);
      return;
    case FIBONACCI:
      { mpz_t t;
	unsigned long int n, i;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0)
	  {
	    error = "Fibonacci index out of range";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	n = mpz_get_ui (lhs);
	mpz_clear (lhs);

#if __GNU_MP_VERSION > 2 || __GNU_MP_VERSION_MINOR >= 1
	mpz_fib_ui (r, n);
#else
	mpz_init_set_ui (t, 1);
	mpz_set_ui (r, 1);

	if (n <= 2)
	  mpz_set_ui (r, 1);
	else
	  {
	    for (i = 3; i <= n; i++)
	      {
		mpz_add (t, t, r);
		mpz_swap (t, r);
	      }
	  }
	mpz_clear (t);
#endif
      }
      return;
    case RANDOM:
      {
	unsigned long int n;
	mpz_init (lhs);
	mpz_eval_expr (lhs, e->operands.ops.lhs);
	if (mpz_sgn (lhs) <= 0 || mpz_cmp_si (lhs, 1000000000) > 0)
	  {
	    error = "random number size out of range";
	    mpz_clear (lhs);
	    longjmp (errjmpbuf, 1);
	  }
	n = mpz_get_ui (lhs);
	mpz_clear (lhs);
	mpz_urandomb (r, rstate, n);
      }
      return;
    case NEXTPRIME:
      {
	mpz_eval_expr (r, e->operands.ops.lhs);
	mpz_nextprime (r, r);
      }
      return;
    case BINOM:
      mpz_init (lhs); mpz_init (rhs);
      mpz_eval_expr (lhs, e->operands.ops.lhs);
      mpz_eval_expr (rhs, e->operands.ops.rhs);
      {
	unsigned long int k;
	if (mpz_cmp_ui (rhs, ~(unsigned long int) 0) > 0)
	  {
	    error = "k too large in (n over k) expression";
	    mpz_clear (lhs); mpz_clear (rhs);
	    longjmp (errjmpbuf, 1);
	  }
	k = mpz_get_ui (rhs);
	mpz_bin_ui (r, lhs, k);
      }
      mpz_clear (lhs); mpz_clear (rhs);
      return;
    case TIMING:
      {
	int t0;
	t0 = cputime ();
	mpz_eval_expr (r, e->operands.ops.lhs);
	printf ("time: %d\n", cputime () - t0);
      }
      return;
    default:
      abort ();
    }
}
Example #18
0
File: big.o.c Project: hoobaa/mecl
static void
mpz_ior_op(cl_object out, cl_object i, cl_object j)
{
	mpz_ior(out->big.big_num, i->big.big_num, j->big.big_num);
}
Example #19
0
File: big.o.c Project: hoobaa/mecl
static void
mpz_orc1_op(cl_object out, cl_object i, cl_object j)
{
	mpz_com(out->big.big_num, i->big.big_num);
	mpz_ior(out->big.big_num, out->big.big_num, j->big.big_num);
}
Example #20
0
void rsa_random_prime(MP_INT *ret, RandomState *state, unsigned int bits)

{

  MP_INT start, aux;

  unsigned int num_primes;

  int *moduli;

  long difference;



  mpz_init(&start);

  mpz_init(&aux);



 retry:



  /* Pick a random integer of the appropriate size. */

  rsa_random_integer(&start, state, bits);



  /* Set the two highest bits. */

  mpz_set_ui(&aux, 3);

  mpz_mul_2exp(&aux, &aux, bits - 2);

  mpz_ior(&start, &start, &aux);

  /* Set the lowest bit to make it odd. */

  mpz_set_ui(&aux, 1);

  mpz_ior(&start, &start, &aux);



  /* Initialize moduli of the small primes with respect to the given

     random number. */

  moduli = xmalloc(MAX_PRIMES_IN_TABLE * sizeof(moduli[0]));

  if (bits < 16)

    num_primes = 0; /* Don\'t use the table for very small numbers. */

  else

    {

      for (num_primes = 0; small_primes[num_primes] != 0; num_primes++)

       {

         mpz_mod_ui(&aux, &start, small_primes[num_primes]);

         moduli[num_primes] = mpz_get_ui(&aux);

       }

    }



  /* Look for numbers that are not evenly divisible by any of the small

     primes. */

  for (difference = 0; ; difference += 2)

    {

      unsigned int i;



      if (difference > 0x70000000)

       { /* Should never happen, I think... */

         if (rsa_verbose)

           fprintf(stderr,

                  "rsa_random_prime: failed to find a prime, retrying.\n");

         xfree(moduli);

         goto retry;

       }



      /* Check if it is a multiple of any small prime.  Note that this

        updates the moduli into negative values as difference grows. */

      for (i = 0; i < num_primes; i++)

       {

         while (moduli[i] + difference >= small_primes[i])

           moduli[i] -= small_primes[i];

         if (moduli[i] + difference == 0)

           break;

       }

      if (i < num_primes)

       continue; /* Multiple of a known prime. */



      /* It passed the small prime test (not divisible by any of them). */

      if (rsa_verbose)

       {

         fprintf(stderr, ".");

       }



      /* Compute the number in question. */

      mpz_add_ui(ret, &start, difference);



      /* Perform the fermat test for witness 2.  This means:

        it is not prime if 2^n mod n != 2. */

      mpz_set_ui(&aux, 2);

      mpz_powm(&aux, &aux, ret, ret);

      if (mpz_cmp_ui(&aux, 2) == 0)

       {

         /* Passed the fermat test for witness 2. */

         if (rsa_verbose)

           {

             fprintf(stderr, "+");

           }

         /* Perform a more tests.  These are probably unnecessary. */

         if (mpz_probab_prime_p(ret, 20))

           break; /* It is a prime with probability 1 - 2^-40. */

       }

    }



  /* Found a (probable) prime.  It is in ret. */

  if (rsa_verbose)

    {

      fprintf(stderr, "+ (distance %ld)\n", difference);

    }



  /* Free the small prime moduli; they are no longer needed. */

  xfree(moduli);



  /* Sanity check: does it still have the high bit set (we might have

     wrapped around)? */

  mpz_div_2exp(&aux, ret, bits - 1);

  if (mpz_get_ui(&aux) != 1)

    {

      if (rsa_verbose)

       fprintf(stderr, "rsa_random_prime: high bit not set, retrying.\n");

      goto retry;

    }

  mpz_clear(&start);

  mpz_clear(&aux);

  /* Return value already set in ret. */

}
void ConstFoldBinop(BinopKind kind,
                    const mpz_t left_val, const mpz_t right_val, mpz_t res)
{
  switch (kind) {

  case B_Plus:
    mpz_add(res, left_val, right_val);
    break;

  case B_Minus:
  case B_MinusPP: // TODO: include scaling here?
    mpz_sub(res, left_val, right_val);
    break;

  case B_Mult:
    mpz_mul(res, left_val, right_val);
    break;

  case B_Div:
  case B_DivExact:  // treat like regular division during folding.
    // leave zero value for division by zero.
    if (mpz_cmp_si(right_val, 0) != 0)
      mpz_fdiv_q(res, left_val, right_val);
    break;

  case B_Mod:
    // ditto for modulus by zero or negative values.
    if (mpz_cmp_si(right_val, 0) > 0)
      mpz_fdiv_r(res, left_val, right_val);
    break;

  case B_ShiftLeft:
    // ditto for left shifts by negative or large values.
    if (mpz_cmp_si(right_val, 0) >= 0 &&
        mpz_cmp_si(right_val, 64) <= 0)
      mpz_mul_2exp(res, left_val, mpz_get_ui(right_val));
    break;

  case B_ShiftRight:
    // ditto for right shifts by negative values or large values.
    if (mpz_cmp_si(right_val, 0) >= 0 &&
        mpz_cmp_si(right_val, 64) <= 0)
      mpz_tdiv_q_2exp(res, left_val, mpz_get_ui(right_val));
    break;

  case B_BitwiseAnd:
    mpz_and(res, left_val, right_val);
    break;

  case B_BitwiseOr:
    mpz_ior(res, left_val, right_val);
    break;

  case B_BitwiseXOr:
    mpz_xor(res, left_val, right_val);
    break;

  case B_Min:
    if (mpz_cmp(left_val, right_val) < 0)
      mpz_set(res, left_val);
    else
      mpz_set(res, right_val);
    break;

  case B_Max:
    if (mpz_cmp(left_val, right_val) > 0)
      mpz_set(res, left_val);
    else
      mpz_set(res, right_val);
    break;

  case B_LessThan:
  case B_LessThanP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) < 0);
    break;

  case B_GreaterThan:
  case B_GreaterThanP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) > 0);
    break;

  case B_LessEqual:
  case B_LessEqualP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) <= 0);
    break;

  case B_GreaterEqual:
  case B_GreaterEqualP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) >= 0);
    break;

  case B_Equal:
  case B_EqualP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) == 0);
    break;

  case B_NotEqual:
  case B_NotEqualP:
    FoldBoolean(res, mpz_cmp(left_val, right_val) != 0);
    break;

  case B_LogicalAnd:
    FoldBoolean(res, mpz_cmp_si(left_val, 0) != 0 &&
                     mpz_cmp_si(right_val, 0) != 0);
    break;

  case B_LogicalOr:
    FoldBoolean(res, mpz_cmp_si(left_val, 0) != 0 ||
                     mpz_cmp_si(right_val, 0) != 0);
    break;

  default:
    logout << "ERROR: ConstFoldBinop: Unexpected "
           << BinopString(kind)
           << " " << left_val << " " << right_val << endl;
    Assert(false);
  }
}
Example #22
0
File: bit.c Project: mahdiz/mpclib
void
check_random (int argc, char *argv[])
{
  mpz_t x, s0, s1, s2, s3, m;
  mp_size_t xsize;
  int i;
  int reps = 100000;
  int bit0, bit1, bit2, bit3;
  unsigned long int bitindex;
  const char  *s = "";

  if (argc == 2)
    reps = atoi (argv[1]);

  mpz_init (x);
  mpz_init (s0);
  mpz_init (s1);
  mpz_init (s2);
  mpz_init (s3);
  mpz_init (m);

  for (i = 0; i < reps; i++)
    {
      xsize = urandom () % (2 * SIZE) - SIZE;
      mpz_random2 (x, xsize);
      bitindex = urandom () % SIZE;

      mpz_set (s0, x);
      bit0 = mpz_tstbit (x, bitindex);
      mpz_setbit (x, bitindex);
      MPZ_CHECK_FORMAT (x);

      mpz_set (s1, x);
      bit1 = mpz_tstbit (x, bitindex);
      mpz_clrbit (x, bitindex);
      MPZ_CHECK_FORMAT (x);

      mpz_set (s2, x);
      bit2 = mpz_tstbit (x, bitindex);
      mpz_setbit (x, bitindex);
      MPZ_CHECK_FORMAT (x);

      mpz_set (s3, x);
      bit3 = mpz_tstbit (x, bitindex);

#define FAIL(str) do { s = str; goto fail; } while (0)

      if (bit1 != 1)  FAIL ("bit1 != 1");
      if (bit2 != 0)  FAIL ("bit2 != 0");
      if (bit3 != 1)  FAIL ("bit3 != 1");

      if (bit0 == 0)
	{
	  if (mpz_cmp (s0, s1) == 0 || mpz_cmp (s0, s2) != 0 || mpz_cmp (s0, s3) == 0)
	    abort ();
	}
      else
	{
	  if (mpz_cmp (s0, s1) != 0 || mpz_cmp (s0, s2) == 0 || mpz_cmp (s0, s3) != 0)
	    abort ();
	}

      if (mpz_cmp (s1, s2) == 0 || mpz_cmp (s1, s3) != 0)
	abort ();
      if (mpz_cmp (s2, s3) == 0)
	abort ();

      mpz_ui_pow_ui (m, 2L, bitindex);
      MPZ_CHECK_FORMAT (m);
      mpz_ior (x, s2, m);
      MPZ_CHECK_FORMAT (x);
      if (mpz_cmp (x, s3) != 0)
	abort ();

      mpz_com (m, m);
      MPZ_CHECK_FORMAT (m);
      mpz_and (x, s1, m);
      MPZ_CHECK_FORMAT (x);
      if (mpz_cmp (x, s2) != 0)
	abort ();
    }

  mpz_clear (x);
  mpz_clear (s0);
  mpz_clear (s1);
  mpz_clear (s2);
  mpz_clear (s3);
  mpz_clear (m);
  return;


 fail:
  printf ("%s\n", s);
  printf ("bitindex = %lu\n", bitindex);
  printf ("x = "); mpz_out_str (stdout, -16, x); printf (" hex\n");
  exit (1);
}
Example #23
0
void C_BigInt::operator |= (const C_BigInt inOperand) {
  mpz_ior (mGMPint, mGMPint, inOperand.mGMPint) ;
}