Vector without exception

dev
MITSUNARI Shigeo 7 years ago
parent 6b29693b64
commit 34573a2444
  1. 2
      Makefile
  2. 18
      include/mcl/bn.hpp
  3. 4
      include/mcl/fp.hpp
  4. 19
      include/mcl/gmp_util.hpp
  5. 3
      include/mcl/op.hpp
  6. 77
      include/mcl/vector.hpp
  7. 34
      include/mcl/vint.hpp
  8. 1
      test/fp_util_test.cpp
  9. 32
      test/vector_test.cpp

@ -5,7 +5,7 @@ EXE_DIR=bin
SRC_SRC=fp.cpp bn_c256.cpp bn_c384.cpp bn_c512.cpp she_c256.cpp SRC_SRC=fp.cpp bn_c256.cpp bn_c384.cpp bn_c512.cpp she_c256.cpp
TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn384_test.cpp glv_test.cpp paillier_test.cpp she_test.cpp vint_test.cpp bn512_test.cpp ecdsa_test.cpp conversion_test.cpp TEST_SRC=fp_test.cpp ec_test.cpp fp_util_test.cpp window_method_test.cpp elgamal_test.cpp fp_tower_test.cpp gmp_test.cpp bn_test.cpp bn384_test.cpp glv_test.cpp paillier_test.cpp she_test.cpp vint_test.cpp bn512_test.cpp ecdsa_test.cpp conversion_test.cpp
TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c512_test.cpp she_c256_test.cpp she_c384_test.cpp TEST_SRC+=bn_c256_test.cpp bn_c384_test.cpp bn_c512_test.cpp she_c256_test.cpp she_c384_test.cpp
TEST_SRC+=aggregate_sig_test.cpp TEST_SRC+=aggregate_sig_test.cpp vector_test.cpp
TEST_SRC+=bls12_test.cpp TEST_SRC+=bls12_test.cpp
TEST_SRC+=ecdsa_c_test.cpp TEST_SRC+=ecdsa_c_test.cpp
ifeq ($(CPU),x86-64) ifeq ($(CPU),x86-64)

@ -10,6 +10,7 @@
#include <mcl/ec.hpp> #include <mcl/ec.hpp>
#include <mcl/curve_type.h> #include <mcl/curve_type.h>
#include <assert.h> #include <assert.h>
#include <vector>
/* /*
set bit size of Fp and Fr set bit size of Fp and Fr
@ -105,7 +106,7 @@ void Frobenius3(G2& D, const G2& S);
namespace local { namespace local {
typedef std::vector<int8_t> SignVec; typedef mcl::Vector<int8_t> SignVec;
inline size_t getPrecomputeQcoeffSize(const SignVec& sv) inline size_t getPrecomputeQcoeffSize(const SignVec& sv)
{ {
@ -1707,6 +1708,13 @@ inline void precomputeG2(std::vector<Fp6>& Qcoeff, const G2& Q)
Qcoeff.resize(BN::param.precomputedQcoeffSize); Qcoeff.resize(BN::param.precomputedQcoeffSize);
precomputeG2(Qcoeff.data(), Q); precomputeG2(Qcoeff.data(), Q);
} }
inline bool precomputeG2(mcl::Vector<Fp6>& Qcoeff, const G2& Q)
{
bool b = Qcoeff.resize(BN::param.precomputedQcoeffSize);
if (!b) return false;
precomputeG2(Qcoeff.data(), Q);
return true;
}
inline void precomputedMillerLoop(Fp12& f, const G1& P_, const Fp6* Qcoeff) inline void precomputedMillerLoop(Fp12& f, const G1& P_, const Fp6* Qcoeff)
{ {
G1 P(P_); G1 P(P_);
@ -1747,6 +1755,10 @@ inline void precomputedMillerLoop(Fp12& f, const G1& P, const std::vector<Fp6>&
{ {
precomputedMillerLoop(f, P, Qcoeff.data()); precomputedMillerLoop(f, P, Qcoeff.data());
} }
inline void precomputedMillerLoop(Fp12& f, const G1& P, const mcl::Vector<Fp6>& Qcoeff)
{
precomputedMillerLoop(f, P, Qcoeff.data());
}
/* /*
f = MillerLoop(P1, Q1) x MillerLoop(P2, Q2) f = MillerLoop(P1, Q1) x MillerLoop(P2, Q2)
*/ */
@ -1805,6 +1817,10 @@ inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const std::vector<Fp6>
{ {
precomputedMillerLoop2(f, P1, Q1coeff.data(), P2, Q2coeff.data()); precomputedMillerLoop2(f, P1, Q1coeff.data(), P2, Q2coeff.data());
} }
inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const mcl::Vector<Fp6>& Q1coeff, const G1& P2, const mcl::Vector<Fp6>& Q2coeff)
{
precomputedMillerLoop2(f, P1, Q1coeff.data(), P2, Q2coeff.data());
}
inline void mapToG1(bool *pb, G1& P, const Fp& x) { *pb = BN::param.mapTo.calcG1(P, x); } inline void mapToG1(bool *pb, G1& P, const Fp& x) { *pb = BN::param.mapTo.calcG1(P, x); }
inline void mapToG2(bool *pb, G2& P, const Fp2& x) { *pb = BN::param.mapTo.calcG2(P, x); } inline void mapToG2(bool *pb, G2& P, const Fp2& x) { *pb = BN::param.mapTo.calcG2(P, x); }
inline void mapToG1(G1& P, const Fp& x) inline void mapToG1(G1& P, const Fp& x)

@ -6,9 +6,7 @@
@license modified new BSD license @license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause http://opensource.org/licenses/BSD-3-Clause
*/ */
#include <iostream> #include <iosfwd>
#include <sstream>
#include <vector>
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(push) #pragma warning(push)
#pragma warning(disable : 4127) #pragma warning(disable : 4127)

@ -9,7 +9,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <vector>
#include <assert.h> #include <assert.h>
#include <stdint.h> #include <stdint.h>
#include <cybozu/exception.hpp> #include <cybozu/exception.hpp>
@ -440,8 +439,13 @@ inline void getRand(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen()
assert(bitSize > 1); assert(bitSize > 1);
const size_t rem = bitSize & 31; const size_t rem = bitSize & 31;
const size_t n = (bitSize + 31) / 32; const size_t n = (bitSize + 31) / 32;
std::vector<uint32_t> buf(n); uint32_t buf[128];
rg.read(buf.data(), n * sizeof(buf[0])); assert(n <= CYBOZU_NUM_OF_ARRAY(buf));
if (n > CYBOZU_NUM_OF_ARRAY(buf)) {
z = 0;
return;
}
rg.read(buf, n * sizeof(buf[0]));
uint32_t v = buf[n - 1]; uint32_t v = buf[n - 1];
if (rem == 0) { if (rem == 0) {
v |= 1U << 31; v |= 1U << 31;
@ -450,7 +454,7 @@ inline void getRand(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen()
v |= 1U << (rem - 1); v |= 1U << (rem - 1);
} }
buf[n - 1] = v; buf[n - 1] = v;
setArray(z, &buf[0], n); setArray(z, buf, n);
} }
inline void getRandPrime(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen(), bool setSecondBit = false, bool mustBe3mod4 = false) inline void getRandPrime(mpz_class& z, size_t bitSize, fp::RandGen rg = fp::RandGen(), bool setSecondBit = false, bool mustBe3mod4 = false)
@ -482,9 +486,9 @@ template<class Vec>
void convertToBinary(Vec& v, const mpz_class& x) void convertToBinary(Vec& v, const mpz_class& x)
{ {
const size_t len = gmp::getBitSize(x); const size_t len = gmp::getBitSize(x);
v.clear(); v.resize(len);
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
v.push_back(gmp::testBit(x, len - 1 - i) ? 1 : 0); v[i] = gmp::testBit(x, len - 1 - i) ? 1 : 0;
} }
} }
@ -501,7 +505,8 @@ size_t getContinuousVal(const Vec& v, size_t pos, int val)
template<class Vec> template<class Vec>
void convertToNAF(Vec& v, const Vec& in) void convertToNAF(Vec& v, const Vec& in)
{ {
v = in; // v = in;
v.copy(in);
size_t pos = v.size() - 1; size_t pos = v.size() - 1;
for (;;) { for (;;) {
size_t p = getContinuousVal(v, pos, 0); size_t p = getContinuousVal(v, pos, 0);

@ -8,6 +8,7 @@
*/ */
#include <mcl/gmp_util.hpp> #include <mcl/gmp_util.hpp>
#include <memory.h> #include <memory.h>
#include <mcl/vector.hpp>
#ifndef MCL_MAX_BIT_SIZE #ifndef MCL_MAX_BIT_SIZE
#define MCL_MAX_BIT_SIZE 521 #define MCL_MAX_BIT_SIZE 521
@ -179,7 +180,7 @@ struct Op {
Unit one[maxUnitSize]; Unit one[maxUnitSize];
Unit R2[maxUnitSize]; Unit R2[maxUnitSize];
Unit R3[maxUnitSize]; Unit R3[maxUnitSize];
std::vector<Unit> invTbl; mcl::Vector<Unit> invTbl;
size_t N; size_t N;
size_t bitSize; size_t bitSize;
bool (*fp_isZero)(const Unit*); bool (*fp_isZero)(const Unit*);

@ -0,0 +1,77 @@
#pragma once
/**
@file
@brief tiny vector class
@author MITSUNARI Shigeo(@herumi)
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <malloc.h>
#include <stddef.h>
#include <algorithm>
namespace mcl {
template<class T>
class Vector {
T *p_;
size_t n_;
Vector(const Vector&);
void operator=(const Vector&);
public:
Vector() : p_(0), n_(0) {}
~Vector()
{
free(p_);
}
bool resize(size_t n)
{
if (n <= n_) {
n_ = n;
if (n == 0) {
free(p_);
p_ = 0;
}
return true;
}
T *q = (T*)malloc(sizeof(T) * n);
if (q == 0) return false;
for (size_t i = 0; i < n_; i++) {
q[i] = p_[i];
}
free(p_);
p_ = q;
n_ = n;
return true;
}
bool copy(const Vector<T>& rhs)
{
if (this == &rhs) return true;
if (n_ < rhs.n_) {
clear();
if (!resize(rhs.n_)) return false;
}
for (size_t i = 0; i < rhs.n_; i++) {
p_[i] = rhs.p_[i];
}
n_ = rhs.n_;
return true;
}
void clear()
{
free(p_);
p_ = 0;
n_ = 0;
}
size_t size() const { return n_; }
void swap(Vector<T>& rhs)
{
std::swap(p_, rhs.p_);
std::swap(n_, rhs.n_);
}
T& operator[](size_t n) { return p_[n]; }
const T& operator[](size_t n) const { return p_[n]; }
T* data() { return p_; }
const T* data() const { return p_; }
};
} // mcl

@ -4,11 +4,10 @@
*/ */
#include <cybozu/exception.hpp> #include <cybozu/exception.hpp>
#include <cybozu/bit_operation.hpp> #include <cybozu/bit_operation.hpp>
#include <vector>
#include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <mcl/vector.hpp>
#include <mcl/util.hpp> #include <mcl/util.hpp>
#include <mcl/randgen.hpp> #include <mcl/randgen.hpp>
#include <mcl/conversion.hpp> #include <mcl/conversion.hpp>
@ -619,34 +618,6 @@ void divNM(T *q, size_t qn, T *r, const T *x, size_t xn, const T *y, size_t yn)
} }
} }
template<class T>
class VariableBuffer {
std::vector<T> v_;
public:
typedef T Unit;
VariableBuffer()
{
}
void clear() { v_.clear(); }
/*
@note extended buffer may be not cleared
*/
void alloc(size_t n)
{
v_.resize(n);
}
void swap(VariableBuffer& rhs) { v_.swap(rhs.v_); }
/*
*this = rhs
rhs may be destroyed
*/
size_t allocSize() const { return v_.size(); }
const T& operator[](size_t n) const { return v_[n]; }
T& operator[](size_t n) { return v_[n]; }
};
template<class T> template<class T>
class Buffer { class Buffer {
size_t allocSize_; size_t allocSize_;
@ -680,6 +651,7 @@ public:
std::swap(allocSize_, rhs.allocSize_); std::swap(allocSize_, rhs.allocSize_);
std::swap(ptr_, rhs.ptr_); std::swap(ptr_, rhs.ptr_);
} }
#if 0
#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11 #if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
Buffer(Buffer&& rhs) noexcept Buffer(Buffer&& rhs) noexcept
: allocSize_(0) : allocSize_(0)
@ -692,6 +664,7 @@ public:
swap(rhs); swap(rhs);
return *this; return *this;
} }
#endif
#endif #endif
void clear() void clear()
{ {
@ -1789,7 +1762,6 @@ public:
VintT operator>>(size_t n) const { VintT c = *this; c >>= n; return c; } VintT operator>>(size_t n) const { VintT c = *this; c >>= n; return c; }
}; };
//typedef VintT<vint::VariableBuffer<mcl::vint::Unit> > Vint;
#ifdef MCL_VINT_FIXED_BUFFER #ifdef MCL_VINT_FIXED_BUFFER
typedef VintT<vint::FixedBuffer<mcl::vint::Unit, MCL_MAX_BIT_SIZE * 2> > Vint; typedef VintT<vint::FixedBuffer<mcl::vint::Unit, MCL_MAX_BIT_SIZE * 2> > Vint;
#else #else

@ -3,6 +3,7 @@
#include <mcl/conversion.hpp> #include <mcl/conversion.hpp>
#include <mcl/gmp_util.hpp> #include <mcl/gmp_util.hpp>
#include <mcl/fp.hpp> #include <mcl/fp.hpp>
#include <vector>
CYBOZU_TEST_AUTO(arrayToHex) CYBOZU_TEST_AUTO(arrayToHex)
{ {

@ -0,0 +1,32 @@
#include <mcl/vector.hpp>
#include <cybozu/test.hpp>
CYBOZU_TEST_AUTO(resize)
{
mcl::Vector<int> a, b;
CYBOZU_TEST_EQUAL(a.size(), 0);
CYBOZU_TEST_EQUAL(b.size(), 0);
const size_t n = 3;
bool ok = a.resize(n);
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(n, a.size());
for (size_t i = 0; i < n; i++) {
a[i] = i;
}
ok = b.copy(a);
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(b.size(), n);
CYBOZU_TEST_EQUAL_ARRAY(a.data(), b.data(), n);
const size_t small = n - 1;
ok = b.resize(small);
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(b.size(), small);
CYBOZU_TEST_EQUAL_ARRAY(a.data(), b.data(), small);
const size_t large = n * 2;
ok = b.resize(large);
CYBOZU_TEST_ASSERT(ok);
CYBOZU_TEST_EQUAL(b.size(), large);
CYBOZU_TEST_EQUAL_ARRAY(a.data(), b.data(), small);
}
Loading…
Cancel
Save