rename Gmp to gmp

dev
MITSUNARI Shigeo 9 years ago
parent 41b9e59017
commit 9f93233018
  1. 2
      include/mcl/ec.hpp
  2. 8
      include/mcl/fp.hpp
  3. 6
      include/mcl/fp_tower.hpp
  4. 28
      include/mcl/gmp_util.hpp
  5. 2
      include/mcl/operator.hpp
  6. 2
      include/mcl/window_method.hpp
  7. 4
      sample/bench.cpp
  8. 30
      src/fp.cpp
  9. 20
      test/base_test.cpp
  10. 16
      test/fp_generator_test.cpp
  11. 8
      test/fp_test.cpp
  12. 6
      test/fp_tower_test.cpp
  13. 4
      test/fp_util_test.cpp
  14. 24
      test/mont_fp_test.cpp
  15. 2
      test/window_method_test.cpp

@ -498,7 +498,7 @@ public:
}
static inline void mul(EcT& z, const EcT& x, const mpz_class& y)
{
mulArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
mulArray(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
/*
0 <= P for any P

@ -132,12 +132,12 @@ public:
}
{ // set half
mpz_class half = (op_.mp - 1) / 2;
Gmp::getArray(op_.half, op_.N, half);
gmp::getArray(op_.half, op_.N, half);
}
}
static inline void getModulo(std::string& pstr)
{
Gmp::getStr(pstr, op_.mp);
gmp::getStr(pstr, op_.mp);
}
static inline bool isFullBit() { return op_.isFullBit; }
/*
@ -291,7 +291,7 @@ public:
{
fp::Block b;
getBlock(b);
Gmp::setArray(x, b.p, b.n);
gmp::setArray(x, b.p, b.n);
}
mpz_class getMpz() const
{
@ -302,7 +302,7 @@ public:
void setMpz(const mpz_class& x)
{
if (x < 0) throw cybozu::Exception("Fp:setMpz:negative is not supported") << x;
setArray(Gmp::getUnit(x), Gmp::getUnitSize(x));
setArray(gmp::getUnit(x), gmp::getUnitSize(x));
}
static inline void add(FpT& z, const FpT& x, const FpT& y) { op_.fp_add(z.v_, x.v_, y.v_); }
static inline void sub(FpT& z, const FpT& x, const FpT& y) { op_.fp_sub(z.v_, x.v_, y.v_); }

@ -28,17 +28,17 @@ public:
void setMpz(const mpz_class& x)
{
if (x < 0) throw cybozu::Exception("FpDblT:_setMpz:negative is not supported") << x;
const size_t xn = Gmp::getUnitSize(x);
const size_t xn = gmp::getUnitSize(x);
const size_t N2 = getUnitSize();
if (xn > N2) {
throw cybozu::Exception("FpDblT:setMpz:too large") << x;
}
memcpy(v_, Gmp::getUnit(x), xn * sizeof(Unit));
memcpy(v_, gmp::getUnit(x), xn * sizeof(Unit));
memset(v_ + xn, 0, (N2 - xn) * sizeof(Unit));
}
void getMpz(mpz_class& x) const
{
Gmp::setArray(x, v_, Fp::op_.N * 2);
gmp::setArray(x, v_, Fp::op_.N * 2);
}
static inline void add(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_add(z.v_, x.v_, y.v_); }
static inline void sub(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_sub(z.v_, x.v_, y.v_); }

@ -55,7 +55,7 @@
namespace mcl {
struct Gmp {
struct gmp {
typedef mpz_class ImplType;
#if CYBOZU_OS_BIT == 64
typedef uint64_t Unit;
@ -78,9 +78,9 @@ struct Gmp {
{
const size_t bufByteSize = sizeof(T) * maxSize;
const int xn = x->_mp_size;
if (xn < 0) throw cybozu::Exception("Gmp:getArray:x is negative");
if (xn < 0) throw cybozu::Exception("gmp:getArray:x is negative");
size_t xByteSize = sizeof(*x->_mp_d) * xn;
if (xByteSize > bufByteSize) throw cybozu::Exception("Gmp:getArray:too small") << maxSize;
if (xByteSize > bufByteSize) throw cybozu::Exception("gmp:getArray:too small") << maxSize;
memcpy(buf, x->_mp_d, xByteSize);
memset((char*)buf + xByteSize, 0, bufByteSize - xByteSize);
}
@ -253,6 +253,10 @@ struct Gmp {
{
return mpz_sizeinbase(x.get_mpz_t(), 2);
}
static inline bool testBit(const mpz_class& x, size_t pos)
{
return mpz_tstbit(x.get_mpz_t(), pos) != 0;
}
static inline Unit getUnit(const mpz_class& x, size_t i)
{
return x.get_mpz_t()->_mp_d[i];
@ -282,7 +286,7 @@ struct Gmp {
v |= 1U << (rem - 1);
}
buf[n - 1] = v;
Gmp::setArray(z, &buf[0], n);
gmp::setArray(z, &buf[0], n);
}
template<class RG>
static void getRandPrime(mpz_class& z, size_t bitSize, RG& rg, bool setSecondBit = false, bool mustBe3mod4 = false)
@ -316,11 +320,11 @@ public:
{
p = _p;
if (p <= 2) throw cybozu::Exception("SquareRoot:bad p") << p;
isPrime = Gmp::isPrime(p);
isPrime = gmp::isPrime(p);
if (!isPrime) return; // don't throw until get() is called
// g is quadratic nonresidue
g = 2;
while (Gmp::legendre(g, p) > 0) {
while (gmp::legendre(g, p) > 0) {
g++;
}
// p - 1 = 2^r q, q is odd
@ -330,7 +334,7 @@ public:
r++;
q /= 2;
}
Gmp::powMod(s, g, q, p);
gmp::powMod(s, g, q, p);
}
/*
solve x^2 = a mod p
@ -338,15 +342,15 @@ public:
bool get(mpz_class& x, const mpz_class& a) const
{
if (!isPrime) throw cybozu::Exception("SquareRoot:get:not prime") << p;
if (Gmp::legendre(a, p) < 0) return false;
if (gmp::legendre(a, p) < 0) return false;
if (r == 1) {
Gmp::powMod(x, a, (p + 1) / 4, p);
gmp::powMod(x, a, (p + 1) / 4, p);
return true;
}
mpz_class c = s, d;
int e = r;
Gmp::powMod(d, a, q, p);
Gmp::powMod(x, a, (q + 1) / 2, p); // destroy a if &x == &a
gmp::powMod(d, a, q, p);
gmp::powMod(x, a, (q + 1) / 2, p); // destroy a if &x == &a
while (d != 1) {
int i = 1;
mpz_class dd = (d * d) % p;
@ -356,7 +360,7 @@ public:
}
mpz_class b = 1;
b <<= e - i - 1;
Gmp::powMod(b, c, b, p);
gmp::powMod(b, c, b, p);
x = (x * b) % p;
c = (b * b) % p;
d = (d * c) % p;

@ -54,7 +54,7 @@ struct Operator : E {
}
static inline void power(T& z, const T& x, const mpz_class& y)
{
powerArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
powerArray(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
private:
static inline void powerArray(T& z, const T& x, const Unit *y, size_t yn, bool isNegative)

@ -124,7 +124,7 @@ public:
}
void mul(Ec& z, const mpz_class& y) const
{
powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
powerArray(z, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
void powerArray(Ec& z, const Unit* y, size_t n, bool isNegative) const
{

@ -153,7 +153,7 @@ void benchToStr16()
Fp x(tbl[i]);
CYBOZU_BENCH("fp::toStr16", mcl::fp::toStr16, str, x.getUnit(), x.getUnitSize(), 16);
mpz_class y(tbl[i]);
CYBOZU_BENCH("Gmp:getStr ", mcl::Gmp::getStr, str, y, 16);
CYBOZU_BENCH("gmp:getStr ", mcl::gmp::getStr, str, y, 16);
}
}
@ -178,7 +178,7 @@ void benchFromStr16()
CYBOZU_BENCH("fp:fromStr16", mcl::fp::fromStr16, buf, N, str.c_str(), str.size());
mpz_class y;
CYBOZU_BENCH("Gmp:setStr ", mcl::Gmp::setStr, y, str, 16);
CYBOZU_BENCH("gmp:setStr ", mcl::gmp::setStr, y, str, 16);
}
}

@ -64,13 +64,13 @@ bool strToMpzArray(size_t *pBitSize, Unit *y, size_t maxBitSize, mpz_class& x, c
{
bool isMinus;
const char *p = verifyStr(&isMinus, &base, str);
if (!Gmp::setStr(x, p, base)) {
if (!gmp::setStr(x, p, base)) {
throw cybozu::Exception("fp:strToMpzArray:bad format") << str;
}
const size_t bitSize = Gmp::getBitSize(x);
const size_t bitSize = gmp::getBitSize(x);
if (bitSize > maxBitSize) throw cybozu::Exception("fp:strToMpzArray:too large str") << str << bitSize << maxBitSize;
if (pBitSize) *pBitSize = bitSize;
Gmp::getArray(y, (maxBitSize + UnitBitSize - 1) / UnitBitSize, x);
gmp::getArray(y, (maxBitSize + UnitBitSize - 1) / UnitBitSize, x);
return isMinus;
}
@ -114,7 +114,7 @@ struct OpeFunc {
if (mpz_cmp(mz, mp) >= 0) {
mpz_sub(mz, mz, mp);
}
Gmp::getArray(z, N, mz);
gmp::getArray(z, N, mz);
}
static inline void fp_subPC(Unit *z, const Unit *x, const Unit *y, const Unit *p)
{
@ -129,7 +129,7 @@ struct OpeFunc {
set_mpz_t(mp, p);
mpz_add(mz, mz, mp);
}
Gmp::getArray(z, N, mz);
gmp::getArray(z, N, mz);
}
static inline void set_pDbl(mpz_t& mp, Unit *pDbl, const Unit *p)
{
@ -153,7 +153,7 @@ struct OpeFunc {
if (mpz_cmp(mz, mp) >= 0) {
mpz_sub(mz, mz, mp);
}
Gmp::getArray(z, N * 2, mz);
gmp::getArray(z, N * 2, mz);
}
static inline void fpDbl_subPC(Unit *z, const Unit *x, const Unit *y, const Unit *p)
{
@ -169,7 +169,7 @@ struct OpeFunc {
set_pDbl(mp, pDbl, p);
mpz_add(mz, mz, mp);
}
Gmp::getArray(z, N * 2, mz);
gmp::getArray(z, N * 2, mz);
}
// z[N] <- x[N] + y[N] without carry
static inline void fp_addNCC(Unit *z, const Unit *x, const Unit *y)
@ -180,7 +180,7 @@ struct OpeFunc {
set_mpz_t(mx, x);
set_mpz_t(my, y);
mpz_add(mz, mx, my);
Gmp::getArray(z, N, mz);
gmp::getArray(z, N, mz);
}
static inline void fp_subNCC(Unit *z, const Unit *x, const Unit *y)
{
@ -191,7 +191,7 @@ struct OpeFunc {
set_mpz_t(my, y);
assert(mpz_cmp(mx, my) >= 0);
mpz_sub(mz, mx, my);
Gmp::getArray(z, N, mz);
gmp::getArray(z, N, mz);
}
// z[N + 1] <- x[N] * y
static inline void fp_mulUPreC(Unit *z, const Unit *x, Unit y)
@ -248,7 +248,7 @@ struct OpeFunc {
set_mpz_t(mx, x);
set_mpz_t(mp, op.p);
mpz_invert(my.get_mpz_t(), mx, mp);
Gmp::getArray(y, N, my);
gmp::getArray(y, N, my);
}
/*
inv(xR) = (1/x)R^-1 -toMont-> 1/x -toMont-> (1/x)R
@ -377,12 +377,12 @@ static void initForMont(Op& op, const Unit *p, Mode mode)
assert(N >= 2);
{
mpz_class t = 1, R;
Gmp::getArray(op.one, N, t);
gmp::getArray(op.one, N, t);
R = (t << (N * 64)) % op.mp;
t = (R * R) % op.mp;
Gmp::getArray(op.R2, N, t);
gmp::getArray(op.R2, N, t);
t = (R * R * R) % op.mp;
Gmp::getArray(op.R3, N, t);
gmp::getArray(op.R3, N, t);
}
op.rp = getMontgomeryCoeff(p[0]);
if (mode != FP_XBYAK) return;
@ -470,8 +470,8 @@ void arrayToStr(std::string& str, const Unit *x, size_t n, int base, bool withPr
case 10:
{
mpz_class t;
Gmp::setArray(t, x, n);
Gmp::getStr(str, t, 10);
gmp::setArray(t, x, n);
gmp::getStr(str, t, 10);
}
return;
case 16:

@ -25,7 +25,7 @@ size_t getUnitSize(size_t bitSize)
void setMpz(mpz_class& mx, const Unit *x, size_t n)
{
mcl::Gmp::setArray(mx, x, n);
mcl::gmp::setArray(mx, x, n);
}
void getMpz(Unit *x, size_t n, const mpz_class& mx)
{
@ -42,8 +42,8 @@ struct Montgomery {
explicit Montgomery(const mpz_class& p)
{
p_ = p;
r_ = mcl::montgomery::getCoff(mcl::Gmp::getUnit(p, 0));
n_ = mcl::Gmp::getUnitSize(p);
r_ = mcl::montgomery::getCoff(mcl::gmp::getUnit(p, 0));
n_ = mcl::gmp::getUnitSize(p);
R_ = 1;
R_ = (R_ << (n_ * 64)) % p_;
RR_ = (R_ * R_) % p_;
@ -63,16 +63,16 @@ struct Montgomery {
void mul(mpz_class& z, const mpz_class& x, const mpz_class& y) const
{
#if 1
const size_t ySize = mcl::Gmp::getUnitSize(y);
mpz_class c = y == 0 ? mpz_class(0) : x * mcl::Gmp::getUnit(y, 0);
Unit q = c == 0 ? 0 : mcl::Gmp::getUnit(c, 0) * r_;
const size_t ySize = mcl::gmp::getUnitSize(y);
mpz_class c = y == 0 ? mpz_class(0) : x * mcl::gmp::getUnit(y, 0);
Unit q = c == 0 ? 0 : mcl::gmp::getUnit(c, 0) * r_;
c += p_ * q;
c >>= sizeof(Unit) * 8;
for (size_t i = 1; i < n_; i++) {
if (i < ySize) {
c += x * mcl::Gmp::getUnit(y, i);
c += x * mcl::gmp::getUnit(y, i);
}
Unit q = c == 0 ? 0 : mcl::Gmp::getUnit(c, 0) * r_;
Unit q = c == 0 ? 0 : mcl::gmp::getUnit(c, 0) * r_;
c += p_ * q;
c >>= sizeof(Unit) * 8;
}
@ -82,10 +82,10 @@ struct Montgomery {
z = c;
#else
z = x * y;
const size_t zSize = mcl::Gmp::getUnitSize(z);
const size_t zSize = mcl::gmp::getUnitSize(z);
for (size_t i = 0; i < n_; i++) {
if (i < zSize) {
Unit q = mcl::Gmp::getUnit(z, 0) * r_;
Unit q = mcl::gmp::getUnit(z, 0) * r_;
z += p_ * (mp_limb_t)q;
}
z >>= sizeof(Unit) * 8;

@ -26,8 +26,8 @@ const char *primeTable[] = {
void strToArray(uint64_t *p, size_t n, const char *pStr)
{
mpz_class x;
mcl::Gmp::setStr(x, pStr, 16);
mcl::Gmp::getArray(p, n, x);
mcl::gmp::setStr(x, pStr, 16);
mcl::gmp::getArray(p, n, x);
}
struct Int {
@ -48,7 +48,7 @@ struct Int {
void set(const char *str) { setStr(str); }
void set(const Fp& rhs)
{
mcl::Gmp::getArray(v, MAX_N, rhs.getMpz());
mcl::gmp::getArray(v, MAX_N, rhs.getMpz());
}
void set(const uint64_t* x)
{
@ -146,13 +146,13 @@ void testMulI(const mcl::fp::FpGenerator& fg, int pn)
rg.read(x, pn);
uint64_t y = rg.get64();
mpz_class mx;
mcl::Gmp::setArray(mx, x, pn);
mcl::gmp::setArray(mx, x, pn);
mpz_class my;
mcl::Gmp::set(my, y);
mcl::gmp::set(my, y);
mx *= my;
uint64_t d = fg.mulU_(z, x, y);
z[pn] = d;
mcl::Gmp::setArray(my, z, pn + 1);
mcl::gmp::setArray(my, z, pn + 1);
CYBOZU_TEST_EQUAL(mx, my);
}
{
@ -172,11 +172,11 @@ void testShr1(const mcl::fp::FpGenerator& fg, int pn)
uint64_t z[MAX_N];
rg.read(x, pn);
mpz_class mx;
mcl::Gmp::setArray(mx, x, pn);
mcl::gmp::setArray(mx, x, pn);
mx >>= 1;
fg.shr1_(z, x);
mpz_class my;
mcl::Gmp::setArray(my, z, pn);
mcl::gmp::setArray(my, z, pn);
CYBOZU_TEST_EQUAL(mx, my);
}
}

@ -471,7 +471,7 @@ CYBOZU_TEST_AUTO(getArray)
mpz_class x(tbl[i].s);
const size_t bufN = 8;
uint32_t buf[bufN];
mcl::Gmp::getArray(buf, bufN, x);
mcl::gmp::getArray(buf, bufN, x);
size_t n = mcl::fp::getNonZeroArraySize(buf, bufN);
CYBOZU_TEST_EQUAL(n, tbl[i].vn);
CYBOZU_TEST_EQUAL_ARRAY(buf, tbl[i].v, n);
@ -494,7 +494,7 @@ CYBOZU_TEST_AUTO(getStr)
mpz_class x(tbl[i]);
Fp y(tbl[i]);
std::string xs, ys;
mcl::Gmp::getStr(xs, x, 16);
mcl::gmp::getStr(xs, x, 16);
y.getStr(ys, 16);
CYBOZU_TEST_EQUAL(xs, ys);
}
@ -527,9 +527,9 @@ CYBOZU_TEST_AUTO(mod_NIST_P521)
mcl::fp::Unit in[N * 2 + 1] = {};
mcl::fp::Unit ok[N + 1];
mcl::fp::Unit ex[N + 1];
mcl::Gmp::getArray(in, N * 2 + 1, mx);
mcl::gmp::getArray(in, N * 2 + 1, mx);
mpz_class my = mx % mp;
mcl::Gmp::getArray(ok, N + 1, my);
mcl::gmp::getArray(ok, N + 1, my);
#ifdef MCL_USE_LLVM
mcl_fpDbl_mod_NIST_P521(ex, in);
CYBOZU_TEST_ASSERT(memcmp(ex, ok, sizeof(ex)) == 0);

@ -204,7 +204,7 @@ void testFpDbl()
mpz_class mp(pstr);
mp <<= Fp::getUnitSize() * mcl::fp::UnitBitSize;
mpz_class mp1 = mp - 1;
mcl::Gmp::getStr(pstr, mp1);
mcl::gmp::getStr(pstr, mp1);
const char *tbl[] = {
"0", "1", "123456", "123456789012345668909", pstr.c_str(),
};
@ -218,10 +218,10 @@ void testFpDbl()
mpz_class my(tbl[j]);
y.setMpz(my);
FpDbl::add(z, x, y);
mcl::Gmp::addMod(mo, mx, my, mp);
mcl::gmp::addMod(mo, mx, my, mp);
z.getMpz(mz);
CYBOZU_TEST_EQUAL(mz, mo);
mcl::Gmp::subMod(mo, mx, my, mp);
mcl::gmp::subMod(mo, mx, my, mp);
FpDbl::sub(z, x, y);
z.getMpz(mz);
CYBOZU_TEST_EQUAL(mz, mo);

@ -182,10 +182,10 @@ CYBOZU_TEST_AUTO(maskArray)
memcpy(x, org, sizeof(org));
mcl::fp::maskArray(x, n, i);
mpz_class t;
mcl::Gmp::setArray(t, org, n);
mcl::gmp::setArray(t, org, n);
t &= (mpz_class(1) << i) - 1;
uint16_t y[n];
mcl::Gmp::getArray(y, n, t);
mcl::gmp::getArray(y, n, t);
CYBOZU_TEST_EQUAL_ARRAY(x, y, n);
}
}

@ -9,7 +9,7 @@ typedef mcl::FpT<ZnTag> Zn;
typedef mcl::FpT<> Fp;
struct Montgomery {
typedef mcl::Gmp::Unit Unit;
typedef mcl::gmp::Unit Unit;
mpz_class p_;
mpz_class R_; // (1 << (pn_ * 64)) % p
mpz_class RR_; // (R * R) % p
@ -19,8 +19,8 @@ struct Montgomery {
explicit Montgomery(const mpz_class& p)
{
p_ = p;
rp_ = mcl::fp::getMontgomeryCoeff(mcl::Gmp::getUnit(p, 0));
pn_ = mcl::Gmp::getUnitSize(p);
rp_ = mcl::fp::getMontgomeryCoeff(mcl::gmp::getUnit(p, 0));
pn_ = mcl::gmp::getUnitSize(p);
R_ = 1;
R_ = (R_ << (pn_ * 64)) % p_;
RR_ = (R_ * R_) % p_;
@ -32,16 +32,16 @@ struct Montgomery {
void mul(mpz_class& z, const mpz_class& x, const mpz_class& y) const
{
#if 0
const size_t ySize = mcl::Gmp::getUnitSize(y);
mpz_class c = x * mcl::Gmp::getUnit(y, 0);
Unit q = mcl::Gmp::getUnit(c, 0) * rp_;
const size_t ySize = mcl::gmp::getUnitSize(y);
mpz_class c = x * mcl::gmp::getUnit(y, 0);
Unit q = mcl::gmp::getUnit(c, 0) * rp_;
c += p_ * q;
c >>= sizeof(Unit) * 8;
for (size_t i = 1; i < pn_; i++) {
if (i < ySize) {
c += x * mcl::Gmp::getUnit(y, i);
c += x * mcl::gmp::getUnit(y, i);
}
Unit q = mcl::Gmp::getUnit(c, 0) * rp_;
Unit q = mcl::gmp::getUnit(c, 0) * rp_;
c += p_ * q;
c >>= sizeof(Unit) * 8;
}
@ -52,7 +52,7 @@ struct Montgomery {
#else
z = x * y;
for (size_t i = 0; i < pn_; i++) {
Unit q = mcl::Gmp::getUnit(z, 0) * rp_;
Unit q = mcl::gmp::getUnit(z, 0) * rp_;
z += p_ * (mp_limb_t)q;
z >>= sizeof(Unit) * 8;
}
@ -68,7 +68,7 @@ mpz_class getMpz(const T& x)
{
std::string str = x.getStr();
mpz_class t;
mcl::Gmp::setStr(t, str);
mcl::gmp::setStr(t, str);
return t;
}
@ -584,8 +584,8 @@ put(z);
mpz_class p(pStr);
Montgomery mont(p);
mpz_class xx, yy;
mcl::Gmp::setArray(xx, x, CYBOZU_NUM_OF_ARRAY(x));
mcl::Gmp::setArray(yy, y, CYBOZU_NUM_OF_ARRAY(y));
mcl::gmp::setArray(xx, x, CYBOZU_NUM_OF_ARRAY(x));
mcl::gmp::setArray(yy, y, CYBOZU_NUM_OF_ARRAY(y));
mpz_class z;
mont.mul(z, xx, yy);
std::cout << std::hex << z << std::endl;

@ -11,7 +11,7 @@ CYBOZU_TEST_AUTO(ArrayIterator)
for (size_t w = 1; w <= 32; w++) {
const uint32_t mask = uint32_t((uint64_t(1) << w) - 1);
mpz_class x;
mcl::Gmp::setArray(x, in, 2);
mcl::gmp::setArray(x, in, 2);
mcl::fp::ArrayIterator<uint32_t> ai(in, bitSize, w);
size_t n = (bitSize + w - 1) / w;
for (size_t j = 0; j < n; j++) {

Loading…
Cancel
Save