setErrFile is removed

dev
MITSUNARI Shigeo 7 years ago
parent f5d9cbf704
commit 299a7053e9
  1. 2
      ffi/cs/bn256.cs
  2. 3
      ffi/cs/bn256_test.cs
  3. 9
      include/mcl/bn.h
  4. 1
      sample/pairing_c.c
  5. 89
      src/bn_c_impl.hpp
  6. 43
      src/she_c_impl.hpp
  7. 8
      test/bn_c_test.hpp

@ -4,8 +4,6 @@ using System.Runtime.InteropServices;
namespace mcl {
class BN256 {
[DllImport("mclBn256.dll")]
public static extern int mclBn_setErrFile([In][MarshalAs(UnmanagedType.LPStr)] string name);
[DllImport("mclBn256.dll")]
public static extern int mclBn_init(int curve, int maxUnitSize);
[DllImport("mclBn256.dll")]

@ -15,9 +15,6 @@ namespace mcl {
try {
assert("64bit system", System.Environment.Is64BitProcess);
init();
int ret;
ret = mclBn_setErrFile("mclbn_test.txt");
assert("mclbn_setErrFile", ret == 0);
TestFr();
TestG1();
TestG2();

@ -76,15 +76,6 @@ typedef struct {
#endif
/*
set errlog file name
use stderr if name == "stderr"
close if name == ""
return 0 if success
@note not threadsafe
*/
MCLBN_DLL_API int mclBn_setErrFile(const char *name);
#include <mcl/curve_type.h>
// for backword compatibility
enum {

@ -42,7 +42,6 @@ int main()
mclBnGT_pow(&e1, &e, &b);
mclBn_pairing(&e2, &P, &bQ);
ASSERT(mclBnGT_isEqual(&e1, &e2));
ASSERT(mclBn_setErrFile("") == 0);
if (g_err) {
printf("err %d\n", g_err);
return 1;

@ -13,8 +13,6 @@ using namespace mcl::bn512;
#endif
#include <mcl/lagrange.hpp>
static FILE *g_fp = NULL;
static Fr *cast(mclBnFr *p) { return reinterpret_cast<Fr*>(p); }
static const Fr *cast(const mclBnFr *p) { return reinterpret_cast<const Fr*>(p); }
@ -30,25 +28,14 @@ static const Fp12 *cast(const mclBnGT *p) { return reinterpret_cast<const Fp12*>
static Fp6 *cast(uint64_t *p) { return reinterpret_cast<Fp6*>(p); }
static const Fp6 *cast(const uint64_t *p) { return reinterpret_cast<const Fp6*>(p); }
static int closeErrFile()
{
if (g_fp == NULL || g_fp == stderr) {
return 0;
}
int ret = fclose(g_fp);
g_fp = NULL;
return ret;
}
template<class T>
mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode, const char *msg)
mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode)
try
{
std::string str;
cast(x)->getStr(str, ioMode);
mclSize terminate = (ioMode == 10 || ioMode == 16) ? 1 : 0;
if (str.size() + terminate > maxBufSize) {
if (g_fp) fprintf(g_fp, "%s:getStr:small maxBufSize %d %d %d\n", msg, (int)maxBufSize, (int)str.size(), (int)terminate);
return 0;
}
memcpy(buf, str.c_str(), str.size());
@ -57,73 +44,48 @@ mclSize getStr(void *buf, mclSize maxBufSize, const T *x, int ioMode, const char
}
return str.size();
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
return 0;
}
template<class T>
mclSize serialize(void *buf, mclSize maxBufSize, const T *x, const char *msg)
mclSize serialize(void *buf, mclSize maxBufSize, const T *x)
try
{
return (mclSize)cast(x)->serialize(buf, maxBufSize);
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
return 0;
}
template<class T>
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode, const char *msg)
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
try
{
cast(x)->setStr(std::string(buf, bufSize), ioMode);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
return -1;
}
template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize, const char *msg)
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
try
{
const size_t n = cast(x)->deserialize(buf, bufSize);
return (mclSize)n;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
return 0;
}
int mclBn_setErrFile(const char *name)
{
int ret = closeErrFile();
if (name == NULL || *name == '\0') {
return ret;
}
if (ret) return ret;
if (strcmp(name, "stderr") == 0) {
g_fp = stderr;
return 0;
}
#ifdef _MSC_VER
return fopen_s(&g_fp, name, "wb");
#else
g_fp = fopen(name, "wb");
return g_fp ? 0 : errno;
#endif
}
int mclBn_init(int curve, int maxUnitSize)
try
{
if (maxUnitSize != MCLBN_FP_UNIT_SIZE) {
fprintf(stderr, "mclBn_init:maxUnitSize is mismatch %d %d\n", maxUnitSize, MCLBN_FP_UNIT_SIZE);
return -1;
}
const mcl::CurveParam& cp = mcl::getCurveParam(curve);
initPairing(cp);
return 0;
} catch (std::exception& e) {
fprintf(stderr, "%s\n", e.what());
return -1;
}
@ -182,7 +144,7 @@ void mclBnFr_setInt32(mclBnFr *y, int x)
int mclBnFr_setStr(mclBnFr *x, const char *buf, mclSize bufSize, int ioMode)
{
return setStr(x, buf, bufSize, ioMode, "mclBnFr_setStr");
return setStr(x, buf, bufSize, ioMode);
}
int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
try
@ -190,12 +152,11 @@ int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize)
cast(x)->setArrayMask((const char *)buf, bufSize);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "setArrayMask %s\n", e.what());
return -1;
}
mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize bufSize)
{
return deserialize(x, buf, bufSize, "mclBnFr_deserialize");
return deserialize(x, buf, bufSize);
}
// return 1 if true
int mclBnFr_isValid(const mclBnFr *x)
@ -221,7 +182,6 @@ int mclBnFr_setByCSPRNG(mclBnFr *x)
cast(x)->setByCSPRNG();
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBnFr_setByCSPRNG %s\n", e.what());
return -1;
}
@ -232,17 +192,16 @@ int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize)
cast(x)->setHashOf(buf, bufSize);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBnFr_setHashOf %s\n", e.what());
return -1;
}
mclSize mclBnFr_getStr(char *buf, mclSize maxBufSize, const mclBnFr *x, int ioMode)
{
return getStr(buf, maxBufSize, x, ioMode, "mclBnFr_getStr");
return getStr(buf, maxBufSize, x, ioMode);
}
mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mclBnFr *x)
{
return serialize(buf, maxBufSize, x, "mclBnFr_serialize");
return serialize(buf, maxBufSize, x);
}
void mclBnFr_neg(mclBnFr *y, const mclBnFr *x)
@ -283,11 +242,11 @@ void mclBnG1_clear(mclBnG1 *x)
int mclBnG1_setStr(mclBnG1 *x, const char *buf, mclSize bufSize, int ioMode)
{
return setStr(x, buf, bufSize, ioMode, "mclBnG1_setStr");
return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize)
{
return deserialize(x, buf, bufSize, "mclBnG1_deserialize");
return deserialize(x, buf, bufSize);
}
// return 1 if true
@ -310,18 +269,17 @@ int mclBnG1_hashAndMapTo(mclBnG1 *x, const void *buf, mclSize bufSize)
hashAndMapToG1(*cast(x), buf, bufSize);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBnG1_hashAndMapTo %s\n", e.what());
return 1;
}
mclSize mclBnG1_getStr(char *buf, mclSize maxBufSize, const mclBnG1 *x, int ioMode)
{
return getStr(buf, maxBufSize, x, ioMode, "mclBnG1_getStr");
return getStr(buf, maxBufSize, x, ioMode);
}
mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x)
{
return serialize(buf, maxBufSize, x, "mclBnG1_serialize");
return serialize(buf, maxBufSize, x);
}
void mclBnG1_neg(mclBnG1 *y, const mclBnG1 *x)
@ -362,11 +320,11 @@ void mclBnG2_clear(mclBnG2 *x)
int mclBnG2_setStr(mclBnG2 *x, const char *buf, mclSize bufSize, int ioMode)
{
return setStr(x, buf, bufSize, ioMode, "mclBnG2_setStr");
return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize)
{
return deserialize(x, buf, bufSize, "mclBnG2_deserialize");
return deserialize(x, buf, bufSize);
}
// return 1 if true
@ -389,17 +347,16 @@ int mclBnG2_hashAndMapTo(mclBnG2 *x, const void *buf, mclSize bufSize)
hashAndMapToG2(*cast(x), buf, bufSize);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBnG2_hashAndMapTo %s\n", e.what());
return 1;
}
mclSize mclBnG2_getStr(char *buf, mclSize maxBufSize, const mclBnG2 *x, int ioMode)
{
return getStr(buf, maxBufSize, x, ioMode, "mclBnG2_getStr");
return getStr(buf, maxBufSize, x, ioMode);
}
mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x)
{
return serialize(buf, maxBufSize, x, "mclBnG2_serialize");
return serialize(buf, maxBufSize, x);
}
void mclBnG2_neg(mclBnG2 *y, const mclBnG2 *x)
@ -450,11 +407,11 @@ void mclBnGT_setInt32(mclBnGT *y, int x)
int mclBnGT_setStr(mclBnGT *x, const char *buf, mclSize bufSize, int ioMode)
{
return setStr(x, buf, bufSize, ioMode, "mclBnGT_setStr");
return setStr(x, buf, bufSize, ioMode);
}
mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize)
{
return deserialize(x, buf, bufSize, "mclBnGT_deserialize");
return deserialize(x, buf, bufSize);
}
// return 1 if true
@ -473,12 +430,12 @@ int mclBnGT_isOne(const mclBnGT *x)
mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnGT *x, int ioMode)
{
return getStr(buf, maxBufSize, x, ioMode, "mclBnGT_getStr");
return getStr(buf, maxBufSize, x, ioMode);
}
mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x)
{
return serialize(buf, maxBufSize, x, "mclBnGT_serialize");
return serialize(buf, maxBufSize, x);
}
void mclBnGT_neg(mclBnGT *y, const mclBnGT *x)
@ -557,7 +514,6 @@ int mclBn_FrLagrangeInterpolation(mclBnFr *out, const mclBnFr *xVec, const mclBn
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_FrLagrangeInterpolation %s\n", e.what());
return -1;
}
int mclBn_G1LagrangeInterpolation(mclBnG1 *out, const mclBnFr *xVec, const mclBnG1 *yVec, mclSize k)
@ -566,7 +522,6 @@ int mclBn_G1LagrangeInterpolation(mclBnG1 *out, const mclBnFr *xVec, const mclBn
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_G1LagrangeInterpolation %s\n", e.what());
return -1;
}
int mclBn_G2LagrangeInterpolation(mclBnG2 *out, const mclBnFr *xVec, const mclBnG2 *yVec, mclSize k)
@ -575,7 +530,6 @@ int mclBn_G2LagrangeInterpolation(mclBnG2 *out, const mclBnFr *xVec, const mclBn
mcl::LagrangeInterpolation(*cast(out), cast(xVec), cast(yVec), k);
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_G2LagrangeInterpolation %s\n", e.what());
return -1;
}
int mclBn_FrEvaluatePolynomial(mclBnFr *out, const mclBnFr *cVec, mclSize cSize, const mclBnFr *x)
@ -584,7 +538,6 @@ int mclBn_FrEvaluatePolynomial(mclBnFr *out, const mclBnFr *cVec, mclSize cSize,
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x));
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_FrEvaluatePolynomial %s\n", e.what());
return -1;
}
int mclBn_G1EvaluatePolynomial(mclBnG1 *out, const mclBnG1 *cVec, mclSize cSize, const mclBnFr *x)
@ -593,7 +546,6 @@ int mclBn_G1EvaluatePolynomial(mclBnG1 *out, const mclBnG1 *cVec, mclSize cSize,
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x));
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_G1EvaluatePolynomial %s\n", e.what());
return -1;
}
int mclBn_G2EvaluatePolynomial(mclBnG2 *out, const mclBnG2 *cVec, mclSize cSize, const mclBnFr *x)
@ -602,7 +554,6 @@ int mclBn_G2EvaluatePolynomial(mclBnG2 *out, const mclBnG2 *cVec, mclSize cSize,
mcl::evaluatePolynomial(*cast(out), cast(cVec), cSize, *cast(x));
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "mclBn_G2EvaluatePolynomial %s\n", e.what());
return -1;
}

@ -14,13 +14,6 @@
using namespace mcl::she;
using namespace mcl::bn_current;
#if defined(CYBOZU_CPP_VERSION) && CYBOZU_CPP_VERSION >= CYBOZU_CPP_VERSION_CPP11
#include <mutex>
#define USE_STD_MUTEX
#else
#include <cybozu/mutex.hpp>
#endif
static SecretKey *cast(sheSecretKey *p) { return reinterpret_cast<SecretKey*>(p); }
static const SecretKey *cast(const sheSecretKey *p) { return reinterpret_cast<const SecretKey*>(p); }
@ -52,19 +45,8 @@ int sheInit(int curve, int maxUnitSize)
try
{
if (maxUnitSize != MCLBN_FP_UNIT_SIZE) {
fprintf(stderr, "err sheInit:maxUnitSize is mismatch %d %d\n", maxUnitSize, MCLBN_FP_UNIT_SIZE);
return -1;
return -2;
}
#ifdef USE_STD_MUTEX
static std::mutex m;
std::lock_guard<std::mutex> lock(m);
#else
static cybozu::Mutex m;
cybozu::AutoLock lock(m);
#endif
static int g_curve = -1;
if (g_curve == curve) return 0;
mcl::CurveParam cp;
switch (curve) {
case MCL_BN254:
@ -86,24 +68,12 @@ int sheInit(int curve, int maxUnitSize)
cp = mcl::BLS12_381;
break;
default:
fprintf(stderr, "err bad curve %d\n", curve);
return -1;
}
SHE::init(cp);
g_curve = curve;
return 0;
} catch (std::exception& e) {
fprintf(stderr, "err sheInit %s\n", e.what());
return -1;
}
template<class T>
mclSize serialize(void *buf, mclSize maxBufSize, const T *x)
try
{
return cast(x)->serialize(buf, maxBufSize);
} catch (std::exception&) {
return 0;
return -1;
}
mclSize sheSecretKeySerialize(void *buf, mclSize maxBufSize, const sheSecretKey *sec)
@ -146,15 +116,6 @@ mclSize sheZkpBinEqSerialize(void *buf, mclSize maxBufSize, const sheZkpBinEq *z
return serialize(buf, maxBufSize, zkp);
}
template<class T>
mclSize deserialize(T *x, const void *buf, mclSize bufSize)
try
{
return cast(x)->deserialize(buf, bufSize);
} catch (std::exception&) {
return 0;
}
mclSize sheSecretKeyDeserialize(sheSecretKey* sec, const void *buf, mclSize bufSize)
{
return deserialize(sec, buf, bufSize);

@ -24,9 +24,6 @@ CYBOZU_TEST_AUTO(init)
CYBOZU_TEST_EQUAL(sizeof(mclBnG2), sizeof(G2));
CYBOZU_TEST_EQUAL(sizeof(mclBnGT), sizeof(Fp12));
// ret = mclBn_setErrFile("stderr");
// CYBOZU_TEST_EQUAL(ret, 0);
#if MCLBN_FP_UNIT_SIZE == 4
printf("test BN254 %d\n", MCLBN_FP_UNIT_SIZE);
ret = mclBn_init(MCL_BN254, MCLBN_FP_UNIT_SIZE);
@ -436,8 +433,3 @@ CYBOZU_TEST_AUTO(badG2)
}
#endif
CYBOZU_TEST_AUTO(end)
{
int ret = mclBn_setErrFile("bn_if.log");
CYBOZU_TEST_EQUAL(ret, 0);
}

Loading…
Cancel
Save