aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/mcl/bn_if.h64
-rw-r--r--src/bn_if.cpp244
-rw-r--r--test/bn_if256_test.cpp90
3 files changed, 225 insertions, 173 deletions
diff --git a/include/mcl/bn_if.h b/include/mcl/bn_if.h
index 10775c1..e8a946c 100644
--- a/include/mcl/bn_if.h
+++ b/include/mcl/bn_if.h
@@ -66,10 +66,25 @@ typedef struct BN_GT BN_GT;
use stderr if name == "stderr"
close if name == ""
return 0 if success
+ @note not threadsafe
*/
BN_DLL_API int BN_setErrFile(const char *name);
-BN_DLL_API int BN_init(void);
+enum {
+ BN_curveFp254BNb = 0,
+ BN_curveFp382_1 = 1,
+ BN_curveFp382_2 = 2
+};
+
+/*
+ init library
+ @param curve [in] type of bn curve
+ @param maxUnitSize [in] 4 or 6
+ curve = BN_CurveFp254BNb is allowed if maxUnitSize = 4
+ curve = BN_CurveFp254BNb/BN_CurveFp382_1/BN_CurveFp382_2 are allowed if maxUnitSize = 6
+ @note not threadsafe
+*/
+BN_DLL_API int BN_init(int curve, int maxUnitSize);
////////////////////////////////////////////////
// set zero
@@ -78,10 +93,11 @@ BN_DLL_API void BN_Fr_clear(BN_Fr *x);
// set x to y
BN_DLL_API void BN_Fr_setInt(BN_Fr *y, int x);
-BN_DLL_API void BN_Fr_copy(BN_Fr *y, const BN_Fr *x);
-
// return 0 if success
-BN_DLL_API int BN_Fr_setStr(BN_Fr *x, const char *s);
+BN_DLL_API int BN_Fr_setDecStr(BN_Fr *x, const char *s);
+BN_DLL_API int BN_Fr_setHexStr(BN_Fr *x, const char *s);
+// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
+BN_DLL_API int BN_Fr_setLittleEndian(BN_Fr *x, const void *buf, size_t bufSize);
// return 1 if true and 0 otherwise
BN_DLL_API int BN_Fr_isValid(const BN_Fr *x);
@@ -89,13 +105,16 @@ BN_DLL_API int BN_Fr_isEqual(const BN_Fr *x, const BN_Fr *y);
BN_DLL_API int BN_Fr_isZero(const BN_Fr *x);
BN_DLL_API int BN_Fr_isOne(const BN_Fr *x);
-BN_DLL_API void BN_Fr_setRand(BN_Fr *x);
+BN_DLL_API void BN_Fr_setByCSPRNG(BN_Fr *x);
// hash(s) and set x
BN_DLL_API void BN_hashToFr(BN_Fr *x, const void *buf, size_t bufSize);
-// return 0 if success
-BN_DLL_API int BN_Fr_getStr(char *buf, int maxBufSize, const BN_Fr *x);
+// return strlen(buf) if sucess else 0
+BN_DLL_API size_t BN_Fr_getDecStr(char *buf, size_t maxBufSize, const BN_Fr *x);
+BN_DLL_API size_t BN_Fr_getHexStr(char *buf, size_t maxBufSize, const BN_Fr *x);
+// return written byte if sucess else 0
+BN_DLL_API size_t BN_Fr_getLittleEndian(void *buf, size_t bufSize, const BN_Fr *x);
BN_DLL_API void BN_Fr_neg(BN_Fr *y, const BN_Fr *x);
BN_DLL_API void BN_Fr_inv(BN_Fr *y, const BN_Fr *x);
@@ -108,10 +127,9 @@ BN_DLL_API void BN_Fr_div(BN_Fr *z, const BN_Fr *x, const BN_Fr *y);
// set zero
BN_DLL_API void BN_G1_clear(BN_G1 *x);
-BN_DLL_API void BN_G1_copy(BN_G1 *y, const BN_G1 *x);
-
// return 0 if success
-BN_DLL_API int BN_G1_setStr(BN_G1 *x, const char *s);
+BN_DLL_API int BN_G1_setHexStr(BN_G1 *x, const char *s);
+BN_DLL_API int BN_G1_deserialize(BN_G1 *x, const char *buf, size_t bufSize);
// return 1 if true and 0 otherwise
BN_DLL_API int BN_G1_isValid(const BN_G1 *x);
@@ -121,7 +139,9 @@ BN_DLL_API int BN_G1_isZero(const BN_G1 *x);
BN_DLL_API int BN_hashAndMapToG1(BN_G1 *x, const void *buf, size_t bufSize);
// return 0 if success
-BN_DLL_API int BN_G1_getStr(char *buf, size_t maxBufSize, const BN_G1 *x);
+BN_DLL_API size_t BN_G1_getHexStr(char *buf, size_t maxBufSize, const BN_G1 *x);
+// return written size if sucess else 0
+BN_DLL_API size_t BN_G1_serialize(void *buf, size_t maxBufSize, const BN_G1 *x);
BN_DLL_API void BN_G1_neg(BN_G1 *y, const BN_G1 *x);
BN_DLL_API void BN_G1_dbl(BN_G1 *y, const BN_G1 *x);
@@ -133,10 +153,9 @@ BN_DLL_API void BN_G1_mul(BN_G1 *z, const BN_G1 *x, const BN_Fr *y);
// set zero
BN_DLL_API void BN_G2_clear(BN_G2 *x);
-BN_DLL_API void BN_G2_copy(BN_G2 *y, const BN_G2 *x);
-
// return 0 if success
-BN_DLL_API int BN_G2_setStr(BN_G2 *x, const char *s);
+BN_DLL_API int BN_G2_setHexStr(BN_G2 *x, const char *s);
+BN_DLL_API int BN_G2_deserialize(BN_G2 *x, const char *buf, size_t bufSize);
// return 1 if true and 0 otherwise
BN_DLL_API int BN_G2_isValid(const BN_G2 *x);
@@ -146,7 +165,9 @@ BN_DLL_API int BN_G2_isZero(const BN_G2 *x);
BN_DLL_API int BN_hashAndMapToG2(BN_G2 *x, const void *buf, size_t bufSize);
// return 0 if success
-BN_DLL_API int BN_G2_getStr(char *buf, size_t maxBufSize, const BN_G2 *x);
+BN_DLL_API size_t BN_G2_getHexStr(char *buf, size_t maxBufSize, const BN_G2 *x);
+// return written size if sucess else 0
+BN_DLL_API size_t BN_G2_serialize(void *buf, size_t maxBufSize, const BN_G2 *x);
BN_DLL_API void BN_G2_neg(BN_G2 *y, const BN_G2 *x);
BN_DLL_API void BN_G2_dbl(BN_G2 *y, const BN_G2 *x);
@@ -158,10 +179,10 @@ BN_DLL_API void BN_G2_mul(BN_G2 *z, const BN_G2 *x, const BN_Fr *y);
// set zero
BN_DLL_API void BN_GT_clear(BN_GT *x);
-BN_DLL_API void BN_GT_copy(BN_GT *y, const BN_GT *x);
-
// return 0 if success
-BN_DLL_API int BN_GT_setStr(BN_GT *x, const char *s);
+BN_DLL_API int BN_GT_setDecStr(BN_GT *x, const char *s);
+BN_DLL_API int BN_GT_setHexStr(BN_GT *x, const char *s);
+BN_DLL_API int BN_GT_deserialize(BN_GT *x, const char *buf, size_t bufSize);
// return 1 if true and 0 otherwise
BN_DLL_API int BN_GT_isEqual(const BN_GT *x, const BN_GT *y);
@@ -169,7 +190,10 @@ BN_DLL_API int BN_GT_isZero(const BN_GT *x);
BN_DLL_API int BN_GT_isOne(const BN_GT *x);
// return 0 if success
-BN_DLL_API int BN_GT_getStr(char *buf, size_t maxBufSize, const BN_GT *x);
+BN_DLL_API size_t BN_GT_getDecStr(char *buf, size_t maxBufSize, const BN_GT *x);
+BN_DLL_API size_t BN_GT_getHexStr(char *buf, size_t maxBufSize, const BN_GT *x);
+// return written size if sucess else 0
+BN_DLL_API size_t BN_GT_serialize(void *buf, size_t maxBufSize, const BN_GT *x);
BN_DLL_API void BN_GT_neg(BN_GT *y, const BN_GT *x);
BN_DLL_API void BN_GT_inv(BN_GT *y, const BN_GT *x);
@@ -178,10 +202,10 @@ BN_DLL_API void BN_GT_sub(BN_GT *z, const BN_GT *x, const BN_GT *y);
BN_DLL_API void BN_GT_mul(BN_GT *z, const BN_GT *x, const BN_GT *y);
BN_DLL_API void BN_GT_div(BN_GT *z, const BN_GT *x, const BN_GT *y);
-BN_DLL_API void BN_GT_finalExp(BN_GT *y, const BN_GT *x);
BN_DLL_API void BN_GT_pow(BN_GT *z, const BN_GT *x, const BN_Fr *y);
BN_DLL_API void BN_pairing(BN_GT *z, const BN_G1 *x, const BN_G2 *y);
+BN_DLL_API void BN_finalExp(BN_GT *y, const BN_GT *x);
BN_DLL_API void BN_millerLoop(BN_GT *z, const BN_G1 *x, const BN_G2 *y);
// return precomputedQcoeffSize * sizeof(Fp6) / sizeof(uint64_t)
diff --git a/src/bn_if.cpp b/src/bn_if.cpp
index 23f119e..3302619 100644
--- a/src/bn_if.cpp
+++ b/src/bn_if.cpp
@@ -45,6 +45,47 @@ static int closeErrFile()
return ret;
}
+template<class T>
+size_t serialize(void *buf, size_t maxBufSize, const T *x, int ioMode, const char *msg, bool convertToHex)
+ try
+{
+ std::string str;
+ cast(x)->getStr(str, ioMode);
+ if (convertToHex) {
+ str = mcl::fp::littleEndianToHexStr(str.c_str(), str.size());
+ }
+ size_t terminate = (convertToHex || ioMode == 10 || ioMode == 16) ? 1 : 0;
+ if (str.size() + terminate > maxBufSize) {
+ if (g_fp) fprintf(g_fp, "%s:serialize:small maxBufSize %d %d %d\n", msg, (int)maxBufSize, (int)str.size(), (int)terminate);
+ return 0;
+ }
+ memcpy(buf, str.c_str(), str.size());
+ if (terminate) {
+ ((char *)buf)[str.size()] = '\0';
+ }
+ return str.size();
+} catch (std::exception& e) {
+ if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
+ return 0;
+}
+
+template<class T>
+size_t deserialize(T *x, const void *buf, size_t bufSize, int ioMode, const char *msg, bool convertFromHex)
+ try
+{
+ std::string str;
+ if (convertFromHex) {
+ str = mcl::fp::hexStrToLittleEndian((const char *)buf, bufSize);
+ } else {
+ str.assign((const char *)buf, bufSize);
+ }
+ cast(x)->setStr(str, ioMode);
+ return 0;
+} catch (std::exception& e) {
+ if (g_fp) fprintf(g_fp, "%s %s\n", msg, e.what());
+ return -1;
+}
+
int BN_setErrFile(const char *name)
{
int ret = closeErrFile();
@@ -64,18 +105,39 @@ int BN_setErrFile(const char *name)
#endif
}
-int BN_init(void)
+int BN_init(int curve, int maxUnitSize)
try
{
+ if (maxUnitSize != BN_MAX_FP_UNIT_SIZE) {
+ if (g_fp) fprintf(g_fp, "BN_Init:maxUnitSize is mismatch %d %d\n", maxUnitSize, BN_MAX_FP_UNIT_SIZE);
+ return -1;
+ }
+ mcl::bn::CurveParam cp;
+ switch (curve) {
+ case BN_curveFp254BNb:
+ cp = mcl::bn::CurveFp254BNb;
+ break;
+#if BLS_MAX_OP_UNIT_SIZE == 6
+ case BN_curveFp382_1:
+ cp = mcl::bn::CurveFp382_1;
+ break;
+ case BN_curveFp382_2:
+ cp = mcl::bn::CurveFp382_2;
+ break;
+#endif
+ default:
+ if (g_fp) fprintf(g_fp, "BN_Init:not supported curve %d\n", curve);
+ return -1;
+ }
#if BN_MAX_FP_UNIT_SIZE == 4
- bn256init();
+ bn256init(cp);
#else
- bn384init();
+ bn384init(cp);
#endif
return 0;
} catch (std::exception& e) {
if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return -1;
}
////////////////////////////////////////////////
@@ -91,20 +153,21 @@ void BN_Fr_setInt(BN_Fr *y, int x)
*cast(y) = x;
}
-void BN_Fr_copy(BN_Fr *y, const BN_Fr *x)
+int BN_Fr_setDecStr(BN_Fr *x, const char *s)
{
- *cast(y) = *cast(x);
+ return deserialize(x, s, strlen(s), 10, "BN_Fr_setDecStr", false);
}
-
-// return 0 if success
-int BN_Fr_setStr(BN_Fr *x, const char *str)
- try
+int BN_Fr_setHexStr(BN_Fr *x, const char *s)
{
- cast(x)->setStr(str);
- return 0;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return deserialize(x, s, strlen(s), 16, "BN_Fr_setHexStr", false);
+}
+int BN_Fr_setLittleEndian(BN_Fr *x, const void *buf, size_t bufSize)
+{
+ const size_t byteSize = cast(x)->getByteSize();
+ if (bufSize > byteSize) bufSize = byteSize;
+ std::string s((const char *)buf, bufSize);
+ s.resize(byteSize);
+ return deserialize(x, s.c_str(), s.size(), mcl::IoFixedSizeByteSeq, "BN_Fr_setLittleEndian", false);
}
// return 1 if true
@@ -125,7 +188,7 @@ int BN_Fr_isOne(const BN_Fr *x)
return cast(x)->isOne();
}
-void BN_Fr_setRand(BN_Fr *x)
+void BN_Fr_setByCSPRNG(BN_Fr *x)
{
cast(x)->setRand(g_rg);
}
@@ -136,20 +199,17 @@ void BN_hashToFr(BN_Fr *x, const void *buf, size_t bufSize)
cast(x)->setHashOf(buf, bufSize);
}
-// return 0 if success
-int BN_Fr_getStr(char *buf, int maxBufSize, const BN_Fr *x)
- try
+size_t BN_Fr_getDecStr(char *buf, size_t maxBufSize, const BN_Fr *x)
{
- std::string str;
- cast(x)->getStr(str);
- if ((int)str.size() < maxBufSize) {
- memcpy(buf, str.c_str(), str.size() + 1);
- return 0;
- }
- return 1;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return serialize(buf, maxBufSize, x, 10, "BN_Fr_getDecStr", false);
+}
+size_t BN_Fr_getHexStr(char *buf, size_t maxBufSize, const BN_Fr *x)
+{
+ return serialize(buf, maxBufSize, x, 16, "BN_Fr_getHexStr", false);
+}
+size_t BN_Fr_getLittleEndian(void *buf, size_t maxBufSize, const BN_Fr *x)
+{
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_Fr_getLittleEndian", false);
}
void BN_Fr_neg(BN_Fr *y, const BN_Fr *x)
@@ -184,20 +244,13 @@ void BN_G1_clear(BN_G1 *x)
cast(x)->clear();
}
-void BN_G1_copy(BN_G1 *y, const BN_G1 *x)
+int BN_G1_setHexStr(BN_G1 *x, const char *s)
{
- *cast(y) = *cast(x);
+ return deserialize(x, s, strlen(s), mcl::IoFixedSizeByteSeq, "BN_G1_setHexStr", true);
}
-
-// return 0 if success
-int BN_G1_setStr(BN_G1 *x, const char *str)
- try
+int BN_G1_deserialize(BN_G1 *x, const char *buf, size_t bufSize)
{
- cast(x)->setStr(str);
- return 0;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return deserialize(x, buf, bufSize, mcl::IoFixedSizeByteSeq, "BN_G1_setHexStr", false);
}
// return 1 if true
@@ -222,24 +275,18 @@ int BN_hashAndMapToG1(BN_G1 *x, const void *buf, size_t bufSize)
BN::mapToG1(*cast(x), y);
return 0;
} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
+ if (g_fp) fprintf(g_fp, "BN_hashAndMapToG1 %s\n", e.what());
return 1;
}
-// return 0 if success
-int BN_G1_getStr(char *buf, size_t maxBufSize, const BN_G1 *x)
- try
+size_t BN_G1_getHexStr(char *buf, size_t maxBufSize, const BN_G1 *x)
{
- std::string str;
- cast(x)->getStr(str);
- if (str.size() < maxBufSize) {
- memcpy(buf, str.c_str(), str.size() + 1);
- return 0;
- }
- return 1;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_G1_getHexStr", true);
+}
+
+size_t BN_G1_serialize(void *buf, size_t maxBufSize, const BN_G1 *x)
+{
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_G1_serialize", false);
}
void BN_G1_neg(BN_G1 *y, const BN_G1 *x)
@@ -270,20 +317,13 @@ void BN_G2_clear(BN_G2 *x)
cast(x)->clear();
}
-void BN_G2_copy(BN_G2 *y, const BN_G2 *x)
+int BN_G2_setHexStr(BN_G2 *x, const char *s)
{
- *cast(y) = *cast(x);
+ return deserialize(x, s, strlen(s), mcl::IoFixedSizeByteSeq, "BN_G2_setHexStr", true);
}
-
-// return 0 if success
-int BN_G2_setStr(BN_G2 *x, const char *str)
- try
+int BN_G2_deserialize(BN_G2 *x, const char *buf, size_t bufSize)
{
- cast(x)->setStr(str);
- return 0;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return deserialize(x, buf, bufSize, mcl::IoFixedSizeByteSeq, "BN_G2_setHexStr", false);
}
// return 1 if true
@@ -308,24 +348,18 @@ int BN_hashAndMapToG2(BN_G2 *x, const void *buf, size_t bufSize)
BN::mapToG2(*cast(x), Fp2(y, 0));
return 0;
} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
+ if (g_fp) fprintf(g_fp, "BN_hashAndMapToG2 %s\n", e.what());
return 1;
}
-// return 0 if success
-int BN_G2_getStr(char *buf, size_t maxBufSize, const BN_G2 *x)
- try
+size_t BN_G2_getHexStr(char *buf, size_t maxBufSize, const BN_G2 *x)
{
- std::string str;
- cast(x)->getStr(str);
- if (str.size() < maxBufSize) {
- memcpy(buf, str.c_str(), str.size() + 1);
- return 0;
- }
- return 1;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_G2_getHexStr", true);
+}
+
+size_t BN_G2_serialize(void *buf, size_t maxBufSize, const BN_G2 *x)
+{
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_G2_serialize", false);
}
void BN_G2_neg(BN_G2 *y, const BN_G2 *x)
@@ -356,21 +390,17 @@ void BN_GT_clear(BN_GT *x)
cast(x)->clear();
}
-void BN_GT_copy(BN_GT *y, const BN_GT *x)
+int BN_GT_setDecStr(BN_GT *x, const char *s)
{
- *cast(y) = *cast(x);
+ return deserialize(x, s, strlen(s), 10, "BN_GT_setDecStr", false);
}
-
-// return 0 if success
-int BN_GT_setStr(BN_GT *x, const char *str)
- try
+int BN_GT_setHexStr(BN_GT *x, const char *s)
{
- std::istringstream is(str);
- is >> *cast(x);
- return is ? 0 : 1;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return deserialize(x, s, strlen(s), 16, "BN_GT_setHexStr", false);
+}
+int BN_GT_deserialize(BN_GT *x, const char *buf, size_t bufSize)
+{
+ return deserialize(x, buf, bufSize, mcl::IoFixedSizeByteSeq, "BN_GT_setHexStr", false);
}
// return 1 if true
@@ -387,19 +417,19 @@ int BN_GT_isOne(const BN_GT *x)
return cast(x)->isOne();
}
-// return 0 if success
-int BN_GT_getStr(char *buf, size_t maxBufSize, const BN_GT *x)
- try
+size_t BN_GT_getDecStr(char *buf, size_t maxBufSize, const BN_GT *x)
{
- std::string str = cast(x)->getStr();
- if (str.size() < maxBufSize) {
- memcpy(buf, str.c_str(), str.size() + 1);
- return 0;
- }
- return 1;
-} catch (std::exception& e) {
- if (g_fp) fprintf(g_fp, "%s\n", e.what());
- return 1;
+ return serialize(buf, maxBufSize, x, 10, "BN_GT_getDecStr", false);
+}
+
+size_t BN_GT_getHexStr(char *buf, size_t maxBufSize, const BN_GT *x)
+{
+ return serialize(buf, maxBufSize, x, 16, "BN_GT_getHexStr", false);
+}
+
+size_t BN_GT_serialize(void *buf, size_t maxBufSize, const BN_GT *x)
+{
+ return serialize(buf, maxBufSize, x, mcl::IoFixedSizeByteSeq, "BN_GT_serialize", false);
}
void BN_GT_neg(BN_GT *y, const BN_GT *x)
@@ -427,10 +457,6 @@ void BN_GT_div(BN_GT *z, const BN_GT *x, const BN_GT *y)
Fp12::div(*cast(z),*cast(x), *cast(y));
}
-void BN_GT_finalExp(BN_GT *y, const BN_GT *x)
-{
- BN::finalExp(*cast(y), *cast(x));
-}
void BN_GT_pow(BN_GT *z, const BN_GT *x, const BN_Fr *y)
{
Fp12::pow(*cast(z), *cast(x), *cast(y));
@@ -440,6 +466,10 @@ void BN_pairing(BN_GT *z, const BN_G1 *x, const BN_G2 *y)
{
BN::pairing(*cast(z), *cast(x), *cast(y));
}
+void BN_finalExp(BN_GT *y, const BN_GT *x)
+{
+ BN::finalExp(*cast(y), *cast(x));
+}
void BN_millerLoop(BN_GT *z, const BN_G1 *x, const BN_G2 *y)
{
BN::millerLoop(*cast(z), *cast(x), *cast(y));
diff --git a/test/bn_if256_test.cpp b/test/bn_if256_test.cpp
index 1d02af4..45690fb 100644
--- a/test/bn_if256_test.cpp
+++ b/test/bn_if256_test.cpp
@@ -31,7 +31,7 @@ CYBOZU_TEST_AUTO(init)
ret = BN_setErrFile("stderr");
CYBOZU_TEST_EQUAL(ret, 0);
- ret = BN_init();
+ ret = BN_init(BN_curveFp254BNb, BN_MAX_FP_UNIT_SIZE);
CYBOZU_TEST_EQUAL(ret, 0);
}
@@ -53,7 +53,7 @@ CYBOZU_TEST_AUTO(Fr)
BN_Fr_setInt(&y, -1);
CYBOZU_TEST_ASSERT(!BN_Fr_isEqual(&x, &y));
- BN_Fr_copy(&y, &x);
+ y = x;
CYBOZU_TEST_ASSERT(BN_Fr_isEqual(&x, &y));
BN_hashToFr(&x, "", 0);
@@ -64,42 +64,50 @@ CYBOZU_TEST_AUTO(Fr)
char buf[1024];
BN_Fr_setInt(&x, 12345678);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size_t size;
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 8);
CYBOZU_TEST_EQUAL(buf, "12345678");
BN_Fr_setInt(&x, -7654321);
BN_Fr_neg(&x, &x);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 7);
CYBOZU_TEST_EQUAL(buf, "7654321");
BN_Fr_setInt(&y, 123 - 7654321);
BN_Fr_add(&x, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 3);
CYBOZU_TEST_EQUAL(buf, "123");
BN_Fr_setInt(&y, 100);
BN_Fr_sub(&x, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 2);
CYBOZU_TEST_EQUAL(buf, "23");
BN_Fr_mul(&x, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 4);
CYBOZU_TEST_EQUAL(buf, "2300");
BN_Fr_div(&x, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 2);
CYBOZU_TEST_EQUAL(buf, "23");
- CYBOZU_TEST_ASSERT(!BN_Fr_setStr(&x, "12345678901234567"));
- CYBOZU_TEST_ASSERT(!BN_Fr_setStr(&y, "20000000000000000"));
+ CYBOZU_TEST_ASSERT(!BN_Fr_setDecStr(&x, "12345678901234567"));
+ CYBOZU_TEST_ASSERT(!BN_Fr_setDecStr(&y, "20000000000000000"));
BN_Fr_add(&x, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
+ size = BN_Fr_getDecStr(buf, sizeof(buf), &x);
+ CYBOZU_TEST_EQUAL(size, 17);
CYBOZU_TEST_EQUAL(buf, "32345678901234567");
BN_Fr_setInt(&x, 1);
BN_Fr_neg(&x, &x);
- CYBOZU_TEST_ASSERT(!BN_Fr_getStr(buf, sizeof(buf), &x));
- CYBOZU_TEST_ASSERT(!BN_Fr_setStr(&y, buf));
+ CYBOZU_TEST_ASSERT(BN_Fr_getDecStr(buf, sizeof(buf), &x) > 0);
+ CYBOZU_TEST_ASSERT(!BN_Fr_setDecStr(&y, buf));
CYBOZU_TEST_ASSERT(BN_Fr_isEqual(&x, &y));
}
@@ -110,27 +118,19 @@ CYBOZU_TEST_AUTO(G1)
CYBOZU_TEST_ASSERT(!BN_G1_isValid(&x));
BN_G1_clear(&x);
CYBOZU_TEST_ASSERT(BN_G1_isValid(&x));
- BN_G1_setStr(&y, "0");
CYBOZU_TEST_ASSERT(BN_G1_isZero(&x));
- CYBOZU_TEST_ASSERT(BN_G1_isZero(&y));
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&y, "1 -1 1")); // "1 <x> <y>"
- CYBOZU_TEST_ASSERT(!BN_G1_isZero(&y));
+
+ CYBOZU_TEST_ASSERT(!BN_hashAndMapToG1(&y, "abc", 3));
+
char buf[1024];
- CYBOZU_TEST_ASSERT(!BN_G1_getStr(buf, sizeof(buf), &y));
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, buf));
+ CYBOZU_TEST_ASSERT(BN_G1_getHexStr(buf, sizeof(buf), &x) > 0);
+ CYBOZU_TEST_ASSERT(!BN_G1_setHexStr(&y, buf));
CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &y));
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, "1 -1 -1")); // "1 <x> <y>"
- CYBOZU_TEST_ASSERT(!BN_G1_isZero(&x));
+ BN_G1_neg(&x, &x);
BN_G1_add(&x, &x, &y);
CYBOZU_TEST_ASSERT(BN_G1_isZero(&x));
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&x, "1 -1 -1")); // "1 <x> <y>"
- BN_G1_neg(&x, &x);
- CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &y));
-
- CYBOZU_TEST_ASSERT(!BN_hashAndMapToG1(&y, "abc", 3));
-
BN_G1_dbl(&x, &y); // x = 2y
BN_G1_add(&z, &y, &y);
CYBOZU_TEST_ASSERT(BN_G1_isEqual(&x, &z));
@@ -153,15 +153,13 @@ CYBOZU_TEST_AUTO(G2)
CYBOZU_TEST_ASSERT(!BN_G2_isValid(&x));
BN_G2_clear(&x);
CYBOZU_TEST_ASSERT(BN_G2_isValid(&x));
- BN_G2_setStr(&y, "0");
CYBOZU_TEST_ASSERT(BN_G2_isZero(&x));
- CYBOZU_TEST_ASSERT(BN_G2_isZero(&y));
CYBOZU_TEST_ASSERT(!BN_hashAndMapToG2(&x, "abc", 3));
char buf[1024];
- CYBOZU_TEST_ASSERT(!BN_G2_getStr(buf, sizeof(buf), &x));
- CYBOZU_TEST_ASSERT(!BN_G2_setStr(&y, buf));
+ CYBOZU_TEST_ASSERT(BN_G2_getHexStr(buf, sizeof(buf), &x) > 0);
+ CYBOZU_TEST_ASSERT(!BN_G2_setHexStr(&y, buf));
CYBOZU_TEST_ASSERT(BN_G2_isEqual(&x, &y));
BN_G2_neg(&x, &x);
@@ -193,16 +191,16 @@ CYBOZU_TEST_AUTO(GT)
CYBOZU_TEST_ASSERT(BN_GT_isZero(&x));
char buf[1024];
- CYBOZU_TEST_ASSERT(!BN_GT_setStr(&x, "1 2 3 4 5 6 7 8 9 10 11 12"));
- CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &x));
+ CYBOZU_TEST_ASSERT(!BN_GT_setDecStr(&x, "1 2 3 4 5 6 7 8 9 10 11 12"));
+ CYBOZU_TEST_ASSERT(BN_GT_getDecStr(buf, sizeof(buf), &x) > 0);
CYBOZU_TEST_EQUAL(buf, "1 2 3 4 5 6 7 8 9 10 11 12");
- BN_GT_copy(&y, &x);
+ y = x;
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &y));
- CYBOZU_TEST_ASSERT(!BN_GT_setStr(&z, "-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12"));
- CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &z));
- CYBOZU_TEST_ASSERT(!BN_GT_setStr(&y, buf));
+ CYBOZU_TEST_ASSERT(!BN_GT_setDecStr(&z, "-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12"));
+ CYBOZU_TEST_ASSERT(BN_GT_getDecStr(buf, sizeof(buf), &z) > 0);
+ CYBOZU_TEST_ASSERT(!BN_GT_setDecStr(&y, buf));
BN_GT_neg(&z, &y);
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &z));
@@ -210,13 +208,13 @@ CYBOZU_TEST_AUTO(GT)
BN_GT_add(&y, &x, &y);
CYBOZU_TEST_ASSERT(BN_GT_isZero(&y));
- CYBOZU_TEST_ASSERT(!BN_GT_setStr(&y, "2 0 0 0 0 0 0 0 0 0 0 0"));
+ CYBOZU_TEST_ASSERT(!BN_GT_setDecStr(&y, "2 0 0 0 0 0 0 0 0 0 0 0"));
BN_GT_mul(&z, &x, &y);
- CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &z));
+ CYBOZU_TEST_ASSERT(BN_GT_getDecStr(buf, sizeof(buf), &z) > 0);
CYBOZU_TEST_EQUAL(buf, "2 4 6 8 10 12 14 16 18 20 22 24");
BN_GT_div(&z, &z, &y);
- CYBOZU_TEST_ASSERT(!BN_GT_getStr(buf, sizeof(buf), &x));
+ CYBOZU_TEST_ASSERT(BN_GT_getDecStr(buf, sizeof(buf), &x) > 0);
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&x, &z));
BN_Fr n;
@@ -237,7 +235,7 @@ CYBOZU_TEST_AUTO(pairing)
BN_G2 Q, bQ;
BN_GT e, e1, e2;
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&P, "1 -1 1")); // "1 <x> <y>"
+ CYBOZU_TEST_ASSERT(!BN_hashAndMapToG1(&P, "1", 1));
CYBOZU_TEST_ASSERT(!BN_hashAndMapToG2(&Q, "1", 1));
BN_G1_mul(&aP, &P, &a);
@@ -257,7 +255,7 @@ CYBOZU_TEST_AUTO(precomputed)
{
BN_G1 P1, P2;
BN_G2 Q1, Q2;
- CYBOZU_TEST_ASSERT(!BN_G1_setStr(&P1, "1 -1 1")); // "1 <x> <y>"
+ CYBOZU_TEST_ASSERT(!BN_hashAndMapToG1(&P1, "1", 1));
CYBOZU_TEST_ASSERT(!BN_hashAndMapToG1(&P2, "123", 3));
CYBOZU_TEST_ASSERT(!BN_hashAndMapToG2(&Q1, "1", 1));
CYBOZU_TEST_ASSERT(!BN_hashAndMapToG2(&Q2, "2", 1));
@@ -272,16 +270,16 @@ CYBOZU_TEST_AUTO(precomputed)
BN_GT e1, e2, f1, f2, f3;
BN_pairing(&e1, &P1, &Q1);
BN_precomputedMillerLoop(&f1, &P1, Q1buf.data());
- BN_GT_finalExp(&f1, &f1);
+ BN_finalExp(&f1, &f1);
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &f1));
BN_pairing(&e2, &P2, &Q2);
BN_precomputedMillerLoop(&f2, &P2, Q2buf.data());
- BN_GT_finalExp(&f2, &f2);
+ BN_finalExp(&f2, &f2);
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e2, &f2));
BN_precomputedMillerLoop2(&f3, &P1, Q1buf.data(), &P2, Q2buf.data());
- BN_GT_finalExp(&f3, &f3);
+ BN_finalExp(&f3, &f3);
BN_GT_mul(&e1, &e1, &e2);
CYBOZU_TEST_ASSERT(BN_GT_isEqual(&e1, &f3));
@@ -289,6 +287,6 @@ CYBOZU_TEST_AUTO(precomputed)
CYBOZU_TEST_AUTO(end)
{
- int ret = BN_setErrFile("");
+ int ret = BN_setErrFile("bn_if.log");
CYBOZU_TEST_EQUAL(ret, 0);
}