void check_limb (void) { int i; mp_limb_t limb; mpz_t z; char *s; check_one ("0", "%Md", CNST_LIMB(0)); check_one ("1", "%Md", CNST_LIMB(1)); /* "i" many 1 bits, tested against mpz_get_str in decimal and hex */ limb = 1; mpz_init_set_ui (z, 1L); for (i = 1; i <= GMP_LIMB_BITS; i++) { s = mpz_get_str (NULL, 10, z); check_one (s, "%Mu", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, 16, z); check_one (s, "%Mx", limb); (*__gmp_free_func) (s, strlen (s) + 1); s = mpz_get_str (NULL, -16, z); check_one (s, "%MX", limb); (*__gmp_free_func) (s, strlen (s) + 1); limb = 2*limb + 1; mpz_mul_2exp (z, z, 1L); mpz_add_ui (z, z, 1L); } mpz_clear (z); }
char *dd_MLGetStrForNumber(mytype x) { /* This is to make a string of rational expression for GMP rational number x. It does nothing (return NULL) if x is double. */ char *sd=NULL,*sn=NULL,*st=NULL; #if defined GMPRATIONAL mpz_t zn,zd; int len; mpz_init(zn); mpz_init(zd); mpq_canonicalize(x); mpq_get_num(zn,x); mpq_get_den(zd,x); if (mpz_sgn(zn)==0){ st=(char *)malloc(2); strcpy(st," 0"); } else if (mpz_cmp_ui(zd,1U)==0){ st=mpz_get_str(st,10,zn); } else { sn=mpz_get_str(sn,10,zn);sd=mpz_get_str(sd,10,zd); len=strlen(sn)+strlen(sd)+2; st=(char *)malloc(len); strcpy(st,sn); strcat(st,"/");strcat(st,sd); } mpz_clear(zn); mpz_clear(zd); if (sd!=NULL) free(sd); if (sn!=NULL) free(sn); #else /* do nothing */ #endif /* printf("String for Number =%s\n",st); */ return st; }
void sharecalc() { char buf[1024]; if(np<n){ fprintf(stderr,"Could not read enough shares, unable to compute.\n"); exit(1); } /*calculate*/ /*printf("Key-Matrix:\n");printm(kmatrix); printf("Hash-Matrix:\n");printm(hmatrix);*/ solve(kmatrix); solve(hmatrix); /*output result*/ str2bin(key,mpz_get_str(buf,16,kmatrix[0][n]),16); str2bin(hashv,mpz_get_str(buf,16,hmatrix[0][n]),16); /*printf("Using key: %s\n",key_prints(0,key)); printf("Expected Hash: %s\n",key_prints(0,hashv));*/ /*printf("Key-Matrix:\n");printm(kmatrix); printf("Hash-Matrix:\n");printm(hmatrix);*/ }
/* =============== SV_RSAGenMsg Generate an encrypted RSA message =============== */ int SV_RSAGenMsg( const char *pubkey, char *cleartext, char *encrypted ) { struct rsa_public_key public_key; mpz_t message; unsigned char buffer[ RSA_KEY_LENGTH / 8 - 11 ]; int retval; Com_RandomBytes( buffer, RSA_KEY_LENGTH / 8 - 11 ); nettle_mpz_init_set_str_256_u( message, RSA_KEY_LENGTH / 8 - 11, buffer ); mpz_get_str( cleartext, 16, message ); rsa_public_key_init( &public_key ); mpz_set_ui( public_key.e, RSA_PUBLIC_EXPONENT ); retval = mpz_set_str( public_key.n, pubkey, 16 ) + 1; if ( retval ) { rsa_public_key_prepare( &public_key ); retval = rsa_encrypt( &public_key, NULL, qnettle_random, RSA_KEY_LENGTH / 8 - 11, buffer, message ); } rsa_public_key_clear( &public_key ); mpz_get_str( encrypted, 16, message ); mpz_clear( message ); return retval; }
/*** * get Public and Private keys and save them in .txt */ void getPublicPrivatekey(mpz_t p, mpz_t q,mpz_t n ,mpz_t phi , mpz_t e, mpz_t d) { mpz_init(p); mpz_init(q); mpz_init(n); mpz_init(phi); mpz_init(d); int seed=rand(); getNumberPremier(p,seed+1); getNumberPremier(q,seed+2); int e_int=65537; mpz_set_ui(e,e_int); mpz_mul(n,p,q); //n=p*q mpz_sub_ui(p, p, 1); //p=p-1 mpz_sub_ui(q, q, 1); //q=q-1 mpz_mul(phi, p, q); //phi=(p-1)*(q-1) mpz_invert(d, e, phi); //calcul d char *str_d=malloc(KEY_LENGTH/4*sizeof(char)); char *str_n=malloc(KEY_LENGTH/2*sizeof(char)); mpz_get_str (str_n, 16, n); mpz_get_str (str_d, 16, d); WriteF(str_n,"publicKey_Modulo"); WriteF(str_d,"privateKey_d"); free(str_n); free(str_d); printf("\nClé ==>==>==>==>==> Gagné\n"); }
// sign a hash using ECDSA, hash must be a string in base used during initialisation. // private key and a generator point are required for it to work (look SetDomain // and SetKeys) void ECDSA::Sign( char *hash, char *&r, char *&s ) { mpz_t z,k,rr,ss,t,u; ECPoint kG; mpz_init_set_str(z, hash, m_base); mpz_inits(k, rr, ss, t, u, NULL); do { mpz_urandomm(k, m_st, ecc->m_n); ecc->MultiplePoint(k, ecc->m_G, kG); mpz_set(rr, kG.x); mpz_mod(rr, rr, ecc->m_n); } while (!mpz_cmp_ui(rr, 0)); do { mpz_set(t,z); mpz_addmul(t,rr,ecc->m_dA); mpz_set(u,k); mpz_invert(u,u,ecc->m_n); mpz_mul(ss, t, u); mpz_mod(ss, ss, ecc->m_n); } while (!mpz_cmp_ui(ss, 0)); mpz_get_str(r, m_base, rr); mpz_get_str(s, m_base, ss); mpz_clears(z,k,rr,ss,t,u,NULL); return; }
/****************************************************************************** PYTHON BINDINGS ******************************************************************************/ static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) { char * msg, * d, * k, * p, * a, * b, * q, * gx, * gy; if (!PyArg_ParseTuple(args, "sssssssss", &msg, &d, &k, &p, &a, &b, &q, &gx, &gy)) { return NULL; } mpz_t privKey, nonce; CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10); Sig sig; mpz_init_set_str(privKey, d, 10); mpz_init_set_str(nonce, k, 10); signZZ_p(&sig, msg, privKey, nonce, curve); destroyCurveZZ_p(curve); char * resultR = mpz_get_str(NULL, 10, sig.r); char * resultS = mpz_get_str(NULL, 10, sig.s); mpz_clears(sig.r, sig.s, privKey, NULL); PyObject * ret = Py_BuildValue("ss", resultR, resultS); free(resultR); free(resultS); return ret; }
/* MakeSpanCaseSpecification : return an ident in the format suitable for Case specifications from a SpanListList */ Ptrchar MakeSpanCaseSpecification (PtrSpanListList list, Bits width) { int CaseSpecLength = CASE_SPEC_STR_LEN; Ptrchar buildStr = NEW_ARRAY (char, CaseSpecLength); *buildStr = '\0'; /* FOR_EACH (list, true, ...) */ while (list && true) { PtrSpanList subList = CAR (list); /* FOR_EACH (subList, true, ...) */ while (subList && true) { mpz_get_str (EndOfString (buildStr), 10, FilterRangeFromMP_INT (CAR (subList).lowerBound, width, 0)); if (mpz_cmp (CAR (subList).lowerBound, CAR (subList).upperBound) != 0) { strcat (buildStr, ".."); mpz_get_str (EndOfString (buildStr), 10, FilterRangeFromMP_INT (CAR (subList).upperBound, width, 0)); } if (CDR (subList)) strcat (buildStr, ","); if (strlen (buildStr) > CaseSpecLength / 2) { CaseSpecLength *= 2; buildStr = EXTEND_ARRAY (buildStr, char, CaseSpecLength); } subList = CDR (subList); } if (CDR (list)) strcat (buildStr, ";"); list = CDR (list); }
obj rational_to_string_obj( obj a, unsigned radix ) { mpq_t v; size_t sz; obj str, str2; int len; OBJ_TO_MPQ(v, a); sz = mpz_sizeinbase(mpq_numref(v), radix) + 2; if(mpz_sgn(mpq_numref(v))<0) sz++; sz += mpz_sizeinbase(mpq_denref(v), radix); str = bvec_alloc(sz, string_class); if(!mpz_get_str(PTR_TO_DATAPTR(str), radix, mpq_numref(v))) return FALSE_OBJ; len = strlen(PTR_TO_DATAPTR(str)); ((char *)PTR_TO_DATAPTR(str))[len++]='/'; if(!mpz_get_str(PTR_TO_DATAPTR(str) + len, radix, mpq_denref(v))) return FALSE_OBJ; if(strlen(PTR_TO_DATAPTR(str)) == sz - 1) return str; str2 = bvec_alloc(sz - 1, string_class); strcpy(PTR_TO_DATAPTR(str2), PTR_TO_DATAPTR(str)); return str2; }
static char * rw_serialize_priv (const dckey *key) { const rw_priv *sk = k2priv (key); size_t buflen = (strlen (rabin_1.name) + sizeof (rwak_ppref) + sizeof (rwak_qpref) + 3 + mpz_sizeinbase (sk->p, 16) + mpz_sizeinbase (sk->q, 16)); char *buf = malloc (buflen); char *p = buf; if (!buf) return NULL; strcpy (p, rabin_1.name); strcat (p, rwak_ppref); strcat (p, "0x"); p += strlen (p); mpz_get_str (p, 16, sk->p); strcat (p, rwak_qpref); strcat (p, "0x"); p += strlen (p); mpz_get_str (p, 16, sk->q); p += strlen (p) + 1; assert (p == buf + buflen); return buf; }
void ecc_doubling(ecc_jcb_t *ROP, ecc_jcb_t *P, ecc_curve *curve) { mpz_t alfa, alfa1, alfa2, beta, x3_2, y3_2, y2, z3_2; if((strcmp(mpz_get_str(NULL, 10, P->x), "0") == 0) && (strcmp(mpz_get_str(NULL, 10, P->y), "0") == 0) && (strcmp(mpz_get_str(NULL, 10, P->z), "0") == 0)) { ecc_cp_jcb(ROP, P); } else { mpz_inits(alfa, alfa1, alfa2, beta, x3_2, y3_2, z3_2, y2, NULL); // alfa = 3*(x1)**2 + a * (z1**4) fp_mul_mpz(alfa1, P->x, P->x, curve->p); fp_mul_mpz(alfa2, P->z, P->z, curve->p); fp_mul_mpz(alfa2, alfa2, alfa2, curve->p); fp_mul_si(alfa1, alfa1, 3, curve->p); fp_mul_mpz(alfa2, alfa2, curve->a, curve->p); fp_add_mpz(alfa, alfa1, alfa2, curve->p); // y2 = y1**2 fp_mul_mpz(y2, P->y, P->y, curve->p); // beta = 4*x1*(y1**2) fp_mul_mpz(beta, y2, P->x, curve->p); fp_mul_si(beta, beta, 4, curve->p); mpz_set(ROP->z, P->z); // z3 = y1*z1 fp_mul_mpz(ROP->z, P->z, P->y, curve->p); // z3 = 2*y1*z1 fp_mul_si(ROP->z, ROP->z, 2, curve->p); mpz_init_set(x3_2, beta); // x3_2 = 2*beta fp_mul_si(x3_2, x3_2, 2, curve->p); mpz_set(ROP->x, alfa); // x3 = alfa**2 - 2*beta fp_mul_mpz(ROP->x, ROP->x, ROP->x, curve->p); fp_sub_mpz(ROP->x, ROP->x, x3_2, curve->p); mpz_init_set(y3_2, P->y); mpz_set(ROP->y, beta); // y3 = alfa(beta -x3) fp_sub_mpz(ROP->y, ROP->y, ROP->x, curve->p); fp_mul_mpz(ROP->y, ROP->y, alfa, curve->p); // y3_2 = 8*y1**4 fp_mul_mpz(y3_2, y2, y2, curve->p); fp_mul_si(y3_2, y3_2, 8, curve->p); // y3 = alfa(beta -x3) - 8*y1**4 fp_sub_mpz(ROP->y, ROP->y, y3_2, curve->p); mpz_clears(alfa, alfa1, alfa2, beta, x3_2, y3_2, z3_2, y2, NULL); } }
void share(char*fn) { int i; mpz_t x,k,t,e; byte rnd[16]; char buf[1024]; FILE *f; f=fopen(fn,"w+"); if(f==NULL){ fprintf(stderr,"Unable to open file %s.\n",fn); return; } /*generate random x*/ mpz_init(x); for(i=0;i<16;i++)rnd[i]=randbyte(); setbin(x,rnd,16); /*calculate k*/ mpz_init(k); mpz_init(t); mpz_init(e); for(i=0;i<n;i++){ /*k=k + kvec[i]*x^i*/ mpz_powm(t,x,e,p); mpz_mul(t,t,kvec[i]); mpz_add(k,k,t); mpz_mod(k,k,p); /*incr e so that it matches the next i*/ mpz_add_ui(e,e,1); } fprintf(f,"%s\n",mpz_get_str(buf,16,p)); fprintf(f,"%s\n",mpz_get_str(buf,16,x)); fprintf(f,"%s\n",mpz_get_str(buf,16,k)); mpz_set_ui(e,0); mpz_set_ui(k,0); for(i=0;i<n;i++){ /*k=k + hvec[i]*x^i*/ mpz_powm(t,x,e,p); mpz_mul(t,t,hvec[i]); mpz_add(k,k,t); mpz_mod(k,k,p); /*incr e so that it matches the next i*/ mpz_add_ui(e,e,1); } fprintf(f,"%s\n",mpz_get_str(buf,16,k)); fclose(f); mpz_clear(x); mpz_clear(k); mpz_clear(t); mpz_clear(e); }
/*Output error message when two numbers does not match*/ void test_unmatch(FILE *stream, const char *where, mpz_srcptr a, mpz_srcptr b) { char buf[MAX_INFO_SIZE+1]; char stra[MAX_INFO_SIZE+1],strb[MAX_INFO_SIZE+1]; mpz_get_str(stra,10,a); mpz_get_str(strb,10,b); strcpy(buf,"Expected is "); strncat(buf,stra,MAX_INFO_SIZE-strlen(buf)); strcat(buf,", but got "); strncat(buf,strb,MAX_INFO_SIZE-strlen(buf)); test_error(stream,where,buf); }
void display(std::ostream & out, __mpz_struct const * v) { size_t sz = mpz_sizeinbase(v, 10) + 2; if (sz < 1024) { char buffer[1024]; mpz_get_str(buffer, 10, v); out << buffer; } else { std::unique_ptr<char> buffer(new char[sz]); mpz_get_str(buffer.get(), 10, v); out << buffer.get(); } }
int main() { big a,b,c; mpz_init_set_str(a,"123456789123456789123",10); mpz_init_set_str(b,"98123981239457",10); mpz_init(c); mpz_mul(c,a,b); printf("%s * %s = %s\n",mpz_get_str(NULL,10,a),mpz_get_str(NULL,10,b),mpz_get_str(NULL,10,c)); mpz_clear(a); mpz_clear(b); mpz_clear(c); return 0; }
int main() { dh_key key_a, key_b; mpz_t s; key_a = new_NIST_dh_key(); key_b = new_NIST_dh_key(); dh_session_key(key_a, &s, key_b.public); printf("(B**a)%%p: %s\n\n", mpz_get_str(NULL, 16, s)); dh_session_key(key_b, &s, key_a.public); printf("(A**b)%%p: %s\n", mpz_get_str(NULL, 16, s)); }
int main(int argc, char* argv[]) { int count = 0; mpz_t n; mpz_t d; mpz_t n1; mpz_t d1; mpz_init_set_ui(n, 1); mpz_init_set_ui(d, 1); mpz_init_set_ui(n1, 3); mpz_init_set_ui(d1, 2); for(int i = 2; i <= 1000; i++) { mpz_t n2; mpz_t d2; mpz_init(n2); mpz_init(d2); mpz_mul_ui(n2, n1, 2); mpz_add(n2, n2, n); mpz_mul_ui(d2, d1, 2); mpz_add(d2, d2, d); char n_digits[1024] = { '\0' }; char d_digits[1024] = { '\0' }; mpz_get_str(n_digits, 10, n2); mpz_get_str(d_digits, 10, d2); int nlen = strlen(n_digits); int dlen = strlen(d_digits); if(nlen > dlen) { count++; } mpz_set(n, n1); mpz_set(n1, n2); mpz_set(d, d1); mpz_set(d1, d2); } printf("%i", count); return 0; }
void QtGMP::fac() { QString s(""); mpz_t aa,bb; char *str=0; mpz_init(bb); mpz_init_set_str(aa,qPrintable(this->lineEdit2->text()),10); mpz_fac_ui(bb,mpz_get_ui(aa)); // gmp_printf("B!:\t%Zd\nA!:\t%Zd\n\n\n",aa,bb); s.sprintf("B:\t%s\nB!:\t%s\n",mpz_get_str(str,10,aa),mpz_get_str(str,10,bb)); this->textEdit->setText(s); mpz_clears(aa,bb,'\0'); }
void QtGMP::nprime() { QString s(""); mpz_t aa,bb; char *str=0; mpz_init(bb); mpz_init_set_str(aa,qPrintable(this->lineEdit1->text()),10); mpz_nextprime(bb,aa); //gmp_printf("A:\t%Zd\nNextPrime(A):\t%Zd\n\n\n",aa,bb); s.sprintf("A:\t%s\nNextPrime(A):\t%s\n",mpz_get_str(str,10,aa),mpz_get_str(str,10,bb)); this->textEdit->setText(s); mpz_clears(aa,bb,'\0'); }
void QtGMP::powAB() { QString s(""); mpz_t aa,bb,cc; char *str=0; mpz_init(cc); mpz_init_set_str(aa,qPrintable(this->lineEdit1->text()),10); mpz_init_set_str(bb,qPrintable(this->lineEdit2->text()),10); mpz_pow_ui(cc,aa,mpz_get_ui(bb)); // gmp_printf("A:\t%Zd\n\B:\t%ZdnA^B:\t%Zd\n\n\n",aa,bb,cc); s.sprintf("A:\t%s\nB:\t%s\nA^B:\t%s\n",mpz_get_str(str,10,aa),mpz_get_str(str,10,bb),mpz_get_str(str,10,cc)); this->textEdit->setText(s); mpz_clears(aa,bb,cc,'\0'); }
int rsa_createkey(PRSAKEY pkey){ /* inisialisasi variabel helper */ unsigned long int k_int = 65537; PPRIVATEATTRIB prsaattrib = create_private_attrib(); if(prsaattrib==0x00) return -1; /* pick two random prime number (p and q ) */ mpz_urandomb(prsaattrib->p,prsaattrib->hrandstate,PRIMESIZE); mpz_nextprime(prsaattrib->p,prsaattrib->p); //gmp_randseed_ui(prsaattrib->hrandstate,GetTickCount()); mpz_urandomb(prsaattrib->q,prsaattrib->hrandstate,PRIMESIZE); mpz_nextprime(prsaattrib->q,prsaattrib->q); /* calculate n = (p * q) */ mpz_mul(prsaattrib->n,prsaattrib->q,prsaattrib->p); /* calculate z = (p-1) * ( q - 1) */ mpz_sub_ui(prsaattrib->pminus,prsaattrib->p,(unsigned int)1); mpz_sub_ui(prsaattrib->qminus,prsaattrib->q,(unsigned int)1); mpz_mul(prsaattrib->z,prsaattrib->pminus,prsaattrib->qminus); /* choose k, such that k is co-prime to z, i.e z is not divisible by k or in other word gcd(k,z) = 1 */ while(1){ mpz_gcd_ui(prsaattrib->gcd,prsaattrib->z,k_int); if(mpz_cmp_ui(prsaattrib->gcd,(unsigned long)1) == 0) break; k_int +=1; } mpz_set_ui(prsaattrib->k,k_int); /* calculate j for ( k * j ) mod z = 1 */ if(mpz_invert(prsaattrib->j,prsaattrib->k,prsaattrib->z) == 0){ /* cannot find j (multiplicative inverse) */ destroy_private_attrib(prsaattrib); return -1; } /* then we have public key = [n,k] */ mpz_get_str(pkey->public_key.strkey_k,16,prsaattrib->k); mpz_get_str(pkey->public_key.strkey_n,16,prsaattrib->n); /* and private key [n,j] */ mpz_get_str(pkey->private_key.strkey_j,16,prsaattrib->j); mpz_get_str(pkey->private_key.strkey_n,16,prsaattrib->n); /* clean up everything */ destroy_private_attrib(prsaattrib); return 0; }
void QtGMP::lcm() { //QApplication::aboutQt(); QString s(""); mpz_t aa,bb,cc; char *str=0; mpz_init(cc); mpz_init_set_str(aa,qPrintable(this->lineEdit1->text()),10); mpz_init_set_str(bb,qPrintable(this->lineEdit2->text()),10); mpz_lcm(cc,aa,bb); // gmp_printf("A:\t%Zd\nB:\t%Zd\nLCM(A,B):\t%Zd\n\n\n",aa,bb,cc); s.sprintf("A:\t%s\nB:\t%s\nLCM(A,B):\t%s\n",mpz_get_str(str,10,aa),mpz_get_str(str,10,bb),mpz_get_str(str,10,cc)); this->textEdit->setText(s); mpz_clears(aa,bb,cc,'\0'); }
char * mpc_get_str (mpc_t op) { char *return_value; mpz_t temp_op; mpz_t temp1; mpz_t temp2; if (op.precision == 0) return_value = mpz_get_str (NULL, 10, op.object); else { mpz_init (temp_op); mpz_abs (temp_op, op.object); mpz_init_set (temp1, temp_op); mpz_init_set_ui (temp2, 1); power_of_ten (temp2, op.precision); mpz_tdiv_q (temp1, temp1, temp2); return_value = concatinate_free (mpz_get_str (NULL, 10, temp1), ".", true, false); mpz_mul (temp1, temp1, temp2); mpz_sub (temp1, temp_op, temp1); mpz_abs (temp1, temp1); mpz_tdiv_q_ui (temp2, temp2, 10); while (mpz_cmp (temp2, temp1) > 0) { return_value = concatinate_free (return_value, "0", true, false); mpz_tdiv_q_ui (temp2, temp2, 10); } return_value = concatinate_free (return_value, mpz_get_str (NULL, 10, temp1), true, true); if (mpz_sgn (op.object) < 0) return_value = concatinate_free ("-", return_value, false, true); mpz_clear (temp1); mpz_clear (temp2); mpz_clear (temp_op); } return return_value; }
static int gmp2bn(mpz_t g, BIGNUM *bn) { if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) && (BN_BITS2 == GMP_NUMB_BITS)) { /* The common case */ int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size; BN_zero(bn); if(bn_expand2 (bn, s) == NULL) return 0; bn->top = s; TINYCLR_SSL_MEMCPY(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0])); bn_correct_top(bn); bn->neg = g->_mp_size >= 0 ? 0 : 1; return 1; } else { int toret; char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10); if(!tmpchar) return 0; mpz_get_str(tmpchar, 16, g); toret = BN_hex2bn(&bn, tmpchar); OPENSSL_free(tmpchar); return toret; } }
int main(void) { unsigned int maxsum = 0; mpz_t num; mpz_init(num); char numstr[200] = ""; for (unsigned int base = 51; base < 100; ++base) { if (base % 10 == 0) { continue; } for (unsigned int exp = 70; exp <= 100; ++exp) { mpz_ui_pow_ui(num, base, exp); mpz_get_str(numstr, 10, num); unsigned int sum = 0; for (char* digit = numstr; *digit; ++digit) { sum += *digit - '0'; } if (sum > maxsum) { maxsum = sum; } } } printf("%u\n", maxsum); return 0; }
int main(int argc, char **argv) { mpz_t counter; mpz_t triangleValue; unsigned int factors; unsigned int previousFactors = 0; mpz_init_set_ui(counter, 1); mpz_init(triangleValue); mpz_init_set_ui(previousTriangleCounter, 0); while (1) { nextTriangle(triangleValue); factors = factorCount(triangleValue); if (previousFactors < factors) { previousFactors = factors; printf("Number %s has triangle %s and %d factors\n", mpz_get_str(NULL, 10, counter), mpz_get_str(NULL, 10, triangleValue), factors); } if (factors >= STOP_COUNT) break; mpz_add_ui(counter, counter, 1); } return 0; }
static void dump (const char *label, const mpz_t x) { char *buf = mpz_get_str (NULL, 16, x); fprintf (stderr, "%s: %s\n", label, buf); testfree (buf); }
int main() { unsigned long long a,b; mpz_t a2, b3, v, sum; mpz_init(a2); mpz_init(b3); mpz_init(v); mpz_init_set_ui(sum, 0); for (b = 1; b < sqrt(MAX); b++) { mpz_ui_pow_ui(b3, b, 3); for (a = 1; a < b ; a++) { mpz_ui_pow_ui(a2, a, 2); mpz_add(v, a2, b3); if (!mpz_divisible_ui_p(v, a)) { continue; } mpz_div_ui(v, v, a); if (mpz_perfect_square_p(v)) { mpz_add(sum, sum, v); printf("%s %llu %llu\n", mpz_get_str(NULL, 10, v), a, b); } } } return(0); }
/* write one */ static int write_radix(void *a, char *b, int radix) { LTC_ARGCHK(a != NULL); LTC_ARGCHK(b != NULL); mpz_get_str(b, radix, a); return CRYPT_OK; }
// stolen from boost.multiprecision std::string str(std::ios_base::fmtflags f = std::ios_base::fmtflags(0)) const { int base = 10; if((f & std::ios_base::oct) == std::ios_base::oct) base = 8; else if((f & std::ios_base::hex) == std::ios_base::hex) base = 16; // // sanity check, bases 8 and 16 are only available for positive numbers: // if((base != 10) && (mpz_sgn(m_data) < 0)) BOOST_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers")); void *(*alloc_func_ptr) (size_t); void *(*realloc_func_ptr) (void *, size_t, size_t); void (*free_func_ptr) (void *, size_t); const char* ps = mpz_get_str (0, base, m_data); std::string s = ps; mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr); (*free_func_ptr)((void*)ps, std::strlen(ps) + 1); if((base != 10) && (f & std::ios_base::showbase)) { int pos = s[0] == '-' ? 1 : 0; const char* pp = base == 8 ? "0" : "0x"; s.insert(pos, pp); } if((f & std::ios_base::showpos) && (s[0] != '-')) s.insert(0, 1, '+'); return s; }