// choose random curve // if IsZero(Z), then X is non-trivial factor of ZZ_p::modulus() void ECM_random_curve(EC_pCurve& curve, ZZ_p& X, ZZ_p& Z) { ZZ sigma; RandomBnd(sigma,ZZ_p::modulus()-6); sigma+=6; ZZ_p u,v; u = sqr(to_ZZ_p(sigma))-5; v = 4*to_ZZ_p(sigma); ZZ_p C,Cd; C = (v-u)*sqr(v-u)*(3*u+v); Cd = 4*u*sqr(u)*v; // make sure Cd is invertible ZZ Cinv; if (InvModStatus(Cinv,rep(Cd),ZZ_p::modulus())!=0) { conv(X,Cinv); clear(Z); return; } C*=to_ZZ_p(Cinv); C-=2; // random curve ZZ_pX f; SetCoeff(f,3); SetCoeff(f,2,C); SetCoeff(f,1); conv(curve,f); curve.SetRepresentation(curve.MONTGOMERY); // initial point mul(X,u,sqr(u)); mul(Z,v,sqr(v)); }
bool TestBFDistance(int i1, int i2, float expectedDistance){ cout << "TestBFDistance(" << i1 << "," << i2 << "," << expectedDistance << "):\t\t"; ZZ_p f1 = to_ZZ_p(i1); ZZ_p f2 = to_ZZ_p(i2); VaultMethod* method = new VaultBF(); float disto = method->distance(f1, f2); if(abs(disto - expectedDistance) <.0001){ cout << "passed" << endl; return true; } cout << "failed " << disto << endl; return false; }
// scalar multiplication (svec_ZZ -> svec_ZZ_p) inline void mul(svec_ZZ_p& result, const svec_ZZ& a, const ZZ_p& b) { result.SetLength(a.length()); clear(result); long an = a.nvalues(); const svec_ZZ::index_t* ai = a.indices(); const svec_ZZ::value_t* av = a.values(); for (long i=0; i<an; ++i) if (av[i]!=0) result[ai[i]] = to_ZZ_p(av[i])*b; }
// inner product (svec_ZZ and svec_ZZ_p) inline void InnerProduct(ZZ_p& result, const svec_ZZ& a, const vec_ZZ_p& b) { if (a.length()!=b.length()) { cerr<<"InnerProduct() length mismatch\n"; exit(1); } clear(result); long an = a.nvalues(); const svec_ZZ::index_t* ai = a.indices(); const svec_ZZ::value_t* av = a.values(); for (long i=0; i<an; ++i) result += to_ZZ_p(av[i])*b[ai[i]]; }
void Testbench::initMatrix(mat_ZZ_p & m, int int_size) { ZZ_p n; n.init(to_ZZ("56563749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); switch (int_size) { case 64: n = to_ZZ_p(to_ZZ("9999999999999999995")); break; case 128: n = to_ZZ_p(to_ZZ("99993749237498237498237493299999999995")); break; case 256: n = to_ZZ_p(to_ZZ("99993749237498237498237493299999937129873912873981273129842343242399999799995")); break; case 512: n = to_ZZ_p(to_ZZ("9999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); break; case 1024: n = to_ZZ_p(to_ZZ("99993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); break; case 2048: n = to_ZZ_p(to_ZZ("9999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); break; case 4096: n = to_ZZ_p(to_ZZ("99993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); break; case 8192: n = to_ZZ_p(to_ZZ("9999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995999937492374982374982374932999999371298739128739812731298423432423999997999959999374923749823749823749329999993712987391287398127312984234324239999979999599993749237498237498237493299999937129873912873981273129842343242399999799995")); break; default: n = to_ZZ_p(to_ZZ("9999999999999999995")); break; } for (int i = 1; i <= m.NumRows(); i++) { for (int j = 1; j <= m.NumCols(); j++) { m(i,j) = n; } } }
inline void SetCoeff(NTL::ZZ_pX& poly, long idx, const NTL::ZZ& val) { SetCoeff(poly, idx, to_ZZ_p(val)); }
inline ZZ_p operator/(const ZZ_p& a, const ZZ& b) { return a/to_ZZ_p(b); }
inline ZZ_p operator*(const ZZ& a, const ZZ_p& b) { return to_ZZ_p(a)*b; }
inline const ZZ_p& operator/=(ZZ_p& a, const NTL::ZZ& b) { a /= to_ZZ_p(b); return a; }
inline void SetCoeff(NTL::ZZ_pX& poly, unsigned int idx, const NTL::ZZ& val) { SetCoeff(poly, idx, to_ZZ_p(val)); }