test is ok on mont

dev
MITSUNARI Shigeo 9 years ago
parent 947132497b
commit 7869e59a48
  1. 22
      include/mcl/fp.hpp
  2. 1
      include/mcl/fp_tower.hpp
  3. 4
      include/mcl/op.hpp
  4. 6
      src/fp.cpp
  5. 25
      test/fp_tower_test.cpp

@ -103,20 +103,20 @@ public:
#endif
if (mode == fp::FP_AUTO) mode = fp::FP_GMP;
op_.useMont = mode == fp::FP_LLVM_MONT || mode == fp::FP_XBYAK;
op_.isMont = mode == fp::FP_LLVM_MONT || mode == fp::FP_XBYAK;
if (mode == fp::FP_LLVM_MONT) {
op_.fp_mul = fp_montW;
op_.fp_sqr = fp_montSqrW;
}
#if 0
fprintf(stderr, "mode=%d, useMont=%d"
fprintf(stderr, "mode=%d, isMont=%d"
#ifdef MCL_USE_XBYAK
" ,MCL_USE_XBYAK"
#endif
#ifdef MCL_USE_LLVM
" ,MCL_USE_LLVM"
#endif
"\n", mode, op_.useMont);
"\n", mode, op_.isMont);
#endif
op_.init(mstr, base, maxBitSize, mode);
{ // set oneRep
@ -195,22 +195,22 @@ public:
}
return *this;
}
static inline bool useMont() { return op_.useMont; }
static inline bool isMont() { return op_.isMont; }
/*
convert normal value to Montgomery value
do nothing is !useMont()
do nothing is !isMont()
*/
void toMont()
{
if (useMont()) op_.toMont(v_, v_);
if (isMont()) op_.toMont(v_, v_);
}
/*
convert Montgomery value to normal value
do nothing is !useMont()
do nothing is !isMont()
*/
void fromMont()
{
if (useMont()) op_.fromMont(v_, v_);
if (isMont()) op_.fromMont(v_, v_);
}
void setStr(const std::string& str, int base = 0)
{
@ -257,7 +257,7 @@ public:
void getBlock(fp::Block& b) const
{
b.n = op_.N;
if (useMont()) {
if (isMont()) {
op_.fromMont(b.v_, v_);
b.p = &b.v_[0];
} else {
@ -392,7 +392,7 @@ public:
}
/*
@note
this compare functions is slow because of calling mul if useMont is true.
this compare functions is slow because of calling mul if isMont is true.
*/
static inline int compare(const FpT& x, const FpT& y)
{
@ -414,7 +414,7 @@ public:
bool operator<=(const FpT& rhs) const { return !operator>(rhs); }
/*
@note
return unexpected order if useMont is set.
return unexpected order if isMont is set.
*/
static inline int compareRaw(const FpT& x, const FpT& y)
{

@ -45,6 +45,7 @@ public:
static inline void addNC(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_addNC(z.v_, x.v_, y.v_); }
static inline void subNC(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_subNC(z.v_, x.v_, y.v_); }
static inline void mulPre(FpDblT& z, const Fp& x, const Fp& y) { Fp::op_.fp_mulPre(z.v_, x.v_, y.v_); }
static inline void mod(Fp& y, const FpDblT& x) { Fp::op_.fp_mod(y.v_, x.v_); }
};
/*

@ -100,7 +100,7 @@ struct Op {
void3u fp_addNC; // assume no carry if !isFullBit
void3u fp_subNC; // assume x > y
// for Montgomery
bool useMont;
bool isMont;
int2u fp_preInv;
// these two members are for mcl_fp_mont
Unit rp;
@ -149,7 +149,7 @@ struct Op {
, fp_isZero(0), fp_clear(0), fp_copy(0)
, fp_neg(0), fp_sqr(0), fp_add(0), fp_sub(0), fp_mul(0)
, isFullBit(true), fp_addNC(0), fp_subNC(0)
, useMont(false), fp_preInv(0)
, isMont(false), fp_preInv(0)
, rp(0), mont(0)
, fp_negP(0), fp_invOp(0), fp_addP(0), fp_subP(0), fp_modP(0)
, fg(createFpGenerator())

@ -286,7 +286,7 @@ struct OpeFunc {
fp_clear = OpeFunc<n>::fp_clearC; \
fp_copy = OpeFunc<n>::fp_copyC; \
fp_negP = OpeFunc<n>::fp_negC; \
if (useMont) { \
if (isMont) { \
fp_invOp = OpeFunc<n>::fp_invMontOpC; \
} else { \
fp_invOp = OpeFunc<n>::fp_invOpC; \
@ -417,10 +417,10 @@ void Op::init(const std::string& mstr, int base, size_t maxBitSize, Mode mode)
#ifdef MCL_USE_LLVM
if (mode == FP_AUTO && mp == mpz_class("0xfffffffffffffffffffffffffffffffeffffffffffffffff")) {
fp_mul = &mcl_fp_mul_NIST_P192;
useMont = false;
isMont = false;
}
#endif
if (useMont) {
if (isMont) {
fp::initForMont(*this, p, mode);
}
sq.set(mp);

@ -1,6 +1,7 @@
#define PUT(x) std::cout << #x "=" << (x) << std::endl
#include <cybozu/test.hpp>
#include <cybozu/benchmark.hpp>
#include <cybozu/xorshift.hpp>
#include <time.h>
#include <mcl/fp.hpp>
#include <mcl/fp_tower.hpp>
@ -110,6 +111,30 @@ void testFpDbl()
}
}
}
{
std::string pstr;
Fp::getModulo(pstr);
const mpz_class mp(pstr);
cybozu::XorShift rg;
for (int i = 0; i < 3; i++) {
Fp x, y, z;
mpz_class mx, my, mz, mo;
x.setRand(rg);
x.fromMont();
x.getMpz(mx);
y.setRand(rg);
y.fromMont();
y.getMpz(my);
mo = mx * my;
FpDbl xy;
FpDbl::mulPre(xy, x, y);
FpDbl::mod(z, xy);
z.fromMont();
z.getMpz(mz);
mo %= mp;
CYBOZU_TEST_EQUAL(mz, mo);
}
}
}
void test(const char *p, mcl::fp::Mode mode)

Loading…
Cancel
Save