static unsigned long* make_rand_polynom(unsigned long n) { unsigned long* const a = malloc((n + 1) * sizeof(unsigned long)); size_t i; for (i = 0; i <= n; ++i) a[i] = modp(rand()); return a; }
void caesar_cipher_dec(char *intext, int intext_size, char *outtext, int *shift, int shift_size) { int i; char ch; for (i = 0; i < intext_size; i++) { ch = *(intext + i); *(outtext + i) = ALPHA_CH(ch) ? NUMCHAR( modn( CHARNUM(ch) - *(shift + modp(i, shift_size)), 26) ) : ch; } }
void InvertibleRWFunction::PrecomputeTweakedRoots() const { ModularArithmetic modp(m_p), modq(m_q); #pragma omp parallel sections if(CRYPTOPP_RW_USE_OMP) { #pragma omp section m_pre_2_9p = modp.Exponentiate(2, (9 * m_p - 11)/8); #pragma omp section m_pre_2_3q = modq.Exponentiate(2, (3 * m_q - 5)/8); #pragma omp section m_pre_q_p = modp.Exponentiate(m_q, m_p - 2); } m_precompute = true; }
static int buildsturm(int ord, polynomial *sseq) { int i; DBL f; DBL *fp, *fc; polynomial *sp; sseq[0].ord = ord; sseq[1].ord = ord - 1; /* calculate the derivative and normalize the leading coefficient. */ f = fabs(sseq[0].coef[ord] * ord); fp = sseq[1].coef; fc = sseq[0].coef + 1; for (i = 1; i <= ord; i++) { *fp++ = *fc++ * i / f; } /* construct the rest of the Sturm sequence */ for (sp = sseq + 2; modp(sp - 2, sp - 1, sp); sp++) { /* reverse the sign and normalize */ f = -fabs(sp->coef[sp->ord]); for (fp = &sp->coef[sp->ord]; fp >= sp->coef; fp--) { *fp /= f; } } /* reverse the sign */ sp->coef[0] = -sp->coef[0]; return(sp - sseq); }
static inline unsigned long add_modp (unsigned long a, unsigned long b) { /* (a + b) mod p */ return modp(a + b); }
static inline unsigned long mul_modp (unsigned long a, unsigned long b) { /* (a * b) mod p */ return modp(a * b); }
void AstCell::dump(ostream& str) { this->AstNode::dump(str); if (modp()) { str<<" -> "; modp()->dump(str); } else { str<<" ->UNLINKED:"<<modName(); } }
void copyFunctionArg (const Box2i transformWindow, size_t firstSample, size_t numSamples, const FunctionArgPtr &src, const Slice &dst) { assert (src->isVarying()); if (dst.xSampling != 1 || dst.ySampling != 1) throwSliceSampling(); long w = transformWindow.max.x - transformWindow.min.x + 1; long x = transformWindow.min.x + modp (firstSample, w); long y = transformWindow.min.y + divp (firstSample, w); const char *srcData = (src->data()); size_t srcStride = src->type()->alignedObjectSize(); switch (dst.type) { case HALF: if (!src->type().cast<HalfType>()) throwDstSliceTypeMismatch (src, "HALF"); for (size_t i = 0; i < numSamples; ++i) { *(half *)(dst.base + x * dst.xStride + y * dst.yStride) = *(half *)srcData; srcData += srcStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; case Imf::FLOAT: if (!src->type().cast<FloatType>()) throwDstSliceTypeMismatch (src, "FLOAT"); for (size_t i = 0; i < numSamples; ++i) { *(float *)(dst.base + x * dst.xStride + y * dst.yStride) = *(float *)srcData; srcData += srcStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; case Imf::UINT: if (!src->type().cast<UIntType>()) throwDstSliceTypeMismatch (src, "UINT"); for (size_t i = 0; i < numSamples; ++i) { *(unsigned int *)(dst.base + x * dst.xStride + y * dst.yStride) = *(unsigned int *)srcData; srcData += srcStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; } }
Momentum PseudoJet::ModP() const { return modp() * GeV; }
virtual string name() const { return modp()->name(); }
// DJB's "RSA signatures and Rabin-Williams signatures..." (http://cr.yp.to/sigs/rwsota-20080131.pdf). Integer InvertibleRWFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const { DoQuickSanityCheck(); if(!m_precompute) Precompute(); ModularArithmetic modn(m_n), modp(m_p), modq(m_q); Integer r, rInv; do { // Do this in a loop for people using small numbers for testing r.Randomize(rng, Integer::One(), m_n - Integer::One()); // Fix for CVE-2015-2141. Thanks to Evgeny Sidorov for reporting. // Squaring to satisfy Jacobi requirements suggested by Jean-Pierre Muench. r = modn.Square(r); rInv = modn.MultiplicativeInverse(r); } while (rInv.IsZero()); Integer re = modn.Square(r); re = modn.Multiply(re, x); // blind const Integer &h = re, &p = m_p, &q = m_q; Integer e, f; const Integer U = modq.Exponentiate(h, (q+1)/8); if(((modq.Exponentiate(U, 4) - h) % q).IsZero()) e = Integer::One(); else e = -1; const Integer eh = e*h, V = modp.Exponentiate(eh, (p-3)/8); if(((modp.Multiply(modp.Exponentiate(V, 4), modp.Exponentiate(eh, 2)) - eh) % p).IsZero()) f = Integer::One(); else f = 2; Integer W, X; #pragma omp parallel sections if(CRYPTOPP_RW_USE_OMP) { #pragma omp section { W = (f.IsUnit() ? U : modq.Multiply(m_pre_2_3q, U)); } #pragma omp section { const Integer t = modp.Multiply(modp.Exponentiate(V, 3), eh); X = (f.IsUnit() ? t : modp.Multiply(m_pre_2_9p, t)); } } const Integer Y = W + q * modp.Multiply(m_pre_q_p, (X - W)); // Signature Integer s = modn.Multiply(modn.Square(Y), rInv); CRYPTOPP_ASSERT((e * f * s.Squared()) % m_n == x); // IEEE P1363, Section 8.2.8 IFSP-RW, p.44 s = STDMIN(s, m_n - s); if (ApplyFunction(s) != x) // check throw Exception(Exception::OTHER_ERROR, "InvertibleRWFunction: computational error during private key operation"); return s; }
void copyFunctionArg (const Box2i transformWindow, size_t firstSample, size_t numSamples, const Slice &src, const FunctionArgPtr &dst) { assert (dst->isVarying()); if (src.xSampling != 1 || src.ySampling != 1) throwSliceSampling(); long w = transformWindow.max.x - transformWindow.min.x + 1; long x = transformWindow.min.x + modp (firstSample, w); long y = transformWindow.min.y + divp (firstSample, w); char *dstData = (dst->data()); size_t dstStride = dst->type()->alignedObjectSize(); switch (src.type) { case HALF: if (!dst->type().cast<HalfType>()) throwSrcSliceTypeMismatch ("HALF", dst); for (size_t i = 0; i < numSamples; ++i) { *(half *)dstData = *(half *)(src.base + x * src.xStride + y * src.yStride); dstData += dstStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; case Imf::FLOAT: if (!dst->type().cast<FloatType>()) throwSrcSliceTypeMismatch ("FLOAT", dst); for (size_t i = 0; i < numSamples; ++i) { *(float *)dstData = *(float *)(src.base + x * src.xStride + y * src.yStride); dstData += dstStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; case Imf::UINT: if (!dst->type().cast<UIntType>()) throwSrcSliceTypeMismatch ("UINT", dst); for (size_t i = 0; i < numSamples; ++i) { *(unsigned int *)dstData = *(unsigned int *)(src.base + x * src.xStride + y * src.yStride); dstData += dstStride; x += 1; if (x > transformWindow.max.x) { y += 1; x = transformWindow.min.x; } } break; default: // eat NUM_PIXELTYPES break; } }