power supports neg

dev
MITSUNARI Shigeo 10 years ago
parent 0683d21e41
commit 75b96be7c6
  1. 2
      include/mcl/ec.hpp
  2. 20
      include/mcl/fp.hpp
  3. 3
      include/mcl/util.hpp
  4. 3
      test/ec_test.cpp
  5. 13
      test/fp_test.cpp

@ -367,7 +367,7 @@ public:
static inline void powerArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative)
{
EcT out;
fp::powerArray(out, x, y, yn, EcT::add, EcT::dbl);
fp::powerGeneric(out, x, y, yn, EcT::add, EcT::dbl);
if (isNegative) {
neg(z, out);
} else {

@ -222,29 +222,31 @@ public:
inv(rev, y);
mul(z, x, rev);
}
static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn)
static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn, bool isNegative)
{
FpT out(1);
fp::powerArray(out, x, y, yn, FpT::mul, FpT::square);
z = out;
fp::powerGeneric(out, x, y, yn, FpT::mul, FpT::square);
if (isNegative) {
FpT::inv(z, out);
} else {
z = out;
}
}
template<class tag2, size_t maxBitSize2>
static inline void power(FpT& z, const FpT& x, const FpT<tag2, maxBitSize2>& y)
{
fp::Block b;
y.getBlock(b);
powerArray(z, x, b.p, b.n);
powerArray(z, x, b.p, b.n, false);
}
static inline void power(FpT& z, const FpT& x, int y)
{
if (y < 0) throw cybozu::Exception("FpT:power with negative y is not support") << y;
const Unit u = y;
powerArray(z, x, &u, 1);
const Unit u = abs(y);
powerArray(z, x, &u, 1, y < 0);
}
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::getUnit(y), Gmp::getUnitSize(x));
powerArray(z, x, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
bool isZero() const { return op_.isZero(v_); }
bool isValid() const

@ -111,8 +111,7 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize)
@param n [in] size of y[]
*/
template<class G, class T>
void powerArray(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&), void square(G&, const G&))
{
void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G&, const G&) , void square(G&, const G&)){
G t(x);
for (size_t i = 0; i < n; i++) {
T v = y[i];

@ -117,7 +117,6 @@ struct Test {
}
}
#if 0
void neg_power() const
{
Fp x(para.gx);
@ -131,7 +130,6 @@ struct Test {
R -= P;
}
}
#endif
void squareRoot() const
{
Fp x(para.gx);
@ -263,6 +261,7 @@ pow 499.00usec
cstr();
ope();
power();
neg_power();
power_fp();
squareRoot();
str();

@ -258,6 +258,19 @@ CYBOZU_TEST_AUTO(power)
CYBOZU_TEST_EQUAL(x, 125);
}
CYBOZU_TEST_AUTO(neg_power)
{
Fp x, y, z;
x = 12345;
z = 1;
Fp rx = 1 / x;
for (int i = 0; i < 100; i++) {
Fp::power(y, x, -i);
CYBOZU_TEST_EQUAL(y, z);
z *= rx;
}
}
CYBOZU_TEST_AUTO(power_fp)
{
Fp x, y, z;

Loading…
Cancel
Save