Пример #1
0
 void log2(const mpz_t n, mpz_t l) {
   mpf_t tmp, tmp2;
   mpf_inits(tmp, tmp2, NULL);
   mpf_set_z(tmp, n);
   log2(tmp, tmp2);
   mpz_set_f(l, tmp2);
   mpf_clears(tmp, tmp2, NULL);        
 }
Пример #2
0
 void log10(const mpz_t n, mpz_t l, int prec) {
   mpf_t tmp, tmp2;
   mpf_inits(tmp, tmp2, NULL);
   mpf_set_z(tmp, n);
   log10(tmp, tmp2, prec);
   mpz_set_f(l, tmp2);
   mpf_clears(tmp, tmp2, NULL);    
 }
Пример #3
0
 void log(const mpz_t n, mpz_t l, mpz_t b) {
   mpf_t tmp, tmp2, tmp3;
   mpf_inits(tmp, tmp2, tmp3, NULL);
   mpf_set_z(tmp, n);
   mpf_set_z(tmp3, b);    
   log(tmp, tmp2, tmp3);
   mpz_set_f(l, tmp2);
   mpf_clears(tmp, tmp2, tmp3, NULL);            
 }
Пример #4
0
/*! Covert floating point number to integer

    The caller is responsible for freeing the allocated integer number
*/
integer_t *ConvertFloatToInteger(number_t num)
{
	integer_t *result;
	
	result = AllocateAndInitInteger();
	mpz_set_f(*result, num);
	
	return result;
}
Пример #5
0
// Factor using lehman method.
int _factor_lehman_method(mpz_class &rop, const mpz_class &n)
{
    if (n < 21)
        throw std::runtime_error("Require n >= 21 to use lehman method");

    int ret_val = 0;
    mpz_class u_bound;

    mpz_root(u_bound.get_mpz_t(), n.get_mpz_t(), 3);
    u_bound = u_bound + 1;

    Sieve::iterator pi(u_bound.get_ui());
    unsigned p;
    while ((p = pi.next_prime()) <= u_bound.get_ui()) {
        if (n % p == 0) {
            rop = n / p;
            ret_val = 1;
            break;
        }
    }

    if (!ret_val) {

        mpz_class k, a, b, l;
        mpf_class t;

        k = 1;

        while (k <= u_bound) {
            t = 2 * sqrt(k * n);
            mpz_set_f(a.get_mpz_t(), t.get_mpf_t());
            mpz_root(b.get_mpz_t(), n.get_mpz_t(), 6);
            mpz_root(l.get_mpz_t(), k.get_mpz_t(), 2);
            b = b / (4 * l);
            b = b + a;

            while (a <= b) {
                l = a * a - 4 * k * n;
                if (mpz_perfect_square_p(l.get_mpz_t())) {
                    mpz_sqrt(b.get_mpz_t(), l.get_mpz_t());
                    b = a + b;
                    mpz_gcd(rop.get_mpz_t(), n.get_mpz_t(), b.get_mpz_t());
                    ret_val = 1;
                    break;
                }
                a = a + 1;
            }
            if (ret_val)
                break;
            k = k + 1;
        }
    }

    return ret_val;
}
Пример #6
0
/* f_floor (rop, op) -- Set rop = floor (op). */
void
f_floor (mpf_t rop, mpf_t op)
{
  mpz_t z;

  mpz_init (z);

  /* No mpf_floor().  Convert to mpz and back. */
  mpz_set_f (z, op);
  mpf_set_z (rop, z);

  mpz_clear (z);
}
Пример #7
0
static void FBIF_floor (void) /* Round a variable down. */
{
  FACT_t push_val;
  FACT_num_t res, arg;

  arg = GET_ARG_NUM ();

  if (arg->value->fp) {
    res = FACT_alloc_num ();
    mpz_set_f (res->value->intv, arg->value->fltv);
    push_val.ap = res;
  } else
    push_val.ap = arg;
  
  push_val.type = NUM_TYPE;
  push_v (push_val);
}
Пример #8
0
void
fmpz_set_mpf(fmpz_t f, const mpf_t x)
{
#if defined(__MPIR_VERSION)
    if (mpf_fits_si_p(x))
#else
    if (flint_mpf_fits_slong_p(x))
#endif
    {
        slong cx = flint_mpf_get_si(x);
        fmpz_set_si(f, cx);
    }
    else
    {
        __mpz_struct *z = _fmpz_promote(f);
        mpz_set_f(z, x);
    }
}
Пример #9
0
static void PrintResult(HWND hText, number_t number)
{
	char buf[MAX_RESULT_BUFFER];
	unsigned long print_format;
	unsigned long print_format_float;
	
	print_format = GetIdentifierValueAsNativeInteger("print_format");
	if (print_format == 0) {
		print_format = PRINT_FORMAT_DEC;
	}
	print_format_float = GetIdentifierValueAsNativeInteger("print_format_float");
	if (print_format_float == 0) {
		print_format_float = PRINT_FORMAT_FLOAT_AUTO;
	}
	if ((print_format & PRINT_FORMAT_BIN) || (print_format & PRINT_FORMAT_HEX)) {
		integer_t *integer_result;
		char *str_result;
		
		integer_result = AllocateAndInitInteger();
		mpz_set_f(*integer_result, number);
		if (print_format & PRINT_FORMAT_BIN) {
			str_result = mpz_get_str(NULL, 2, *integer_result);
			AppendText(hText, NEWLINE"=0b");
			AppendText(hText, str_result);
			free(str_result);
		}
		if (print_format & PRINT_FORMAT_HEX) {
			str_result = mpz_get_str(NULL, 16, *integer_result);
			AppendText(hText, NEWLINE"=0x");
			AppendText(hText, str_result);
			free(str_result);
		}
		FreeInteger(integer_result);
	}
	if (print_format & PRINT_FORMAT_DEC) {
		char format_str[] = NEWLINE"=%F ";
		format_str[strlen(format_str)-1] = print_format_float == PRINT_FORMAT_FLOAT_AUTO ? 'G' :
										   print_format_float == PRINT_FORMAT_FLOAT_FIXED ? 'f' :
										   print_format_float == PRINT_FORMAT_FLOAT_SCIENTIFIC ? 'e' :
										   print_format_float == PRINT_FORMAT_FLOAT_HEX ? 'X' : 'g';
		gmp_snprintf(buf, sizeof(buf), format_str, number);
		AppendText(hText, buf);
	}
}
Пример #10
0
void my_divexact(mpz_t r, mpz_t y, mpz_t x)
{
    unsigned long prec = mpz_sizeinbase(y, 2);
    unsigned long prec2 = mpz_sizeinbase(x, 2);
    if (prec >= div_threshold) {
        mpf_set_prec_raw(d1, prec);
        mpf_set_prec_raw(d2, prec);
        mpf_set_z(d1, y);
        mpf_set_z(d2, x);
        mpf_div_2exp(d1, d1, prec);
        mpf_div_2exp(d2, d2, prec2);
        my_div(d2, d1, d2);
        mpf_mul_2exp(d2, d2, prec-prec2);
        mpf_set_d(d1, 0.5);
        mpf_add(d2, d2, d1);
        mpz_set_f(y, d2);
    } else {
        mpz_divexact(y, y, x);
        //mpz_tdiv_q(y, y, x);
    }
}
Пример #11
0
void
mpc_mod (mpc_t *rop, mpc_t op1, mpc_t op2)
{
  /* I am 90% sure that this doesn't work. However, it works for
   * integers, so I'll put off fixing it for a little while.
   */
  mpf_t hold_op1;
  mpf_t hold_op2;
  mpf_t hold_res;
  unsigned int prec;

  mpf_init (hold_op1);
  mpf_init (hold_op2);
  mpf_init (hold_res);
  mpf_set_z (hold_op1, op1.object);
  mpf_set_z (hold_op2, op2.object);

  // Get the largest precision.
  prec = (op1.precision > op2.precision) ? op1.precision : op2.precision;

  // Set the scalar values
  while (op1.precision-- > 0)
    mpf_div_ui (hold_op1, hold_op1, 10);
  while (op2.precision-- > 0)
    mpf_div_ui (hold_op2, hold_op2, 10);

  // Get the value
  mpf_div (hold_res, hold_op1, hold_op2);
  mpf_floor (hold_res, hold_res);
  mpf_mul (hold_res, hold_res, hold_op2);
  mpf_sub (hold_res, hold_op1, hold_res);

  for (rop->precision = prec; prec > 0; prec--)
    mpf_mul_ui (hold_res, hold_res, 10);
  
  mpz_set_f (rop->object, hold_res);
}
Пример #12
0
//Le but de la fonction est de calculer le développement en fractions continue jusqu'à un certain rang de racine carrée de kN et de stocker les couples (A_n-1,Q_n) comme décrit dans la section (à venir)
cfrac expand(const mpz_t N, const long long unsigned int rang, const mpz_t k)
{
	cfrac res;	//Contient l'ensemble des A_n-1 et l'ensemble Q_n
	mpz_inits(res.N, res.k, res.g, NULL);

	//Initialisation des variables
	mpz_t* A = (mpz_t*)malloc((rang+1)*sizeof(mpz_t));		//Le tableau contenant les A_n-1
	mpz_t* Q = (mpz_t*)malloc((rang+2)*sizeof(mpz_t));		//Le tableau contenant les Q_n
	mpz_t* P = (mpz_t*)malloc((rang+1)*sizeof(mpz_t));		//Éléments reliés au Q_n
	mpz_t* r = (mpz_t*)malloc((rang+1)*sizeof(mpz_t));		//Interviennent dans le calcul des A_n-1 & Q_n
	mpz_t* q = (mpz_t*)malloc(rang*sizeof(mpz_t));			//Idem
	mpz_t g, tempz;											//Idem, tempz = variable à tout faire	
	mpf_t sqrtkN, tempf, tempf2;									//sqrtkN = sqrt(k*N), tempf = variable à tout faire

	

	//Valeurs d'initialisation de la boucle
	mpz_inits(g, A[0], Q[0], r[0], tempz, NULL);
	mpf_inits(tempf, sqrtkN, tempf2, NULL);

	//Initialisation des différentes valeurs "indépendantes"

	mpz_set(tempz, N);
	mpz_mul(tempz, tempz, k);
	mpf_set_z(sqrtkN, tempz);
	mpf_sqrt(sqrtkN, sqrtkN);
	mpf_floor(tempf, sqrtkN);
	mpz_set_f(g, tempf);				//g = [sqrt(kN)]

	mpz_set(Q[0], k);
	mpz_mul(Q[0], Q[0], N);				//Q_-1 = kN = Q[0]
	mpz_set(r[0], g);					//r_-1 = r[0]
	mpz_set_ui(A[0], 1);				//A_-1 = A[0]

	
	//Calcul de P_0 & Q_0
	mpz_init_set_ui(Q[1], 1);			//Q_0 = Q[1]
	mpz_init_set_ui(P[0], 0);			//P_0 = P[0]	

	for(long long int i = 0; i < rang; i++)
	{
		switch(i)
		{
			case 0:

				//Calcul de q_0
				mpz_init_set(q[0], g);				//q_0 = [(sqrt(kN) + P_0)/Q_0] avec P_0 = 0 et Q_0 = 1

				//Calcul de A_0
				mpz_init_set(A[1],A[0]);
				mpz_mul(A[1], A[1], q[0]);			//A_0 = q_0*A_-1
				mpz_mod(A[1], A[1], N);				//On réduit mod N

				//Calcul de r_0
				mpz_init_set_ui(r[1], 0);			//r_0 = P_0 + g - q_0.Q_0 = 0 + g - g.1 = 0

				//Calcul de P_1
				mpz_init_set(P[1], g);				//P_1 = g - r_0 = g - 0 = g

				//Calcul de Q_1 = Q[2]
				mpz_init_set(Q[2], r[1]);		
				mpz_sub(Q[2], Q[2], r[0]);
				mpz_mul(Q[2], Q[2], q[0]);
				mpz_add(Q[2], Q[2], Q[0]);
				
				break;
			default:
				//Calcul q_i
				mpz_init(q[i]);	
				mpf_set_z(tempf, P[i]);
				mpf_set_z(tempf2, Q[i+1]);
				mpf_add(tempf, tempf, sqrtkN);				//sqrt(kN) + P_i
				mpf_div(tempf, tempf, tempf2);		
				mpf_floor(tempf, tempf);					//floor((sqrt(kN) + P_i)/Q_i)
				mpz_set_f(q[i], tempf);

				//Calcul de r_n = r[n+1]	
				mpz_init(r[i+1]);
				mpz_submul(r[i+1], q[i], Q[i+1]);
				mpz_add(r[i+1], r[i+1], P[i]);
				mpz_add(r[i+1], r[i+1], g);

				//Calcul de A_n = A[n+1] 		
				mpz_init_set(A[i+1],A[i]);
				mpz_mul(A[i+1], A[i+1], q[i]);		//A_i-1*q_i
				mpz_add(A[i+1], A[i+1], A[i-1]);	//A_i-1*q_i + A_i-2
				mpz_mod(A[i+1], A[i+1], N);			//réduction modulo N

				//Calcul P_n+1 
				mpz_init_set(P[i+1], g);
				mpz_sub(P[i+1], P[i+1], r[i+1]);		//P[n+1] = g - r_n = g - r[n+1]

				//Calcul Q_n+1 = Q[n+2]
				mpz_init_set(Q[i+2], r[i+1]); 
				mpz_sub(Q[i+2],	Q[i+2], r[i]);		//(r_n - r_n-1)
				mpz_mul(Q[i+2], Q[i+2], q[i]);		//q_n(r_n - r_n-1)
				mpz_add(Q[i+2], Q[i+2], Q[i]);		//Q_n-1 + q_n(r_n - r_n-1)
				
				break;
		}

	}

	//Test de routine pour voir si le développement de la fraction continue s'est bien passé
	mpz_t tempsqrt;
	mpz_init(tempsqrt);
	mpz_set(tempsqrt, k);
	mpz_mul(tempsqrt, tempsqrt, N);
	mpz_sqrt(tempsqrt, tempsqrt);
	mpz_mul_ui(tempsqrt, tempsqrt, 2);

	for(int i = 1; i < rang; i++)				//Éviter de commencer à i = 0 puisque cela représente Q_-1 qui n'intervient uniquement dans l'algo et non dans le développement en fraction continue
	{
		if(mpz_cmp(Q[i], tempsqrt) >= 0)	//Si Q_n >= 2sqrt(kN)
		{
			res.rang = 0;

			mpz_clears(g, tempz, NULL);
			mpf_clears(sqrtkN, tempf, tempf2, NULL);
			return res;
		}
	}

	//Assignation des tableaux dans le résultat
	res.A = A;
	res.Q = Q;
	res.r = r;
	res.q = q;
	res.P = P;
	mpz_set(res.N, N);
	mpz_set(res.k, k);
	res.rang = rang;
	mpz_set(res.g, g);

	//Libération de mémoire
	mpz_clears(g, tempz, NULL);
	mpf_clears(sqrtkN, tempf, tempf2, NULL);

	return res;
}
Пример #13
0
//------------------------------------------------------------------------------
// Name: knumber_integer
//------------------------------------------------------------------------------
knumber_integer::knumber_integer(const knumber_float *value) {
	mpz_init(mpz_);
	mpz_set_f(mpz_, value->mpf_);
}
int
aks (mpz_t n)
{
  mpz_t r;
  mpz_t a;
  mpz_t max_a;
  mpz_t gcd_rslt;
  mpz_t totient_r;
  mpf_t ftotient_r;
  mpf_t sqrt_rslt;
  mpf_t sqrt_rslt2;
  mpf_t temp;
  mpf_t temp2;
  sli_t logn;
/* For the sake of maple kernel */
  int argc = 0;
  char **argv;
  char err[2048];
  mpz_init (r);
  mpz_init (a);
  mpz_init (max_a);
  mpz_init (gcd_rslt);
  mpz_init (totient_r);
  mpf_init (ftotient_r);
  mpf_init (sqrt_rslt);
  mpf_init (sqrt_rslt2);
  mpf_init (temp);
  mpf_init (temp2);
/* 1. If (n = a^k for a in N and b > 1) output COMPOSITE */
  if (mpz_perfect_power_p (n) != 0)
    {
      printf ("Step 1 detected composite\n");
      return FALSE;
    }
/* 2. Find the smallest r such that or(n) > 4(log n)^2 */
  find_smallest_r (r, n);
  gmp_printf ("good r seems to be %Zd\n", r);
/* 3. If 1 < gcd(a, n) < n for some a <= r, output COMPOSITE */
/* for (a = 1; a <= r; a++) {
* gcd_rslt = gcd(a, n);
* if (gcd_rslt > 1 && gcd_rslt < n) {
* return FALSE;
* }
* }
*/
  for (mpz_set_ui (a, 1);
       mpz_cmp (a, r) < 0 || mpz_cmp (a, r) == 0; mpz_add_ui (a, a, 1))
    {
      mpz_gcd (gcd_rslt, a, n);
      if (mpz_cmp_ui (gcd_rslt, 1) > 0 && mpz_cmp (gcd_rslt, n) < 0)
	{
	  printf ("Step 3 detected composite\n");
	  return FALSE;
	}
    }
/* 4. If n <= r, output PRIME */
  if (mpz_cmp (n, r) < 0 || mpz_cmp (n, r) == 0)
    {
      printf ("Step 4 detected prime\n");
      return TRUE;
    }
/* 5. For a = 1 to floor(2*sqrt(totient(r))*(log n)
* if ( (X+a)^n != X^n + a (mod X^r-1, n) ), output COMPOSITE
*
* Choices of implementation to evaluate the polynomial equality:
* (1) Implement powermodreduce on polynomial ourselves (tough manly way)
* (2) Use MAPLE (not so manly, but less painful)
*/
/* Compute totient(r), since r is prime, this is simply r-1 */
  mpz_sub_ui (totient_r, r, 1);
/* Compute log n (ceilinged) */
  mpz_logbase2cl (&logn, n);
/* Compute sqrt(totient(r)) */
  mpf_set_z (ftotient_r, totient_r);
  mpf_sqrt (sqrt_rslt, ftotient_r);
/* Compute 2*sqrt(totient(r)) */
  mpf_mul_ui (sqrt_rslt2, sqrt_rslt, 2);
/* Compute 2*sqrt(totient(r))*(log n) */
  mpf_set (temp, sqrt_rslt2);
  mpf_set_si (temp2, logn);
  mpf_mul (temp, temp, temp2);
/* Finally, compute max_a, after lots of singing and dancing */
  mpf_floor (temp, temp);
  mpz_set_f (max_a, temp);
  gmp_printf ("max_a = %Zd\n", max_a);
/* Now evaluate the polynomial equality with the help of maple kernel */
/* Set up maple kernel incantations */
  MKernelVector kv;
  MCallBackVectorDesc cb = { textCallBack,
    0,				/* errorCallBack not used */
    0,				/* statusCallBack not used */
    0,				/* readLineCallBack not used */
    0,				/* redirectCallBack not used */
    0,				/* streamCallBack not used */
    0,				/* queryInterrupt not used */
    0				/* callBackCallBack not used */
  };
/* Initialize Maple */
  if ((kv = StartMaple (argc, argv, &cb, NULL, NULL, err)) == NULL)
    {
      printf ("Could not start Maple, %s\n", err);
      exit (666);
    }
/* Here comes the complexity and bottleneck */
/* for (a = 1; a <= max_a; a++) {
* if (!poly_eq_holds(kv, a, n, r)) {
* return FALSE;
* }
* }
*/
/* Make max_a only up to 5 */
  mpz_set_ui (max_a, 5);
  for (mpz_set_ui (a, 1);
       mpz_cmp (a, max_a) < 0 || mpz_cmp (a, max_a) == 0;
       mpz_add_ui (a, a, 1))
    {
      if (!poly_eq_holds (kv, a, n, r))
	{
	  printf ("Step 5 detected composite\n");
	  return FALSE;
	}
    }
/* 6. Output PRIME */
  printf ("Step 6 detected prime\n");
  return TRUE;
}
Пример #15
0
int scanhash_m7m_hash(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
    uint64_t max_nonce, unsigned long *hashes_done)
{
    uint32_t data[32] __attribute__((aligned(128)));
    uint32_t *data_p64 = data + (M7_MIDSTATE_LEN / sizeof(data[0]));
    uint32_t hash[8] __attribute__((aligned(32)));
    uint8_t bhash[7][64] __attribute__((aligned(32)));
    uint32_t n = pdata[19] - 1;
    const uint32_t first_nonce = pdata[19];
    char data_str[161], hash_str[65], target_str[65];
    uint8_t *bdata = 0;
    mpz_t bns[8];
    int rc = 0;
    int bytes, nnNonce2;

    mpz_t product;
    mpz_init(product);

    for(int i=0; i < 8; i++){
        mpz_init(bns[i]);
    }

    memcpy(data, pdata, 80);

    sph_sha256_context       ctx_final_sha256;

    sph_sha256_context       ctx_sha256;
    sph_sha512_context       ctx_sha512;
    sph_keccak512_context    ctx_keccak;
    sph_whirlpool_context    ctx_whirlpool;
    sph_haval256_5_context   ctx_haval;
    sph_tiger_context        ctx_tiger;
    sph_ripemd160_context    ctx_ripemd;

    sph_sha256_init(&ctx_sha256);
    sph_sha256 (&ctx_sha256, data, M7_MIDSTATE_LEN);
    
    sph_sha512_init(&ctx_sha512);
    sph_sha512 (&ctx_sha512, data, M7_MIDSTATE_LEN);
    
    sph_keccak512_init(&ctx_keccak);
    sph_keccak512 (&ctx_keccak, data, M7_MIDSTATE_LEN);

    sph_whirlpool_init(&ctx_whirlpool);
    sph_whirlpool (&ctx_whirlpool, data, M7_MIDSTATE_LEN);
    
    sph_haval256_5_init(&ctx_haval);
    sph_haval256_5 (&ctx_haval, data, M7_MIDSTATE_LEN);

    sph_tiger_init(&ctx_tiger);
    sph_tiger (&ctx_tiger, data, M7_MIDSTATE_LEN);

    sph_ripemd160_init(&ctx_ripemd);
    sph_ripemd160 (&ctx_ripemd, data, M7_MIDSTATE_LEN);

    sph_sha256_context       ctx2_sha256;
    sph_sha512_context       ctx2_sha512;
    sph_keccak512_context    ctx2_keccak;
    sph_whirlpool_context    ctx2_whirlpool;
    sph_haval256_5_context   ctx2_haval;
    sph_tiger_context        ctx2_tiger;
    sph_ripemd160_context    ctx2_ripemd;

    do {
        data[19] = ++n;
        nnNonce2 = (int)(data[19]/2);
        memset(bhash, 0, 7 * 64);

        ctx2_sha256 = ctx_sha256;
        sph_sha256 (&ctx2_sha256, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_sha256_close(&ctx2_sha256, (void*)(bhash[0]));

        ctx2_sha512 = ctx_sha512;
        sph_sha512 (&ctx2_sha512, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_sha512_close(&ctx2_sha512, (void*)(bhash[1]));
        
        ctx2_keccak = ctx_keccak;
        sph_keccak512 (&ctx2_keccak, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_keccak512_close(&ctx2_keccak, (void*)(bhash[2]));

        ctx2_whirlpool = ctx_whirlpool;
        sph_whirlpool (&ctx2_whirlpool, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_whirlpool_close(&ctx2_whirlpool, (void*)(bhash[3]));
        
        ctx2_haval = ctx_haval;
        sph_haval256_5 (&ctx2_haval, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_haval256_5_close(&ctx2_haval, (void*)(bhash[4]));

        ctx2_tiger = ctx_tiger;
        sph_tiger (&ctx2_tiger, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_tiger_close(&ctx2_tiger, (void*)(bhash[5]));

        ctx2_ripemd = ctx_ripemd;
        sph_ripemd160 (&ctx2_ripemd, data_p64, 80 - M7_MIDSTATE_LEN);
        sph_ripemd160_close(&ctx2_ripemd, (void*)(bhash[6]));

        for(int i=0; i < 7; i++){
            set_one_if_zero(bhash[i]);
            mpz_set_uint512(bns[i],bhash[i]);
        }

        mpz_set_ui(bns[7],0);

        for(int i=0; i < 7; i++){
	        mpz_add(bns[7], bns[7], bns[i]);
        }

        mpz_set_ui(product,1);

        for(int i=0; i < 8; i++){
            mpz_mul(product,product,bns[i]);
        }

        mpz_pow_ui(product, product, 2); 

        bytes = mpz_sizeinbase(product, 256);
        bdata = (uint8_t *)realloc(bdata, bytes);
        mpz_export((void *)bdata, NULL, -1, 1, 0, 0, product);

        sph_sha256_init(&ctx_final_sha256);
        sph_sha256 (&ctx_final_sha256, bdata, bytes);
        sph_sha256_close(&ctx_final_sha256, (void*)(hash));

        int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75);
        int iterations=20;
        mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16));

        mpz_t magipi;
        mpz_t magisw;
        mpf_t magifpi;
        mpf_t mpa1, mpb1, mpt1, mpp1;
        mpf_t mpa2, mpb2, mpt2, mpp2;
        mpf_t mpsft;

        mpz_init(magipi);
        mpz_init(magisw);
        mpf_init(magifpi);
        mpf_init(mpsft);
        mpf_init(mpa1);
        mpf_init(mpb1);
        mpf_init(mpt1);
        mpf_init(mpp1);

        mpf_init(mpa2);
        mpf_init(mpb2);
        mpf_init(mpt2);
        mpf_init(mpp2);

        uint32_t usw_;
        usw_ = sw_(nnNonce2, SW_DIVS);
        if (usw_ < 1) usw_ = 1;
        mpz_set_ui(magisw, usw_);
        uint32_t mpzscale=mpz_size(magisw);

        for(int i=0; i < NM7M; i++){
            if (mpzscale > 1000) {
                mpzscale = 1000;
            }
            else if (mpzscale < 1) {
                mpzscale = 1;
            }

            mpf_set_ui(mpa1, 1);
            mpf_set_ui(mpb1, 2);
            mpf_set_d(mpt1, 0.25*mpzscale);
            mpf_set_ui(mpp1, 1);
            mpf_sqrt(mpb1, mpb1);
            mpf_ui_div(mpb1, 1, mpb1);
            mpf_set_ui(mpsft, 10);

            for(int j=0; j <= iterations; j++){
	            mpf_add(mpa2, mpa1,  mpb1);
            	mpf_div_ui(mpa2, mpa2, 2);
            	mpf_mul(mpb2, mpa1, mpb1);
            	mpf_abs(mpb2, mpb2);
            	mpf_sqrt(mpb2, mpb2);
            	mpf_sub(mpt2, mpa1, mpa2);
            	mpf_abs(mpt2, mpt2);
            	mpf_sqrt(mpt2, mpt2);
            	mpf_mul(mpt2, mpt2, mpp1);
            	mpf_sub(mpt2, mpt1, mpt2);
            	mpf_mul_ui(mpp2, mpp1, 2);
            	mpf_swap(mpa1, mpa2);
            	mpf_swap(mpb1, mpb2);
            	mpf_swap(mpt1, mpt2);
            	mpf_swap(mpp1, mpp2);
            }

            mpf_add(magifpi, mpa1, mpb1);
            mpf_pow_ui(magifpi, magifpi, 2);
            mpf_div_ui(magifpi, magifpi, 4);
            mpf_abs(mpt1, mpt1);
            mpf_div(magifpi, magifpi, mpt1);

            mpf_pow_ui(mpsft, mpsft, digits/2);
            mpf_mul(magifpi, magifpi, mpsft);

            mpz_set_f(magipi, magifpi);

            mpz_add(product,product,magipi);
            mpz_add(product,product,magisw);

            mpz_set_uint256(bns[0], (void*)(hash));
            mpz_add(bns[7], bns[7], bns[0]);

            mpz_mul(product,product,bns[7]);
            mpz_cdiv_q (product, product, bns[0]);
            if (mpz_sgn(product) <= 0) mpz_set_ui(product,1);

            bytes = mpz_sizeinbase(product, 256);
            mpzscale=bytes;
            bdata = (uint8_t *)realloc(bdata, bytes);
            mpz_export(bdata, NULL, -1, 1, 0, 0, product);

            sph_sha256_init(&ctx_final_sha256);
            sph_sha256 (&ctx_final_sha256, bdata, bytes);
            sph_sha256_close(&ctx_final_sha256, (void*)(hash));

        }

        mpz_clear(magipi);
        mpz_clear(magisw);
        mpf_clear(magifpi);
        mpf_clear(mpsft);
        mpf_clear(mpa1);
        mpf_clear(mpb1);
        mpf_clear(mpt1);
        mpf_clear(mpp1);

        mpf_clear(mpa2);
        mpf_clear(mpb2);
        mpf_clear(mpt2);
        mpf_clear(mpp2);

        rc = fulltest_m7hash(hash, ptarget);
        if (rc) {
            if (opt_debug) {
                bin2hex(hash_str, (unsigned char *)hash, 32);
                bin2hex(target_str, (unsigned char *)ptarget, 32);
                bin2hex(data_str, (unsigned char *)data, 80);
                applog(LOG_DEBUG, "DEBUG: [%d thread] Found share!\ndata   %s\nhash   %s\ntarget %s", thr_id, 
                    data_str,
                    hash_str,
                    target_str);
            }

            pdata[19] = data[19];

            goto out;
        }
    } while (n < max_nonce && !work_restart[thr_id].restart);

    pdata[19] = n;

out:
    for(int i=0; i < 8; i++){
        mpz_clear(bns[i]);
    }
    mpz_clear(product);
    free(bdata);

    *hashes_done = n - first_nonce + 1;
    return rc;
}
Пример #16
0
// See Cohen; my D is -D in his notation.
size_t pbc_hilbert(mpz_t **arr, int D) {
  int a, b;
  int t;
  int B = (int)floor(sqrt((double) D / 3.0));
  mpc_t alpha;
  mpc_t j;
  mpf_t sqrtD;
  mpf_t f0;
  darray_t Pz;
  mpc_t z0, z1, z2;
  double d = 1.0;
  int h = 1;
  int jcount = 1;

  // Compute required precision.
  b = D % 2;
  for (;;) {
    t = (b*b + D) / 4;
    a = b;
    if (a <= 1) {
      a = 1;
      goto step535_4;
    }
step535_3:
    if (!(t % a)) {
      jcount++;
      if ((a == b) || (a*a == t) || !b) {
        d += 1.0 / ((double) a);
        h++;
      } else {
        d += 2.0 / ((double) a);
        h+=2;
      }
    }
step535_4:
    a++;
    if (a * a <= t) {
      goto step535_3;
    } else {
      b += 2;
      if (b > B) break;
    }
  }

  //printf("modulus: %f\n", exp(3.14159265358979 * sqrt(D)) * d * 0.5);
  d *= sqrt(D) * 3.14159265358979 / log(2);
  precision_init((int)(d + 34));
  pbc_info("class number %d, %d bit precision", h, (int) d + 34);

  darray_init(Pz);
  mpc_init(alpha);
  mpc_init(j);
  mpc_init(z0);
  mpc_init(z1);
  mpc_init(z2);
  mpf_init(sqrtD);
  mpf_init(f0);

  mpf_sqrt_ui(sqrtD, D);
  b = D % 2;
  h = 0;
  for (;;) {
    t = (b*b + D) / 4;
    if (b > 1) {
      a = b;
    } else {
      a = 1;
    }
step3:
    if (t % a) {
step4:
      a++;
      if (a * a <= t) goto step3;
    } else {
      // a, b, t/a are coeffs of an appropriate primitive reduced positive
      // definite form.
      // Compute j((-b + sqrt{-D})/(2a)).
      h++;
      pbc_info("[%d/%d] a b c = %d %d %d", h, jcount, a, b, t/a);
      mpf_set_ui(f0, 1);
      mpf_div_ui(f0, f0, 2 * a);
      mpf_mul(mpc_im(alpha), sqrtD, f0);
      mpf_mul_ui(f0, f0, b);
      mpf_neg(mpc_re(alpha), f0);

      compute_j(j, alpha);
if (0) {
  int i;
  for (i=Pz->count - 1; i>=0; i--) {
    printf("P %d = ", i);
    mpc_out_str(stdout, 10, 4, Pz->item[i]);
    printf("\n");
  }
}
      if (a == b || a * a == t || !b) {
        // P *= X - j
        int i, n;
        mpc_ptr p0;
        p0 = (mpc_ptr) pbc_malloc(sizeof(mpc_t));
        mpc_init(p0);
        mpc_neg(p0, j);
        n = Pz->count;
        if (n) {
          mpc_set(z1, Pz->item[0]);
          mpc_add(Pz->item[0], z1, p0);
          for (i=1; i<n; i++) {
            mpc_mul(z0, z1, p0);
            mpc_set(z1, Pz->item[i]);
            mpc_add(Pz->item[i], z1, z0);
          }
          mpc_mul(p0, p0, z1);
        }
        darray_append(Pz, p0);
      } else {
        // P *= X^2 - 2 Re(j) X + |j|^2
        int i, n;
        mpc_ptr p0, p1;
        p0 = (mpc_ptr) pbc_malloc(sizeof(mpc_t));
        p1 = (mpc_ptr) pbc_malloc(sizeof(mpc_t));
        mpc_init(p0);
        mpc_init(p1);
        // p1 = - 2 Re(j)
        mpf_mul_ui(f0, mpc_re(j), 2);
        mpf_neg(f0, f0);
        mpf_set(mpc_re(p1), f0);
        // p0 = |j|^2
        mpf_mul(f0, mpc_re(j), mpc_re(j));
        mpf_mul(mpc_re(p0), mpc_im(j), mpc_im(j));
        mpf_add(mpc_re(p0), mpc_re(p0), f0);
        n = Pz->count;
        if (!n) {
        } else if (n == 1) {
          mpc_set(z1, Pz->item[0]);
          mpc_add(Pz->item[0], z1, p1);
          mpc_mul(p1, z1, p1);
          mpc_add(p1, p1, p0);
          mpc_mul(p0, p0, z1);
        } else {
          mpc_set(z2, Pz->item[0]);
          mpc_set(z1, Pz->item[1]);
          mpc_add(Pz->item[0], z2, p1);
          mpc_mul(z0, z2, p1);
          mpc_add(Pz->item[1], z1, z0);
          mpc_add(Pz->item[1], Pz->item[1], p0);
          for (i=2; i<n; i++) {
            mpc_mul(z0, z1, p1);
            mpc_mul(alpha, z2, p0);
            mpc_set(z2, z1);
            mpc_set(z1, Pz->item[i]);
            mpc_add(alpha, alpha, z0);
            mpc_add(Pz->item[i], z1, alpha);
          }
          mpc_mul(z0, z2, p0);
          mpc_mul(p1, p1, z1);
          mpc_add(p1, p1, z0);
          mpc_mul(p0, p0, z1);
        }
        darray_append(Pz, p1);
        darray_append(Pz, p0);
      }
      goto step4;
    }
    b+=2;
    if (b > B) break;
  }

  // Round polynomial and assign.
  int k = 0;
  {
    *arr = pbc_malloc(sizeof(mpz_t) * (Pz->count + 1));
    int i;
    for (i=Pz->count - 1; i>=0; i--) {
      if (mpf_sgn(mpc_re(Pz->item[i])) < 0) {
        mpf_set_d(f0, -0.5);
      } else {
        mpf_set_d(f0, 0.5);
      }
      mpf_add(f0, f0, mpc_re(Pz->item[i]));
      mpz_init((*arr)[k]);
      mpz_set_f((*arr)[k], f0);
      k++;
      mpc_clear(Pz->item[i]);
      pbc_free(Pz->item[i]);
    }
    mpz_init((*arr)[k]);
    mpz_set_ui((*arr)[k], 1);
    k++;
  }
  darray_clear(Pz);
  mpc_clear(z0);
  mpc_clear(z1);
  mpc_clear(z2);
  mpf_clear(f0);
  mpf_clear(sqrtD);
  mpc_clear(alpha);
  mpc_clear(j);

  precision_clear();
  return k;
}
Пример #17
0
int main(int argc, char *argv[]) {
    mpz_t total;
    mpz_init_set_str(total, "0", 10);

    mpz_t i;
    mpz_init_set_str(i, "1", 10);

    mpz_t upper_bound;
    mpz_init_set_str(upper_bound, "9999999996", 10);
    printf("upper_bound: ");
    mpz_out_str(stdout, 10, upper_bound);
    printf("\n");

    mpz_t prev_pow;
    mpz_init_set_str(prev_pow, "1", 10);

    for (; mpz_cmp(i, upper_bound) < 0; mpz_add_ui(i, i, 1)) {
        // (i+1)^2
        mpz_t tmp;
        mpz_init(tmp);
        mpz_add_ui(tmp, i, 1);
        mpz_pow_ui(tmp, tmp, 2);

        // prev_pow + tmp
        mpz_t tmp_sum;
        mpz_init(tmp_sum);
        mpz_add(tmp_sum, prev_pow, tmp);

        // sqrt(prev_pow + tmp)
        mpf_t result;
        mpf_init(result);
        mpf_set_z(result, tmp_sum);
        mpf_sqrt(result, result);

        mpz_clear(tmp_sum);

        mpz_set(prev_pow, tmp);
        mpz_clear(tmp);

        // (int) result
        mpz_t result_int;
        mpz_init(result_int);
        mpz_set_f(result_int, result);

        // (float) (int) result
        mpf_t result_int_float;
        mpf_init(result_int_float);
        mpf_set_z(result_int_float, result_int);

        // result = (float) (int) result ?
        if (mpf_cmp(result, result_int_float) == 0) {
            mpz_out_str(stdout, 10, i);
            printf("\n");
            mpz_add(total, total, i);
        }

        mpz_clear(result_int);
        mpf_clear(result_int_float);
        mpf_clear(result);

    }

    printf("Total: ");
    mpz_out_str(stdout, 10, total);
    printf("\n");
    mpz_clear(total);
    mpz_clear(i);
    mpz_clear(upper_bound);
    mpz_clear(prev_pow);

    return 0;
}
Пример #18
0
vanilla::int_object::gmp_mpz_wrapper::gmp_mpz_wrapper(mpf_t op)
    :   _mpz(), _valid(true)
{
    mpz_init(_mpz);
    mpz_set_f(_mpz, op);
}
Пример #19
0
/** 
\brief 整系数多项式最大公因子.
\param f,g 整系数本原多项式,且\f$\deg f=n\ge\deg g\ge 1\f$.
\param r 最大公因子.
\note 小素数模方法.
\todo 理论文档此处有误.
*/
void UniGcdZ_SmallPrime1(poly_z & r,const poly_z & f,const poly_z & g)
{
	poly_z fp,gp,vp,v1;
	poly_z fstar,gstar;
	mpz_t c,s,t;
	mpz_init(c);mpz_init(s);mpz_init(t);
	mpz_t A,b,B,z_temp,k,p_bound,p,p_low_bound,p1,B2;
	mpf_t float_temp;
	mpz_init(A);mpz_init(b);mpz_init(B);mpz_init(z_temp);mpz_init(k);mpz_init(p_bound);mpz_init(p);mpz_init(p1);mpz_init(p_low_bound);mpf_init(float_temp);mpz_init(B2);
	UniMaxNormZ(A,f);UniMaxNormZ(b,g);
	int n=f.size()-1,m=g.size()-1;
	if(mpz_cmp(A,b)<0)mpz_set(A,b);
	mpz_gcd(b,f[n],g[m]);
	mpz_ui_pow_ui(B,2,n);
	mpz_mul(B,B,A);
	mpz_mul(B,B,b);
	mpf_sqrt_ui(float_temp,n+1);
	mpz_set_f(z_temp,float_temp);
	mpz_mul(B,B,z_temp);
	mpz_mul_ui(B2,B,2);
	mpz_set_si(z_temp,n);
	int tempint;
	tempint=2*n*mpz_sizeinbase(z_temp,2)+2*mpz_sizeinbase(b,2)+4*n*mpz_sizeinbase(A,2);
	//tempint=2*n*Modules::NumberTheory::IntegerLength(Z(n),2)+2*Modules::NumberTheory::IntegerLength(b,2)+4*n*Modules::NumberTheory::IntegerLength(A,2);
	mpz_set_si(k,tempint);
	mpz_mul_ui(p_bound,k,2);
	mpz_mul_ui(p_bound,p_bound,mpz_sizeinbase(k,2));
	mpz_set_ui(p_low_bound,3);
	fstar.resize(n+1);
	for(size_t i=0;i<=n;i++)
	{
		mpz_mul(fstar[i],f[i],b);
	}
	gstar.resize(m+1);
	for(size_t i=0;i<=m;i++)
	{
		mpz_mul(gstar[i],g[i],b);
	}
	while(1)
	{
		while(1)
		{
			random::randominteger(p,p_low_bound,p_bound);
			if(mpz_probab_prime_p(p,10)>0&&mpz_divisible_p(b,p)==0)break;
		}
		UniPolynomialMod(fp,f,p);
		UniPolynomialMod(gp,g,p);
		UniGcdZp(vp,fp,gp,p);
		if(vp.size()==1)
		{
			r.resize(1);
			mpz_set_si(r[0],1);
			fp.resize(0);gp.resize(0);vp.resize(0);v1.resize(0);
			fstar.resize(0);gstar.resize(0);
			mpz_clear(c);mpz_clear(s);mpz_clear(t);
			mpz_clear(A);mpz_clear(b);mpz_clear(B);mpz_clear(z_temp);mpz_clear(k);mpz_clear(p_bound);mpz_clear(p);mpz_clear(p1);mpz_clear(p_low_bound);mpf_clear(float_temp);
			return ;
		}
		mpz_set(p1,p);
		copy_poly_z(v1,vp);
		while(mpz_cmp(p1,B2)<0)
		{
			while(1)
			{
				random::randominteger(p,p_low_bound,p_bound);
				if(mpz_probab_prime_p(p,10)>0&&mpz_divisible_p(b,p)==0)break;
			}
			UniPolynomialMod(fp,f,p);
			UniPolynomialMod(gp,g,p);
			UniGcdZp(vp,fp,gp,p);
			if(vp.size()==1)
			{
				r.resize(1);
				mpz_set_si(r[0],1);
				fp.resize(0);gp.resize(0);vp.resize(0);v1.resize(0);
				fstar.resize(0);gstar.resize(0);
				mpz_clear(c);mpz_clear(s);mpz_clear(t);
				mpz_clear(A);mpz_clear(b);mpz_clear(B);mpz_clear(z_temp);mpz_clear(k);mpz_clear(p_bound);mpz_clear(p);mpz_clear(p1);mpz_clear(p_low_bound);mpf_clear(float_temp);
				return ;
			}
			if(vp.size()<v1.size())
			{
				mpz_set(p1,p);
				copy_poly_z(v1,vp);
				continue;
			}
			if(vp.size()==v1.size())
			{
				mpz_gcdext(c,s,t,p1,p);
				mpz_mul(t,p,t);
				mpz_mul(s,p1,s);
				mpz_mul(p1,p1,p);
				for(size_t i=0;i<vp.size();i++)
				{
					mpz_mul(c,v1[i],t);
					mpz_addmul(c,vp[i],s);
					mpz_set(v1[i],c);
				}
				UniPolynomialMod(v1,v1,p1);
			}
		}
		for(size_t i=0;i<v1.size();i++)mpz_mul(v1[i],v1[i],b);
		UniPolynomialMod(v1,v1,p1);
		if(poly_z_divisible(fstar,v1)&&poly_z_divisible(gstar,v1))
		{
			UniPPZ(r,v1);break;
		}
	}
	fp.resize(0);gp.resize(0);vp.resize(0);v1.resize(0);
	fstar.resize(0);gstar.resize(0);
	mpz_clear(c);mpz_clear(s);mpz_clear(t);
	mpz_clear(A);mpz_clear(b);mpz_clear(B);mpz_clear(z_temp);mpz_clear(k);mpz_clear(p_bound);mpz_clear(p);mpz_clear(p1);mpz_clear(p_low_bound);mpf_clear(float_temp);
	return ;
}
Пример #20
0
void
spectral_test (mpf_t rop[], unsigned int T, mpz_t a, mpz_t m)
{
  /* Knuth "Seminumerical Algorithms, Third Edition", section 3.3.4
     (pp. 101-103). */

  /* v[t] = min { sqrt (x[1]^2 + ... + x[t]^2) |
     x[1] + a*x[2] + ... + pow (a, t-1) * x[t] is congruent to 0 (mod m) } */


  /* Variables. */
  unsigned int ui_t;
  unsigned int ui_i, ui_j, ui_k, ui_l;
  mpf_t f_tmp1, f_tmp2;
  mpz_t tmp1, tmp2, tmp3;
  mpz_t U[GMP_SPECT_MAXT][GMP_SPECT_MAXT],
    V[GMP_SPECT_MAXT][GMP_SPECT_MAXT],
    X[GMP_SPECT_MAXT],
    Y[GMP_SPECT_MAXT],
    Z[GMP_SPECT_MAXT];
  mpz_t h, hp, r, s, p, pp, q, u, v;

  /* GMP inits. */
  mpf_init (f_tmp1);
  mpf_init (f_tmp2);
  for (ui_i = 0; ui_i < GMP_SPECT_MAXT; ui_i++)
    {
      for (ui_j = 0; ui_j < GMP_SPECT_MAXT; ui_j++)
	{
	  mpz_init_set_ui (U[ui_i][ui_j], 0);
	  mpz_init_set_ui (V[ui_i][ui_j], 0);
	}
      mpz_init_set_ui (X[ui_i], 0);
      mpz_init_set_ui (Y[ui_i], 0);
      mpz_init (Z[ui_i]);
    }
  mpz_init (tmp1);
  mpz_init (tmp2);
  mpz_init (tmp3);
  mpz_init (h);
  mpz_init (hp);
  mpz_init (r);
  mpz_init (s);
  mpz_init (p);
  mpz_init (pp);
  mpz_init (q);
  mpz_init (u);
  mpz_init (v);

  /* Implementation inits. */
  if (T > GMP_SPECT_MAXT)
    T = GMP_SPECT_MAXT;			/* FIXME: Lazy. */

  /* S1 [Initialize.] */
  ui_t = 2 - 1;			/* NOTE: `t' in description == ui_t + 1
				   for easy indexing */
  mpz_set (h, a);
  mpz_set (hp, m);
  mpz_set_ui (p, 1);
  mpz_set_ui (pp, 0);
  mpz_set (r, a);
  mpz_pow_ui (s, a, 2);
  mpz_add_ui (s, s, 1);		/* s = 1 + a^2 */

  /* S2 [Euclidean step.] */
  while (1)
    {
      if (g_debug > DEBUG_1)
	{
	  mpz_mul (tmp1, h, pp);
	  mpz_mul (tmp2, hp, p);
	  mpz_sub (tmp1, tmp1, tmp2);
	  if (mpz_cmpabs (m, tmp1))
	    {
	      printf ("***BUG***: h*pp - hp*p = ");
	      mpz_out_str (stdout, 10, tmp1);
	      printf ("\n");
	    }
	}
      if (g_debug > DEBUG_2)
	{
	  printf ("hp = ");
	  mpz_out_str (stdout, 10, hp);
	  printf ("\nh = ");
	  mpz_out_str (stdout, 10, h);
	  printf ("\n");
	  fflush (stdout);
	}

      if (mpz_sgn (h))
	mpz_tdiv_q (q, hp, h);	/* q = floor(hp/h) */
      else
	mpz_set_ui (q, 1);

      if (g_debug > DEBUG_2)
	{
	  printf ("q = ");
	  mpz_out_str (stdout, 10, q);
	  printf ("\n");
	  fflush (stdout);
	}

      mpz_mul (tmp1, q, h);
      mpz_sub (u, hp, tmp1);	/* u = hp - q*h */

      mpz_mul (tmp1, q, p);
      mpz_sub (v, pp, tmp1);	/* v = pp - q*p */

      mpz_pow_ui (tmp1, u, 2);
      mpz_pow_ui (tmp2, v, 2);
      mpz_add (tmp1, tmp1, tmp2);
      if (mpz_cmp (tmp1, s) < 0)
	{
	  mpz_set (s, tmp1);	/* s = u^2 + v^2 */
	  mpz_set (hp, h);	/* hp = h */
	  mpz_set (h, u);	/* h = u */
	  mpz_set (pp, p);	/* pp = p */
	  mpz_set (p, v);	/* p = v */
	}
      else
	break;
    }

  /* S3 [Compute v2.] */
  mpz_sub (u, u, h);
  mpz_sub (v, v, p);

  mpz_pow_ui (tmp1, u, 2);
  mpz_pow_ui (tmp2, v, 2);
  mpz_add (tmp1, tmp1, tmp2);
  if (mpz_cmp (tmp1, s) < 0)
    {
      mpz_set (s, tmp1);	/* s = u^2 + v^2 */
      mpz_set (hp, u);
      mpz_set (pp, v);
    }
  mpf_set_z (f_tmp1, s);
  mpf_sqrt (rop[ui_t - 1], f_tmp1);

  /* S4 [Advance t.] */
  mpz_neg (U[0][0], h);
  mpz_set (U[0][1], p);
  mpz_neg (U[1][0], hp);
  mpz_set (U[1][1], pp);

  mpz_set (V[0][0], pp);
  mpz_set (V[0][1], hp);
  mpz_neg (V[1][0], p);
  mpz_neg (V[1][1], h);
  if (mpz_cmp_ui (pp, 0) > 0)
    {
      mpz_neg (V[0][0], V[0][0]);
      mpz_neg (V[0][1], V[0][1]);
      mpz_neg (V[1][0], V[1][0]);
      mpz_neg (V[1][1], V[1][1]);
    }

  while (ui_t + 1 != T)		/* S4 loop */
    {
      ui_t++;
      mpz_mul (r, a, r);
      mpz_mod (r, r, m);

      /* Add new row and column to U and V.  They are initialized with
	 all elements set to zero, so clearing is not necessary. */

      mpz_neg (U[ui_t][0], r); /* U: First col in new row. */
      mpz_set_ui (U[ui_t][ui_t], 1); /* U: Last col in new row. */

      mpz_set (V[ui_t][ui_t], m); /* V: Last col in new row. */

      /* "Finally, for 1 <= i < t,
	   set q = round (vi1 * r / m),
	   vit = vi1*r - q*m,
	   and Ut=Ut+q*Ui */

      for (ui_i = 0; ui_i < ui_t; ui_i++)
	{
	  mpz_mul (tmp1, V[ui_i][0], r); /* tmp1=vi1*r */
	  zdiv_round (q, tmp1, m); /* q=round(vi1*r/m) */
	  mpz_mul (tmp2, q, m);	/* tmp2=q*m */
	  mpz_sub (V[ui_i][ui_t], tmp1, tmp2);

	  for (ui_j = 0; ui_j <= ui_t; ui_j++) /* U[t] = U[t] + q*U[i] */
	    {
	      mpz_mul (tmp1, q, U[ui_i][ui_j]);	/* tmp=q*uij */
	      mpz_add (U[ui_t][ui_j], U[ui_t][ui_j], tmp1); /* utj = utj + q*uij */
	    }
	}

      /* s = min (s, zdot (U[t], U[t]) */
      vz_dot (tmp1, U[ui_t], U[ui_t], ui_t + 1);
      if (mpz_cmp (tmp1, s) < 0)
	mpz_set (s, tmp1);

      ui_k = ui_t;
      ui_j = 0;			/* WARNING: ui_j no longer a temp. */

      /* S5 [Transform.] */
      if (g_debug > DEBUG_2)
	printf ("(t, k, j, q1, q2, ...)\n");
      do
	{
	  if (g_debug > DEBUG_2)
	    printf ("(%u, %u, %u", ui_t + 1, ui_k + 1, ui_j + 1);

	  for (ui_i = 0; ui_i <= ui_t; ui_i++)
	    {
	      if (ui_i != ui_j)
		{
		  vz_dot (tmp1, V[ui_i], V[ui_j], ui_t + 1); /* tmp1=dot(Vi,Vj). */
		  mpz_abs (tmp2, tmp1);
		  mpz_mul_ui (tmp2, tmp2, 2); /* tmp2 = 2*abs(dot(Vi,Vj) */
		  vz_dot (tmp3, V[ui_j], V[ui_j], ui_t + 1); /* tmp3=dot(Vj,Vj). */

		  if (mpz_cmp (tmp2, tmp3) > 0)
		    {
		      zdiv_round (q, tmp1, tmp3); /* q=round(Vi.Vj/Vj.Vj) */
		      if (g_debug > DEBUG_2)
			{
			  printf (", ");
			  mpz_out_str (stdout, 10, q);
			}

		      for (ui_l = 0; ui_l <= ui_t; ui_l++)
			{
			  mpz_mul (tmp1, q, V[ui_j][ui_l]);
			  mpz_sub (V[ui_i][ui_l], V[ui_i][ui_l], tmp1); /* Vi=Vi-q*Vj */
			  mpz_mul (tmp1, q, U[ui_i][ui_l]);
			  mpz_add (U[ui_j][ui_l], U[ui_j][ui_l], tmp1); /* Uj=Uj+q*Ui */
			}

		      vz_dot (tmp1, U[ui_j], U[ui_j], ui_t + 1); /* tmp1=dot(Uj,Uj) */
		      if (mpz_cmp (tmp1, s) < 0) /* s = min(s,dot(Uj,Uj)) */
			mpz_set (s, tmp1);
		      ui_k = ui_j;
		    }
		  else if (g_debug > DEBUG_2)
		    printf (", #"); /* 2|Vi.Vj| <= Vj.Vj */
		}
	      else if (g_debug > DEBUG_2)
		printf (", *");	/* i == j */
	    }

	  if (g_debug > DEBUG_2)
	    printf (")\n");

	  /* S6 [Advance j.] */
	  if (ui_j == ui_t)
	    ui_j = 0;
	  else
	    ui_j++;
	}
      while (ui_j != ui_k);	/* S5 */

      /* From Knuth p. 104: "The exhaustive search in steps S8-S10
	 reduces the value of s only rarely." */
#ifdef DO_SEARCH
      /* S7 [Prepare for search.] */
      /* Find minimum in (x[1], ..., x[t]) satisfying condition
	 x[k]^2 <= f(y[1], ...,y[t]) * dot(V[k],V[k]) */

      ui_k = ui_t;
      if (g_debug > DEBUG_2)
	{
	  printf ("searching...");
	  /*for (f = 0; f < ui_t*/
	  fflush (stdout);
	}

      /* Z[i] = floor (sqrt (floor (dot(V[i],V[i]) * s / m^2))); */
      mpz_pow_ui (tmp1, m, 2);
      mpf_set_z (f_tmp1, tmp1);
      mpf_set_z (f_tmp2, s);
      mpf_div (f_tmp1, f_tmp2, f_tmp1);	/* f_tmp1 = s/m^2 */
      for (ui_i = 0; ui_i <= ui_t; ui_i++)
	{
	  vz_dot (tmp1, V[ui_i], V[ui_i], ui_t + 1);
	  mpf_set_z (f_tmp2, tmp1);
	  mpf_mul (f_tmp2, f_tmp2, f_tmp1);
	  f_floor (f_tmp2, f_tmp2);
	  mpf_sqrt (f_tmp2, f_tmp2);
	  mpz_set_f (Z[ui_i], f_tmp2);
	}

      /* S8 [Advance X[k].] */
      do
	{
	  if (g_debug > DEBUG_2)
	    {
	      printf ("X[%u] = ", ui_k);
	      mpz_out_str (stdout, 10, X[ui_k]);
	      printf ("\tZ[%u] = ", ui_k);
	      mpz_out_str (stdout, 10, Z[ui_k]);
	      printf ("\n");
	      fflush (stdout);
	    }

	  if (mpz_cmp (X[ui_k], Z[ui_k]))
	    {
	      mpz_add_ui (X[ui_k], X[ui_k], 1);
	      for (ui_i = 0; ui_i <= ui_t; ui_i++)
		mpz_add (Y[ui_i], Y[ui_i], U[ui_k][ui_i]);

	      /* S9 [Advance k.] */
	      while (++ui_k <= ui_t)
		{
		  mpz_neg (X[ui_k], Z[ui_k]);
		  mpz_mul_ui (tmp1, Z[ui_k], 2);
		  for (ui_i = 0; ui_i <= ui_t; ui_i++)
		    {
		      mpz_mul (tmp2, tmp1, U[ui_k][ui_i]);
		      mpz_sub (Y[ui_i], Y[ui_i], tmp2);
		    }
		}
	      vz_dot (tmp1, Y, Y, ui_t + 1);
	      if (mpz_cmp (tmp1, s) < 0)
		mpz_set (s, tmp1);
	    }
	}
      while (--ui_k);
#endif /* DO_SEARCH */
      mpf_set_z (f_tmp1, s);
      mpf_sqrt (rop[ui_t - 1], f_tmp1);
#ifdef DO_SEARCH
      if (g_debug > DEBUG_2)
	printf ("done.\n");
#endif /* DO_SEARCH */
    } /* S4 loop */

  /* Clear GMP variables. */

  mpf_clear (f_tmp1);
  mpf_clear (f_tmp2);
  for (ui_i = 0; ui_i < GMP_SPECT_MAXT; ui_i++)
    {
      for (ui_j = 0; ui_j < GMP_SPECT_MAXT; ui_j++)
	{
	  mpz_clear (U[ui_i][ui_j]);
	  mpz_clear (V[ui_i][ui_j]);
	}
      mpz_clear (X[ui_i]);
      mpz_clear (Y[ui_i]);
      mpz_clear (Z[ui_i]);
    }
  mpz_clear (tmp1);
  mpz_clear (tmp2);
  mpz_clear (tmp3);
  mpz_clear (h);
  mpz_clear (hp);
  mpz_clear (r);
  mpz_clear (s);
  mpz_clear (p);
  mpz_clear (pp);
  mpz_clear (q);
  mpz_clear (u);
  mpz_clear (v);

  return;
}
Пример #21
0
void
mpc_div (mpc_t *rop, mpc_t op1, mpc_t op2) 
{
  /* This function needs to be fixed. At the current moment I am tired
   * of having to mess around with the precision values, so I'm just
   * going to convert the values to 'mpf's and call it quits for the
   * day. I'll fix it later.
   * Division by zero is not checked for in this function.
   */
  mpf_t hold_op1;
  mpf_t hold_op2;
  mpf_t hold_res;
  unsigned int prec;

  mpf_init (hold_op1);
  mpf_init (hold_op2);
  mpf_init (hold_res);
  mpf_set_z (hold_op1, op1.object);
  mpf_set_z (hold_op2, op2.object);

  // Get the largest precision
  prec = (op1.precision > op2.precision) ? op1.precision : op2.precision;
  if (prec == 0)
    prec = default_prec;

  // Set the scalar values
  while (op1.precision-- > 0)
    mpf_div_ui (hold_op1, hold_op1, 10);
  while (op2.precision-- > 0)
    mpf_div_ui (hold_op2, hold_op2, 10);

  // Get the value
  mpf_div (hold_res, hold_op1, hold_op2);

  for (rop->precision = prec; prec > 0; prec--)
    mpf_mul_ui (hold_res, hold_res, 10);
  
  mpz_set_f (rop->object, hold_res);
  /*
  temp.precision = (op1.precision < op2.precision) ? op2.precision : op1.precision;

  if (!op1.precision && !op2.precision)
    {
      temp.precision = default_prec;
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, default_prec);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }
  else if (op1.precision < op2.precision)
    {
      temp.precision = op2.precision;
      mpz_set (temp.object, op1.object);
      power_of_ten (temp.object, op2.precision - op1.precision);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }
  else if (op1.precision > op2.precision)
    {
      temp.precision = op1.precision;
      mpz_set (temp.object, op2.object);
      power_of_ten (temp.object, op1.precision - op2.precision);
      mpz_tdiv_q (temp_res, op1.object, temp.object);
    }
  else
    {
      temp.precision = op1.precision;
      mpz_set (temp.object, op1.object);
      mpz_tdiv_q (temp_res, temp.object, op2.object);
    }

  rop->precision = temp.precision;
  mpz_set (rop->object, temp_res);
  */
}
Пример #22
0
void
check_one (mpz_srcptr z)
{
  static const int shift[] = {
    0, 1, BITS_PER_MP_LIMB, 2*BITS_PER_MP_LIMB, 5*BITS_PER_MP_LIMB
  };

  int    sh, shneg, neg;
  mpf_t  f;
  mpz_t  got, want;

  mpf_init2 (f, mpz_sizeinbase(z,2));
  mpz_init (got);
  mpz_init (want);

  for (sh = 0; sh < numberof(shift); sh++)
    {
      for (shneg = 0; shneg <= 1; shneg++)
        {
          for (neg = 0; neg <= 1; neg++)
            {
              mpf_set_z (f, z);
              mpz_set (want, z);
            
              if (neg)
                {
                  mpf_neg (f, f);
                  mpz_neg (want, want);
                }

              if (shneg)
                {
                  mpz_tdiv_q_2exp (want, want, shift[sh]);
                  mpf_div_2exp (f, f, shift[sh]);
                }
              else
                {
                  mpz_mul_2exp (want, want, shift[sh]);
                  mpf_mul_2exp (f, f, shift[sh]);
                }

              mpz_set_f (got, f);
              MPZ_CHECK_FORMAT (got);

              if (mpz_cmp (got, want) != 0)
                {
                  printf ("wrong result\n");
                  printf ("  shift  %d\n", shneg ? -shift[sh] : shift[sh]);
                  printf ("  neg    %d\n", neg);
                  mpf_trace ("     f", f);
                  mpz_trace ("   got", got);
                  mpz_trace ("  want", want);
                  abort ();
                }
            }
        }
    }

  mpf_clear (f);
  mpz_clear (got);
  mpz_clear (want);
}
Пример #23
0
//#define SW_MAX 1000
void m7magi_hash(const char* input, char* output)
{
    unsigned int nnNonce;
    uint32_t pdata[32];
    memcpy(pdata, input, 80);
//    memcpy(&nnNonce, input+76, 4);

    int i, j, bytes, nnNonce2;
    nnNonce2 = (int)(pdata[19]/2);
    size_t sz = 80;
    uint8_t bhash[7][64];
    uint32_t hash[8];
    memset(bhash, 0, 7 * 64);

    sph_sha256_context       ctx_final_sha256;

    sph_sha256_context       ctx_sha256;
    sph_sha512_context       ctx_sha512;
    sph_keccak512_context    ctx_keccak;
    sph_whirlpool_context    ctx_whirlpool;
    sph_haval256_5_context   ctx_haval;
    sph_tiger_context        ctx_tiger;
    sph_ripemd160_context    ctx_ripemd;

    sph_sha256_init(&ctx_sha256);
    // ZSHA256;
    sph_sha256 (&ctx_sha256, input, sz);
    sph_sha256_close(&ctx_sha256, (void*)(bhash[0]));

    sph_sha512_init(&ctx_sha512);
    // ZSHA512;
    sph_sha512 (&ctx_sha512, input, sz);
    sph_sha512_close(&ctx_sha512, (void*)(bhash[1]));
    
    sph_keccak512_init(&ctx_keccak);
    // ZKECCAK;
    sph_keccak512 (&ctx_keccak, input, sz);
    sph_keccak512_close(&ctx_keccak, (void*)(bhash[2]));

    sph_whirlpool_init(&ctx_whirlpool);
    // ZWHIRLPOOL;
    sph_whirlpool (&ctx_whirlpool, input, sz);
    sph_whirlpool_close(&ctx_whirlpool, (void*)(bhash[3]));
    
    sph_haval256_5_init(&ctx_haval);
    // ZHAVAL;
    sph_haval256_5 (&ctx_haval, input, sz);
    sph_haval256_5_close(&ctx_haval, (void*)(bhash[4]));

    sph_tiger_init(&ctx_tiger);
    // ZTIGER;
    sph_tiger (&ctx_tiger, input, sz);
    sph_tiger_close(&ctx_tiger, (void*)(bhash[5]));

    sph_ripemd160_init(&ctx_ripemd);
    // ZRIPEMD;
    sph_ripemd160 (&ctx_ripemd, input, sz);
    sph_ripemd160_close(&ctx_ripemd, (void*)(bhash[6]));

//    printf("%s\n", hash[6].GetHex().c_str());

    mpz_t bns[8];
    for(i=0; i < 8; i++){
        mpz_init(bns[i]);
    }
    //Take care of zeros and load gmp
    for(i=0; i < 7; i++){
	set_one_if_zero(bhash[i]);
	mpz_set_uint512(bns[i],bhash[i]);
    }

    mpz_set_ui(bns[7],0);
    for(i=0; i < 7; i++)
	mpz_add(bns[7], bns[7], bns[i]);

    mpz_t product;
    mpz_init(product);
    mpz_set_ui(product,1);
//    mpz_pow_ui(bns[7], bns[7], 2);
    for(i=0; i < 8; i++){
	mpz_mul(product,product,bns[i]);
    }
    mpz_pow_ui(product, product, 2);

    bytes = mpz_sizeinbase(product, 256);
//    printf("M7M data space: %iB\n", bytes);
    char *data = (char*)malloc(bytes);
    mpz_export(data, NULL, -1, 1, 0, 0, product);

    sph_sha256_init(&ctx_final_sha256);
    // ZSHA256;
    sph_sha256 (&ctx_final_sha256, data, bytes);
    sph_sha256_close(&ctx_final_sha256, (void*)(hash));
    free(data);

    int digits=(int)((sqrt((double)(nnNonce2))*(1.+EPS))/9000+75);
//    int iterations=(int)((sqrt((double)(nnNonce2))+EPS)/500+350); // <= 500
//    int digits=100;
    int iterations=20; // <= 500
    mpf_set_default_prec((long int)(digits*BITS_PER_DIGIT+16));

    mpz_t magipi;
    mpz_t magisw;
    mpf_t magifpi;
    mpf_t mpa1, mpb1, mpt1, mpp1;
    mpf_t mpa2, mpb2, mpt2, mpp2;
    mpf_t mpsft;

    mpz_init(magipi);
    mpz_init(magisw);
    mpf_init(magifpi);
    mpf_init(mpsft);
    mpf_init(mpa1);
    mpf_init(mpb1);
    mpf_init(mpt1);
    mpf_init(mpp1);

    mpf_init(mpa2);
    mpf_init(mpb2);
    mpf_init(mpt2);
    mpf_init(mpp2);

    uint32_t usw_;
    usw_ = sw_(nnNonce2, SW_DIVS);
    if (usw_ < 1) usw_ = 1;
//    if(fDebugMagi) printf("usw_: %d\n", usw_);
    mpz_set_ui(magisw, usw_);
    uint32_t mpzscale=mpz_size(magisw);
for(i=0; i < NM7M; i++)
{
    if (mpzscale > 1000) {
      mpzscale = 1000;
    }
    else if (mpzscale < 1) {
      mpzscale = 1;
    }
//    if(fDebugMagi) printf("mpzscale: %d\n", mpzscale);

    mpf_set_ui(mpa1, 1);
    mpf_set_ui(mpb1, 2);
    mpf_set_d(mpt1, 0.25*mpzscale);
    mpf_set_ui(mpp1, 1);
    mpf_sqrt(mpb1, mpb1);
    mpf_ui_div(mpb1, 1, mpb1);
    mpf_set_ui(mpsft, 10);

    for(j=0; j <= iterations; j++)
    {
	mpf_add(mpa2, mpa1,  mpb1);
	mpf_div_ui(mpa2, mpa2, 2);
	mpf_mul(mpb2, mpa1, mpb1);
	mpf_abs(mpb2, mpb2);
	mpf_sqrt(mpb2, mpb2);
	mpf_sub(mpt2, mpa1, mpa2);
	mpf_abs(mpt2, mpt2);
	mpf_sqrt(mpt2, mpt2);
	mpf_mul(mpt2, mpt2, mpp1);
	mpf_sub(mpt2, mpt1, mpt2);
	mpf_mul_ui(mpp2, mpp1, 2);
	mpf_swap(mpa1, mpa2);
	mpf_swap(mpb1, mpb2);
	mpf_swap(mpt1, mpt2);
	mpf_swap(mpp1, mpp2);
    }
    mpf_add(magifpi, mpa1, mpb1);
    mpf_pow_ui(magifpi, magifpi, 2);
    mpf_div_ui(magifpi, magifpi, 4);
    mpf_abs(mpt1, mpt1);
    mpf_div(magifpi, magifpi, mpt1);

//    mpf_out_str(stdout, 10, digits+2, magifpi);

    mpf_pow_ui(mpsft, mpsft, digits/2);
    mpf_mul(magifpi, magifpi, mpsft);

    mpz_set_f(magipi, magifpi);

//mpz_set_ui(magipi,1);

    mpz_add(product,product,magipi);
    mpz_add(product,product,magisw);
    
    mpz_set_uint256(bns[0], (void*)(hash));
    mpz_add(bns[7], bns[7], bns[0]);

    mpz_mul(product,product,bns[7]);
    mpz_cdiv_q (product, product, bns[0]);
    if (mpz_sgn(product) <= 0) mpz_set_ui(product,1);

    bytes = mpz_sizeinbase(product, 256);
    mpzscale=bytes;
//    printf("M7M data space: %iB\n", bytes);
    char *bdata = (char*)malloc(bytes);
    mpz_export(bdata, NULL, -1, 1, 0, 0, product);

    sph_sha256_init(&ctx_final_sha256);
    // ZSHA256;
    sph_sha256 (&ctx_final_sha256, bdata, bytes);
    sph_sha256_close(&ctx_final_sha256, (void*)(hash));
    free(bdata);
}
    //Free the memory
    for(i=0; i < 8; i++){
	mpz_clear(bns[i]);
    }
//    mpz_clear(dSpectralWeight);
    mpz_clear(product);

    mpz_clear(magipi);
    mpz_clear(magisw);
    mpf_clear(magifpi);
    mpf_clear(mpsft);
    mpf_clear(mpa1);
    mpf_clear(mpb1);
    mpf_clear(mpt1);
    mpf_clear(mpp1);

    mpf_clear(mpa2);
    mpf_clear(mpb2);
    mpf_clear(mpt2);
    mpf_clear(mpp2);
    
    memcpy(output, hash, 32);
}