dev
MITSUNARI Shigeo 9 years ago
parent fba1875b6a
commit 3dc549e17a
  1. 19
      include/mcl/fp.hpp
  2. 2
      include/mcl/fp_tower.hpp
  3. 7
      include/mcl/op.hpp
  4. 21
      src/fp.cpp
  5. 1
      src/fp_proto.hpp
  6. 11
      test/mont_fp_test.cpp

@ -87,6 +87,7 @@ public:
op_.fpDbl_add = fpDbl_addW;
op_.fpDbl_sub = fpDbl_subW;
op_.fp_mul = fp_mulW;
op_.fp_mulI = fp_mulIW;
op_.fpDbl_mod = fpDbl_modW;
/*
priority : MCL_USE_XBYAK > MCL_USE_LLVM > none
@ -306,6 +307,7 @@ public:
static inline void addNC(FpT& z, const FpT& x, const FpT& y) { op_.fp_addNC(z.v_, x.v_, y.v_); }
static inline void subNC(FpT& z, const FpT& x, const FpT& y) { op_.fp_subNC(z.v_, x.v_, y.v_); }
static inline void mul(FpT& z, const FpT& x, const FpT& y) { op_.fp_mul(z.v_, x.v_, y.v_); }
static inline void mulI(FpT& z, const FpT& x, const Unit y) { op_.fp_mulI(z.v_, x.v_, y); }
static inline void inv(FpT& y, const FpT& x) { op_.fp_invOp(y.v_, x.v_, op_); }
static inline void neg(FpT& y, const FpT& x) { op_.fp_neg(y.v_, x.v_); }
static inline void sqr(FpT& y, const FpT& x) { op_.fp_sqr(y.v_, x.v_); }
@ -465,16 +467,27 @@ private:
{
op_.fpDbl_subP(z, x, y, op_.p);
}
// z[N] <- xy[N * 2] % p[N]
static inline void fpDbl_modW(Unit *z, const Unit *xy)
// y[N] <- x[N + 1] % p[N]
static inline void fpN1_modW(Unit *y, const Unit *x)
{
op_.fpDbl_modP(z, xy, op_.p);
op_.fpN1_modP(y, x, op_.p);
}
// y[N] <- x[N * 2] % p[N]
static inline void fpDbl_modW(Unit *y, const Unit *x)
{
op_.fpDbl_modP(y, x, op_.p);
}
// z[N] <- montRed(xy[N * 2])
static inline void fp_montRedW(Unit *z, const Unit *xy)
{
op_.montRedPU(z, xy, op_.p, op_.rp);
}
static inline void fp_mulIW(Unit *z, const Unit *x, Unit y)
{
Unit xy[maxSize + 1];
op_.fp_mulIPre(xy, x, y);
fpN1_modW(z, xy);
}
static inline void fp_mulW(Unit *z, const Unit *x, const Unit *y)
{
Unit xy[maxSize * 2];

@ -164,7 +164,7 @@ public:
{
powerArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
static inline void init(int xi_c)
static inline void init(uint32_t xi_c)
{
assert(Fp::maxSize <= 256);
xi_c_ = xi_c;

@ -31,6 +31,7 @@ struct Op;
typedef void (*void1u)(Unit*);
typedef void (*void2u)(Unit*, const Unit*);
typedef void (*void2uI)(Unit*, const Unit*, Unit);
typedef void (*void2uOp)(Unit*, const Unit*, const Op&);
typedef void (*void3u)(Unit*, const Unit*, const Unit*);
typedef void (*void4u)(Unit*, const Unit*, const Unit*, const Unit*);
@ -92,6 +93,9 @@ struct Op {
void3u fp_add;
void3u fp_sub;
void3u fp_mul;
void2uI fp_mulIPre; // z[N + 1] = x[N] * y
void3u fpN1_modP; // y[N] = x[N + 1] % p[N]
void2uI fp_mulI; // fpN1_modP + fp_mulIPre
bool isFullBit; // true if bitSize % uniSize == 0
bool isMont; // true if use Montgomery
@ -152,6 +156,9 @@ struct Op {
: N(0), bitSize(0)
, fp_isZero(0), fp_clear(0), fp_copy(0)
, fp_neg(0), fp_sqr(0), fp_add(0), fp_sub(0), fp_mul(0)
, fp_mulIPre(0)
, fpN1_modP(0)
, fp_mulI(0)
, isFullBit(true)
, isMont(false)
, isFastMod(false)

@ -193,6 +193,15 @@ struct OpeFunc {
mpz_sub(mz, mx, my);
Gmp::getArray(z, N, mz);
}
// z[N + 1] <- x[N] * y
static inline void fp_mulIPreC(Unit *z, const Unit *x, Unit y)
{
mpz_t mx, mz;
set_zero(mz, z, N + 1);
set_mpz_t(mx, x);
mpz_mul_ui(mz, mx, y);
clearArray(z, mz->_mp_size, N + 1);
}
// z[N * 2] <- x[N] * y[N]
static inline void fpDbl_mulPreC(Unit *z, const Unit *x, const Unit *y)
{
@ -212,6 +221,16 @@ struct OpeFunc {
mpz_mul(my, mx, mx);
clearArray(y, my->_mp_size, N * 2);
}
// y[N] <- x[N + 1] mod p[N]
static inline void fpN1_modPC(Unit *y, const Unit *x, const Unit *p)
{
mpz_t mx, my, mp;
set_mpz_t(mx, x, N + 1);
set_mpz_t(my, y);
set_mpz_t(mp, p);
mpz_mod(my, mx, mp);
clearArray(y, my->_mp_size, N);
}
// y[N] <- x[N * 2] mod p[N]
static inline void fpDbl_modPC(Unit *y, const Unit *x, const Unit *p)
{
@ -315,6 +334,8 @@ struct OpeFunc {
fpDbl_subNC = OpeFunc<n * 2>::fp_subNCC; \
} \
} \
fp_mulIPre = OpeFunc<n>::fp_mulIPreC; \
fpN1_modP = OpeFunc<n>::fpN1_modPC; \
fpDbl_mulPre = OpeFunc<n>::fpDbl_mulPreC; \
fpDbl_sqrPre = OpeFunc<n>::fpDbl_sqrPreC; \
fpDbl_modP = OpeFunc<n>::fpDbl_modPC; \

@ -19,6 +19,7 @@ void mcl_fp_sub ## len ## S(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl:
void mcl_fp_sub ## len ## L(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl::fp::Unit* y, const mcl::fp::Unit* p); \
void mcl_fp_addNC ## len(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl::fp::Unit* y); \
void mcl_fp_subNC ## len(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl::fp::Unit* y); \
void mcl_fp_mulIPre ## len(mcl::fp::Unit* z, const mcl::fp::Unit* x, mcl::fp::Unit y); \
void mcl_fpDbl_mulPre ## len(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl::fp::Unit* y); \
void mcl_fpDbl_sqrPre ## len(mcl::fp::Unit* y, const mcl::fp::Unit* x); \
void mcl_fp_mont ## len(mcl::fp::Unit* z, const mcl::fp::Unit* x, const mcl::fp::Unit* y, const mcl::fp::Unit* p, mcl::fp::Unit r); \

@ -125,6 +125,7 @@ struct Test {
modulo();
ope();
power();
mulI();
power_Zn();
setArray();
set64bit();
@ -488,7 +489,15 @@ struct Test {
z *= x;
}
}
void mulI()
{
Fp x(-1), y, z;
for (int i = 0; i < 20; i++) {
Fp::mul(y, x, i);
Fp::mulI(z, x, i);
CYBOZU_TEST_EQUAL(y, z);
}
}
void power_Zn()
{
Fp x, y, z;

Loading…
Cancel
Save