aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMITSUNARI Shigeo <herumi@nifty.com>2019-01-31 11:16:12 +0800
committerMITSUNARI Shigeo <herumi@nifty.com>2019-01-31 11:16:12 +0800
commit5b4abee36ffde712e24ed30f59ff4fca7e0585e5 (patch)
treef2c4892f9ec4ac291b88c6d3d02c9f432f152a12
parent6ef8f3db38f6a60cfe6c091b9f6d6a888b2325e1 (diff)
downloadtangerine-mcl-5b4abee36ffde712e24ed30f59ff4fca7e0585e5.tar.gz
tangerine-mcl-5b4abee36ffde712e24ed30f59ff4fca7e0585e5.tar.zst
tangerine-mcl-5b4abee36ffde712e24ed30f59ff4fca7e0585e5.zip
add mclBnFp_mapToG1 and mclBnFp2_mapToG2
-rw-r--r--include/mcl/bn.h26
-rw-r--r--include/mcl/impl/bn_c_impl.hpp72
-rw-r--r--readme.md1
-rw-r--r--test/bn_c_test.hpp68
4 files changed, 167 insertions, 0 deletions
diff --git a/include/mcl/bn.h b/include/mcl/bn.h
index c130004..08174ce 100644
--- a/include/mcl/bn.h
+++ b/include/mcl/bn.h
@@ -68,6 +68,8 @@ typedef struct mclBnFr mclBnFr;
typedef struct mclBnG1 mclBnG1;
typedef struct mclBnG2 mclBnG2;
typedef struct mclBnGT mclBnGT;
+typedef struct mclBnFp mclBnFp;
+typedef struct mclBnFp2 mclBnFp2;
#else
@@ -87,6 +89,15 @@ typedef struct {
uint64_t d[MCLBN_FP_UNIT_SIZE * 12];
} mclBnGT;
+typedef struct {
+ uint64_t d[MCLBN_FP_UNIT_SIZE];
+} mclBnFp;
+
+typedef struct {
+ mclBnFp a;
+ mclBnFp b;
+} mclBnFp2;
+
#endif
#include <mcl/curve_type.h>
@@ -168,6 +179,8 @@ MCLBN_DLL_API mclSize mclBnFr_deserialize(mclBnFr *x, const void *buf, mclSize b
MCLBN_DLL_API mclSize mclBnG1_deserialize(mclBnG1 *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnG2_deserialize(mclBnG2 *x, const void *buf, mclSize bufSize);
MCLBN_DLL_API mclSize mclBnGT_deserialize(mclBnGT *x, const void *buf, mclSize bufSize);
+MCLBN_DLL_API mclSize mclBnFp_deserialize(mclBnFp *x, const void *buf, mclSize bufSize);
+MCLBN_DLL_API mclSize mclBnFp2_deserialize(mclBnFp2 *x, const void *buf, mclSize bufSize);
/*
serialize
@@ -177,6 +190,8 @@ MCLBN_DLL_API mclSize mclBnFr_serialize(void *buf, mclSize maxBufSize, const mcl
MCLBN_DLL_API mclSize mclBnG1_serialize(void *buf, mclSize maxBufSize, const mclBnG1 *x);
MCLBN_DLL_API mclSize mclBnG2_serialize(void *buf, mclSize maxBufSize, const mclBnG2 *x);
MCLBN_DLL_API mclSize mclBnGT_serialize(void *buf, mclSize maxBufSize, const mclBnGT *x);
+MCLBN_DLL_API mclSize mclBnFp_serialize(void *buf, mclSize maxBufSize, const mclBnFp *x);
+MCLBN_DLL_API mclSize mclBnFp2_serialize(void *buf, mclSize maxBufSize, const mclBnFp2 *x);
/*
set string
@@ -202,6 +217,8 @@ MCLBN_DLL_API mclSize mclBnGT_getStr(char *buf, mclSize maxBufSize, const mclBnG
// set zero
MCLBN_DLL_API void mclBnFr_clear(mclBnFr *x);
+MCLBN_DLL_API void mclBnFp_clear(mclBnFp *x);
+MCLBN_DLL_API void mclBnFp2_clear(mclBnFp2 *x);
// set x to y
MCLBN_DLL_API void mclBnFr_setInt(mclBnFr *y, mclInt x);
@@ -209,6 +226,7 @@ MCLBN_DLL_API void mclBnFr_setInt32(mclBnFr *y, int x);
// mask buf with (1 << (bitLen(r) - 1)) - 1 if buf >= r
MCLBN_DLL_API int mclBnFr_setLittleEndian(mclBnFr *x, const void *buf, mclSize bufSize);
+MCLBN_DLL_API int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize);
// return 1 if true and 0 otherwise
MCLBN_DLL_API int mclBnFr_isValid(const mclBnFr *x);
@@ -216,6 +234,9 @@ MCLBN_DLL_API int mclBnFr_isEqual(const mclBnFr *x, const mclBnFr *y);
MCLBN_DLL_API int mclBnFr_isZero(const mclBnFr *x);
MCLBN_DLL_API int mclBnFr_isOne(const mclBnFr *x);
+MCLBN_DLL_API int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y);
+MCLBN_DLL_API int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y);
+
#ifndef MCL_DONT_USE_CSRPNG
// return 0 if success
MCLBN_DLL_API int mclBnFr_setByCSPRNG(mclBnFr *x);
@@ -234,7 +255,12 @@ MCLBN_DLL_API void mclBn_setRandFunc(void *self, unsigned int (*readFunc)(void *
// hash(s) and set x
// return 0 if success
MCLBN_DLL_API int mclBnFr_setHashOf(mclBnFr *x, const void *buf, mclSize bufSize);
+MCLBN_DLL_API int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize);
+// map x to y
+// return 0 if success else -1
+MCLBN_DLL_API int mclBnFp_mapToG1(mclBnG1 *y, const mclBnFp *x);
+MCLBN_DLL_API int mclBnFp2_mapToG2(mclBnG2 *y, const mclBnFp2 *x);
MCLBN_DLL_API void mclBnFr_neg(mclBnFr *y, const mclBnFr *x);
MCLBN_DLL_API void mclBnFr_inv(mclBnFr *y, const mclBnFr *x);
diff --git a/include/mcl/impl/bn_c_impl.hpp b/include/mcl/impl/bn_c_impl.hpp
index 7d92fe6..acf096b 100644
--- a/include/mcl/impl/bn_c_impl.hpp
+++ b/include/mcl/impl/bn_c_impl.hpp
@@ -34,6 +34,12 @@ 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 Fp2 *cast(mclBnFp2 *p) { return reinterpret_cast<Fp2*>(p); }
+static const Fp2 *cast(const mclBnFp2 *p) { return reinterpret_cast<const Fp2*>(p); }
+
+static Fp *cast(mclBnFp *p) { return reinterpret_cast<Fp*>(p); }
+static const Fp *cast(const mclBnFp *p) { return reinterpret_cast<const Fp*>(p); }
+
template<class T>
int setStr(T *x, const char *buf, mclSize bufSize, int ioMode)
{
@@ -525,3 +531,69 @@ void mclBn_verifyOrderG2(int doVerify)
verifyOrderG2(doVerify != 0);
}
+mclSize mclBnFp_deserialize(mclBnFp *x, const void *buf, mclSize bufSize)
+{
+ return (mclSize)cast(x)->deserialize(buf, bufSize);
+}
+
+mclSize mclBnFp_serialize(void *buf, mclSize maxBufSize, const mclBnFp *x)
+{
+ return (mclSize)cast(x)->serialize(buf, maxBufSize);
+}
+
+void mclBnFp_clear(mclBnFp *x)
+{
+ cast(x)->clear();
+}
+
+int mclBnFp_setLittleEndian(mclBnFp *x, const void *buf, mclSize bufSize)
+{
+ cast(x)->setArrayMask((const char *)buf, bufSize);
+ return 0;
+}
+
+int mclBnFp_isEqual(const mclBnFp *x, const mclBnFp *y)
+{
+ return *cast(x) == *cast(y);
+}
+
+int mclBnFp_setHashOf(mclBnFp *x, const void *buf, mclSize bufSize)
+{
+ cast(x)->setHashOf(buf, bufSize);
+ return 0;
+}
+
+int mclBnFp_mapToG1(mclBnG1 *y, const mclBnFp *x)
+{
+ bool b;
+ mapToG1(&b, *cast(y), *cast(x));
+ return b ? 0 : -1;
+}
+
+mclSize mclBnFp2_deserialize(mclBnFp2 *x, const void *buf, mclSize bufSize)
+{
+ return (mclSize)cast(x)->deserialize(buf, bufSize);
+}
+
+mclSize mclBnFp2_serialize(void *buf, mclSize maxBufSize, const mclBnFp2 *x)
+{
+ return (mclSize)cast(x)->serialize(buf, maxBufSize);
+}
+
+void mclBnFp2_clear(mclBnFp2 *x)
+{
+ cast(x)->clear();
+}
+
+int mclBnFp2_isEqual(const mclBnFp2 *x, const mclBnFp2 *y)
+{
+ return *cast(x) == *cast(y);
+}
+
+int mclBnFp2_mapToG2(mclBnG2 *y, const mclBnFp2 *x)
+{
+ bool b;
+ mapToG2(&b, *cast(y), *cast(x));
+ return b ? 0 : -1;
+}
+
diff --git a/readme.md b/readme.md
index 44489d0..2ca42d3 100644
--- a/readme.md
+++ b/readme.md
@@ -405,6 +405,7 @@ Y. Sakemi, Y. Nogami, K. Okeya, Y. Morikawa, CANS 2008.
# History
+* 2019/Jan/31 add mclBnFp_mapToG1, mclBnFp2_mapToG2
* 2019/Jan/31 fix crash on x64-CPU without AVX
# Author
diff --git a/test/bn_c_test.hpp b/test/bn_c_test.hpp
index ba4dcf8..f4fc79a 100644
--- a/test/bn_c_test.hpp
+++ b/test/bn_c_test.hpp
@@ -560,3 +560,71 @@ CYBOZU_TEST_AUTO(setRandFunc)
}
}
}
+
+CYBOZU_TEST_AUTO(Fp)
+{
+ mclBnFp x1, x2;
+ char buf[1024];
+ int ret = mclBnFp_setHashOf(&x1, "abc", 3);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ mclSize n = mclBnFp_serialize(buf, sizeof(buf), &x1);
+ CYBOZU_TEST_ASSERT(n > 0);
+ n = mclBnFp_deserialize(&x2, buf, n);
+ CYBOZU_TEST_ASSERT(n > 0);
+ CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x1, &x2));
+ for (size_t i = 0; i < n; i++) {
+ buf[i] = char(i);
+ }
+ ret = mclBnFp_setLittleEndian(&x1, buf, n);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ memset(buf, 0, sizeof(buf));
+ n = mclBnFp_serialize(buf, sizeof(buf), &x1);
+ CYBOZU_TEST_ASSERT(n > 0);
+ for (size_t i = 0; i < n - 1; i++) {
+ CYBOZU_TEST_EQUAL(buf[i], char(i));
+ }
+ mclBnFp_clear(&x1);
+ memset(&x2, 0, sizeof(x2));
+ CYBOZU_TEST_ASSERT(mclBnFp_isEqual(&x1, &x2));
+}
+
+CYBOZU_TEST_AUTO(Fp2)
+{
+ mclBnFp2 x1, x2;
+ char buf[1024];
+ int ret = mclBnFp_setHashOf(&x1.a, "abc", 3);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ ret = mclBnFp_setHashOf(&x1.b, "xyz", 3);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ mclSize n = mclBnFp2_serialize(buf, sizeof(buf), &x1);
+ CYBOZU_TEST_ASSERT(n > 0);
+ n = mclBnFp2_deserialize(&x2, buf, n);
+ CYBOZU_TEST_ASSERT(n > 0);
+ CYBOZU_TEST_ASSERT(mclBnFp2_isEqual(&x1, &x2));
+ mclBnFp2_clear(&x1);
+ memset(&x2, 0, sizeof(x2));
+ CYBOZU_TEST_ASSERT(mclBnFp2_isEqual(&x1, &x2));
+}
+
+CYBOZU_TEST_AUTO(mapToG1)
+{
+ mclBnFp x;
+ mclBnG1 P1, P2;
+ mclBnFp_setHashOf(&x, "abc", 3);
+ int ret = mclBnFp_mapToG1(&P1, &x);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ mclBnG1_hashAndMapTo(&P2, "abc", 3);
+ CYBOZU_TEST_ASSERT(mclBnG1_isEqual(&P1, &P2));
+}
+
+CYBOZU_TEST_AUTO(mapToG2)
+{
+ mclBnFp2 x;
+ mclBnG2 P1, P2;
+ mclBnFp_setHashOf(&x.a, "abc", 3);
+ mclBnFp_clear(&x.b);
+ int ret = mclBnFp2_mapToG2(&P1, &x);
+ CYBOZU_TEST_ASSERT(ret == 0);
+ mclBnG2_hashAndMapTo(&P2, "abc", 3);
+ CYBOZU_TEST_ASSERT(mclBnG2_isEqual(&P1, &P2));
+}