rename BlockType to Unit

dev
MITSUNARI Shigeo 10 years ago
parent 94b6711a49
commit 1fe925c26a
  1. 2
      include/mcl/ec.hpp
  2. 8
      include/mcl/fp.hpp
  3. 12
      include/mcl/gmp_util.hpp
  4. 2
      include/mcl/window_method.hpp
  5. 22
      test/base_test.cpp
  6. 26
      test/mont_fp_test.cpp

@ -388,7 +388,7 @@ public:
}
static inline void power(EcT& z, const EcT& x, const mpz_class& y)
{
powerArray(z, x, Gmp::getBlock(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);
}
/*
0 <= P for any P

@ -45,7 +45,7 @@ class FpT {
public:
// return pointer to array v_[]
const Unit *getUnit() const { return v_; }
size_t getUnitN() const { return op_.N; }
size_t getUnitSize() const { return op_.N; }
void dump() const
{
const size_t N = op_.N;
@ -208,7 +208,7 @@ public:
}
void setGmp(const mpz_class& x)
{
setArray(Gmp::getBlock(x), Gmp::getBlockSize(x));
setArray(Gmp::getUnit(x), Gmp::getUnitSize(x));
}
static inline void add(FpT& z, const FpT& x, const FpT& y) { op_.add(z.v_, x.v_, y.v_); }
static inline void sub(FpT& z, const FpT& x, const FpT& y) { op_.sub(z.v_, x.v_, y.v_); }
@ -244,7 +244,7 @@ public:
static inline void power(FpT& z, const FpT& x, const mpz_class& y)
{
if (y < 0) throw cybozu::Exception("FpT:power with negative y is not support") << y;
powerArray(z, x, Gmp::getBlock(y), Gmp::getBlockSize(x));
powerArray(z, x, Gmp::getUnit(y), Gmp::getUnitSize(x));
}
bool isZero() const { return op_.isZero(v_); }
bool isValid() const
@ -341,7 +341,7 @@ template<class tag, size_t maxBitN>
struct hash<mcl::FpT<tag, maxBitN> > : public std::unary_function<mcl::FpT<tag, maxBitN>, size_t> {
size_t operator()(const mcl::FpT<tag, maxBitN>& x, uint64_t v = 0) const
{
return static_cast<size_t>(cybozu::hash64(x.getUnit(), x.getUnitN(), v));
return static_cast<size_t>(cybozu::hash64(x.getUnit(), x.getUnitSize(), v));
}
};

@ -58,9 +58,9 @@ namespace mcl {
struct Gmp {
typedef mpz_class ImplType;
#if CYBOZU_OS_BIT == 64
typedef uint64_t BlockType;
typedef uint64_t Unit;
#else
typedef uint32_t BlockType;
typedef uint32_t Unit;
#endif
// z = [buf[n-1]:..:buf[1]:buf[0]]
// eg. buf[] = {0x12345678, 0xaabbccdd}; => z = 0xaabbccdd12345678;
@ -248,15 +248,15 @@ struct Gmp {
{
return mpz_sizeinbase(x.get_mpz_t(), 2);
}
static inline BlockType getBlock(const mpz_class& x, size_t i)
static inline Unit getUnit(const mpz_class& x, size_t i)
{
return x.get_mpz_t()->_mp_d[i];
}
static inline const BlockType *getBlock(const mpz_class& x)
static inline const Unit *getUnit(const mpz_class& x)
{
return reinterpret_cast<const BlockType*>(x.get_mpz_t()->_mp_d);
return reinterpret_cast<const Unit*>(x.get_mpz_t()->_mp_d);
}
static inline size_t getBlockSize(const mpz_class& x)
static inline size_t getUnitSize(const mpz_class& x)
{
assert(x.get_mpz_t()->_mp_size >= 0);
return x.get_mpz_t()->_mp_size;

@ -128,7 +128,7 @@ public:
}
void power(Ec& z, const mpz_class& y) const
{
powerArray(z, Gmp::getBlock(y), abs(y.get_mpz_t()->_mp_size) * UnitBitN, y < 0);
powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size) * UnitBitN, y < 0);
}
void powerArray(Ec& z, const Unit* y, size_t bitLen, bool isNegative) const
{

@ -18,7 +18,7 @@
const size_t MAX_N = 32;
typedef mcl::fp::Unit Unit;
size_t getUnitN(size_t bitLen)
size_t getUnitSize(size_t bitLen)
{
return (bitLen + sizeof(Unit) * 8 - 1) / (sizeof(Unit) * 8);
}
@ -42,8 +42,8 @@ struct Montgomery {
explicit Montgomery(const mpz_class& p)
{
p_ = p;
r_ = mcl::montgomery::getCoff(mcl::Gmp::getBlock(p, 0));
n_ = mcl::Gmp::getBlockSize(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::getBlockSize(y);
mpz_class c = y == 0 ? mpz_class(0) : x * mcl::Gmp::getBlock(y, 0);
Unit q = c == 0 ? 0 : mcl::Gmp::getBlock(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::getBlock(y, i);
c += x * mcl::Gmp::getUnit(y, i);
}
Unit q = c == 0 ? 0 : mcl::Gmp::getBlock(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::getBlockSize(z);
const size_t zSize = mcl::Gmp::getUnitSize(z);
for (size_t i = 0; i < n_; i++) {
if (i < zSize) {
Unit q = mcl::Gmp::getBlock(z, 0) * r_;
Unit q = mcl::Gmp::getUnit(z, 0) * r_;
z += p_ * (mp_limb_t)q;
}
z >>= sizeof(Unit) * 8;
@ -250,7 +250,7 @@ FuncOp getFuncOp(size_t bitLen)
void test(const Unit *p, size_t bitLen)
{
printf("bitLen %d\n", (int)bitLen);
const size_t n = getUnitN(bitLen);
const size_t n = getUnitSize(bitLen);
#ifdef NDEBUG
bool doBench = true;
#else

@ -14,18 +14,18 @@ typedef mcl::FpT<> MontFp6;
typedef mcl::FpT<> MontFp9;
struct Montgomery {
typedef mcl::Gmp::BlockType BlockType;
typedef mcl::Gmp::Unit Unit;
mpz_class p_;
mpz_class R_; // (1 << (pn_ * 64)) % p
mpz_class RR_; // (R * R) % p
BlockType pp_; // p * pp = -1 mod M = 1 << 64
Unit pp_; // p * pp = -1 mod M = 1 << 64
size_t pn_;
Montgomery() {}
explicit Montgomery(const mpz_class& p)
{
p_ = p;
pp_ = mcl::fp::getMontgomeryCoeff(mcl::Gmp::getBlock(p, 0));
pn_ = mcl::Gmp::getBlockSize(p);
pp_ = mcl::fp::getMontgomeryCoeff(mcl::Gmp::getUnit(p, 0));
pn_ = mcl::Gmp::getUnitSize(p);
R_ = 1;
R_ = (R_ << (pn_ * 64)) % p_;
RR_ = (R_ * R_) % p_;
@ -37,18 +37,18 @@ struct Montgomery {
void mul(mpz_class& z, const mpz_class& x, const mpz_class& y) const
{
#if 0
const size_t ySize = mcl::Gmp::getBlockSize(y);
mpz_class c = x * mcl::Gmp::getBlock(y, 0);
BlockType q = mcl::Gmp::getBlock(c, 0) * pp_;
const size_t ySize = mcl::Gmp::getUnitSize(y);
mpz_class c = x * mcl::Gmp::getUnit(y, 0);
Unit q = mcl::Gmp::getUnit(c, 0) * pp_;
c += p_ * q;
c >>= sizeof(BlockType) * 8;
c >>= sizeof(Unit) * 8;
for (size_t i = 1; i < pn_; i++) {
if (i < ySize) {
c += x * mcl::Gmp::getBlock(y, i);
c += x * mcl::Gmp::getUnit(y, i);
}
BlockType q = mcl::Gmp::getBlock(c, 0) * pp_;
Unit q = mcl::Gmp::getUnit(c, 0) * pp_;
c += p_ * q;
c >>= sizeof(BlockType) * 8;
c >>= sizeof(Unit) * 8;
}
if (c >= p_) {
c -= p_;
@ -57,9 +57,9 @@ struct Montgomery {
#else
z = x * y;
for (size_t i = 0; i < pn_; i++) {
BlockType q = mcl::Gmp::getBlock(z, 0) * pp_;
Unit q = mcl::Gmp::getUnit(z, 0) * pp_;
z += p_ * (mp_limb_t)q;
z >>= sizeof(BlockType) * 8;
z >>= sizeof(Unit) * 8;
}
if (z >= p_) {
z -= p_;

Loading…
Cancel
Save