aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2018-05-23 19:03:55 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2018-05-23 19:03:55 +0800
commit34573a2444fc42fb417b26cce484146d5a910a0d (patch)
treee19837abd38a7da1c98093864a06c0ce4ccfb451
parent6b29693b6423791fb1590c2b7e8f307bcb5de09d (diff)
downloadtangerine-mcl-34573a2444fc42fb417b26cce484146d5a910a0d.tar.gz
tangerine-mcl-34573a2444fc42fb417b26cce484146d5a910a0d.tar.zst
tangerine-mcl-34573a2444fc42fb417b26cce484146d5a910a0d.zip
Vector without exception
-rw-r--r--Makefile2
-rw-r--r--include/mcl/bn.hpp18
-rw-r--r--include/mcl/fp.hpp4
-rw-r--r--include/mcl/gmp_util.hpp19
-rw-r--r--include/mcl/op.hpp3
-rw-r--r--include/mcl/vector.hpp77
-rw-r--r--include/mcl/vint.hpp34
-rw-r--r--test/fp_util_test.cpp1
-rw-r--r--test/vector_test.cpp32
9 files changed, 146 insertions, 44 deletions
diff --git a/Makefile b/Makefile
index 039f573..b613ee0 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ EXE_DIR=bin
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+=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+=ecdsa_c_test.cpp
ifeq ($(CPU),x86-64)
diff --git a/include/mcl/bn.hpp b/include/mcl/bn.hpp
index 04397f6..1070c44 100644
--- a/include/mcl/bn.hpp
+++ b/include/mcl/bn.hpp
@@ -10,6 +10,7 @@
#include <mcl/ec.hpp>
#include <mcl/curve_type.h>
#include <assert.h>
+#include <vector>
/*
set bit size of Fp and Fr
@@ -105,7 +106,7 @@ void Frobenius3(G2& D, const G2& S);
namespace local {
-typedef std::vector<int8_t> SignVec;
+typedef mcl::Vector<int8_t> SignVec;
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);
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)
{
G1 P(P_);
@@ -1747,6 +1755,10 @@ inline void precomputedMillerLoop(Fp12& f, const G1& P, const std::vector<Fp6>&
{
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)
*/
@@ -1805,6 +1817,10 @@ inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const std::vector<Fp6>
{
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 mapToG2(bool *pb, G2& P, const Fp2& x) { *pb = BN::param.mapTo.calcG2(P, x); }
inline void mapToG1(G1& P, const Fp& x)
diff --git a/include/mcl/fp.hpp b/include/mcl/fp.hpp
index 6c39d1f..4b6d848 100644
--- a/include/mcl/fp.hpp
+++ b/include/mcl/fp.hpp
@@ -6,9 +6,7 @@
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
-#include <iostream>
-#include <sstream>
-#include <vector>
+#include <iosfwd>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4127)
diff --git a/include/mcl/gmp_util.hpp b/include/mcl/gmp_util.hpp
index 47c1ab8..f27253c 100644
--- a/include/mcl/gmp_util.hpp
+++ b/include/mcl/gmp_util.hpp
@@ -9,7 +9,6 @@
*/
#include <stdio.h>
#include <stdlib.h>
-#include <vector>
#include <assert.h>
#include <stdint.h>
#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);
const size_t rem = bitSize & 31;
const size_t n = (bitSize + 31) / 32;
- std::vector<uint32_t> buf(n);
- rg.read(buf.data(), n * sizeof(buf[0]));
+ uint32_t buf[128];
+ 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];
if (rem == 0) {
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);
}
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)
@@ -482,9 +486,9 @@ template<class Vec>
void convertToBinary(Vec& v, const mpz_class& x)
{
const size_t len = gmp::getBitSize(x);
- v.clear();
+ v.resize(len);
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>
void convertToNAF(Vec& v, const Vec& in)
{
- v = in;
+// v = in;
+ v.copy(in);
size_t pos = v.size() - 1;
for (;;) {
size_t p = getContinuousVal(v, pos, 0);
diff --git a/include/mcl/op.hpp b/include/mcl/op.hpp
index c0c1098..b947284 100644
--- a/include/mcl/op.hpp
+++ b/include/mcl/op.hpp
@@ -8,6 +8,7 @@
*/
#include <mcl/gmp_util.hpp>
#include <memory.h>
+#include <mcl/vector.hpp>
#ifndef MCL_MAX_BIT_SIZE
#define MCL_MAX_BIT_SIZE 521
@@ -179,7 +180,7 @@ struct Op {
Unit one[maxUnitSize];
Unit R2[maxUnitSize];
Unit R3[maxUnitSize];
- std::vector<Unit> invTbl;
+ mcl::Vector<Unit> invTbl;
size_t N;
size_t bitSize;
bool (*fp_isZero)(const Unit*);
diff --git a/include/mcl/vector.hpp b/include/mcl/vector.hpp
new file mode 100644
index 0000000..eb0fb07
--- /dev/null
+++ b/include/mcl/vector.hpp
@@ -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
+
diff --git a/include/mcl/vint.hpp b/include/mcl/vint.hpp
index 5530b46..1ffeba2 100644
--- a/include/mcl/vint.hpp
+++ b/include/mcl/vint.hpp
@@ -4,11 +4,10 @@
*/
#include <cybozu/exception.hpp>
#include <cybozu/bit_operation.hpp>
-#include <vector>
-#include <stdlib.h>
#include <assert.h>
#include <cmath>
#include <iostream>
+#include <mcl/vector.hpp>
#include <mcl/util.hpp>
#include <mcl/randgen.hpp>
#include <mcl/conversion.hpp>
@@ -620,34 +619,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>
class Buffer {
size_t allocSize_;
T *ptr_;
@@ -680,6 +651,7 @@ public:
std::swap(allocSize_, rhs.allocSize_);
std::swap(ptr_, rhs.ptr_);
}
+#if 0
#if CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
Buffer(Buffer&& rhs) noexcept
: allocSize_(0)
@@ -693,6 +665,7 @@ public:
return *this;
}
#endif
+#endif
void clear()
{
allocSize_ = 0;
@@ -1789,7 +1762,6 @@ public:
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
typedef VintT<vint::FixedBuffer<mcl::vint::Unit, MCL_MAX_BIT_SIZE * 2> > Vint;
#else
diff --git a/test/fp_util_test.cpp b/test/fp_util_test.cpp
index 3e77b3b..b11311b 100644
--- a/test/fp_util_test.cpp
+++ b/test/fp_util_test.cpp
@@ -3,6 +3,7 @@
#include <mcl/conversion.hpp>
#include <mcl/gmp_util.hpp>
#include <mcl/fp.hpp>
+#include <vector>
CYBOZU_TEST_AUTO(arrayToHex)
{
diff --git a/test/vector_test.cpp b/test/vector_test.cpp
new file mode 100644
index 0000000..d593e4c
--- /dev/null
+++ b/test/vector_test.cpp
@@ -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);
+}