rename Ec::power to Ec::mul

dev
MITSUNARI Shigeo 10 years ago
parent 230fd2e21e
commit 96c6d37dc1
  1. 22
      include/mcl/ec.hpp
  2. 13
      include/mcl/util.hpp
  3. 44
      include/mcl/window_method.hpp
  4. 8
      sample/ecdh_smpl.cpp
  5. 2
      test/Makefile
  6. 24
      test/window_method_test.cpp

@ -412,11 +412,11 @@ public:
}
static inline int compare(const EcT& P, const EcT& Q)
{
return compareFunc(P, Q, _Fp::compare);
return compareFunc(P, Q, Fp::compare);
}
static inline int compareRaw(const EcT& P, const EcT& Q)
{
return compareFunc(P, Q, _Fp::compareRaw);
return compareFunc(P, Q, Fp::compareRaw);
}
bool isZero() const
{
@ -500,10 +500,10 @@ public:
bool operator!=(const EcT& rhs) const { return !operator==(rhs); }
};
template<class _Fp> _Fp EcT<_Fp>::a_;
template<class _Fp> _Fp EcT<_Fp>::b_;
template<class _Fp> int EcT<_Fp>::specialA_;
template<class _Fp> bool EcT<_Fp>::compressedExpression_;
template<class Fp> Fp EcT<Fp>::a_;
template<class Fp> Fp EcT<Fp>::b_;
template<class Fp> int EcT<Fp>::specialA_;
template<class Fp> bool EcT<Fp>::compressedExpression_;
struct EcParam {
const char *name;
@ -521,14 +521,14 @@ struct EcParam {
namespace std { CYBOZU_NAMESPACE_TR1_BEGIN
template<class T> struct hash;
template<class _Fp>
struct hash<mcl::EcT<_Fp> > {
size_t operator()(const mcl::EcT<_Fp>& P) const
template<class Fp>
struct hash<mcl::EcT<Fp> > {
size_t operator()(const mcl::EcT<Fp>& P) const
{
if (P.isZero()) return 0;
P.normalize();
uint64_t v = hash<_Fp>()(P.x);
v = hash<_Fp>()(P.y, v);
uint64_t v = hash<Fp>()(P.x);
v = hash<Fp>()(P.y, v);
return static_cast<size_t>(v);
}
};

@ -113,17 +113,16 @@ 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&)){
G t(x);
while (n > 0) {
if (y[n - 1]) break;
n--;
}
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
assert(v);
m = cybozu::bsr<T>(v) + 1;
}
for (int j = 0; j < m; j++) {
if (v & (T(1) << j)) {

@ -17,24 +17,24 @@ namespace mcl { namespace fp {
*/
template<class T>
struct ArrayIterator {
static const size_t TBitN = sizeof(T) * 8;
static const size_t TbitSize = sizeof(T) * 8;
ArrayIterator(const T *x, size_t bitSize, size_t w)
: x(x)
, bitSize(bitSize)
, w(w)
, pos(0)
, mask((w == TBitN ? 0 : (T(1) << w)) - 1)
, mask((w == TbitSize ? 0 : (T(1) << w)) - 1)
{
assert(w <= TBitN);
assert(w <= TbitSize);
}
bool hasNext() const { return bitSize > 0; }
T getNext()
{
if (w == TBitN) {
if (w == TbitSize) {
bitSize -= w;
return *x++;
}
if (pos + w < TBitN) {
if (pos + w < TbitSize) {
T v = (*x >> pos) & mask;
pos += w;
if (bitSize < w) {
@ -44,7 +44,7 @@ struct ArrayIterator {
}
return v;
}
if (pos + bitSize <= TBitN) {
if (pos + bitSize <= TbitSize) {
assert(bitSize <= w);
T v = *x >> pos;
assert((v >> bitSize) == 0);
@ -52,9 +52,9 @@ struct ArrayIterator {
return v & mask;
}
assert(pos > 0);
T v = (x[0] >> pos) | (x[1] << (TBitN - pos));
T v = (x[0] >> pos) | (x[1] << (TbitSize - pos));
v &= mask;
pos = (pos + w) - TBitN;
pos = (pos + w) - TbitSize;
bitSize -= w;
x++;
return v;
@ -111,30 +111,32 @@ public:
@param y [in] exponent
*/
template<class tag2, size_t maxBitSize2>
void power(Ec& z, const FpT<tag2, maxBitSize2>& y) const
void mul(Ec& z, const FpT<tag2, maxBitSize2>& y) const
{
fp::Block b;
y.getBlock(b);
powerArray(z, b.p, b.n * UnitBitSize, false);
powerArray(z, b.p, b.n, false);
}
void power(Ec& z, int y) const
void mul(Ec& z, int y) const
{
if (y == 0) {
z.clear();
return;
}
Unit u = std::abs(y);
powerArray(z, &u, cybozu::bsr<Unit>(y) + 1, y < 0);
powerArray(z, &u, 1, y < 0);
}
void power(Ec& z, const mpz_class& y) const
void mul(Ec& z, const mpz_class& y) const
{
powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size) * UnitBitSize, y < 0);
powerArray(z, Gmp::getUnit(y), abs(y.get_mpz_t()->_mp_size), y < 0);
}
void powerArray(Ec& z, const Unit* y, size_t bitSize, bool isNegative) const
void powerArray(Ec& z, const Unit* y, size_t n, bool isNegative) const
{
if ((bitSize + winSize_ - 1) / winSize_ > tbl_.size()) throw cybozu::Exception("mcl:WindowMethod:powerArray:bad value") << bitSize << bitSize_ << winSize_;
z.clear();
if (bitSize == 0) return;
while (n > 0) {
if (y[n - 1]) break;
n--;
}
if (n == 0) return;
if (n > tbl_.size()) throw cybozu::Exception("mcl:WindowMethod:powerArray:bad n") << n << tbl_.size();
assert(y[n - 1]);
const size_t bitSize = (n - 1) * UnitBitSize + cybozu::bsr<Unit>(y[n - 1]) + 1;
size_t i = 0;
ArrayIterator<Unit> ai(y, bitSize, winSize_);
do {

@ -37,7 +37,7 @@ int main()
Ec aP;
a.setRand(rg);
Ec::power(aP, P, a); // aP = a * P;
Ec::mul(aP, P, a); // aP = a * P;
std::cout << "aP=" << aP << std::endl;
@ -48,17 +48,17 @@ int main()
Ec bP;
b.setRand(rg);
Ec::power(bP, P, b); // bP = b * P;
Ec::mul(bP, P, b); // bP = b * P;
std::cout << "bP=" << bP << std::endl;
Ec abP, baP;
// Alice uses bP(B's public key) and a(A's priavte key)
Ec::power(abP, bP, a); // abP = a * (bP)
Ec::mul(abP, bP, a); // abP = a * (bP)
// Bob uses aP(A's public key) and b(B's private key)
Ec::power(baP, aP, b); // baP = b * (aP)
Ec::mul(baP, aP, b); // baP = b * (aP)
if (abP == baP) {
std::cout << "key sharing succeed:" << abP << std::endl;

@ -2,7 +2,7 @@ include ../common.mk
TARGET=$(TEST_FILE)
SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp
SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp
ifeq ($(CPU),x64)
SRC+=fp_generator_test.cpp mont_fp_test.cpp
endif

@ -42,29 +42,29 @@ CYBOZU_TEST_AUTO(int)
for (size_t winSize = 2; winSize <= bitSize; winSize += 3) {
PW pw(P, bitSize, winSize);
for (int i = 0; i < (1 << bitSize); i++) {
pw.power(Q, i);
Ec::power(R, P, i);
pw.mul(Q, i);
Ec::mul(R, P, i);
CYBOZU_TEST_EQUAL(Q, R);
}
}
PW pw(P, para.bitSize, 10);
pw.power(Q, -12345);
Ec::power(R, P, -12345);
pw.mul(Q, -12345);
Ec::mul(R, P, -12345);
CYBOZU_TEST_EQUAL(Q, R);
mpz_class t(para.gx);
pw.power(Q, t);
Ec::power(R, P, t);
pw.mul(Q, t);
Ec::mul(R, P, t);
CYBOZU_TEST_EQUAL(Q, R);
t = -t;
pw.power(Q, t);
Ec::power(R, P, t);
pw.mul(Q, t);
Ec::mul(R, P, t);
CYBOZU_TEST_EQUAL(Q, R);
pw.power(Q, x);
Ec::power(R, P, x);
pw.mul(Q, x);
Ec::mul(R, P, x);
CYBOZU_TEST_EQUAL(Q, R);
pw.power(Q, y);
Ec::power(R, P, y);
pw.mul(Q, y);
Ec::mul(R, P, y);
CYBOZU_TEST_EQUAL(Q, R);
}

Loading…
Cancel
Save