build ok;test ng

dev
MITSUNARI Shigeo 7 years ago
parent 6761073e8b
commit fd8810a398
  1. 16
      common.mk
  2. 6
      include/mcl/bn.hpp
  3. 6
      include/mcl/ec.hpp
  4. 2
      include/mcl/fp.hpp
  5. 73
      include/mcl/gmp_util.hpp
  6. 2
      include/mcl/op.hpp
  7. 6
      include/mcl/operator.hpp
  8. 2
      include/mcl/window_method.hpp
  9. 23
      src/fp.cpp
  10. 32
      src/low_func.hpp
  11. 9
      test/elgamal_test.cpp
  12. 11
      test/fp_test.cpp
  13. 16
      test/fp_util_test.cpp

@ -89,7 +89,21 @@ CFLAGS_OPT_USER?=$(CFLAGS_OPT)
ifeq ($(DEBUG),0)
CFLAGS+=$(CFLAGS_OPT_USER)
endif
LDFLAGS+=-lgmp -lgmpxx -lcrypto $(BIT_OPT) $(LDFLAGS_USER)
MCL_USE_GMP?=1
MCL_USE_OPENSSL?=1
ifeq ($(MCL_USE_GMP),0)
CFLAGS+=-DMCL_USE_VINT
endif
ifeq ($(MCL_USE_OPENSSL),0)
CFLAGS+=-DMCL_DONT_USE_OPENSSL
endif
ifeq ($(MCL_USE_GMP),1)
GMP_LIB=-lgmp -lgmpxx
endif
ifeq ($(MCL_USE_OPENSSL),1)
OPENSSL_LIB=-lcrypto
endif
LDFLAGS+=$(GMP_LIB) $(OPENSSL_LIB) $(BIT_OPT) $(LDFLAGS_USER)
CFLAGS+=-fPIC

@ -521,7 +521,7 @@ struct ParamT {
p = eval(pCoff, z);
assert((p % 6) == 1);
r = eval(rCoff, z);
Fp::init(p.get_str(), mode);
Fp::init(gmp::getStr(p), mode);
Fp2::init(cp.xi_a);
b = cp.b;
Fp2 xi(cp.xi_a, 1);
@ -533,10 +533,10 @@ struct ParamT {
mapTo.init(2 * p - r);
glv1.init(r, z);
const mpz_class largest_c = abs(6 * z + 2);
const mpz_class largest_c = gmp::abs(z * 6 + 2);
useNAF = gmp::getNAF(siTbl, largest_c);
precomputedQcoeffSize = getPrecomputeQcoeffSize(siTbl);
gmp::getNAF(zReplTbl, abs(z));
gmp::getNAF(zReplTbl, gmp::abs(z));
exp_c0 = -2 + z * (-18 + z * (-30 - 36 *z));
exp_c1 = 1 + z * (-12 + z * (-18 - 36 * z));
exp_c2 = 6 * z * z + 1;

@ -592,7 +592,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), gmp::getUnitSize(y), y < 0);
}
template<class tag, size_t maxBitSize, template<class _tag, size_t _maxBitSize>class FpT>
static inline void mulCT(EcT& z, const EcT& x, const FpT<tag, maxBitSize>& y)
@ -603,7 +603,7 @@ public:
}
static inline void mulCT(EcT& z, const EcT& x, const mpz_class& y)
{
mulArray(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0, true);
mulArray(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, true);
}
/*
0 <= P for any P
@ -850,7 +850,7 @@ public:
*/
static inline void mulGeneric(EcT& z, const EcT& x, const mpz_class& y, bool constTime = false)
{
mulArrayBase(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0, constTime);
mulArrayBase(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, constTime);
}
};

@ -96,7 +96,7 @@ public:
}
static inline void init(const mpz_class& m, fp::Mode mode = fp::FP_AUTO)
{
init(m.get_str(), mode);
init(gmp::getStr(m), mode);
}
static inline void init(const std::string& mstr, fp::Mode mode = fp::FP_AUTO)
{

@ -99,6 +99,12 @@ inline void getStr(std::string& str, const mpz_class& z, int base = 10)
str = z.get_str(base);
#endif
}
inline std::string getStr(const mpz_class& z, int base = 10)
{
std::string s;
getStr(s, z, base);
return s;
}
inline void add(mpz_class& z, const mpz_class& x, const mpz_class& y)
{
#ifdef MCL_USE_VINT
@ -251,7 +257,7 @@ inline void sqrMod(mpz_class& z, const mpz_class& x, const mpz_class& m)
inline void pow(mpz_class& z, const mpz_class& x, unsigned int y)
{
#ifdef MCL_USE_VINT
mcl::pow(z, x, y);
Vint::pow(z, x, y);
#else
mpz_pow_ui(z.get_mpz_t(), x.get_mpz_t(), y);
#endif
@ -260,7 +266,7 @@ inline void pow(mpz_class& z, const mpz_class& x, unsigned int y)
inline void powMod(mpz_class& z, const mpz_class& x, const mpz_class& y, const mpz_class& m)
{
#ifdef MCL_USE_VINT
mcl::powMod(z, x, y, m);
Vint::powMod(z, x, y, m);
#else
mpz_powm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t(), m.get_mpz_t());
#endif
@ -268,12 +274,20 @@ inline void powMod(mpz_class& z, const mpz_class& x, const mpz_class& y, const m
// z = 1/x mod m
inline void invMod(mpz_class& z, const mpz_class& x, const mpz_class& m)
{
#ifdef MCL_USE_VINT
Vint::invMod(z, x, m);
#else
mpz_invert(z.get_mpz_t(), x.get_mpz_t(), m.get_mpz_t());
#endif
}
// z = lcm(x, y)
inline void lcm(mpz_class& z, const mpz_class& x, const mpz_class& y)
{
#ifdef MCL_USE_VINT
Vint::lcm(z, x, y);
#else
mpz_lcm(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t());
#endif
}
inline mpz_class lcm(const mpz_class& x, const mpz_class& y)
{
@ -284,7 +298,11 @@ inline mpz_class lcm(const mpz_class& x, const mpz_class& y)
// z = gcd(x, y)
inline void gcd(mpz_class& z, const mpz_class& x, const mpz_class& y)
{
#ifdef MCL_USE_VINT
Vint::gcd(z, x, y);
#else
mpz_gcd(z.get_mpz_t(), x.get_mpz_t(), y.get_mpz_t());
#endif
}
inline mpz_class gcd(const mpz_class& x, const mpz_class& y)
{
@ -299,44 +317,83 @@ inline mpz_class gcd(const mpz_class& x, const mpz_class& y)
*/
inline int legendre(const mpz_class& a, const mpz_class& p)
{
#ifdef MCL_USE_VINT
return Vint::jacobi(a, p);
#else
return mpz_legendre(a.get_mpz_t(), p.get_mpz_t());
#endif
}
inline bool isPrime(const mpz_class& x)
{
#ifdef MCL_USE_VINT
return x.isPrime(32);
#else
return mpz_probab_prime_p(x.get_mpz_t(), 32) != 0;
#endif
}
inline size_t getBitSize(const mpz_class& x)
{
#ifdef MCL_USE_VINT
return x.getBitSize();
#else
return mpz_sizeinbase(x.get_mpz_t(), 2);
#endif
}
inline bool testBit(const mpz_class& x, size_t pos)
{
#ifdef MCL_USE_VINT
return x.testBit(pos);
#else
return mpz_tstbit(x.get_mpz_t(), pos) != 0;
#endif
}
inline void resetBit(mpz_class& x, size_t pos)
{
#ifdef MCL_USE_VINT
x.setBit(pos, false);
#else
mpz_clrbit(x.get_mpz_t(), pos);
#endif
}
inline void setBit(mpz_class& x, size_t pos, bool v = true)
{
#ifdef MCL_USE_VINT
x.setBit(pos, v);
#else
if (v) {
mpz_setbit(x.get_mpz_t(), pos);
} else {
resetBit(x, pos);
}
}
inline Unit getUnit(const mpz_class& x, size_t i)
{
return x.get_mpz_t()->_mp_d[i];
#endif
}
inline const Unit *getUnit(const mpz_class& x)
{
#ifdef MCL_USE_VINT
return x.getUnit();
#else
return reinterpret_cast<const Unit*>(x.get_mpz_t()->_mp_d);
#endif
}
inline Unit getUnit(const mpz_class& x, size_t i)
{
return getUnit(x)[i];
}
inline size_t getUnitSize(const mpz_class& x)
{
assert(x.get_mpz_t()->_mp_size >= 0);
return x.get_mpz_t()->_mp_size;
#ifdef MCL_USE_VINT
return x.getUnitSize();
#else
return abs(x.get_mpz_t()->_mp_size);
#endif
}
inline mpz_class abs(const mpz_class& x)
{
#ifdef MCL_USE_VINT
return Vint::abs(x);
#else
return abs(x.get_mpz_t()->_mp_size);
#endif
}
template<class RG>
void getRand(mpz_class& z, size_t bitSize, RG& rg)

@ -89,7 +89,7 @@ enum IoMode {
namespace fp {
#if defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32)
#if MCL_UNIT_BYTE_SIZE == 4 || (defined(CYBOZU_OS_BIT) && (CYBOZU_OS_BIT == 32))
typedef uint32_t Unit;
#else
typedef uint64_t Unit;

@ -68,15 +68,15 @@ struct Operator : E {
}
static void pow(T& z, const T& x, const mpz_class& y)
{
powArray(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0, false);
powArray(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, false);
}
static void powGeneric(T& z, const T& x, const mpz_class& y)
{
powArrayBase(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0, false);
powArrayBase(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, false);
}
static void powCT(T& z, const T& x, const mpz_class& y)
{
powArray(z, x, gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0, true);
powArray(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, true);
}
static void setPowArrayGLV(void f(T& z, const T& x, const Unit *y, size_t yn, bool isNegative, bool constTime))
{

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

@ -1,6 +1,10 @@
#include <mcl/op.hpp>
#include <mcl/util.hpp>
#ifdef MCL_DONT_USE_OPENSSL
#include <cybozu/sha1.hpp>
#else
#include <cybozu/crypto.hpp>
#endif
#include <cybozu/endian.hpp>
#include "conversion.hpp"
#include "fp_generator.hpp"
@ -189,6 +193,11 @@ bool isEnableJIT()
std::string hash(size_t bitSize, const void *msg, size_t msgSize)
{
#ifdef MCL_DONT_USE_OPENSSL
(void)bitSize;
cybozu::Sha1 sha1;
return sha1.digest((const char*)msg, msgSize);
#else
cybozu::crypto::Hash::Name name;
if (bitSize <= 160) {
name = cybozu::crypto::Hash::N_SHA1;
@ -202,8 +211,10 @@ std::string hash(size_t bitSize, const void *msg, size_t msgSize)
name = cybozu::crypto::Hash::N_SHA512;
}
return cybozu::crypto::Hash::digest(name, (const char *)msg, msgSize);
#endif
}
#ifndef MCL_USE_VINT
static inline void set_mpz_t(mpz_t& z, const Unit* p, int n)
{
int s = n;
@ -215,16 +226,28 @@ static inline void set_mpz_t(mpz_t& z, const Unit* p, int n)
z->_mp_size = s;
z->_mp_d = (mp_limb_t*)const_cast<Unit*>(p);
}
#endif
/*
y = (1/x) mod op.p
*/
static inline void fp_invOpC(Unit *y, const Unit *x, const Op& op)
{
const int N = (int)op.N;
#ifdef MCL_USE_VINT
Vint vx, vy, vp;
vx.setArray(x, N);
vy.setArray(op.p, N);
Vint::invMod(vy, vx, vp);
vy.getArray(y, N);
#else
mpz_class my;
mpz_t mx, mp;
set_mpz_t(mx, x, N);
set_mpz_t(mp, op.p, N);
mpz_invert(my.get_mpz_t(), mx, mp);
gmp::getArray(y, N, my);
#endif
}
/*

@ -51,7 +51,11 @@ template<size_t N, class Tag = Gtag>
struct AddPre {
static inline Unit func(Unit *z, const Unit *x, const Unit *y)
{
#ifdef MCL_USE_VINT
return mcl::vint::addN(z, x, y, N);
#else
return mpn_add_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N);
#endif
}
static const u3u f;
};
@ -90,7 +94,11 @@ template<size_t N, class Tag = Gtag>
struct SubPre {
static inline Unit func(Unit *z, const Unit *x, const Unit *y)
{
#ifdef MCL_USE_VINT
return mcl::vint::subN(z, x, y, N);
#else
return mpn_sub_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, N);
#endif
}
static const u3u f;
};
@ -103,7 +111,11 @@ template<size_t N, class Tag = Gtag>
struct Shr1 {
static inline void func(Unit *y, const Unit *x)
{
#ifdef MCL_USE_VINT
mcl::vint::shrN(y, x, N, 1);
#else
mpn_rshift((mp_limb_t*)y, (const mp_limb_t*)x, (int)N, 1);
#endif
}
static const void2u f;
};
@ -133,7 +145,11 @@ template<size_t N, class Tag = Gtag>
struct MulPreCore {
static inline void func(Unit *z, const Unit *x, const Unit *y)
{
#ifdef MCL_USE_VINT
mcl::vint::mulNM(z, x, N, y, N);
#else
mpn_mul_n((mp_limb_t*)z, (const mp_limb_t*)x, (const mp_limb_t*)y, (int)N);
#endif
}
static const void3u f;
};
@ -218,7 +234,11 @@ template<size_t N, class Tag = Gtag>
struct SqrPreCore {
static inline void func(Unit *y, const Unit *x)
{
#ifdef MCL_USE_VINT
mcl::vint::sqrN(y, x, N);
#else
mpn_sqr((mp_limb_t*)y, (const mp_limb_t*)x, N);
#endif
}
static const void2u f;
};
@ -279,7 +299,11 @@ template<size_t N, class Tag = Gtag>
struct MulUnitPre {
static inline void func(Unit *z, const Unit *x, Unit y)
{
#ifdef MCL_USE_VINT
z[N] = mcl::vint::mul1(z, x, N, y);
#else
z[N] = mpn_mul_1((mp_limb_t*)z, (const mp_limb_t*)x, N, y);
#endif
}
static const void2uI f;
};
@ -292,8 +316,12 @@ template<size_t N, class Tag = Gtag>
struct N1_Mod {
static inline void func(Unit *y, const Unit *x, const Unit *p)
{
#ifdef MCL_USE_VINT
mcl::vint::divNM<Unit>(0, y, x, N + 1, p, N);
#else
mp_limb_t q[2]; // not used
mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N + 1, (const mp_limb_t*)p, N);
#endif
}
static const void3u f;
};
@ -351,8 +379,12 @@ template<size_t N, class Tag = Gtag>
struct Dbl_Mod {
static inline void func(Unit *y, const Unit *x, const Unit *p)
{
#ifdef MCL_USE_VINT
mcl::vint::divNM<Unit>(0, y, x, N * 2, p, N);
#else
mp_limb_t q[N + 1]; // not used
mpn_tdiv_qr(q, (mp_limb_t*)y, 0, (const mp_limb_t*)x, N * 2, (const mp_limb_t*)p, N);
#endif
}
static const void3u f;
};

@ -1,6 +1,10 @@
#include <cybozu/test.hpp>
#include <cybozu/random_generator.hpp>
#ifdef MCL_DONT_USE_OPENSSL
#include <cybozu/sha1.hpp>
#else
#include <cybozu/crypto.hpp>
#endif
#include <mcl/fp.hpp>
#include <mcl/ecparam.hpp>
#include <mcl/elgamal.hpp>
@ -141,8 +145,11 @@ CYBOZU_TEST_AUTO(testEc)
{
ElgamalEc::Zkp zkp;
ElgamalEc::CipherText c;
// cybozu::Sha1 hash;
#ifdef MCL_DONT_USE_OPENSSL
cybozu::Sha1 hash;
#else
cybozu::crypto::Hash hash(cybozu::crypto::Hash::N_SHA256);
#endif
pub.encWithZkp(c, zkp, 0, hash, rg);
CYBOZU_TEST_ASSERT(pub.verify(c, zkp, hash));
zkp.s0 += 1;

@ -7,7 +7,11 @@
#include <time.h>
#include <cybozu/benchmark.hpp>
#include <cybozu/option.hpp>
#ifdef MCL_DONT_USE_OPENSSL
#include <cybozu/sha1.hpp>
#else
#include <cybozu/crypto.hpp>
#endif
#ifdef _MSC_VER
#pragma warning(disable: 4127) // const condition
@ -340,7 +344,7 @@ void moduloTest(const char *pStr)
{
std::string str;
Fp::getModulo(str);
CYBOZU_TEST_EQUAL(str, mpz_class(pStr).get_str());
CYBOZU_TEST_EQUAL(str, mcl::gmp::getStr(mpz_class(pStr)));
}
void opeTest()
@ -711,6 +715,10 @@ void setHashOfTest()
"", "abc", "111111111111111111111111111111111111",
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(msgTbl); i++) {
#ifdef MCL_DONT_USE_OPENSSL
cybozu::Sha1 sha1;
std::string digest = sha1.digest(msgTbl[i]);
#else
size_t bitSize = Fp::getBitSize();
cybozu::crypto::Hash::Name name;
if (bitSize <= 160) {
@ -725,6 +733,7 @@ void setHashOfTest()
name = cybozu::crypto::Hash::N_SHA512;
}
std::string digest = cybozu::crypto::Hash::digest(name, msgTbl[i]);
#endif
Fp x, y;
x.setArrayMask(digest.c_str(), digest.size());
y.setHashOf(msgTbl[i]);

@ -177,6 +177,21 @@ CYBOZU_TEST_AUTO(getRandVal)
CYBOZU_TEST_AUTO(maskArray)
{
#if 1
const size_t n = 2;
uint32_t org[n] = { 0xabce1234, 0xffffef32 };
for (size_t i = 0; i <= sizeof(org) * 8; i++) {
uint32_t x[n];
memcpy(x, org, sizeof(org));
mcl::fp::maskArray(x, n, i);
mpz_class t;
mcl::gmp::setArray(t, org, n);
t &= (mpz_class(1) << i) - 1;
uint32_t y[n];
mcl::gmp::getArray(y, n, t);
CYBOZU_TEST_EQUAL_ARRAY(x, y, n);
}
#else
const size_t n = 4;
uint16_t org[n] = { 0x1234, 0xabce, 0xef32, 0xffff };
for (size_t i = 0; i <= sizeof(org) * 8; i++) {
@ -190,4 +205,5 @@ CYBOZU_TEST_AUTO(maskArray)
mcl::gmp::getArray(y, n, t);
CYBOZU_TEST_EQUAL_ARRAY(x, y, n);
}
#endif
}

Loading…
Cancel
Save