avoid power.hpp

dev
MITSUNARI Shigeo 10 years ago
parent e9726b4338
commit 90415639ca
  1. 31
      include/mcl/ec.hpp
  2. 32
      include/mcl/fp.hpp
  3. 32
      include/mcl/util.hpp
  4. 3
      test/ec_test.cpp

@ -8,7 +8,6 @@
*/
#include <sstream>
#include <cybozu/exception.hpp>
#include <mcl/power.hpp>
#include <mcl/gmp_util.hpp>
namespace mcl {
@ -113,6 +112,10 @@ public:
y.clear();
}
static inline void dbl2(EcT& R, const EcT& P)
{
dbl(R, P);
}
static inline void dbl(EcT& R, const EcT& P, bool verifyInf = true)
{
if (verifyInf) {
@ -363,10 +366,30 @@ public:
R.z = P.z;
#endif
}
template<class N>
static inline void power(EcT& z, const EcT& x, const N& y)
static inline void powerArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn)
{
EcT out;
out.clear();
fp::powerArray(out, x, y, yn, EcT::add, EcT::dbl2);
z = out;
}
template<class tag, size_t maxBitN>
static inline void power(EcT& z, const EcT& x, const FpT<tag, maxBitN>& y)
{
fp::Block b;
y.getBlock(b);
powerArray(z, x, b.p, b.n);
}
static inline void power(EcT& z, const EcT& x, int y)
{
if (y < 0) throw cybozu::Exception("EcT:power with negative y is not support") << y;
const fp::Unit u = y;
powerArray(z, x, &u, 1);
}
static inline void power(EcT& z, const EcT& x, const mpz_class& y)
{
power_impl::power(z, x, y);
if (y < 0) throw cybozu::Exception("EcT:power with negative y is not support") << y;
powerArray(z, x, Gmp::getBlock(y), Gmp::getBlockSize(x));
}
#if 0
/*

@ -228,26 +228,7 @@ public:
static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn)
{
FpT out(1);
FpT t(x);
for (size_t i = 0; i < yn; i++) {
const Unit v = y[i];
int m = (int)fp::UnitBitN;
if (i == yn - 1) {
#if 1
m = v ? cybozu::bsr<Unit>(v) + 1 : 0;
#else
while (m > 0 && (v & (Unit(1) << (m - 1))) == 0) {
m--;
}
#endif
}
for (int j = 0; j < m; j++) {
if (v & (Unit(1) << j)) {
out *= t;
}
t *= t;
}
}
fp::powerArray(out, x, y, yn, FpT::mul, FpT::square);
z = out;
}
template<class tag2, size_t maxBitN2>
@ -354,17 +335,6 @@ private:
template<class tag, size_t maxBitN> fp::Op FpT<tag, maxBitN>::op_;
namespace power_impl {
template<class G, class tag, size_t bitN, template<class _tag, size_t _bitN>class FpT>
void power(G& z, const G& x, const FpT<tag, bitN>& y)
{
fp::Block b;
y.getBlock(b);
mcl::power_impl::powerArray(z, x, b.p, b.n);
}
} // mcl::power_impl
} // mcl
namespace std { CYBOZU_NAMESPACE_TR1_BEGIN

@ -7,6 +7,7 @@
http://opensource.org/licenses/BSD-3-Clause
*/
#include <mcl/gmp_util.hpp>
#include <cybozu/bit_operation.hpp>
namespace mcl { namespace fp {
@ -103,5 +104,36 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitLen)
}
}
/*
@param out [inout] : set element of G ; out = x^y[]
@param x [in]
@param y [in]
@param n [in] size of y[]
*/
template<class G, class T, class Mul, class Square>
void powerArray(G& out, const G& x, const T *y, size_t n, Mul mul, Square square)
{
G t(x);
for (size_t i = 0; i < n; i++) {
T v = y[i];
int m = (int)sizeof(T) * 8;
if (i == n - 1) {
#if 1
m = v ? cybozu::bsr<T>(v) + 1 : 0;
#else
while (m > 0 && (v & (Unit(1) << (m - 1))) == 0) {
m--;
}
#endif
}
for (int j = 0; j < m; j++) {
if (v & (T(1) << j)) {
mul(out, out, t);
}
square(t, t);
}
}
}
} } // mcl::fp

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

Loading…
Cancel
Save