a little optimize of power

dev
MITSUNARI Shigeo 9 years ago
parent 714f979a1e
commit 0806070fdf
  1. 14
      include/mcl/ec.hpp
  2. 14
      include/mcl/fp.hpp
  3. 29
      include/mcl/util.hpp
  4. 2
      sample/Makefile

@ -368,12 +368,16 @@ public:
}
static inline void mulArray(EcT& z, const EcT& x, const fp::Unit *y, size_t yn, bool isNegative)
{
EcT out;
fp::powerGeneric(out, x, y, yn, EcT::add, EcT::dbl);
EcT tmp;
const EcT *px = &x;
if (&z == &x) {
tmp = x;
px = &tmp;
}
z.clear();
fp::powerGeneric(z, *px, y, yn, EcT::add, EcT::dbl);
if (isNegative) {
neg(z, out);
} else {
z = out;
neg(z, z);
}
}
template<class tag, size_t maxBitSize, template<class _tag, size_t _maxBitSize>class FpT>

@ -270,12 +270,16 @@ public:
}
static inline void powerArray(FpT& z, const FpT& x, const Unit *y, size_t yn, bool isNegative)
{
FpT out(1);
fp::powerGeneric(out, x, y, yn, FpT::mul, FpT::square);
FpT tmp;
const FpT *px = &x;
if (&z == &x) {
tmp = x;
px = &tmp;
}
z = 1;
fp::powerGeneric(z, *px, y, yn, FpT::mul, FpT::square);
if (isNegative) {
FpT::inv(z, out);
} else {
z = out;
FpT::inv(z, z);
}
}
template<class tag2, size_t maxBitSize2>

@ -142,6 +142,34 @@ void getRandVal(T *out, RG& rg, const T *in, size_t bitSize)
*/
template<class G, class T>
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&)){
#if 1
assert(&out != &x);
G t(x);
while (n > 0) {
if (y[n - 1]) break;
n--;
}
if (n == 0) return;
out = x;
int m = cybozu::bsr<T>(y[n - 1]);
if (m == 0) {
if (n == 1) return;
n--;
m = (int)sizeof(T) * 8;
}
for (int i = (int)n - 1; i >= 0; i--) {
T v = y[i];
if (i < n - 1) {
m = (int)sizeof(T) * 8;
}
for (int j = m - 1; j >= 0; j--) {
square(out, out);
if (v & (T(1) << j)) {
mul(out, out, t);
}
}
}
#else
G t(x);
while (n > 0) {
if (y[n - 1]) break;
@ -161,6 +189,7 @@ void powerGeneric(G& out, const G& x, const T *y, size_t n, void mul(G&, const G
square(t, t);
}
}
#endif
}
} } // mcl::fp

@ -3,7 +3,7 @@ include ../common.mk
TARGET=$(TEST_FILE)
LIBS=
SRC=$(wildcard *.cpp)
SRC=bench.cpp ecdh.cpp random.cpp vote.cpp
all: $(TARGET)

Loading…
Cancel
Save