BitVector conversion is removed

dev
MITSUNARI Shigeo 10 years ago
parent c6523b848a
commit cf7a2e0715
  1. 54
      include/mcl/ec.hpp
  2. 15
      include/mcl/fp.hpp
  3. 89
      include/mcl/fp_util.hpp
  4. 54
      test/ec_test.cpp
  5. 35
      test/fp_test.cpp
  6. 68
      test/fp_util_test.cpp
  7. 37
      test/mont_fp_test.cpp

@ -8,7 +8,6 @@
*/
#include <sstream>
#include <cybozu/exception.hpp>
#include <cybozu/bitvector.hpp>
#include <mcl/operator.hpp>
#include <mcl/power.hpp>
#include <mcl/gmp_util.hpp>
@ -449,59 +448,6 @@ public:
/*
append to bv(not clear bv)
*/
void appendToBitVec(cybozu::BitVector& bv) const
{
#if MCL_EC_COORD == MCL_EC_USE_AFFINE
#error "not implemented"
#else
normalize();
const size_t bitLen = _Fp::getModBitLen();
/*
elem |x|y|z|
size n n 1 if not compressed
size n 1 1 if compressed
*/
const size_t maxBitLen = compressedExpression_ ? (bitLen + 1 + 1) : (bitLen * 2 + 1);
if (isZero()) {
bv.resize(bv.size() + maxBitLen);
return;
}
x.appendToBitVec(bv);
if (compressedExpression_) {
bv.append(Fp::isOdd(y), 1);
} else {
y.appendToBitVec(bv);
}
bv.append(1, 1); // z = 1
#endif
}
void fromBitVec(const cybozu::BitVector& bv)
{
#if MCL_EC_COORD == MCL_EC_USE_AFFINE
#error "not implemented"
#else
const size_t bitLen = _Fp::getModBitLen();
const size_t maxBitLen = compressedExpression_ ? (bitLen + 1 + 1) : (bitLen * 2 + 1);
if (bv.size() != maxBitLen) {
throw cybozu::Exception("EcT:fromBitVec:bad size") << bv.size() << maxBitLen;
}
if (!bv.get(maxBitLen - 1)) { // if z = 0
clear();
return;
}
cybozu::BitVector t;
bv.extract(t, 0, bitLen);
x.fromBitVec(t);
if (compressedExpression_) {
bool odd = bv.get(bitLen); // y
getYfromX(y, x, odd);
} else {
bv.extract(t, bitLen, bitLen);
y.fromBitVec(t);
}
z = 1;
#endif
}
static inline size_t getBitVecSize()
{
const size_t bitLen = _Fp::getModBitLen();

@ -22,7 +22,6 @@
#include <cybozu/hash.hpp>
#include <cybozu/itoa.hpp>
#include <cybozu/atoi.hpp>
#include <cybozu/bitvector.hpp>
#include <mcl/op.hpp>
#include <mcl/fp_util.hpp>
#include <mcl/power.hpp>
@ -293,24 +292,10 @@ public:
powerArray(z, x, Gmp::getBlock(y), Gmp::getBlockSize(x));
}
bool isZero() const { return op_.isZero(v_); }
/*
append to bv(not clear bv)
*/
void appendToBitVec(cybozu::BitVector& bv) const
{
fp::Block b;
getBlock(b);
bv.append(b.p, op_.bitLen);
}
bool isValid() const
{
return fp::compareArray(v_, op_.p, op_.N) < 0;
}
void fromBitVec(const cybozu::BitVector& bv)
{
if (bv.size() != op_.bitLen) throw cybozu::Exception("FpT:fromBitVec:bad size") << bv.size() << op_.bitLen;
setRaw(bv.getBlock(), bv.getBlockSize());
}
static inline size_t getModBitLen() { return op_.bitLen; }
static inline size_t getBitVecSize() { return op_.bitLen; }
bool operator==(const FpT& rhs) const { return fp::isEqualArray(v_, rhs.v_, op_.N); }

@ -2,7 +2,6 @@
#include <vector>
#include <cybozu/itoa.hpp>
#include <cybozu/atoi.hpp>
#include <cybozu/bitvector.hpp>
/**
@file
@brief utility of Fp
@ -201,94 +200,6 @@ inline void getRandVal(S *out, RG& rg, const S *in, size_t bitLen)
}
}
/*
z[] = (x[] << shift) | y
@param z [out] z[0..n)
@param x [in] x[0..n)
@param n [in] length of x, z
@param shift [in] 0 <= shift < (sizeof(S) * 8)
@param y [in]
@return (x[] << shift)[n]
*/
template<class S>
S shiftLeftOr(S* z, const S* x, size_t n, size_t shift, S y = 0)
{
if (n == 0) {
throw cybozu::Exception("fp:shiftLeftOr:bad n");
}
if (shift == 0) {
for (size_t i = n - 1; i > 0; i--) {
z[i] = x[i];
}
z[0] = x[0] | y;
return 0;
}
const size_t unitSize = sizeof(S) * 8;
if (shift >= unitSize) {
throw cybozu::Exception("fp:shiftLeftOr:large shift") << shift;
}
const size_t rev = unitSize - shift;
S ret = x[n - 1] >> rev;
for (size_t i = n - 1; i > 0; i--) {
z[i] = (x[i] << shift) | (x[i - 1] >> rev);
}
z[0] = (x[0] << shift) | y;
return ret;
}
template<class S>
void shiftRight(S* z, const S* x, size_t n, size_t shift)
{
if (n == 0) return;
if (shift == 0) {
for (size_t i = 0; i < n; i++) {
z[i] = x[i];
}
return;
}
const size_t unitSize = sizeof(S) * 8;
if (shift >= unitSize) {
throw cybozu::Exception("fp:shiftRight:large shift") << shift;
}
const size_t rev = unitSize - shift;
S prev = x[0];
for (size_t i = 0; i < n - 1; i++) {
S t = x[i + 1];
z[i] = (prev >> shift) | (t << rev);
prev = t;
}
z[n - 1] = prev >> shift;
}
template<class Vec, class T>
size_t splitBitVec(Vec& v, const cybozu::BitVectorT<T>& bv, size_t width)
{
if (width > sizeof(typename Vec::value_type) * 8) {
throw cybozu::Exception("fp:splitBitVec:bad width") << width;
}
const size_t q = bv.size() / width;
const size_t r = bv.size() % width;
for (size_t i = 0; i < q; i++) {
v.push_back(bv.extract(i * width, width));
}
if (r > 0) {
v.push_back(bv.extract(q * width, r));
}
return r ? r : width;
}
template<class Vec, class T>
void concatBitVec(cybozu::BitVectorT<T>& bv, const Vec& v, size_t width, size_t lastWidth)
{
if (width > sizeof(typename Vec::value_type) * 8) {
throw cybozu::Exception("fp:splitBitVec:bad width") << width;
}
bv.clear();
for (size_t i = 0; i < v.size() - 1; i++) {
bv.append(v[i], width);
}
bv.append(v[v.size() - 1], lastWidth);
}
} // mcl::fp
} // fp

@ -157,59 +157,6 @@ struct Test {
R += P;
}
}
void binaryExpression() const
{
puts("test binaryExpression");
const Fp x(para.gx);
const Fp y(para.gy);
Ec P(x, y);
Ec Q;
// not compressed
Ec::setCompressedExpression(false);
{
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
{
P = -P;
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
P.clear();
{
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
// compressed
Ec::setCompressedExpression(true);
P.set(x, y);
{
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
{
P = -P;
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
P.clear();
{
cybozu::BitVector bv;
P.appendToBitVec(bv);
Q.fromBitVec(bv);
CYBOZU_TEST_EQUAL(P, Q);
}
}
void str() const
{
puts("test str");
@ -316,7 +263,6 @@ pow 499.00usec
power();
neg_power();
power_fp();
binaryExpression();
squareRoot();
str();
#ifdef NDEBUG

@ -374,41 +374,6 @@ CYBOZU_TEST_AUTO(toStr)
}
}
CYBOZU_TEST_AUTO(binaryRepl)
{
const struct {
const char *s;
size_t n;
uint32_t v[6];
} tbl[] = {
{ "0", 0, { 0, 0, 0, 0, 0 } },
{ "1234", 1, { 1234, 0, 0, 0, 0 } },
{ "0xaabbccdd12345678", 2, { 0x12345678, 0xaabbccdd, 0, 0, 0 } },
{ "0x11112222333344445555666677778888", 4, { 0x77778888, 0x55556666, 0x33334444, 0x11112222, 0 } },
{ "0x9911112222333344445555666677778888", 5, { 0x77778888, 0x55556666, 0x33334444, 0x11112222, 0x99, 0 } },
};
Fp::setModulo("0xfffffffffffffffffffffffe26f2fc170f69466a74defd8d");
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
Fp x(tbl[i].s);
cybozu::BitVector bv;
x.appendToBitVec(bv);
CYBOZU_TEST_EQUAL(bv.size(), Fp::getModBitLen());
CYBOZU_TEST_EQUAL(bv.size(), Fp::getBitVecSize());
const Fp::BlockType *block = bv.getBlock();
if (sizeof(Fp::BlockType) == 4) {
CYBOZU_TEST_EQUAL_ARRAY(block, tbl[i].v, tbl[i].n);
} else {
const size_t n = (tbl[i].n + 1) / 2;
for (size_t j = 0; j < n; j++) {
uint64_t v = (uint64_t(tbl[i].v[j * 2 + 1]) << 32) | tbl[i].v[j * 2];
CYBOZU_TEST_EQUAL(block[j], v);
}
}
Fp y;
y.fromBitVec(bv);
CYBOZU_TEST_EQUAL(x, y);
}
}
#endif
#ifdef NDEBUG

@ -121,71 +121,3 @@ CYBOZU_TEST_AUTO(getRandVal)
}
}
CYBOZU_TEST_AUTO(shiftLeftOr)
{
const struct {
uint32_t x[4];
size_t n;
size_t shift;
uint32_t y;
uint32_t z[4];
uint32_t ret;
} tbl[] = {
{ { 0x12345678, 0, 0, 0 }, 1, 0, 0, { 0x12345678, 0, 0, 0 }, 0 },
{ { 0x12345678, 0, 0, 0 }, 1, 1, 0, { 0x2468acf0, 0, 0, 0 }, 0 },
{ { 0xf2345678, 0, 0, 0 }, 1, 1, 5, { 0xe468acf5, 0, 0, 0 }, 1 },
{ { 0x12345678, 0x9abcdef0, 0x11112222, 0xffccaaee }, 4, 19, 0x1234, { 0xb3c01234, 0xf78091a2, 0x1114d5e6, 0x57708889 }, 0x7fe65 },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
uint32_t z[4];
uint32_t ret = mcl::fp::shiftLeftOr(z, tbl[i].x, tbl[i].n, tbl[i].shift, tbl[i].y);
CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, tbl[i].n);
CYBOZU_TEST_EQUAL(ret, tbl[i].ret);
}
}
CYBOZU_TEST_AUTO(shiftRight)
{
const struct {
uint32_t x[4];
size_t n;
size_t shift;
uint32_t z[4];
} tbl[] = {
{ { 0x12345678, 0, 0, 0 }, 4, 0, { 0x12345678, 0, 0, 0 } },
{ { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874 }, 4, 1, { 0x891a2b3c, 0x55555ddd, 0x7ff75de6, 0x7f5d4c3a } },
{ { 0x12345678, 0xaaaabbbb, 0xffeebbcc, 0xfeba9874 }, 4, 18, { 0xaeeec48d, 0xaef32aaa, 0xa61d3ffb, 0x3fae } },
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(tbl); i++) {
uint32_t z[4];
mcl::fp::shiftRight(z, tbl[i].x, tbl[i].n, tbl[i].shift);
CYBOZU_TEST_EQUAL_ARRAY(z, tbl[i].z, tbl[i].n);
}
}
CYBOZU_TEST_AUTO(splitBitVec)
{
uint32_t tbl[] = { 0x12345678, 0xaaaabbbb, 0xffeebbcc };
typedef cybozu::BitVectorT<uint32_t> BitVec;
typedef std::vector<int> IntVec;
BitVec bv;
bv.append(tbl, sizeof(tbl) * 8);
for (size_t len = bv.size(); len > 0; len--) {
bv.resize(len);
for (size_t w = 1; w < 18; w++) {
IntVec iv;
size_t last = mcl::fp::splitBitVec(iv, bv, w);
size_t q = len / w;
size_t r = len % w;
if (r == 0) {
r = w;
} else {
q++;
}
CYBOZU_TEST_EQUAL(iv.size(), q);
BitVec bv2;
mcl::fp::concatBitVec(bv2, iv, w, last);
CYBOZU_TEST_ASSERT(bv == bv2);
}
}
}

@ -136,7 +136,6 @@ struct Test {
setRaw();
set64bit();
getRaw();
binaryExp();
bench();
}
void cstr()
@ -487,42 +486,6 @@ struct Test {
CYBOZU_TEST_EQUAL(x, Fp("0x3400000012"));
#endif
}
void binaryExp()
{
puts("binaryExp");
for (int i = 2; i < 7; i++) {
mpz_class g = m / i;
Fp x, y;
// Fp::toMont(x, g);
x.fromGmp(g);
cybozu::BitVector bv;
x.appendToBitVec(bv);
uint64_t buf[N];
mcl::Gmp::getRaw(buf, N, g);
CYBOZU_TEST_EQUAL(bv.getBlockSize(), N);
CYBOZU_TEST_EQUAL(bv.size(), Fp::getModBitLen());
CYBOZU_TEST_EQUAL(bv.size(), Fp::getBitVecSize());
const uint64_t *p = bv.getBlock();
CYBOZU_TEST_EQUAL_ARRAY(p, buf, N);
}
const mpz_class yy("0x1255556666777788881111222233334444");
if (yy > m) {
return;
}
Fp y;
// Fp::toMont(y, yy);
y.fromGmp(yy);
uint64_t b1[N] = { uint64_t(0x1111222233334444ull), uint64_t(0x5555666677778888ull), 0x12 };
Fp x;
cybozu::BitVector bv;
bv.append(b1, Fp::getModBitLen());
x.fromBitVec(bv);
CYBOZU_TEST_EQUAL(x, y);
bv.clear();
x.appendToBitVec(bv);
const uint64_t *b2 = bv.getBlock();
CYBOZU_TEST_EQUAL_ARRAY(b1, b2, N);
}
void set64bit()
{

Loading…
Cancel
Save