本文整理汇总了C++中mpz_class类的典型用法代码示例。如果您正苦于以下问题:C++ mpz_class类的具体用法?C++ mpz_class怎么用?C++ mpz_class使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了mpz_class类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: _Format_b
bool _Format_b(Signal &sig, Formatter *pFormatter,
const Formatter::Flags &flags, const mpz_class &num)
{
char *str = nullptr;
::gmp_asprintf(&str, Formatter::ComposeFlags(flags, "Zb").c_str(), num.get_mpz_t());
bool rtn = pFormatter->PutString(sig, str);
::free(str);
return rtn;
}
示例2: crypto_dh_keypair
int crypto_dh_keypair(
unsigned char *pk,
unsigned char *sk
)
{
randombytes(sk, SECRETKEY_BYTES);
mpz_class p;
p = MODULUS;
mpz_class a;
static mpz_class base = 2;
mpz_class result;
mpz_import(a.get_mpz_t(),SECRETKEY_BYTES,-1,1,0,0,sk);
mpz_powm(result.get_mpz_t(),base.get_mpz_t(),a.get_mpz_t(),p.get_mpz_t());
if (mpz_sizeinbase(result.get_mpz_t(),256) > PUBLICKEY_BYTES) return -1;
long long i;
for (i = 0;i < PUBLICKEY_BYTES;++i) pk[i] = 0;
mpz_export(pk,0,-1,1,0,0,result.get_mpz_t());
return 0;
}
示例3: FermatProbablePrimalityTestFast
// Check Fermat probable primality test (2-PRP): 2 ** (n-1) = 1 (mod n)
// true: n is probable prime
// false: n is composite; set fractional length in the nLength output
static bool FermatProbablePrimalityTestFast(const mpz_class& n, unsigned int& nLength, CPrimalityTestParams& testParams, bool fFastDiv = false)
{
// Faster GMP version
mpz_t& mpzN = testParams.mpzN;
mpz_t& mpzE = testParams.mpzE;
mpz_t& mpzR = testParams.mpzR;
const unsigned int nPrimorialSeq = testParams.nPrimorialSeq;
mpz_set(mpzN, n.get_mpz_t());
if (fFastDiv)
{
// Fast divisibility tests
// Starting from the first prime not included in the round primorial
const unsigned int nBeginSeq = nPrimorialSeq + 1;
const unsigned int nEndSeq = nBeginSeq + nFastDivPrimes;
for (unsigned int nPrimeSeq = nBeginSeq; nPrimeSeq < nEndSeq; nPrimeSeq++) {
if (mpz_divisible_ui_p(mpzN, vPrimes[nPrimeSeq])) {
return false;
}
}
}
++fermats;
mpz_sub_ui(mpzE, mpzN, 1);
mpz_powm(mpzR, mpzTwo.get_mpz_t(), mpzE, mpzN);
if (mpz_cmp_ui(mpzR, 1) == 0)
{
return true;
}
// Failed Fermat test, calculate fractional length
mpz_sub(mpzE, mpzN, mpzR);
mpz_mul_2exp(mpzR, mpzE, nFractionalBits);
mpz_tdiv_q(mpzE, mpzR, mpzN);
unsigned int nFractionalLength = mpz_get_ui(mpzE);
if (nFractionalLength >= (1 << nFractionalBits))
{
cout << "FermatProbablePrimalityTest() : fractional assert" << endl;
return false;
}
nLength = (nLength & TARGET_LENGTH_MASK) | nFractionalLength;
return false;
}
示例4: pollard_brent
/* Pollard Brent factorization method. Pretty much the same as Pollard's rho
* though uses brent cycle detection. */
mpz_class primes::pollard_brent(mpz_class &n) {
mpz_class y; // random
random(y, n);
unsigned int m = mpz_sizeinbase(n.get_mpz_t(), 2);
unsigned int brentLimit = m_numBrents;
if (m > 99) {
brentLimit = brentLimit >> 3;
} else if (m > 98) {
示例5: eval
mpz_class UnivariatePolynomial::eval(const mpz_class &x) const {
//TODO: Use Horner's Scheme
mpz_class ans = 0;
for (const auto &p : dict_) {
mpz_class temp;
mpz_pow_ui(temp.get_mpz_t(), x.get_mpz_t(), p.first);
ans += p.second * temp;
}
return ans;
}
示例6: sumOfDigits
mpz_class sumOfDigits(const mpz_class number, const unsigned radix) {
assert(radix <= 10);
assert(number >= 0);
auto digits = number.get_str(static_cast<int>(radix));
mpz_class sum(0);
for (const auto i : digits)
sum += i - '0';
return sum;
}
示例7: digit_sum
int digit_sum(const mpz_class& bignum) {
/**Find sum of digits in a bignum
* Return the sum of the digits in the number
*/
int sum = 0;
string bigstr(bignum.get_str());
for (int i = 0; i < (int)bigstr.size(); ++i)
// implicit conversion from char to int
sum += bigstr[i] - '0';
return sum;
}
示例8: FermatProbablePrimalityTestFast
bool FermatProbablePrimalityTestFast(const mpz_class &n,
unsigned int& nLength,
CPrimalityTestParams &testParams,
bool fFastFail)
{
static const mpz_class mpzTwo = 2;
mpz_t& mpzE = testParams.mpzE;
mpz_t& mpzR = testParams.mpzR;
mpz_sub_ui(mpzE, n.get_mpz_t(), 1);
mpz_powm(mpzR, mpzTwo.get_mpz_t(), mpzE, n.get_mpz_t());
if (mpz_cmp_ui(mpzR, 1) == 0)
return true;
if (fFastFail)
return false;
// Fermat test failed, calculate chain length (integer and fractional part)
mpz_sub(mpzE, n.get_mpz_t(), mpzR);
mpz_mul_2exp(mpzR, mpzE, DifficultyFractionalBits);
mpz_tdiv_q(mpzE, mpzR, n.get_mpz_t());
unsigned int fractionalLength = mpz_get_ui(mpzE);
nLength = (nLength & DifficultyChainLengthMask) | fractionalLength;
return false;
}
示例9: generate_random
void generate_random(mpz_class &z, const mpz_class &n)
{
if (n == 0)
{
z = 0;
return;
}
size_t nbchar = mpz_sizeinbase(n.get_mpz_t(), 2) / (sizeof(unsigned char) * CHAR_BIT) + 1;
unsigned char *rnd = new unsigned char[nbchar];
mpz_class mask(2);
mask <<= mpz_sizeinbase(n.get_mpz_t(), 2);
mask -= 1;
do
{
_Pragma(STRINGIFY(omp critical))
{
fastrandombytes(rnd, nbchar);
}
mpz_import (z.get_mpz_t(), nbchar, 1, sizeof(unsigned char), 0, 0, rnd);
z &= mask;
}
while (z >= n);
delete[] rnd;
}
示例10: is_mersenne_prime
static bool is_mersenne_prime(mpz_class p)
{
if( 2 == p )
return true;
else
{
mpz_class s(4);
mpz_class div( (mpz_class(1) << p.get_ui()) - 1 );
for( mpz_class i(3); i <= p; ++i )
{
s = (s * s - mpz_class(2)) % div ;
}
return ( s == mpz_class(0) );
}
}
示例11: mpzToString
/**
* Helper function to convert an mpz_class object into
* its internal QString representation. Mainly used for
* debugging.
*/
static QString mpzToString(const mpz_class & val)
{
char *p = 0;
// use the gmp provided conversion routine
gmp_asprintf(&p, "%Zd", val.get_mpz_t());
// convert it into a QString
QString result(QString::fromLatin1(p));
// and free up the resources allocated by gmp_asprintf
__gmp_freefunc_t freefunc;
mp_get_memory_functions(NULL, NULL, &freefunc);
(*freefunc)(p, std::strlen(p) + 1);
// done
return result;
}
示例12: c
/* Pollard P-1 fatorization method. Used as a second (fast as limited to primes
* < 200000) method incase brent fails to gain a factor. */
mpz_class primes::pollard_p1(mpz_class &n) {
mpz_class c(2);
for (size_t i = 0; i < m_primes.size(); ++i) {
mpz_class pp = m_primes[i];
while (pp < 200000) {
mpz_powm(c.get_mpz_t(), c.get_mpz_t(), m_primes[i].get_mpz_t(),
n.get_mpz_t());
pp *= m_primes[i];
}
}
mpz_class g;
c--;
gcd(g, c, n);
if (g > 1 && g <= n) {
return g;
}
return 1;
}
示例13: FermatProbablePrimalityTest
// Check Fermat probable primality test (2-PRP): 2 ** (n-1) = 1 (mod n)
// true: n is probable prime
// false: n is composite; set fractional length in the nLength output
static bool FermatProbablePrimalityTest(const mpz_class& n, unsigned int& nLength) {
// Faster GMP version
mpz_t mpzN;mpz_t mpzE;mpz_t mpzR;
mpz_init_set(mpzN, n.get_mpz_t());
mpz_init(mpzE);
mpz_sub_ui(mpzE, mpzN, 1);
mpz_init(mpzR);
mpz_powm(mpzR, mpzTwo.get_mpz_t(), mpzE, mpzN);
if (mpz_cmp_ui(mpzR, 1) == 0) { mpz_clear(mpzN);mpz_clear(mpzE);mpz_clear(mpzR);return true; }
// Failed Fermat test, calculate fractional length
mpz_sub(mpzE, mpzN, mpzR);
mpz_mul_2exp(mpzR, mpzE, nFractionalBits);
mpz_tdiv_q(mpzE, mpzR, mpzN);
unsigned int nFractionalLength = mpz_get_ui(mpzE);
mpz_clear(mpzN);mpz_clear(mpzE);mpz_clear(mpzR);
if (nFractionalLength >= (1 << nFractionalBits)) { return error("FermatProbablePrimalityTest() : fractional assert"); }
nLength = (nLength & TARGET_LENGTH_MASK) | nFractionalLength;
return false;
}
示例14: naf
std::vector<int> Ellipticcurve::getNAF(mpz_class k) {
//implementation folows pg. 98
std::vector<int> naf(mpz_sizeinbase(k.get_mpz_t(), 2) + 1);
int i = 0;
while (k >= 1){
if (k % 2 == 1){
mpz_class temp = (k % 4);
naf[i] = 2 - (int)temp.get_ui();
k -= naf[i];
}
else{
naf[i] = 0;
}
k /= 2;
i++;
}
return naf;
}
示例15: trialDivisionChainTest
bool trialDivisionChainTest(const PrimeSource &primeSource,
mpz_class &N,
bool fSophieGermain,
unsigned chainLength,
unsigned depth)
{
N += (fSophieGermain ? -1 : 1);
for (unsigned i = 0; i < chainLength; i++) {
for (unsigned divIdx = 0; divIdx < depth; divIdx++) {
if (mpz_tdiv_ui(N.get_mpz_t(), primeSource[divIdx]) == 0) {
fprintf(stderr, " * divisor: [%u]%u\n", divIdx, primeSource[divIdx]);
return false;
}
}
N <<= 1;
N += (fSophieGermain ? 1 : -1);
}
return true;
}