add addNC/subNC

dev
MITSUNARI Shigeo 9 years ago
parent 648590dee7
commit 20a3d70625
  1. 2
      include/mcl/fp.hpp
  2. 15
      include/mcl/fp_tower.hpp
  3. 8
      include/mcl/op.hpp
  4. 29
      src/fp.cpp
  5. 5
      test/fp_test.cpp
  6. 7
      test/mont_fp_test.cpp

@ -278,6 +278,8 @@ public:
}
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_); }
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 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_); }

@ -126,6 +126,9 @@ public:
}
static inline void init(int xi_c)
{
if (Fp::maxSize > 256) {
throw cybozu::Exception("Fp2T:init:not support size") << Fp::maxSize;
}
xi_c_ = xi_c;
Fp::op_.fp2_add = fp2_addW;
Fp::op_.fp2_sub = fp2_subW;
@ -259,5 +262,17 @@ private:
template<class Fp> Fp Fp2T<Fp>::xi_c_;
/*
Fp6T = Fp2[v] / (v^3 - xi)
xi = -u - 1
x = a + b v + c v^2
*/
template<class Fp>
class Fp6T {
typedef Fp2T<Fp> Fp2;
public:
};
} // mcl

@ -79,6 +79,13 @@ struct Op {
void3u fp_add;
void3u fp_sub;
void3u fp_mul;
bool fullBit; // true if bitSize % uniSize == 0
/*
same fp_add, fp_sub if fullBit
*/
void3u fp_addNC; // assume no carry if !fullBit
void3u fp_subNC; // assume x > y
// for Montgomery
bool useMont;
int2u fp_preInv;
@ -112,6 +119,7 @@ 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)
, fullBit(true), fp_addNC(0), fp_subNC(0)
, useMont(false), fp_preInv(0)
, rp(0), mont(0)
, fp_negP(0), fp_sqrPreP(0), fp_invOp(0), fp_addP(0), fp_subP(0), fp_mulPreP(0), fp_modP(0)

@ -122,6 +122,27 @@ struct OpeFunc {
}
Gmp::getArray(z, N, mz);
}
// z = x + y
static inline void fp_addNCC(Unit *z, const Unit *x, const Unit *y)
{
Unit ret[N + 2]; // not N + 1
mpz_t mz, mx, my;
set_zero(mz, ret, N + 2);
set_mpz_t(mx, x);
set_mpz_t(my, y);
mpz_add(mz, mx, my);
Gmp::getArray(z, N, mz);
}
static inline void fp_subNCC(Unit *z, const Unit *x, const Unit *y)
{
Unit ret[N + 1];
mpz_t mz, mx, my;
set_zero(mz, ret, N + 1);
set_mpz_t(mx, x);
set_mpz_t(my, y);
mpz_sub(mz, mx, my);
Gmp::getArray(z, N, mz);
}
// z[N * 2] <- x[N] * y[N]
static inline void fp_mulPreC(Unit *z, const Unit *x, const Unit *y)
{
@ -210,6 +231,13 @@ struct OpeFunc {
} \
fp_addP = OpeFunc<n>::fp_addC; \
fp_subP = OpeFunc<n>::fp_subC; \
if (fullBit) { \
fp_addNC = fp_add; \
fp_subNC = fp_sub; \
} else { \
fp_addNC = OpeFunc<n>::fp_addNCC; \
fp_subNC = OpeFunc<n>::fp_subNCC; \
} \
fp_mulPreP = OpeFunc<n>::fp_mulPreC; \
fp_sqrPreP = OpeFunc<n>::fp_sqrPreC; \
fp_modP = OpeFunc<n>::fp_modC; \
@ -284,6 +312,7 @@ void Op::init(const std::string& mstr, int base, size_t maxBitSize, Mode mode)
bool isMinus = fp::strToMpzArray(&bitSize, p, maxBitSize, mp, mstr, base);
if (isMinus) throw cybozu::Exception("Op:init:mstr is minus") << mstr;
if (mp == 0) throw cybozu::Exception("Op:init:mstr is zero") << mstr;
fullBit = (bitSize % UnitBitSize) == 0;
const size_t roundBit = (bitSize + UnitBitSize - 1) & ~(UnitBitSize - 1);
switch (roundBit) {

@ -239,6 +239,11 @@ CYBOZU_TEST_AUTO(ope)
z *= y;
CYBOZU_TEST_EQUAL(z, tbl[i].x);
}
Fp x(5), y(3), z;
Fp::addNC(z, x, y);
CYBOZU_TEST_EQUAL(z, Fp(8));
Fp::subNC(z, x, y);
CYBOZU_TEST_EQUAL(z, Fp(2));
}
struct tag2;

@ -456,6 +456,13 @@ struct Test {
z *= y;
CYBOZU_TEST_EQUAL(z, castTo<Fp>(tbl[i].x));
}
{
Fp x(5), y(3), z;
Fp::addNC(z, x, y);
CYBOZU_TEST_EQUAL(z, Fp(8));
// Fp::subNC(z, x, y);
// CYBOZU_TEST_EQUAL(z, Fp(2));
}
}
void power()
{

Loading…
Cancel
Save