build bn_c256 with -fno-exceptions

dev
MITSUNARI Shigeo 7 years ago
parent c5e574bbfc
commit d82f2944a8
  1. 4
      Makefile
  2. 51
      include/mcl/bn.hpp
  3. 81
      include/mcl/ec.hpp
  4. 31
      include/mcl/fp.hpp
  5. 102
      include/mcl/fp_tower.hpp
  6. 25
      include/mcl/gmp_util.hpp
  7. 29
      include/mcl/lagrange.hpp
  8. 18
      include/mcl/randgen.hpp
  9. 90
      include/mcl/vint.hpp
  10. 12
      src/bn_c_impl.hpp
  11. 8
      src/fp.cpp

@ -261,10 +261,10 @@ endif
emcc -o $@ src/fp.cpp src/she_c384.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=384 -s TOTAL_MEMORY=67108864 -s DISABLE_EXCEPTION_CATCHING=0
../mcl-wasm/mcl_c.js: src/bn_c256.cpp $(MCL_C_DEP)
emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 #-DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING
emcc -o $@ src/fp.cpp src/bn_c256.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions
../mcl-wasm/mcl_c512.js: src/bn_c512.cpp $(MCL_C_DEP)
emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1
emcc -o $@ src/fp.cpp src/bn_c512.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=512 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1 -DCYBOZU_DONT_USE_EXCEPTION -DCYBOZU_DONT_USE_STRING -fno-exceptions
../ecdsa-wasm/ecdsa_c.js: src/ecdsa_c.cpp src/fp.cpp include/mcl/ecdsa.hpp include/mcl/ecdsa.h Makefile
emcc -o $@ src/fp.cpp src/ecdsa_c.cpp $(EMCC_OPT) -DMCL_MAX_BIT_SIZE=256 -DMCL_USE_WEB_CRYPTO_API -s DISABLE_EXCEPTION_CATCHING=1

@ -316,15 +316,18 @@ struct MapTo {
mpz_class z_;
mpz_class cofactor_;
bool isBN_;
int legendre(const Fp& x) const
int legendre(bool *pb, const Fp& x) const
{
return gmp::legendre(x.getMpz(), Fp::getOp().mp);
mpz_class xx;
x.getMpz(pb, xx);
if (!*pb) return 0;
return gmp::legendre(xx, Fp::getOp().mp);
}
int legendre(const Fp2& x) const
int legendre(bool *pb, const Fp2& x) const
{
Fp y;
Fp2::norm(y, x);
return legendre(y);
return legendre(pb, y);
}
void mulFp(Fp& x, const Fp& y) const
{
@ -347,7 +350,9 @@ struct MapTo {
bool calcBN(G& P, const F& t) const
{
F x, y, w;
bool negative = legendre(t) < 0;
bool b;
bool negative = legendre(&b, t) < 0;
if (!b) return false;
if (t.isZero()) return false;
F::sqr(w, t);
w += G::b_;
@ -365,7 +370,9 @@ struct MapTo {
G::getWeierstrass(y, x);
if (F::squareRoot(y, y)) {
if (negative) F::neg(y, y);
P.set(x, y, false);
bool b;
P.set(&b, x, y, false);
assert(b);
return true;
}
}
@ -412,7 +419,9 @@ struct MapTo {
F y;
G::getWeierstrass(y, x);
if (F::squareRoot(y, y)) {
P.set(x, y, false);
bool b;
P.set(&b, x, y, false);
assert(b);
return;
}
*x.getFp0() += Fp::one();
@ -609,7 +618,9 @@ struct GLV1 {
G1::add(tbl[3], in[0], in[1]);
tbl[3].normalize();
for (int i = 0; i < splitN; i++) {
mcl::gmp::getArray(w[i], maxUnit, u[i]);
bool b;
mcl::gmp::getArray(&b, w[i], maxUnit, u[i]);
assert(b);
bitTbl[i] = (int)mcl::gmp::getBitSize(u[i]);
maxBit = std::max(maxBit, bitTbl[i]);
}
@ -801,7 +812,9 @@ struct GLV2 {
// tbl[i].normalize();
}
for (int i = 0; i < splitN; i++) {
mcl::gmp::getArray(w[i], maxUnit, u[i]);
bool b;
mcl::gmp::getArray(&b, w[i], maxUnit, u[i]);
assert(b);
bitTbl[i] = (int)mcl::gmp::getBitSize(u[i]);
maxBit = std::max(maxBit, bitTbl[i]);
}
@ -971,7 +984,7 @@ struct Param {
glv2.init(r, z, isBLS12);
*pb = true;
}
#ifndef CYBOZU_DONT_EXCEPTION
#ifndef CYBOZU_DONT_USE_EXCEPTION
void init(const mcl::CurveParam& cp, fp::Mode mode)
{
bool b;
@ -1002,21 +1015,27 @@ namespace local {
inline void mulArrayGLV1(G1& z, const G1& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime)
{
mpz_class s;
mcl::gmp::setArray(s, y, yn);
bool b;
mcl::gmp::setArray(&b, s, y, yn);
assert(b);
if (isNegative) s = -s;
BN::param.glv1.mul(z, x, s, constTime);
}
inline void mulArrayGLV2(G2& z, const G2& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime)
{
mpz_class s;
mcl::gmp::setArray(s, y, yn);
bool b;
mcl::gmp::setArray(&b, s, y, yn);
assert(b);
if (isNegative) s = -s;
BN::param.glv2.mul(z, x, s, constTime);
}
inline void powArrayGLV2(Fp12& z, const Fp12& x, const mcl::fp::Unit *y, size_t yn, bool isNegative, bool constTime)
{
mpz_class s;
mcl::gmp::setArray(s, y, yn);
bool b;
mcl::gmp::setArray(&b, s, y, yn);
assert(b);
if (isNegative) s = -s;
BN::param.glv2.pow(z, x, s, constTime);
}
@ -1831,7 +1850,7 @@ inline void precomputedMillerLoop2(Fp12& f, const G1& P1, const mcl::Array<Fp6>&
}
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); }
#ifndef CYBOZU_DONT_EXCEPTION
#ifndef CYBOZU_DONT_USE_EXCEPTION
inline void mapToG1(G1& P, const Fp& x)
{
bool b;
@ -1943,7 +1962,7 @@ inline void init(bool *pb, const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode
*pb = true;
}
#ifndef CYBOZU_DONT_EXCEPTION
#ifndef CYBOZU_DONT_USE_EXCEPTION
inline void init(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{
bool b;
@ -1959,7 +1978,7 @@ inline void initPairing(bool *pb, const mcl::CurveParam& cp = mcl::BN254, fp::Mo
BN::init(pb, cp, mode);
}
#ifndef CYBOZU_DONT_EXCEPTION
#ifndef CYBOZU_DONT_USE_EXCEPTION
inline void initPairing(const mcl::CurveParam& cp = mcl::BN254, fp::Mode mode = fp::FP_AUTO)
{
bool b;

@ -208,12 +208,6 @@ public:
if (!*pb) return;
init(a, b, mode);
}
static inline void init(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi)
{
bool b;
init(&b, astr.c_str(), bstr.c_str(), mode);
if (!b) throw cybozu::Exception("mcl:EcT:init");
}
// verify the order
bool isValidOrder() const
{
@ -244,7 +238,7 @@ public:
if (verifyOrder_) return isValidOrder();
return true;
}
void set(const Fp& _x, const Fp& _y, bool verify, bool *pb)
void set(bool *pb, const Fp& _x, const Fp& _y, bool verify = true)
{
if (verify && !isValid(_x, _y)) {
*pb = false;
@ -262,12 +256,6 @@ public:
*pb = true;
}
}
void set(const Fp& _x, const Fp& _y, bool verify = true)
{
bool b;
set(_x, _y, verify, &b);
if (!b) throw cybozu::Exception("ec:EcT:set") << _x << _y;
}
void clear()
{
#ifdef MCL_EC_USE_AFFINE
@ -751,13 +739,6 @@ public:
P.y.save(pb, os, ioMode);
}
}
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("EcT:save");
}
template<class InputStream>
void load(bool *pb, InputStream& is, int ioMode)
{
@ -792,7 +773,8 @@ public:
isYodd = (buf[n - 1] >> 7) != 0;
buf[n - 1] &= 0x7f;
}
x.setArray(buf + adj, n);
x.setArray(pb, buf + adj, n);
if (!*pb) return;
*pb = getYfromX(y, x, isYodd);
if (!*pb) return;
} else {
@ -831,23 +813,6 @@ public:
*pb = true;
}
}
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("EcT:load");
}
friend inline std::istream& operator>>(std::istream& is, EcT& self)
{
self.load(is, fp::detectIoMode(getIoMode(), is));
return is;
}
friend inline std::ostream& operator<<(std::ostream& os, const EcT& self)
{
self.save(os, fp::detectIoMode(getIoMode(), os));
return os;
}
// deplicated
static void setCompressedExpression(bool compressedExpression = true)
{
@ -933,6 +898,46 @@ public:
{
mulArrayBase(z, x, gmp::getUnit(y), gmp::getUnitSize(y), y < 0, constTime);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
static inline void init(const std::string& astr, const std::string& bstr, int mode = ec::Jacobi)
{
bool b;
init(&b, astr.c_str(), bstr.c_str(), mode);
if (!b) throw cybozu::Exception("mcl:EcT:init");
}
void set(const Fp& _x, const Fp& _y, bool verify = true)
{
bool b;
set(&b, _x, _y, verify);
if (!b) throw cybozu::Exception("ec:EcT:set") << _x << _y;
}
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("EcT:save");
}
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("EcT:load");
}
#endif
#ifndef CYBOZU_DONT_USE_STRING
friend inline std::istream& operator>>(std::istream& is, EcT& self)
{
self.load(is, fp::detectIoMode(getIoMode(), is));
return is;
}
friend inline std::ostream& operator<<(std::ostream& os, const EcT& self)
{
self.save(os, fp::detectIoMode(getIoMode(), os));
return os;
}
#endif
};
template<class Fp> Fp EcT<Fp>::a_;

@ -123,7 +123,8 @@ public:
}
{ // set half
mpz_class half = (op_.mp + 1) / 2;
gmp::getArray(op_.half, op_.N, half);
gmp::getArray(pb, op_.half, op_.N, half);
if (!*pb) return;
}
inv(inv2_, 2);
*pb = true;
@ -158,8 +159,10 @@ public:
{
if (isMont()) return op_.sq.get(y, x);
mpz_class mx, my;
x.getMpz(mx);
bool b = op_.sq.get(my, mx);
bool b = false;
x.getMpz(&b, mx);
if (!b) return false;
b = op_.sq.get(my, mx);
if (!b) return false;
y.setMpz(&b, my);
return b;
@ -325,17 +328,11 @@ public:
uint32_t size = op_.hash(buf, static_cast<uint32_t>(sizeof(buf)), msg, static_cast<uint32_t>(msgSize));
setArrayMask(buf, size);
}
void getMpz(mpz_class& x) const
void getMpz(bool *pb, mpz_class& x) const
{
fp::Block b;
getBlock(b);
gmp::setArray(x, b.p, b.n);
}
mpz_class getMpz() const
{
mpz_class x;
getMpz(x);
return x;
gmp::setArray(pb, x, b.p, b.n);
}
void setMpz(bool *pb, const mpz_class& x)
{
@ -546,6 +543,18 @@ public:
if (!b) throw cybozu::Exception("Fp:getInt64:large value");
return v;
}
void getMpz(mpz_class& x) const
{
bool b;
getMpz(&b, x);
if (!b) throw cybozu::Exception("Fp:getMpz");
}
mpz_class getMpz() const
{
mpz_class x;
getMpz(x);
return x;
}
#endif
};

@ -57,20 +57,34 @@ public:
for (size_t i = n; i < getUnitSize(); i++) v_[i] = 0;
*pb = true;
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("fp:save") << ioMode;
if (!b) throw cybozu::Exception("FpDblT:save") << ioMode;
}
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("fp:load") << ioMode;
if (!b) throw cybozu::Exception("FpDblT:load") << ioMode;
}
void getMpz(mpz_class& x) const
{
bool b;
getMpz(&b, x);
if (!b) throw cybozu::Exception("FpDblT:getMpz");
}
mpz_class getMpz() const
{
mpz_class x;
getMpz(x);
return x;
}
#endif
void clear()
{
const size_t n = getUnitSize();
@ -99,15 +113,9 @@ public:
memcpy(v_, gmp::getUnit(x), xn * sizeof(Unit));
memset(v_ + xn, 0, (N2 - xn) * sizeof(Unit));
}
void getMpz(mpz_class& x) const
{
gmp::setArray(x, v_, Fp::op_.N * 2);
}
mpz_class getMpz() const
void getMpz(bool *pb, mpz_class& x) const
{
mpz_class x;
getMpz(x);
return x;
gmp::setArray(pb, x, v_, Fp::op_.N * 2);
}
static void add(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_add(z.v_, x.v_, y.v_, Fp::op_.p); }
static void sub(FpDblT& z, const FpDblT& x, const FpDblT& y) { Fp::op_.fpDbl_sub(z.v_, x.v_, y.v_, Fp::op_.p); }
@ -205,12 +213,13 @@ public:
Fp::mul(z.b, x.b, y);
}
template<class S>
void setArray(const S *buf, size_t n)
void setArray(bool *pb, const S *buf, size_t n)
{
assert((n & 1) == 0);
n /= 2;
a.setArray(buf, n);
b.setArray(buf + n, n);
a.setArray(pb, buf, n);
if (!*pb) return;
b.setArray(pb, buf + n, n);
}
template<class InputStream>
void load(bool *pb, InputStream& is, int ioMode)
@ -234,30 +243,6 @@ public:
}
b.save(pb, os, ioMode);
}
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("Fp2T:load");
}
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("Fp2T:save");
}
friend std::istream& operator>>(std::istream& is, Fp2T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
return is;
}
friend std::ostream& operator<<(std::ostream& os, const Fp2T& self)
{
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
bool isZero() const { return a.isZero() && b.isZero(); }
bool isOne() const { return a.isOne() && b.isZero(); }
bool operator==(const Fp2T& rhs) const { return a == rhs.a && b == rhs.b; }
@ -391,6 +376,41 @@ public:
g3[i] = g[i] * g2[i];
}
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
bool b;
load(&b, is, ioMode);
if (!b) throw cybozu::Exception("Fp2T:load");
}
template<class OutputStream>
void save(OutputStream& os, int ioMode = IoSerialize) const
{
bool b;
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("Fp2T:save");
}
template<class S>
void setArray(const S *buf, size_t n)
{
bool b;
setArray(&b, buf, n);
if (!b) throw cybozu::Exception("Fp2T:setArray");
}
#endif
#ifndef CYBOZU_DONT_USE_STRING
friend std::istream& operator>>(std::istream& is, Fp2T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
return is;
}
friend std::ostream& operator<<(std::ostream& os, const Fp2T& self)
{
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
#endif
private:
/*
default Fp2T operator
@ -758,6 +778,7 @@ struct Fp6T : public fp::Serializable<Fp6T<_Fp>,
}
c.save(pb, os, ioMode);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
@ -772,6 +793,8 @@ struct Fp6T : public fp::Serializable<Fp6T<_Fp>,
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("Fp6T:save");
}
#endif
#ifndef CYBOZU_DONT_USE_STRING
friend std::istream& operator>>(std::istream& is, Fp6T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
@ -782,6 +805,7 @@ struct Fp6T : public fp::Serializable<Fp6T<_Fp>,
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
#endif
static void add(Fp6T& z, const Fp6T& x, const Fp6T& y)
{
Fp2::add(z.a, x.a, y.a);
@ -1173,6 +1197,7 @@ struct Fp12T : public fp::Serializable<Fp12T<Fp>,
}
b.save(pb, os, ioMode);
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class InputStream>
void load(InputStream& is, int ioMode = IoSerialize)
{
@ -1187,6 +1212,8 @@ struct Fp12T : public fp::Serializable<Fp12T<Fp>,
save(&b, os, ioMode);
if (!b) throw cybozu::Exception("Fp12T:save");
}
#endif
#ifndef CYBOZU_DONT_USE_STRING
friend std::istream& operator>>(std::istream& is, Fp12T& self)
{
self.load(is, fp::detectIoMode(Fp::BaseFp::getIoMode(), is));
@ -1197,6 +1224,7 @@ struct Fp12T : public fp::Serializable<Fp12T<Fp>,
self.save(os, fp::detectIoMode(Fp::BaseFp::getIoMode(), os));
return os;
}
#endif
};
/*

@ -670,13 +670,11 @@ public:
/*
solve x^2 = a mod p
*/
bool get(bool *pb, mpz_class& x, const mpz_class& a) const
bool get(mpz_class& x, const mpz_class& a) const
{
if (!isPrime) {
*pb = false;
return false;
}
*pb = true;
if (a == 0) {
x = 0;
return true;
@ -721,14 +719,24 @@ public:
x = 0;
return true;
}
if (gmp::legendre(a.getMpz(), p) < 0) return false;
{
bool b;
mpz_class aa;
a.getMpz(&b, aa);
assert(b);
if (gmp::legendre(aa, p) < 0) return false;
}
if (r == 1) {
// (p + 1) / 4 = (q + 1) / 2
Fp::pow(x, a, q_add_1_div_2);
return true;
}
Fp c, d;
c.setMpz(s);
{
bool b;
c.setMpz(&b, s);
assert(b);
}
int e = r;
Fp::pow(d, a, q);
Fp::pow(x, a, q_add_1_div_2); // destroy a if &x == &a
@ -761,13 +769,6 @@ public:
set(&b, _p);
if (!b) throw cybozu::Exception("gmp:SquareRoot:set");
}
bool get(mpz_class& x, const mpz_class& a) const
{
bool b;
bool ret = get(&b, x, a);
if (!b) throw cybozu::Exception("gmp:SquareRoot:get:not prime");
return ret;
}
#endif
};

@ -6,7 +6,7 @@
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#include <vector>
#include <mcl/array.hpp>
namespace mcl {
@ -15,7 +15,7 @@ namespace mcl {
@retval 0 if succeed else -1
*/
template<class G, class F>
void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k, bool *pb)
void LagrangeInterpolation(bool *pb, G& out, const F *S, const G *vec, size_t k)
{
/*
delta_{i,S}(0) = prod_{j != i} S[j] / (S[j] - S[i]) = a / b
@ -25,7 +25,9 @@ void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k, bool *pb)
*pb = false;
return;
}
std::vector<F> delta(k);
mcl::Array<F> delta;
*pb = delta.resize(k);
if (!*pb) return;
F a = S[0];
for (size_t i = 1; i < k; i++) {
a *= S[i];
@ -62,19 +64,12 @@ void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k, bool *pb)
*pb = true;
}
template<class G, class F>
void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k)
{
bool b;
LagrangeInterpolation(out, S, vec, k, &b);
if (!b) throw cybozu::Exception("LagrangeInterpolation");
}
/*
out = f(x) = c[0] + c[1] * x + c[2] * x^2 + ... + c[cSize - 1] * x^(cSize - 1)
@retval 0 if succeed else -1
*/
template<class G, class T>
void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x, bool *pb)
void evaluatePolynomial(bool *pb, G& out, const G *c, size_t cSize, const T& x)
{
if (cSize < 2) {
*pb = false;
@ -89,12 +84,22 @@ void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x, bool *pb)
*pb = true;
}
#ifndef CYBOZU_DONT_USE_EXCEPTION
template<class G, class F>
void LagrangeInterpolation(G& out, const F *S, const G *vec, size_t k)
{
bool b;
LagrangeInterpolation(&b, out, S, vec, k);
if (!b) throw cybozu::Exception("LagrangeInterpolation");
}
template<class G, class T>
void evaluatePolynomial(G& out, const G *c, size_t cSize, const T& x)
{
bool b;
evaluatePolynomial(out, c, cSize, x, &b);
evaluatePolynomial(&b, out, c, cSize, x);
if (!b) throw cybozu::Exception("evaluatePolynomial");
}
#endif
} // mcl

@ -6,7 +6,19 @@
@license modified new BSD license
http://opensource.org/licenses/BSD-3-Clause
*/
#ifdef MCL_USE_WEB_CRYPTO_API
#ifdef MCL_DONT_USE_RANDOM
#include <stdlib.h>
namespace mcl {
struct NoRandomGenerator {
void read(void *, size_t)
{
exit(1);
}
};
} // mcl
#elif defined(MCL_USE_WEB_CRYPTO_API)
#include <emscripten.h>
namespace mcl {
@ -90,7 +102,9 @@ public:
bool isZero() const { return self_ == 0 && readFunc_ == 0; }
static RandGen& get()
{
#ifdef MCL_USE_WEB_CRYPTO_API
#ifdef MCL_DONT_USE_RANDOM
static mcl::NoRandomGenerator rg;
#elif defined(MCL_USE_WEB_CRYPTO_API)
static mcl::RandomGeneratorJS rg;
#else
static cybozu::RandomGenerator rg;

@ -699,8 +699,8 @@ class FixedBuffer {
enum {
N = (BitLen + sizeof(T) * 8 - 1) / (sizeof(T) * 8)
};
T v_[N];
size_t size_;
T v_[N];
public:
typedef T Unit;
FixedBuffer()
@ -851,21 +851,27 @@ private:
static void uadd(VintT& z, const Buffer& x, size_t xn, const Buffer& y, size_t yn)
{
size_t zn = std::max(xn, yn) + 1;
z.buf_.alloc(zn);
bool b;
z.buf_.alloc(&b, zn);
assert(b); (void)b;
z.buf_[zn - 1] = vint::addNM(&z.buf_[0], &x[0], xn, &y[0], yn);
z.trim(zn);
}
static void uadd1(VintT& z, const Buffer& x, size_t xn, Unit y)
{
size_t zn = xn + 1;
z.buf_.alloc(zn);
bool b;
z.buf_.alloc(&b, zn);
assert(b); (void)b;
z.buf_[zn - 1] = vint::addu1(&z.buf_[0], &x[0], xn, y);
z.trim(zn);
}
static void usub1(VintT& z, const Buffer& x, size_t xn, Unit y)
{
size_t zn = xn;
z.buf_.alloc(zn);
bool b;
z.buf_.alloc(&b, zn);
assert(b); (void)b;
Unit c = vint::subu1(&z.buf_[0], &x[0], xn, y);
(void)c;
assert(!c);
@ -874,7 +880,9 @@ private:
static void usub(VintT& z, const Buffer& x, size_t xn, const Buffer& y, size_t yn)
{
assert(xn >= yn);
z.buf_.alloc(xn);
bool b;
z.buf_.alloc(&b, xn);
assert(b); (void)b;
Unit c = vint::subN(&z.buf_[0], &x[0], &y[0], yn);
if (xn > yn) {
c = vint::subu1(&z.buf_[yn], &x[yn], xn - yn, c);
@ -946,10 +954,13 @@ private:
return;
}
size_t qn = xn - yn + 1;
bool b;
if (q) {
q->buf_.alloc(qn);
q->buf_.alloc(&b, qn);
assert(b); (void)b;
}
r.buf_.alloc(yn);
r.buf_.alloc(&b, yn);
assert(b); (void)b;
vint::divNM(q ? &q->buf_[0] : 0, qn, &r.buf_[0], &x[0], xn, &y[0], yn);
if (q) {
q->trim(qn);
@ -1006,7 +1017,9 @@ public:
{
assert(x != invalidVar);
isNeg_ = x < 0;
buf_.alloc(1);
bool b;
buf_.alloc(&b, 1);
assert(b); (void)b;
buf_[0] = std::abs(x);
size_ = 1;
return *this;
@ -1014,7 +1027,9 @@ public:
VintT& operator=(Unit x)
{
isNeg_ = false;
buf_.alloc(1);
bool b;
buf_.alloc(&b, 1);
assert(b); (void)b;
buf_[0] = x;
size_ = 1;
return *this;
@ -1059,7 +1074,7 @@ public:
@note assume little endian system
*/
template<class S>
void setArray(const S *x, size_t size)
void setArray(bool *pb, const S *x, size_t size)
{
isNeg_ = false;
if (size == 0) {
@ -1067,7 +1082,8 @@ public:
return;
}
size_t unitSize = (sizeof(S) * size + sizeof(Unit) - 1) / sizeof(Unit);
buf_.alloc(unitSize);
buf_.alloc(pb, unitSize);
if (!*pb) return;
buf_[unitSize - 1] = 0;
memcpy(&buf_[0], x, sizeof(S) * size);
trim(unitSize);
@ -1156,7 +1172,9 @@ public:
size_t q = i / unitBitSize;
size_t r = i % unitBitSize;
assert(q <= size());
buf_.alloc(q + 1);
bool b;
buf_.alloc(&b, q + 1);
assert(b); (void)b;
Unit mask = Unit(1) << r;
if (v) {
buf_[q] |= mask;
@ -1235,7 +1253,9 @@ public:
const size_t xn = x.size();
const size_t yn = y.size();
size_t zn = xn + yn;
z.buf_.alloc(zn);
bool b;
z.buf_.alloc(&b, zn);
assert(b); (void)b;
vint::mulNM(&z.buf_[0], &x.buf_[0], xn, &y.buf_[0], yn);
z.isNeg_ = x.isNeg_ ^ y.isNeg_;
z.trim(zn);
@ -1256,7 +1276,9 @@ public:
{
size_t xn = x.size();
size_t zn = xn + 1;
z.buf_.alloc(zn);
bool b;
z.buf_.alloc(&b, zn);
assert(b); (void)b;
z.buf_[zn - 1] = vint::mulu1(&z.buf_[0], &x.buf_[0], xn, y);
z.isNeg_ = x.isNeg_;
z.trim(zn);
@ -1303,7 +1325,9 @@ public:
int r;
if (q) {
q->isNeg_ = xNeg ^ yNeg;
q->buf_.alloc(xn);
bool b;
q->buf_.alloc(&b, xn);
assert(b); (void)b;
r = vint::divu1(&q->buf_[0], &x.buf_[0], xn, absY);
q->trim(xn);
} else {
@ -1348,7 +1372,11 @@ public:
{
assert(!x.isNeg_);
size_t xn = x.size();
if (q) q->buf_.alloc(xn);
if (q) {
bool b;
q->buf_.alloc(&b, xn);
assert(b); (void)b;
}
Unit r = vint::divu1(q ? &q->buf_[0] : 0, &x.buf_[0], xn, y);
if (q) {
q->trim(xn);
@ -1385,7 +1413,8 @@ public:
size_t n = fp::local::loadWord(buf, sizeof(buf), is);
if (n == 0) return;
const size_t maxN = 384 / (sizeof(MCL_SIZEOF_UNIT) * 8);
buf_.alloc(maxN);
buf_.alloc(pb, maxN);
if (!*pb) return;
isNeg_ = false;
n = fp::strToArray(&isNeg_, &buf_[0], maxN, buf, n, ioMode);
if (n == 0) return;
@ -1397,7 +1426,9 @@ public:
{
size_t xn = x.size();
size_t yn = xn + (shiftBit + unitBitSize - 1) / unitBitSize;
y.buf_.alloc(yn);
bool b;
y.buf_.alloc(&b, yn);
assert(b); (void)b;
vint::shlN(&y.buf_[0], &x.buf_[0], xn, shiftBit);
y.isNeg_ = x.isNeg_;
y.trim(yn);
@ -1411,7 +1442,9 @@ public:
return;
}
size_t yn = xn - shiftBit / unitBitSize;
y.buf_.alloc(yn);
bool b;
y.buf_.alloc(&b, yn);
assert(b); (void)b;
vint::shrN(&y.buf_[0], &x.buf_[0], xn, shiftBit);
y.isNeg_ = x.isNeg_;
y.trim(yn);
@ -1443,7 +1476,9 @@ public:
size_t xn = px->size();
size_t yn = py->size();
assert(xn >= yn);
z.buf_.alloc(xn);
bool b;
z.buf_.alloc(&b, xn);
assert(b); (void)b;
for (size_t i = 0; i < yn; i++) {
z.buf_[i] = x.buf_[i] | y.buf_[i];
}
@ -1459,7 +1494,9 @@ public:
}
size_t yn = py->size();
assert(px->size() >= yn);
z.buf_.alloc(yn);
bool b;
z.buf_.alloc(&b, yn);
assert(b); (void)b;
for (size_t i = 0; i < yn; i++) {
z.buf_[i] = x.buf_[i] & y.buf_[i];
}
@ -1474,7 +1511,9 @@ public:
static void andBitu1(VintT& z, const VintT& x, Unit y)
{
assert(!x.isNeg_);
z.buf_.alloc(1);
bool b;
z.buf_.alloc(&b, 1);
assert(b); (void)b;
z.buf_[0] = x.buf_[0] & y;
z.size_ = 1;
z.isNeg_ = false;
@ -1743,6 +1782,13 @@ public:
if (!b) throw cybozu::Exception("Vint:isPrime");
return ret;
}
template<class S>
void setArray(const S *x, size_t size)
{
bool b;
setArray(&b, x, size);
if (!b) throw cybozu::Exception("Vint:setArray");
}
#endif
VintT& operator++() { adds1(*this, *this, 1); return *this; }
VintT& operator--() { subs1(*this, *this, 1); return *this; }

@ -452,37 +452,37 @@ void mclBn_precomputedMillerLoop2(mclBnGT *f, const mclBnG1 *P1, const uint64_t
int mclBn_FrLagrangeInterpolation(mclBnFr *out, const mclBnFr *xVec, const mclBnFr *yVec, mclSize k)
{
bool b;
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b);
mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
return b ? 0 : -1;
}
int mclBn_G1LagrangeInterpolation(mclBnG1 *out, const mclBnFr *xVec, const mclBnG1 *yVec, mclSize k)
{
bool b;
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b);
mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
return b ? 0 : -1;
}
int mclBn_G2LagrangeInterpolation(mclBnG2 *out, const mclBnFr *xVec, const mclBnG2 *yVec, mclSize k)
{
bool b;
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k, &b);
mcl::LagrangeInterpolation(&b, *cast(out), cast(xVec), cast(yVec), k);
return b ? 0 : -1;
}
int mclBn_FrEvaluatePolynomial(mclBnFr *out, const mclBnFr *cVec, mclSize cSize, const mclBnFr *x)
{
bool b;
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b);
mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
return b ? 0 : -1;
}
int mclBn_G1EvaluatePolynomial(mclBnG1 *out, const mclBnG1 *cVec, mclSize cSize, const mclBnFr *x)
{
bool b;
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b);
mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
return b ? 0 : -1;
}
int mclBn_G2EvaluatePolynomial(mclBnG2 *out, const mclBnG2 *cVec, mclSize cSize, const mclBnFr *x)
{
bool b;
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x), &b);
mcl::evaluatePolynomial(&b, *cast(out), cast(cVec), cSize, *cast(x));
return b ? 0 : -1;
}

@ -176,16 +176,16 @@ static inline void set_mpz_t(mpz_t& z, const Unit* p, int n)
static inline void fp_invOpC(Unit *y, const Unit *x, const Op& op)
{
const int N = (int)op.N;
bool b;
bool b = false;
#ifdef MCL_USE_VINT
Vint vx, vy, vp;
vx.setArray(&b, x, N);
assert(b);
assert(b); (void)b;
vp.setArray(&b, op.p, N);
assert(b);
assert(b); (void)b;
Vint::invMod(vy, vx, vp);
vy.getArray(&b, y, N);
assert(b);
assert(b); (void)b;
#else
mpz_class my;
mpz_t mx, mp;

Loading…
Cancel
Save